Add unit test cases to increase sonar coverage
[aai/sparky-be.git] / src / test / java / org / onap / aai / sparky / synchronizer / SyncHelperTest.java
index 460bdf0..5a2584f 100644 (file)
-package org.onap.aai.sparky.synchronizer;
-
-import static org.junit.Assert.assertEquals;
-
-import java.io.File;
-import java.io.IOException;
-import java.text.SimpleDateFormat;
-import java.util.Calendar;
-import java.util.TimeZone;
-
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.runners.MockitoJUnitRunner;
-import org.onap.aai.sparky.config.oxm.OxmModelLoader;
-import org.onap.aai.sparky.synchronizer.config.SynchronizerConstants;
-import org.onap.aai.sparky.viewandinspect.config.TierSupportUiConstants;
-
-@RunWith(MockitoJUnitRunner.class)
-public class SyncHelperTest {
-
-  private SyncHelper syncHelper;
-
-  @BeforeClass
-  public static void initBeforeClass() throws IOException {
-    String configHomePath =
-        (new File(".").getCanonicalPath() + "/src/test/resources/appconfig/").replace('\\', '/');
-    TierSupportUiConstants.AJSC_HOME = configHomePath;
-    TierSupportUiConstants.CONFIG_HOME = configHomePath;
-    TierSupportUiConstants.DYNAMIC_CONFIG_APP_LOCATION = configHomePath;
-  }
-
-
-  @Test
-  public void testGetOxmModelLoader() throws Exception {
-    syncHelper = new SyncHelper(new OxmModelLoader());
-    OxmModelLoader oxmLoader = new OxmModelLoader();
-    syncHelper.setOxmModelLoader(oxmLoader);
-    assertEquals(oxmLoader, syncHelper.getOxmModelLoader());
-  }
-  
-  @Test
-  public void testGetFirstSyncTime(){
-    SyncHelper syncHelper = new SyncHelper(new OxmModelLoader());
-    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
-    TimeZone tz = TimeZone.getTimeZone("05:00:00 GMT+00:00");
-    Calendar calendar = Calendar.getInstance(tz);
-    sdf.setTimeZone(tz);
-
-    calendar.set(Calendar.HOUR_OF_DAY, 1);
-    calendar.set(Calendar.MINUTE, 1);
-    calendar.set(Calendar.SECOND, 1);
-
-    long timeCurrent = calendar.getTimeInMillis();
-    int taskFrequencyInDay = 2;
-    
-    assertEquals(calendar.getTimeInMillis(), syncHelper.getFirstSyncTime(calendar, timeCurrent, taskFrequencyInDay));
-    taskFrequencyInDay = 0;
-    assertEquals(SynchronizerConstants.DELAY_NO_PERIODIC_SYNC_IN_MS, syncHelper.getFirstSyncTime(calendar, timeCurrent, taskFrequencyInDay));
-    timeCurrent = timeCurrent + 100;
-    taskFrequencyInDay = 2;
-    Calendar expCalendar = calendar;
-    expCalendar.add(Calendar.DAY_OF_MONTH, taskFrequencyInDay);
-    //assertEquals(expCalendar.getTimeInMillis(), syncHelper.getFirstSyncTime(calendar, calendar.getTimeInMillis() + 100, taskFrequencyInDay));
-    
-  }
-}
+/**\r
+ * ============LICENSE_START=======================================================\r
+ * org.onap.aai\r
+ * ================================================================================\r
+ * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
+ * Copyright © 2017 Amdocs\r
+ * ================================================================================\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *       http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * ============LICENSE_END=========================================================\r
+ *\r
+ * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
+ */\r
+package org.onap.aai.sparky.synchronizer;\r
+\r
+import static org.junit.Assert.assertEquals;\r
+\r
+import java.io.File;\r
+import java.io.IOException;\r
+import java.lang.reflect.Field;\r
+import java.lang.reflect.Modifier;\r
+import java.text.SimpleDateFormat;\r
+import java.util.Calendar;\r
+import java.util.TimeZone;\r
+\r
+import org.junit.BeforeClass;\r
+import org.junit.Test;\r
+import org.junit.runner.RunWith;\r
+import org.mockito.runners.MockitoJUnitRunner;\r
+import org.onap.aai.sparky.config.oxm.OxmModelLoader;\r
+import org.onap.aai.sparky.dal.elasticsearch.config.ElasticSearchConfig;\r
+import org.onap.aai.sparky.synchronizer.config.SynchronizerConfiguration;\r
+import org.onap.aai.sparky.synchronizer.config.SynchronizerConstants;\r
+import org.onap.aai.sparky.viewandinspect.config.TierSupportUiConstants;\r
+\r
+@RunWith(MockitoJUnitRunner.class)\r
+public class SyncHelperTest {\r
+\r
+    private SyncHelper syncHelper;\r
+\r
+    @BeforeClass\r
+    public static void initBeforeClass() throws IOException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {\r
+        String configHomePath =\r
+                (new File(".").getCanonicalPath() + "/src/test/resources/appconfig/").replace('\\', '/');\r
+        TierSupportUiConstants.AJSC_HOME = configHomePath;\r
+        TierSupportUiConstants.CONFIG_HOME = configHomePath;\r
+        TierSupportUiConstants.DYNAMIC_CONFIG_APP_LOCATION = configHomePath;\r
+        ElasticSearchConfig.setConfig(null);\r
+        SynchronizerConfiguration.setInstance(null);\r
+        setFinalStatic();\r
+        System.setProperty("AJSC_HOME", new File(".").getCanonicalPath().replace('\\', '/'));\r
+        TierSupportUiConstants.DYNAMIC_CONFIG_APP_LOCATION = System.getProperty("AJSC_HOME")+"/src/test/resources/appconfig/";\r
+    }\r
+\r
+\r
+    @Test\r
+    public void testGetOxmModelLoader() throws Exception {\r
+        syncHelper = new SyncHelper(new OxmModelLoader());\r
+        OxmModelLoader oxmLoader = new OxmModelLoader();\r
+        syncHelper.setOxmModelLoader(oxmLoader);\r
+        assertEquals(oxmLoader, syncHelper.getOxmModelLoader());\r
+    }\r
+\r
+    @Test\r
+    public void testGetFirstSyncTime(){\r
+        SyncHelper syncHelper = new SyncHelper(new OxmModelLoader());\r
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");\r
+        TimeZone tz = TimeZone.getTimeZone("05:00:00 GMT+00:00");\r
+        Calendar calendar = Calendar.getInstance(tz);\r
+        sdf.setTimeZone(tz);\r
+\r
+        calendar.set(Calendar.HOUR_OF_DAY, 1);\r
+        calendar.set(Calendar.MINUTE, 1);\r
+        calendar.set(Calendar.SECOND, 1);\r
+\r
+        long timeCurrent = calendar.getTimeInMillis();\r
+        int taskFrequencyInDay = 2;\r
+\r
+        assertEquals(calendar.getTimeInMillis(), syncHelper.getFirstSyncTime(calendar, timeCurrent, taskFrequencyInDay));\r
+        taskFrequencyInDay = 0;\r
+        assertEquals(SynchronizerConstants.DELAY_NO_PERIODIC_SYNC_IN_MS, syncHelper.getFirstSyncTime(calendar, timeCurrent, taskFrequencyInDay));\r
+        timeCurrent = timeCurrent + 100;\r
+        taskFrequencyInDay = 2;\r
+        Calendar expCalendar = calendar;\r
+        expCalendar.add(Calendar.DAY_OF_MONTH, taskFrequencyInDay);\r
+        //assertEquals(expCalendar.getTimeInMillis(), syncHelper.getFirstSyncTime(calendar, calendar.getTimeInMillis() + 100, taskFrequencyInDay));\r
+\r
+    }\r
+\r
+    static void setFinalStatic() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {\r
+        Field configField = ElasticSearchConfig.class.getDeclaredField("CONFIG_FILE");\r
+        configField.setAccessible(true);\r
+\r
+        Field modifiersField = Field.class.getDeclaredField( "modifiers" );\r
+        modifiersField.setAccessible( true );\r
+        modifiersField.setInt( configField, configField.getModifiers() & ~Modifier.FINAL );\r
+\r
+        configField.set(null, System.getProperty("AJSC_HOME")+"/src/test/resources/appconfig/elasticsearch.properties");\r
+\r
+        Field syncField = SynchronizerConfiguration.class.getDeclaredField("CONFIG_FILE");\r
+        syncField.setAccessible(true);\r
+\r
+        Field syncModifiersField = Field.class.getDeclaredField( "modifiers" );\r
+        syncModifiersField.setAccessible( true );\r
+        syncModifiersField.setInt( syncField, syncField.getModifiers() & ~Modifier.FINAL );\r
+\r
+        syncField.set(null, System.getProperty("AJSC_HOME")+"/src/test/resources/appconfig/synchronizer.properties");\r
+    }\r
+}\r