Renaming openecomp to onap
[aai/data-router.git] / src / test / java / org / openecomp / datarouter / entity / SuggestionSearchEntityTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * DataRouter
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
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
12  *
13  *    http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25 package org.openecomp.datarouter.entity;
26
27 import static org.junit.Assert.assertEquals;
28
29 import java.io.File;
30 import java.io.IOException;
31 import java.util.Arrays;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Scanner;
36
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.onap.aai.datarouter.search.filters.config.UiFiltersSchemaUtility;
44
45 public class SuggestionSearchEntityTest {
46   private static SuggestionSearchEntity suggestionSearchEntity;
47
48   @Before
49   public void setUpBeforeTest() {
50     UiFiltersSchemaUtility filtersSchemaUtility = Mockito.mock(UiFiltersSchemaUtility.class);
51     Mockito.when(filtersSchemaUtility.loadUiFiltersConfig()).thenReturn(null);
52
53     suggestionSearchEntity = new SuggestionSearchEntity();
54     suggestionSearchEntity.setFiltersSchemaUtility(filtersSchemaUtility);
55     suggestionSearchEntity.setEntityType("generic-vnf");
56     suggestionSearchEntity.setEntityTypeAliases(Arrays.asList("VNFs"));
57   }
58
59   /**
60    * Read in the contents of the given file (can include sub-path) in test/resources folder
61    *
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
64    */
65   public String getResourceFileContents(String filePath) {
66     StringBuilder result = new StringBuilder("");
67
68     ClassLoader classLoader = getClass().getClassLoader();
69     File file = new File(classLoader.getResource(filePath).getFile());
70
71     try (Scanner scanner = new Scanner(file)) {
72       while (scanner.hasNextLine()) {
73         String line = scanner.nextLine();
74         result.append(line).append("\n");
75       }
76
77       scanner.close();
78
79     } catch (IOException e) {
80       e.printStackTrace();
81     }
82
83     return result.toString();
84   }
85
86   @Test
87   public void testGetAsJson_multipleFilterAttributableStatusesIncluded() throws IOException {
88     String expectedOutput =
89       getResourceFileContents("uifilters/testGetAsJson_multipleFilterAttributableStatusesIncluded_expectedValue.json");
90
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");
98
99     Map<String, String>inputOutputData = new HashMap<>();
100     inputOutputData.put("prov-status", "provStatus1");
101     inputOutputData.put("orchestration-status", "orchestrationStatus1");
102
103     // Build UI filters JSON string
104     JSONObject payloadFilter1 = new JSONObject();
105     payloadFilter1.put("filterId", "1");
106     payloadFilter1.put("filterValue", "orchestrationStatus1");
107
108     JSONObject payloadFilter2 = new JSONObject();
109     payloadFilter2.put("filterId", "2");
110     payloadFilter2.put("filterValue", "provStatus1");
111
112     JSONArray payloadFilters = new JSONArray();
113     payloadFilters.put(payloadFilter2);
114     payloadFilters.put(payloadFilter1);
115
116     JSONObject filterPayload = new JSONObject();
117     filterPayload.put("filterList", payloadFilters);
118
119     suggestionSearchEntity.setSuggestionInputPermutations(suggestionInputPermutations);
120     suggestionSearchEntity.setInputOutputData(inputOutputData);
121     suggestionSearchEntity.setFilterPayload(filterPayload);
122
123     String actualOutput = suggestionSearchEntity.getAsJson();
124
125     assertEquals(expectedOutput.trim(), actualOutput.trim());
126   }
127
128   @Test
129   public void testGetAsJson_singleFilterAttributableStatusIncluded() throws IOException {
130     String expectedOutput =
131       getResourceFileContents("uifilters/testGetAsJson_singleFilterAttributableStatusIncluded_expectedValue.json");
132
133     List<String> suggestionInputPermutations = Arrays.asList(
134         "provStatus1 generic-vnf",
135         "generic-vnf provStatus1");
136
137     Map<String, String>inputOutputData = new HashMap<>();
138     inputOutputData.put("prov-status", "provStatus1");
139
140     // Build UI filters JSON string
141     JSONObject payloadFilter1 = new JSONObject();
142     payloadFilter1.put("filterId", "2");
143     payloadFilter1.put("filterValue", "provStatus1");
144
145     JSONArray payloadFilters = new JSONArray();
146     payloadFilters.put(payloadFilter1);
147
148     JSONObject filterPayload = new JSONObject();
149     filterPayload.put("filterList", payloadFilters);
150
151     suggestionSearchEntity.setSuggestionInputPermutations(suggestionInputPermutations);
152     suggestionSearchEntity.setInputOutputData(inputOutputData);
153     suggestionSearchEntity.setFilterPayload(filterPayload);
154
155     String actualOutput = suggestionSearchEntity.getAsJson();
156
157     assertEquals(expectedOutput.trim(), actualOutput.trim());
158   }
159 }