diff --git a/pom.xml b/pom.xml
index cd417de..109df82 100644
--- a/pom.xml
+++ b/pom.xml
@@ -23,6 +23,17 @@
pom
import
+
+
+ net.bytebuddy
+ byte-buddy
+ 1.12.9
+
+
+ net.bytebuddy
+ byte-buddy-agent
+ 1.12.9
+
@@ -67,6 +78,30 @@
test
+
+ com.github.tomakehurst
+ wiremock
+ 2.27.2
+
+
+
+ org.mockito
+ mockito-core
+ 2.23.4
+
+
+
+ net.bytebuddy
+ byte-buddy
+ 1.12.9
+
+
+
+ net.bytebuddy
+ byte-buddy-agent
+ 1.12.9
+
+
junit
junit
@@ -85,6 +120,14 @@
1.0.1
test
+
+
+ org.junit.jupiter
+ junit-jupiter
+ 5.9.3
+ test
+
+
@@ -120,6 +163,11 @@
11
+
+ org.apache.maven.plugins
+ maven-resources-plugin
+ 3.3.1
+
diff --git a/src/main/java/com/gmgauthier/client/CalculatorApp.java b/src/main/java/com/gmgauthier/client/CalculatorApp.java
new file mode 100644
index 0000000..5910bd9
--- /dev/null
+++ b/src/main/java/com/gmgauthier/client/CalculatorApp.java
@@ -0,0 +1,67 @@
+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 CalculatorApp {
+
+ private static final String ROOT_URL = "http://localhost:8000/";
+ 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/client/CalculatorClient.java b/src/main/java/com/gmgauthier/client/CalculatorClient.java
index 552e738..b5e77c2 100644
--- a/src/main/java/com/gmgauthier/client/CalculatorClient.java
+++ b/src/main/java/com/gmgauthier/client/CalculatorClient.java
@@ -12,49 +12,55 @@ import java.net.http.HttpResponse;
public class CalculatorClient {
- private static final String ROOT_URL = "http://localhost:8000/";
+ private String DEFAULT_ROOT_URL = "http://localhost:8000";
static HttpClient httpClient;
- public static void main(String[] args)
- throws URISyntaxException, IOException, InterruptedException {
+ public CalculatorClient() {
+ httpClient = HttpClient.newHttpClient();
+ }
+
+ public CalculatorClient(String serverUrl) {
+ this.DEFAULT_ROOT_URL = serverUrl;
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)
+ public Integer getSum(Integer operanda, Integer operandb)
throws URISyntaxException, IOException, InterruptedException {
- URI addUrl = new URI(ROOT_URL + "sum");
+ URI addUrl = new URI(DEFAULT_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)
+ public Integer getDifference(Integer operanda, Integer operandb)
throws URISyntaxException, IOException, InterruptedException {
- URI addUrl = new URI(ROOT_URL + "difference");
+ URI addUrl = new URI(DEFAULT_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)
+ public Integer getProduct(Integer operanda, Integer operandb)
throws URISyntaxException, IOException, InterruptedException {
- URI addUrl = new URI(ROOT_URL + "product");
+ URI addUrl = new URI(DEFAULT_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)
+ public BigDecimal getQuotient(Integer operanda, Integer operandb)
throws URISyntaxException, IOException, InterruptedException {
- URI addUrl = new URI(ROOT_URL + "quotient");
+ URI addUrl = new URI(DEFAULT_ROOT_URL + "/quotient");
JSONObject postJson = new JSONObject().put("values", new Integer[] {operanda, operandb});
- return (BigDecimal) new JSONObject(makeRequest(addUrl, postJson).body()).get("quotient");
+ JSONObject response = new JSONObject(makeRequest(addUrl, postJson).body());
+ String val = response.get("quotient").toString();
+ BigDecimal quotient;
+ try {
+ quotient = BigDecimal.valueOf(Long.parseLong(val));
+ } catch (Exception e){
+ quotient = BigDecimal.valueOf(Integer.parseInt(val));
+ }
+ return quotient;
}
- private static HttpResponse makeRequest(URI uri, JSONObject jsonBody)
+ private HttpResponse makeRequest(URI uri, JSONObject jsonBody)
throws IOException, InterruptedException {
HttpRequest httpRequest = HttpRequest.newBuilder()
.header("Content-Type","application/json")
@@ -63,5 +69,4 @@ public class CalculatorClient {
.build();
return httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
}
-
}
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
index 059b55a..555b239 100644
--- a/src/main/resources/application.properties
+++ b/src/main/resources/application.properties
@@ -6,3 +6,7 @@ server.port=8000
#Open "http://0.0.0.0:8000/h2-console" and hit "Connect" button
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
+
+spring.main.banner-mode=off
+logging.level.org.springframework=OFF
+logging.level.root=OFF
\ No newline at end of file
diff --git a/src/test/java/com/gmgauthier/client/requests/CalculatorClientTests.java b/src/test/java/com/gmgauthier/client/requests/CalculatorClientTests.java
index 324019f..bd123a5 100644
--- a/src/test/java/com/gmgauthier/client/requests/CalculatorClientTests.java
+++ b/src/test/java/com/gmgauthier/client/requests/CalculatorClientTests.java
@@ -1,18 +1,88 @@
package com.gmgauthier.client.requests;
+import com.github.tomakehurst.wiremock.client.WireMock;
+import com.github.tomakehurst.wiremock.junit.WireMockRule;
+import com.gmgauthier.client.CalculatorClient;
import com.hackerrank.test.utility.OrderedTestRunner;
+import org.junit.Before;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.jupiter.api.Assertions;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.net.URISyntaxException;
+
+import static com.github.tomakehurst.wiremock.client.WireMock.*;
+
@SpringBootTest
@RunWith(OrderedTestRunner.class)
public class CalculatorClientTests {
- @Test
- public void testDummy() throws Exception {
- assert true;
+ @Rule
+ public WireMockRule wireMockRule = new WireMockRule(8000);
+ //public WireMockServer wms = new WireMockServer(9999);
+
+ @Before
+ public void reset() {
+ WireMock.reset();
}
+ @Test
+ public void testAddition() throws URISyntaxException, IOException, InterruptedException {
+ stubFor(
+ post(urlEqualTo("/sum"))
+ .withHeader("Content-Type", equalTo("application/json"))
+ .willReturn(aResponse()
+ .withStatus(200)
+ .withHeader("Content-Type", "application/json")
+ .withBody("{\"sum\": 4}")));
+ CalculatorClient calc = new CalculatorClient();
+ Integer resp = calc.getSum(2,2);//two plus two
+ Assertions.assertEquals(4, resp.intValue());
+ }
+ @Test
+ public void testSubtraction() throws URISyntaxException, IOException, InterruptedException {
+ stubFor(
+ post(urlEqualTo("/difference"))
+ .withHeader("Content-Type", equalTo("application/json"))
+ .willReturn(aResponse()
+ .withStatus(200)
+ .withHeader("Content-Type", "application/json")
+ .withBody("{\"difference\": 4}")));
+ CalculatorClient calc = new CalculatorClient();
+ Integer resp = calc.getDifference(6,2); //six minus two
+ Assertions.assertEquals(4, resp.intValue());
+ }
+
+ @Test
+ public void testMultiplication() throws URISyntaxException, IOException, InterruptedException {
+ stubFor(
+ post(urlEqualTo("/product"))
+ .withHeader("Content-Type", equalTo("application/json"))
+ .willReturn(aResponse()
+ .withStatus(200)
+ .withHeader("Content-Type", "application/json")
+ .withBody("{\"product\": 4}")));
+ CalculatorClient calc = new CalculatorClient();
+ Integer resp = calc.getProduct(2,2);//two times two
+ Assertions.assertEquals(4, resp.intValue());
+ }
+
+ @Test
+ public void testDivision() throws URISyntaxException, IOException, InterruptedException {
+ stubFor(
+ post(urlEqualTo("/quotient"))
+ .withHeader("Content-Type", equalTo("application/json"))
+ .willReturn(aResponse()
+ .withStatus(200)
+ .withHeader("Content-Type", "application/json")
+ .withBody("{\"quotient\": 4}")));
+ CalculatorClient calc = new CalculatorClient();
+ BigDecimal resp = calc.getQuotient(8,2);//eight divided by two
+ Assertions.assertEquals(BigDecimal.valueOf(4), resp);
+ }
}
diff --git a/src/test/resources/logback-test.xml b/src/test/resources/logback-test.xml
new file mode 100644
index 0000000..4d43cb6
--- /dev/null
+++ b/src/test/resources/logback-test.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file