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