X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=models-interactions%2Fmodel-actors%2Factor.so%2Fsrc%2Ftest%2Fjava%2Forg%2Fonap%2Fpolicy%2Fcontrolloop%2Factor%2Fso%2FVfModuleDeleteTest.java;h=8c35e236f45acfbc8e618c0dc20b24f60283d977;hb=e3938e43b8a1f02f74368ecb75c38530285feac0;hp=9335f014130aa4e9290e4c10992d4768ddc345cc;hpb=00e48acdc51fa2a64b0f7fdc221c28308d7f214d;p=policy%2Fmodels.git diff --git a/models-interactions/model-actors/actor.so/src/test/java/org/onap/policy/controlloop/actor/so/VfModuleDeleteTest.java b/models-interactions/model-actors/actor.so/src/test/java/org/onap/policy/controlloop/actor/so/VfModuleDeleteTest.java index 9335f0141..8c35e236f 100644 --- a/models-interactions/model-actors/actor.so/src/test/java/org/onap/policy/controlloop/actor/so/VfModuleDeleteTest.java +++ b/models-interactions/model-actors/actor.so/src/test/java/org/onap/policy/controlloop/actor/so/VfModuleDeleteTest.java @@ -2,7 +2,9 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2020 Wipro Limited. + * Modifications Copyright (C) 2023 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,16 +25,14 @@ 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.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.lenient; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; import java.net.http.HttpHeaders; import java.net.http.HttpRequest; @@ -46,7 +46,6 @@ 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; @@ -55,29 +54,28 @@ import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; +import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; 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.endpoints.http.client.HttpClientFactoryInstance; import org.onap.policy.common.utils.coder.CoderException; 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.HttpPollingConfig; import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpPollingParams; -import org.onap.policy.controlloop.policy.PolicyResult; import org.onap.policy.so.SoRequest; import org.onap.policy.so.SoResponse; +@RunWith(MockitoJUnitRunner.class) 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"; @@ -108,6 +106,7 @@ public class VfModuleDeleteTest extends BasicSoOperation { /** * Sets up. */ + @Override @Before public void setUp() throws Exception { super.setUp(); @@ -117,6 +116,8 @@ public class VfModuleDeleteTest extends BasicSoOperation { configureResponse(coder.encode(response)); oper = new MyOperation(params, config); + + loadProperties(); } /** @@ -132,42 +133,11 @@ public class VfModuleDeleteTest extends BasicSoOperation { oper = new VfModuleDelete(params, config); - outcome = oper.start().get(); - assertEquals(PolicyResult.SUCCESS, outcome.getResult()); - assertTrue(outcome.getResponse() instanceof SoResponse); - } - - /** - * Tests "success" case with simulator, using properties instead of custom query data. - */ - @Test - public void testSuccessViaProperties() 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()); - - params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).preprocessed(true).build(); - params.getContext().removeProperty(AaiCqResponse.CONTEXT_KEY); - - oper = new VfModuleDelete(params, config); - - // 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); + loadProperties(); // run the operation outcome = oper.start().get(); - assertEquals(PolicyResult.SUCCESS, outcome.getResult()); + assertEquals(OperationResult.SUCCESS, outcome.getResult()); assertTrue(outcome.getResponse() instanceof SoResponse); int count = oper.getProperty(OperationProperties.DATA_VF_COUNT); @@ -178,9 +148,10 @@ public class VfModuleDeleteTest extends BasicSoOperation { 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().target(null).build(); + params = params.toBuilder().targetType(null).build(); assertThatIllegalArgumentException().isThrownBy(() -> new MyOperation(params, config)) .withMessageContaining("Target information"); } @@ -198,76 +169,8 @@ public class VfModuleDeleteTest extends BasicSoOperation { // @formatter:on } - @Test - public void testStartPreprocessorAsync() throws Exception { - // insert CQ data so it's there for the check - context.setProperty(AaiCqResponse.CONTEXT_KEY, makeCqResponse()); - - AtomicBoolean guardStarted = new AtomicBoolean(); - - oper = new MyOperation(params, config) { - @Override - protected CompletableFuture startGuardAsync() { - guardStarted.set(true); - return super.startGuardAsync(); - } - }; - - CompletableFuture future3 = oper.startPreprocessorAsync(); - assertNotNull(future3); - assertTrue(guardStarted.get()); - } - - /** - * Tests startPreprocessorAsync(), when preprocessing is disabled. - */ - @Test - public void testStartPreprocessorAsyncDisabled() { - params = params.toBuilder().preprocessed(true).build(); - assertNull(new MyOperation(params, config).startPreprocessorAsync()); - } - - @Test - public void testStartGuardAsync() throws Exception { - // remove CQ data so it's forced to query - context.removeProperty(AaiCqResponse.CONTEXT_KEY); - - CompletableFuture 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()); - } - - @Test - public void testMakeGuardPayload() { - final int origCount = 30; - oper.setVfCount(origCount); - - CompletableFuture future2 = oper.startPreprocessorAsync(); - assertTrue(executor.runAll(100)); - assertTrue(future2.isDone()); - - // get the payload from the request - ArgumentCaptor captor = ArgumentCaptor.forClass(ControlLoopOperationParams.class); - verify(guardOperator).buildOperation(captor.capture()); - - Map payload = captor.getValue().getPayload(); - assertNotNull(payload); - - Integer newCount = (Integer) payload.get(VfModuleDelete.PAYLOAD_KEY_VF_COUNT); - assertNotNull(newCount); - assertEquals(origCount - 1, newCount.intValue()); - } - @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(); @@ -278,10 +181,15 @@ public class VfModuleDeleteTest extends BasicSoOperation { } }; + loadProperties(); + + final int origCount = 30; + oper.setVfCount(origCount); + CompletableFuture 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); @@ -299,8 +207,8 @@ public class VfModuleDeleteTest extends BasicSoOperation { // indicate that the response was incomplete configureResponse(coder.encode(response).replace("COMPLETE", "incomplete")); - when(rawResponse.getStatus()).thenReturn(500, 500, 500, 200, 200); - when(client.get(any(), any(), any())).thenAnswer(provideResponse(rawResponse)); + lenient().when(rawResponse.getStatus()).thenReturn(500, 500, 500, 200, 200); + lenient().when(client.get(any(), any(), any())).thenAnswer(provideResponse(rawResponse)); // use a real executor params = params.toBuilder().executor(ForkJoinPool.commonPool()).build(); @@ -312,10 +220,12 @@ public class VfModuleDeleteTest extends BasicSoOperation { } }; + loadProperties(); + CompletableFuture future2 = oper.start(); outcome = future2.get(5, TimeUnit.SECONDS); - assertEquals(PolicyResult.SUCCESS, outcome.getResult()); + assertEquals(OperationResult.SUCCESS, outcome.getResult()); } @Test @@ -371,7 +281,7 @@ public class VfModuleDeleteTest extends BasicSoOperation { // need a new future, with an exception javaFuture = CompletableFuture.failedFuture(thrown); - when(javaClient.sendAsync(any(), any(BodyHandlers.ofString().getClass()))).thenReturn(javaFuture); + lenient().when(javaClient.sendAsync(any(), any(BodyHandlers.ofString().getClass()))).thenReturn(javaFuture); SoRequest req = new SoRequest(); req.setRequestId(REQ_ID); @@ -394,8 +304,8 @@ public class VfModuleDeleteTest extends BasicSoOperation { @Test public void testAddAuthHeader() { Builder builder = mock(Builder.class); - when(client.getUserName()).thenReturn("the-user"); - when(client.getPassword()).thenReturn("the-password"); + lenient().when(client.getUserName()).thenReturn("the-user"); + lenient().when(client.getPassword()).thenReturn("the-password"); oper.addAuthHeader(builder); ArgumentCaptor keyCaptor = ArgumentCaptor.forClass(String.class); @@ -415,12 +325,12 @@ public class VfModuleDeleteTest extends BasicSoOperation { @Test public void testAddAuthHeaderNoUser() { Builder builder = mock(Builder.class); - when(client.getPassword()).thenReturn("world"); + lenient().when(client.getPassword()).thenReturn("world"); oper.addAuthHeader(builder); verify(builder, never()).header(any(), any()); // repeat with empty username - when(client.getUserName()).thenReturn(""); + lenient().when(client.getUserName()).thenReturn(""); oper.addAuthHeader(builder); verify(builder, never()).header(any(), any()); } @@ -431,7 +341,7 @@ public class VfModuleDeleteTest extends BasicSoOperation { @Test public void testAddAuthHeaderUserOnly() { Builder builder = mock(Builder.class); - when(client.getUserName()).thenReturn("my-user"); + lenient().when(client.getUserName()).thenReturn("my-user"); oper.addAuthHeader(builder); ArgumentCaptor keyCaptor = ArgumentCaptor.forClass(String.class); @@ -445,50 +355,38 @@ public class VfModuleDeleteTest extends BasicSoOperation { assertEquals("Basic " + encoded, valueCaptor.getValue()); } + /** + * Tests makeRequest() when a property is missing. + */ @Test - public void testMakeHttpClient() { - // must use a real operation to invoke this method - assertNotNull(new MyOperation(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); + public void testMakeRequestMissingProperty() throws Exception { + loadProperties(); 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()); + oper.setProperty(OperationProperties.AAI_SERVICE, instance); - ModelVer modelVers = new ModelVer(); - when(cq.getModelVerByVersionId(any())).thenReturn(modelVers); - modelVers.setModelName(MODEL_NAME2); - modelVers.setModelVersion(MODEL_VERS2); + assertThatIllegalArgumentException().isThrownBy(() -> oper.makeRequest()) + .withMessageContaining("missing service instance ID"); + } - params.getContext().setProperty(AaiCqResponse.CONTEXT_KEY, cq); + @Test + public void testMakeHttpClient() { + // must use a real operation to invoke this method + assertNotNull(new MyOperation(params, config).makeHttpClient()); } private void initHostPort() { - when(client.getBaseUrl()).thenReturn("http://my-host:6969/"); + lenient().when(client.getBaseUrl()).thenReturn("http://my-host:6969/"); } @SuppressWarnings("unchecked") private void configureResponse(String responseText) throws CoderException { // indicate that the response was completed - when(javaResp.statusCode()).thenReturn(200); - when(javaResp.body()).thenReturn(responseText); + lenient().when(javaResp.statusCode()).thenReturn(200); + lenient().when(javaResp.body()).thenReturn(responseText); javaFuture = CompletableFuture.completedFuture(javaResp); - when(javaClient.sendAsync(any(), any(BodyHandlers.ofString().getClass()))).thenReturn(javaFuture); + lenient().when(javaClient.sendAsync(any(), any(BodyHandlers.ofString().getClass()))).thenReturn(javaFuture); } private class MyOperation extends VfModuleDelete { @@ -502,4 +400,25 @@ public class VfModuleDeleteTest extends BasicSoOperation { 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); + + CloudRegion cloudRegion = new CloudRegion(); + cloudRegion.setCloudRegionId("my-cloud-id"); + oper.setProperty(OperationProperties.AAI_DEFAULT_CLOUD_REGION, cloudRegion); + + Tenant tenant = new Tenant(); + tenant.setTenantId("my-tenant-id"); + oper.setProperty(OperationProperties.AAI_DEFAULT_TENANT, tenant); + + oper.setProperty(OperationProperties.DATA_VF_COUNT, VF_COUNT); + } }