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