Adding validation to a field and tested using mockMVC
The controller class (controller.java) has this code.
if (farmerIds.contains(farmerId)) {
farmerUnionRequest farmerUnionRequest = new farmerUnionRequest(farmerId, customerId);
String encryptedRequest = encryptor.encrypt(StringUtil.toJsonString(farmerUnionRequest));
response = redirectService.getConfirmAccountResponse(headers, ImmutableMap.of("encryptedUnionId", encryptedRequest));
} else {
LOG.warn("Unknown farmer id: {}", farmerId);
response = new ResponseEntity("Unknown farmer id", BAD_REQUEST);
}
I noticed that there is another main field called customerId and it is not checked for null or empty.
I changed the controller class like this.
if (farmerIds.contains(farmerId)) {
if (!customerId.isEmpty()) { . // isEmpty takes care of null as well as empty
farmerFeedRequest farmerFeedRequest = new farmerFeedRequest(farmerId, customerId);
String encryptedRequest = encryptor.encrypt(StringUtil.toJsonString(farmerFeedRequest));
response = redirectService.getConfirmAccountResponse(headers, ImmutableMap.of("encryptedFeedId", encryptedRequest));
} else {
LOG.warn("Empty customer id for farmer id: {}", farmerId);
response = new ResponseEntity("Unknown customer id", BAD_REQUEST);
}
} else {
LOG.warn("Unknown farmer id: {}", farmerId);
response = new ResponseEntity("Unknown farmer id", BAD_REQUEST); /
}
But before I changed it, I added the following mockMVC test, failed it, wrote the code above to make the test pass, and created a pull request for review. This is TDD.
@Test
+ public void receiveFarmerRequestWithInvalidCustomerId() throws Exception {
+
+ MockHttpServletRequestBuilder request =
+ post("/api/farmersUnion")
+ .param("farmerId", "ZER-123") // this is the correct id
+ .param("clientId", "") // client id is empty
+ .header("Content-Type", "application/json")
+ .header("x-correlation-id", "correlationId");
+
+ mockMvc.perform(request).andExpect(status().isBadRequest());
+ }
Comments
Post a Comment