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,10 +23,10 @@
package com.iluwatar.abstractdocument; package com.iluwatar.abstractdocument;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -55,9 +55,13 @@ public abstract class AbstractDocument implements Document {
@Override @Override
public <T> Stream<T> children(String key, Function<Map<String, Object>, T> constructor) { public <T> Stream<T> children(String key, Function<Map<String, Object>, T> constructor) {
Optional<List<Map<String, Object>>> any = Stream.of(get(key)).filter(Objects::nonNull) return Stream.ofNullable(get(key))
.map(el -> (List<Map<String, Object>>) el).findAny(); .filter(Objects::nonNull)
return any.map(maps -> maps.stream().map(constructor)).orElseGet(Stream::empty); .map(el -> (List<Map<String, Object>>) el)
.findAny()
.stream()
.flatMap(Collection::stream)
.map(constructor);
} }
@Override @Override

View File

@ -66,11 +66,14 @@ public class App {
var car = new Car(carProperties); var car = new Car(carProperties);
LOGGER.info("Here is our car:"); LOGGER.info("Here is our car:");
LOGGER.info("-> model: {}", car.getModel().get()); LOGGER.info("-> model: {}", car.getModel().orElseThrow());
LOGGER.info("-> price: {}", car.getPrice().get()); LOGGER.info("-> price: {}", car.getPrice().orElseThrow());
LOGGER.info("-> parts: "); LOGGER.info("-> parts: ");
car.getParts().forEach(p -> LOGGER car.getParts().forEach(p -> LOGGER.info("\t{}/{}/{}",
.info("\t{}/{}/{}", p.getType().get(), p.getModel().get(), p.getPrice().get())); p.getType().orElse(null),
p.getModel().orElse(null),
p.getPrice().orElse(null))
);
} }
/** /**

View File

@ -23,14 +23,14 @@
package com.iluwatar.abstractdocument; package com.iluwatar.abstractdocument;
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Stream; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
/** /**
* AbstractDocument test class * AbstractDocument test class
@ -61,22 +61,22 @@ public class AbstractDocumentTest {
document.put(KEY, children); document.put(KEY, children);
Stream<DocumentImplementation> childrenStream = document.children(KEY, DocumentImplementation::new); var childrenStream = document.children(KEY, DocumentImplementation::new);
assertNotNull(children); assertNotNull(children);
assertEquals(2, childrenStream.count()); assertEquals(2, childrenStream.count());
} }
@Test @Test
public void shouldRetrieveEmptyStreamForNonExistingChildren() { public void shouldRetrieveEmptyStreamForNonExistingChildren() {
Stream<DocumentImplementation> children = document.children(KEY, DocumentImplementation::new); var children = document.children(KEY, DocumentImplementation::new);
assertNotNull(children); assertNotNull(children);
assertEquals(0, children.count()); assertEquals(0, children.count());
} }
@Test @Test
public void shouldIncludePropsInToString() { public void shouldIncludePropsInToString() {
Map<String, Object> props = Map.of(KEY, VALUE); var props = Map.of(KEY, (Object) VALUE);
DocumentImplementation document = new DocumentImplementation(props); var document = new DocumentImplementation(props);
assertTrue(document.toString().contains(KEY)); assertTrue(document.toString().contains(KEY));
assertTrue(document.toString().contains(VALUE)); assertTrue(document.toString().contains(VALUE));
} }

View File

@ -23,15 +23,14 @@
package com.iluwatar.abstractdocument; package com.iluwatar.abstractdocument;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.iluwatar.abstractdocument.domain.Car; import com.iluwatar.abstractdocument.domain.Car;
import com.iluwatar.abstractdocument.domain.Part; import com.iluwatar.abstractdocument.domain.Part;
import com.iluwatar.abstractdocument.domain.enums.Property; import com.iluwatar.abstractdocument.domain.enums.Property;
import org.junit.jupiter.api.Test;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
/** /**
* Test for Part and Car * Test for Part and Car
@ -47,27 +46,27 @@ public class DomainTest {
@Test @Test
public void shouldConstructPart() { public void shouldConstructPart() {
Map<String, Object> partProperties = Map.of( var partProperties = Map.of(
Property.TYPE.toString(), TEST_PART_TYPE, Property.TYPE.toString(), TEST_PART_TYPE,
Property.MODEL.toString(), TEST_PART_MODEL, Property.MODEL.toString(), TEST_PART_MODEL,
Property.PRICE.toString(), TEST_PART_PRICE); Property.PRICE.toString(), (Object) TEST_PART_PRICE
Part part = new Part(partProperties); );
var part = new Part(partProperties);
assertEquals(TEST_PART_TYPE, part.getType().get()); assertEquals(TEST_PART_TYPE, part.getType().orElseThrow());
assertEquals(TEST_PART_MODEL, part.getModel().get()); assertEquals(TEST_PART_MODEL, part.getModel().orElseThrow());
assertEquals(TEST_PART_PRICE, part.getPrice().get()); assertEquals(TEST_PART_PRICE, part.getPrice().orElseThrow());
} }
@Test @Test
public void shouldConstructCar() { public void shouldConstructCar() {
Map<String, Object> carProperties = Map.of( var carProperties = Map.of(
Property.MODEL.toString(), TEST_CAR_MODEL, Property.MODEL.toString(), TEST_CAR_MODEL,
Property.PRICE.toString(), TEST_CAR_PRICE, Property.PRICE.toString(), TEST_CAR_PRICE,
Property.PARTS.toString(), List.of(Map.of(), Map.of())); Property.PARTS.toString(), List.of(Map.of(), Map.of())
Car car = new Car(carProperties); );
var car = new Car(carProperties);
assertEquals(TEST_CAR_MODEL, car.getModel().get()); assertEquals(TEST_CAR_MODEL, car.getModel().orElseThrow());
assertEquals(TEST_CAR_PRICE, car.getPrice().get()); assertEquals(TEST_CAR_PRICE, car.getPrice().orElseThrow());
assertEquals(2, car.getParts().count()); assertEquals(2, car.getParts().count());
} }

View File

@ -109,10 +109,10 @@ public class OrcKingdomFactory implements KingdomFactory {
Now we have our abstract factory that lets us make family of related objects i.e. Elven kingdom factory creates Elven castle, king and army etc. Now we have our abstract factory that lets us make family of related objects i.e. Elven kingdom factory creates Elven castle, king and army etc.
```java ```java
KingdomFactory factory = new ElfKingdomFactory(); var factory = new ElfKingdomFactory();
Castle castle = factory.createCastle(); var castle = factory.createCastle();
King king = factory.createKing(); var king = factory.createKing();
Army army = factory.createArmy(); var army = factory.createArmy();
castle.getDescription(); // Output: This is the Elven castle! castle.getDescription(); // Output: This is the Elven castle!
king.getDescription(); // Output: This is the Elven king! king.getDescription(); // Output: This is the Elven king!
@ -143,7 +143,7 @@ public static class FactoryMaker {
} }
public static void main(String[] args) { public static void main(String[] args) {
App app = new App(); var app = new App();
LOGGER.info("Elf Kingdom"); LOGGER.info("Elf Kingdom");
app.createKingdom(FactoryMaker.makeFactory(KingdomType.ELF)); app.createKingdom(FactoryMaker.makeFactory(KingdomType.ELF));

View File

@ -128,7 +128,7 @@ public class App {
*/ */
public static void main(String[] args) { public static void main(String[] args) {
App app = new App(); var app = new App();
LOGGER.info("Elf Kingdom"); LOGGER.info("Elf Kingdom");
app.createKingdom(FactoryMaker.makeFactory(KingdomType.ELF)); app.createKingdom(FactoryMaker.makeFactory(KingdomType.ELF));

View File

@ -28,7 +28,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import com.iluwatar.abstractfactory.App.FactoryMaker; import com.iluwatar.abstractfactory.App.FactoryMaker;
import com.iluwatar.abstractfactory.App.FactoryMaker.KingdomType; import com.iluwatar.abstractfactory.App.FactoryMaker.KingdomType;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -49,30 +48,30 @@ public class AbstractFactoryTest {
@Test @Test
public void king() { public void king() {
final King elfKing = app.getKing(elfFactory); final var elfKing = app.getKing(elfFactory);
assertTrue(elfKing instanceof ElfKing); assertTrue(elfKing instanceof ElfKing);
assertEquals(ElfKing.DESCRIPTION, elfKing.getDescription()); assertEquals(ElfKing.DESCRIPTION, elfKing.getDescription());
final King orcKing = app.getKing(orcFactory); final var orcKing = app.getKing(orcFactory);
assertTrue(orcKing instanceof OrcKing); assertTrue(orcKing instanceof OrcKing);
assertEquals(OrcKing.DESCRIPTION, orcKing.getDescription()); assertEquals(OrcKing.DESCRIPTION, orcKing.getDescription());
} }
@Test @Test
public void castle() { public void castle() {
final Castle elfCastle = app.getCastle(elfFactory); final var elfCastle = app.getCastle(elfFactory);
assertTrue(elfCastle instanceof ElfCastle); assertTrue(elfCastle instanceof ElfCastle);
assertEquals(ElfCastle.DESCRIPTION, elfCastle.getDescription()); assertEquals(ElfCastle.DESCRIPTION, elfCastle.getDescription());
final Castle orcCastle = app.getCastle(orcFactory); final var orcCastle = app.getCastle(orcFactory);
assertTrue(orcCastle instanceof OrcCastle); assertTrue(orcCastle instanceof OrcCastle);
assertEquals(OrcCastle.DESCRIPTION, orcCastle.getDescription()); assertEquals(OrcCastle.DESCRIPTION, orcCastle.getDescription());
} }
@Test @Test
public void army() { public void army() {
final Army elfArmy = app.getArmy(elfFactory); final var elfArmy = app.getArmy(elfFactory);
assertTrue(elfArmy instanceof ElfArmy); assertTrue(elfArmy instanceof ElfArmy);
assertEquals(ElfArmy.DESCRIPTION, elfArmy.getDescription()); assertEquals(ElfArmy.DESCRIPTION, elfArmy.getDescription());
final Army orcArmy = app.getArmy(orcFactory); final var orcArmy = app.getArmy(orcFactory);
assertTrue(orcArmy instanceof OrcArmy); assertTrue(orcArmy instanceof OrcArmy);
assertEquals(OrcArmy.DESCRIPTION, orcArmy.getDescription()); assertEquals(OrcArmy.DESCRIPTION, orcArmy.getDescription());
} }
@ -80,9 +79,9 @@ public class AbstractFactoryTest {
@Test @Test
public void createElfKingdom() { public void createElfKingdom() {
app.createKingdom(elfFactory); app.createKingdom(elfFactory);
final King king = app.getKing(); final var king = app.getKing();
final Castle castle = app.getCastle(); final var castle = app.getCastle();
final Army army = app.getArmy(); final var army = app.getArmy();
assertTrue(king instanceof ElfKing); assertTrue(king instanceof ElfKing);
assertEquals(ElfKing.DESCRIPTION, king.getDescription()); assertEquals(ElfKing.DESCRIPTION, king.getDescription());
assertTrue(castle instanceof ElfCastle); assertTrue(castle instanceof ElfCastle);
@ -94,9 +93,9 @@ public class AbstractFactoryTest {
@Test @Test
public void createOrcKingdom() { public void createOrcKingdom() {
app.createKingdom(orcFactory); app.createKingdom(orcFactory);
final King king = app.getKing(); final var king = app.getKing();
final Castle castle = app.getCastle(); final var castle = app.getCastle();
final Army army = app.getArmy(); final var army = app.getArmy();
assertTrue(king instanceof OrcKing); assertTrue(king instanceof OrcKing);
assertEquals(OrcKing.DESCRIPTION, king.getDescription()); assertEquals(OrcKing.DESCRIPTION, king.getDescription());
assertTrue(castle instanceof OrcCastle); assertTrue(castle instanceof OrcCastle);

View File

@ -25,15 +25,12 @@ package com.iluwatar.abstractfactory;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.io.IOException;
/** /**
* Tests that Abstract Factory example runs without errors. * Tests that Abstract Factory example runs without errors.
*/ */
public class AppTest { public class AppTest {
@Test @Test
public void test() throws IOException { public void test() {
String[] args = {}; App.main(new String[]{});
App.main(args);
} }
} }

View File

@ -25,16 +25,13 @@ package com.iluwatar.acyclicvisitor;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import com.iluwatar.acyclicvisitor.App;
/** /**
* Tests that the Acyclic Visitor example runs without errors. * Tests that the Acyclic Visitor example runs without errors.
*/ */
public class AppTest { public class AppTest {
@Test @Test
public void test() { public void test() {
String[] args = {}; App.main(new String[]{});
App.main(args);
} }
} }

View File

@ -37,7 +37,7 @@ import uk.org.lidalia.slf4jtest.TestLoggerFactory;
*/ */
public class ConfigureForDosVisitorTest { public class ConfigureForDosVisitorTest {
TestLogger logger = TestLoggerFactory.getTestLogger(ConfigureForDosVisitor.class); private TestLogger logger = TestLoggerFactory.getTestLogger(ConfigureForDosVisitor.class);
@Test @Test
public void testVisitForZoom() { public void testVisitForZoom() {
@ -46,19 +46,21 @@ public class ConfigureForDosVisitorTest {
conDos.visit(zoom); conDos.visit(zoom);
assertThat(logger.getLoggingEvents()).extracting("level", "message").contains( assertThat(logger.getLoggingEvents())
tuple(INFO, zoom + " used with Dos configurator.")); .extracting("level", "message")
.contains(tuple(INFO, zoom + " used with Dos configurator."));
} }
@Test @Test
public void testVisitForHayes() { public void testVisitForHayes() {
ConfigureForDosVisitor conDos = new ConfigureForDosVisitor(); var conDos = new ConfigureForDosVisitor();
Hayes hayes = new Hayes(); var hayes = new Hayes();
conDos.visit(hayes); conDos.visit(hayes);
assertThat(logger.getLoggingEvents()).extracting("level", "message").contains( assertThat(logger.getLoggingEvents())
tuple(INFO, hayes + " used with Dos configurator.")); .extracting("level", "message")
.contains(tuple(INFO, hayes + " used with Dos configurator."));
} }
@AfterEach @AfterEach

View File

@ -52,7 +52,8 @@ public class ConfigureForUnixVisitorTest {
conUnix.visit(zoom); conUnix.visit(zoom);
assertThat(LOGGER.getLoggingEvents()).extracting("level", "message").contains( assertThat(LOGGER.getLoggingEvents())
tuple(INFO, zoom + " used with Unix configurator.")); .extracting("level", "message")
.contains(tuple(INFO, zoom + " used with Unix configurator."));
} }
} }

View File

@ -93,7 +93,7 @@ public class FishingBoatAdapter implements RowingBoat {
And now the `Captain` can use the `FishingBoat` to escape the pirates. And now the `Captain` can use the `FishingBoat` to escape the pirates.
```java ```java
Captain captain = new Captain(new FishingBoatAdapter()); var captain = new Captain(new FishingBoatAdapter());
captain.row(); captain.row();
``` ```

View File

@ -23,18 +23,16 @@
package com.iluwatar.adapter; package com.iluwatar.adapter;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.HashMap;
import java.util.Map;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
/** /**
* Test class * Test class
*
*/ */
public class AdapterPatternTest { public class AdapterPatternTest {
@ -51,7 +49,7 @@ public class AdapterPatternTest {
public void setup() { public void setup() {
beans = new HashMap<>(); beans = new HashMap<>();
FishingBoatAdapter fishingBoatAdapter = spy(new FishingBoatAdapter()); var fishingBoatAdapter = spy(new FishingBoatAdapter());
beans.put(FISHING_BEAN, fishingBoatAdapter); beans.put(FISHING_BEAN, fishingBoatAdapter);
var captain = new Captain(); var captain = new Captain();
@ -60,10 +58,10 @@ public class AdapterPatternTest {
} }
/** /**
* This test asserts that when we use the row() method on a captain bean(client), it is * This test asserts that when we use the row() method on a captain bean(client), it is internally
* internally calling sail method on the fishing boat object. The Adapter ({@link FishingBoatAdapter} * calling sail method on the fishing boat object. The Adapter ({@link FishingBoatAdapter} )
* ) converts the interface of the target class ( {@link FishingBoat}) into a suitable one * converts the interface of the target class ( {@link FishingBoat}) into a suitable one expected
* expected by the client ({@link Captain} ). * by the client ({@link Captain} ).
*/ */
@Test @Test
public void testAdapter() { public void testAdapter() {

View File

@ -25,15 +25,12 @@ package com.iluwatar.adapter;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.io.IOException;
/** /**
* Tests that Adapter example runs without errors. * Tests that Adapter example runs without errors.
*/ */
public class AppTest { public class AppTest {
@Test @Test
public void test() throws IOException { public void test() {
String[] args = {}; App.main(new String[]{});
App.main(args);
} }
} }

View File

@ -23,6 +23,8 @@
package com.iluwatar.aggregator.microservices; package com.iluwatar.aggregator.microservices;
import static java.util.Objects.requireNonNullElse;
import javax.annotation.Resource; import javax.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
@ -52,20 +54,14 @@ public class Aggregator {
public Product getProduct() { public Product getProduct() {
var product = new Product(); var product = new Product();
String productTitle = informationClient.getProductTitle(); var productTitle = informationClient.getProductTitle();
Integer productInventory = inventoryClient.getProductInventories(); var productInventory = inventoryClient.getProductInventories();
if (productTitle != null) { //Fallback to error message
product.setTitle(productTitle); product.setTitle(requireNonNullElse(productTitle, "Error: Fetching Product Title Failed"));
} else {
product.setTitle("Error: Fetching Product Title Failed"); //Fallback to error message
}
if (productInventory != null) { //Fallback to default error inventory
product.setProductInventories(productInventory); product.setProductInventories(requireNonNullElse(productInventory, -1));
} else {
product.setProductInventories(-1); //Fallback to default error inventory
}
return product; return product;
} }

View File

@ -42,19 +42,19 @@ public class ProductInformationClientImpl implements ProductInformationClient {
@Override @Override
public String getProductTitle() { public String getProductTitle() {
String response = null; var request = HttpRequest.newBuilder()
var request = .GET()
HttpRequest.newBuilder().GET().uri(URI.create("http://localhost:51515/information")) .uri(URI.create("http://localhost:51515/information"))
.build(); .build();
var client = HttpClient.newHttpClient(); var client = HttpClient.newHttpClient();
try { try {
var httpResponse = client.send(request, HttpResponse.BodyHandlers.ofString()); var httpResponse = client.send(request, HttpResponse.BodyHandlers.ofString());
response = httpResponse.body(); return httpResponse.body();
} catch (IOException ioe) { } catch (IOException ioe) {
LOGGER.error("IOException Occurred", ioe); LOGGER.error("IOException Occurred", ioe);
} catch (InterruptedException ie) { } catch (InterruptedException ie) {
LOGGER.error("InterruptedException Occurred", ie); LOGGER.error("InterruptedException Occurred", ie);
} }
return response; return null;
} }
} }

View File

@ -44,9 +44,10 @@ public class ProductInventoryClientImpl implements ProductInventoryClient {
public Integer getProductInventories() { public Integer getProductInventories() {
var response = ""; var response = "";
var request = var request = HttpRequest.newBuilder()
HttpRequest.newBuilder().GET().uri(URI.create("http://localhost:51516/inventories")) .GET()
.build(); .uri(URI.create("http://localhost:51516/inventories"))
.build();
var client = HttpClient.newHttpClient(); var client = HttpClient.newHttpClient();
try { try {
var httpResponse = client.send(request, HttpResponse.BodyHandlers.ofString()); var httpResponse = client.send(request, HttpResponse.BodyHandlers.ofString());

View File

@ -56,13 +56,13 @@ public class AggregatorTest {
*/ */
@Test @Test
public void testGetProduct() { public void testGetProduct() {
String title = "The Product Title."; var title = "The Product Title.";
int inventories = 5; var inventories = 5;
when(informationClient.getProductTitle()).thenReturn(title); when(informationClient.getProductTitle()).thenReturn(title);
when(inventoryClient.getProductInventories()).thenReturn(inventories); when(inventoryClient.getProductInventories()).thenReturn(inventories);
Product testProduct = aggregator.getProduct(); var testProduct = aggregator.getProduct();
assertEquals(title, testProduct.getTitle()); assertEquals(title, testProduct.getTitle());
assertEquals(inventories, testProduct.getProductInventories()); assertEquals(inventories, testProduct.getProductInventories());

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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