22

I have a calculator service that gets the operation type, num1 and num2 from the user. I need to validate that the user actually inputs these values and doesn't just leave it blank.

@RequestMapping(value = "/calculate")
@ResponseBody
public CalculationResult calculate(@RequestParam(name = "op") String operation, @RequestParam(name = "num1") Double num1, @RequestParam(name = "num2") Double num2) {
    System.out.print("Operation:" + operation);
    Double calculate = calculatorService.calculate(operation, num1, num2);
    return new CalculationResult(calculate);
}

I have an Integration test that I need to make pass as it is currently failing with error:

{\"timestamp\":1488875777084,\"status\":400,\"error\":\"Bad Request\",\"exception\":\"org.springframework.web.method.annotation.MethodArgumentTypeMismatchException\",\"message\":\"Failed to convert value of type 'java.lang.String' to required type 'java.lang.Double';

Below is my Test Case:

@Test
public void validates_all_parameters_are_set() throws Exception {
    ResponseEntity<String> response = template.getForEntity( "/calculate?op=&num1=&num2=",
            String.class);
    assertThat(response.getStatusCode(), equalTo(HttpStatus.BAD_REQUEST));
    assertThat(response.getBody(), equalTo("{\"error\":\"At least one parameter is invalid or not supplied\"}"));
}

I don't know how to validate this.

Arpit Aggarwal
  • 27,626
  • 16
  • 90
  • 108
Saakina
  • 275
  • 1
  • 2
  • 9

3 Answers3

45

I answered similar problem long before here which you can follow to write your test as well , as follows:

@Validated
public class CalculationController {

    @RequestMapping(value = "/calculate")
    @ResponseBody
    public CalculationResult calculate(
            @Valid @NotBlank @RequestParam(name = "op") String operation,
            @Valid @NotNull @RequestParam(name = "num1") Double num1,
            @Valid @NotNull @RequestParam(name = "num2") Double num2) {
        System.out.print("Operation:" + operation);
        Double calculate = calculatorService.calculate(operation, num1, num2);
        return new CalculationResult(calculate);
    }
}

Corresponding @Test should be modified to test for an array of "may not be null" message, as:

@Test
public void validates_all_parameters_are_set() throws Exception {
    ResponseEntity<String> response = template.getForEntity( "/calculate?op=&num1=&num2=",
                String.class);
    assertThat(response.getStatusCode(), equalTo(HttpStatus.BAD_REQUEST));
    assertThat(response.getBody(), equalTo("{\"error\":[\"may not be null\",\"may not be null\"]}"));
}
Community
  • 1
  • 1
Arpit Aggarwal
  • 27,626
  • 16
  • 90
  • 108
  • 5
    This does not work. With these annotations one can still send a request with empty parameter value. – Jagger Nov 01 '20 at 14:33
  • For this solution to work, you have to explicitly add: ` org.springframework.boot spring-boot-starter-validation ` – user1927829 Nov 29 '22 at 14:12
3

You do not check the values up to now; you could change your code to:

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

@RequestMapping(value = "/calculate")
@ResponseBody
public ResponseEntity<CalculationResult> calculate(@RequestParam(name = "op") String operation, 
    @RequestParam(name = "num1") Double num1, 
    @RequestParam(name = "num2") Double num2) {

    if(null == op || null == num1 || null == num2) {
        throw new IllegalArgumentException("{\"error\":\"At least one parameter is invalid or not supplied\"}")
    }

    System.out.print("Operation:" + operation);
    Double calculate = calculatorService.calculate(operation, num1, num2);

    return new ResponseEntity<>(new CalculationResult(calculate), HttpStatus.OK);
}    

@ExceptionHandler(IllegalArgumentException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public final String exceptionHandlerIllegalArgumentException(final IllegalArgumentException e) {
    return '"' + e.getMessage() + '"';
}
P.J.Meisch
  • 18,013
  • 6
  • 50
  • 66
  • When I add this, and run the test I mentioned in the question, the test fails with: "java.lang.AssertionError: Expected: "{\"error\":\"At least one parameter is invalid or not supplied\"}" but: was null" – Saakina Mar 07 '17 at 10:31
  • your service code is returning a CalculationResult object, but your test is expecting a String; this is inconsistent. I changed my answer so that an ExceptionHandler is used. I did not compile it; hope I didn't make any typos – P.J.Meisch Mar 07 '17 at 11:09
  • There should be a grey checkmark below the up/downvoting arrows for the answer, you have to click that to accept an answer. – P.J.Meisch Mar 07 '17 at 11:43
  • Why we are checking for null inside method implementation `if(null == op || null == num1 || null == num2)`, when we can handle it using JSR bean validation? – Arpit Aggarwal Mar 07 '17 at 16:13
  • How do we check if an array is NULL? – Deepjyoti De Sep 20 '21 at 13:31
-1
@RestController
@RequestMapping("/")
@Validated
public class Controller {
    @GetMapping("/endpoint")
    public String getEndpoint(@RequestParam @NotBlank String name) {}
}
vladkabat
  • 1
  • 3
  • 9
    Welcome to SO! While your answer may be correct, you should always try to give it some context and explain why it may be the solution. – kuhnroyal Feb 28 '21 at 18:46