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
4 changes: 3 additions & 1 deletion .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
Expand All @@ -22,9 +23,10 @@
<attributes>
<attribute name="test" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
Expand Down
11 changes: 11 additions & 0 deletions .project
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,15 @@
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
<filteredResources>
<filter>
<id>1737349338393</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
149 changes: 76 additions & 73 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,74 +1,77 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.microservices.example</groupId>
<artifactId>spring-boot-microservices</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Spring Boot Microservices Example</name>
<description>Spring Boot Microservices.</description>

<properties>
<start-class>com.microservices.example.Main</start-class>
<java.version>17</java.version>
</properties>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.4</version>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<!-- <version>Finchley.RELEASE</version> -->
<version>2020.0.3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.microservices.example</groupId>
<artifactId>spring-boot-microservices</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Spring Boot Microservices Example</name>
<description>Spring Boot Microservices.</description>
<properties>
<start-class>com.microservices.example.Main</start-class>
<java.version>17</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.4</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2020.0.3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
26 changes: 26 additions & 0 deletions src/main/java/com/microservices/example/web/TestController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.microservices.example.web;
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 java.util.logging.Logger;
@Controller
public class TestController {
@Autowired
private TestService testService;
protected Logger logger = Logger.getLogger(TestController.class.getName());
@RequestMapping("/run-tests")
public String runTests(Model model) {
try {
logger.info("Triggering test execution.");
String results = testService.runAllTests();
model.addAttribute("results", results);
logger.info("Test execution completed successfully.");
} catch (Exception e) {
logger.severe("Error occurred while running tests: " + e.getMessage());
logger.severe("Stack Trace: " + e.getStackTrace());
model.addAttribute("results", "Error occurred while running tests. Please check the logs.");
}
return "testResults";
}
}
28 changes: 28 additions & 0 deletions src/main/java/com/microservices/example/web/TestService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.microservices.example.web;
import com.rest.api.test.ArithmeticIntegrationTest;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.logging.Logger;
import org.springframework.stereotype.Service;
@Service
public class TestService {
protected Logger logger = Logger.getLogger(TestService.class.getName());
public String runAllTests() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(outputStream);
PrintStream originalOut = System.out;
try {
System.setOut(printStream);
logger.info("Running all integration tests.");
ArithmeticIntegrationTest.main(new String[]{}); // Run the integration tests
System.out.flush();
logger.info("Integration tests executed successfully.");
} catch (Exception e) {
logger.severe("Error occurred while running integration tests: " + e.getMessage());
logger.severe("Stack Trace: " + e.getStackTrace());
} finally {
System.setOut(originalOut);
}
return outputStream.toString(); // Return the captured output
}
}
68 changes: 34 additions & 34 deletions src/main/resources/web-server/templates/index.html
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
<!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>
<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>
<hr>
<h3>Run Integration Tests</h3>
<form action="/run-tests">
<input type="submit" value="Run Tests">
</form>
<div th:replace="footer :: copy"></div>
</body>
</html>
10 changes: 10 additions & 0 deletions src/main/resources/web-server/templates/testResults.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!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>Test Results</h1>
<pre th:text="${results}">Test results will appear here</pre>
<a href="/">Go back</a>
<div th:replace="footer :: copy"></div>
</body>
</html>
32 changes: 32 additions & 0 deletions src/test/java/com/rest/api/test/AdditionsAPITest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.rest.api.test;
import java.util.logging.Logger;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
public class AdditionsAPITest {
private static final Logger logger = Logger.getLogger(AdditionsAPITest.class.getName());
private static final String BASE_URL = "http://localhost:2222/add";
public static void main(String[] args) {
testAddition(5, 10);
testAddition(0, 0);
testAddition(-5, 5);
testAddition(100, 200);
}
private static void testAddition(int addend1, int addend2) {
String url = BASE_URL + "?addend1=" + addend1 + "&addend2=" + addend2;
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpGet request = new HttpGet(url);
HttpResponse response = httpClient.execute(request);
if (response.getStatusLine().getStatusCode() == 200) {
logger.info("Response for addend1=" + addend1 + " & addend2=" + addend2 + ": " +
new java.io.BufferedReader(new java.io.InputStreamReader(response.getEntity().getContent())).lines().collect(java.util.stream.Collectors.joining("\n")));
} else {
logger.warning("Failed to call addition API: " + response.getStatusLine().getStatusCode());
}
} catch (Exception e) {
logger.severe("Exception occurred while calling addition API: " + e.getMessage());
e.printStackTrace();
}
}
}
Loading