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.openecomp.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.openecomp.sparky.dal.elasticsearch.config.ElasticSearchConfig;
42 import org.openecomp.sparky.dal.exception.ElasticSearchOperationException;
45 import ch.qos.logback.classic.Level;
48 * The Class ElasticSearchConfigTest.
50 public class ElasticSearchConfigTest {
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\"}" + "}" + "}";
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\"]}}}}";
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\"]}}";
81 * @throws Exception the exception
84 public void init() throws Exception {
88 * Failure to initialize properties results in config defaults.
91 public void failureToInitializePropertiesResultsInConfigDefaults() {
93 ElasticSearchConfig config = ElasticSearchConfig.getConfig();
96 * Now verify that all the internal members have been set to default values
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");
109 } catch (Exception exc) {
110 assertEquals("null", exc.getLocalizedMessage());
116 * Validate accessors.
118 * @throws IOException Signals that an I/O exception has occurred.
119 * @throws ServletException the servlet exception
120 * @throws Exception the exception
123 public void validateAccessors() throws IOException, ServletException, Exception {
125 ElasticSearchConfig esConfig = new ElasticSearchConfig();
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");
137 ElasticSearchConfig.setConfig(esConfig);
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");
149 String output = esConfig.toString();
151 assertNotEquals(output, null);
156 * Gets the elastic search settings expect valid config.
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
164 public void getElasticSearchSettings_expectValidConfig()
165 throws IOException, ElasticSearchOperationException {
166 System.setProperty("AJSC_HOME", new File(".").getCanonicalPath().replace('\\', '/'));
168 ElasticSearchConfig esConfig = new ElasticSearchConfig();
170 esConfig.setSettingsFileName("src/main/config/es_settings.json");
172 assertNotNull(esConfig.getElasticSearchSettings());
176 * Gets the elastic search settings expect file not found exception.
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
182 * Need to revisit this test case and change the way this class works
185 public void getElasticSearchSettings_expectFileNotFoundException()
186 throws IOException, ElasticSearchOperationException {
187 System.setProperty("AJSC_HOME", new File(".").getCanonicalPath().replace('\\', '/'));
189 ElasticSearchConfig esConfig = new ElasticSearchConfig();
191 esConfig.setSettingsFileName("src/main/config/es_setting.json");
193 esConfig.getElasticSearchSettings();
198 * Gets the elastic search mappings expect valid config.
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
204 * Need to revisit this test case and change the way this class works
207 public void getElasticSearchMappings_expectValidConfig()
208 throws IOException, ElasticSearchOperationException {
209 System.setProperty("AJSC_HOME", new File(".").getCanonicalPath().replace('\\', '/'));
211 ElasticSearchConfig esConfig = new ElasticSearchConfig();
213 esConfig.setMappingsFileName("src/main/config/es_mappings.json");
215 assertNotNull(esConfig.getElasticSearchMappings());
219 * Gets the elastic search mappings expect file not found exception.
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
225 @Test(expected = ElasticSearchOperationException.class)
226 public void getElasticSearchMappings_expectFileNotFoundException()
227 throws IOException, ElasticSearchOperationException {
228 System.setProperty("AJSC_HOME", new File(".").getCanonicalPath().replace('\\', '/'));
230 ElasticSearchConfig esConfig = new ElasticSearchConfig();
232 esConfig.setSettingsFileName("src/main/config/es_setting.json");
234 esConfig.getElasticSearchMappings();
239 * Builds the elastic search table config expect valid result.
241 * @throws ElasticSearchOperationException the elastic search operation exception
242 * @throws IOException Signals that an I/O exception has occurred.
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();
252 assertNotNull(spyEsConfig.buildElasticSearchTableConfig());
256 * Builds the elastic search table config expect exception.
258 * @throws ElasticSearchOperationException the elastic search operation exception
259 * @throws IOException Signals that an I/O exception has occurred.
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();
269 spyEsConfig.buildElasticSearchTableConfig();