public void testSearchMessages() throws Exception {
String json = JSONUtils.readJSONFromClasspath("/com/streamreduce/search/elastic_search_raw_payload.json");
JSONObject elasticSearchPayload = JSONObject.fromObject(json);
//Set these fields through reflection (mock prevents setters from working) to dummy values.
SearchServiceImpl searchService = mock(SearchServiceImpl.class);
ReflectionTestUtils.setField(searchService,"elasticSearchHost","doesNotMatter");
ReflectionTestUtils.setField(searchService,"elasticSearchPort",666);
ReflectionTestUtils.setField(searchService,"messageDatabaseName","nodeablemsgdb");
ReflectionTestUtils.setField(searchService,"enabled",true);
//Make sure calls to searchMessages uses the real implementation
when(searchService.searchMessages(any(Account.class), anyString(), anyMap(), any(JSONObject.class))).thenCallRealMethod();
//Make call to makeRequest so that it returns the elasticSearchPayload
when(searchService.makeRequest(anyString(),any(JSONObject.class),anyMap(),anyString())).thenReturn(elasticSearchPayload);
Account a = new Account.Builder().name("testAccount").build();
a.setId(new ObjectId());
List<SobaMessage> sobaMessages = searchService.searchMessages(a,"resource", null,null);
//Expected number of hits in json:
int expectedSize = elasticSearchPayload.getJSONObject("hits").getJSONArray("hits").size();
Assert.assertEquals(expectedSize,sobaMessages.size());
}