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%2FSoOperationTest.java;h=a2d3925103edf26b5c17bd998523b73e9047981b;hb=e3938e43b8a1f02f74368ecb75c38530285feac0;hp=e9a49545e176d04030d4dc441283afd97e669b6d;hpb=64591b51d4da3900f37f53701b2ffef4a38778e9;p=policy%2Fmodels.git diff --git a/models-interactions/model-actors/actor.so/src/test/java/org/onap/policy/controlloop/actor/so/SoOperationTest.java b/models-interactions/model-actors/actor.so/src/test/java/org/onap/policy/controlloop/actor/so/SoOperationTest.java index e9a49545e..a2d392510 100644 --- a/models-interactions/model-actors/actor.so/src/test/java/org/onap/policy/controlloop/actor/so/SoOperationTest.java +++ b/models-interactions/model-actors/actor.so/src/test/java/org/onap/policy/controlloop/actor/so/SoOperationTest.java @@ -2,8 +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. @@ -24,35 +25,26 @@ package org.onap.policy.controlloop.actor.so; 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.junit.Assert.assertTrue; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.lenient; 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.function.BiConsumer; -import java.util.function.Supplier; import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +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.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.controlloop.actorserviceprovider.OperationProperties; import org.onap.policy.controlloop.actorserviceprovider.OperationResult; import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams; import org.onap.policy.so.SoModelInfo; @@ -61,15 +53,11 @@ import org.onap.policy.so.SoRequestInfo; import org.onap.policy.so.SoRequestStatus; import org.onap.policy.so.SoResponse; +@RunWith(MockitoJUnitRunner.class) 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 static final List PROP_NAMES = Collections.emptyList(); - private static final String VERSION_ID = "1.2.3"; - private SoOperation oper; /** @@ -119,77 +107,6 @@ public class SoOperationTest extends BasicSoOperation { targetEntities.put(expectedText, originalValue); } - @Test - public void testStartPreprocessorAsync() { - assertNotNull(oper.startPreprocessorAsync()); - } - - @Test - public void testObtainVfCount_testGetVfCount_testSetVfCount() throws Exception { - // insert CQ data so it's there for the check - context.setProperty(AaiCqResponse.CONTEXT_KEY, makeCqResponse()); - - // 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 the VF Count methods when properties are being used. - * @throws Exception if an error occurs - */ - @Test - public void testGetVfCount_testSetVfCount_ViaProperties() throws Exception { - oper.setProperty(OperationProperties.DATA_VF_COUNT, VF_COUNT); - - // verify that the count was stored - assertEquals(VF_COUNT.intValue(), oper.getVfCount()); - - oper.setVfCount(VF_COUNT + 1); - - int count = oper.getProperty(OperationProperties.DATA_VF_COUNT); - assertEquals(VF_COUNT + 1, count); - assertEquals(VF_COUNT + 1, oper.getVfCount()); - } - - /** - * Tests obtainVfCount() when it actually has to query. - */ - @Test - public void testObtainVfCountQuery() throws Exception { - CompletableFuture future2 = oper.obtainVfCount(); - assertNotNull(future2); - assertTrue(executor.runAll(100)); - - // not done yet - assertFalse(future2.isDone()); - - provideCqResponse(makeCqResponse()); - - assertTrue(executor.runAll(100)); - assertTrue(future2.isDone()); - assertEquals(OperationResult.SUCCESS, future2.get().getResult()); - - // verify that the count was stored - 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 testGetRequestState() { SoResponse resp = new SoResponse(); @@ -213,14 +130,14 @@ public class SoOperationTest extends BasicSoOperation { assertTrue(oper.isSuccess(rawResponse, response)); - when(rawResponse.getStatus()).thenReturn(500); + lenient().when(rawResponse.getStatus()).thenReturn(500); assertTrue(oper.isSuccess(rawResponse, response)); } @Test public void testSetOutcome() { // success case - when(rawResponse.getStatus()).thenReturn(200); + lenient().when(rawResponse.getStatus()).thenReturn(200); assertSame(outcome, oper.setOutcome(outcome, OperationResult.SUCCESS, rawResponse, response)); assertEquals(OperationResult.SUCCESS, outcome.getResult()); @@ -228,7 +145,7 @@ public class SoOperationTest extends BasicSoOperation { assertSame(response, outcome.getResponse()); // failure case - when(rawResponse.getStatus()).thenReturn(500); + lenient().when(rawResponse.getStatus()).thenReturn(500); assertSame(outcome, oper.setOutcome(outcome, OperationResult.FAILURE, rawResponse, response)); assertEquals(OperationResult.FAILURE, outcome.getResult()); @@ -295,114 +212,35 @@ public class SoOperationTest extends BasicSoOperation { } @Test - public void testGetItem() { - AaiCqResponse cq = mock(AaiCqResponse.class); - params.getContext().setProperty(AaiCqResponse.CONTEXT_KEY, cq); - - // in neither property nor custom query - assertThatIllegalArgumentException().isThrownBy(() -> oper.getItem("propA", cq2 -> null, "not found")) - .withMessage("not found"); - - // only in custom query - assertEquals("valueB", oper.getItem("propB", cq2 -> "valueB", "failureB")); - - // both - should choose the property - oper.setProperty("propC", "valueC"); - assertEquals("valueC", oper.getItem("propC", cq2 -> "valueC2", "failureC")); - - // both - should choose the property, even if it's null - oper.setProperty("propD", null); - assertNull(oper.getItem("propD", cq2 -> "valueD2", "failureD")); - } - - @Test - public void testGetVnfItem() { - // @formatter:off - verifyItems(OperationProperties.AAI_VNF, GenericVnf::new, - (cq, instance) -> when(cq.getGenericVnfByVfModuleModelInvariantId(MODEL_INVAR_ID)).thenReturn(instance), - () -> oper.getVnfItem(oper.prepareSoModelInfo()), - "missing generic VNF"); - // @formatter:on + public void testConstructCloudConfiguration() throws Exception { + Tenant tenantItem = new Tenant(); + tenantItem.setTenantId("my-tenant-id"); + + CloudRegion cloudRegionItem = new CloudRegion(); + cloudRegionItem.setCloudRegionId("my-cloud-id"); + + assertThatCode(() -> oper.constructCloudConfiguration(tenantItem, cloudRegionItem)).doesNotThrowAnyException(); + + tenantItem.setTenantId(null); + assertThatIllegalArgumentException() + .isThrownBy(() -> oper.constructCloudConfiguration(tenantItem, cloudRegionItem)) + .withMessageContaining("missing tenant ID"); + tenantItem.setTenantId("my-tenant-id"); + + cloudRegionItem.setCloudRegionId(null); + assertThatIllegalArgumentException() + .isThrownBy(() -> oper.constructCloudConfiguration(tenantItem, cloudRegionItem)) + .withMessageContaining("missing cloud region ID"); + cloudRegionItem.setCloudRegionId("my-cloud-id"); } @Test - public void testGetServiceInstance() { - // @formatter:off - verifyItems(OperationProperties.AAI_SERVICE, ServiceInstance::new, - (cq, instance) -> when(cq.getServiceInstance()).thenReturn(instance), - () -> oper.getServiceInstance(), - "missing VNF Service Item"); - // @formatter:on - } - - @Test - public void testGetDefaultTenant() { - // @formatter:off - verifyItems(OperationProperties.AAI_DEFAULT_TENANT, Tenant::new, - (cq, tenant) -> when(cq.getDefaultTenant()).thenReturn(tenant), - () -> oper.getDefaultTenant(), - "missing Default Tenant Item"); - // @formatter:on - } - - @Test - public void testGetVnfModel() { - GenericVnf vnf = new GenericVnf(); - vnf.setModelVersionId(VERSION_ID); - - // @formatter:off - verifyItems(OperationProperties.AAI_VNF_MODEL, ModelVer::new, - (cq, model) -> when(cq.getModelVerByVersionId(VERSION_ID)).thenReturn(model), - () -> oper.getVnfModel(vnf), - "missing generic VNF Model"); - // @formatter:on - } - - @Test - public void testGetServiceModel() { - ServiceInstance service = new ServiceInstance(); - service.setModelVersionId(VERSION_ID); - - // @formatter:off - verifyItems(OperationProperties.AAI_SERVICE_MODEL, ModelVer::new, - (cq, model) -> when(cq.getModelVerByVersionId(VERSION_ID)).thenReturn(model), - () -> oper.getServiceModel(service), - "missing Service Model"); - // @formatter:on - } - - @Test - public void testGetDefaultCloudRegion() { - // @formatter:off - verifyItems(OperationProperties.AAI_DEFAULT_CLOUD_REGION, CloudRegion::new, - (cq, region) -> when(cq.getDefaultCloudRegion()).thenReturn(region), - () -> oper.getDefaultCloudRegion(), - "missing Default Cloud Region"); - // @formatter:on - } - - private void verifyItems(String propName, Supplier maker, BiConsumer setter, - Supplier getter, String errmsg) { - - AaiCqResponse cq = mock(AaiCqResponse.class); - params.getContext().setProperty(AaiCqResponse.CONTEXT_KEY, cq); - - // in neither property nor custom query - assertThatIllegalArgumentException().isThrownBy(getter::get).withMessage(errmsg); - - // only in custom query - final T item1 = maker.get(); - setter.accept(cq, item1); - assertSame(item1, getter.get()); + public void testGetRequiredText() throws Exception { - // both - should choose the property - final T item2 = maker.get(); - oper.setProperty(propName, item2); - assertSame(item2, getter.get()); + assertThatCode(() -> oper.getRequiredText("some value", "my value")).doesNotThrowAnyException(); - // both - should choose the property, even if it's null - oper.setProperty(propName, null); - assertNull(getter.get()); + assertThatIllegalArgumentException().isThrownBy(() -> oper.getRequiredText("some value", null)) + .withMessageContaining("missing some value"); } @Test