diff options
Diffstat (limited to 'src/test')
4 files changed, 364 insertions, 187 deletions
diff --git a/src/test/java/org/onap/policy/clamp/policy/pdpgroup/PdpGroupAnalyzerTest.java b/src/test/java/org/onap/policy/clamp/policy/pdpgroup/PdpGroupAnalyzerTest.java index 1af048da5..5a9cac07b 100644 --- a/src/test/java/org/onap/policy/clamp/policy/pdpgroup/PdpGroupAnalyzerTest.java +++ b/src/test/java/org/onap/policy/clamp/policy/pdpgroup/PdpGroupAnalyzerTest.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * ONAP CLAMP + * ONAP POLICY-CLAMP * ================================================================================ * Copyright (C) 2021 AT&T Intellectual Property. All rights * reserved. @@ -25,19 +25,15 @@ package org.onap.policy.clamp.policy.pdpgroup; import static org.assertj.core.api.Assertions.assertThat; -import com.google.gson.Gson; -import com.google.gson.JsonObject; import java.util.Arrays; -import java.util.LinkedList; -import java.util.List; -import javax.transaction.Transactional; +import org.junit.BeforeClass; import org.junit.Test; -import org.onap.policy.clamp.clds.util.JsonUtils; import org.onap.policy.clamp.loop.template.PolicyModel; import org.onap.policy.models.pdp.concepts.PdpGroup; import org.onap.policy.models.pdp.concepts.PdpGroups; import org.onap.policy.models.pdp.concepts.PdpSubGroup; import org.onap.policy.models.pdp.enums.PdpState; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; /** @@ -45,33 +41,13 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifi */ public class PdpGroupAnalyzerTest { - private PolicyModel createPolicyModel(String policyType, String policyModelTosca, String version, - String policyAcronym, String policyVariant, String createdBy) { - PolicyModel policyModel = new PolicyModel(); - policyModel.setCreatedBy(createdBy); - policyModel.setPolicyAcronym(policyAcronym); - policyModel.setPolicyModelTosca(policyModelTosca); - policyModel.setPolicyModelType(policyType); - policyModel.setUpdatedBy(createdBy); - policyModel.setVersion(version); - return policyModel; - } + private static PdpGroups pdpGroups; /** - * This tests the pdpgroup GSON encode/decode and saving. + * This method preloads the pdpGroups for the tests. */ - @Test - @Transactional - public void testUpdatePdpGroup() { - // Create policyModel - PolicyModel policyModel = new PolicyModel(); - policyModel.setCreatedBy("user"); - policyModel.setPolicyAcronym("TEST"); - policyModel.setPolicyModelTosca("yaml"); - policyModel.setPolicyModelType("org.onap.test"); - policyModel.setUpdatedBy("user"); - policyModel.setVersion("1.0.0"); - + @BeforeClass + public static void setupPdpGroup() { // Create Pdp Groups // Those that do not work first PdpSubGroup pdpSubgroupBad = new PdpSubGroup(); @@ -83,7 +59,11 @@ public class PdpGroupAnalyzerTest { new ToscaPolicyTypeIdentifier("org.onap.tes", "1.0.0"), new ToscaPolicyTypeIdentifier("org.onap", "1.0.0") )); - + pdpSubgroupBad.setPolicies(Arrays.asList(new ToscaPolicyIdentifier("org.onap", "1.0.0"))); + pdpSubgroupBad.setPolicies(Arrays.asList(new ToscaPolicyIdentifier("org.onap.testos", "2.0.0"))); + pdpSubgroupBad.setPolicies(Arrays.asList(new ToscaPolicyIdentifier("org.onap.testos", "1.0.1"))); + pdpSubgroupBad.setPolicies(Arrays.asList(new ToscaPolicyIdentifier("org.onap.testos.new", "1.0.0"))); + pdpSubgroupBad.setPolicies(Arrays.asList(new ToscaPolicyIdentifier("org.onap.*", "1.0.0"))); PdpSubGroup pdpSubgroup1 = new PdpSubGroup(); pdpSubgroup1.setPdpType("subGroup1"); @@ -93,38 +73,70 @@ public class PdpGroupAnalyzerTest { PdpSubGroup pdpSubgroup2 = new PdpSubGroup(); pdpSubgroup2.setPdpType("subGroup2"); pdpSubgroup2.setSupportedPolicyTypes(Arrays.asList(new ToscaPolicyTypeIdentifier("org.onap.test", "1.0.0"))); + pdpSubgroup2.setPolicies(Arrays.asList(new ToscaPolicyIdentifier("org.onap.testos", "1.0.0"))); PdpSubGroup pdpSubgroup3 = new PdpSubGroup(); pdpSubgroup3.setPdpType("subGroup3"); pdpSubgroup3.setSupportedPolicyTypes(Arrays.asList(new ToscaPolicyTypeIdentifier("org.onap.test*", "1.0.0"))); + pdpSubgroup3.setPolicies(Arrays.asList(new ToscaPolicyIdentifier("org.onap.testos", "2.0.0"))); - + // Should match pdpSubgroup1 PdpGroup pdpGroup1 = new PdpGroup(); pdpGroup1.setName("pdpGroup1"); pdpGroup1.setPdpGroupState(PdpState.ACTIVE); pdpGroup1.setPdpSubgroups(Arrays.asList(pdpSubgroup1, pdpSubgroupBad)); + // Should match pdpSubgroup1, pdpSubgroup2, pdpSubgroup3 + // Should match also for the policy (pdpSubgroup2) PdpGroup pdpGroup2 = new PdpGroup(); pdpGroup2.setName("pdpGroup2"); pdpGroup2.setPdpGroupState(PdpState.ACTIVE); pdpGroup2.setPdpSubgroups(Arrays.asList(pdpSubgroup1, pdpSubgroup2, pdpSubgroup3, pdpSubgroupBad)); + /// Should not match PdpGroup pdpGroup3 = new PdpGroup(); pdpGroup3.setName("pdpGroup3"); pdpGroup3.setPdpGroupState(PdpState.ACTIVE); pdpGroup3.setPdpSubgroups(Arrays.asList(pdpSubgroupBad)); + // Should not match PdpGroup pdpGroup4 = new PdpGroup(); pdpGroup4.setName("pdpGroup4"); pdpGroup4.setPdpGroupState(PdpState.PASSIVE); pdpGroup4.setPdpSubgroups(Arrays.asList(pdpSubgroup1)); - PdpGroups pdpGroups = new PdpGroups(); + pdpGroups = new PdpGroups(); pdpGroups.setGroups(Arrays.asList(pdpGroup1, pdpGroup2, pdpGroup3, pdpGroup4)); - PdpGroupsAnalyzer.updatePdpGroup(Arrays.asList(policyModel), pdpGroups); + } + + @Test + public void testUpdatePdpGroupOfPolicyModels() { + // Create policyModel + PolicyModel policyModel = new PolicyModel(); + policyModel.setCreatedBy("user"); + policyModel.setPolicyAcronym("TEST"); + policyModel.setPolicyModelTosca("yaml"); + policyModel.setPolicyModelType("org.onap.test"); + policyModel.setUpdatedBy("user"); + policyModel.setVersion("1.0.0"); + PdpGroupsAnalyzer.updatePdpGroupOfPolicyModels(Arrays.asList(policyModel), pdpGroups); assertThat(policyModel.getPolicyPdpGroup().toString()).isEqualTo( "{\"supportedPdpGroups\":[{\"pdpGroup1\":[\"subGroup1\"]}," + "{\"pdpGroup2\":[\"subGroup1\",\"subGroup2\",\"subGroup3\"]}]}"); } + + @Test + public void testGetPdpGroupDeploymentsOfOnePolicy() { + assertThat( + PdpGroupsAnalyzer.getPdpGroupDeploymentOfOnePolicy(pdpGroups, "org.onap.testos", "1.0.0").toString()) + .isEqualTo("{\"pdpGroupInfo\":{\"pdpGroup\":\"pdpGroup2\",\"pdpSubGroup\":\"subGroup2\"}}"); + } + + @Test + public void testGetPdpGroupDeploymentsOfOnePolicyNull() { + assertThat( + PdpGroupsAnalyzer.getPdpGroupDeploymentOfOnePolicy(pdpGroups, "org.onap.DoNotExist", "1.0.0")) + .isNull(); + } } diff --git a/src/test/java/org/onap/policy/clamp/policy/pdpgroup/PoliciesPdpMergerTest.java b/src/test/java/org/onap/policy/clamp/policy/pdpgroup/PoliciesPdpMergerTest.java new file mode 100644 index 000000000..b3fd3f19a --- /dev/null +++ b/src/test/java/org/onap/policy/clamp/policy/pdpgroup/PoliciesPdpMergerTest.java @@ -0,0 +1,102 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP POLICY-CLAMP + * ================================================================================ + * Copyright (C) 2021 AT&T Intellectual Property. 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. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.policy.clamp.policy.pdpgroup; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.IOException; +import java.util.Arrays; +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.policy.clamp.clds.util.JsonUtils; +import org.onap.policy.clamp.clds.util.ResourceFileUtils; +import org.onap.policy.models.pdp.concepts.PdpGroup; +import org.onap.policy.models.pdp.concepts.PdpGroups; +import org.onap.policy.models.pdp.concepts.PdpSubGroup; +import org.onap.policy.models.pdp.enums.PdpState; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; +import org.skyscreamer.jsonassert.JSONAssert; + +/** + * This test class validates the PdpGroupAnalyzer class. + */ +public class PoliciesPdpMergerTest { + + private static String pdpGroupsJson; + + /** + * This method setups the pdpGroups required for the tests. + */ + @BeforeClass + public static void setupPdpGroup() { + // Create Pdp Groups + PdpSubGroup pdpSubgroup1 = new PdpSubGroup(); + pdpSubgroup1.setPdpType("subGroup1"); + pdpSubgroup1.setSupportedPolicyTypes( + Arrays.asList(new ToscaPolicyTypeIdentifier("org.onap.*", "1.0.0"))); + + PdpSubGroup pdpSubgroup2 = new PdpSubGroup(); + pdpSubgroup2.setPdpType("subGroup2"); + pdpSubgroup2.setSupportedPolicyTypes( + Arrays.asList(new ToscaPolicyTypeIdentifier("onap.policies.monitoring.tcagen2", "1.0.0"), + new ToscaPolicyTypeIdentifier("onap.policies.controlloop.operational.common.Drools", "1.0.0"))); + pdpSubgroup2.setPolicies(Arrays.asList( + new ToscaPolicyIdentifier("MICROSERVICE_vLoadBalancerMS_v1_0_tcagen2_1_0_0_AV0", "1.0.0"))); + + PdpSubGroup pdpSubgroup3 = new PdpSubGroup(); + pdpSubgroup3.setPdpType("subGroup3"); + pdpSubgroup3.setSupportedPolicyTypes( + Arrays.asList(new ToscaPolicyTypeIdentifier("onap.policies.monitoring.tcagen2", "1.0.0"), + new ToscaPolicyTypeIdentifier("onap.policies.controlloop.operational.common.Drools", "1.0.0"))); + pdpSubgroup3.setPolicies(Arrays.asList(new ToscaPolicyIdentifier("org.onap.testos", "2.0.0"), + new ToscaPolicyIdentifier("OPERATIONAL_vLoadBalancerMS_v1_0_Drools_1_0_0_7xd", "1.0.0"))); + + // Should match pdpSubgroup1 + PdpGroup pdpGroup1 = new PdpGroup(); + pdpGroup1.setName("pdpGroup1"); + pdpGroup1.setPdpGroupState(PdpState.ACTIVE); + pdpGroup1.setPdpSubgroups(Arrays.asList(pdpSubgroup1)); + + // Should match pdpSubgroup1, pdpSubgroup2, pdpSubgroup3 + // Should match also for the policy (pdpSubgroup2) + PdpGroup pdpGroup2 = new PdpGroup(); + pdpGroup2.setName("pdpGroup2"); + pdpGroup2.setPdpGroupState(PdpState.ACTIVE); + pdpGroup2.setPdpSubgroups(Arrays.asList(pdpSubgroup1, pdpSubgroup2, pdpSubgroup3)); + + PdpGroups pdpGroups = new PdpGroups(); + pdpGroups.setGroups(Arrays.asList(pdpGroup1, pdpGroup2)); + + pdpGroupsJson = JsonUtils.GSON.toJson(pdpGroups); + } + + @Test + public void testUpdatePdpGroupOfPolicyModels() throws IOException { + JSONAssert.assertEquals(ResourceFileUtils.getResourceAsString("clds/policy-merger.json"), PoliciesPdpMerger + .mergePoliciesAndPdpGroupStates( + ResourceFileUtils.getResourceAsString("http-cache/example/policy/api/v1/policies/.file"), + pdpGroupsJson), true); + } +} diff --git a/src/test/resources/clds/policy-merger.json b/src/test/resources/clds/policy-merger.json new file mode 100644 index 000000000..5c6bd5b6b --- /dev/null +++ b/src/test/resources/clds/policy-merger.json @@ -0,0 +1,215 @@ +{ + "policies": [ + { + "MICROSERVICE_vLoadBalancerMS_v1_0_tcagen2_1_0_0_AV0": { + "type": "onap.policies.monitoring.tcagen2", + "type_version": "1.0.0", + "properties": { + "tca.policy": { + "domain": "measurementsForVfScaling", + "metricsPerEventName": [ + { + "policyScope": "DCAE", + "thresholds": [ + { + "version": "1.0.2", + "severity": "MAJOR", + "thresholdValue": 200, + "closedLoopEventStatus": "ONSET", + "closedLoopControlName": "LOOP_test", + "direction": "LESS_OR_EQUAL", + "fieldPath": "$.event.measurementsForVfScalingFields.vNicPerformanceArray[*].receivedTotalPacketsDelta" + } + ], + "eventName": "vLoadBalancer", + "policyVersion": "v0.0.1", + "controlLoopSchemaType": "VM", + "policyName": "DCAE.Config_tca-hi-lo" + } + ] + } + }, + "name": "MICROSERVICE_vLoadBalancerMS_v1_0_tcagen2_1_0_0_AV0", + "version": "1.0.0", + "metadata": { + "policy-id": "MICROSERVICE_vLoadBalancerMS_v1_0_tcagen2_1_0_0_AV0", + "policy-version": "1.0.0" + }, + "pdpGroupInfo": { + "pdpGroup": "pdpGroup2", + "pdpSubGroup": "subGroup2" + }, + "supportedPdpGroups": [ + { + "pdpGroup2": [ + "subGroup2", + "subGroup3" + ] + } + ] + } + }, + { + "OPERATIONAL_vLoadBalancerMS_v1_0_Drools_1_0_0_7xd": { + "type": "onap.policies.controlloop.operational.common.Drools", + "type_version": "1.0.0", + "properties": { + "abatement": false, + "operations": [ + { + "failure_retries": "final_failure_retries", + "id": "test1", + "failure_timeout": "final_failure_timeout", + "failure": "final_failure", + "operation": { + "payload": { + "artifact_name": "baseconfiguration", + "artifact_version": "1.0.0", + "mode": "async", + "data": "{\"resource-assignment-properties\":{\"request-id\":\"\",\"service-instance-id\":\"\",\"hostname\":\"\",\"request-info\":{\"prop1\":\"\",\"prop2\":\"\"}}}" + }, + "target": { + "entityIds": { + "resourceID": "Vloadbalancerms..vdns..module-3", + "modelInvariantId": "4c10ba9b-f88f-415e-9de3-5d33336047fa", + "modelVersionId": "4fa73b49-8a6c-493e-816b-eb401567b720", + "modelName": "Vloadbalancerms..vdns..module-3", + "modelVersion": "1", + "modelCustomizationId": "bafcdab0-801d-4d81-9ead-f464640a38b1" + }, + "targetType": "VNF" + }, + "actor": "SDNR", + "operation": "BandwidthOnDemand" + }, + "failure_guard": "final_failure_guard", + "retries": 0, + "timeout": 0, + "failure_exception": "final_failure_exception", + "description": "test", + "success": "final_success" + } + ], + "trigger": "test1", + "timeout": 0, + "id": "LOOP_test" + }, + "name": "OPERATIONAL_vLoadBalancerMS_v1_0_Drools_1_0_0_7xd", + "version": "1.0.0", + "metadata": { + "policy-id": "OPERATIONAL_vLoadBalancerMS_v1_0_Drools_1_0_0_7xd", + "policy-version": "1.0.0" + }, + "pdpGroupInfo": { + "pdpGroup": "pdpGroup2", + "pdpSubGroup": "subGroup3" + }, + "supportedPdpGroups": [ + { + "pdpGroup2": [ + "subGroup2", + "subGroup3" + ] + } + ] + } + }, + { + "SDNC_Policy.ONAP_NF_NAMING_TIMESTAMP": { + "type": "onap.policies.Naming", + "type_version": "1.0.0", + "properties": { + "naming-models": [ + { + "naming-type": "VNF", + "naming-recipe": "AIC_CLOUD_REGION|DELIMITER|CONSTANT|DELIMITER|TIMESTAMP", + "name-operation": "to_lower_case()", + "naming-properties": [ + { + "property-name": "AIC_CLOUD_REGION" + }, + { + "property-name": "CONSTANT", + "property-value": "onap-nf" + }, + { + "property-name": "TIMESTAMP" + }, + { + "property-value": "-", + "property-name": "DELIMITER" + } + ] + }, + { + "naming-type": "VNFC", + "naming-recipe": "VNF_NAME|DELIMITER|NFC_NAMING_CODE|DELIMITER|SEQUENCE", + "name-operation": "to_lower_case()", + "naming-properties": [ + { + "property-name": "VNF_NAME" + }, + { + "property-name": "SEQUENCE", + "increment-sequence": { + "max": "zzz", + "scope": "ENTIRETY", + "start-value": "1", + "length": "3", + "increment": "1", + "sequence-type": "alpha-numeric" + } + }, + { + "property-name": "NFC_NAMING_CODE" + }, + { + "property-value": "-", + "property-name": "DELIMITER" + } + ] + }, + { + "naming-type": "VF-MODULE", + "naming-recipe": "VNF_NAME|DELIMITER|VF_MODULE_LABEL|DELIMITER|VF_MODULE_TYPE|DELIMITER|SEQUENCE", + "name-operation": "to_lower_case()", + "naming-properties": [ + { + "property-name": "VNF_NAME" + }, + { + "property-value": "-", + "property-name": "DELIMITER" + }, + { + "property-name": "VF_MODULE_LABEL" + }, + { + "property-name": "VF_MODULE_TYPE" + }, + { + "property-name": "SEQUENCE", + "increment-sequence": { + "max": "zzz", + "scope": "PRECEEDING", + "start-value": "1", + "length": "3", + "increment": "1", + "sequence-type": "alpha-numeric" + } + } + ] + } + ], + "policy-instance-name": "ONAP_NF_NAMING_TIMESTAMP" + }, + "name": "SDNC_Policy.ONAP_NF_NAMING_TIMESTAMP", + "version": "1.0.0", + "metadata": { + "policy-id": "SDNC_Policy.ONAP_NF_NAMING_TIMESTAMP", + "policy-version": "1.0.0" + } + } + } + ] +}
\ No newline at end of file diff --git a/src/test/resources/http-cache/example/policy/pap/v1/pdps/test.json b/src/test/resources/http-cache/example/policy/pap/v1/pdps/test.json deleted file mode 100644 index c22508441..000000000 --- a/src/test/resources/http-cache/example/policy/pap/v1/pdps/test.json +++ /dev/null @@ -1,152 +0,0 @@ -{ - "groups": [ - { - "name": "defaultGroup", - "description": "The default group that registers all supported policy types and pdps.", - "pdpGroupState": "ACTIVE", - "properties": {}, - "pdpSubgroups": [ - { - "pdpType": "apex", - "supportedPolicyTypes": [ - { - "name": "onap.policies.controlloop.operational.common.Apex", - "version": "1.0.0" - }, - { - "name": "onap.policies.native.Apex", - "version": "1.0.0" - } - ], - "policies": [], - "currentInstanceCount": 0, - "desiredInstanceCount": 1, - "properties": {}, - "pdpInstances": [] - }, - { - "pdpType": "drools", - "supportedPolicyTypes": [ - { - "name": "onap.policies.controlloop.operational.common.Drools", - "version": "1.0.0" - }, - { - "name": "onap.policies.native.drools.Controller", - "version": "1.0.0" - }, - { - "name": "onap.policies.native.drools.Artifact", - "version": "1.0.0" - } - ], - "policies": [ - { - "name": "OPERATIONAL_vLoadBalancerMS_v1_0_Drools_1_0_0_7xd", - "version": "1.0.0" - } - ], - "currentInstanceCount": 0, - "desiredInstanceCount": 1, - "properties": {}, - "pdpInstances": [] - }, - { - "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" - } - ], - "policies": [ - { - "name": "SDNC_Policy.ONAP_NF_NAMING_TIMESTAMP", - "version": "1.0.0" - }, - { - "name": "MICROSERVICE_vLoadBalancerMS_v1_0_tcagen2_1_0_0_AV0", - "version": "1.0.0" - } - ], - "currentInstanceCount": 0, - "desiredInstanceCount": 1, - "properties": {}, - "pdpInstances": [] - } - ] - } - ] -}
\ No newline at end of file |