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

@ -47,7 +47,7 @@ public class ApiGateway {
*/
@RequestMapping(path = "/desktop", method = RequestMethod.GET)
public DesktopProduct getProductDesktop() {
DesktopProduct desktopProduct = new DesktopProduct();
var desktopProduct = new DesktopProduct();
desktopProduct.setImagePath(imageClient.getImagePath());
desktopProduct.setPrice(priceClient.getPrice());
return desktopProduct;
@ -60,7 +60,7 @@ public class ApiGateway {
*/
@RequestMapping(path = "/mobile", method = RequestMethod.GET)
public MobileProduct getProductMobile() {
MobileProduct mobileProduct = new MobileProduct();
var mobileProduct = new MobileProduct();
mobileProduct.setPrice(priceClient.getPrice());
return mobileProduct;
}

View File

@ -27,7 +27,6 @@ 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.springframework.stereotype.Component;
@ -43,21 +42,19 @@ public class ImageClientImpl implements ImageClient {
*/
@Override
public String getImagePath() {
String response = null;
HttpClient httpClient = HttpClient.newHttpClient();
HttpRequest httpGet =
HttpRequest.newBuilder().GET().uri(URI.create("http://localhost:50005/image-path")).build();
var httpClient = HttpClient.newHttpClient();
var httpGet = HttpRequest.newBuilder()
.GET()
.uri(URI.create("http://localhost:50005/image-path"))
.build();
try {
HttpResponse<String> httpResponse = httpClient.send(httpGet, BodyHandlers.ofString());
response = httpResponse.body();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
var httpResponse = httpClient.send(httpGet, BodyHandlers.ofString());
return httpResponse.body();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
return response;
return null;
}
}

View File

@ -27,7 +27,6 @@ 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.springframework.stereotype.Component;
@ -43,22 +42,19 @@ public class PriceClientImpl implements PriceClient {
*/
@Override
public String getPrice() {
String response = null;
HttpClient httpClient = HttpClient.newHttpClient();
HttpRequest httpGet =
HttpRequest.newBuilder().GET().uri(URI.create("http://localhost:50006/price")).build();
var httpClient = HttpClient.newHttpClient();
var httpGet = HttpRequest.newBuilder()
.GET()
.uri(URI.create("http://localhost:50006/price"))
.build();
try {
HttpResponse<String> httpResponse = httpClient.send(httpGet, BodyHandlers.ofString());
response = httpResponse.body();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
var httpResponse = httpClient.send(httpGet, BodyHandlers.ofString());
return httpResponse.body();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
return response;
return null;
}
}

View File

@ -23,15 +23,15 @@
package com.iluwatar.api.gateway;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when;
/**
* Test API Gateway Pattern
*/
@ -56,12 +56,12 @@ public class ApiGatewayTest {
*/
@Test
public void testGetProductDesktop() {
String imagePath = "/product-image.png";
String price = "20";
var imagePath = "/product-image.png";
var price = "20";
when(imageClient.getImagePath()).thenReturn(imagePath);
when(priceClient.getPrice()).thenReturn(price);
DesktopProduct desktopProduct = apiGateway.getProductDesktop();
var desktopProduct = apiGateway.getProductDesktop();
assertEquals(price, desktopProduct.getPrice());
assertEquals(imagePath, desktopProduct.getImagePath());
@ -72,10 +72,10 @@ public class ApiGatewayTest {
*/
@Test
public void testGetProductMobile() {
String price = "20";
var price = "20";
when(priceClient.getPrice()).thenReturn(price);
MobileProduct mobileProduct = apiGateway.getProductMobile();
var mobileProduct = apiGateway.getProductMobile();
assertEquals(price, mobileProduct.getPrice());
}

View File

@ -23,20 +23,18 @@
package com.iluwatar.image.microservice;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
/**
* Test for Image Rest Controller
*/
public class ImageControllerTest {
@Test
public void testGetImagePath() {
ImageController imageController = new ImageController();
String imagePath = imageController.getImagePath();
var imageController = new ImageController();
var imagePath = imageController.getImagePath();
assertEquals("/product-image.png", imagePath);
}
}

View File

@ -23,20 +23,18 @@
package com.iluwatar.price.microservice;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
/**
* Test for Price Rest Controller
*/
public class PriceControllerTest {
@Test
public void testgetPrice() {
PriceController priceController = new PriceController();
String price = priceController.getPrice();
var priceController = new PriceController();
var price = priceController.getPrice();
assertEquals("20", price);
}
}