📍Use lombok, reformat, and optimize the code (#1560)
* Use lombok, reformat, and optimize the code * Fix merge conflicts and some sonar issues Co-authored-by: va1m <va1m@email.com>
This commit is contained in:
@ -24,8 +24,7 @@
|
||||
package com.iluwatar.api.gateway;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
@ -45,7 +44,7 @@ public class ApiGateway {
|
||||
*
|
||||
* @return Product information for clients on a desktop
|
||||
*/
|
||||
@RequestMapping(path = "/desktop", method = RequestMethod.GET)
|
||||
@GetMapping("/desktop")
|
||||
public DesktopProduct getProductDesktop() {
|
||||
var desktopProduct = new DesktopProduct();
|
||||
desktopProduct.setImagePath(imageClient.getImagePath());
|
||||
@ -58,7 +57,7 @@ public class ApiGateway {
|
||||
*
|
||||
* @return Product information for clients on a mobile device
|
||||
*/
|
||||
@RequestMapping(path = "/mobile", method = RequestMethod.GET)
|
||||
@GetMapping("/mobile")
|
||||
public MobileProduct getProductMobile() {
|
||||
var mobileProduct = new MobileProduct();
|
||||
mobileProduct.setPrice(priceClient.getPrice());
|
||||
|
@ -23,10 +23,16 @@
|
||||
|
||||
package com.iluwatar.api.gateway;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
* Encapsulates all of the information that a desktop client needs to display a product.
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
public class DesktopProduct {
|
||||
|
||||
/**
|
||||
* The price of the product.
|
||||
*/
|
||||
@ -37,19 +43,4 @@ public class DesktopProduct {
|
||||
*/
|
||||
private String imagePath;
|
||||
|
||||
public String getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(String price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public String getImagePath() {
|
||||
return imagePath;
|
||||
}
|
||||
|
||||
public void setImagePath(String imagePath) {
|
||||
this.imagePath = imagePath;
|
||||
}
|
||||
}
|
||||
|
@ -23,24 +23,21 @@
|
||||
|
||||
package com.iluwatar.api.gateway;
|
||||
|
||||
import static org.slf4j.LoggerFactory.getLogger;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.net.http.HttpResponse.BodyHandlers;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* An adapter to communicate with the Image microservice.
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class ImageClientImpl implements ImageClient {
|
||||
private static final Logger LOGGER = getLogger(ImageClientImpl.class);
|
||||
|
||||
/**
|
||||
* Makes a simple HTTP Get request to the Image microservice.
|
||||
@ -60,8 +57,11 @@ public class ImageClientImpl implements ImageClient {
|
||||
var httpResponse = httpClient.send(httpGet, BodyHandlers.ofString());
|
||||
logResponse(httpResponse);
|
||||
return httpResponse.body();
|
||||
} catch (IOException | InterruptedException e) {
|
||||
LOGGER.error("Failure occurred while getting image path", e);
|
||||
} catch (IOException ioe) {
|
||||
LOGGER.error("Failure occurred while getting image path", ioe);
|
||||
} catch (InterruptedException ie) {
|
||||
LOGGER.error("Failure occurred while getting image path", ie);
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
|
||||
return null;
|
||||
|
@ -23,20 +23,17 @@
|
||||
|
||||
package com.iluwatar.api.gateway;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
* Encapsulates all of the information that mobile client needs to display a product.
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
public class MobileProduct {
|
||||
/**
|
||||
* The price of the product.
|
||||
*/
|
||||
private String price;
|
||||
|
||||
public String getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(String price) {
|
||||
this.price = price;
|
||||
}
|
||||
}
|
||||
|
@ -23,25 +23,22 @@
|
||||
|
||||
package com.iluwatar.api.gateway;
|
||||
|
||||
import static org.slf4j.LoggerFactory.getLogger;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.net.http.HttpResponse.BodyHandlers;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
|
||||
/**
|
||||
* An adapter to communicate with the Price microservice.
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class PriceClientImpl implements PriceClient {
|
||||
private static final Logger LOGGER = getLogger(PriceClientImpl.class);
|
||||
|
||||
/**
|
||||
* Makes a simple HTTP Get request to the Price microservice.
|
||||
@ -61,8 +58,11 @@ public class PriceClientImpl implements PriceClient {
|
||||
var httpResponse = httpClient.send(httpGet, BodyHandlers.ofString());
|
||||
logResponse(httpResponse);
|
||||
return httpResponse.body();
|
||||
} catch (IOException | InterruptedException e) {
|
||||
} catch (IOException e) {
|
||||
LOGGER.error("Failure occurred while getting price info", e);
|
||||
} catch (InterruptedException e) {
|
||||
LOGGER.error("Failure occurred while getting price info", e);
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
|
||||
return null;
|
||||
|
@ -35,7 +35,7 @@ import org.mockito.MockitoAnnotations;
|
||||
/**
|
||||
* Test API Gateway Pattern
|
||||
*/
|
||||
public class ApiGatewayTest {
|
||||
class ApiGatewayTest {
|
||||
|
||||
@InjectMocks
|
||||
private ApiGateway apiGateway;
|
||||
@ -55,7 +55,7 @@ public class ApiGatewayTest {
|
||||
* Tests getting the data for a desktop client
|
||||
*/
|
||||
@Test
|
||||
public void testGetProductDesktop() {
|
||||
void testGetProductDesktop() {
|
||||
var imagePath = "/product-image.png";
|
||||
var price = "20";
|
||||
when(imageClient.getImagePath()).thenReturn(imagePath);
|
||||
@ -71,7 +71,7 @@ public class ApiGatewayTest {
|
||||
* Tests getting the data for a mobile client
|
||||
*/
|
||||
@Test
|
||||
public void testGetProductMobile() {
|
||||
void testGetProductMobile() {
|
||||
var price = "20";
|
||||
when(priceClient.getPrice()).thenReturn(price);
|
||||
|
||||
|
@ -23,27 +23,24 @@
|
||||
|
||||
package com.iluwatar.image.microservice;
|
||||
|
||||
import static org.slf4j.LoggerFactory.getLogger;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
|
||||
/**
|
||||
* Exposes the Image microservice's endpoints.
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
public class ImageController {
|
||||
private static final Logger LOGGER = getLogger(ImageController.class);
|
||||
|
||||
/**
|
||||
* An endpoint for a user to retrieve an image path.
|
||||
*
|
||||
* @return An image path
|
||||
*/
|
||||
@RequestMapping(value = "/image-path", method = RequestMethod.GET)
|
||||
@GetMapping("/image-path")
|
||||
public String getImagePath() {
|
||||
LOGGER.info("Successfully found image path");
|
||||
return "/product-image.png";
|
||||
|
@ -23,16 +23,17 @@
|
||||
|
||||
package com.iluwatar.image.microservice;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* Test for Image Rest Controller
|
||||
*/
|
||||
public class ImageControllerTest {
|
||||
class ImageControllerTest {
|
||||
|
||||
@Test
|
||||
public void testGetImagePath() {
|
||||
void testGetImagePath() {
|
||||
var imageController = new ImageController();
|
||||
var imagePath = imageController.getImagePath();
|
||||
assertEquals("/product-image.png", imagePath);
|
||||
|
@ -23,27 +23,24 @@
|
||||
|
||||
package com.iluwatar.price.microservice;
|
||||
|
||||
import static org.slf4j.LoggerFactory.getLogger;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
|
||||
/**
|
||||
* Exposes the Price microservice's endpoints.
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
public class PriceController {
|
||||
private static final Logger LOGGER = getLogger(PriceController.class);
|
||||
|
||||
/**
|
||||
* An endpoint for a user to retrieve a product's price.
|
||||
*
|
||||
* @return A product's price
|
||||
*/
|
||||
@RequestMapping(value = "/price", method = RequestMethod.GET)
|
||||
@GetMapping("/price")
|
||||
public String getPrice() {
|
||||
LOGGER.info("Successfully found price info");
|
||||
return "20";
|
||||
|
@ -23,16 +23,17 @@
|
||||
|
||||
package com.iluwatar.price.microservice;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* Test for Price Rest Controller
|
||||
*/
|
||||
public class PriceControllerTest {
|
||||
class PriceControllerTest {
|
||||
|
||||
@Test
|
||||
public void testgetPrice() {
|
||||
void testgetPrice() {
|
||||
var priceController = new PriceController();
|
||||
var price = priceController.getPrice();
|
||||
assertEquals("20", price);
|
||||
|
Reference in New Issue
Block a user