diff options
Diffstat (limited to 'src/test/java/org/onap')
-rw-r--r-- | src/test/java/org/onap/aai/sparky/search/UnifiedSearchProcessorTest.java | 277 | ||||
-rw-r--r-- | src/test/java/org/onap/aai/sparky/search/entity/MockSearchResponse.java | 71 |
2 files changed, 188 insertions, 160 deletions
diff --git a/src/test/java/org/onap/aai/sparky/search/UnifiedSearchProcessorTest.java b/src/test/java/org/onap/aai/sparky/search/UnifiedSearchProcessorTest.java index e123c5d..8dd3543 100644 --- a/src/test/java/org/onap/aai/sparky/search/UnifiedSearchProcessorTest.java +++ b/src/test/java/org/onap/aai/sparky/search/UnifiedSearchProcessorTest.java @@ -44,6 +44,7 @@ import org.mockito.ArgumentCaptor; import org.mockito.Mockito; import org.onap.aai.sparky.common.search.CommonSearchSuggestion; import org.onap.aai.sparky.search.api.SearchProvider; +import org.onap.aai.sparky.search.entity.MockSearchResponse; import org.onap.aai.sparky.search.entity.QuerySearchEntity; import org.onap.aai.sparky.search.entity.SearchSuggestion; import org.onap.aai.sparky.search.registry.SearchProviderRegistry; @@ -54,19 +55,18 @@ import org.restlet.data.MediaType; import org.restlet.data.Status; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; public class UnifiedSearchProcessorTest { - + public interface Suggester { public void addSuggestion( SearchSuggestion suggestion ); } - + private abstract class AbstractDummySearchProvider implements SearchProvider, Suggester { - private List<SearchSuggestion> suggestions; + protected List<SearchSuggestion> suggestions; protected AbstractDummySearchProvider() { suggestions = new ArrayList<SearchSuggestion>(); @@ -76,44 +76,53 @@ public class UnifiedSearchProcessorTest { return suggestions; } - public void addSuggestion(SearchSuggestion suggestion) { - if (suggestion != null) { - suggestions.add(suggestion); - } - } - + @Override public List<SearchSuggestion> search(QuerySearchEntity queryRequest) { return getSuggestions(); } - } private class AlphaSearchProvider extends AbstractDummySearchProvider { - public AlphaSearchProvider() { super(); } + @Override + public void addSuggestion(SearchSuggestion suggestion) { + if (suggestion != null) { + suggestions.add(suggestion); + } + } } private class BravoSearchProvider extends AbstractDummySearchProvider { - public BravoSearchProvider() { super(); } + @Override + public void addSuggestion(SearchSuggestion suggestion) { + if (suggestion != null) { + suggestions.add(suggestion); + } + } } private class GammaSearchProvider extends AbstractDummySearchProvider { - public GammaSearchProvider() { super(); } + @Override + public void addSuggestion(SearchSuggestion suggestion) { + if (suggestion != null) { + suggestions.add(suggestion); + } + } } - + private SearchServiceAdapter mockSearchAdapter; - + private UnifiedSearchProcessor unifiedSearchProcessor; private Exchange mockExchange; private Message mockRequestMessage; @@ -127,7 +136,7 @@ public class UnifiedSearchProcessorTest { public void init() { requestClientInfo = new ClientInfo(); - + mockExchange = Mockito.mock(Exchange.class); mockRequestMessage = Mockito.mock(Message.class); mockResponseMessage = Mockito.mock(Message.class); @@ -138,8 +147,7 @@ public class UnifiedSearchProcessorTest { unifiedSearchProcessor.setUseOrderedSearchProviderKeys(true); mapper = new ObjectMapper(); - mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); - + mockSearchAdapter = Mockito.mock(SearchServiceAdapter.class); } @@ -151,20 +159,20 @@ public class UnifiedSearchProcessorTest { assertNull(unifiedSearchProcessor.getSearchProviderRegistry()); } - - + + @Test public void validateAccessors() { SearchProviderRegistry searchProviderRegistry = new SearchProviderRegistry(); unifiedSearchProcessor.setSearchProviderRegistry(searchProviderRegistry); - + // initially it should be null until the bean wiring initializes it assertNotNull(unifiedSearchProcessor.getSearchProviderRegistry()); assertEquals(0, searchProviderRegistry.getSearchProviders().size()); } - + private void initializeSearchMocks(String requestPayload) { Mockito.when(mockRestletRequest.getClientInfo()).thenReturn(requestClientInfo); @@ -175,47 +183,33 @@ public class UnifiedSearchProcessorTest { Mockito.when(mockRequestMessage.getHeader(RestletConstants.RESTLET_RESPONSE, Response.class)) .thenReturn(mockRestletResponse); - + Mockito.when(mockExchange.getIn()).thenReturn(mockRequestMessage); Mockito.when(mockExchange.getOut()).thenReturn(mockResponseMessage); } - - private void initializePerspectiveMocks(String requestPayload) throws JsonProcessingException { - Mockito.when(mockRestletRequest.getClientInfo()).thenReturn(requestClientInfo); - - Mockito.when(mockRequestMessage.getBody(String.class)).thenReturn(requestPayload); - Mockito.when(mockRequestMessage.getHeader(RestletConstants.RESTLET_REQUEST, Request.class)) - .thenReturn(mockRestletRequest); - - Mockito.when(mockRequestMessage.getHeader(RestletConstants.RESTLET_RESPONSE, Response.class)) - .thenReturn(mockRestletResponse); - - Mockito.when(mockExchange.getIn()).thenReturn(mockRequestMessage); - Mockito.when(mockExchange.getOut()).thenReturn(mockResponseMessage); - } - + private String getSearchRequestJson(String queryString, int maxResults) { - + JSONObject root = new JSONObject(); root.put("queryStr", queryString); root.put("maxResults", maxResults); - + return root.toString(); } - + private String getExternalSearchRequestJson() { JSONObject root = new JSONObject(); - + root.put("view", "testView"); root.put("entityId", "thisIsAnId"); root.put("entityType", "pserver"); - + return root.toString(); } - - + + @Test public void testSearch_search_when_noSearchProviders() throws IOException { @@ -225,7 +219,7 @@ public class UnifiedSearchProcessorTest { SearchProviderRegistry searchProviderRegistry = new SearchProviderRegistry(); unifiedSearchProcessor.setSearchProviderRegistry(searchProviderRegistry); - + // method under test unifiedSearchProcessor.search(mockExchange); @@ -247,9 +241,9 @@ public class UnifiedSearchProcessorTest { assertEquals(0, searchResponse.getTotalFound()); assertEquals(0, searchResponse.getSuggestions().size()); - + } - + @Test public void testSearch_search_when_ThreeSearchProviders_no_suggestions() throws IOException { @@ -258,21 +252,21 @@ public class UnifiedSearchProcessorTest { initializeSearchMocks(getSearchRequestJson("vnfs",10)); SearchProviderRegistry searchProviderRegistry = new SearchProviderRegistry(); - + AlphaSearchProvider alpha = new AlphaSearchProvider(); BravoSearchProvider bravo = new BravoSearchProvider(); GammaSearchProvider gamma = new GammaSearchProvider(); - + searchProviderRegistry.addSearchProvider(alpha); searchProviderRegistry.addSearchProvider(bravo); searchProviderRegistry.addSearchProvider(gamma); - + unifiedSearchProcessor.setSearchProviderRegistry(searchProviderRegistry); - - + + // method under test unifiedSearchProcessor.search(mockExchange); - + ArgumentCaptor<Status> responseCodeCaptor = ArgumentCaptor.forClass(Status.class); Mockito.verify(mockRestletResponse, Mockito.atLeast(1)).setStatus(responseCodeCaptor.capture()); assertEquals(Status.SUCCESS_OK, responseCodeCaptor.getValue()); @@ -286,68 +280,41 @@ public class UnifiedSearchProcessorTest { ArgumentCaptor<Response> responseObject = ArgumentCaptor.forClass(Response.class); Mockito.verify(mockResponseMessage, Mockito.atLeast(1)).setBody(responseObject.capture()); assertEquals(MediaType.APPLICATION_JSON, payloadMediaType.getValue()); - + /* * With a null view name, an empty filter set should be returned - there should be 0 filters */ - + SearchResponse searchResponse = mapper.readValue(entityPayload.getValue(), SearchResponse.class); assertEquals(0, searchResponse.getTotalFound()); assertEquals(0, searchResponse.getSuggestions().size()); - + } - + private void addSuggestions(int numSuggestions, String suggestionPrefix, Suggester suggester) { - SearchSuggestion suggestion = null; for ( int x = 0; x < numSuggestions; x++ ){ - suggestion = new CommonSearchSuggestion(); + CommonSearchSuggestion suggestion = new CommonSearchSuggestion(); suggestion.setText(suggestionPrefix + "-" + x); suggester.addSuggestion(suggestion); } } - - private void addSuggestion(String perspective, String text, String hashId, Suggester suggester) { - SearchSuggestion suggestion = new CommonSearchSuggestion(); - suggestion.setText(text); - suggestion.setHashId(hashId); - suggester.addSuggestion(suggestion); - } - - private int countSuggestions(String suggestionPrefix, SearchResponse response) { - + + private int countSuggestions(String suggestionPrefix, MockSearchResponse response) { + int totalFound = 0; - + for ( SearchSuggestion suggestion : response.getSuggestions()) { - + if ( suggestion.getText() != null && suggestion.getText().startsWith(suggestionPrefix)) { totalFound++; } } - + return totalFound; - + } - - private int countSuggestions(String suggestionPrefix, JSONArray suggestions) { - - int totalFound = 0; - - for ( int x = 0; x < suggestions.length(); x++ ) { - - JSONObject suggestion = (JSONObject)suggestions.get(x); - - String text = suggestion.getString("text"); - if ( String.valueOf(text).startsWith(suggestionPrefix)) { - totalFound++; - } - - } - - return totalFound; - - } - + @Test public void testSearch_search_when_ThreeSearchProviders_5suggestions_each() throws IOException { @@ -356,21 +323,21 @@ public class UnifiedSearchProcessorTest { initializeSearchMocks(getSearchRequestJson("vnfs",10)); SearchProviderRegistry searchProviderRegistry = new SearchProviderRegistry(); - + AlphaSearchProvider alpha = new AlphaSearchProvider(); BravoSearchProvider bravo = new BravoSearchProvider(); GammaSearchProvider gamma = new GammaSearchProvider(); - + + addSuggestions(5, "alpha", alpha); + addSuggestions(5, "bravo", bravo); + addSuggestions(5, "gamma", gamma); + searchProviderRegistry.addSearchProvider(alpha); searchProviderRegistry.addSearchProvider(bravo); searchProviderRegistry.addSearchProvider(gamma); - + unifiedSearchProcessor.setSearchProviderRegistry(searchProviderRegistry); - - addSuggestions(5,"alpha",alpha); - addSuggestions(5,"bravo",bravo); - addSuggestions(5,"gamma",gamma); - + // method under test unifiedSearchProcessor.search(mockExchange); @@ -387,21 +354,16 @@ public class UnifiedSearchProcessorTest { ArgumentCaptor<Response> responseObject = ArgumentCaptor.forClass(Response.class); Mockito.verify(mockResponseMessage, Mockito.atLeast(1)).setBody(responseObject.capture()); assertEquals(MediaType.APPLICATION_JSON, payloadMediaType.getValue()); - - - JSONObject response = new JSONObject(entityPayload.getValue()); - - assertEquals(response.getInt("totalFound"),10); - - JSONArray suggestions = response.getJSONArray("suggestions"); - assertNotNull(suggestions); - - assertEquals(suggestions.length(),10); - - assertEquals( 4, countSuggestions("alpha", suggestions)); - assertEquals( 3, countSuggestions("bravo", suggestions)); - assertEquals( 3, countSuggestions("gamma", suggestions)); - + + MockSearchResponse searchResponse = mapper.readValue(entityPayload.getValue(), MockSearchResponse.class); + + assertEquals(10, searchResponse.getTotalFound()); + assertEquals(10, searchResponse.getSuggestions().size()); + + assertEquals( 4, countSuggestions("alpha", searchResponse)); + assertEquals( 3, countSuggestions("bravo", searchResponse)); + assertEquals( 3, countSuggestions("gamma", searchResponse)); + } @Test @@ -412,21 +374,21 @@ public class UnifiedSearchProcessorTest { initializeSearchMocks(getSearchRequestJson("vnfs",13)); SearchProviderRegistry searchProviderRegistry = new SearchProviderRegistry(); - + AlphaSearchProvider alpha = new AlphaSearchProvider(); BravoSearchProvider bravo = new BravoSearchProvider(); GammaSearchProvider gamma = new GammaSearchProvider(); - + searchProviderRegistry.addSearchProvider(alpha); searchProviderRegistry.addSearchProvider(bravo); searchProviderRegistry.addSearchProvider(gamma); - + unifiedSearchProcessor.setSearchProviderRegistry(searchProviderRegistry); - + addSuggestions(45,"alpha",alpha); addSuggestions(1,"bravo",bravo); addSuggestions(99,"gamma",gamma); - + // method under test unifiedSearchProcessor.search(mockExchange); @@ -443,38 +405,34 @@ public class UnifiedSearchProcessorTest { ArgumentCaptor<Response> responseObject = ArgumentCaptor.forClass(Response.class); Mockito.verify(mockResponseMessage, Mockito.atLeast(1)).setBody(responseObject.capture()); assertEquals(MediaType.APPLICATION_JSON, payloadMediaType.getValue()); + + MockSearchResponse searchResponse = mapper.readValue(entityPayload.getValue(), MockSearchResponse.class); - JSONObject response = new JSONObject(entityPayload.getValue()); - - assertEquals(response.getInt("totalFound"),13); - - JSONArray suggestions = response.getJSONArray("suggestions"); - assertNotNull(suggestions); - - assertEquals(suggestions.length(),13); - + assertEquals(13, searchResponse.getTotalFound()); + assertEquals(13, searchResponse.getSuggestions().size()); + /** * There should be an even divide of suggestions per search provider relative * to the suggestions available per search provider. * Alpha has 45 suggestions * Bravo has 1 suggestion * Gamma has 99 suggestions - * + * * We only asked for 13 suggestions to be returned, so based on the suggestion * distribution algorithm we will get a fair distribution of suggestions per provider * relative to what each provider has available. Resulting in: * 6 from Alpha * 1 from Bravo * 6 from Gamma - * + * */ - - assertEquals( 6, countSuggestions("alpha", suggestions)); - assertEquals( 1, countSuggestions("bravo", suggestions)); - assertEquals( 6, countSuggestions("gamma", suggestions)); - + + assertEquals( 6, countSuggestions("alpha", searchResponse)); + assertEquals( 1, countSuggestions("bravo", searchResponse)); + assertEquals( 6, countSuggestions("gamma", searchResponse)); + } - + @Test public void testSearch_search_when_ThreeSearchProviders_wantedMoreSuggestionsThanAvailable() throws IOException { @@ -483,21 +441,21 @@ public class UnifiedSearchProcessorTest { initializeSearchMocks(getSearchRequestJson("vnfs",13)); SearchProviderRegistry searchProviderRegistry = new SearchProviderRegistry(); - + AlphaSearchProvider alpha = new AlphaSearchProvider(); BravoSearchProvider bravo = new BravoSearchProvider(); GammaSearchProvider gamma = new GammaSearchProvider(); - + searchProviderRegistry.addSearchProvider(alpha); searchProviderRegistry.addSearchProvider(bravo); searchProviderRegistry.addSearchProvider(gamma); - + unifiedSearchProcessor.setSearchProviderRegistry(searchProviderRegistry); - + addSuggestions(1,"alpha",alpha); addSuggestions(4,"bravo",bravo); addSuggestions(0,"gamma",gamma); - + // method under test unifiedSearchProcessor.search(mockExchange); @@ -511,19 +469,18 @@ public class UnifiedSearchProcessorTest { payloadMediaType.capture()); assertNotNull(entityPayload.getValue()); - JSONObject response = new JSONObject(entityPayload.getValue()); - - assertEquals(response.getInt("totalFound"),5); - - JSONArray suggestions = response.getJSONArray("suggestions"); - assertNotNull(suggestions); - - assertEquals(suggestions.length(),5); - - assertEquals( 1, countSuggestions("alpha", suggestions)); - assertEquals( 4, countSuggestions("bravo", suggestions)); - assertEquals( 0, countSuggestions("gamma", suggestions)); - + ArgumentCaptor<Response> responseObject = ArgumentCaptor.forClass(Response.class); + Mockito.verify(mockResponseMessage, Mockito.atLeast(1)).setBody(responseObject.capture()); + assertEquals(MediaType.APPLICATION_JSON, payloadMediaType.getValue()); + + MockSearchResponse searchResponse = mapper.readValue(entityPayload.getValue(), MockSearchResponse.class); + + assertEquals(5, searchResponse.getTotalFound()); + assertEquals(5, searchResponse.getSuggestions().size()); + + assertEquals( 1, countSuggestions("alpha", searchResponse)); + assertEquals( 4, countSuggestions("bravo", searchResponse)); + assertEquals( 0, countSuggestions("gamma", searchResponse)); + } - -} +}
\ No newline at end of file diff --git a/src/test/java/org/onap/aai/sparky/search/entity/MockSearchResponse.java b/src/test/java/org/onap/aai/sparky/search/entity/MockSearchResponse.java new file mode 100644 index 0000000..02a1aee --- /dev/null +++ b/src/test/java/org/onap/aai/sparky/search/entity/MockSearchResponse.java @@ -0,0 +1,71 @@ +package org.onap.aai.sparky.search.entity; + +import java.util.ArrayList; +import java.util.List; + +import org.onap.aai.sparky.common.search.CommonSearchSuggestion; + +public class MockSearchResponse { + private long processingTimeInMs; + private int totalFound; + + private List<CommonSearchSuggestion> suggestions; + + /** + * Instantiates a new search response. + */ + public MockSearchResponse() { + this.suggestions = new ArrayList<CommonSearchSuggestion>(); + this.processingTimeInMs = 0; + this.totalFound = 0; + } + + public long getProcessingTimeInMs() { + return processingTimeInMs; + } + + public void setProcessingTimeInMs(long processingTimeInMs) { + this.processingTimeInMs = processingTimeInMs; + } + + public int getTotalFound() { + return totalFound; + } + + public void setTotalFound(int totalFound) { + this.totalFound = totalFound; + } + + public List<CommonSearchSuggestion> getSuggestions() { + return suggestions; + } + + public void setSuggestions(List<CommonSearchSuggestion> suggestions) { + this.suggestions = suggestions; + } + + /** + * Adds the entity entry. + * + * @param suggestionEntry that will be converted to JSON + */ + public void addSuggestion(CommonSearchSuggestion suggestionEntity){ + suggestions.add(suggestionEntity); + } + + /** + * Increments the total number of hits for this SearchResponse by + * the value passed in. + * + * @param additionalCount - Count to increment the total found + */ + public void addToTotalFound(int additionalCount) { + totalFound += additionalCount; + } + + @Override + public String toString() { + return "DummySearchResponse [processingTimeInMs=" + processingTimeInMs + ", totalFound=" + + totalFound + ", suggestions=" + suggestions + "]"; + } +} |