* ONAP
* ================================================================================
* Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2020 Wipro Limited.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
package org.onap.policy.controlloop.actor.so;
+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.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import java.net.http.HttpResponse.BodyHandlers;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
+import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicBoolean;
import javax.ws.rs.client.InvocationCallback;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.apache.commons.lang3.tuple.Pair;
+import org.junit.AfterClass;
import org.junit.Before;
+import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.onap.aai.domain.yang.CloudRegion;
import org.onap.aai.domain.yang.GenericVnf;
-import org.onap.aai.domain.yang.ModelVer;
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.endpoints.http.client.HttpClientFactoryInstance;
import org.onap.policy.common.utils.coder.CoderException;
-import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
+import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
+import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
-import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
-import org.onap.policy.controlloop.policy.PolicyResult;
+import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpPollingConfig;
+import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpPollingParams;
import org.onap.policy.so.SoRequest;
import org.onap.policy.so.SoResponse;
public class VfModuleDeleteTest extends BasicSoOperation {
private static final String EXPECTED_EXCEPTION = "expected exception";
- private static final String MODEL_NAME2 = "my-model-name-B";
- private static final String MODEL_VERS2 = "my-model-version-B";
private static final String SVC_INSTANCE_ID = "my-service-instance-id";
private static final String VNF_ID = "my-vnf-id";
super(DEFAULT_ACTOR, VfModuleDelete.NAME);
}
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ initBeforeClass();
+ }
+
+ @AfterClass
+ public static void tearDownAfterClass() {
+ destroyAfterClass();
+ }
/**
* Sets up.
*/
+ @Override
@Before
public void setUp() throws Exception {
super.setUp();
configureResponse(coder.encode(response));
oper = new MyOperation(params, config);
+
+ loadProperties();
}
+ /**
+ * Tests "success" case with simulator.
+ */
@Test
- public void testConstructor() {
- assertEquals(DEFAULT_ACTOR, oper.getActorName());
- assertEquals(VfModuleDelete.NAME, oper.getName());
+ public void testSuccess() throws Exception {
+ HttpPollingParams opParams = HttpPollingParams.builder().clientName(MY_CLIENT).path("serviceInstances/v7")
+ .pollPath("orchestrationRequests/v5/").maxPolls(2).build();
+ config = new HttpPollingConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
- // verify that target validation is done
- params = params.toBuilder().target(null).build();
- assertThatIllegalArgumentException().isThrownBy(() -> new VfModuleDelete(params, config))
- .withMessageContaining("Target information");
- }
+ params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build();
- @Test
- public void testStartPreprocessorAsync() throws Exception {
- // insert CQ data so it's there for the check
- context.setProperty(AaiCqResponse.CONTEXT_KEY, makeCqResponse());
+ oper = new VfModuleDelete(params, config);
- AtomicBoolean guardStarted = new AtomicBoolean();
+ loadProperties();
- oper = new MyOperation(params, config) {
- @Override
- protected CompletableFuture<OperationOutcome> startGuardAsync() {
- guardStarted.set(true);
- return super.startGuardAsync();
- }
- };
+ // run the operation
+ outcome = oper.start().get();
+ assertEquals(OperationResult.SUCCESS, outcome.getResult());
+ assertTrue(outcome.getResponse() instanceof SoResponse);
- CompletableFuture<OperationOutcome> future3 = oper.startPreprocessorAsync();
- assertNotNull(future3);
- assertTrue(guardStarted.get());
+ int count = oper.getProperty(OperationProperties.DATA_VF_COUNT);
+ assertEquals(VF_COUNT - 1, count);
}
@Test
- public void testStartGuardAsync() throws Exception {
- // remove CQ data so it's forced to query
- context.removeProperty(AaiCqResponse.CONTEXT_KEY);
-
- CompletableFuture<OperationOutcome> future2 = oper.startPreprocessorAsync();
- assertTrue(executor.runAll(100));
- assertFalse(future2.isDone());
-
- provideCqResponse(makeCqResponse());
- assertTrue(executor.runAll(100));
- assertTrue(future2.isDone());
- assertEquals(PolicyResult.SUCCESS, future2.get().getResult());
+ public void testConstructor() {
+ assertEquals(DEFAULT_ACTOR, oper.getActorName());
+ assertEquals(VfModuleDelete.NAME, oper.getName());
+ assertTrue(oper.isUsePolling());
+
+ // verify that target validation is done
+ params = params.toBuilder().targetType(null).build();
+ assertThatIllegalArgumentException().isThrownBy(() -> new MyOperation(params, config))
+ .withMessageContaining("Target information");
}
@Test
- public void testMakeGuardPayload() {
- final int origCount = 30;
- oper.setVfCount(origCount);
-
- CompletableFuture<OperationOutcome> future2 = oper.startPreprocessorAsync();
- assertTrue(executor.runAll(100));
- assertTrue(future2.isDone());
-
- // get the payload from the request
- ArgumentCaptor<ControlLoopOperationParams> captor = ArgumentCaptor.forClass(ControlLoopOperationParams.class);
- verify(guardOperator).buildOperation(captor.capture());
-
- Map<String, Object> payload = captor.getValue().getPayload();
- assertNotNull(payload);
-
- @SuppressWarnings("unchecked")
- Map<String, Object> resource = (Map<String, Object>) payload.get("resource");
- assertNotNull(resource);
-
- @SuppressWarnings("unchecked")
- Map<String, Object> guard = (Map<String, Object>) resource.get("guard");
- assertNotNull(guard);
-
- Integer newCount = (Integer) guard.get(VfModuleDelete.PAYLOAD_KEY_VF_COUNT);
- assertNotNull(newCount);
- assertEquals(origCount - 1, newCount.intValue());
+ public void testGetPropertyNames() {
+ // @formatter:off
+ assertThat(oper.getPropertyNames()).isEqualTo(
+ List.of(
+ OperationProperties.AAI_SERVICE,
+ OperationProperties.AAI_VNF,
+ OperationProperties.AAI_DEFAULT_CLOUD_REGION,
+ OperationProperties.AAI_DEFAULT_TENANT,
+ OperationProperties.DATA_VF_COUNT));
+ // @formatter:on
}
@Test
public void testStartOperationAsync_testSuccessfulCompletion() throws Exception {
- final int origCount = 30;
- oper.setVfCount(origCount);
-
// use a real executor
params = params.toBuilder().executor(ForkJoinPool.commonPool()).build();
oper = new MyOperation(params, config) {
@Override
- public long getWaitMsGet() {
+ public long getPollWaitMs() {
return 1;
}
};
+ loadProperties();
+
+ final int origCount = 30;
+ oper.setVfCount(origCount);
+
CompletableFuture<OperationOutcome> future2 = oper.start();
outcome = future2.get(5, TimeUnit.SECONDS);
- assertEquals(PolicyResult.SUCCESS, outcome.getResult());
+ assertEquals(OperationResult.SUCCESS, outcome.getResult());
+
+ SoResponse resp = outcome.getResponse();
+ assertNotNull(resp);
+ assertEquals(REQ_ID.toString(), resp.getRequestReferences().getRequestId());
assertEquals(origCount - 1, oper.getVfCount());
}
/**
- * Tests startOperationAsync() when "get" operations are required.
+ * Tests startOperationAsync() when polling is required.
*/
@Test
- public void testStartOperationAsyncWithGets() throws Exception {
+ public void testStartOperationAsyncWithPolling() throws Exception {
// indicate that the response was incomplete
configureResponse(coder.encode(response).replace("COMPLETE", "incomplete"));
oper = new MyOperation(params, config) {
@Override
- public long getWaitMsGet() {
+ public long getPollWaitMs() {
return 1;
}
};
+ loadProperties();
+
CompletableFuture<OperationOutcome> future2 = oper.start();
outcome = future2.get(5, TimeUnit.SECONDS);
- assertEquals(PolicyResult.SUCCESS, outcome.getResult());
+ assertEquals(OperationResult.SUCCESS, outcome.getResult());
}
@Test
Map<String, Object> headers = Map.of("key-A", "value-A");
+ String reqText = oper.prettyPrint(req);
+
final CompletableFuture<Response> delFuture =
- oper.delete("my-uri", headers, MediaType.APPLICATION_JSON, req, callback);
+ oper.delete("my-uri", headers, MediaType.APPLICATION_JSON, reqText, callback);
ArgumentCaptor<HttpRequest> reqCaptor = ArgumentCaptor.forClass(HttpRequest.class);
verify(javaClient).sendAsync(reqCaptor.capture(), any());
SoRequest req = new SoRequest();
req.setRequestId(REQ_ID);
+ String reqText = oper.prettyPrint(req);
+
CompletableFuture<Response> delFuture =
- oper.delete("/my-uri", Map.of(), MediaType.APPLICATION_JSON, req, callback);
+ oper.delete("/my-uri", Map.of(), MediaType.APPLICATION_JSON, reqText, callback);
assertTrue(delFuture.isCompletedExceptionally());
assertSame(thrown, thrownCaptor.getValue().getCause());
}
- @Test
- public void testEncodeBody() {
- // try when request is already a string
- assertEquals("hello", oper.encodeRequest("hello"));
-
- // try with a real request
- SoRequest req = new SoRequest();
- req.setRequestId(REQ_ID);
- assertEquals("{\"requestId\":\"" + REQ_ID.toString() + "\"}", oper.encodeRequest(req));
-
- // coder throws an exception
- oper = new MyOperation(params, config) {
- @Override
- protected Coder makeCoder() {
- return new StandardCoder() {
- @Override
- public String encode(Object object) throws CoderException {
- throw new CoderException(EXPECTED_EXCEPTION);
- }
- };
- }
- };
-
- assertThatIllegalArgumentException().isThrownBy(() -> oper.encodeRequest(req))
- .withMessage("cannot encode request");
- }
-
/**
* Tests addAuthHeader() when there is a username, but no password.
*/
@Test
public void testMakeHttpClient() {
// must use a real operation to invoke this method
- assertNotNull(new VfModuleDelete(params, config).makeHttpClient());
- }
-
-
- @Override
- protected void makeContext() {
- super.makeContext();
-
- AaiCqResponse cq = mock(AaiCqResponse.class);
-
- GenericVnf vnf = new GenericVnf();
- when(cq.getGenericVnfByVfModuleModelInvariantId(MODEL_INVAR_ID)).thenReturn(vnf);
- vnf.setVnfId(VNF_ID);
-
- ServiceInstance instance = new ServiceInstance();
- when(cq.getServiceInstance()).thenReturn(instance);
- instance.setServiceInstanceId(SVC_INSTANCE_ID);
-
- when(cq.getDefaultTenant()).thenReturn(new Tenant());
- when(cq.getDefaultCloudRegion()).thenReturn(new CloudRegion());
-
- ModelVer modelVers = new ModelVer();
- when(cq.getModelVerByVersionId(any())).thenReturn(modelVers);
- modelVers.setModelName(MODEL_NAME2);
- modelVers.setModelVersion(MODEL_VERS2);
-
- params.getContext().setProperty(AaiCqResponse.CONTEXT_KEY, cq);
+ assertNotNull(new MyOperation(params, config).makeHttpClient());
}
private void initHostPort() {
private class MyOperation extends VfModuleDelete {
- public MyOperation(ControlLoopOperationParams params, HttpConfig config) {
+ public MyOperation(ControlLoopOperationParams params, HttpPollingConfig config) {
super(params, config);
}
return javaClient;
}
}
+
+ private void loadProperties() {
+ // set the properties
+ ServiceInstance instance = new ServiceInstance();
+ instance.setServiceInstanceId(SVC_INSTANCE_ID);
+ oper.setProperty(OperationProperties.AAI_SERVICE, instance);
+
+ GenericVnf vnf = new GenericVnf();
+ vnf.setVnfId(VNF_ID);
+ oper.setProperty(OperationProperties.AAI_VNF, vnf);
+
+ oper.setProperty(OperationProperties.AAI_DEFAULT_CLOUD_REGION, new CloudRegion());
+ oper.setProperty(OperationProperties.AAI_DEFAULT_TENANT, new Tenant());
+
+ oper.setProperty(OperationProperties.DATA_VF_COUNT, VF_COUNT);
+ }
}