diff --git a/abstract-document/src/main/java/com/iluwatar/abstractdocument/AbstractDocument.java b/abstract-document/src/main/java/com/iluwatar/abstractdocument/AbstractDocument.java index 421d6890a..485af25b3 100644 --- a/abstract-document/src/main/java/com/iluwatar/abstractdocument/AbstractDocument.java +++ b/abstract-document/src/main/java/com/iluwatar/abstractdocument/AbstractDocument.java @@ -23,10 +23,10 @@ package com.iluwatar.abstractdocument; +import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Objects; -import java.util.Optional; import java.util.function.Function; import java.util.stream.Stream; @@ -55,9 +55,13 @@ public abstract class AbstractDocument implements Document { @Override public Stream children(String key, Function, T> constructor) { - Optional>> any = Stream.of(get(key)).filter(Objects::nonNull) - .map(el -> (List>) el).findAny(); - return any.map(maps -> maps.stream().map(constructor)).orElseGet(Stream::empty); + return Stream.ofNullable(get(key)) + .filter(Objects::nonNull) + .map(el -> (List>) el) + .findAny() + .stream() + .flatMap(Collection::stream) + .map(constructor); } @Override diff --git a/abstract-document/src/main/java/com/iluwatar/abstractdocument/App.java b/abstract-document/src/main/java/com/iluwatar/abstractdocument/App.java index 4f4dddafa..b881ee7ac 100644 --- a/abstract-document/src/main/java/com/iluwatar/abstractdocument/App.java +++ b/abstract-document/src/main/java/com/iluwatar/abstractdocument/App.java @@ -66,11 +66,14 @@ public class App { var car = new Car(carProperties); LOGGER.info("Here is our car:"); - LOGGER.info("-> model: {}", car.getModel().get()); - LOGGER.info("-> price: {}", car.getPrice().get()); + LOGGER.info("-> model: {}", car.getModel().orElseThrow()); + LOGGER.info("-> price: {}", car.getPrice().orElseThrow()); LOGGER.info("-> parts: "); - car.getParts().forEach(p -> LOGGER - .info("\t{}/{}/{}", p.getType().get(), p.getModel().get(), p.getPrice().get())); + car.getParts().forEach(p -> LOGGER.info("\t{}/{}/{}", + p.getType().orElse(null), + p.getModel().orElse(null), + p.getPrice().orElse(null)) + ); } /** diff --git a/abstract-document/src/test/java/com/iluwatar/abstractdocument/AbstractDocumentTest.java b/abstract-document/src/test/java/com/iluwatar/abstractdocument/AbstractDocumentTest.java index da5765391..d7fe5688d 100644 --- a/abstract-document/src/test/java/com/iluwatar/abstractdocument/AbstractDocumentTest.java +++ b/abstract-document/src/test/java/com/iluwatar/abstractdocument/AbstractDocumentTest.java @@ -23,14 +23,14 @@ 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.List; import java.util.Map; -import java.util.stream.Stream; - -import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; /** * AbstractDocument test class @@ -61,22 +61,22 @@ public class AbstractDocumentTest { document.put(KEY, children); - Stream childrenStream = document.children(KEY, DocumentImplementation::new); + var childrenStream = document.children(KEY, DocumentImplementation::new); assertNotNull(children); assertEquals(2, childrenStream.count()); } @Test public void shouldRetrieveEmptyStreamForNonExistingChildren() { - Stream children = document.children(KEY, DocumentImplementation::new); + var children = document.children(KEY, DocumentImplementation::new); assertNotNull(children); assertEquals(0, children.count()); } @Test public void shouldIncludePropsInToString() { - Map props = Map.of(KEY, VALUE); - DocumentImplementation document = new DocumentImplementation(props); + var props = Map.of(KEY, (Object) VALUE); + var document = new DocumentImplementation(props); assertTrue(document.toString().contains(KEY)); assertTrue(document.toString().contains(VALUE)); } diff --git a/abstract-document/src/test/java/com/iluwatar/abstractdocument/DomainTest.java b/abstract-document/src/test/java/com/iluwatar/abstractdocument/DomainTest.java index eed4ad9bb..5b1311ff6 100644 --- a/abstract-document/src/test/java/com/iluwatar/abstractdocument/DomainTest.java +++ b/abstract-document/src/test/java/com/iluwatar/abstractdocument/DomainTest.java @@ -23,15 +23,14 @@ package com.iluwatar.abstractdocument; +import static org.junit.jupiter.api.Assertions.assertEquals; + import com.iluwatar.abstractdocument.domain.Car; import com.iluwatar.abstractdocument.domain.Part; import com.iluwatar.abstractdocument.domain.enums.Property; -import org.junit.jupiter.api.Test; - import java.util.List; import java.util.Map; - -import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.Test; /** * Test for Part and Car @@ -47,27 +46,27 @@ public class DomainTest { @Test public void shouldConstructPart() { - Map partProperties = Map.of( - Property.TYPE.toString(), TEST_PART_TYPE, - Property.MODEL.toString(), TEST_PART_MODEL, - Property.PRICE.toString(), TEST_PART_PRICE); - Part part = new Part(partProperties); - - assertEquals(TEST_PART_TYPE, part.getType().get()); - assertEquals(TEST_PART_MODEL, part.getModel().get()); - assertEquals(TEST_PART_PRICE, part.getPrice().get()); + var partProperties = Map.of( + Property.TYPE.toString(), TEST_PART_TYPE, + Property.MODEL.toString(), TEST_PART_MODEL, + Property.PRICE.toString(), (Object) TEST_PART_PRICE + ); + var part = new Part(partProperties); + assertEquals(TEST_PART_TYPE, part.getType().orElseThrow()); + assertEquals(TEST_PART_MODEL, part.getModel().orElseThrow()); + assertEquals(TEST_PART_PRICE, part.getPrice().orElseThrow()); } @Test public void shouldConstructCar() { - Map carProperties = Map.of( - Property.MODEL.toString(), TEST_CAR_MODEL, - Property.PRICE.toString(), TEST_CAR_PRICE, - Property.PARTS.toString(), List.of(Map.of(), Map.of())); - Car car = new Car(carProperties); - - assertEquals(TEST_CAR_MODEL, car.getModel().get()); - assertEquals(TEST_CAR_PRICE, car.getPrice().get()); + var carProperties = Map.of( + Property.MODEL.toString(), TEST_CAR_MODEL, + Property.PRICE.toString(), TEST_CAR_PRICE, + Property.PARTS.toString(), List.of(Map.of(), Map.of()) + ); + var car = new Car(carProperties); + assertEquals(TEST_CAR_MODEL, car.getModel().orElseThrow()); + assertEquals(TEST_CAR_PRICE, car.getPrice().orElseThrow()); assertEquals(2, car.getParts().count()); } diff --git a/abstract-factory/README.md b/abstract-factory/README.md index aefedb07f..f6719af45 100644 --- a/abstract-factory/README.md +++ b/abstract-factory/README.md @@ -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. ```java -KingdomFactory factory = new ElfKingdomFactory(); -Castle castle = factory.createCastle(); -King king = factory.createKing(); -Army army = factory.createArmy(); +var factory = new ElfKingdomFactory(); +var castle = factory.createCastle(); +var king = factory.createKing(); +var army = factory.createArmy(); castle.getDescription(); // Output: This is the Elven castle! king.getDescription(); // Output: This is the Elven king! @@ -143,7 +143,7 @@ public static class FactoryMaker { } public static void main(String[] args) { - App app = new App(); + var app = new App(); LOGGER.info("Elf Kingdom"); app.createKingdom(FactoryMaker.makeFactory(KingdomType.ELF)); diff --git a/abstract-factory/src/main/java/com/iluwatar/abstractfactory/App.java b/abstract-factory/src/main/java/com/iluwatar/abstractfactory/App.java index 2694dd708..e158ece74 100644 --- a/abstract-factory/src/main/java/com/iluwatar/abstractfactory/App.java +++ b/abstract-factory/src/main/java/com/iluwatar/abstractfactory/App.java @@ -128,7 +128,7 @@ public class App { */ public static void main(String[] args) { - App app = new App(); + var app = new App(); LOGGER.info("Elf Kingdom"); app.createKingdom(FactoryMaker.makeFactory(KingdomType.ELF)); diff --git a/abstract-factory/src/test/java/com/iluwatar/abstractfactory/AbstractFactoryTest.java b/abstract-factory/src/test/java/com/iluwatar/abstractfactory/AbstractFactoryTest.java index eab501e9b..cecd8a894 100644 --- a/abstract-factory/src/test/java/com/iluwatar/abstractfactory/AbstractFactoryTest.java +++ b/abstract-factory/src/test/java/com/iluwatar/abstractfactory/AbstractFactoryTest.java @@ -28,7 +28,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import com.iluwatar.abstractfactory.App.FactoryMaker; import com.iluwatar.abstractfactory.App.FactoryMaker.KingdomType; - import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -49,30 +48,30 @@ public class AbstractFactoryTest { @Test public void king() { - final King elfKing = app.getKing(elfFactory); + final var elfKing = app.getKing(elfFactory); assertTrue(elfKing instanceof ElfKing); assertEquals(ElfKing.DESCRIPTION, elfKing.getDescription()); - final King orcKing = app.getKing(orcFactory); + final var orcKing = app.getKing(orcFactory); assertTrue(orcKing instanceof OrcKing); assertEquals(OrcKing.DESCRIPTION, orcKing.getDescription()); } @Test public void castle() { - final Castle elfCastle = app.getCastle(elfFactory); + final var elfCastle = app.getCastle(elfFactory); assertTrue(elfCastle instanceof ElfCastle); assertEquals(ElfCastle.DESCRIPTION, elfCastle.getDescription()); - final Castle orcCastle = app.getCastle(orcFactory); + final var orcCastle = app.getCastle(orcFactory); assertTrue(orcCastle instanceof OrcCastle); assertEquals(OrcCastle.DESCRIPTION, orcCastle.getDescription()); } @Test public void army() { - final Army elfArmy = app.getArmy(elfFactory); + final var elfArmy = app.getArmy(elfFactory); assertTrue(elfArmy instanceof ElfArmy); assertEquals(ElfArmy.DESCRIPTION, elfArmy.getDescription()); - final Army orcArmy = app.getArmy(orcFactory); + final var orcArmy = app.getArmy(orcFactory); assertTrue(orcArmy instanceof OrcArmy); assertEquals(OrcArmy.DESCRIPTION, orcArmy.getDescription()); } @@ -80,9 +79,9 @@ public class AbstractFactoryTest { @Test public void createElfKingdom() { app.createKingdom(elfFactory); - final King king = app.getKing(); - final Castle castle = app.getCastle(); - final Army army = app.getArmy(); + final var king = app.getKing(); + final var castle = app.getCastle(); + final var army = app.getArmy(); assertTrue(king instanceof ElfKing); assertEquals(ElfKing.DESCRIPTION, king.getDescription()); assertTrue(castle instanceof ElfCastle); @@ -94,9 +93,9 @@ public class AbstractFactoryTest { @Test public void createOrcKingdom() { app.createKingdom(orcFactory); - final King king = app.getKing(); - final Castle castle = app.getCastle(); - final Army army = app.getArmy(); + final var king = app.getKing(); + final var castle = app.getCastle(); + final var army = app.getArmy(); assertTrue(king instanceof OrcKing); assertEquals(OrcKing.DESCRIPTION, king.getDescription()); assertTrue(castle instanceof OrcCastle); diff --git a/abstract-factory/src/test/java/com/iluwatar/abstractfactory/AppTest.java b/abstract-factory/src/test/java/com/iluwatar/abstractfactory/AppTest.java index 892af7392..4036cc9b8 100644 --- a/abstract-factory/src/test/java/com/iluwatar/abstractfactory/AppTest.java +++ b/abstract-factory/src/test/java/com/iluwatar/abstractfactory/AppTest.java @@ -25,15 +25,12 @@ package com.iluwatar.abstractfactory; import org.junit.jupiter.api.Test; -import java.io.IOException; - /** * Tests that Abstract Factory example runs without errors. */ public class AppTest { @Test - public void test() throws IOException { - String[] args = {}; - App.main(args); + public void test() { + App.main(new String[]{}); } } diff --git a/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/AppTest.java b/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/AppTest.java index 1f555ee99..4b9a7ec6c 100644 --- a/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/AppTest.java +++ b/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/AppTest.java @@ -25,16 +25,13 @@ package com.iluwatar.acyclicvisitor; import org.junit.jupiter.api.Test; -import com.iluwatar.acyclicvisitor.App; - /** * Tests that the Acyclic Visitor example runs without errors. */ public class AppTest { - + @Test public void test() { - String[] args = {}; - App.main(args); + App.main(new String[]{}); } } \ No newline at end of file diff --git a/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ConfigureForDosVisitorTest.java b/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ConfigureForDosVisitorTest.java index d49ba591c..8847a131e 100644 --- a/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ConfigureForDosVisitorTest.java +++ b/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ConfigureForDosVisitorTest.java @@ -37,7 +37,7 @@ import uk.org.lidalia.slf4jtest.TestLoggerFactory; */ public class ConfigureForDosVisitorTest { - TestLogger logger = TestLoggerFactory.getTestLogger(ConfigureForDosVisitor.class); + private TestLogger logger = TestLoggerFactory.getTestLogger(ConfigureForDosVisitor.class); @Test public void testVisitForZoom() { @@ -46,19 +46,21 @@ public class ConfigureForDosVisitorTest { conDos.visit(zoom); - assertThat(logger.getLoggingEvents()).extracting("level", "message").contains( - tuple(INFO, zoom + " used with Dos configurator.")); + assertThat(logger.getLoggingEvents()) + .extracting("level", "message") + .contains(tuple(INFO, zoom + " used with Dos configurator.")); } @Test public void testVisitForHayes() { - ConfigureForDosVisitor conDos = new ConfigureForDosVisitor(); - Hayes hayes = new Hayes(); + var conDos = new ConfigureForDosVisitor(); + var hayes = new Hayes(); conDos.visit(hayes); - assertThat(logger.getLoggingEvents()).extracting("level", "message").contains( - tuple(INFO, hayes + " used with Dos configurator.")); + assertThat(logger.getLoggingEvents()) + .extracting("level", "message") + .contains(tuple(INFO, hayes + " used with Dos configurator.")); } @AfterEach diff --git a/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ConfigureForUnixVisitorTest.java b/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ConfigureForUnixVisitorTest.java index e1765c81b..32067ad38 100644 --- a/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ConfigureForUnixVisitorTest.java +++ b/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ConfigureForUnixVisitorTest.java @@ -52,7 +52,8 @@ public class ConfigureForUnixVisitorTest { conUnix.visit(zoom); - assertThat(LOGGER.getLoggingEvents()).extracting("level", "message").contains( - tuple(INFO, zoom + " used with Unix configurator.")); + assertThat(LOGGER.getLoggingEvents()) + .extracting("level", "message") + .contains(tuple(INFO, zoom + " used with Unix configurator.")); } } diff --git a/adapter/README.md b/adapter/README.md index 03334bf18..0076e962c 100644 --- a/adapter/README.md +++ b/adapter/README.md @@ -93,7 +93,7 @@ public class FishingBoatAdapter implements RowingBoat { And now the `Captain` can use the `FishingBoat` to escape the pirates. ```java -Captain captain = new Captain(new FishingBoatAdapter()); +var captain = new Captain(new FishingBoatAdapter()); captain.row(); ``` diff --git a/adapter/src/test/java/com/iluwatar/adapter/AdapterPatternTest.java b/adapter/src/test/java/com/iluwatar/adapter/AdapterPatternTest.java index d4cca004f..f87073b23 100644 --- a/adapter/src/test/java/com/iluwatar/adapter/AdapterPatternTest.java +++ b/adapter/src/test/java/com/iluwatar/adapter/AdapterPatternTest.java @@ -23,18 +23,16 @@ 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.verify; +import java.util.HashMap; +import java.util.Map; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + /** * Test class - * */ public class AdapterPatternTest { @@ -51,7 +49,7 @@ public class AdapterPatternTest { public void setup() { beans = new HashMap<>(); - FishingBoatAdapter fishingBoatAdapter = spy(new FishingBoatAdapter()); + var fishingBoatAdapter = spy(new FishingBoatAdapter()); beans.put(FISHING_BEAN, fishingBoatAdapter); 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 - * internally 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 - * expected by the client ({@link Captain} ). + * This test asserts that when we use the row() method on a captain bean(client), it is internally + * 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 expected + * by the client ({@link Captain} ). */ @Test public void testAdapter() { diff --git a/adapter/src/test/java/com/iluwatar/adapter/AppTest.java b/adapter/src/test/java/com/iluwatar/adapter/AppTest.java index 7e94241b6..3bf8e1010 100644 --- a/adapter/src/test/java/com/iluwatar/adapter/AppTest.java +++ b/adapter/src/test/java/com/iluwatar/adapter/AppTest.java @@ -25,15 +25,12 @@ package com.iluwatar.adapter; import org.junit.jupiter.api.Test; -import java.io.IOException; - /** * Tests that Adapter example runs without errors. */ public class AppTest { @Test - public void test() throws IOException { - String[] args = {}; - App.main(args); + public void test() { + App.main(new String[]{}); } } diff --git a/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/Aggregator.java b/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/Aggregator.java index 25369f0e0..f28377a1d 100644 --- a/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/Aggregator.java +++ b/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/Aggregator.java @@ -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; } diff --git a/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/ProductInformationClientImpl.java b/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/ProductInformationClientImpl.java index 93fbb2ee6..d19dcd829 100644 --- a/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/ProductInformationClientImpl.java +++ b/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/ProductInformationClientImpl.java @@ -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; } } diff --git a/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/ProductInventoryClientImpl.java b/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/ProductInventoryClientImpl.java index f64208539..e493c8040 100644 --- a/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/ProductInventoryClientImpl.java +++ b/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/ProductInventoryClientImpl.java @@ -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()); diff --git a/aggregator-microservices/aggregator-service/src/test/java/com/iluwatar/aggregator/microservices/AggregatorTest.java b/aggregator-microservices/aggregator-service/src/test/java/com/iluwatar/aggregator/microservices/AggregatorTest.java index 28e8b8397..cb958ecf9 100644 --- a/aggregator-microservices/aggregator-service/src/test/java/com/iluwatar/aggregator/microservices/AggregatorTest.java +++ b/aggregator-microservices/aggregator-service/src/test/java/com/iluwatar/aggregator/microservices/AggregatorTest.java @@ -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()); diff --git a/aggregator-microservices/information-microservice/src/test/java/com/iluwatar/information/microservice/InformationControllerTest.java b/aggregator-microservices/information-microservice/src/test/java/com/iluwatar/information/microservice/InformationControllerTest.java index a7130957b..90388af1a 100644 --- a/aggregator-microservices/information-microservice/src/test/java/com/iluwatar/information/microservice/InformationControllerTest.java +++ b/aggregator-microservices/information-microservice/src/test/java/com/iluwatar/information/microservice/InformationControllerTest.java @@ -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); } diff --git a/aggregator-microservices/inventory-microservice/src/test/java/com/iluwatar/inventory/microservice/InventoryControllerTest.java b/aggregator-microservices/inventory-microservice/src/test/java/com/iluwatar/inventory/microservice/InventoryControllerTest.java index abf3fa6a4..e9961796b 100644 --- a/aggregator-microservices/inventory-microservice/src/test/java/com/iluwatar/inventory/microservice/InventoryControllerTest.java +++ b/aggregator-microservices/inventory-microservice/src/test/java/com/iluwatar/inventory/microservice/InventoryControllerTest.java @@ -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); } } diff --git a/api-gateway/api-gateway-service/src/main/java/com/iluwatar/api/gateway/ApiGateway.java b/api-gateway/api-gateway-service/src/main/java/com/iluwatar/api/gateway/ApiGateway.java index 18a071c09..fbb8bdfd1 100644 --- a/api-gateway/api-gateway-service/src/main/java/com/iluwatar/api/gateway/ApiGateway.java +++ b/api-gateway/api-gateway-service/src/main/java/com/iluwatar/api/gateway/ApiGateway.java @@ -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; } diff --git a/api-gateway/api-gateway-service/src/main/java/com/iluwatar/api/gateway/ImageClientImpl.java b/api-gateway/api-gateway-service/src/main/java/com/iluwatar/api/gateway/ImageClientImpl.java index 6aa9698ef..52dd065ff 100644 --- a/api-gateway/api-gateway-service/src/main/java/com/iluwatar/api/gateway/ImageClientImpl.java +++ b/api-gateway/api-gateway-service/src/main/java/com/iluwatar/api/gateway/ImageClientImpl.java @@ -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 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; } } diff --git a/api-gateway/api-gateway-service/src/main/java/com/iluwatar/api/gateway/PriceClientImpl.java b/api-gateway/api-gateway-service/src/main/java/com/iluwatar/api/gateway/PriceClientImpl.java index 0c63e57f4..0dc44a51b 100644 --- a/api-gateway/api-gateway-service/src/main/java/com/iluwatar/api/gateway/PriceClientImpl.java +++ b/api-gateway/api-gateway-service/src/main/java/com/iluwatar/api/gateway/PriceClientImpl.java @@ -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 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; } } diff --git a/api-gateway/api-gateway-service/src/test/java/com/iluwatar/api/gateway/ApiGatewayTest.java b/api-gateway/api-gateway-service/src/test/java/com/iluwatar/api/gateway/ApiGatewayTest.java index 27068b52b..d8b4234d6 100644 --- a/api-gateway/api-gateway-service/src/test/java/com/iluwatar/api/gateway/ApiGatewayTest.java +++ b/api-gateway/api-gateway-service/src/test/java/com/iluwatar/api/gateway/ApiGatewayTest.java @@ -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()); } diff --git a/api-gateway/image-microservice/src/test/java/com/iluwatar/image/microservice/ImageControllerTest.java b/api-gateway/image-microservice/src/test/java/com/iluwatar/image/microservice/ImageControllerTest.java index d0c44ff0f..0d2414c5f 100644 --- a/api-gateway/image-microservice/src/test/java/com/iluwatar/image/microservice/ImageControllerTest.java +++ b/api-gateway/image-microservice/src/test/java/com/iluwatar/image/microservice/ImageControllerTest.java @@ -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); } } diff --git a/api-gateway/price-microservice/src/test/java/com/iluwatar/price/microservice/PriceControllerTest.java b/api-gateway/price-microservice/src/test/java/com/iluwatar/price/microservice/PriceControllerTest.java index 54a4413a9..4d5dd92d9 100644 --- a/api-gateway/price-microservice/src/test/java/com/iluwatar/price/microservice/PriceControllerTest.java +++ b/api-gateway/price-microservice/src/test/java/com/iluwatar/price/microservice/PriceControllerTest.java @@ -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); } }