Update the dependencies to use project version
[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,\r
52       IllegalArgumentException, IllegalAccessException {\r
53     String configHomePath =\r
54         (new File(".").getCanonicalPath() + "/src/test/resources/appconfig/").replace('\\', '/');\r
55     TierSupportUiConstants.AJSC_HOME = configHomePath;\r
56     TierSupportUiConstants.CONFIG_HOME = configHomePath;\r
57     TierSupportUiConstants.DYNAMIC_CONFIG_APP_LOCATION = configHomePath;\r
58     ElasticSearchConfig.setConfig(null);\r
59     SynchronizerConfiguration.setInstance(null);\r
60     setFinalStatic();\r
61     System.setProperty("AJSC_HOME", new File(".").getCanonicalPath().replace('\\', '/'));\r
62     TierSupportUiConstants.DYNAMIC_CONFIG_APP_LOCATION =\r
63         System.getProperty("AJSC_HOME") + "/src/test/resources/appconfig/";\r
64   }\r
65 \r
66 \r
67   @Test\r
68   public void testGetOxmModelLoader() throws Exception {\r
69     syncHelper = new SyncHelper(new OxmModelLoader());\r
70     OxmModelLoader oxmLoader = new OxmModelLoader();\r
71     syncHelper.setOxmModelLoader(oxmLoader);\r
72     assertEquals(oxmLoader, syncHelper.getOxmModelLoader());\r
73   }\r
74 \r
75   @Test\r
76   public void testGetFirstSyncTime() {\r
77     SyncHelper syncHelper = new SyncHelper(new OxmModelLoader());\r
78     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");\r
79     TimeZone tz = TimeZone.getTimeZone("05:00:00 GMT+00:00");\r
80     Calendar calendar = Calendar.getInstance(tz);\r
81     sdf.setTimeZone(tz);\r
82 \r
83     calendar.set(Calendar.HOUR_OF_DAY, 1);\r
84     calendar.set(Calendar.MINUTE, 1);\r
85     calendar.set(Calendar.SECOND, 1);\r
86 \r
87     long timeCurrent = calendar.getTimeInMillis();\r
88     int taskFrequencyInDay = 2;\r
89 \r
90     assertEquals(calendar.getTimeInMillis(),\r
91         syncHelper.getFirstSyncTime(calendar, timeCurrent, taskFrequencyInDay));\r
92     taskFrequencyInDay = 0;\r
93     assertEquals(SynchronizerConstants.DELAY_NO_PERIODIC_SYNC_IN_MS,\r
94         syncHelper.getFirstSyncTime(calendar, timeCurrent, taskFrequencyInDay));\r
95     timeCurrent = timeCurrent + 100;\r
96     taskFrequencyInDay = 2;\r
97     Calendar expCalendar = calendar;\r
98     expCalendar.add(Calendar.DAY_OF_MONTH, taskFrequencyInDay);\r
99     // assertEquals(expCalendar.getTimeInMillis(), syncHelper.getFirstSyncTime(calendar,\r
100     // calendar.getTimeInMillis() + 100, taskFrequencyInDay));\r
101 \r
102   }\r
103 \r
104   static void setFinalStatic() throws NoSuchFieldException, SecurityException,\r
105       IllegalArgumentException, IllegalAccessException {\r
106     Field configField = ElasticSearchConfig.class.getDeclaredField("CONFIG_FILE");\r
107     configField.setAccessible(true);\r
108 \r
109     Field modifiersField = Field.class.getDeclaredField("modifiers");\r
110     modifiersField.setAccessible(true);\r
111     modifiersField.setInt(configField, configField.getModifiers() & ~Modifier.FINAL);\r
112 \r
113     configField.set(null,\r
114         System.getProperty("AJSC_HOME") + "/src/test/resources/appconfig/elasticsearch.properties");\r
115 \r
116     Field syncField = SynchronizerConfiguration.class.getDeclaredField("CONFIG_FILE");\r
117     syncField.setAccessible(true);\r
118 \r
119     Field syncModifiersField = Field.class.getDeclaredField("modifiers");\r
120     syncModifiersField.setAccessible(true);\r
121     syncModifiersField.setInt(syncField, syncField.getModifiers() & ~Modifier.FINAL);\r
122 \r
123     syncField.set(null,\r
124         System.getProperty("AJSC_HOME") + "/src/test/resources/appconfig/synchronizer.properties");\r
125   }\r
126 }\r