aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/openecomp/datarouter/entity/SuggestionSearchEntityTest.java
blob: 4461bdceebda5d0a5f3015554c304bf77eafd731 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
 * ============LICENSE_START=======================================================
 * DataRouter
 * ================================================================================
 * Copyright © 2017 AT&T Intellectual Property.
 * Copyright © 2017 Amdocs
 * All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 *
 * ECOMP and OpenECOMP are trademarks
 * and service marks of AT&T Intellectual Property.
 */
package org.openecomp.datarouter.entity;

import static org.junit.Assert.assertEquals;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.onap.aai.datarouter.entity.SuggestionSearchEntity;
import org.openecomp.datarouter.search.filters.config.UiFiltersSchemaUtility;

public class SuggestionSearchEntityTest {
  private static SuggestionSearchEntity suggestionSearchEntity;

  @Before
  public void setUpBeforeTest() {
    UiFiltersSchemaUtility filtersSchemaUtility = Mockito.mock(UiFiltersSchemaUtility.class);
    Mockito.when(filtersSchemaUtility.loadUiFiltersConfig()).thenReturn(null);

    suggestionSearchEntity = new SuggestionSearchEntity();
    suggestionSearchEntity.setFiltersSchemaUtility(filtersSchemaUtility);
    suggestionSearchEntity.setEntityType("generic-vnf");
    suggestionSearchEntity.setEntityTypeAliases(Arrays.asList("VNFs"));
  }

  /**
   * Read in the contents of the given file (can include sub-path) in test/resources folder
   *
   * @param filePath The file name or path (relative to test/resources) to read from
   * @return The contents of the file as a String
   */
  public String getResourceFileContents(String filePath) {
    StringBuilder result = new StringBuilder("");

    ClassLoader classLoader = getClass().getClassLoader();
    File file = new File(classLoader.getResource(filePath).getFile());

    try (Scanner scanner = new Scanner(file)) {
      while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        result.append(line).append("\n");
      }

      scanner.close();

    } catch (IOException e) {
      e.printStackTrace();
    }

    return result.toString();
  }

  @Test
  public void testGetAsJson_multipleFilterAttributableStatusesIncluded() throws IOException {
    String expectedOutput =
      getResourceFileContents("uifilters/testGetAsJson_multipleFilterAttributableStatusesIncluded_expectedValue.json");

    List<String> suggestionInputPermutations = Arrays.asList(
        "provStatus1 orchestrationStatus1 generic-vnf",
        "provStatus1 generic-vnf orchestrationStatus1",
        "orchestrationStatus1 generic-vnf provStatus1",
        "orchestrationStatus1 provStatus1 generic-vnf",
        "generic-vnf provStatus1 orchestrationStatus1",
        "generic-vnf orchestrationStatus1 provStatus1");

    Map<String, String>inputOutputData = new HashMap<>();
    inputOutputData.put("prov-status", "provStatus1");
    inputOutputData.put("orchestration-status", "orchestrationStatus1");

    // Build UI filters JSON string
    JSONObject payloadFilter1 = new JSONObject();
    payloadFilter1.put("filterId", "1");
    payloadFilter1.put("filterValue", "orchestrationStatus1");

    JSONObject payloadFilter2 = new JSONObject();
    payloadFilter2.put("filterId", "2");
    payloadFilter2.put("filterValue", "provStatus1");

    JSONArray payloadFilters = new JSONArray();
    payloadFilters.put(payloadFilter2);
    payloadFilters.put(payloadFilter1);

    JSONObject filterPayload = new JSONObject();
    filterPayload.put("filterList", payloadFilters);

    suggestionSearchEntity.setSuggestionInputPermutations(suggestionInputPermutations);
    suggestionSearchEntity.setInputOutputData(inputOutputData);
    suggestionSearchEntity.setFilterPayload(filterPayload);

    String actualOutput = suggestionSearchEntity.getAsJson();

    assertEquals(expectedOutput.trim(), actualOutput.trim());
  }

  @Test
  public void testGetAsJson_singleFilterAttributableStatusIncluded() throws IOException {
    String expectedOutput =
      getResourceFileContents("uifilters/testGetAsJson_singleFilterAttributableStatusIncluded_expectedValue.json");

    List<String> suggestionInputPermutations = Arrays.asList(
        "provStatus1 generic-vnf",
        "generic-vnf provStatus1");

    Map<String, String>inputOutputData = new HashMap<>();
    inputOutputData.put("prov-status", "provStatus1");

    // Build UI filters JSON string
    JSONObject payloadFilter1 = new JSONObject();
    payloadFilter1.put("filterId", "2");
    payloadFilter1.put("filterValue", "provStatus1");

    JSONArray payloadFilters = new JSONArray();
    payloadFilters.put(payloadFilter1);

    JSONObject filterPayload = new JSONObject();
    filterPayload.put("filterList", payloadFilters);

    suggestionSearchEntity.setSuggestionInputPermutations(suggestionInputPermutations);
    suggestionSearchEntity.setInputOutputData(inputOutputData);
    suggestionSearchEntity.setFilterPayload(filterPayload);

    String actualOutput = suggestionSearchEntity.getAsJson();

    assertEquals(expectedOutput.trim(), actualOutput.trim());
  }
}