3404eeca358b5f4ff26c633afdd10eea95759fae
[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.openecomp.datarouter.search.filters.config.UiFiltersSchemaUtility;
43
44 public class SuggestionSearchEntityTest {
45   private static SuggestionSearchEntity suggestionSearchEntity;
46
47   @Before
48   public void setUpBeforeTest() {
49     UiFiltersSchemaUtility filtersSchemaUtility = Mockito.mock(UiFiltersSchemaUtility.class);
50     Mockito.when(filtersSchemaUtility.loadUiFiltersConfig()).thenReturn(null);
51
52     suggestionSearchEntity = new SuggestionSearchEntity();
53     suggestionSearchEntity.setFiltersSchemaUtility(filtersSchemaUtility);
54     suggestionSearchEntity.setEntityType("generic-vnf");
55     suggestionSearchEntity.setEntityTypeAliases(Arrays.asList("VNFs"));
56   }
57
58   /**
59    * Read in the contents of the given file (can include sub-path) in test/resources folder
60    *
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
63    */
64   public String getResourceFileContents(String filePath) {
65     StringBuilder result = new StringBuilder("");
66
67     ClassLoader classLoader = getClass().getClassLoader();
68     File file = new File(classLoader.getResource(filePath).getFile());
69
70     try (Scanner scanner = new Scanner(file)) {
71       while (scanner.hasNextLine()) {
72         String line = scanner.nextLine();
73         result.append(line).append("\n");
74       }
75
76       scanner.close();
77
78     } catch (IOException e) {
79       e.printStackTrace();
80     }
81
82     return result.toString();
83   }
84
85   @Test
86   public void testGetAsJson_multipleFilterAttributableStatusesIncluded() throws IOException {
87     String expectedOutput =
88       getResourceFileContents("uifilters/testGetAsJson_multipleFilterAttributableStatusesIncluded_expectedValue.json");
89
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");
97
98     Map<String, String>inputOutputData = new HashMap<>();
99     inputOutputData.put("prov-status", "provStatus1");
100     inputOutputData.put("orchestration-status", "orchestrationStatus1");
101
102     // Build UI filters JSON string
103     JSONObject payloadFilter1 = new JSONObject();
104     payloadFilter1.put("filterId", "1");
105     payloadFilter1.put("filterValue", "orchestrationStatus1");
106
107     JSONObject payloadFilter2 = new JSONObject();
108     payloadFilter2.put("filterId", "2");
109     payloadFilter2.put("filterValue", "provStatus1");
110
111     JSONArray payloadFilters = new JSONArray();
112     payloadFilters.put(payloadFilter2);
113     payloadFilters.put(payloadFilter1);
114
115     JSONObject filterPayload = new JSONObject();
116     filterPayload.put("filterList", payloadFilters);
117
118     suggestionSearchEntity.setSuggestionInputPermutations(suggestionInputPermutations);
119     suggestionSearchEntity.setInputOutputData(inputOutputData);
120     suggestionSearchEntity.setFilterPayload(filterPayload);
121
122     String actualOutput = suggestionSearchEntity.getAsJson();
123
124     assertEquals(expectedOutput.trim(), actualOutput.trim());
125   }
126
127   @Test
128   public void testGetAsJson_singleFilterAttributableStatusIncluded() throws IOException {
129     String expectedOutput =
130       getResourceFileContents("uifilters/testGetAsJson_singleFilterAttributableStatusIncluded_expectedValue.json");
131
132     List<String> suggestionInputPermutations = Arrays.asList(
133         "provStatus1 generic-vnf",
134         "generic-vnf provStatus1");
135
136     Map<String, String>inputOutputData = new HashMap<>();
137     inputOutputData.put("prov-status", "provStatus1");
138
139     // Build UI filters JSON string
140     JSONObject payloadFilter1 = new JSONObject();
141     payloadFilter1.put("filterId", "2");
142     payloadFilter1.put("filterValue", "provStatus1");
143
144     JSONArray payloadFilters = new JSONArray();
145     payloadFilters.put(payloadFilter1);
146
147     JSONObject filterPayload = new JSONObject();
148     filterPayload.put("filterList", payloadFilters);
149
150     suggestionSearchEntity.setSuggestionInputPermutations(suggestionInputPermutations);
151     suggestionSearchEntity.setInputOutputData(inputOutputData);
152     suggestionSearchEntity.setFilterPayload(filterPayload);
153
154     String actualOutput = suggestionSearchEntity.getAsJson();
155
156     assertEquals(expectedOutput.trim(), actualOutput.trim());
157   }
158 }