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