fix division tests
This commit is contained in:
parent
137b1ecfc9
commit
1b6b1a50db
@ -45,19 +45,13 @@ public class CalculatorClient {
|
|||||||
return (Integer) new JSONObject(makeRequest(addUrl, postJson).body()).get("product");
|
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 {
|
throws URISyntaxException, IOException, InterruptedException {
|
||||||
URI addUrl = new URI(DEFAULT_ROOT_URL + "/quotient");
|
URI addUrl = new URI(DEFAULT_ROOT_URL + "/quotient");
|
||||||
JSONObject postJson = new JSONObject().put("values", new Integer[] {operanda, operandb});
|
JSONObject postJson = new JSONObject().put("values", new Integer[] {operanda, operandb});
|
||||||
JSONObject response = new JSONObject(makeRequest(addUrl, postJson).body());
|
JSONObject response = new JSONObject(makeRequest(addUrl, postJson).body());
|
||||||
String val = response.get("quotient").toString();
|
String val = response.get("quotient").toString();
|
||||||
BigDecimal quotient;
|
return Double.valueOf(val);
|
||||||
try {
|
|
||||||
quotient = BigDecimal.valueOf(Long.parseLong(val));
|
|
||||||
} catch (Exception e){
|
|
||||||
quotient = BigDecimal.valueOf(Integer.parseInt(val));
|
|
||||||
}
|
|
||||||
return quotient;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private HttpResponse<String> makeRequest(URI uri, JSONObject jsonBody)
|
private HttpResponse<String> makeRequest(URI uri, JSONObject jsonBody)
|
||||||
|
@ -11,7 +11,6 @@ import org.junit.runner.RunWith;
|
|||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.math.BigDecimal;
|
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
|
|
||||||
import static com.github.tomakehurst.wiremock.client.WireMock.*;
|
import static com.github.tomakehurst.wiremock.client.WireMock.*;
|
||||||
@ -73,7 +72,7 @@ public class CalculatorClientTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDivision() throws URISyntaxException, IOException, InterruptedException {
|
public void testWholeNumberDivision() throws URISyntaxException, IOException, InterruptedException {
|
||||||
stubFor(
|
stubFor(
|
||||||
post(urlEqualTo("/quotient"))
|
post(urlEqualTo("/quotient"))
|
||||||
.withHeader("Content-Type", equalTo("application/json"))
|
.withHeader("Content-Type", equalTo("application/json"))
|
||||||
@ -82,7 +81,21 @@ public class CalculatorClientTests {
|
|||||||
.withHeader("Content-Type", "application/json")
|
.withHeader("Content-Type", "application/json")
|
||||||
.withBody("{\"quotient\": 4}")));
|
.withBody("{\"quotient\": 4}")));
|
||||||
CalculatorClient calc = new CalculatorClient();
|
CalculatorClient calc = new CalculatorClient();
|
||||||
BigDecimal resp = calc.getQuotient(8,2);//eight divided by two
|
Double resp = calc.getQuotient(8,2);//eight divided by two
|
||||||
assertEquals(BigDecimal.valueOf(4), resp);
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -145,4 +145,19 @@ public class CalculatorControllerTest {
|
|||||||
|
|
||||||
Assert.assertEquals("{\"quotient\":2}", response);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user