diff --git a/src/main/java/com/microservices/example/web/WebArithmeticController.java b/src/main/java/com/microservices/example/web/WebArithmeticController.java index 29a1814..c77f192 100644 --- a/src/main/java/com/microservices/example/web/WebArithmeticController.java +++ b/src/main/java/com/microservices/example/web/WebArithmeticController.java @@ -1,54 +1,61 @@ -package com.microservices.example.web; - -import java.util.logging.Logger; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; - -@Controller -public class WebArithmeticController { - @Autowired - protected WebAdditionService additionService; - - @Autowired - protected WebSubtractionService subtractionService; - - protected Logger logger = Logger.getLogger(WebArithmeticController.class - .getName()); - - public WebArithmeticController(WebAdditionService additionService, WebSubtractionService subtractionService) { - this.additionService = additionService; - this.subtractionService = subtractionService; - } - - //Exposing the request mapping of '/add for the http://localhost:4444/add -> to be consumed by the addition form - @RequestMapping("/add") - public String doAdd(@RequestParam(defaultValue="0") String addend1, - @RequestParam(defaultValue="0") String addend2, - Model model) {//Form parameters are accepted as arguments. - - String sum = additionService.add(addend1, addend2);//Triggering a call to the Addition Service add method. - - logger.info("Sum: " + sum); - model.addAttribute("json", sum); - - return "sum"; - } - - //Exposing the request mapping of '/subtract for the http://localhost:4444/subtract -> to be consumed by the subtraction form - @RequestMapping("/subtract") - public String doSubtract(@RequestParam(defaultValue="0") String minuend, - @RequestParam(defaultValue="0") String subtrahend, - Model model) { - - String difference = subtractionService.subtract(minuend, subtrahend); - - logger.info("Difference: " + difference); - model.addAttribute("json", difference); - - return "difference"; - } -} +package com.microservices.example.web; +import java.util.logging.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +@Controller +public class WebArithmeticController { + @Autowired + protected WebAdditionService additionService; + @Autowired + protected WebSubtractionService subtractionService; + protected Logger logger = Logger.getLogger(WebArithmeticController.class.getName()); + public WebArithmeticController(WebAdditionService additionService, WebSubtractionService subtractionService) { + this.additionService = additionService; + this.subtractionService = subtractionService; + } + // Handle addition requests + @RequestMapping("/add") + public String doAdd(@RequestParam(defaultValue="0") String addend1, + @RequestParam(defaultValue="0") String addend2, + Model model) { + try { + String sum = additionService.add(addend1, addend2); + logger.info("Sum: " + sum); + model.addAttribute("json", sum); + } catch (Exception e) { + logger.severe("Error during addition: " + e.getMessage()); + logger.severe("Stack Trace: " + e.getStackTrace()); + model.addAttribute("json", "Error occurred during addition"); + } + return "sum"; // Return to the sum.html template with result + } + // Handle subtraction requests + @RequestMapping("/subtract") + public String doSubtract(@RequestParam(defaultValue="0") String minuend, + @RequestParam(defaultValue="0") String subtrahend, + Model model) { + try { + String difference = subtractionService.subtract(minuend, subtrahend); + logger.info("Difference: " + difference); + model.addAttribute("json", difference); + } catch (Exception e) { + logger.severe("Error during subtraction: " + e.getMessage()); + logger.severe("Stack Trace: " + e.getStackTrace()); + model.addAttribute("json", "Error occurred during subtraction"); + } + return "difference"; // Return to the difference.html template with result + } + // New method to show the addition form + @RequestMapping("/addition") + public String showAdditionForm(Model model) { + return "addition"; // Returns the addition.html template + } + // New method to show the subtraction form + @RequestMapping("/subtraction") + public String showSubtractionForm(Model model) { + return "subtraction"; // Returns the subtraction.html template + } +} \ No newline at end of file diff --git a/src/main/resources/web-server/templates/addition.html b/src/main/resources/web-server/templates/addition.html new file mode 100644 index 0000000..cff14f7 --- /dev/null +++ b/src/main/resources/web-server/templates/addition.html @@ -0,0 +1,21 @@ + + +
+ +JSON result received from Addition Service: json
+JSON result received from Subtraction Service: json
- - - - + + + + +JSON result received from Subtraction Service: json
+ Go back to Subtraction + Go to Addition + + + \ No newline at end of file diff --git a/src/main/resources/web-server/templates/index.html b/src/main/resources/web-server/templates/index.html index ea6937a..151e961 100644 --- a/src/main/resources/web-server/templates/index.html +++ b/src/main/resources/web-server/templates/index.html @@ -1,34 +1,31 @@ - - - - - - - -JSON result received from Subtraction Service: json
+JSON result received from Addition Service: json
- - - - + + + + +JSON result received from Addition Service: json
+ Go back to Addition + Go to Subtraction + + + \ No newline at end of file diff --git a/src/test/java/com/rest/api/test/AdditionControllerTest.java b/src/test/java/com/rest/api/test/AdditionControllerTest.java new file mode 100644 index 0000000..634bd94 --- /dev/null +++ b/src/test/java/com/rest/api/test/AdditionControllerTest.java @@ -0,0 +1,41 @@ +package com.rest.api.test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.client.RestTemplate; +import static org.junit.jupiter.api.Assertions.assertEquals; +public class AdditionControllerTest { + private RestTemplate restTemplate; + private String baseUrl; + @BeforeEach + public void setUp() { + restTemplate = new RestTemplate(); + baseUrl = "http://localhost:2222/add"; // URL for the addition service + } + @Test + public void testAdditionOfPositiveNumbers() { + String response = restTemplate.getForObject(baseUrl + "?addend1=3&addend2=5", String.class); + assertEquals("{\"addend1\":\"3\", \"addend2\":\"5\", \"sum\": \"8\"}", response); + } + @Test + public void testAdditionOfNegativeNumbers() { + String response = restTemplate.getForObject(baseUrl + "?addend1=-3&addend2=-5", String.class); + assertEquals("{\"addend1\":\"-3\", \"addend2\":\"-5\", \"sum\": \"-8\"}", response); + } + @Test + public void testAdditionOfPositiveAndNegative() { + String response = restTemplate.getForObject(baseUrl + "?addend1=3&addend2=-5", String.class); + assertEquals("{\"addend1\":\"3\", \"addend2\":\"-5\", \"sum\": \"-2\"}", response); + } + @Test + public void testAdditionWithMaxIntegerValues() { + String response = restTemplate.getForObject(baseUrl + "?addend1=2147483647&addend2=1", String.class); + assertEquals("{\"addend1\":\"2147483647\", \"addend2\":\"1\", \"sum\": \"2147483648\"}", response); + } + @Test + public void testAdditionWithMinIntegerValues() { + String response = restTemplate.getForObject(baseUrl + "?addend1=-2147483648&addend2=-1", String.class); + assertEquals("{\"addend1\":\"-2147483648\", \"addend2\":\"-1\", \"sum\": \"-2147483649\"}", response); + } +} \ No newline at end of file diff --git a/src/test/java/com/rest/api/test/SubtractionControllerTest.java b/src/test/java/com/rest/api/test/SubtractionControllerTest.java new file mode 100644 index 0000000..58cf6ed --- /dev/null +++ b/src/test/java/com/rest/api/test/SubtractionControllerTest.java @@ -0,0 +1,40 @@ +package com.rest.api.test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.http.ResponseEntity; +import org.springframework.web.client.RestTemplate; +import static org.junit.jupiter.api.Assertions.assertEquals; +public class SubtractionControllerTest { + private RestTemplate restTemplate; + private String baseUrl; + @BeforeEach + public void setUp() { + restTemplate = new RestTemplate(); + baseUrl = "http://localhost:3333/subtract"; // URL for the subtraction service + } + @Test + public void testSubtractionOfPositiveNumbers() { + String response = restTemplate.getForObject(baseUrl + "?minuend=10&subtrahend=5", String.class); + assertEquals("{\"minuend\":\"10\", \"subtrahend\":\"5\", \"difference\": \"5\"}", response); + } + @Test + public void testSubtractionOfNegativeNumbers() { + String response = restTemplate.getForObject(baseUrl + "?minuend=-10&subtrahend=-5", String.class); + assertEquals("{\"minuend\":\"-10\", \"subtrahend\":\"-5\", \"difference\": \"-5\"}", response); + } + @Test + public void testSubtractionOfPositiveAndNegative() { + String response = restTemplate.getForObject(baseUrl + "?minuend=10&subtrahend=-5", String.class); + assertEquals("{\"minuend\":\"10\", \"subtrahend\":\"-5\", \"difference\": \"15\"}", response); + } + @Test + public void testSubtractionWithMaxIntegerValues() { + String response = restTemplate.getForObject(baseUrl + "?minuend=2147483647&subtrahend=-1", String.class); + assertEquals("{\"minuend\":\"2147483647\", \"subtrahend\":\"-1\", \"difference\": \"2147483648\"}", response); + } + @Test + public void testSubtractionWithMinIntegerValues() { + String response = restTemplate.getForObject(baseUrl + "?minuend=-2147483648&subtrahend=1", String.class); + assertEquals("{\"minuend\":\"-2147483648\", \"subtrahend\":\"1\", \"difference\": \"-2147483649\"}", response); + } +} \ No newline at end of file