fix division tests

This commit is contained in:
Greg Gauthier 2023-05-29 13:27:53 +01:00
parent 137b1ecfc9
commit 1b6b1a50db
3 changed files with 34 additions and 12 deletions

View File

@ -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<String> makeRequest(URI uri, JSONObject jsonBody)

View File

@ -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);
}
}

View File

@ -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);
}
}