Add unit test cases to increase sonar coverage
[aai/sparky-be.git] / src / test / java / org / onap / aai / sparky / synchronizer / AggregationSuggestionSynchronizerTest.java
1 /**\r
2  * ============LICENSE_START=======================================================\r
3  * org.onap.aai\r
4  * ================================================================================\r
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * Copyright © 2017 Amdocs\r
7  * ================================================================================\r
8  * Licensed under the Apache License, Version 2.0 (the "License");\r
9  * you may not use this file except in compliance with the License.\r
10  * You may obtain a copy of the License at\r
11  *\r
12  *       http://www.apache.org/licenses/LICENSE-2.0\r
13  *\r
14  * Unless required by applicable law or agreed to in writing, software\r
15  * distributed under the License is distributed on an "AS IS" BASIS,\r
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17  * See the License for the specific language governing permissions and\r
18  * limitations under the License.\r
19  * ============LICENSE_END=========================================================\r
20  *\r
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
22  */\r
23 package org.onap.aai.sparky.synchronizer;\r
24 \r
25 import java.io.File;\r
26 import java.io.IOException;\r
27 import java.lang.reflect.Field;\r
28 import java.lang.reflect.Modifier;\r
29 \r
30 import org.junit.Assert;\r
31 import org.junit.Before;\r
32 import org.junit.Test;\r
33 import org.onap.aai.sparky.dal.NetworkTransaction;\r
34 import org.onap.aai.sparky.dal.elasticsearch.config.ElasticSearchConfig;\r
35 import org.onap.aai.sparky.dal.rest.HttpMethod;\r
36 import org.onap.aai.sparky.dal.rest.OperationResult;\r
37 import org.onap.aai.sparky.synchronizer.enumeration.OperationState;\r
38 import org.onap.aai.sparky.synchronizer.enumeration.SynchronizerState;\r
39 import org.onap.aai.sparky.viewandinspect.config.TierSupportUiConstants;\r
40 \r
41 public class AggregationSuggestionSynchronizerTest {\r
42 \r
43         @Before\r
44         public void init() throws IOException {\r
45                 System.setProperty("AJSC_HOME", new File(".").getCanonicalPath().replace('\\', '/'));\r
46             TierSupportUiConstants.DYNAMIC_CONFIG_APP_LOCATION = System.getProperty("AJSC_HOME")+"/src/test/resources/appconfig/";\r
47            // TierSupportUiConstants.CONFIG_OXM_LOCATION = System.getProperty("AJSC_HOME")+"/bundleconfig-local/oxm/";\r
48         }\r
49         \r
50         @Test\r
51         public void testDoSync() throws Exception {\r
52         ElasticSearchConfig.setConfig(null);\r
53         setFinalStatic();\r
54                 AggregationSuggestionSynchronizer ass = new AggregationSuggestionSynchronizer("entity-search-index");\r
55                 Assert.assertNotNull(ass);\r
56                 OperationState state = ass.doSync();\r
57                 Assert.assertEquals(OperationState.OK, state);\r
58 \r
59         OperationResult result = new OperationResult();\r
60         result.setResultCode(200);\r
61         result.setResult("result-1");\r
62         result.setNumRequestRetries(1);\r
63         NetworkTransaction ntwTxn = new NetworkTransaction(HttpMethod.GET, "entity-1", result);\r
64         ass.updateElasticSearchCounters(ntwTxn);\r
65 \r
66                 SynchronizerState syncState = ass.getState();\r
67                 Assert.assertEquals(SynchronizerState.IDLE, syncState);\r
68                 \r
69                 String statReport = ass.getStatReport(true);\r
70                 Assert.assertNotNull(statReport);\r
71                 Assert.assertTrue(statReport.contains("Aggregation Suggestion Synchronizer"));\r
72                 \r
73                 ass.shutdown();\r
74         }\r
75 \r
76         static void setFinalStatic() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {\r
77                 Field configField = ElasticSearchConfig.class.getDeclaredField("CONFIG_FILE");\r
78                 configField.setAccessible(true);\r
79 \r
80                 Field modifiersField = Field.class.getDeclaredField( "modifiers" );\r
81                 modifiersField.setAccessible( true );\r
82                 modifiersField.setInt( configField, configField.getModifiers() & ~Modifier.FINAL );\r
83 \r
84                 configField.set(null, System.getProperty("AJSC_HOME")+"/src/test/resources/appconfig/elasticsearch.properties");\r
85         }\r
86 }\r