Don't override SystemProperties without restoring it 61/91661/1
authorIttay Stern <ittay.stern@att.com>
Thu, 18 Jul 2019 08:41:19 +0000 (11:41 +0300)
committerIttay Stern <ittay.stern@att.com>
Thu, 18 Jul 2019 11:06:53 +0000 (11:06 +0000)
This resolves NullPointerException in Dublin's Jenkins'
SyncRestClientForHttpServerTest.

Issue-ID: VID-503

(cherry picked from commit ef34f16650be1f251447a7b9a1d9f1369ca459de)

Change-Id: I65fbed0a6f6214d9495d0cb6456b3a8e4f094a3d
Signed-off-by: Ittay Stern <ittay.stern@att.com>
vid-app-common/src/test/java/org/onap/vid/job/command/ServiceInProgressStatusCommandTest.java
vid-app-common/src/test/java/org/onap/vid/testUtils/TestUtils.java

index 527ba17..31dbc9f 100644 (file)
@@ -58,6 +58,7 @@ import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.eq;
 import static org.mockito.Mockito.*;
 import static org.onap.vid.job.Job.JobStatus.*;
+import static org.onap.vid.testUtils.TestUtils.testWithSystemProperty;
 
 public class ServiceInProgressStatusCommandTest {
 
@@ -218,11 +219,9 @@ public class ServiceInProgressStatusCommandTest {
     }
 
     @Test(dataProvider = "isExpiredJobStatusData")
-    public void isExpiredJobStatusTest(ZonedDateTime jobStartTime, String configValue, boolean expectedResult) {
-        SystemProperties systemProperties = new SystemProperties();
-        systemProperties.setEnvironment(environment);
-        when(environment.getRequiredProperty(VidProperties.VID_JOB_MAX_HOURS_IN_PROGRESS)).thenReturn(configValue);
-        when(environment.containsProperty(VidProperties.VID_JOB_MAX_HOURS_IN_PROGRESS)).thenReturn(true);
-        Assert.assertEquals(command.getExpiryChecker().isExpired(jobStartTime), expectedResult);
+    public void isExpiredJobStatusTest(ZonedDateTime jobStartTime, String configValue, boolean expectedResult) throws Exception {
+        testWithSystemProperty(VidProperties.VID_JOB_MAX_HOURS_IN_PROGRESS, configValue, ()->
+            Assert.assertEquals(command.getExpiryChecker().isExpired(jobStartTime), expectedResult)
+        );
     }
 }
index 3d919d7..756d175 100644 (file)
@@ -50,6 +50,7 @@ import javax.ws.rs.core.GenericType;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 import org.apache.commons.beanutils.BeanUtils;
+import org.apache.commons.lang3.reflect.MethodUtils;
 import org.apache.log4j.LogManager;
 import org.apache.log4j.Logger;
 import org.json.JSONArray;
@@ -61,6 +62,7 @@ import org.mockito.invocation.InvocationOnMock;
 import org.mockito.stubbing.Answer;
 import org.onap.portalsdk.core.util.SystemProperties;
 import org.onap.vid.asdc.beans.Service;
+import org.springframework.core.env.Environment;
 import org.springframework.mock.env.MockEnvironment;
 
 /**
@@ -252,18 +254,27 @@ public class TestUtils {
     }
 
 
-    //Please use resetSystemProperties after using this method, so other test won't be affected
-    public static void mockSystemPropertyWithKeyValue(String key, String value) {
-        MockEnvironment mockEnvironment = new MockEnvironment();
-        mockEnvironment.setProperty(key, value);
+    public interface Test {
 
-        SystemProperties systemProperties = new SystemProperties();
-        systemProperties.setEnvironment(mockEnvironment);
+        void apply();
     }
 
-    public static void resetSystemProperties() {
+    public static void testWithSystemProperty(String key, String value, Test test) throws Exception {
         SystemProperties systemProperties = new SystemProperties();
-        systemProperties.setEnvironment(null);
+        //use reflection to invoke protected method
+        Environment originalEnvironment = (Environment) MethodUtils
+            .invokeMethod(systemProperties, true, "getEnvironment");
+
+        try {
+            Environment environment = mock(Environment.class);
+            systemProperties.setEnvironment(environment);
+            when(environment.getRequiredProperty(key)).thenReturn(value);
+            when(environment.containsProperty(key)).thenReturn(true);
+            test.apply();
+        }
+        finally {
+            systemProperties.setEnvironment(originalEnvironment);
+        }
     }
 
 }