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:
Anurag Agarwal
2019-11-13 21:34:51 +05:30
committed by Ilkka Seppälä
parent 3c57bf7078
commit f04fc3c0dc
26 changed files with 151 additions and 172 deletions

View File

@ -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;
}

View File

@ -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;
}
}

View File

@ -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());

View File

@ -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());

View File

@ -34,10 +34,8 @@ public class InformationControllerTest {
@Test
public void shouldGetProductTitle() {
InformationController infoController = new InformationController();
String title = infoController.getProductTitle();
var infoController = new InformationController();
var title = infoController.getProductTitle();
assertEquals("The Product Title.", title);
}

View File

@ -33,10 +33,8 @@ import org.junit.jupiter.api.Test;
public class InventoryControllerTest {
@Test
public void testGetProductInventories() {
InventoryController inventoryController = new InventoryController();
int numberOfInventories = inventoryController.getProductInventories();
var inventoryController = new InventoryController();
var numberOfInventories = inventoryController.getProductInventories();
assertEquals(5, numberOfInventories);
}
}