Renaming openecomp to onap
[aai/sparky-be.git] / src / test / java / org / onap / aai / sparky / dal / elasticsearch / ElasticSearchConfigTest.java
1 /* 
2 * ============LICENSE_START=======================================================
3 * SPARKY (AAI UI service)
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
26 package org.onap.aai.sparky.dal.elasticsearch;
27
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertNotEquals;
30 import static org.junit.Assert.assertNotNull;
31
32 import java.io.File;
33 import java.io.IOException;
34
35 import javax.servlet.ServletException;
36
37 import org.junit.Before;
38 import org.junit.Ignore;
39 import org.junit.Test;
40 import org.mockito.Mockito;
41 import org.onap.aai.sparky.dal.elasticsearch.config.ElasticSearchConfig;
42 import org.onap.aai.sparky.dal.exception.ElasticSearchOperationException;
43
44 import ch.qos.logback.classic.Level;
45
46 /**
47  * The Class ElasticSearchConfigTest.
48  */
49 public class ElasticSearchConfigTest {
50
51   private static final String GOOD_MAPPINGS_FILE =
52       "{" + "\"properties\": {" + "\"entityType\": {" + "\"type\": \"string\"" + "},"
53           + "\"edgeTagQueryEntityFieldName\": {" + "\"type\": \"string\"," + "\"index\": \"no\""
54           + "}," + "\"edgeTagQueryEntityFieldValue\": {" + "\"type\": \"string\","
55           + "\"index\": \"no\"" + "}," + "\"searchTagIDs\" : {" + "\"type\" : \"string\"" + "},"
56           + "\"searchTags\": {" + "\"type\": \"string\"," + "\"analyzer\": \"nGram_analyzer\","
57           + "\"search_analyzer\": \"whitespace_analyzer\"}" + "}" + "}";
58
59   private static final String GOOD_SETTINGS_FILE = "{\"analysis\": {" + "\"filter\": {"
60       + "\"nGram_filter\": {" + "\"type\": \"nGram\"," + "\"min_gram\": 1," + "\"max_gram\": 50,"
61       + "\"token_chars\": [" + "\"letter\"," + "\"digit\"," + "\"punctuation\"," + "\"symbol\""
62       + "]}}," + "\"analyzer\": {" + "\"nGram_analyzer\": {" + "\"type\": \"custom\","
63       + "\"tokenizer\": \"whitespace\"," + "\"filter\": [" + "\"lowercase\"," + "\"asciifolding\","
64       + "\"nGram_filter\"]}," + "\"whitespace_analyzer\": {" + "\"type\": \"custom\","
65       + "\"tokenizer\": \"whitespace\"," + "\"filter\": [" + "\"lowercase\","
66       + "\"asciifolding\"]}}}}";
67
68   private static final String BAD_SETTINGS_FILE = "{\"analysis\": {" + "\"filter\": {"
69       + "\"nGram_filter\": {" + "\"type\": \"nGram\"," + "\"min_gram\": 1," + "\"max_gram\": 50,"
70       + "\"token_chars\": [" + "\"letter\"," + "\"digit\"," + "\"punctuation\"," + "\"symbol\""
71       + "]}}," + "\"analyzer\": {" + "\"nGram_analyzer\": {" + "\"type\": \"custom\","
72       + "\"tokenizer\": \"whitespace\"," + "\"filter\": [" + "\"lowercase\"," + "\"asciifolding\","
73       + "\"nGram_filter\"]}," + "\"whitespace_analyzer\": {" + "\"type\": \"custom\","
74       + "\"tokenizer\": \"whitespace\"," + "\"filter\": [" + "\"lowercase\","
75       + "\"asciifolding\"]}}";
76
77   /**
78    * Inits the.
79    *
80    * @throws Exception the exception
81    */
82   @Before
83   public void init() throws Exception {
84   }
85
86   /**
87    * Failure to initialize properties results in config defaults.
88    */
89   @Test
90   public void failureToInitializePropertiesResultsInConfigDefaults() {
91     try {
92       ElasticSearchConfig config = ElasticSearchConfig.getConfig();
93
94       /*
95        * Now verify that all the internal members have been set to default values
96        */
97
98       assertEquals(config.getIpAddress(), "localhost");
99       assertEquals(config.getHttpPort(), "" + 9200);
100       assertEquals(config.getJavaApiPort(), "" + 9300);
101       assertEquals(config.getIndexName(), "entitySearchIndex");
102       assertEquals(config.getType(), "aaiEntities");
103       assertEquals(config.getClusterName(), "elasticsearch");
104       assertEquals(config.getMappingsFileName(), null);
105       assertEquals(config.getSettingsFileName(), null);
106       assertEquals(config.getAuditIndexName(), "auditdataindex");
107
108     } catch (Exception exc) {
109       assertEquals("null", exc.getLocalizedMessage());
110     }
111   }
112
113
114   /**
115    * Validate accessors.
116    *
117    * @throws IOException Signals that an I/O exception has occurred.
118    * @throws ServletException the servlet exception
119    * @throws Exception the exception
120    */
121   @Test
122   public void validateAccessors() throws IOException, ServletException, Exception {
123
124     ElasticSearchConfig esConfig = new ElasticSearchConfig();
125
126     esConfig.setIpAddress("47.248.10.127");
127     esConfig.setHttpPort("8123");
128     esConfig.setJavaApiPort("9123");
129     esConfig.setIndexName("myIndexName");
130     esConfig.setType("myIndexTableType");
131     esConfig.setClusterName("ES_AAI_DEV");
132     esConfig.setMappingsFileName("d:\\1\\mappings.json");
133     esConfig.setSettingsFileName("d:\\1\\settings.json");
134     esConfig.setAuditIndexName("auditIndexName");
135
136     ElasticSearchConfig.setConfig(esConfig);
137
138     assertEquals(esConfig.getIpAddress(), "47.248.10.127");
139     assertEquals(esConfig.getHttpPort(), "8123");
140     assertEquals(esConfig.getJavaApiPort(), "9123");
141     assertEquals(esConfig.getIndexName(), "myIndexName");
142     assertEquals(esConfig.getType(), "myIndexTableType");
143     assertEquals(esConfig.getClusterName(), "ES_AAI_DEV");
144     assertEquals(esConfig.getMappingsFileName(), "d:\\1\\mappings.json");
145     assertEquals(esConfig.getSettingsFileName(), "d:\\1\\settings.json");
146     assertEquals(esConfig.getAuditIndexName(), "auditIndexName");
147
148     String output = esConfig.toString();
149
150     assertNotEquals(output, null);
151
152   }
153
154   /**
155    * Gets the elastic search settings expect valid config.
156    *
157    * @return the elastic search settings expect valid config
158    * @throws IOException Signals that an I/O exception has occurred.
159    * @throws ElasticSearchOperationException the elastic search operation exception
160    * Need to revisit this test case and change the way this class works
161    */
162   @Ignore
163   public void getElasticSearchSettings_expectValidConfig()
164       throws IOException, ElasticSearchOperationException {
165     System.setProperty("AJSC_HOME", new File(".").getCanonicalPath().replace('\\', '/'));
166
167     ElasticSearchConfig esConfig = new ElasticSearchConfig();
168
169     esConfig.setSettingsFileName("src/main/config/es_settings.json");
170
171     assertNotNull(esConfig.getElasticSearchSettings());
172   }
173
174   /**
175    * Gets the elastic search settings expect file not found exception.
176    *
177    * @return the elastic search settings expect file not found exception
178    * @throws IOException Signals that an I/O exception has occurred.
179    * @throws ElasticSearchOperationException the elastic search operation exception
180    * 
181    * Need to revisit this test case and change the way this class works
182    */
183   @Ignore
184   public void getElasticSearchSettings_expectFileNotFoundException()
185       throws IOException, ElasticSearchOperationException {
186     System.setProperty("AJSC_HOME", new File(".").getCanonicalPath().replace('\\', '/'));
187
188     ElasticSearchConfig esConfig = new ElasticSearchConfig();
189
190     esConfig.setSettingsFileName("src/main/config/es_setting.json");
191
192     esConfig.getElasticSearchSettings();
193
194   }
195
196   /**
197    * Gets the elastic search mappings expect valid config.
198    *
199    * @return the elastic search mappings expect valid config
200    * @throws IOException Signals that an I/O exception has occurred.
201    * @throws ElasticSearchOperationException the elastic search operation exception
202    * 
203    * Need to revisit this test case and change the way this class works
204    */
205   @Ignore
206   public void getElasticSearchMappings_expectValidConfig()
207       throws IOException, ElasticSearchOperationException {
208     System.setProperty("AJSC_HOME", new File(".").getCanonicalPath().replace('\\', '/'));
209
210     ElasticSearchConfig esConfig = new ElasticSearchConfig();
211
212     esConfig.setMappingsFileName("src/main/config/es_mappings.json");
213
214     assertNotNull(esConfig.getElasticSearchMappings());
215   }
216
217   /**
218    * Gets the elastic search mappings expect file not found exception.
219    *
220    * @return the elastic search mappings expect file not found exception
221    * @throws IOException Signals that an I/O exception has occurred.
222    * @throws ElasticSearchOperationException the elastic search operation exception
223    */
224   @Test(expected = ElasticSearchOperationException.class)
225   public void getElasticSearchMappings_expectFileNotFoundException()
226       throws IOException, ElasticSearchOperationException {
227     System.setProperty("AJSC_HOME", new File(".").getCanonicalPath().replace('\\', '/'));
228
229     ElasticSearchConfig esConfig = new ElasticSearchConfig();
230
231     esConfig.setSettingsFileName("src/main/config/es_setting.json");
232
233     esConfig.getElasticSearchMappings();
234
235   }
236
237   /**
238    * Builds the elastic search table config expect valid result.
239    *
240    * @throws ElasticSearchOperationException the elastic search operation exception
241    * @throws IOException Signals that an I/O exception has occurred.
242    */
243   @Test
244   public void buildElasticSearchTableConfig_expectValidResult()
245       throws ElasticSearchOperationException, IOException {
246     ElasticSearchConfig spyEsConfig = Mockito.spy(new ElasticSearchConfig());
247     Mockito.doReturn(GOOD_MAPPINGS_FILE).when(spyEsConfig).getElasticSearchMappings();
248     Mockito.doReturn(GOOD_SETTINGS_FILE).when(spyEsConfig).getElasticSearchSettings();
249     Mockito.doReturn("myIndexTableType").when(spyEsConfig).getType();
250
251     assertNotNull(spyEsConfig.buildElasticSearchTableConfig());
252   }
253
254   /**
255    * Builds the elastic search table config expect exception.
256    *
257    * @throws ElasticSearchOperationException the elastic search operation exception
258    * @throws IOException Signals that an I/O exception has occurred.
259    */
260   @Test(expected = ElasticSearchOperationException.class)
261   public void buildElasticSearchTableConfig_expectException()
262       throws ElasticSearchOperationException, IOException {
263     ElasticSearchConfig spyEsConfig = Mockito.spy(new ElasticSearchConfig());
264     Mockito.doReturn(GOOD_MAPPINGS_FILE).when(spyEsConfig).getElasticSearchMappings();
265     Mockito.doReturn(BAD_SETTINGS_FILE).when(spyEsConfig).getElasticSearchSettings();
266     Mockito.doReturn("myIndexTableType").when(spyEsConfig).getType();
267
268     spyEsConfig.buildElasticSearchTableConfig();
269   }
270
271 }