Add unit test cases to increase sonar coverage
[aai/sparky-be.git] / src / test / java / org / onap / aai / sparky / synchronizer / SyncHelperTest.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 static org.junit.Assert.assertEquals;\r
26 \r
27 import java.io.File;\r
28 import java.io.IOException;\r
29 import java.lang.reflect.Field;\r
30 import java.lang.reflect.Modifier;\r
31 import java.text.SimpleDateFormat;\r
32 import java.util.Calendar;\r
33 import java.util.TimeZone;\r
34 \r
35 import org.junit.BeforeClass;\r
36 import org.junit.Test;\r
37 import org.junit.runner.RunWith;\r
38 import org.mockito.runners.MockitoJUnitRunner;\r
39 import org.onap.aai.sparky.config.oxm.OxmModelLoader;\r
40 import org.onap.aai.sparky.dal.elasticsearch.config.ElasticSearchConfig;\r
41 import org.onap.aai.sparky.synchronizer.config.SynchronizerConfiguration;\r
42 import org.onap.aai.sparky.synchronizer.config.SynchronizerConstants;\r
43 import org.onap.aai.sparky.viewandinspect.config.TierSupportUiConstants;\r
44 \r
45 @RunWith(MockitoJUnitRunner.class)\r
46 public class SyncHelperTest {\r
47 \r
48     private SyncHelper syncHelper;\r
49 \r
50     @BeforeClass\r
51     public static void initBeforeClass() throws IOException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {\r
52         String configHomePath =\r
53                 (new File(".").getCanonicalPath() + "/src/test/resources/appconfig/").replace('\\', '/');\r
54         TierSupportUiConstants.AJSC_HOME = configHomePath;\r
55         TierSupportUiConstants.CONFIG_HOME = configHomePath;\r
56         TierSupportUiConstants.DYNAMIC_CONFIG_APP_LOCATION = configHomePath;\r
57         ElasticSearchConfig.setConfig(null);\r
58         SynchronizerConfiguration.setInstance(null);\r
59         setFinalStatic();\r
60         System.setProperty("AJSC_HOME", new File(".").getCanonicalPath().replace('\\', '/'));\r
61         TierSupportUiConstants.DYNAMIC_CONFIG_APP_LOCATION = System.getProperty("AJSC_HOME")+"/src/test/resources/appconfig/";\r
62     }\r
63 \r
64 \r
65     @Test\r
66     public void testGetOxmModelLoader() throws Exception {\r
67         syncHelper = new SyncHelper(new OxmModelLoader());\r
68         OxmModelLoader oxmLoader = new OxmModelLoader();\r
69         syncHelper.setOxmModelLoader(oxmLoader);\r
70         assertEquals(oxmLoader, syncHelper.getOxmModelLoader());\r
71     }\r
72 \r
73     @Test\r
74     public void testGetFirstSyncTime(){\r
75         SyncHelper syncHelper = new SyncHelper(new OxmModelLoader());\r
76         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");\r
77         TimeZone tz = TimeZone.getTimeZone("05:00:00 GMT+00:00");\r
78         Calendar calendar = Calendar.getInstance(tz);\r
79         sdf.setTimeZone(tz);\r
80 \r
81         calendar.set(Calendar.HOUR_OF_DAY, 1);\r
82         calendar.set(Calendar.MINUTE, 1);\r
83         calendar.set(Calendar.SECOND, 1);\r
84 \r
85         long timeCurrent = calendar.getTimeInMillis();\r
86         int taskFrequencyInDay = 2;\r
87 \r
88         assertEquals(calendar.getTimeInMillis(), syncHelper.getFirstSyncTime(calendar, timeCurrent, taskFrequencyInDay));\r
89         taskFrequencyInDay = 0;\r
90         assertEquals(SynchronizerConstants.DELAY_NO_PERIODIC_SYNC_IN_MS, syncHelper.getFirstSyncTime(calendar, timeCurrent, taskFrequencyInDay));\r
91         timeCurrent = timeCurrent + 100;\r
92         taskFrequencyInDay = 2;\r
93         Calendar expCalendar = calendar;\r
94         expCalendar.add(Calendar.DAY_OF_MONTH, taskFrequencyInDay);\r
95         //assertEquals(expCalendar.getTimeInMillis(), syncHelper.getFirstSyncTime(calendar, calendar.getTimeInMillis() + 100, taskFrequencyInDay));\r
96 \r
97     }\r
98 \r
99     static void setFinalStatic() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {\r
100         Field configField = ElasticSearchConfig.class.getDeclaredField("CONFIG_FILE");\r
101         configField.setAccessible(true);\r
102 \r
103         Field modifiersField = Field.class.getDeclaredField( "modifiers" );\r
104         modifiersField.setAccessible( true );\r
105         modifiersField.setInt( configField, configField.getModifiers() & ~Modifier.FINAL );\r
106 \r
107         configField.set(null, System.getProperty("AJSC_HOME")+"/src/test/resources/appconfig/elasticsearch.properties");\r
108 \r
109         Field syncField = SynchronizerConfiguration.class.getDeclaredField("CONFIG_FILE");\r
110         syncField.setAccessible(true);\r
111 \r
112         Field syncModifiersField = Field.class.getDeclaredField( "modifiers" );\r
113         syncModifiersField.setAccessible( true );\r
114         syncModifiersField.setInt( syncField, syncField.getModifiers() & ~Modifier.FINAL );\r
115 \r
116         syncField.set(null, System.getProperty("AJSC_HOME")+"/src/test/resources/appconfig/synchronizer.properties");\r
117     }\r
118 }\r