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 {
}
@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)
+ );
}
}
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;
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;
/**
}
- //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);
+ }
}
}