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.openecomp.datarouter.search.filters.config.UiFiltersSchemaUtility;
44 public class SuggestionSearchEntityTest {
45 private static SuggestionSearchEntity suggestionSearchEntity;
48 public void setUpBeforeTest() {
49 UiFiltersSchemaUtility filtersSchemaUtility = Mockito.mock(UiFiltersSchemaUtility.class);
50 Mockito.when(filtersSchemaUtility.loadUiFiltersConfig()).thenReturn(null);
52 suggestionSearchEntity = new SuggestionSearchEntity();
53 suggestionSearchEntity.setFiltersSchemaUtility(filtersSchemaUtility);
54 suggestionSearchEntity.setEntityType("generic-vnf");
55 suggestionSearchEntity.setEntityTypeAliases(Arrays.asList("VNFs"));
59 * Read in the contents of the given file (can include sub-path) in test/resources folder
61 * @param filePath The file name or path (relative to test/resources) to read from
62 * @return The contents of the file as a String
64 public String getResourceFileContents(String filePath) {
65 StringBuilder result = new StringBuilder("");
67 ClassLoader classLoader = getClass().getClassLoader();
68 File file = new File(classLoader.getResource(filePath).getFile());
70 try (Scanner scanner = new Scanner(file)) {
71 while (scanner.hasNextLine()) {
72 String line = scanner.nextLine();
73 result.append(line).append("\n");
78 } catch (IOException e) {
82 return result.toString();
86 public void testGetAsJson_multipleFilterAttributableStatusesIncluded() throws IOException {
87 String expectedOutput =
88 getResourceFileContents("uifilters/testGetAsJson_multipleFilterAttributableStatusesIncluded_expectedValue.json");
90 List<String> suggestionInputPermutations = Arrays.asList(
91 "provStatus1 orchestrationStatus1 generic-vnf",
92 "provStatus1 generic-vnf orchestrationStatus1",
93 "orchestrationStatus1 generic-vnf provStatus1",
94 "orchestrationStatus1 provStatus1 generic-vnf",
95 "generic-vnf provStatus1 orchestrationStatus1",
96 "generic-vnf orchestrationStatus1 provStatus1");
98 Map<String, String>inputOutputData = new HashMap<>();
99 inputOutputData.put("prov-status", "provStatus1");
100 inputOutputData.put("orchestration-status", "orchestrationStatus1");
102 // Build UI filters JSON string
103 JSONObject payloadFilter1 = new JSONObject();
104 payloadFilter1.put("filterId", "1");
105 payloadFilter1.put("filterValue", "orchestrationStatus1");
107 JSONObject payloadFilter2 = new JSONObject();
108 payloadFilter2.put("filterId", "2");
109 payloadFilter2.put("filterValue", "provStatus1");
111 JSONArray payloadFilters = new JSONArray();
112 payloadFilters.put(payloadFilter2);
113 payloadFilters.put(payloadFilter1);
115 JSONObject filterPayload = new JSONObject();
116 filterPayload.put("filterList", payloadFilters);
118 suggestionSearchEntity.setSuggestionInputPermutations(suggestionInputPermutations);
119 suggestionSearchEntity.setInputOutputData(inputOutputData);
120 suggestionSearchEntity.setFilterPayload(filterPayload);
122 String actualOutput = suggestionSearchEntity.getAsJson();
124 assertEquals(expectedOutput.trim(), actualOutput.trim());
128 public void testGetAsJson_singleFilterAttributableStatusIncluded() throws IOException {
129 String expectedOutput =
130 getResourceFileContents("uifilters/testGetAsJson_singleFilterAttributableStatusIncluded_expectedValue.json");
132 List<String> suggestionInputPermutations = Arrays.asList(
133 "provStatus1 generic-vnf",
134 "generic-vnf provStatus1");
136 Map<String, String>inputOutputData = new HashMap<>();
137 inputOutputData.put("prov-status", "provStatus1");
139 // Build UI filters JSON string
140 JSONObject payloadFilter1 = new JSONObject();
141 payloadFilter1.put("filterId", "2");
142 payloadFilter1.put("filterValue", "provStatus1");
144 JSONArray payloadFilters = new JSONArray();
145 payloadFilters.put(payloadFilter1);
147 JSONObject filterPayload = new JSONObject();
148 filterPayload.put("filterList", payloadFilters);
150 suggestionSearchEntity.setSuggestionInputPermutations(suggestionInputPermutations);
151 suggestionSearchEntity.setInputOutputData(inputOutputData);
152 suggestionSearchEntity.setFilterPayload(filterPayload);
154 String actualOutput = suggestionSearchEntity.getAsJson();
156 assertEquals(expectedOutput.trim(), actualOutput.trim());