3

I use 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));
    }
}

And here is JUnit controller test with some filled request in 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;
    }
}

I don't understand how I can mock of fill SearchResponse with some data. Does anybody have some experience with it? Maybe there is some way to fill it by json data like searchRequest ?

Nikita Krasnov
  • 57
  • 1
  • 11
  • Try this approach here: https://stackoverflow.com/questions/49798654/json-string-to-elasticsearch-searchresponse-with-aggregation/57117845#57117845 – technocrat Jul 19 '19 at 18:36

1 Answers1

0

SearchResponse has only the method readFrom(InputStream) to set the fields. Using this method to create a real SearchResponse object would be very complicated as you would need to know the internal format of the content of the stream.

What you should do is to use a mocking library like Mockito to create a mock object that is of the type SearchResponse but you can define in your test preperation what content a method e.g. getHits​() should return.

Example code to create and overwrite the behavior of a mock with Mockito:

import static org.mockito.Mockito.*;
// mock creation
List mockedList = mock(List.class);
// define method behavior
when(mockedList.get(0)).thenReturn("first");
// the following prints "first"
System.out.println(mockedList.get(0));

As the ElasticSearch API returns a lot of internal Objects before you can access the real values you should have a look into deep stubs when using Mockito so you do not have to mock each level of objects.

Simulant
  • 19,190
  • 8
  • 63
  • 98