Я использую Elastic 6.2, SpringBoot, Java 8.
@RestController
@Log4j2
@AllArgsConstructor
@RequestMapping("/api/logs")
public class ElasticRestController {
@PostMapping("/search")
public GenericResponse<?> findLogs(@RequestBody ESLogRequestDTO esLogRequest,
Pageable pageable) throws NoConnectionException {
SearchResponse searchResponse = elasticUIService.
findLogsByParameters(esLogRequest, pageable);
return GenericResponse.
success(convertToStandardResponse(searchResponse.getHits(), pageable));
}
}
А вот тест контроллера JUnit с некоторым заполненным запросом в json (searchRequest):
@WebMvcTest(
value = ElasticRestController.class,
secure = false
)
public class ElasticRestControllerTest extends AbstractControllerTest {
private static final String CONTENT_TYPE = "application/json;charset=UTF-8";
@MockBean
private ElasticUIService elasticUIService;
@MockBean
private ElasticsearchService elasticsearchService;
@Autowired
private ElasticRestController elasticRestController;
@Autowired
private MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter;
@Autowired
private MockMvc mockMvc;
@Rule
public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation();
@Before
public void before() {
mockMvc = MockMvcBuilders.standaloneSetup(elasticRestController)
.setCustomArgumentResolvers(new PageableHandlerMethodArgumentResolver())
.setMessageConverters(mappingJackson2HttpMessageConverter)
.apply(MockMvcRestDocumentation.documentationConfiguration(this.restDocumentation))
.build();
}
@Test
public void findLogsByParametersTest() throws Exception {
String searchRequest = "{\n" +
"\t \"levels\": [\"INFO\"],\n" +
" \"module\": \"test module\",\n" +
" \"version\": \"version 1\",\n" +
" \"thread\": \"test thread\",\n" +
" \"requestId\": \"1\",\n" +
" \"message\": \"test message 3\",\n" +
" \"rangeFrom\": \"2018-02-26T07:02:50.000Z\",\n" +
" \"rangeTo\": \"2018-03-05T07:02:50.000Z\",\n" +
" \"node\": \"first node\",\n" +
" \"system\": \"super system 1\",\n" +
" \"header\": \"test\",\n" +
" \"submodule\": \"test submodule\",\n" +
" \"operation\": \"some operation\",\n" +
" \"service\": \"some service\",\n" +
" \"type\": \"some type\",\n" +
" \"metricType\": \"duration\",\n" +
" \"valueFrom\":400,\n" +
" \"valueTo\":600\n" +
"}";
SearchResponse searchResponse = getSearchResponse();
when(elasticUIService.findLogsByParameters(any(ESLogRequestDTO.class),
any(Pageable.class)))
.thenReturn(searchResponse);
mockMvc.perform(post("/api/logs/search")
.contentType(CONTENT_TYPE)
.content(searchRequest)
.accept(CONTENT_TYPE)
)
.andDo(document(CLASS_NAME_METHOD_NAME))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE));
}
public SearchResponse getSearchResponse() {
SearchResponse searchResponse = new SearchResponse();
return searchResponse;
}
}
Не знаюНе понимаю, как я могу издеваться над заполнением SearchResponse некоторыми данными.У кого-нибудь есть опыт?Может быть, есть какой-нибудь способ заполнить его данными json, например searchRequest ?