Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
}
}
21 changes: 21 additions & 0 deletions src/main/resources/web-server/templates/addition.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head th:replace="header :: copy"></head>
<body>
<h1>Addition Operation</h1>
<form action="/add" method="get">
<label for="addend1">First number:</label>
<input type="number" id="addend1" name="addend1" value="0" required>
<br>
<label for="addend2">Second number:</label>
<input type="number" id="addend2" name="addend2" value="0" required>
<br><br>
<input type="submit" value="Add">
</form>
<div th:if="${json}">
<h2>Result:</h2>
<p>JSON result received from Addition Service: <b th:text="${json}">json</b></p>
</div>
<div th:replace="footer :: copy"></div>
</body>
</html>
25 changes: 12 additions & 13 deletions src/main/resources/web-server/templates/difference.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">

<head th:replace="header :: copy"></head>

<body>
<h1>Difference</h1>
<p>JSON result received from Subtraction Service: <b th:text="${json}">json</b></p>

<div th:replace="footer :: copy"></div>
</body>
</html>
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head th:replace="header :: copy"></head>
<body>
<h1>Difference</h1>
<p>JSON result received from Subtraction Service: <b th:text="${json}">json</b></p>
<a href="/subtraction">Go back to Subtraction</a>
<a href="/addition">Go to Addition</a>
<div th:replace="footer :: copy"></div>
</body>
</html>
65 changes: 31 additions & 34 deletions src/main/resources/web-server/templates/index.html
Original file line number Diff line number Diff line change
@@ -1,34 +1,31 @@
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">

<head th:replace="header :: copy"></head>

<body>

<h1>Spring Boot Microservices Example</h1>
<h3>Addition Service</h3>
<form action="/add">
First number:<br>
<input type="number" name="addend1" value="0">
<br>
Second number<br>
<input type="number" name="addend2" value="0">
<br><br>
<input type="submit" value="Submit">
</form>
<hr>
<form action="/subtract">
First number:<br>
<input type="number" name="minuend" value="0">
<br>
Second number<br>
<input type="number" name="subtrahend" value="0">
<br><br>
<input type="submit" value="Submit">
</form>

<div th:replace="footer :: copy"></div>

</body>
</html>
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head th:replace="header :: copy"></head>
<body>
<h1>Spring Boot Microservices Example</h1>
<h3>Addition Service</h3>
<a href="/addition">Go to Addition</a>
<hr>
<form action="/add">
First number:<br>
<input type="number" name="addend1" value="0">
<br>
Second number<br>
<input type="number" name="addend2" value="0">
<br><br>
<input type="submit" value="Submit">
</form>
<hr>
<form action="/subtract">
First number:<br>
<input type="number" name="minuend" value="0">
<br>
Second number<br>
<input type="number" name="subtrahend" value="0">
<br><br>
<input type="submit" value="Submit">
</form>
<div th:replace="footer :: copy"></div>
</body>
</html>
21 changes: 21 additions & 0 deletions src/main/resources/web-server/templates/subtraction.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head th:replace="header :: copy"></head>
<body>
<h1>Subtraction Operation</h1>
<form action="/subtract" method="get">
<label for="minuend">First number:</label>
<input type="number" id="minuend" name="minuend" value="0" required>
<br>
<label for="subtrahend">Second number:</label>
<input type="number" id="subtrahend" name="subtrahend" value="0" required>
<br><br>
<input type="submit" value="Subtract">
</form>
<div th:if="${json}">
<h2>Result:</h2>
<p>JSON result received from Subtraction Service: <b th:text="${json}">json</b></p>
</div>
<div th:replace="footer :: copy"></div>
</body>
</html>
25 changes: 12 additions & 13 deletions src/main/resources/web-server/templates/sum.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">

<head th:replace="header :: copy"></head>

<body>
<h1>Sum</h1>
<p>JSON result received from Addition Service: <b th:text="${json}">json</b></p>

<div th:replace="footer :: copy"></div>
</body>
</html>
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head th:replace="header :: copy"></head>
<body>
<h1>Sum</h1>
<p>JSON result received from Addition Service: <b th:text="${json}">json</b></p>
<a href="/addition">Go back to Addition</a>
<a href="/subtraction">Go to Subtraction</a>
<div th:replace="footer :: copy"></div>
</body>
</html>
41 changes: 41 additions & 0 deletions src/test/java/com/rest/api/test/AdditionControllerTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
40 changes: 40 additions & 0 deletions src/test/java/com/rest/api/test/SubtractionControllerTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}