package org.onap.policy.controlloop.actor.cds;
+import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
+import org.junit.AfterClass;
import org.junit.Before;
+import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.onap.aai.domain.yang.GenericVnf;
import org.onap.aai.domain.yang.ServiceInstance;
import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput;
+import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput;
import org.onap.policy.aai.AaiCqResponse;
import org.onap.policy.cds.client.CdsProcessorGrpcClient;
import org.onap.policy.cds.properties.CdsServerProperties;
import org.onap.policy.controlloop.actor.cds.constants.CdsActorConstants;
import org.onap.policy.controlloop.actorserviceprovider.ActorService;
import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
+import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
import org.onap.policy.controlloop.policy.PolicyResult;
import org.onap.policy.controlloop.policy.Target;
import org.onap.policy.controlloop.policy.TargetType;
+import org.onap.policy.simulators.CdsSimulator;
+import org.onap.policy.simulators.Util;
public class GrpcOperationTest {
private static final String TARGET_ENTITY = "entity";
private static final UUID REQUEST_ID = UUID.randomUUID();
private static final Coder coder = new StandardCoder();
+ protected static final Executor blockingExecutor = command -> {
+ Thread thread = new Thread(command);
+ thread.setDaemon(true);
+ thread.start();
+ };
+
+ private static CdsSimulator sim;
+
@Mock
private CdsProcessorGrpcClient cdsClient;
+ @Mock
+ private ControlLoopEventContext context;
private CdsServerProperties cdsProps;
private VirtualControlLoopEvent onset;
private PseudoExecutor executor;
private Target target;
+ private ControlLoopOperationParams params;
+ private GrpcConfig config;
+ private CompletableFuture<OperationOutcome> cqFuture;
private GrpcOperation operation;
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ sim = Util.buildCdsSim();
+ }
+
+ @AfterClass
+ public static void tearDownAfterClass() {
+ sim.stop();
+ }
+
/**
* Sets up the fields.
*/
target = new Target();
target.setType(TargetType.VM);
target.setResourceID(RESOURCE_ID);
+
+ cqFuture = new CompletableFuture<>();
+ when(context.obtain(eq(AaiCqResponse.CONTEXT_KEY), any())).thenReturn(cqFuture);
+ when(context.getEvent()).thenReturn(onset);
+
+ params = ControlLoopOperationParams.builder().actor(CdsActorConstants.CDS_ACTOR)
+ .operation(GrpcOperation.NAME).context(context).actorService(new ActorService())
+ .targetEntity(TARGET_ENTITY).target(target).build();
}
+ /**
+ * Tests "success" case with simulator.
+ */
@Test
- public void testStartPreprocessorAsync() throws InterruptedException, ExecutionException, TimeoutException {
+ public void testSuccess() throws Exception {
+ ControlLoopEventContext context = new ControlLoopEventContext(onset);
+ loadCqData(context);
- CompletableFuture<OperationOutcome> future2 = new CompletableFuture<>();
- ControlLoopEventContext context = mock(ControlLoopEventContext.class);
- when(context.obtain(eq(AaiCqResponse.CONTEXT_KEY), any())).thenReturn(future2);
- when(context.getEvent()).thenReturn(onset);
+ Map<String, Object> payload = Map.of("artifact_name", "my_artifact", "artifact_version", "1.0");
- AtomicBoolean guardStarted = new AtomicBoolean();
+ params = ControlLoopOperationParams.builder()
+ .actor(CdsActorConstants.CDS_ACTOR).operation("subscribe").context(context)
+ .actorService(new ActorService()).targetEntity(TARGET_ENTITY).target(target).retry(0)
+ .timeoutSec(5).executor(blockingExecutor).payload(payload).build();
- ControlLoopOperationParams params = ControlLoopOperationParams.builder().actor(CdsActorConstants.CDS_ACTOR)
- .operation(GrpcOperation.NAME).context(context).actorService(new ActorService())
- .targetEntity(TARGET_ENTITY).target(target).build();
- GrpcConfig config = new GrpcConfig(executor, cdsProps);
+ cdsProps.setHost("localhost");
+ cdsProps.setPort(sim.getPort());
+ cdsProps.setTimeout(3);
+
+ GrpcConfig config = new GrpcConfig(blockingExecutor, cdsProps);
+
+ operation = new GrpcOperation(params, config) {
+ @Override
+ protected CompletableFuture<OperationOutcome> startGuardAsync() {
+ // indicate that guard completed successfully
+ return CompletableFuture.completedFuture(params.makeOutcome());
+ }
+ };
+
+ OperationOutcome outcome = operation.start().get();
+ assertEquals(PolicyResult.SUCCESS, outcome.getResult());
+ assertTrue(outcome.getResponse() instanceof ExecutionServiceOutput);
+ }
+
+ @Test
+ public void testGetPropertyNames() {
+
+ /*
+ * check VNF case
+ */
+ operation = new GrpcOperation(params, config);
+
+ // @formatter:off
+ assertThat(operation.getPropertyNames()).isEqualTo(
+ List.of(
+ OperationProperties.AAI_MODEL_INVARIANT_GENERIC_VNF,
+ OperationProperties.AAI_RESOURCE_SERVICE_INSTANCE,
+ OperationProperties.EVENT_ADDITIONAL_PARAMS));
+ // @formatter:on
+
+ /*
+ * check PNF case
+ */
+ target.setType(TargetType.PNF);
+ operation = new GrpcOperation(params, config);
+
+ // @formatter:off
+ assertThat(operation.getPropertyNames()).isEqualTo(
+ List.of(
+ OperationProperties.AAI_PNF,
+ OperationProperties.EVENT_ADDITIONAL_PARAMS));
+ // @formatter:on
+ }
+
+ @Test
+ public void testStartPreprocessorAsync() throws InterruptedException, ExecutionException, TimeoutException {
+ AtomicBoolean guardStarted = new AtomicBoolean();
operation = new GrpcOperation(params, config) {
@Override
protected CompletableFuture<OperationOutcome> startGuardAsync() {
guardStarted.set(true);
- return future2;
+ return cqFuture;
}
};
assertTrue(guardStarted.get());
verify(context).obtain(eq(AaiCqResponse.CONTEXT_KEY), any());
- future2.complete(params.makeOutcome());
+ cqFuture.complete(params.makeOutcome());
assertTrue(executor.runAll(100));
assertEquals(PolicyResult.SUCCESS, future3.get(2, TimeUnit.SECONDS).getResult());
assertTrue(future3.isDone());
*/
@Test
public void testStartPreprocessorAsyncPnf() throws InterruptedException, ExecutionException, TimeoutException {
-
- CompletableFuture<OperationOutcome> future2 = new CompletableFuture<>();
- ControlLoopEventContext context = mock(ControlLoopEventContext.class);
- when(context.obtain(eq(AaiCqResponse.CONTEXT_KEY), any())).thenReturn(future2);
- when(context.getEvent()).thenReturn(onset);
-
AtomicBoolean guardStarted = new AtomicBoolean();
target.setType(TargetType.PNF);
- ControlLoopOperationParams params = ControlLoopOperationParams.builder().actor(CdsActorConstants.CDS_ACTOR)
- .operation(GrpcOperation.NAME).context(context).actorService(new ActorService())
- .targetEntity(TARGET_ENTITY).target(target).build();
- GrpcConfig config = new GrpcConfig(executor, cdsProps);
-
operation = new GrpcOperation(params, config) {
@Override
protected CompletableFuture<OperationOutcome> startGuardAsync() {
guardStarted.set(true);
- return future2;
+ return cqFuture;
}
};
assertTrue(guardStarted.get());
verify(context).obtain(eq(AaiGetPnfOperation.getKey(TARGET_ENTITY)), any());
- future2.complete(params.makeOutcome());
+ cqFuture.complete(params.makeOutcome());
assertTrue(executor.runAll(100));
assertEquals(PolicyResult.SUCCESS, future3.get(2, TimeUnit.SECONDS).getResult());
assertTrue(future3.isDone());
}
+ /**
+ * Tests startPreprocessorAsync(), when preprocessing is disabled.
+ */
+ @Test
+ public void testStartPreprocessorAsyncDisabled() {
+ params = params.toBuilder().preprocessed(true).build();
+ assertNull(new GrpcOperation(params, config).startPreprocessorAsync());
+ }
+
@Test
public void testStartOperationAsync() throws Exception {
@Test
public void testStartOperationAsyncError() throws Exception {
-
- ControlLoopEventContext context = new ControlLoopEventContext(onset);
- ControlLoopOperationParams params = ControlLoopOperationParams.builder().actor(CdsActorConstants.CDS_ACTOR)
- .operation(GrpcOperation.NAME).context(context).actorService(new ActorService())
- .targetEntity(TARGET_ENTITY).target(target).build();
-
- GrpcConfig config = new GrpcConfig(executor, cdsProps);
operation = new GrpcOperation(params, config);
assertThatIllegalArgumentException().isThrownBy(() -> operation.startOperationAsync(1, params.makeOutcome()));
}