import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
+import java.time.LocalDateTime;
+import java.time.Month;
import java.util.Collections;
import java.util.List;
-import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.TimeUnit;
import org.onap.aai.domain.yang.ServiceInstance;
import org.onap.aai.domain.yang.Tenant;
import org.onap.policy.aai.AaiCqResponse;
+import org.onap.policy.common.utils.coder.Coder;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.controlloop.ControlLoopOperation;
import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
import org.onap.policy.so.SoModelInfo;
import org.onap.policy.so.SoRequest;
import org.onap.policy.so.SoRequestInfo;
-import org.onap.policy.so.SoRequestParameters;
import org.onap.policy.so.SoRequestReferences;
import org.onap.policy.so.SoRequestStatus;
import org.onap.policy.so.SoResponse;
public class SoOperationTest extends BasicSoOperation {
+ private static final String VF_COUNT_KEY = SoConstants.VF_COUNT_PREFIX
+ + "[my-model-customization-id][my-model-invariant-id][my-model-version-id]";
private SoOperation oper;
/**
assertEquals(DEFAULT_OPERATION, oper.getName());
assertSame(config, oper.getConfig());
assertEquals(1000 * WAIT_SEC_GETS, oper.getWaitMsGet());
+
+ // check when Target is null
+ params = params.toBuilder().target(null).build();
+ assertThatIllegalArgumentException().isThrownBy(() -> new SoOperation(params, config) {})
+ .withMessageContaining("Target information");
}
@Test
public void testValidateTarget() {
// check when various fields are null
- verifyNotNull("model-customization-id", target::getModelCustomizationId, target::setModelCustomizationId);
- verifyNotNull("model-invariant-id", target::getModelInvariantId, target::setModelInvariantId);
- verifyNotNull("model-version-id", target::getModelVersionId, target::setModelVersionId);
+ verifyNotNull("modelCustomizationId", target::getModelCustomizationId, target::setModelCustomizationId);
+ verifyNotNull("modelInvariantId", target::getModelInvariantId, target::setModelInvariantId);
+ verifyNotNull("modelVersionId", target::getModelVersionId, target::setModelVersionId);
// verify it's still valid
assertThatCode(() -> new VfModuleCreate(params, config)).doesNotThrowAnyException();
-
- // check when Target, itself, is null
- params = params.toBuilder().target(null).build();
- assertThatIllegalArgumentException().isThrownBy(() -> new VfModuleCreate(params, config))
- .withMessageContaining("Target information");
}
private void verifyNotNull(String expectedText, Supplier<String> getter, Consumer<String> setter) {
}
@Test
- public void testStoreVfCountRunGuard() throws Exception {
- // insert CQ data so it's there for the guard
+ public void testObtainVfCount_testGetVfCount_testSetVfCount() throws Exception {
+ // insert CQ data so it's there for the check
context.setProperty(AaiCqResponse.CONTEXT_KEY, makeCqResponse());
- // cause guard to fail
- OperationOutcome outcome2 = params.makeOutcome();
- outcome2.setResult(PolicyResult.FAILURE);
- when(guardOperation.start()).thenReturn(CompletableFuture.completedFuture(outcome2));
+ // shouldn't actually need to do anything
+ assertNull(oper.obtainVfCount());
+
+ // verify that the count was stored
+ Integer vfcount = context.getProperty(VF_COUNT_KEY);
+ assertEquals(VF_COUNT, vfcount);
+ assertEquals(VF_COUNT.intValue(), oper.getVfCount());
+
+ // change the count and then verify that it isn't overwritten by another call
+ oper.setVfCount(VF_COUNT + 1);
+
+ assertNull(oper.obtainVfCount());
+ vfcount = context.getProperty(VF_COUNT_KEY);
+ assertEquals(VF_COUNT + 1, vfcount.intValue());
+ assertEquals(VF_COUNT + 1, oper.getVfCount());
+ }
+
+ /**
+ * Tests obtainVfCount() when it actually has to query.
+ */
+ @Test
+ public void testObtainVfCountQuery() throws Exception {
+ CompletableFuture<OperationOutcome> future2 = oper.obtainVfCount();
+ assertNotNull(future2);
+ assertTrue(executor.runAll(100));
+
+ // not done yet
+ assertFalse(future2.isDone());
+
+ provideCqResponse(makeCqResponse());
- CompletableFuture<OperationOutcome> future2 = oper.storeVfCountRunGuard();
assertTrue(executor.runAll(100));
assertTrue(future2.isDone());
- assertEquals(PolicyResult.FAILURE, future2.get().getResult());
+ assertEquals(PolicyResult.SUCCESS, future2.get().getResult());
// verify that the count was stored
- Integer vfcount = context.getProperty(SoConstants.CONTEXT_KEY_VF_COUNT);
+ Integer vfcount = context.getProperty(VF_COUNT_KEY);
assertEquals(VF_COUNT, vfcount);
+
+ // repeat - shouldn't need to do anything now
+ assertNull(oper.obtainVfCount());
}
@Test
public void testPostProcess() throws Exception {
// completed
+ oper.generateSubRequestId(2);
+ assertNull(oper.getSubRequestId());
CompletableFuture<OperationOutcome> future2 = oper.postProcessResponse(outcome, PATH, rawResponse, response);
assertTrue(future2.isDone());
assertSame(outcome, future2.get());
assertEquals(PolicyResult.SUCCESS, outcome.getResult());
+ assertNotNull(oper.getSubRequestId());
// failed
+ oper.generateSubRequestId(2);
+ assertNull(oper.getSubRequestId());
response.getRequest().getRequestStatus().setRequestState(SoOperation.FAILED);
future2 = oper.postProcessResponse(outcome, PATH, rawResponse, response);
assertTrue(future2.isDone());
assertSame(outcome, future2.get());
assertEquals(PolicyResult.FAILURE, outcome.getResult());
+ assertNotNull(oper.getSubRequestId());
// no request id in the response
+ oper.generateSubRequestId(2);
+ assertNull(oper.getSubRequestId());
response.getRequestReferences().setRequestId(null);
response.getRequest().getRequestStatus().setRequestState("unknown");
assertThatIllegalArgumentException()
CompletableFuture<OperationOutcome> future2 = oper.postProcessResponse(outcome, PATH, rawResponse, response);
- assertSame(outcome, future2.get(500, TimeUnit.SECONDS));
+ assertSame(outcome, future2.get(5, TimeUnit.SECONDS));
assertEquals(PolicyResult.SUCCESS, outcome.getResult());
assertEquals(2, oper.getGetCount());
oper.resetGetCount();
assertEquals(0, oper.getGetCount());
+ assertNull(oper.getSubRequestId());
}
@Test
// try with null target
params = params.toBuilder().target(null).build();
- oper = new SoOperation(params, config) {};
-
- assertThatIllegalArgumentException().isThrownBy(() -> oper.prepareSoModelInfo()).withMessage("missing Target");
+ assertThatIllegalArgumentException().isThrownBy(() -> new SoOperation(params, config) {})
+ .withMessageContaining("missing Target");
}
private void verifyMissingModelInfo(Supplier<String> getter, Consumer<String> setter) {
@Test
public void testBuildRequestParameters() throws CoderException {
// valid data
- SoRequestParameters reqParams = oper.buildRequestParameters();
- verifyRequest("reqparams.json", reqParams);
+ verifyRequest("reqparams.json", oper.buildRequestParameters().get());
// invalid json
params.getPayload().put(SoOperation.REQ_PARAM_NM, "{invalid json");
// missing data
params.getPayload().remove(SoOperation.REQ_PARAM_NM);
- assertNull(oper.buildRequestParameters());
+ assertTrue(oper.buildRequestParameters().isEmpty());
// null payload
params = params.toBuilder().payload(null).build();
oper = new SoOperation(params, config) {};
- assertNull(oper.buildRequestParameters());
+ assertTrue(oper.buildRequestParameters().isEmpty());
}
@Test
public void testBuildConfigurationParameters() {
// valid data
- List<Map<String, String>> result = oper.buildConfigurationParameters();
- assertEquals(List.of(Collections.emptyMap()), result);
+ assertEquals(List.of(Collections.emptyMap()), oper.buildConfigurationParameters().get());
// invalid json
params.getPayload().put(SoOperation.CONFIG_PARAM_NM, "{invalid json");
// missing data
params.getPayload().remove(SoOperation.CONFIG_PARAM_NM);
- assertNull(oper.buildConfigurationParameters());
+ assertTrue(oper.buildConfigurationParameters().isEmpty());
// null payload
params = params.toBuilder().payload(null).build();
oper = new SoOperation(params, config) {};
- assertNull(oper.buildConfigurationParameters());
+ assertTrue(oper.buildConfigurationParameters().isEmpty());
}
@Test
when(cq.getDefaultCloudRegion()).thenReturn(region);
assertSame(region, oper.getDefaultCloudRegion(cq));
}
+
+ @Test
+ public void testMakeCoder() throws CoderException {
+ Coder opcoder = oper.makeCoder();
+
+ // ensure we can decode an SO timestamp
+ String json = "{'request':{'finishTime':'Fri, 15 May 2020 12:14:21 GMT'}}";
+ SoResponse resp = opcoder.decode(json.replace('\'', '"'), SoResponse.class);
+
+ LocalDateTime tfinish = resp.getRequest().getFinishTime();
+ assertNotNull(tfinish);
+ assertEquals(2020, tfinish.getYear());
+ assertEquals(Month.MAY, tfinish.getMonth());
+ }
}