2 * ============LICENSE_START=======================================================
3 * SPARKY (AAI UI service)
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
26 package org.onap.aai.sparky.dal.elasticsearch;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertNotEquals;
30 import static org.junit.Assert.assertNotNull;
33 import java.io.IOException;
35 import javax.servlet.ServletException;
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;
44 import ch.qos.logback.classic.Level;
47 * The Class ElasticSearchConfigTest.
49 public class ElasticSearchConfigTest {
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\"}" + "}" + "}";
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\"]}}}}";
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\"]}}";
80 * @throws Exception the exception
83 public void init() throws Exception {
87 * Failure to initialize properties results in config defaults.
90 public void failureToInitializePropertiesResultsInConfigDefaults() {
92 ElasticSearchConfig config = ElasticSearchConfig.getConfig();
95 * Now verify that all the internal members have been set to default values
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");
108 } catch (Exception exc) {
109 assertEquals("null", exc.getLocalizedMessage());
115 * Validate accessors.
117 * @throws IOException Signals that an I/O exception has occurred.
118 * @throws ServletException the servlet exception
119 * @throws Exception the exception
122 public void validateAccessors() throws IOException, ServletException, Exception {
124 ElasticSearchConfig esConfig = new ElasticSearchConfig();
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");
136 ElasticSearchConfig.setConfig(esConfig);
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");
148 String output = esConfig.toString();
150 assertNotEquals(output, null);
155 * Gets the elastic search settings expect valid config.
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
163 public void getElasticSearchSettings_expectValidConfig()
164 throws IOException, ElasticSearchOperationException {
165 System.setProperty("AJSC_HOME", new File(".").getCanonicalPath().replace('\\', '/'));
167 ElasticSearchConfig esConfig = new ElasticSearchConfig();
169 esConfig.setSettingsFileName("src/main/config/es_settings.json");
171 assertNotNull(esConfig.getElasticSearchSettings());
175 * Gets the elastic search settings expect file not found exception.
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
181 * Need to revisit this test case and change the way this class works
184 public void getElasticSearchSettings_expectFileNotFoundException()
185 throws IOException, ElasticSearchOperationException {
186 System.setProperty("AJSC_HOME", new File(".").getCanonicalPath().replace('\\', '/'));
188 ElasticSearchConfig esConfig = new ElasticSearchConfig();
190 esConfig.setSettingsFileName("src/main/config/es_setting.json");
192 esConfig.getElasticSearchSettings();
197 * Gets the elastic search mappings expect valid config.
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
203 * Need to revisit this test case and change the way this class works
206 public void getElasticSearchMappings_expectValidConfig()
207 throws IOException, ElasticSearchOperationException {
208 System.setProperty("AJSC_HOME", new File(".").getCanonicalPath().replace('\\', '/'));
210 ElasticSearchConfig esConfig = new ElasticSearchConfig();
212 esConfig.setMappingsFileName("src/main/config/es_mappings.json");
214 assertNotNull(esConfig.getElasticSearchMappings());
218 * Gets the elastic search mappings expect file not found exception.
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
224 @Test(expected = ElasticSearchOperationException.class)
225 public void getElasticSearchMappings_expectFileNotFoundException()
226 throws IOException, ElasticSearchOperationException {
227 System.setProperty("AJSC_HOME", new File(".").getCanonicalPath().replace('\\', '/'));
229 ElasticSearchConfig esConfig = new ElasticSearchConfig();
231 esConfig.setSettingsFileName("src/main/config/es_setting.json");
233 esConfig.getElasticSearchMappings();
238 * Builds the elastic search table config expect valid result.
240 * @throws ElasticSearchOperationException the elastic search operation exception
241 * @throws IOException Signals that an I/O exception has occurred.
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();
251 assertNotNull(spyEsConfig.buildElasticSearchTableConfig());
255 * Builds the elastic search table config expect exception.
257 * @throws ElasticSearchOperationException the elastic search operation exception
258 * @throws IOException Signals that an I/O exception has occurred.
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();
268 spyEsConfig.buildElasticSearchTableConfig();