From 51f9390cec3e466cc81ee41b11cf7be4348721e2 Mon Sep 17 00:00:00 2001 From: Greg Gauthier Date: Thu, 25 May 2023 21:32:24 +0100 Subject: [PATCH] add client for server; do some refactoring --- pom.xml | 8 +++ .../gmgauthier/client/CalculatorClient.java | 68 +++++++++++++++++++ .../CalculatorServer.java} | 6 +- .../controllers/CalculatorController.java | 2 +- .../requests/CalculatorClientTests.java | 13 ++++ .../requests/CalculatorControllerTest.java | 2 +- 6 files changed, 94 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/gmgauthier/client/CalculatorClient.java rename src/main/java/com/gmgauthier/{Application.java => server/CalculatorServer.java} (66%) rename src/main/java/com/gmgauthier/{ => server}/controllers/CalculatorController.java (98%) create mode 100644 src/test/java/com/gmgauthier/client/requests/CalculatorClientTests.java rename src/test/java/com/gmgauthier/{ => server}/requests/CalculatorControllerTest.java (99%) diff --git a/pom.xml b/pom.xml index c4b3bd3..f930fc8 100644 --- a/pom.xml +++ b/pom.xml @@ -111,6 +111,14 @@ + + org.apache.maven.plugins + maven-compiler-plugin + + 11 + 11 + + diff --git a/src/main/java/com/gmgauthier/client/CalculatorClient.java b/src/main/java/com/gmgauthier/client/CalculatorClient.java new file mode 100644 index 0000000..f7cfab1 --- /dev/null +++ b/src/main/java/com/gmgauthier/client/CalculatorClient.java @@ -0,0 +1,68 @@ +package com.gmgauthier.client; + +import org.json.JSONObject; + +import java.io.IOException; +import java.math.BigDecimal; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; + +public class CalculatorClient { + + private static final String ROOT_URL = "http://localhost:8000/"; + static URI targetURI; + static HttpClient httpClient; + + public static void main(String[] args) + throws URISyntaxException, IOException, InterruptedException { + httpClient = HttpClient.newHttpClient(); + + System.out.println(getSum(10,20)); + System.out.println(getProduct(123, 444)); + System.out.println(getDifference(846, 233)); + System.out.println(getQuotient(999, 4)); + + } + + public static Integer getSum(Integer operanda, Integer operandb) + throws URISyntaxException, IOException, InterruptedException { + URI addUrl = new URI(ROOT_URL + "sum"); + JSONObject postJson = new JSONObject().put("values", new Integer[] {operanda, operandb}); + return (Integer) new JSONObject(makeRequest(addUrl, postJson).body()).get("sum"); + } + + public static Integer getDifference(Integer operanda, Integer operandb) + throws URISyntaxException, IOException, InterruptedException { + URI addUrl = new URI(ROOT_URL + "difference"); + JSONObject postJson = new JSONObject().put("values", new Integer[] {operanda, operandb}); + return (Integer) new JSONObject(makeRequest(addUrl, postJson).body()).get("difference"); + } + + public static Integer getProduct(Integer operanda, Integer operandb) + throws URISyntaxException, IOException, InterruptedException { + URI addUrl = new URI(ROOT_URL + "product"); + JSONObject postJson = new JSONObject().put("values", new Integer[] {operanda, operandb}); + return (Integer) new JSONObject(makeRequest(addUrl, postJson).body()).get("product"); + } + + public static BigDecimal getQuotient(Integer operanda, Integer operandb) + throws URISyntaxException, IOException, InterruptedException { + URI addUrl = new URI(ROOT_URL + "quotient"); + JSONObject postJson = new JSONObject().put("values", new Integer[] {operanda, operandb}); + return (BigDecimal) new JSONObject(makeRequest(addUrl, postJson).body()).get("quotient"); + } + + private static HttpResponse makeRequest(URI uri, JSONObject jsonBody) + throws IOException, InterruptedException { + HttpRequest httpRequest = HttpRequest.newBuilder() + .header("Content-Type","application/json") + .uri(uri) + .POST(HttpRequest.BodyPublishers.ofString(String.valueOf(jsonBody))) + .build(); + return httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString()); + } + +} diff --git a/src/main/java/com/gmgauthier/Application.java b/src/main/java/com/gmgauthier/server/CalculatorServer.java similarity index 66% rename from src/main/java/com/gmgauthier/Application.java rename to src/main/java/com/gmgauthier/server/CalculatorServer.java index 1f16e50..7bb271b 100644 --- a/src/main/java/com/gmgauthier/Application.java +++ b/src/main/java/com/gmgauthier/server/CalculatorServer.java @@ -1,4 +1,4 @@ -package com.gmgauthier; +package com.gmgauthier.server; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -8,8 +8,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; * Spring Boot application starter class */ @SpringBootApplication -public class Application { +public class CalculatorServer { public static void main(String[] args) { - SpringApplication.run(Application.class, args); + SpringApplication.run(CalculatorServer.class, args); } } diff --git a/src/main/java/com/gmgauthier/controllers/CalculatorController.java b/src/main/java/com/gmgauthier/server/controllers/CalculatorController.java similarity index 98% rename from src/main/java/com/gmgauthier/controllers/CalculatorController.java rename to src/main/java/com/gmgauthier/server/controllers/CalculatorController.java index 3cd6e90..e6bd9d4 100644 --- a/src/main/java/com/gmgauthier/controllers/CalculatorController.java +++ b/src/main/java/com/gmgauthier/server/controllers/CalculatorController.java @@ -1,4 +1,4 @@ -package com.gmgauthier.controllers; +package com.gmgauthier.server.controllers; import org.json.JSONObject; import org.springframework.http.HttpStatus; diff --git a/src/test/java/com/gmgauthier/client/requests/CalculatorClientTests.java b/src/test/java/com/gmgauthier/client/requests/CalculatorClientTests.java new file mode 100644 index 0000000..5248dec --- /dev/null +++ b/src/test/java/com/gmgauthier/client/requests/CalculatorClientTests.java @@ -0,0 +1,13 @@ +package com.gmgauthier.client.requests; + +import org.junit.Test; + +public class CalculatorClientTests { + + @Test + public void testDummy() throws Exception { + assert true; + } + + +} diff --git a/src/test/java/com/gmgauthier/requests/CalculatorControllerTest.java b/src/test/java/com/gmgauthier/server/requests/CalculatorControllerTest.java similarity index 99% rename from src/test/java/com/gmgauthier/requests/CalculatorControllerTest.java rename to src/test/java/com/gmgauthier/server/requests/CalculatorControllerTest.java index 76d839e..2686c10 100644 --- a/src/test/java/com/gmgauthier/requests/CalculatorControllerTest.java +++ b/src/test/java/com/gmgauthier/server/requests/CalculatorControllerTest.java @@ -1,4 +1,4 @@ -package com.gmgauthier.requests; +package com.gmgauthier.server.requests; import com.hackerrank.test.utility.OrderedTestRunner; import com.hackerrank.test.utility.TestWatchman;