From 79074cd6243779656ae625b5ed4923bd9c27f9f6 Mon Sep 17 00:00:00 2001 From: rleontiev Date: Wed, 10 Apr 2024 17:53:12 +0300 Subject: [PATCH] #2904 : Update api-gateway pattern --- .../price/microservice/PriceController.java | 9 ++-- .../price/microservice/PriceService.java | 38 ++++++++++++++++ .../price/microservice/PriceServiceImpl.java | 45 +++++++++++++++++++ .../microservice/PriceControllerTest.java | 4 +- .../price/microservice/PriceServiceTest.java | 42 +++++++++++++++++ 5 files changed, 132 insertions(+), 6 deletions(-) create mode 100644 api-gateway/price-microservice/src/main/java/com/iluwatar/price/microservice/PriceService.java create mode 100644 api-gateway/price-microservice/src/main/java/com/iluwatar/price/microservice/PriceServiceImpl.java create mode 100644 api-gateway/price-microservice/src/test/java/com/iluwatar/price/microservice/PriceServiceTest.java diff --git a/api-gateway/price-microservice/src/main/java/com/iluwatar/price/microservice/PriceController.java b/api-gateway/price-microservice/src/main/java/com/iluwatar/price/microservice/PriceController.java index 87f107ea50d1..9b922a50285a 100644 --- a/api-gateway/price-microservice/src/main/java/com/iluwatar/price/microservice/PriceController.java +++ b/api-gateway/price-microservice/src/main/java/com/iluwatar/price/microservice/PriceController.java @@ -24,7 +24,7 @@ */ package com.iluwatar.price.microservice; -import lombok.extern.slf4j.Slf4j; +import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @@ -32,10 +32,12 @@ /** * Exposes the Price microservice's endpoints. */ -@Slf4j @RestController +@RequiredArgsConstructor public class PriceController { + private final PriceService priceService; + /** * An endpoint for a user to retrieve a product's price. * @@ -43,7 +45,6 @@ public class PriceController { */ @GetMapping("/price") public String getPrice() { - LOGGER.info("Successfully found price info"); - return "20"; + return priceService.getPrice(); } } diff --git a/api-gateway/price-microservice/src/main/java/com/iluwatar/price/microservice/PriceService.java b/api-gateway/price-microservice/src/main/java/com/iluwatar/price/microservice/PriceService.java new file mode 100644 index 000000000000..1d09c5311b20 --- /dev/null +++ b/api-gateway/price-microservice/src/main/java/com/iluwatar/price/microservice/PriceService.java @@ -0,0 +1,38 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.price.microservice; + +/** + * Service to get a product's price. + */ +public interface PriceService { + + /** + * Getting the price of a product. + * + * @return A product's price + */ + String getPrice(); +} diff --git a/api-gateway/price-microservice/src/main/java/com/iluwatar/price/microservice/PriceServiceImpl.java b/api-gateway/price-microservice/src/main/java/com/iluwatar/price/microservice/PriceServiceImpl.java new file mode 100644 index 000000000000..c4b8aa1059e3 --- /dev/null +++ b/api-gateway/price-microservice/src/main/java/com/iluwatar/price/microservice/PriceServiceImpl.java @@ -0,0 +1,45 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.price.microservice; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; + +/** + * {@inheritDoc} + */ +@Service +@Slf4j +public class PriceServiceImpl implements PriceService { + + /** + * {@inheritDoc} + */ + @Override + public String getPrice() { + LOGGER.info("Successfully found price info"); + return "20"; + } +} 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 081e35946f1b..91c9b36bef14 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 @@ -34,8 +34,8 @@ class PriceControllerTest { @Test - void testgetPrice() { - var priceController = new PriceController(); + void getPriceTest() { + var priceController = new PriceController(new PriceServiceImpl()); var price = priceController.getPrice(); assertEquals("20", price); } diff --git a/api-gateway/price-microservice/src/test/java/com/iluwatar/price/microservice/PriceServiceTest.java b/api-gateway/price-microservice/src/test/java/com/iluwatar/price/microservice/PriceServiceTest.java new file mode 100644 index 000000000000..c6e87e7c7db1 --- /dev/null +++ b/api-gateway/price-microservice/src/test/java/com/iluwatar/price/microservice/PriceServiceTest.java @@ -0,0 +1,42 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.price.microservice; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Test for Price Service + */ +class PriceServiceTest { + + @Test + void getPriceTest() { + var priceService = new PriceServiceImpl(); + var price = priceService.getPrice(); + assertEquals("20", price); + } +}