Java 11 migration: patterns starting with a (#1084)
* Moves abstract-factory pattern to java 11 * Moves abstract-document pattern to java 11 * Moves acyclic-visitor pattern to java 11 * Moves adapter pattern to java 11 * Moves aggregator-microservices pattern to java 11 * Moves api-gateway pattern to java 11
This commit is contained in:
committed by
Ilkka Seppälä
parent
3c57bf7078
commit
f04fc3c0dc
@ -23,6 +23,8 @@
|
||||
|
||||
package com.iluwatar.aggregator.microservices;
|
||||
|
||||
import static java.util.Objects.requireNonNullElse;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
@ -52,20 +54,14 @@ public class Aggregator {
|
||||
public Product getProduct() {
|
||||
|
||||
var product = new Product();
|
||||
String productTitle = informationClient.getProductTitle();
|
||||
Integer productInventory = inventoryClient.getProductInventories();
|
||||
var productTitle = informationClient.getProductTitle();
|
||||
var productInventory = inventoryClient.getProductInventories();
|
||||
|
||||
if (productTitle != null) {
|
||||
product.setTitle(productTitle);
|
||||
} else {
|
||||
product.setTitle("Error: Fetching Product Title Failed"); //Fallback to error message
|
||||
}
|
||||
//Fallback to error message
|
||||
product.setTitle(requireNonNullElse(productTitle, "Error: Fetching Product Title Failed"));
|
||||
|
||||
if (productInventory != null) {
|
||||
product.setProductInventories(productInventory);
|
||||
} else {
|
||||
product.setProductInventories(-1); //Fallback to default error inventory
|
||||
}
|
||||
//Fallback to default error inventory
|
||||
product.setProductInventories(requireNonNullElse(productInventory, -1));
|
||||
|
||||
return product;
|
||||
}
|
||||
|
@ -42,19 +42,19 @@ public class ProductInformationClientImpl implements ProductInformationClient {
|
||||
|
||||
@Override
|
||||
public String getProductTitle() {
|
||||
String response = null;
|
||||
var request =
|
||||
HttpRequest.newBuilder().GET().uri(URI.create("http://localhost:51515/information"))
|
||||
.build();
|
||||
var request = HttpRequest.newBuilder()
|
||||
.GET()
|
||||
.uri(URI.create("http://localhost:51515/information"))
|
||||
.build();
|
||||
var client = HttpClient.newHttpClient();
|
||||
try {
|
||||
var httpResponse = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
response = httpResponse.body();
|
||||
return httpResponse.body();
|
||||
} catch (IOException ioe) {
|
||||
LOGGER.error("IOException Occurred", ioe);
|
||||
} catch (InterruptedException ie) {
|
||||
LOGGER.error("InterruptedException Occurred", ie);
|
||||
}
|
||||
return response;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -44,9 +44,10 @@ public class ProductInventoryClientImpl implements ProductInventoryClient {
|
||||
public Integer getProductInventories() {
|
||||
var response = "";
|
||||
|
||||
var request =
|
||||
HttpRequest.newBuilder().GET().uri(URI.create("http://localhost:51516/inventories"))
|
||||
.build();
|
||||
var request = HttpRequest.newBuilder()
|
||||
.GET()
|
||||
.uri(URI.create("http://localhost:51516/inventories"))
|
||||
.build();
|
||||
var client = HttpClient.newHttpClient();
|
||||
try {
|
||||
var httpResponse = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
|
@ -56,13 +56,13 @@ public class AggregatorTest {
|
||||
*/
|
||||
@Test
|
||||
public void testGetProduct() {
|
||||
String title = "The Product Title.";
|
||||
int inventories = 5;
|
||||
var title = "The Product Title.";
|
||||
var inventories = 5;
|
||||
|
||||
when(informationClient.getProductTitle()).thenReturn(title);
|
||||
when(inventoryClient.getProductInventories()).thenReturn(inventories);
|
||||
|
||||
Product testProduct = aggregator.getProduct();
|
||||
var testProduct = aggregator.getProduct();
|
||||
|
||||
assertEquals(title, testProduct.getTitle());
|
||||
assertEquals(inventories, testProduct.getProductInventories());
|
||||
|
Reference in New Issue
Block a user