diff options
Diffstat (limited to 'main/src/test')
9 files changed, 1054 insertions, 687 deletions
diff --git a/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyProvider.java b/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyProvider.java deleted file mode 100644 index ad394e33..00000000 --- a/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyProvider.java +++ /dev/null @@ -1,488 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP Policy API - * ================================================================================ - * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019-2021 Nordix Foundation. - * Modifications Copyright (C) 2020,2022 Bell Canada. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.api.main.rest.provider; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatCode; -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.fail; - -import java.util.ArrayList; -import java.util.List; -import org.junit.Ignore; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.onap.policy.api.main.PolicyApiApplication; -import org.onap.policy.common.utils.coder.CoderException; -import org.onap.policy.common.utils.coder.StandardCoder; -import org.onap.policy.common.utils.coder.StandardYamlCoder; -import org.onap.policy.common.utils.resources.ResourceUtils; -import org.onap.policy.models.base.PfModelException; -import org.onap.policy.models.pdp.concepts.Pdp; -import org.onap.policy.models.pdp.concepts.PdpGroup; -import org.onap.policy.models.pdp.concepts.PdpGroupFilter; -import org.onap.policy.models.pdp.concepts.PdpSubGroup; -import org.onap.policy.models.pdp.enums.PdpHealthStatus; -import org.onap.policy.models.pdp.enums.PdpState; -import org.onap.policy.models.provider.PolicyModelsProvider; -import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; -import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.annotation.DirtiesContext; -import org.springframework.test.annotation.DirtiesContext.ClassMode; -import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.junit4.SpringRunner; - -/** - * This class performs unit test of {@link PolicyProvider}. - * - * @author Chenfei Gao (cgao@research.att.com) - */ -// Provider classes will be obsolete upon migration to Hibernate -@Ignore -@RunWith(SpringRunner.class) -@SpringBootTest(classes = PolicyApiApplication.class, properties = {"database.initialize=false"}) -@ActiveProfiles("test") -@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) -public class TestPolicyProvider { - - private static StandardCoder standardCoder = new StandardCoder(); - private static StandardYamlCoder standardYamlCoder = new StandardYamlCoder(); - - private static final String POLICY_RESOURCE = "policies/vCPE.policy.monitoring.input.tosca.json"; - private static final String POLICY_TYPE_RESOURCE = "policytypes/onap.policies.monitoring.tcagen2.yaml"; - private static final String POLICY_RESOURCE_WITH_BAD_POLICYTYPE_ID = "policies/vCPE.policy.bad.policytypeid.json"; - private static final String POLICY_RESOURCE_WITH_BAD_POLICYTYPE_VERSION = - "policies/vCPE.policy.bad.policytypeversion.json"; - private static final String POLICY_RESOURCE_WITH_NO_POLICY_VERSION = "policies/vCPE.policy.no.policyversion.json"; - private static final String POLICY_RESOURCE_WITH_DIFFERENT_FIELDS = - "policies/vCPE.policy.different.policy.fields.json"; - private static final String MULTIPLE_POLICIES_RESOURCE = "policies/vCPE.policies.optimization.input.tosca.json"; - - private static final String POLICY_TYPE_RESOURCE_OPERATIONAL_COMMON = - "policytypes/onap.policies.controlloop.operational.Common.yaml"; - private static final String POLICY_TYPE_RESOURCE_OPERATIONAL_DROOLS = - "policytypes/onap.policies.controlloop.operational.common.Drools.yaml"; - private static final String POLICY_RESOURCE_OPERATIONAL = "policies/vCPE.policy.operational.input.tosca.json"; - private static final String POLICY_TYPE_OPERATIONAL_DROOLS = "onap.policies.controlloop.operational.common.Drools"; - - @Autowired - private PolicyProvider policyProvider; - @Autowired - private PolicyTypeProvider policyTypeProvider; - @Autowired - private PolicyModelsProvider databaseProvider; - - @Test - public void testFetchPolicies() { - - assertThatThrownBy(() -> { - policyProvider.fetchPolicies("dummy", "1.0.0", null, null, null); - }).hasMessage("service template not found in database"); - - assertThatThrownBy(() -> { - policyProvider.fetchPolicies("dummy", "1.0.0", "dummy", null, null); - }).hasMessage("service template not found in database"); - - assertThatThrownBy(() -> { - policyProvider.fetchPolicies("dummy", "1.0.0", "dummy", "1.0.0", null); - }).hasMessage("service template not found in database"); - - assertThatThrownBy(() -> { - policyProvider.fetchPolicies(null, null, "dummy", "1.0.0", null); - }).hasMessage("service template not found in database"); - } - - @Test - public void testFetchLatestPolicies() { - - assertThatThrownBy(() -> { - policyProvider.fetchLatestPolicies("dummy", "dummy", "dummy", null); - }).hasMessage("service template not found in database"); - } - - @Test - public void testFetchDeployedPolicies() { - String policyId = "onap.restart.tca"; - String policyVersion = "1.0.0"; - String policyTypeVersion = "1.0.0"; - String policyTypeId = "onap.policies.monitoring.cdap.tca.hi.lo.app"; - - try { - assertEquals(0, databaseProvider.getPdpGroups("name").size()); - assertEquals(0, databaseProvider.getFilteredPdpGroups(PdpGroupFilter.builder().build()).size()); - - assertNotNull(databaseProvider.createPdpGroups(new ArrayList<>())); - assertNotNull(databaseProvider.updatePdpGroups(new ArrayList<>())); - - PdpGroup pdpGroup = new PdpGroup(); - pdpGroup.setName("group"); - pdpGroup.setVersion("1.2.3"); - pdpGroup.setPdpGroupState(PdpState.ACTIVE); - pdpGroup.setPdpSubgroups(new ArrayList<>()); - List<PdpGroup> groupList = new ArrayList<>(); - groupList.add(pdpGroup); - - PdpSubGroup pdpSubGroup = new PdpSubGroup(); - pdpSubGroup.setPdpType("type"); - pdpSubGroup.setDesiredInstanceCount(123); - pdpSubGroup.setSupportedPolicyTypes(new ArrayList<>()); - pdpSubGroup.getSupportedPolicyTypes().add(new ToscaConceptIdentifier(policyTypeId, policyTypeVersion)); - pdpGroup.getPdpSubgroups().add(pdpSubGroup); - - Pdp pdp = new Pdp(); - pdp.setInstanceId("type-0"); - pdp.setMessage("Hello"); - pdp.setPdpState(PdpState.ACTIVE); - pdp.setHealthy(PdpHealthStatus.UNKNOWN); - pdpSubGroup.setPdpInstances(new ArrayList<>()); - pdpSubGroup.getPdpInstances().add(pdp); - - // Create Pdp Groups - assertEquals(123, databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0) - .getDesiredInstanceCount()); - assertEquals(1, databaseProvider.getPdpGroups("group").size()); - - // Create Policy Type - assertThatCode(() -> { - ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder - .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class); - policyTypeProvider.createPolicyType(policyTypeServiceTemplate); - }).doesNotThrowAnyException(); - - // Create Policy - assertThatCode(() -> { - String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE); - ToscaServiceTemplate policyServiceTemplate = - standardCoder.decode(policyString, ToscaServiceTemplate.class); - ToscaServiceTemplate serviceTemplate = - policyProvider.createPolicy(policyTypeId, policyTypeVersion, policyServiceTemplate); - assertFalse(serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).isEmpty()); - }).doesNotThrowAnyException(); - - // Update pdpSubGroup - pdpSubGroup.setPolicies(new ArrayList<>()); - pdpSubGroup.getPolicies().add(new ToscaConceptIdentifier(policyId, policyVersion)); - assertEquals(1, - databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0).getPolicies().size()); - - // Test validateDeleteEligibility exception path(!pdpGroups.isEmpty()) - assertThatThrownBy(() -> { - policyProvider.deletePolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", "onap.restart.tca", - "1.0.0"); - }).hasMessageContaining("policy is in use, it is deployed in PDP group group subgroup type"); - } catch (Exception exc) { - fail("Test should not throw an exception"); - } - } - - @Test - public void testCreatePolicy() throws Exception { - - assertThatThrownBy(() -> { - policyProvider.createPolicy("dummy", "1.0.0", new ToscaServiceTemplate()); - }).hasMessage("topology template not specified on service template"); - - ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder - .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class); - policyTypeProvider.createPolicyType(policyTypeServiceTemplate); - - assertThatCode(() -> policyTypeProvider.createPolicyType(policyTypeServiceTemplate)).doesNotThrowAnyException(); - - assertThatThrownBy(() -> { - String badPolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_WITH_BAD_POLICYTYPE_ID); - ToscaServiceTemplate badPolicyServiceTemplate = - standardCoder.decode(badPolicyString, ToscaServiceTemplate.class); - policyProvider.createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", - badPolicyServiceTemplate); - }).hasMessage( - "Version not specified, the version of this TOSCA entity must be specified in " - + "the type_version field"); - - assertThatThrownBy(() -> { - String badPolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_WITH_BAD_POLICYTYPE_VERSION); - ToscaServiceTemplate badPolicyServiceTemplate = - standardCoder.decode(badPolicyString, ToscaServiceTemplate.class); - policyProvider.createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", - badPolicyServiceTemplate); - }).hasMessageContaining( - "item \"policy type\" value \"onap.policies.monitoring.cdap.tca.hi.lo.app:2.0.0\" INVALID, not found"); - - assertThatThrownBy(() -> { - String badPolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_WITH_NO_POLICY_VERSION); - ToscaServiceTemplate badPolicyServiceTemplate = - standardCoder.decode(badPolicyString, ToscaServiceTemplate.class); - policyProvider.createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", - badPolicyServiceTemplate); - }).hasMessageContaining("item \"version\" value \"0.0.0\" INVALID, is null"); - - String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE); - ToscaServiceTemplate policyServiceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class); - ToscaServiceTemplate serviceTemplate = policyProvider - .createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate); - assertFalse(serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).isEmpty()); - - assertThatThrownBy(() -> { - String badPolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_WITH_DIFFERENT_FIELDS); - ToscaServiceTemplate badPolicyServiceTemplate = - standardCoder.decode(badPolicyString, ToscaServiceTemplate.class); - policyProvider.createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", - badPolicyServiceTemplate); - }).hasMessageContaining( - "item \"entity\" value \"onap.restart.tca:1.0.0\" INVALID, " + "does not equal existing entity"); - } - - @Test - public void testCreateOperationalDroolsPolicy() throws CoderException, PfModelException { - ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder.decode( - ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_OPERATIONAL_COMMON), ToscaServiceTemplate.class); - - policyTypeProvider.createPolicyType(policyTypeServiceTemplate); - - policyTypeServiceTemplate = standardYamlCoder.decode( - ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_OPERATIONAL_DROOLS), ToscaServiceTemplate.class); - policyTypeProvider.createPolicyType(policyTypeServiceTemplate); - - String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_OPERATIONAL); - ToscaServiceTemplate policyServiceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class); - ToscaServiceTemplate serviceTemplate = - policyProvider.createPolicy(POLICY_TYPE_OPERATIONAL_DROOLS, "1.0.0", policyServiceTemplate); - assertFalse(serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).isEmpty()); - } - - @Test - public void testSimpleCreatePolicy() throws Exception { - - assertThatThrownBy(() -> { - String multiPoliciesString = ResourceUtils.getResourceAsString(MULTIPLE_POLICIES_RESOURCE); - ToscaServiceTemplate multiPoliciesServiceTemplate = - standardCoder.decode(multiPoliciesString, ToscaServiceTemplate.class); - policyProvider.createPolicies(multiPoliciesServiceTemplate); - }).hasMessageContaining( - "no policy types are defined on the service template for the policies in the topology template"); - - // Create required policy types - ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder.decode( - ResourceUtils.getResourceAsString("policytypes/onap.policies.Optimization.yaml"), - ToscaServiceTemplate.class); - policyTypeProvider.createPolicyType(policyTypeServiceTemplate); - - policyTypeServiceTemplate = standardYamlCoder.decode( - ResourceUtils.getResourceAsString("policytypes/onap.policies.optimization.Resource.yaml"), - ToscaServiceTemplate.class); - policyTypeProvider.createPolicyType(policyTypeServiceTemplate); - - policyTypeServiceTemplate = standardYamlCoder.decode( - ResourceUtils - .getResourceAsString("policytypes/onap.policies.optimization.resource.AffinityPolicy.yaml"), - ToscaServiceTemplate.class); - policyTypeProvider.createPolicyType(policyTypeServiceTemplate); - - policyTypeServiceTemplate = standardYamlCoder.decode( - ResourceUtils - .getResourceAsString("policytypes/onap.policies.optimization.resource.DistancePolicy.yaml"), - ToscaServiceTemplate.class); - policyTypeProvider.createPolicyType(policyTypeServiceTemplate); - - policyTypeServiceTemplate = standardYamlCoder.decode( - ResourceUtils.getResourceAsString("policytypes/onap.policies.optimization.resource.Vim_fit.yaml"), - ToscaServiceTemplate.class); - policyTypeProvider.createPolicyType(policyTypeServiceTemplate); - - policyTypeServiceTemplate = standardYamlCoder.decode( - ResourceUtils.getResourceAsString("policytypes/onap.policies.optimization.resource.HpaPolicy.yaml"), - ToscaServiceTemplate.class); - policyTypeProvider.createPolicyType(policyTypeServiceTemplate); - - policyTypeServiceTemplate = standardYamlCoder.decode( - ResourceUtils.getResourceAsString("policytypes/onap.policies.optimization.resource.VnfPolicy.yaml"), - ToscaServiceTemplate.class); - policyTypeProvider.createPolicyType(policyTypeServiceTemplate); - - policyTypeServiceTemplate = standardYamlCoder.decode( - ResourceUtils.getResourceAsString("policytypes/onap.policies.optimization.Service.yaml"), - ToscaServiceTemplate.class); - policyTypeProvider.createPolicyType(policyTypeServiceTemplate); - - policyTypeServiceTemplate = standardYamlCoder.decode( - ResourceUtils - .getResourceAsString("policytypes/onap.policies.optimization.service.SubscriberPolicy.yaml"), - ToscaServiceTemplate.class); - policyTypeProvider.createPolicyType(policyTypeServiceTemplate); - - policyTypeServiceTemplate = standardYamlCoder.decode( - ResourceUtils.getResourceAsString("policytypes/onap.policies.optimization.service.QueryPolicy.yaml"), - ToscaServiceTemplate.class); - policyTypeProvider.createPolicyType(policyTypeServiceTemplate); - - policyTypeServiceTemplate = standardYamlCoder.decode( - ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.tcagen2.yaml"), - ToscaServiceTemplate.class); - policyTypeProvider.createPolicyType(policyTypeServiceTemplate); - - // Create multiple policies in one call - String multiPoliciesString = ResourceUtils.getResourceAsString(MULTIPLE_POLICIES_RESOURCE); - ToscaServiceTemplate multiPoliciesServiceTemplate = - standardCoder.decode(multiPoliciesString, ToscaServiceTemplate.class); - - assertThatCode(() -> { - policyProvider.createPolicies(multiPoliciesServiceTemplate); - policyProvider.createPolicies(multiPoliciesServiceTemplate); - }).doesNotThrowAnyException(); - } - - @Test - public void testDeletePolicy() { - - assertThatThrownBy(() -> { - policyProvider.deletePolicy("dummy", "1.0.0", "dummy", "1.0.0"); - }).hasMessage("service template not found in database"); - - assertThatCode(() -> { - ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder - .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class); - policyTypeProvider.createPolicyType(policyTypeServiceTemplate); - }).doesNotThrowAnyException(); - - assertThatCode(() -> { - String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE); - ToscaServiceTemplate policyServiceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class); - ToscaServiceTemplate serviceTemplate = policyProvider - .createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate); - assertFalse(serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).isEmpty()); - }).doesNotThrowAnyException(); - - assertThatCode(() -> { - ToscaServiceTemplate serviceTemplate = policyProvider - .deletePolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", "onap.restart.tca", "1.0.0"); - assertFalse(serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).isEmpty()); - }).doesNotThrowAnyException(); - - assertThatThrownBy(() -> { - policyProvider.deletePolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", "onap.restart.tca", - "1.0.0"); - }).hasMessageContaining("no policies found"); - } - - @Test - public void testFetchAllPolicies() throws Exception { - // Create Policy Type - ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder - .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class); - policyTypeProvider.createPolicyType(policyTypeServiceTemplate); - - // Create Policy - String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE); - ToscaServiceTemplate policyServiceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class); - ToscaServiceTemplate serviceTemplate = policyProvider - .createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate); - - assertThat(serviceTemplate.getToscaTopologyTemplate().getPolicies()).hasSize(1); - - // Test fetch all policies - policyTypeServiceTemplate = policyProvider.fetchPolicies(null, null, null, null, null); - - assertThat(policyTypeServiceTemplate.getToscaTopologyTemplate().getPolicies()).hasSize(1); - } - - @Test - public void testFetchSpecificPolicy_availablePolicy() throws Exception { - // Create Policy Type - ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder - .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class); - policyTypeProvider.createPolicyType(policyTypeServiceTemplate); - - // Create Policy - String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE); - ToscaServiceTemplate policyServiceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class); - ToscaServiceTemplate serviceTemplate = policyProvider - .createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate); - - assertThat(serviceTemplate.getToscaTopologyTemplate().getPolicies()).hasSize(1); - - // Test fetch specific policy - assertThat(policyProvider.fetchPolicies(null, null, "onap.restart.tca", "1.0.0", null) - .getToscaTopologyTemplate().getPolicies()).hasSize(1); - } - - @Test - public void testFetchSpecificPolicy_unavailablePolicy() throws Exception { - // Create Policy Type - ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder - .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class); - policyTypeProvider.createPolicyType(policyTypeServiceTemplate); - - // Create Policy - String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE); - ToscaServiceTemplate policyServiceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class); - ToscaServiceTemplate serviceTemplate = policyProvider - .createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate); - assertNotNull(serviceTemplate.getToscaTopologyTemplate().getPolicies()); - assertThat(serviceTemplate.getToscaTopologyTemplate().getPolicies()).hasSize(1); - - // Test fetch specific policy - assertThatThrownBy(() -> policyProvider.fetchPolicies(null, null, "onap.restart.tca", "2.0.0", null)) - .hasMessageContaining("policies for onap.restart.tca:2.0.0 do not exist"); - } - - @Test - public void testDeleteSpecificPolicy_availablePolicy() throws Exception { - ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder - .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class); - policyTypeProvider.createPolicyType(policyTypeServiceTemplate); - - String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE); - ToscaServiceTemplate policyServiceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class); - ToscaServiceTemplate serviceTemplate = policyProvider - .createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate); - assertThat(serviceTemplate.getToscaTopologyTemplate().getPolicies()).hasSize(1); - - ToscaServiceTemplate svcTemplate = policyProvider.deletePolicy(null, null, "onap.restart.tca", "1.0.0"); - assertThat(svcTemplate.getToscaTopologyTemplate().getPolicies()).hasSize(1); - } - - @Test - public void testDeleteSpecificPolicy_unavailablePolicy() throws Exception { - ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder - .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class); - policyTypeProvider.createPolicyType(policyTypeServiceTemplate); - - String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE); - ToscaServiceTemplate policyServiceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class); - ToscaServiceTemplate serviceTemplate = policyProvider - .createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate); - assertThat(serviceTemplate.getToscaTopologyTemplate().getPolicies()).hasSize(1); - - assertThatThrownBy(() -> policyProvider.deletePolicy(null, null, "onap.restart.tca", "2.0.0")) - .hasMessageContaining("not found"); - - assertThatThrownBy(() -> policyProvider.deletePolicy(null, null, "onap.restart.tca.unavailable", "1.0.0")) - .hasMessageContaining("not found"); - } -}
\ No newline at end of file diff --git a/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyTypeProvider.java b/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyTypeProvider.java deleted file mode 100644 index 6b7630c7..00000000 --- a/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyTypeProvider.java +++ /dev/null @@ -1,199 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP Policy API - * ================================================================================ - * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019-2021 Nordix Foundation. - * Modifications Copyright (C) 2020-2022 Bell Canada. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.api.main.rest.provider; - -import static org.assertj.core.api.Assertions.assertThatCode; -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; - -import org.junit.Ignore; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.onap.policy.api.main.PolicyApiApplication; -import org.onap.policy.common.utils.coder.CoderException; -import org.onap.policy.common.utils.coder.StandardYamlCoder; -import org.onap.policy.common.utils.resources.ResourceUtils; -import org.onap.policy.models.base.PfModelException; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType; -import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.annotation.DirtiesContext; -import org.springframework.test.annotation.DirtiesContext.ClassMode; -import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.junit4.SpringRunner; - -/** - * This class performs unit test of {@link PolicyTypeProvider}. - * - * @author Chenfei Gao (cgao@research.att.com) - */ -// Provider classes will be obsolete upon migration to Hibernate -@Ignore -@RunWith(SpringRunner.class) -@SpringBootTest(classes = PolicyApiApplication.class, properties = {"database.initialize=false"}) -@ActiveProfiles("test") -@DirtiesContext(classMode = ClassMode.AFTER_CLASS) -public class TestPolicyTypeProvider { - - private static StandardYamlCoder standardYamlCoder = new StandardYamlCoder(); - - @Autowired - private PolicyProvider policyProvider; - - @Autowired - private PolicyTypeProvider policyTypeProvider; - - private static final String POLICY_TYPE_VERSION = "1.0.0"; - - private static final String POLICY_RESOURCE_MONITORING = "policies/vCPE.policy.monitoring.input.tosca.yaml"; - private static final String POLICY_TYPE_RESOURCE_MONITORING = "policytypes/onap.policies.monitoring.tcagen2.yaml"; - private static final String POLICY_TYPE_RESOURCE_WITH_NO_VERSION = - "policytypes/onap.policies.optimization.Resource.no.version.yaml"; - private static final String POLICY_TYPE_NAME_MONITORING = "onap.policies.monitoring.tcagen2"; - - private static final String POLICY_TYPE_RESOURCE_OPERATIONAL_COMMON = - "policytypes/onap.policies.controlloop.operational.Common.yaml"; - private static final String POLICY_TYPE_RESOURCE_OPERATIONAL_DROOLS = - "policytypes/onap.policies.controlloop.operational.common.Drools.yaml"; - private static final String POLICY_TYPE_RESOURCE_OPERATIONAL_APEX = - "policytypes/onap.policies.controlloop.operational.common.Apex.yaml"; - private static final String POLICY_TYPE_OPERATIONAL_COMMON = "onap.policies.controlloop.operational.Common"; - private static final String POLICY_TYPE_OPERATIONAL_APEX = "onap.policies.controlloop.operational.common.Apex"; - private static final String POLICY_TYPE_OPERATIONAL_DROOLS = "onap.policies.controlloop.operational.common.Drools"; - - @Test - public void testFetchPolicyTypes() throws Exception { - - ToscaServiceTemplate serviceTemplate = policyTypeProvider.fetchPolicyTypes(null, null); - assertFalse(serviceTemplate.getPolicyTypes().isEmpty()); - - assertThatThrownBy(() -> { - policyTypeProvider.fetchPolicyTypes("dummy", null); - }).hasMessage("policy types for filter ToscaEntityFilter(name=dummy, version=null) do not exist"); - - assertThatThrownBy(() -> { - policyTypeProvider.fetchPolicyTypes("dummy", "dummy"); - }).hasMessage("policy types for filter ToscaEntityFilter(name=dummy, version=dummy) do not exist"); - } - - @Test - public void testFetchLatestPolicyTypes() { - - assertThatThrownBy(() -> { - policyTypeProvider.fetchLatestPolicyTypes("dummy"); - }).hasMessage("policy types for filter ToscaEntityFilter(name=dummy, version=LATEST) do not exist"); - } - - @Test - public void testCreatePolicyType() throws Exception { - - ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder - .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_MONITORING), ToscaServiceTemplate.class); - ToscaServiceTemplate serviceTemplate = policyTypeProvider.createPolicyType(policyTypeServiceTemplate); - assertFalse(serviceTemplate.getPolicyTypes().isEmpty()); - - assertThatCode(() -> { - policyTypeProvider.createPolicyType(policyTypeServiceTemplate); - }).doesNotThrowAnyException(); - - ToscaPolicyType policyType = policyTypeServiceTemplate.getPolicyTypes().get("onap.policies.monitoring.tcagen2"); - policyType.setDescription("Some other description"); - - assertThatThrownBy(() -> { - policyTypeProvider.createPolicyType(policyTypeServiceTemplate); - }).hasMessageContaining("item \"entity\" value \"onap.policies.monitoring.tcagen2:1.0.0\" INVALID, " - + "does not equal existing entity"); - - assertThatThrownBy(() -> { - ToscaServiceTemplate badPolicyType = - standardYamlCoder.decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_WITH_NO_VERSION), - ToscaServiceTemplate.class); - policyTypeProvider.createPolicyType(badPolicyType); - }).hasMessageContaining("item \"version\" value \"0.0.0\" INVALID, is null"); - - policyTypeProvider.deletePolicyType(POLICY_TYPE_NAME_MONITORING, POLICY_TYPE_VERSION); - } - - @Test - public void testCreateOperationalPolicyTypes() throws CoderException, PfModelException { - ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder.decode( - ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_OPERATIONAL_COMMON), ToscaServiceTemplate.class); - ToscaServiceTemplate serviceTemplate = policyTypeProvider.createPolicyType(policyTypeServiceTemplate); - - assertNotNull(serviceTemplate.getPolicyTypes().get(POLICY_TYPE_OPERATIONAL_COMMON)); - - policyTypeServiceTemplate = standardYamlCoder.decode( - ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_OPERATIONAL_DROOLS), ToscaServiceTemplate.class); - serviceTemplate = policyTypeProvider.createPolicyType(policyTypeServiceTemplate); - assertNotNull(serviceTemplate.getPolicyTypes().get(POLICY_TYPE_OPERATIONAL_DROOLS)); - - policyTypeProvider.deletePolicyType(POLICY_TYPE_OPERATIONAL_DROOLS, POLICY_TYPE_VERSION); - policyTypeProvider.deletePolicyType(POLICY_TYPE_OPERATIONAL_COMMON, POLICY_TYPE_VERSION); - } - - @Test - public void testCreateApexOperationalPolicyTypes() throws CoderException, PfModelException { - ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder.decode( - ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_OPERATIONAL_COMMON), ToscaServiceTemplate.class); - ToscaServiceTemplate serviceTemplate = policyTypeProvider.createPolicyType(policyTypeServiceTemplate); - policyTypeServiceTemplate = standardYamlCoder.decode( - ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_OPERATIONAL_APEX), ToscaServiceTemplate.class); - serviceTemplate = policyTypeProvider.createPolicyType(policyTypeServiceTemplate); - assertNotNull(serviceTemplate.getPolicyTypes().get(POLICY_TYPE_OPERATIONAL_APEX)); - policyTypeProvider.deletePolicyType(POLICY_TYPE_OPERATIONAL_APEX, POLICY_TYPE_VERSION); - } - - @Test - public void testDeletePolicyType() throws Exception { - - ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder - .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_MONITORING), ToscaServiceTemplate.class); - ToscaServiceTemplate serviceTemplate = policyTypeProvider.createPolicyType(policyTypeServiceTemplate); - assertFalse(serviceTemplate.getPolicyTypes().isEmpty()); - - ToscaServiceTemplate policyServiceTemplate = standardYamlCoder - .decode(ResourceUtils.getResourceAsString(POLICY_RESOURCE_MONITORING), ToscaServiceTemplate.class); - policyProvider.createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate); - - String exceptionMessage = "policy type onap.policies.monitoring.tcagen2:1.0.0 is in use, " - + "it is referenced in policy onap.restart.tca:1.0.0"; - assertThatThrownBy(() -> { - policyTypeProvider.deletePolicyType("onap.policies.monitoring.tcagen2", "1.0.0"); - }).hasMessage(exceptionMessage); - - serviceTemplate = - policyProvider.deletePolicy("onap.policies.monitoring.tcagen2", "1.0.0", "onap.restart.tca", "1.0.0"); - assertFalse(serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).isEmpty()); - - serviceTemplate = policyTypeProvider.deletePolicyType("onap.policies.monitoring.tcagen2", "1.0.0"); - assertFalse(serviceTemplate.getPolicyTypes().isEmpty()); - - assertThatThrownBy(() -> { - policyTypeProvider.deletePolicyType("onap.policies.monitoring.tcagen2", "1.0.0"); - }).hasMessage("policy type onap.policies.monitoring.tcagen2:1.0.0 not found"); - } -}
\ No newline at end of file diff --git a/main/src/test/java/org/onap/policy/api/main/service/TestCommonToscaServiceTemplateService.java b/main/src/test/java/org/onap/policy/api/main/service/TestCommonToscaServiceTemplateService.java new file mode 100644 index 00000000..8d80cac8 --- /dev/null +++ b/main/src/test/java/org/onap/policy/api/main/service/TestCommonToscaServiceTemplateService.java @@ -0,0 +1,97 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022 Bell Canada. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END=========================================================; + */ + +package org.onap.policy.api.main.service; + +import java.util.Optional; +import org.junit.Before; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.onap.policy.api.main.repository.ToscaServiceTemplateRepository; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate; + +/** + * This class offers common mock utility methods for uni testing {@link ToscaServiceTemplateService}. + */ +public class TestCommonToscaServiceTemplateService { + + protected enum Operation { + CREATE_POLICY_TYPE, + DELETE_POLICY_TYPE, + CREATE_POLICY, + DELETE_POLICY; + } + + @Mock + protected ToscaServiceTemplateRepository toscaServiceTemplateRepository; + @Mock + protected PolicyTypeService policyTypeService; + @Mock + protected PolicyService policyService; + + /** + * Setup the DB TOSCA service template object post create, and delete request. + * @param dbSvcTemplate ToscaServiceTemplate object to update + * @param svcTemplateFragment the CRUD operation response ToscaServiceTemplate object + * @param operation the CRUD operation performed + */ + protected void mockDbServiceTemplate(ToscaServiceTemplate dbSvcTemplate, ToscaServiceTemplate svcTemplateFragment, + TestToscaServiceTemplateServiceForPolicyCrud.Operation operation) { + if (operation != null) { + switch (operation) { + case CREATE_POLICY_TYPE: + dbSvcTemplate.getPolicyTypes().putAll(svcTemplateFragment.getPolicyTypes()); + if (svcTemplateFragment.getDataTypes() != null) { + if (dbSvcTemplate.getDataTypes() == null) { + dbSvcTemplate.setDataTypes(svcTemplateFragment.getDataTypes()); + } else { + dbSvcTemplate.getDataTypes().putAll(svcTemplateFragment.getDataTypes()); + } + } + break; + case DELETE_POLICY_TYPE: + dbSvcTemplate.getPolicyTypes().keySet().removeAll(svcTemplateFragment.getPolicyTypes().keySet()); + break; + case CREATE_POLICY: + dbSvcTemplate.setToscaTopologyTemplate(svcTemplateFragment.getToscaTopologyTemplate()); + break; + case DELETE_POLICY: + dbSvcTemplate.getToscaTopologyTemplate().setPolicies(null); + break; + default: + break; + } + } + Mockito.when(toscaServiceTemplateRepository.findById(new PfConceptKey(JpaToscaServiceTemplate.DEFAULT_NAME, + JpaToscaServiceTemplate.DEFAULT_VERSION))) + .thenReturn(Optional.of(new JpaToscaServiceTemplate(dbSvcTemplate))); + } + + /** + * Setup to return empty DB service template. + */ + @Before + public void setUp() { + Mockito.when(toscaServiceTemplateRepository.findById(new PfConceptKey(JpaToscaServiceTemplate.DEFAULT_NAME, + JpaToscaServiceTemplate.DEFAULT_VERSION))).thenReturn(Optional.of(new JpaToscaServiceTemplate())); + } +}
\ No newline at end of file diff --git a/main/src/test/java/org/onap/policy/api/main/service/TestPdpGroupService.java b/main/src/test/java/org/onap/policy/api/main/service/TestPdpGroupService.java new file mode 100644 index 00000000..4c04a08e --- /dev/null +++ b/main/src/test/java/org/onap/policy/api/main/service/TestPdpGroupService.java @@ -0,0 +1,84 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022 Bell Canada. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END=========================================================; + */ + +package org.onap.policy.api.main.service; + +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.List; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.policy.api.main.repository.PdpGroupRepository; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.common.utils.resources.ResourceUtils; +import org.onap.policy.models.pdp.concepts.PdpGroups; +import org.onap.policy.models.pdp.persistence.concepts.JpaPdpGroup; + +@RunWith(MockitoJUnitRunner.class) +public class TestPdpGroupService { + + @Mock + private PdpGroupRepository pdpGroupRepository; + + @InjectMocks + private PdpGroupService pdpGroupService; + + /** + * Test setup. + * @throws CoderException decode errors + */ + @Before + public void setUp() throws CoderException { + var pdpGroups = new StandardCoder().decode(ResourceUtils.getResourceAsString("pdpgroups/PdpGroups.json"), + PdpGroups.class).getGroups(); + List<JpaPdpGroup> jpaPdpGroupList = new ArrayList<>(); + pdpGroups.forEach(pdpGroup -> jpaPdpGroupList.add(new JpaPdpGroup(pdpGroup))); + + when(pdpGroupRepository.findAll()).thenReturn(jpaPdpGroupList); + } + + @Test + public void testAssertPolicyTypeNotSupportedInPdpGroup() { + assertThatCode(() -> pdpGroupService.assertPolicyTypeNotSupportedInPdpGroup("policy_type_not_supported", + "1.0.0")).doesNotThrowAnyException(); + + assertThatThrownBy(() -> pdpGroupService.assertPolicyTypeNotSupportedInPdpGroup( + "onap.policies.controlloop.guard.common.FrequencyLimiter", "1.0.0")) + .hasMessage("policy type is in use, it is referenced in PDP group defaultGroup subgroup xacml"); + } + + @Test + public void testAssertPolicyNotDeployedInPdpGroup() { + assertThatCode(() -> pdpGroupService.assertPolicyNotDeployedInPdpGroup("policy_not_deployed", "1.0.0")) + .doesNotThrowAnyException(); + + assertThatThrownBy(() -> pdpGroupService.assertPolicyNotDeployedInPdpGroup( + "onap.policies.controlloop.operational.common.apex.SampleDomain", "1.0.0")) + .hasMessage("policy is in use, it is deployed in PDP group defaultGroup subgroup apex"); + } +}
\ No newline at end of file diff --git a/main/src/test/java/org/onap/policy/api/main/service/TestPolicyService.java b/main/src/test/java/org/onap/policy/api/main/service/TestPolicyService.java new file mode 100644 index 00000000..f9baf76a --- /dev/null +++ b/main/src/test/java/org/onap/policy/api/main/service/TestPolicyService.java @@ -0,0 +1,49 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022 Bell Canada. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END=========================================================; + */ + +package org.onap.policy.api.main.service; + +import static org.assertj.core.api.Assertions.assertThatCode; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.policy.api.main.repository.PolicyRepository; +import org.onap.policy.models.base.PfConceptKey; + +@RunWith(MockitoJUnitRunner.class) +public class TestPolicyService { + + @Mock + private PolicyRepository policyRepository; + + @InjectMocks + private PolicyService policyService; + + @Test + public void testDeletePolicy() { + PfConceptKey id = new PfConceptKey("dummy", "1.0.0"); + Mockito.doNothing().when(policyRepository).deleteById(id); + assertThatCode(() -> policyService.deletePolicy(id)).doesNotThrowAnyException(); + } +}
\ No newline at end of file diff --git a/main/src/test/java/org/onap/policy/api/main/service/TestPolicyTypeService.java b/main/src/test/java/org/onap/policy/api/main/service/TestPolicyTypeService.java new file mode 100644 index 00000000..21f1ba4d --- /dev/null +++ b/main/src/test/java/org/onap/policy/api/main/service/TestPolicyTypeService.java @@ -0,0 +1,49 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022 Bell Canada. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END=========================================================; + */ + +package org.onap.policy.api.main.service; + +import static org.assertj.core.api.Assertions.assertThatCode; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.policy.api.main.repository.PolicyTypeRepository; +import org.onap.policy.models.base.PfConceptKey; + +@RunWith(MockitoJUnitRunner.class) +public class TestPolicyTypeService { + + @Mock + private PolicyTypeRepository policyTypeRepository; + + @InjectMocks + private PolicyTypeService policyTypeService; + + @Test + public void testDeletePolicy() { + PfConceptKey id = new PfConceptKey("dummy", "1.0.0"); + Mockito.doNothing().when(policyTypeRepository).deleteById(id); + assertThatCode(() -> policyTypeService.deletePolicyType(id)).doesNotThrowAnyException(); + } +}
\ No newline at end of file diff --git a/main/src/test/java/org/onap/policy/api/main/service/TestToscaServiceTemplateServiceForPolicyCrud.java b/main/src/test/java/org/onap/policy/api/main/service/TestToscaServiceTemplateServiceForPolicyCrud.java new file mode 100644 index 00000000..69864282 --- /dev/null +++ b/main/src/test/java/org/onap/policy/api/main/service/TestToscaServiceTemplateServiceForPolicyCrud.java @@ -0,0 +1,437 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy API + * ================================================================================ + * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2019-2021 Nordix Foundation. + * Modifications Copyright (C) 2020,2022 Bell Canada. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.api.main.service; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; + +import java.util.Optional; +import javax.ws.rs.core.Response; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.common.utils.coder.StandardYamlCoder; +import org.onap.policy.common.utils.resources.ResourceUtils; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.base.PfModelRuntimeException; +import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate; + +/** + * This class performs unit test of Policy CRUD operations as implemented in {@link ToscaServiceTemplateService}. + * + * @author Chenfei Gao (cgao@research.att.com) + */ +@RunWith(MockitoJUnitRunner.class) +public class TestToscaServiceTemplateServiceForPolicyCrud extends TestCommonToscaServiceTemplateService { + + private static StandardCoder standardCoder = new StandardCoder(); + private static StandardYamlCoder standardYamlCoder = new StandardYamlCoder(); + + private static final String POLICY_RESOURCE = "policies/vCPE.policy.monitoring.input.tosca.json"; + private static final String POLICY_TYPE_RESOURCE = "policytypes/onap.policies.monitoring.tcagen2.yaml"; + private static final String POLICY_RESOURCE_WITH_BAD_POLICYTYPE_ID = "policies/vCPE.policy.bad.policytypeid.json"; + private static final String POLICY_RESOURCE_WITH_BAD_POLICYTYPE_VERSION = + "policies/vCPE.policy.bad.policytypeversion.json"; + private static final String POLICY_RESOURCE_WITH_NO_POLICY_VERSION = "policies/vCPE.policy.no.policyversion.json"; + private static final String POLICY_RESOURCE_WITH_DIFFERENT_FIELDS = + "policies/vCPE.policy.different.policy.fields.json"; + private static final String MULTIPLE_POLICIES_RESOURCE = "policies/vCPE.policies.optimization.input.tosca.json"; + + private static final String POLICY_TYPE_RESOURCE_OPERATIONAL_COMMON = + "policytypes/onap.policies.controlloop.operational.Common.yaml"; + private static final String POLICY_TYPE_RESOURCE_OPERATIONAL_DROOLS = + "policytypes/onap.policies.controlloop.operational.common.Drools.yaml"; + private static final String POLICY_RESOURCE_OPERATIONAL = "policies/vCPE.policy.operational.input.tosca.json"; + private static final String POLICY_TYPE_OPERATIONAL_DROOLS = "onap.policies.controlloop.operational.common.Drools"; + + @Mock + private PdpGroupService pdpGroupService; + + @InjectMocks + private ToscaServiceTemplateService toscaServiceTemplateService; + + @Before + public void setUp() { + super.setUp(); + } + + @Test + public void testFetchPolicies() { + Mockito.when(toscaServiceTemplateRepository.findById(new PfConceptKey(JpaToscaServiceTemplate.DEFAULT_NAME, + JpaToscaServiceTemplate.DEFAULT_VERSION))).thenReturn(Optional.empty()); + + assertThatThrownBy(() -> { + toscaServiceTemplateService.fetchPolicies("dummy", "1.0.0", null, null, null); + }).hasMessage("service template not found in database"); + + assertThatThrownBy(() -> { + toscaServiceTemplateService.fetchPolicies("dummy", "1.0.0", "dummy", null, null); + }).hasMessage("service template not found in database"); + + assertThatThrownBy(() -> { + toscaServiceTemplateService.fetchPolicies("dummy", "1.0.0", "dummy", "1.0.0", null); + }).hasMessage("service template not found in database"); + + assertThatThrownBy(() -> { + toscaServiceTemplateService.fetchPolicies(null, null, "dummy", "1.0.0", null); + }).hasMessage("service template not found in database"); + } + + @Test + public void testFetchLatestPolicies() { + Mockito.when(toscaServiceTemplateRepository.findById(new PfConceptKey(JpaToscaServiceTemplate.DEFAULT_NAME, + JpaToscaServiceTemplate.DEFAULT_VERSION))).thenReturn(Optional.empty()); + + assertThatThrownBy(() -> { + toscaServiceTemplateService.fetchLatestPolicies("dummy", "dummy", "dummy", null); + }).hasMessage("service template not found in database"); + } + + @Test + public void testCreatePolicy() throws Exception { + assertThatThrownBy(() -> { + toscaServiceTemplateService.createPolicy("dummy", "1.0.0", new ToscaServiceTemplate()); + }).hasMessage("topology template not specified on service template"); + + var policyTypeServiceTemplate = standardYamlCoder + .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class); + var serviceTemplate = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate); + mockDbServiceTemplate(serviceTemplate, null, null); + + assertThatCode(() -> toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate)) + .doesNotThrowAnyException(); + + assertThatThrownBy(() -> { + var badPolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_WITH_BAD_POLICYTYPE_ID); + var badPolicyServiceTemplate = + standardCoder.decode(badPolicyString, ToscaServiceTemplate.class); + toscaServiceTemplateService.createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", + badPolicyServiceTemplate); + }).hasMessage( + "Version not specified, the version of this TOSCA entity must be specified in " + + "the type_version field"); + + assertThatThrownBy(() -> { + var badPolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_WITH_BAD_POLICYTYPE_VERSION); + var badPolicyServiceTemplate = + standardCoder.decode(badPolicyString, ToscaServiceTemplate.class); + toscaServiceTemplateService.createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", + badPolicyServiceTemplate); + }).hasMessageContaining( + "item \"policy type\" value \"onap.policies.monitoring.cdap.tca.hi.lo.app:2.0.0\" INVALID, not found"); + + assertThatThrownBy(() -> { + var badPolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_WITH_NO_POLICY_VERSION); + var badPolicyServiceTemplate = + standardCoder.decode(badPolicyString, ToscaServiceTemplate.class); + toscaServiceTemplateService.createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", + badPolicyServiceTemplate); + }).hasMessageContaining("item \"version\" value \"0.0.0\" INVALID, is null"); + + var policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE); + var policyServiceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class); + var createPolicyResponseFragment = toscaServiceTemplateService + .createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate); + assertFalse(createPolicyResponseFragment.getToscaTopologyTemplate().getPolicies().get(0).isEmpty()); + mockDbServiceTemplate(serviceTemplate, createPolicyResponseFragment, Operation.CREATE_POLICY); + + assertThatThrownBy(() -> { + var badPolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_WITH_DIFFERENT_FIELDS); + var badPolicyServiceTemplate = + standardCoder.decode(badPolicyString, ToscaServiceTemplate.class); + toscaServiceTemplateService.createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", + badPolicyServiceTemplate); + }).hasMessageContaining( + "item \"entity\" value \"onap.restart.tca:1.0.0\" INVALID, " + "does not equal existing entity"); + } + + @Test + public void testCreateOperationalDroolsPolicy() throws CoderException { + var policyTypeServiceTemplate = standardYamlCoder.decode( + ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_OPERATIONAL_COMMON), ToscaServiceTemplate.class); + + var serviceTemplate = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate); + mockDbServiceTemplate(serviceTemplate, null, null); + + policyTypeServiceTemplate = standardYamlCoder.decode( + ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_OPERATIONAL_DROOLS), ToscaServiceTemplate.class); + var createPolicyTypeResponseFragment = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate); + mockDbServiceTemplate(serviceTemplate, createPolicyTypeResponseFragment, Operation.CREATE_POLICY_TYPE); + + var policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_OPERATIONAL); + var policyServiceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class); + serviceTemplate = + toscaServiceTemplateService.createPolicy(POLICY_TYPE_OPERATIONAL_DROOLS, "1.0.0", policyServiceTemplate); + assertFalse(serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).isEmpty()); + } + + @Test + public void testSimpleCreatePolicy() throws Exception { + + assertThatThrownBy(() -> { + String multiPoliciesString = ResourceUtils.getResourceAsString(MULTIPLE_POLICIES_RESOURCE); + ToscaServiceTemplate multiPoliciesServiceTemplate = + standardCoder.decode(multiPoliciesString, ToscaServiceTemplate.class); + toscaServiceTemplateService.createPolicies(multiPoliciesServiceTemplate); + }).hasMessageContaining( + "no policy types are defined on the service template for the policies in the topology template"); + + // Create required policy types + var policyTypeServiceTemplate = standardYamlCoder.decode( + ResourceUtils.getResourceAsString("policytypes/onap.policies.Optimization.yaml"), + ToscaServiceTemplate.class); + var serviceTemplate = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate); + mockDbServiceTemplate(serviceTemplate, null, null); + + policyTypeServiceTemplate = standardYamlCoder.decode( + ResourceUtils.getResourceAsString("policytypes/onap.policies.optimization.Resource.yaml"), + ToscaServiceTemplate.class); + var createPolicyTypeResponseFragment = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate); + mockDbServiceTemplate(serviceTemplate, createPolicyTypeResponseFragment, Operation.CREATE_POLICY_TYPE); + + policyTypeServiceTemplate = standardYamlCoder.decode( + ResourceUtils + .getResourceAsString("policytypes/onap.policies.optimization.resource.AffinityPolicy.yaml"), + ToscaServiceTemplate.class); + createPolicyTypeResponseFragment = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate); + mockDbServiceTemplate(serviceTemplate, createPolicyTypeResponseFragment, Operation.CREATE_POLICY_TYPE); + + policyTypeServiceTemplate = standardYamlCoder.decode( + ResourceUtils + .getResourceAsString("policytypes/onap.policies.optimization.resource.DistancePolicy.yaml"), + ToscaServiceTemplate.class); + createPolicyTypeResponseFragment = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate); + mockDbServiceTemplate(serviceTemplate, createPolicyTypeResponseFragment, Operation.CREATE_POLICY_TYPE); + + policyTypeServiceTemplate = standardYamlCoder.decode( + ResourceUtils.getResourceAsString("policytypes/onap.policies.optimization.resource.Vim_fit.yaml"), + ToscaServiceTemplate.class); + createPolicyTypeResponseFragment = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate); + mockDbServiceTemplate(serviceTemplate, createPolicyTypeResponseFragment, Operation.CREATE_POLICY_TYPE); + + policyTypeServiceTemplate = standardYamlCoder.decode( + ResourceUtils.getResourceAsString("policytypes/onap.policies.optimization.resource.HpaPolicy.yaml"), + ToscaServiceTemplate.class); + createPolicyTypeResponseFragment = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate); + mockDbServiceTemplate(serviceTemplate, createPolicyTypeResponseFragment, Operation.CREATE_POLICY_TYPE); + + policyTypeServiceTemplate = standardYamlCoder.decode( + ResourceUtils.getResourceAsString("policytypes/onap.policies.optimization.resource.VnfPolicy.yaml"), + ToscaServiceTemplate.class); + createPolicyTypeResponseFragment = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate); + mockDbServiceTemplate(serviceTemplate, createPolicyTypeResponseFragment, Operation.CREATE_POLICY_TYPE); + + policyTypeServiceTemplate = standardYamlCoder.decode( + ResourceUtils.getResourceAsString("policytypes/onap.policies.optimization.Service.yaml"), + ToscaServiceTemplate.class); + createPolicyTypeResponseFragment = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate); + mockDbServiceTemplate(serviceTemplate, createPolicyTypeResponseFragment, Operation.CREATE_POLICY_TYPE); + + policyTypeServiceTemplate = standardYamlCoder.decode( + ResourceUtils + .getResourceAsString("policytypes/onap.policies.optimization.service.SubscriberPolicy.yaml"), + ToscaServiceTemplate.class); + createPolicyTypeResponseFragment = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate); + mockDbServiceTemplate(serviceTemplate, createPolicyTypeResponseFragment, Operation.CREATE_POLICY_TYPE); + + policyTypeServiceTemplate = standardYamlCoder.decode( + ResourceUtils.getResourceAsString("policytypes/onap.policies.optimization.service.QueryPolicy.yaml"), + ToscaServiceTemplate.class); + createPolicyTypeResponseFragment = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate); + mockDbServiceTemplate(serviceTemplate, createPolicyTypeResponseFragment, Operation.CREATE_POLICY_TYPE); + + policyTypeServiceTemplate = standardYamlCoder.decode( + ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.tcagen2.yaml"), + ToscaServiceTemplate.class); + createPolicyTypeResponseFragment = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate); + mockDbServiceTemplate(serviceTemplate, createPolicyTypeResponseFragment, Operation.CREATE_POLICY_TYPE); + + // Create multiple policies in one call + var multiPoliciesString = ResourceUtils.getResourceAsString(MULTIPLE_POLICIES_RESOURCE); + var multiPoliciesServiceTemplate = + standardCoder.decode(multiPoliciesString, ToscaServiceTemplate.class); + + assertThatCode(() -> { + toscaServiceTemplateService.createPolicies(multiPoliciesServiceTemplate); + toscaServiceTemplateService.createPolicies(multiPoliciesServiceTemplate); + }).doesNotThrowAnyException(); + } + + @Test + public void testDeletePolicy() throws CoderException, PfModelException { + + assertThatThrownBy(() -> { + toscaServiceTemplateService.deletePolicy("dummy", "1.0.0", "dummy", "1.0.0"); + }).hasMessage("no policies found"); + + var policyTypeServiceTemplate = standardYamlCoder + .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class); + var serviceTemplate = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate); + mockDbServiceTemplate(serviceTemplate, null, null); + + var policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE); + var policyServiceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class); + var createPolicyResponseFragment = toscaServiceTemplateService + .createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate); + assertFalse(createPolicyResponseFragment.getToscaTopologyTemplate().getPolicies().get(0).isEmpty()); + mockDbServiceTemplate(serviceTemplate, createPolicyResponseFragment, Operation.CREATE_POLICY); + + var exceptionMessage = "policy is in use, it is deployed in PDP group dummy subgroup dummy"; + Mockito.doThrow(new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE, exceptionMessage)) + .when(pdpGroupService).assertPolicyNotDeployedInPdpGroup("onap.restart.tca", "1.0.0"); + assertThatThrownBy(() -> { + toscaServiceTemplateService.deletePolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", + "onap.restart.tca", "1.0.0"); + }).hasMessage(exceptionMessage); + Mockito.doNothing().when(pdpGroupService).assertPolicyNotDeployedInPdpGroup("onap.restart.tca", "1.0.0"); + + var deletePolicyResponseFragment = toscaServiceTemplateService + .deletePolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", "onap.restart.tca", "1.0.0"); + assertFalse(deletePolicyResponseFragment.getToscaTopologyTemplate().getPolicies().get(0).isEmpty()); + + mockDbServiceTemplate(serviceTemplate, deletePolicyResponseFragment, Operation.DELETE_POLICY); + assertThatThrownBy(() -> { + toscaServiceTemplateService.deletePolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", + "onap.restart.tca", "1.0.0"); + }).hasMessageContaining("no policies found"); + } + + @Test + public void testFetchAllPolicies() throws Exception { + // Create Policy Type + var policyTypeServiceTemplate = standardYamlCoder + .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class); + var serviceTemplate = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate); + mockDbServiceTemplate(serviceTemplate, null, null); + + // Create Policy + var policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE); + var policyServiceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class); + var createPolicyResponseFragment = toscaServiceTemplateService + .createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate); + mockDbServiceTemplate(serviceTemplate, createPolicyResponseFragment, Operation.CREATE_POLICY); + + assertThat(serviceTemplate.getToscaTopologyTemplate().getPolicies()).hasSize(1); + + // Test fetch all policies + serviceTemplate = toscaServiceTemplateService.fetchPolicies(null, null, null, null, null); + + assertThat(serviceTemplate.getToscaTopologyTemplate().getPolicies()).hasSize(1); + } + + @Test + public void testFetchSpecificPolicy_availablePolicy() throws Exception { + // Create Policy Type + var policyTypeServiceTemplate = standardYamlCoder + .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class); + var serviceTemplate = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate); + mockDbServiceTemplate(serviceTemplate, null, null); + + // Create Policy + var policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE); + var policyServiceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class); + var createPolicyResponseFragment = toscaServiceTemplateService + .createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate); + mockDbServiceTemplate(serviceTemplate, createPolicyResponseFragment, Operation.CREATE_POLICY); + + assertThat(serviceTemplate.getToscaTopologyTemplate().getPolicies()).hasSize(1); + + // Test fetch specific policy + assertThat(toscaServiceTemplateService.fetchPolicies(null, null, "onap.restart.tca", "1.0.0", null) + .getToscaTopologyTemplate().getPolicies()).hasSize(1); + } + + @Test + public void testFetchSpecificPolicy_unavailablePolicy() throws Exception { + // Create Policy Type + var policyTypeServiceTemplate = standardYamlCoder + .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class); + var serviceTemplate = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate); + mockDbServiceTemplate(serviceTemplate, null, null); + + // Create Policy + var policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE); + var policyServiceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class); + var createPolicyResponseFragment = toscaServiceTemplateService + .createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate); + mockDbServiceTemplate(serviceTemplate, createPolicyResponseFragment, Operation.CREATE_POLICY); + assertNotNull(serviceTemplate.getToscaTopologyTemplate().getPolicies()); + assertThat(serviceTemplate.getToscaTopologyTemplate().getPolicies()).hasSize(1); + + // Test fetch specific policy + assertThatThrownBy(() -> toscaServiceTemplateService.fetchPolicies(null, null, "onap.restart.tca", + "2.0.0", null)).hasMessageContaining("policies for onap.restart.tca:2.0.0 do not exist"); + } + + @Test + public void testDeleteSpecificPolicy_availablePolicy() throws Exception { + var policyTypeServiceTemplate = standardYamlCoder + .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class); + var serviceTemplate = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate); + mockDbServiceTemplate(serviceTemplate, null, null); + + var policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE); + var policyServiceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class); + var createPolicyResponseFragment = toscaServiceTemplateService + .createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate); + assertThat(createPolicyResponseFragment.getToscaTopologyTemplate().getPolicies()).hasSize(1); + mockDbServiceTemplate(serviceTemplate, createPolicyResponseFragment, Operation.CREATE_POLICY); + + serviceTemplate = toscaServiceTemplateService.deletePolicy(null, null, "onap.restart.tca", "1.0.0"); + assertThat(serviceTemplate.getToscaTopologyTemplate().getPolicies()).hasSize(1); + } + + @Test + public void testDeleteSpecificPolicy_unavailablePolicy() throws Exception { + var policyTypeServiceTemplate = standardYamlCoder + .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class); + var serviceTemplate = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate); + mockDbServiceTemplate(serviceTemplate, null, null); + + var policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE); + var policyServiceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class); + var createPolicyResponseFragment = toscaServiceTemplateService + .createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate); + assertThat(createPolicyResponseFragment.getToscaTopologyTemplate().getPolicies()).hasSize(1); + mockDbServiceTemplate(serviceTemplate, createPolicyResponseFragment, Operation.CREATE_POLICY); + + assertThatThrownBy(() -> toscaServiceTemplateService.deletePolicy(null, null, "onap.restart.tca", "2.0.0")) + .hasMessageContaining("not found"); + + assertThatThrownBy(() -> toscaServiceTemplateService.deletePolicy(null, null, + "onap.restart.tca.unavailable", "1.0.0")).hasMessageContaining("not found"); + } +}
\ No newline at end of file diff --git a/main/src/test/java/org/onap/policy/api/main/service/TestToscaServiceTemplateServiceForPolicyTypeCrud.java b/main/src/test/java/org/onap/policy/api/main/service/TestToscaServiceTemplateServiceForPolicyTypeCrud.java new file mode 100644 index 00000000..cea2bcf0 --- /dev/null +++ b/main/src/test/java/org/onap/policy/api/main/service/TestToscaServiceTemplateServiceForPolicyTypeCrud.java @@ -0,0 +1,213 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022 Bell Canada. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END=========================================================; + */ + +package org.onap.policy.api.main.service; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; + +import javax.ws.rs.core.Response; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardYamlCoder; +import org.onap.policy.common.utils.resources.ResourceUtils; +import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.base.PfModelRuntimeException; +import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; + +/** + * This class performs unit test of Policy Type CRUD operations as implemented in {@link ToscaServiceTemplateService}. + * + * @author Chenfei Gao (cgao@research.att.com) + */ +@RunWith(MockitoJUnitRunner.class) +public class TestToscaServiceTemplateServiceForPolicyTypeCrud extends TestCommonToscaServiceTemplateService { + + private static StandardYamlCoder coder = new StandardYamlCoder(); + private static final String POLICY_TYPE_VERSION = "1.0.0"; + + private static final String POLICY_RESOURCE_MONITORING = "policies/vCPE.policy.monitoring.input.tosca.yaml"; + private static final String POLICY_TYPE_RESOURCE_MONITORING = "policytypes/onap.policies.monitoring.tcagen2.yaml"; + private static final String POLICY_TYPE_RESOURCE_WITH_NO_VERSION = + "policytypes/onap.policies.optimization.Resource.no.version.yaml"; + private static final String POLICY_TYPE_NAME_MONITORING = "onap.policies.monitoring.tcagen2"; + + private static final String POLICY_TYPE_RESOURCE_OPERATIONAL_COMMON = + "policytypes/onap.policies.controlloop.operational.Common.yaml"; + private static final String POLICY_TYPE_RESOURCE_OPERATIONAL_DROOLS = + "policytypes/onap.policies.controlloop.operational.common.Drools.yaml"; + private static final String POLICY_TYPE_RESOURCE_OPERATIONAL_APEX = + "policytypes/onap.policies.controlloop.operational.common.Apex.yaml"; + private static final String POLICY_TYPE_OPERATIONAL_COMMON = "onap.policies.controlloop.operational.Common"; + private static final String POLICY_TYPE_OPERATIONAL_APEX = "onap.policies.controlloop.operational.common.Apex"; + private static final String POLICY_TYPE_OPERATIONAL_DROOLS = "onap.policies.controlloop.operational.common.Drools"; + + @Mock + private PdpGroupService pdpGroupService; + + @InjectMocks + private ToscaServiceTemplateService toscaServiceTemplateService; + + /** + * Test setup. + */ + @Before + public void setUp() { + super.setUp(); + } + + @Test + public void testFetchPolicyTypes() throws PfModelException { + assertThatThrownBy(() -> { + toscaServiceTemplateService.fetchPolicyTypes("dummy", null); + }).hasMessage("policy types for filter ToscaEntityFilter(name=dummy, version=null) do not exist"); + + assertThatThrownBy(() -> { + toscaServiceTemplateService.fetchPolicyTypes("dummy", "dummy"); + }).hasMessage("policy types for filter ToscaEntityFilter(name=dummy, version=dummy) do not exist"); + + // FIXME + // ToscaServiceTemplate serviceTemplate = toscaServiceTemplateService.fetchPolicyTypes(null, null); + // assertFalse(serviceTemplate.getPolicyTypes().isEmpty()); + } + + @Test + public void testFetchLatestPolicyTypes() { + + assertThatThrownBy(() -> { + toscaServiceTemplateService.fetchLatestPolicyTypes("dummy"); + }).hasMessage("policy types for filter ToscaEntityFilter(name=dummy, version=LATEST) do not exist"); + } + + @Test + public void testCreatePolicyType() throws CoderException { + var policyTypeServiceTemplate = coder + .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_MONITORING), ToscaServiceTemplate.class); + var serviceTemplate = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate); + assertFalse(serviceTemplate.getPolicyTypes().isEmpty()); + assertEquals(2, serviceTemplate.getPolicyTypes().size()); + mockDbServiceTemplate(serviceTemplate, null, null); + + policyTypeServiceTemplate.getPolicyTypes().get("onap.policies.monitoring.tcagen2") + .setDescription("Some other description"); + + assertThatThrownBy(() -> { + toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate); + }).hasMessageContaining("item \"entity\" value \"onap.policies.monitoring.tcagen2:1.0.0\" INVALID, " + + "does not equal existing entity"); + + assertThatThrownBy(() -> { + ToscaServiceTemplate badPolicyType = + coder.decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_WITH_NO_VERSION), + ToscaServiceTemplate.class); + toscaServiceTemplateService.createPolicyType(badPolicyType); + }).hasMessageContaining("item \"version\" value \"0.0.0\" INVALID, is null"); + + toscaServiceTemplateService.deletePolicyType(POLICY_TYPE_NAME_MONITORING, POLICY_TYPE_VERSION); + } + + @Test + public void testCreateOperationalPolicyTypes() throws CoderException { + ToscaServiceTemplate policyTypeServiceTemplate = coder.decode( + ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_OPERATIONAL_COMMON), ToscaServiceTemplate.class); + ToscaServiceTemplate serviceTemplate = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate); + assertNotNull(serviceTemplate.getPolicyTypes().get(POLICY_TYPE_OPERATIONAL_COMMON)); + mockDbServiceTemplate(serviceTemplate, null, null); + + policyTypeServiceTemplate = coder.decode( + ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_OPERATIONAL_DROOLS), ToscaServiceTemplate.class); + var createPolicyTypeResponseFragment = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate); + assertNotNull(createPolicyTypeResponseFragment.getPolicyTypes().get(POLICY_TYPE_OPERATIONAL_DROOLS)); + mockDbServiceTemplate(serviceTemplate, createPolicyTypeResponseFragment, Operation.CREATE_POLICY_TYPE); + + var deletePolicyTypeResponseFragment = toscaServiceTemplateService + .deletePolicyType(POLICY_TYPE_OPERATIONAL_DROOLS, POLICY_TYPE_VERSION); + mockDbServiceTemplate(serviceTemplate, deletePolicyTypeResponseFragment, Operation.DELETE_POLICY_TYPE); + toscaServiceTemplateService.deletePolicyType(POLICY_TYPE_OPERATIONAL_COMMON, POLICY_TYPE_VERSION); + } + + @Test + public void testCreateApexOperationalPolicyTypes() throws CoderException { + var policyTypeServiceTemplate = coder.decode( + ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_OPERATIONAL_COMMON), ToscaServiceTemplate.class); + var serviceTemplate = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate); + + mockDbServiceTemplate(serviceTemplate, null, null); + policyTypeServiceTemplate = coder.decode( + ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_OPERATIONAL_APEX), ToscaServiceTemplate.class); + var createPolicyTypeResponseFragment = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate); + assertNotNull(createPolicyTypeResponseFragment.getPolicyTypes().get(POLICY_TYPE_OPERATIONAL_APEX)); + + mockDbServiceTemplate(serviceTemplate, createPolicyTypeResponseFragment, Operation.CREATE_POLICY_TYPE); + toscaServiceTemplateService.deletePolicyType(POLICY_TYPE_OPERATIONAL_APEX, POLICY_TYPE_VERSION); + } + + @Test + public void testDeletePolicyType() throws CoderException { + var policyTypeServiceTemplate = coder + .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_MONITORING), ToscaServiceTemplate.class); + var serviceTemplate = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate); + assertFalse(serviceTemplate.getPolicyTypes().isEmpty()); + + var policyServiceTemplate = coder + .decode(ResourceUtils.getResourceAsString(POLICY_RESOURCE_MONITORING), ToscaServiceTemplate.class); + mockDbServiceTemplate(serviceTemplate, null, null); + var createPolicyResponseFragment = toscaServiceTemplateService.createPolicy("onap.policies.monitoring.tcagen2", + "1.0.0", policyServiceTemplate); + + mockDbServiceTemplate(serviceTemplate, createPolicyResponseFragment, Operation.CREATE_POLICY); + var exceptionMessage = "policy type onap.policies.monitoring.tcagen2:1.0.0 is in use, " + + "it is referenced in policy onap.restart.tca:1.0.0"; + assertThatThrownBy(() -> { + toscaServiceTemplateService.deletePolicyType("onap.policies.monitoring.tcagen2", "1.0.0"); + }).hasMessage(exceptionMessage); + + var deletePolicyResponseFragment = toscaServiceTemplateService + .deletePolicy("onap.policies.monitoring.tcagen2", "1.0.0", "onap.restart.tca", "1.0.0"); + assertFalse(deletePolicyResponseFragment.getToscaTopologyTemplate().getPolicies().get(0).isEmpty()); + mockDbServiceTemplate(serviceTemplate, deletePolicyResponseFragment, Operation.DELETE_POLICY); + + exceptionMessage = "policy type is in use, it is referenced in PDP group dummy subgroup dummy"; + Mockito.doThrow(new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE, exceptionMessage)) + .when(pdpGroupService).assertPolicyTypeNotSupportedInPdpGroup("onap.policies.monitoring.tcagen2", "1.0.0"); + assertThatThrownBy(() -> { + toscaServiceTemplateService.deletePolicyType("onap.policies.monitoring.tcagen2", "1.0.0"); + }).hasMessage(exceptionMessage); + + Mockito.doNothing().when(pdpGroupService) + .assertPolicyTypeNotSupportedInPdpGroup("onap.policies.monitoring.tcagen2", "1.0.0"); + var deletePolicyTypeResponseFragment = toscaServiceTemplateService + .deletePolicyType("onap.policies.monitoring.tcagen2", "1.0.0"); + assertFalse(deletePolicyTypeResponseFragment.getPolicyTypes().isEmpty()); + + mockDbServiceTemplate(serviceTemplate, deletePolicyTypeResponseFragment, Operation.DELETE_POLICY_TYPE); + assertThatThrownBy(() -> { + toscaServiceTemplateService.deletePolicyType("onap.policies.monitoring.tcagen2", "1.0.0"); + }).hasMessage("policy type onap.policies.monitoring.tcagen2:1.0.0 not found"); + } +}
\ No newline at end of file diff --git a/main/src/test/resources/pdpgroups/PdpGroups.json b/main/src/test/resources/pdpgroups/PdpGroups.json new file mode 100644 index 00000000..5ec387ed --- /dev/null +++ b/main/src/test/resources/pdpgroups/PdpGroups.json @@ -0,0 +1,125 @@ +{ + "groups": [ + { + "name": "defaultGroup", + "version": "1.0.0", + "description": "The default group that registers all supported policy types and pdps.", + "pdpGroupState": "ACTIVE", + "pdpSubgroups": [ + { + "pdpType": "xacml", + "supportedPolicyTypes": [ + { + "name": "onap.policies.controlloop.guard.common.FrequencyLimiter", + "version": "1.0.0" + }, + { + "name": "onap.policies.controlloop.guard.common.MinMax", + "version": "1.0.0" + }, + { + "name": "onap.policies.controlloop.guard.common.Blacklist", + "version": "1.0.0" + }, + { + "name": "onap.policies.controlloop.guard.common.Filter", + "version": "1.0.0" + }, + { + "name": "onap.policies.controlloop.guard.coordination.FirstBlocksSecond", + "version": "1.0.0" + }, + { + "name": "onap.policies.monitoring.*", + "version": "1.0.0" + }, + { + "name": "onap.policies.optimization.*", + "version": "1.0.0" + }, + { + "name": "onap.policies.optimization.resource.AffinityPolicy", + "version": "1.0.0" + }, + { + "name": "onap.policies.optimization.resource.DistancePolicy", + "version": "1.0.0" + }, + { + "name": "onap.policies.optimization.resource.HpaPolicy", + "version": "1.0.0" + }, + { + "name": "onap.policies.optimization.resource.OptimizationPolicy", + "version": "1.0.0" + }, + { + "name": "onap.policies.optimization.resource.PciPolicy", + "version": "1.0.0" + }, + { + "name": "onap.policies.optimization.service.QueryPolicy", + "version": "1.0.0" + }, + { + "name": "onap.policies.optimization.service.SubscriberPolicy", + "version": "1.0.0" + }, + { + "name": "onap.policies.optimization.resource.Vim_fit", + "version": "1.0.0" + }, + { + "name": "onap.policies.optimization.resource.VnfPolicy", + "version": "1.0.0" + }, + { + "name": "onap.policies.native.Xacml", + "version": "1.0.0" + }, + { + "name": "onap.policies.Naming", + "version": "1.0.0" + }, + { + "name": "onap.policies.match.*", + "version": "1.0.0" + } + ], + "currentInstanceCount": 0, + "desiredInstanceCount": 1, + "policies": [] + }, + { + "pdpType": "drools", + "supportedPolicyTypes": [ + { + "name": "onap.policies.controlloop.operational.common.Drools", + "version": "1.0.0" + } + ], + "currentInstanceCount": 0, + "desiredInstanceCount": 1, + "policies": [] + }, + { + "pdpType": "apex", + "supportedPolicyTypes": [ + { + "name": "onap.policies.controlloop.operational.common.Apex", + "version": "1.0.0" + } + ], + "policies": [ + { + "name": "onap.policies.controlloop.operational.common.apex.SampleDomain", + "version": "1.0.0" + } + ], + "currentInstanceCount": 0, + "desiredInstanceCount": 1 + } + ] + } + ] +}
\ No newline at end of file |