diff --git a/.classpath b/.classpath index cdd21e0..1988dd2 100644 --- a/.classpath +++ b/.classpath @@ -9,6 +9,7 @@ + @@ -22,9 +23,10 @@ + - + diff --git a/.project b/.project index bcce82d..08ea1bb 100644 --- a/.project +++ b/.project @@ -25,4 +25,15 @@ org.eclipse.jdt.core.javanature org.eclipse.m2e.core.maven2Nature + + + 1737349338393 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/pom.xml b/pom.xml index 254c0a5..ffa6559 100644 --- a/pom.xml +++ b/pom.xml @@ -1,74 +1,77 @@ - - 4.0.0 - com.microservices.example - spring-boot-microservices - 0.0.1-SNAPSHOT - Spring Boot Microservices Example - Spring Boot Microservices. - - - com.microservices.example.Main - 17 - - - - org.springframework.boot - spring-boot-starter-parent - 3.1.4 - - - - - org.springframework.boot - spring-boot-starter - - - org.apache.httpcomponents - httpclient - 4.1.1 - - - org.springframework.boot - spring-boot-starter-web - - - - org.springframework.boot - spring-boot-starter-thymeleaf - - - - org.springframework.cloud - spring-cloud-starter - - - - org.springframework.cloud - spring-cloud-starter-netflix-eureka-server - - - - - - - org.springframework.cloud - spring-cloud-dependencies - - 2020.0.3 - pom - import - - - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - + + 4.0.0 + com.microservices.example + spring-boot-microservices + 0.0.1-SNAPSHOT + Spring Boot Microservices Example + Spring Boot Microservices. + + com.microservices.example.Main + 17 + + + org.springframework.boot + spring-boot-starter-parent + 3.1.4 + + + + org.springframework.boot + spring-boot-starter + + + org.apache.httpcomponents + httpclient + 4.1.1 + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + org.springframework.cloud + spring-cloud-starter + + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-server + + + org.junit.jupiter + junit-jupiter-api + 5.7.0 + test + + + junit + junit + 4.13.2 + test + + + + + + org.springframework.cloud + spring-cloud-dependencies + 2020.0.3 + pom + import + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + \ No newline at end of file diff --git a/src/main/java/com/microservices/example/web/TestController.java b/src/main/java/com/microservices/example/web/TestController.java new file mode 100644 index 0000000..565c435 --- /dev/null +++ b/src/main/java/com/microservices/example/web/TestController.java @@ -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"; + } +} \ No newline at end of file diff --git a/src/main/java/com/microservices/example/web/TestService.java b/src/main/java/com/microservices/example/web/TestService.java new file mode 100644 index 0000000..8c378d5 --- /dev/null +++ b/src/main/java/com/microservices/example/web/TestService.java @@ -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 + } +} \ 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..3b6897d 100644 --- a/src/main/resources/web-server/templates/index.html +++ b/src/main/resources/web-server/templates/index.html @@ -1,34 +1,34 @@ - - - - - - - -

Spring Boot Microservices Example

-

Addition Service

-
- First number:
- -
- Second number
- -

- -
-
-
- First number:
- -
- Second number
- -

- -
- -
- - - + + + + +

Spring Boot Microservices Example

+

Addition Service

+
+ First number:
+ +
+ Second number
+ +

+ +
+
+
+ First number:
+ +
+ Second number
+ +

+ +
+
+

Run Integration Tests

+
+ +
+
+ + \ No newline at end of file diff --git a/src/main/resources/web-server/templates/testResults.html b/src/main/resources/web-server/templates/testResults.html new file mode 100644 index 0000000..24af918 --- /dev/null +++ b/src/main/resources/web-server/templates/testResults.html @@ -0,0 +1,10 @@ + + + + +

Test Results

+
Test results will appear here
+ Go back +
+ + \ No newline at end of file diff --git a/src/test/java/com/rest/api/test/AdditionsAPITest.java b/src/test/java/com/rest/api/test/AdditionsAPITest.java new file mode 100644 index 0000000..e741669 --- /dev/null +++ b/src/test/java/com/rest/api/test/AdditionsAPITest.java @@ -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(); + } + } +} \ No newline at end of file diff --git a/src/test/java/com/rest/api/test/ArithmeticIntegrationTest.java b/src/test/java/com/rest/api/test/ArithmeticIntegrationTest.java new file mode 100644 index 0000000..e96524f --- /dev/null +++ b/src/test/java/com/rest/api/test/ArithmeticIntegrationTest.java @@ -0,0 +1,41 @@ +package com.rest.api.test; +import java.util.logging.Logger; +public class ArithmeticIntegrationTest { + private static final Logger logger = Logger.getLogger(ArithmeticIntegrationTest.class.getName()); + private static final String ADDITION_URL = "http://localhost:2222/add"; + private static final String SUBTRACTION_URL = "http://localhost:3333/subtract"; + public static void main(String[] args) { + // Test addition + testAddition(5, 10); + testAddition(0, 0); + testAddition(-5, 5); + testAddition(100, 200); + // Test subtraction + testSubtraction(10, 5); + testSubtraction(0, 0); + testSubtraction(5, 10); + testSubtraction(-5, -5); + } + private static void testAddition(int addend1, int addend2) { + String url = ADDITION_URL + "?addend1=" + addend1 + "&addend2=" + addend2; + try { + TestMyRESTAPI testApi = new TestMyRESTAPI(url); + String response = testApi.executeGETMethod(); + logger.info("Addition Response for addend1=" + addend1 + " & addend2=" + addend2 + ": " + response); + } catch (Exception e) { + logger.severe("Failed to call addition API: " + e.getMessage()); + e.printStackTrace(); + } + } + private static void testSubtraction(int minuend, int subtrahend) { + String url = SUBTRACTION_URL + "?minuend=" + minuend + "&subtrahend=" + subtrahend; + try { + TestMyRESTAPI testApi = new TestMyRESTAPI(url); + String response = testApi.executeGETMethod(); + logger.info("Subtraction Response for minuend=" + minuend + " & subtrahend=" + subtrahend + ": " + response); + } catch (Exception e) { + logger.severe("Failed to call subtraction API: " + e.getMessage()); + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/src/test/java/com/rest/api/test/ArithmeticTestSuite.java b/src/test/java/com/rest/api/test/ArithmeticTestSuite.java new file mode 100644 index 0000000..7eb47ae --- /dev/null +++ b/src/test/java/com/rest/api/test/ArithmeticTestSuite.java @@ -0,0 +1,13 @@ +package com.rest.api.test; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +// Define the test suite +@RunWith(Suite.class) +@Suite.SuiteClasses({ + AdditionsAPITest.class, + SubtractionAPITest.class, + ArithmeticIntegrationTest.class +}) +public class ArithmeticTestSuite { + // This class remains empty, it is used only as a holder for the above annotations +} \ No newline at end of file diff --git a/src/test/java/com/rest/api/test/SubtractionAPITest.java b/src/test/java/com/rest/api/test/SubtractionAPITest.java new file mode 100644 index 0000000..e472010 --- /dev/null +++ b/src/test/java/com/rest/api/test/SubtractionAPITest.java @@ -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 SubtractionAPITest { + private static final Logger logger = Logger.getLogger(SubtractionAPITest.class.getName()); + private static final String BASE_URL = "http://localhost:3333/subtract"; + public static void main(String[] args) { + testSubtraction(10, 5); + testSubtraction(0, 0); + testSubtraction(5, 10); + testSubtraction(-5, -5); + } + private static void testSubtraction(int minuend, int subtrahend) { + String url = BASE_URL + "?minuend=" + minuend + "&subtrahend=" + subtrahend; + try (CloseableHttpClient httpClient = HttpClients.createDefault()) { + HttpGet request = new HttpGet(url); + HttpResponse response = httpClient.execute(request); + if (response.getStatusLine().getStatusCode() == 200) { + logger.info("Response for minuend=" + minuend + " & subtrahend=" + subtrahend + ": " + + 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 subtraction API: " + response.getStatusLine().getStatusCode()); + } + } catch (Exception e) { + logger.severe("Exception occurred while calling subtraction API: " + e.getMessage()); + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/target/classes/META-INF/maven/com.microservices.example/spring-boot-microservices/pom.properties b/target/classes/META-INF/maven/com.microservices.example/spring-boot-microservices/pom.properties deleted file mode 100644 index 0705adc..0000000 --- a/target/classes/META-INF/maven/com.microservices.example/spring-boot-microservices/pom.properties +++ /dev/null @@ -1,7 +0,0 @@ -#Generated by Maven Integration for Eclipse -#Tue Dec 05 15:37:38 IST 2023 -m2e.projectLocation=C\:\\Java\\spring-boot-microservices\\spring-boot-microservices -m2e.projectName=spring-boot-microservices -groupId=com.microservices.example -artifactId=spring-boot-microservices -version=0.0.1-SNAPSHOT diff --git a/target/classes/META-INF/maven/com.microservices.example/spring-boot-microservices/pom.xml b/target/classes/META-INF/maven/com.microservices.example/spring-boot-microservices/pom.xml deleted file mode 100644 index 254c0a5..0000000 --- a/target/classes/META-INF/maven/com.microservices.example/spring-boot-microservices/pom.xml +++ /dev/null @@ -1,74 +0,0 @@ - - 4.0.0 - com.microservices.example - spring-boot-microservices - 0.0.1-SNAPSHOT - Spring Boot Microservices Example - Spring Boot Microservices. - - - com.microservices.example.Main - 17 - - - - org.springframework.boot - spring-boot-starter-parent - 3.1.4 - - - - - org.springframework.boot - spring-boot-starter - - - org.apache.httpcomponents - httpclient - 4.1.1 - - - org.springframework.boot - spring-boot-starter-web - - - - org.springframework.boot - spring-boot-starter-thymeleaf - - - - org.springframework.cloud - spring-cloud-starter - - - - org.springframework.cloud - spring-cloud-starter-netflix-eureka-server - - - - - - - org.springframework.cloud - spring-cloud-dependencies - - 2020.0.3 - pom - import - - - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - - \ No newline at end of file diff --git a/target/classes/com/microservices/example/Main.class b/target/classes/com/microservices/example/Main.class index ef430d3..3667ecc 100644 Binary files a/target/classes/com/microservices/example/Main.class and b/target/classes/com/microservices/example/Main.class differ diff --git a/target/classes/com/microservices/example/registration/EurekaServer.class b/target/classes/com/microservices/example/registration/EurekaServer.class index f3f9483..eddd0fa 100644 Binary files a/target/classes/com/microservices/example/registration/EurekaServer.class and b/target/classes/com/microservices/example/registration/EurekaServer.class differ diff --git a/target/classes/com/microservices/example/rest/addition/AdditionController.class b/target/classes/com/microservices/example/rest/addition/AdditionController.class index 1c9b448..81781c0 100644 Binary files a/target/classes/com/microservices/example/rest/addition/AdditionController.class and b/target/classes/com/microservices/example/rest/addition/AdditionController.class differ diff --git a/target/classes/com/microservices/example/rest/addition/AdditionServer.class b/target/classes/com/microservices/example/rest/addition/AdditionServer.class index 7f35d9f..d63647e 100644 Binary files a/target/classes/com/microservices/example/rest/addition/AdditionServer.class and b/target/classes/com/microservices/example/rest/addition/AdditionServer.class differ diff --git a/target/classes/com/microservices/example/rest/addition/HomeController.class b/target/classes/com/microservices/example/rest/addition/HomeController.class index 61e483c..b38f0ad 100644 Binary files a/target/classes/com/microservices/example/rest/addition/HomeController.class and b/target/classes/com/microservices/example/rest/addition/HomeController.class differ diff --git a/target/classes/com/microservices/example/rest/subtraction/HomeController.class b/target/classes/com/microservices/example/rest/subtraction/HomeController.class index 12cfcbc..0e3292e 100644 Binary files a/target/classes/com/microservices/example/rest/subtraction/HomeController.class and b/target/classes/com/microservices/example/rest/subtraction/HomeController.class differ diff --git a/target/classes/com/microservices/example/rest/subtraction/SubtractionController.class b/target/classes/com/microservices/example/rest/subtraction/SubtractionController.class index dc42871..4ec5fba 100644 Binary files a/target/classes/com/microservices/example/rest/subtraction/SubtractionController.class and b/target/classes/com/microservices/example/rest/subtraction/SubtractionController.class differ diff --git a/target/classes/com/microservices/example/rest/subtraction/SubtractionServer.class b/target/classes/com/microservices/example/rest/subtraction/SubtractionServer.class index 4711602..3232b0a 100644 Binary files a/target/classes/com/microservices/example/rest/subtraction/SubtractionServer.class and b/target/classes/com/microservices/example/rest/subtraction/SubtractionServer.class differ diff --git a/target/classes/com/microservices/example/web/HomeController.class b/target/classes/com/microservices/example/web/HomeController.class index a995214..9eeb86e 100644 Binary files a/target/classes/com/microservices/example/web/HomeController.class and b/target/classes/com/microservices/example/web/HomeController.class differ diff --git a/target/classes/com/microservices/example/web/TestController.class b/target/classes/com/microservices/example/web/TestController.class new file mode 100644 index 0000000..925e5f5 Binary files /dev/null and b/target/classes/com/microservices/example/web/TestController.class differ diff --git a/target/classes/com/microservices/example/web/TestService.class b/target/classes/com/microservices/example/web/TestService.class new file mode 100644 index 0000000..5446f9c Binary files /dev/null and b/target/classes/com/microservices/example/web/TestService.class differ diff --git a/target/classes/com/microservices/example/web/WebAdditionService.class b/target/classes/com/microservices/example/web/WebAdditionService.class index 97153b1..827569b 100644 Binary files a/target/classes/com/microservices/example/web/WebAdditionService.class and b/target/classes/com/microservices/example/web/WebAdditionService.class differ diff --git a/target/classes/com/microservices/example/web/WebArithmeticController.class b/target/classes/com/microservices/example/web/WebArithmeticController.class index 3d2f13b..169ff7a 100644 Binary files a/target/classes/com/microservices/example/web/WebArithmeticController.class and b/target/classes/com/microservices/example/web/WebArithmeticController.class differ diff --git a/target/classes/com/microservices/example/web/WebServer.class b/target/classes/com/microservices/example/web/WebServer.class index da195c2..4666005 100644 Binary files a/target/classes/com/microservices/example/web/WebServer.class and b/target/classes/com/microservices/example/web/WebServer.class differ diff --git a/target/classes/com/microservices/example/web/WebSubtractionService.class b/target/classes/com/microservices/example/web/WebSubtractionService.class index 5df6a3e..dccf5ce 100644 Binary files a/target/classes/com/microservices/example/web/WebSubtractionService.class and b/target/classes/com/microservices/example/web/WebSubtractionService.class differ diff --git a/target/classes/web-server/templates/index.html b/target/classes/web-server/templates/index.html index ea6937a..3b6897d 100644 --- a/target/classes/web-server/templates/index.html +++ b/target/classes/web-server/templates/index.html @@ -1,34 +1,34 @@ - - - - - - - -

Spring Boot Microservices Example

-

Addition Service

-
- First number:
- -
- Second number
- -

- -
-
-
- First number:
- -
- Second number
- -

- -
- -
- - - + + + + +

Spring Boot Microservices Example

+

Addition Service

+
+ First number:
+ +
+ Second number
+ +

+ +
+
+
+ First number:
+ +
+ Second number
+ +

+ +
+
+

Run Integration Tests

+
+ +
+
+ + \ No newline at end of file diff --git a/target/classes/web-server/templates/testResults.html b/target/classes/web-server/templates/testResults.html new file mode 100644 index 0000000..24af918 --- /dev/null +++ b/target/classes/web-server/templates/testResults.html @@ -0,0 +1,10 @@ + + + + +

Test Results

+
Test results will appear here
+ Go back +
+ + \ No newline at end of file diff --git a/target/test-classes/com/rest/api/test/AdditionsAPITest.class b/target/test-classes/com/rest/api/test/AdditionsAPITest.class new file mode 100644 index 0000000..50c576d Binary files /dev/null and b/target/test-classes/com/rest/api/test/AdditionsAPITest.class differ diff --git a/target/test-classes/com/rest/api/test/ArithmeticIntegrationTest.class b/target/test-classes/com/rest/api/test/ArithmeticIntegrationTest.class new file mode 100644 index 0000000..4aeeec2 Binary files /dev/null and b/target/test-classes/com/rest/api/test/ArithmeticIntegrationTest.class differ diff --git a/target/test-classes/com/rest/api/test/ArithmeticTestSuite.class b/target/test-classes/com/rest/api/test/ArithmeticTestSuite.class new file mode 100644 index 0000000..dddb0e9 Binary files /dev/null and b/target/test-classes/com/rest/api/test/ArithmeticTestSuite.class differ diff --git a/target/test-classes/com/rest/api/test/ChatGPTAPIExample$1.class b/target/test-classes/com/rest/api/test/ChatGPTAPIExample$1.class index e7eb4c2..2157e4f 100644 Binary files a/target/test-classes/com/rest/api/test/ChatGPTAPIExample$1.class and b/target/test-classes/com/rest/api/test/ChatGPTAPIExample$1.class differ diff --git a/target/test-classes/com/rest/api/test/ChatGPTAPIExample$2.class b/target/test-classes/com/rest/api/test/ChatGPTAPIExample$2.class index 8498d5c..4cf3fc0 100644 Binary files a/target/test-classes/com/rest/api/test/ChatGPTAPIExample$2.class and b/target/test-classes/com/rest/api/test/ChatGPTAPIExample$2.class differ diff --git a/target/test-classes/com/rest/api/test/ChatGPTAPIExample.class b/target/test-classes/com/rest/api/test/ChatGPTAPIExample.class index 0288e4d..ba692ee 100644 Binary files a/target/test-classes/com/rest/api/test/ChatGPTAPIExample.class and b/target/test-classes/com/rest/api/test/ChatGPTAPIExample.class differ diff --git a/target/test-classes/com/rest/api/test/SubtractionAPITest.class b/target/test-classes/com/rest/api/test/SubtractionAPITest.class new file mode 100644 index 0000000..360d7bb Binary files /dev/null and b/target/test-classes/com/rest/api/test/SubtractionAPITest.class differ diff --git a/target/test-classes/com/rest/api/test/TestMyRESTAPI.class b/target/test-classes/com/rest/api/test/TestMyRESTAPI.class index 84711db..fe710cf 100644 Binary files a/target/test-classes/com/rest/api/test/TestMyRESTAPI.class and b/target/test-classes/com/rest/api/test/TestMyRESTAPI.class differ