diff --git a/src/main/java/com/gmgauthier/client/CalculatorClient.java b/src/main/java/com/gmgauthier/client/CalculatorClient.java index b5e77c2..6a48492 100644 --- a/src/main/java/com/gmgauthier/client/CalculatorClient.java +++ b/src/main/java/com/gmgauthier/client/CalculatorClient.java @@ -45,19 +45,13 @@ public class CalculatorClient { return (Integer) new JSONObject(makeRequest(addUrl, postJson).body()).get("product"); } - public BigDecimal getQuotient(Integer operanda, Integer operandb) + public Double getQuotient(Integer operanda, Integer operandb) throws URISyntaxException, IOException, InterruptedException { URI addUrl = new URI(DEFAULT_ROOT_URL + "/quotient"); JSONObject postJson = new JSONObject().put("values", new Integer[] {operanda, operandb}); 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; + return Double.valueOf(val); } private HttpResponse makeRequest(URI uri, JSONObject jsonBody) diff --git a/src/test/java/com/gmgauthier/client/requests/CalculatorClientTests.java b/src/test/java/com/gmgauthier/client/requests/CalculatorClientTests.java index bcc8894..807b72a 100644 --- a/src/test/java/com/gmgauthier/client/requests/CalculatorClientTests.java +++ b/src/test/java/com/gmgauthier/client/requests/CalculatorClientTests.java @@ -11,7 +11,6 @@ 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.*; @@ -73,7 +72,7 @@ public class CalculatorClientTests { } @Test - public void testDivision() throws URISyntaxException, IOException, InterruptedException { + public void testWholeNumberDivision() throws URISyntaxException, IOException, InterruptedException { stubFor( post(urlEqualTo("/quotient")) .withHeader("Content-Type", equalTo("application/json")) @@ -82,7 +81,21 @@ public class CalculatorClientTests { .withHeader("Content-Type", "application/json") .withBody("{\"quotient\": 4}"))); CalculatorClient calc = new CalculatorClient(); - BigDecimal resp = calc.getQuotient(8,2);//eight divided by two - assertEquals(BigDecimal.valueOf(4), resp); + Double resp = calc.getQuotient(8,2);//eight divided by two + assertEquals(Double.valueOf(4), resp); + } + + @Test + public void testDecimalDivision() 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\": 3.8}"))); + CalculatorClient calc = new CalculatorClient(); + Double resp = calc.getQuotient(19,5); + assertEquals(Double.valueOf(3.8), resp); } } diff --git a/src/test/java/com/gmgauthier/server/requests/CalculatorControllerTest.java b/src/test/java/com/gmgauthier/server/requests/CalculatorControllerTest.java index 2686c10..fef8a25 100644 --- a/src/test/java/com/gmgauthier/server/requests/CalculatorControllerTest.java +++ b/src/test/java/com/gmgauthier/server/requests/CalculatorControllerTest.java @@ -145,4 +145,19 @@ public class CalculatorControllerTest { Assert.assertEquals("{\"quotient\":2}", response); } + + @Test + public void testDecimalQuotients() throws Exception { + String response = mockMvc.perform( + MockMvcRequestBuilders.post("/quotient") + .content("{\"values\":[19,5]}") + .contentType("application/json") + ) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andReturn() + .getResponse() + .getContentAsString(); + + Assert.assertEquals("{\"quotient\":3.8}", response); + } }