I want to test method of receiving a list of objects, according to list of given strings. My original method:
@RequestMapping(value = "/fil/", method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.OK)
public ResponseEntity<List<Tag>> findAllByCpe(@RequestBody Fil fil) {
return ResponseEntity.ok(tagRepository.findAllBy(fil));
}
Query (tagRepository):
@Query("SELECT t FROM Tag t WHERE (t.cpe IS NULL OR t.cpe IN (:#{#fil.cpes}))")
List<Tag> findAllBy(@Param("fil") Fil fil);
Fil (Class holding a list of strings I want to search by)
@Getter
@Setter
@AllArgsConstructor
public class Fil {
public Fil() {
}
@NotNull
private List<String> cpes;
}
I wrote an integration test:
@Test
public void FindTagGivenListOfCpes() {
//GIVEN
List<String> cpes = new ArrayList<>();
cpes .add("C1");
cpes .add("C2");
cpes .add("C3");
List<Tag> tagList = (List<Tag>) tagTestBuilder
.saved()
.itemList()
.build();
//WHEN
ResponseEntity<Tag[]> response = restTemplate.postForEntity(TagsResourceConstants.PATH + "/fil/", cpes, Tag[].class);
//THEN
assertEquals(HttpStatus.OK.value(), response.getStatusCodeValue());
}