2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package org.openecomp.datarouter.entity;
23 import static org.junit.Assert.assertEquals;
26 import java.io.IOException;
27 import java.util.Arrays;
28 import java.util.HashMap;
29 import java.util.List;
31 import java.util.Scanner;
33 import org.json.JSONArray;
34 import org.json.JSONObject;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.mockito.Mockito;
38 import org.onap.aai.datarouter.entity.SuggestionSearchEntity;
39 import org.onap.aai.datarouter.search.filters.config.UiFiltersSchemaUtility;
41 public class SuggestionSearchEntityTest {
42 private static SuggestionSearchEntity suggestionSearchEntity;
45 public void setUpBeforeTest() {
46 UiFiltersSchemaUtility filtersSchemaUtility = Mockito.mock(UiFiltersSchemaUtility.class);
47 Mockito.when(filtersSchemaUtility.loadUiFiltersConfig()).thenReturn(null);
49 suggestionSearchEntity = new SuggestionSearchEntity();
50 suggestionSearchEntity.setFiltersSchemaUtility(filtersSchemaUtility);
51 suggestionSearchEntity.setEntityType("generic-vnf");
52 suggestionSearchEntity.setEntityTypeAliases(Arrays.asList("VNFs"));
56 * Read in the contents of the given file (can include sub-path) in test/resources folder
58 * @param filePath The file name or path (relative to test/resources) to read from
59 * @return The contents of the file as a String
61 public String getResourceFileContents(String filePath) {
62 StringBuilder result = new StringBuilder("");
64 ClassLoader classLoader = getClass().getClassLoader();
65 File file = new File(classLoader.getResource(filePath).getFile());
67 try (Scanner scanner = new Scanner(file)) {
68 while (scanner.hasNextLine()) {
69 String line = scanner.nextLine();
70 result.append(line).append("\n");
75 } catch (IOException e) {
79 return result.toString();
83 public void testGetAsJson_multipleFilterAttributableStatusesIncluded() throws IOException {
84 String expectedOutput =
85 getResourceFileContents("uifilters/testGetAsJson_multipleFilterAttributableStatusesIncluded_expectedValue.json");
87 List<String> suggestionInputPermutations = Arrays.asList(
88 "provStatus1 orchestrationStatus1 generic-vnf",
89 "provStatus1 generic-vnf orchestrationStatus1",
90 "orchestrationStatus1 generic-vnf provStatus1",
91 "orchestrationStatus1 provStatus1 generic-vnf",
92 "generic-vnf provStatus1 orchestrationStatus1",
93 "generic-vnf orchestrationStatus1 provStatus1");
95 Map<String, String>inputOutputData = new HashMap<>();
96 inputOutputData.put("prov-status", "provStatus1");
97 inputOutputData.put("orchestration-status", "orchestrationStatus1");
99 // Build UI filters JSON string
100 JSONObject payloadFilter1 = new JSONObject();
101 payloadFilter1.put("filterId", "1");
102 payloadFilter1.put("filterValue", "orchestrationStatus1");
104 JSONObject payloadFilter2 = new JSONObject();
105 payloadFilter2.put("filterId", "2");
106 payloadFilter2.put("filterValue", "provStatus1");
108 JSONArray payloadFilters = new JSONArray();
109 payloadFilters.put(payloadFilter2);
110 payloadFilters.put(payloadFilter1);
112 JSONObject filterPayload = new JSONObject();
113 filterPayload.put("filterList", payloadFilters);
115 suggestionSearchEntity.setSuggestionInputPermutations(suggestionInputPermutations);
116 suggestionSearchEntity.setInputOutputData(inputOutputData);
117 suggestionSearchEntity.setFilterPayload(filterPayload);
119 String actualOutput = suggestionSearchEntity.getAsJson();
121 assertEquals(expectedOutput.trim(), actualOutput.trim());
125 public void testGetAsJson_singleFilterAttributableStatusIncluded() throws IOException {
126 String expectedOutput =
127 getResourceFileContents("uifilters/testGetAsJson_singleFilterAttributableStatusIncluded_expectedValue.json");
129 List<String> suggestionInputPermutations = Arrays.asList(
130 "provStatus1 generic-vnf",
131 "generic-vnf provStatus1");
133 Map<String, String>inputOutputData = new HashMap<>();
134 inputOutputData.put("prov-status", "provStatus1");
136 // Build UI filters JSON string
137 JSONObject payloadFilter1 = new JSONObject();
138 payloadFilter1.put("filterId", "2");
139 payloadFilter1.put("filterValue", "provStatus1");
141 JSONArray payloadFilters = new JSONArray();
142 payloadFilters.put(payloadFilter1);
144 JSONObject filterPayload = new JSONObject();
145 filterPayload.put("filterList", payloadFilters);
147 suggestionSearchEntity.setSuggestionInputPermutations(suggestionInputPermutations);
148 suggestionSearchEntity.setInputOutputData(inputOutputData);
149 suggestionSearchEntity.setFilterPayload(filterPayload);
151 String actualOutput = suggestionSearchEntity.getAsJson();
153 assertEquals(expectedOutput.trim(), actualOutput.trim());