From: liamfallon Date: Tue, 18 Feb 2020 17:44:18 +0000 (+0000) Subject: Add changes for safe delete in policy-models X-Git-Tag: 2.2.0~10 X-Git-Url: https://gerrit.onap.org/r/gitweb?a=commitdiff_plain;h=19286bf7b66750aba7192af363349db2a4d5323d;p=policy%2Fapi.git Add changes for safe delete in policy-models Issue-ID: POLICY-1402 Change-Id: I3f97fbb4c11105118857d354970d6a3de1385f2d Signed-off-by: liamfallon --- diff --git a/main/src/main/java/org/onap/policy/api/main/rest/provider/LegacyGuardPolicyProvider.java b/main/src/main/java/org/onap/policy/api/main/rest/provider/LegacyGuardPolicyProvider.java index 2a0874dc..80c57d36 100644 --- a/main/src/main/java/org/onap/policy/api/main/rest/provider/LegacyGuardPolicyProvider.java +++ b/main/src/main/java/org/onap/policy/api/main/rest/provider/LegacyGuardPolicyProvider.java @@ -3,6 +3,7 @@ * ONAP Policy API * ================================================================================ * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,25 +23,18 @@ package org.onap.policy.api.main.rest.provider; -import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedHashMap; -import java.util.List; import java.util.Map; import java.util.Map.Entry; + import javax.ws.rs.core.Response; import org.apache.commons.lang3.tuple.Pair; 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.pdp.concepts.PdpGroup; -import org.onap.policy.models.pdp.concepts.PdpGroupFilter; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier; import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput; import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** * Class to provide all kinds of legacy guard policy operations. @@ -49,17 +43,12 @@ import org.slf4j.LoggerFactory; */ public class LegacyGuardPolicyProvider extends CommonModelProvider { - private static final Logger LOGGER = LoggerFactory.getLogger(LegacyGuardPolicyProvider.class); - - private static final String INVALID_POLICY_VERSION = "legacy policy version is not an integer"; - private static final String LEGACY_MINOR_PATCH_SUFFIX = ".0.0"; private static final Map GUARD_POLICY_TYPE_MAP = new LinkedHashMap<>(); static { GUARD_POLICY_TYPE_MAP.put("guard.frequency.", new PfConceptKey("onap.policies.controlloop.guard.FrequencyLimiter:1.0.0")); - GUARD_POLICY_TYPE_MAP.put("guard.minmax.", - new PfConceptKey("onap.policies.controlloop.guard.MinMax:1.0.0")); + GUARD_POLICY_TYPE_MAP.put("guard.minmax.", new PfConceptKey("onap.policies.controlloop.guard.MinMax:1.0.0")); GUARD_POLICY_TYPE_MAP.put("guard.blacklist.", new PfConceptKey("onap.policies.controlloop.guard.Blacklist:1.0.0")); } @@ -82,9 +71,6 @@ public class LegacyGuardPolicyProvider extends CommonModelProvider { public Map fetchGuardPolicy(String policyId, String policyVersion) throws PfModelException { - if (policyVersion != null) { - validNumber(policyVersion, INVALID_POLICY_VERSION); - } return modelsProvider.getGuardPolicy(policyId, policyVersion); } @@ -100,8 +86,8 @@ public class LegacyGuardPolicyProvider extends CommonModelProvider { public Map, Map> fetchDeployedGuardPolicies(String policyId) throws PfModelException { - return collectDeployedPolicies( - policyId, getGuardPolicyType(policyId), modelsProvider::getGuardPolicy, Map::putAll, new HashMap<>()); + return collectDeployedPolicies(policyId, getGuardPolicyType(policyId), modelsProvider::getGuardPolicy, + Map::putAll, new HashMap<>()); } /** @@ -111,10 +97,8 @@ public class LegacyGuardPolicyProvider extends CommonModelProvider { * * @return the map of LegacyGuardPolicyOutput objectst */ - public Map createGuardPolicy(LegacyGuardPolicyInput body) - throws PfModelException { + public Map createGuardPolicy(LegacyGuardPolicyInput body) throws PfModelException { - validateGuardPolicyVersion(body); return modelsProvider.createGuardPolicy(body); } @@ -129,107 +113,9 @@ public class LegacyGuardPolicyProvider extends CommonModelProvider { public Map deleteGuardPolicy(String policyId, String policyVersion) throws PfModelException { - validNumber(policyVersion, INVALID_POLICY_VERSION); - validateDeleteEligibility(policyId, policyVersion); - return modelsProvider.deleteGuardPolicy(policyId, policyVersion); } - /** - * Validates whether specified policy can be deleted based on the rule that deployed policy cannot be deleted. - * - * @param policyId the ID of policy - * @param policyVersion the version of policy - * - * @throws PfModelException the PfModel parsing exception - */ - private void validateDeleteEligibility(String policyId, String policyVersion) throws PfModelException { - - List policies = new ArrayList<>(); - policies.add(new ToscaPolicyIdentifier(policyId, policyVersion + LEGACY_MINOR_PATCH_SUFFIX)); - PdpGroupFilter pdpGroupFilter = PdpGroupFilter.builder().policyList(policies).build(); - - List pdpGroups = modelsProvider.getFilteredPdpGroups(pdpGroupFilter); - - if (!pdpGroups.isEmpty()) { - throw new PfModelException(Response.Status.CONFLICT, - constructDeletePolicyViolationMessage(policyId, policyVersion, pdpGroups)); - } - } - - /** - * Validates the provided guard policy version in the payload. - * - * @param body the guard policy payload - * - * @throws PfModelException the PfModel parsing exception - */ - private void validateGuardPolicyVersion(LegacyGuardPolicyInput body) throws PfModelException { - - validateGuardPolicyVersionExist(body); - validateNoDuplicateVersionInDb(body); - } - - /** - * Validates that the guard policy has version specified. - * - * @param body the guard policy payload - * - * @throws PfModelException the PfModel parsing exception - */ - private void validateGuardPolicyVersionExist(LegacyGuardPolicyInput body) throws PfModelException { - - if (body.getPolicyVersion() == null) { - String errMsg = "mandatory field 'policy-version' is missing in the policy: " + body.getPolicyId(); - throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errMsg); - } - } - - /** - * Validates that there is no duplicate version already stored in the database. - * - * @param body the guard policy payload - * - * @throws PfModelException the PfModel parsing exception - */ - private void validateNoDuplicateVersionInDb(LegacyGuardPolicyInput body) throws PfModelException { - - try { - modelsProvider.getGuardPolicy(body.getPolicyId(), body.getPolicyVersion()); - } catch (PfModelRuntimeException exc) { - if (!hasSameGuardPolicyFound(body, exc)) { - return; - } - throw new PfModelException(exc.getErrorResponse().getResponseCode(), "unexpected runtime error", exc); - } - - // If it gets here, there is one duplicate version stored in the DB. - // Try to get the latest version and return it to the user. - Map latest = modelsProvider.getGuardPolicy(body.getPolicyId(), null); - final String[] versionArray = latest.values().iterator().next().getVersion().split("\\."); - String errMsg = "guard policy " + body.getPolicyId() + ":" + body.getPolicyVersion() - + " already exists; its latest version is " + versionArray[0]; - throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errMsg); - } - - /** - * Checks if the same guard policy found in the database. - * - * @param body the legacy guard policy payload - * @param exc the thrown runtime exception from policy model provider - * - * @return a boolean flag indicating the check result - */ - private boolean hasSameGuardPolicyFound(LegacyGuardPolicyInput body, PfModelRuntimeException exc) { - - if (exc.getErrorResponse().getResponseCode() == Response.Status.BAD_REQUEST - && exc.getErrorResponse().getErrorMessage().contains("no policy found")) { - LOGGER.debug("no duplicate policy {}:{} found in the DB", body.getPolicyId(), body.getPolicyVersion()); - return false; - } - return true; - } - /** * Retrieves guard policy type given guard policy ID. * diff --git a/main/src/main/java/org/onap/policy/api/main/rest/provider/LegacyOperationalPolicyProvider.java b/main/src/main/java/org/onap/policy/api/main/rest/provider/LegacyOperationalPolicyProvider.java index 6bffb2b0..a433e02c 100644 --- a/main/src/main/java/org/onap/policy/api/main/rest/provider/LegacyOperationalPolicyProvider.java +++ b/main/src/main/java/org/onap/policy/api/main/rest/provider/LegacyOperationalPolicyProvider.java @@ -3,6 +3,7 @@ * ONAP Policy API * ================================================================================ * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,18 +26,11 @@ package org.onap.policy.api.main.rest.provider; import java.util.ArrayList; import java.util.List; import java.util.Map; -import javax.ws.rs.core.Response; import org.apache.commons.lang3.tuple.Pair; 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.pdp.concepts.PdpGroup; -import org.onap.policy.models.pdp.concepts.PdpGroupFilter; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier; import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** * Class to provide all kinds of legacy operational policy operations. @@ -45,10 +39,6 @@ import org.slf4j.LoggerFactory; */ public class LegacyOperationalPolicyProvider extends CommonModelProvider { - private static final Logger LOGGER = LoggerFactory.getLogger(LegacyOperationalPolicyProvider.class); - - private static final String INVALID_POLICY_VERSION = "legacy policy version is not an integer"; - private static final String LEGACY_MINOR_PATCH_SUFFIX = ".0.0"; private static final PfConceptKey LEGACY_OPERATIONAL_TYPE = new PfConceptKey("onap.policies.controlloop.Operational", "1.0.0"); @@ -70,9 +60,6 @@ public class LegacyOperationalPolicyProvider extends CommonModelProvider { public LegacyOperationalPolicy fetchOperationalPolicy(String policyId, String policyVersion) throws PfModelException { - if (policyVersion != null) { - validNumber(policyVersion, INVALID_POLICY_VERSION); - } return modelsProvider.getOperationalPolicy(policyId, policyVersion); } @@ -88,8 +75,8 @@ public class LegacyOperationalPolicyProvider extends CommonModelProvider { public Map, List> fetchDeployedOperationalPolicies(String policyId) throws PfModelException { - return collectDeployedPolicies( - policyId, LEGACY_OPERATIONAL_TYPE, modelsProvider::getOperationalPolicy, List::add, new ArrayList<>(5)); + return collectDeployedPolicies(policyId, LEGACY_OPERATIONAL_TYPE, modelsProvider::getOperationalPolicy, + List::add, new ArrayList<>(5)); } /** @@ -101,7 +88,6 @@ public class LegacyOperationalPolicyProvider extends CommonModelProvider { */ public LegacyOperationalPolicy createOperationalPolicy(LegacyOperationalPolicy body) throws PfModelException { - validateOperationalPolicyVersion(body); return modelsProvider.createOperationalPolicy(body); } @@ -116,104 +102,6 @@ public class LegacyOperationalPolicyProvider extends CommonModelProvider { public LegacyOperationalPolicy deleteOperationalPolicy(String policyId, String policyVersion) throws PfModelException { - validNumber(policyVersion, INVALID_POLICY_VERSION); - validateDeleteEligibility(policyId, policyVersion); - return modelsProvider.deleteOperationalPolicy(policyId, policyVersion); } - - /** - * Validates whether specified policy can be deleted based on the rule that deployed policy cannot be deleted. - * - * @param policyId the ID of policy - * @param policyVersion the version of policy - * - * @throws PfModelException the PfModel parsing exception - */ - private void validateDeleteEligibility(String policyId, String policyVersion) throws PfModelException { - - List policies = new ArrayList<>(5); - policies.add(new ToscaPolicyIdentifier(policyId, policyVersion + LEGACY_MINOR_PATCH_SUFFIX)); - PdpGroupFilter pdpGroupFilter = PdpGroupFilter.builder().policyList(policies).build(); - - List pdpGroups = modelsProvider.getFilteredPdpGroups(pdpGroupFilter); - - if (!pdpGroups.isEmpty()) { - throw new PfModelException(Response.Status.CONFLICT, - constructDeletePolicyViolationMessage(policyId, policyVersion, pdpGroups)); - } - } - - /** - * Validates the specified version of the operational policy provided in the payload. - * - * @param body the operational policy payload - * - * @throws PfModelException on errors parsing PfModel - */ - private void validateOperationalPolicyVersion(LegacyOperationalPolicy body) throws PfModelException { - - validateOperationalPolicyVersionExist(body); - validateNoDuplicateVersionInDb(body); - } - - /** - * Validates whether the version of the operational policy is specified in the payload. - * - * @param body the operational policy payload - * - * @throws PfModelException on errors parsing PfModel - */ - private void validateOperationalPolicyVersionExist(LegacyOperationalPolicy body) throws PfModelException { - - if (body.getPolicyVersion() == null) { - String errMsg = "mandatory field 'policy-version' is missing in the policy: " + body.getPolicyId(); - throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errMsg); - } - } - - /** - * Validates that there is no duplicate version of the operational policy which is already stored in the database. - * - * @param body the operational policy payload - * - * @throws PfModelException on errors parsing PfModel - */ - private void validateNoDuplicateVersionInDb(LegacyOperationalPolicy body) throws PfModelException { - - try { - modelsProvider.getOperationalPolicy(body.getPolicyId(), body.getPolicyVersion()); - } catch (PfModelRuntimeException exc) { - if (!hasSameOperationalPolicyFound(body, exc)) { - return; - } - throw new PfModelException(exc.getErrorResponse().getResponseCode(), "unexpected runtime error", exc); - } - - // There is one duplicate version stored in the DB. - // Try to get the latest version - LegacyOperationalPolicy latest = modelsProvider.getOperationalPolicy(body.getPolicyId(), null); - final String[] versionArray = latest.getPolicyVersion().split("\\."); - String errMsg = "operational policy " + body.getPolicyId() + ":" + body.getPolicyVersion() - + " already exists; its latest version is " + versionArray[0]; - throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errMsg); - } - - /** - * Checks if the same operational policy found in the database. - * - * @param body the legacy operational policy payload - * @param exc the runtime exception thrown by policy model provider - * - * @return a boolean flag indicating the check result - */ - private boolean hasSameOperationalPolicyFound(LegacyOperationalPolicy body, PfModelRuntimeException exc) { - - if (exc.getErrorResponse().getResponseCode() == Response.Status.BAD_REQUEST - && exc.getErrorResponse().getErrorMessage().contains("no policy found")) { - LOGGER.debug("no duplicate policy {}:{} found in the DB", body.getPolicyId(), body.getPolicyVersion()); - return false; - } - return true; - } } \ No newline at end of file diff --git a/main/src/main/java/org/onap/policy/api/main/rest/provider/PolicyProvider.java b/main/src/main/java/org/onap/policy/api/main/rest/provider/PolicyProvider.java index 15e409c6..6b123952 100644 --- a/main/src/main/java/org/onap/policy/api/main/rest/provider/PolicyProvider.java +++ b/main/src/main/java/org/onap/policy/api/main/rest/provider/PolicyProvider.java @@ -32,12 +32,8 @@ import javax.ws.rs.core.Response; import org.apache.commons.lang3.tuple.Pair; import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfModelException; -import org.onap.policy.models.pdp.concepts.PdpGroup; -import org.onap.policy.models.pdp.concepts.PdpGroupFilter; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; /** @@ -168,8 +164,6 @@ public class PolicyProvider extends CommonModelProvider { public ToscaServiceTemplate deletePolicy(String policyTypeId, String policyTypeVersion, String policyId, String policyVersion) throws PfModelException { - validateDeleteEligibility(policyTypeId, policyTypeVersion, policyId, policyVersion); - ToscaServiceTemplate serviceTemplate = modelsProvider.deletePolicy(policyId, policyVersion); if (!hasPolicy(serviceTemplate)) { @@ -180,35 +174,6 @@ public class PolicyProvider extends CommonModelProvider { return serviceTemplate; } - /** - * Validates whether specified policy can be deleted based on the rule that deployed policy cannot be deleted. - * - * @param policyTypeId the ID of policy type - * @param policyTypeVersion the version of policy type - * @param policyId the ID of policy - * @param policyVersion the version of policy - * - * @throws PfModelException the PfModel parsing exception - */ - private void validateDeleteEligibility(String policyTypeId, String policyTypeVersion, String policyId, - String policyVersion) throws PfModelException { - - // TODO: Remove this method when delete validation is implemented in the tosca provider - List policyTypes = new ArrayList<>(1); - policyTypes.add(new ToscaPolicyTypeIdentifier(policyTypeId, policyTypeVersion)); - List policies = new ArrayList<>(1); - policies.add(new ToscaPolicyIdentifier(policyId, policyVersion)); - PdpGroupFilter pdpGroupFilter = - PdpGroupFilter.builder().policyTypeList(policyTypes).policyList(policies).build(); - - List pdpGroups = modelsProvider.getFilteredPdpGroups(pdpGroupFilter); - - if (!pdpGroups.isEmpty()) { - throw new PfModelException(Response.Status.CONFLICT, - constructDeletePolicyViolationMessage(policyId, policyVersion, pdpGroups)); - } - } - /** * Retrieves the specified version of the policy. * diff --git a/main/src/main/java/org/onap/policy/api/main/rest/provider/PolicyTypeProvider.java b/main/src/main/java/org/onap/policy/api/main/rest/provider/PolicyTypeProvider.java index d325f2c0..6c8e73e6 100644 --- a/main/src/main/java/org/onap/policy/api/main/rest/provider/PolicyTypeProvider.java +++ b/main/src/main/java/org/onap/policy/api/main/rest/provider/PolicyTypeProvider.java @@ -23,16 +23,9 @@ package org.onap.policy.api.main.rest.provider; -import java.util.ArrayList; -import java.util.List; -import java.util.Map.Entry; - import javax.ws.rs.core.Response; import org.onap.policy.models.base.PfModelException; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; @@ -107,7 +100,7 @@ public class PolicyTypeProvider extends CommonModelProvider { throw new PfModelException(Response.Status.BAD_REQUEST, "no policy types specified in the service template"); } - validatePolicyTypeVersionExist(body); + return modelsProvider.createPolicyTypes(body); } @@ -124,8 +117,6 @@ public class PolicyTypeProvider extends CommonModelProvider { public ToscaServiceTemplate deletePolicyType(String policyTypeId, String policyTypeVersion) throws PfModelException { - validateDeleteEligibility(policyTypeId, policyTypeVersion); - ToscaServiceTemplate serviceTemplate = modelsProvider.deletePolicyType(policyTypeId, policyTypeVersion); if (!hasPolicyType(serviceTemplate)) { @@ -136,50 +127,6 @@ public class PolicyTypeProvider extends CommonModelProvider { return serviceTemplate; } - /** - * Validates whether specified policy type can be deleted based on the rule that policy type parameterized by at - * least one policies cannot be deleted. - * - * @param policyTypeId the ID of policy type - * @param policyTypeVersion the version of policy type - * - * @throws PfModelException the PfModel parsing exception - */ - private void validateDeleteEligibility(String policyTypeId, String policyTypeVersion) throws PfModelException { - - ToscaPolicyFilter policyFilter = - ToscaPolicyFilter.builder().type(policyTypeId).typeVersion(policyTypeVersion).build(); - List policies = modelsProvider.getFilteredPolicyList(policyFilter); - if (!policies.isEmpty()) { - throw new PfModelException(Response.Status.CONFLICT, - constructDeletePolicyTypeViolationMessage(policyTypeId, policyTypeVersion, policies)); - } - } - - /** - * Validates that each policy type has a version specified in the payload. - * - * @param body the TOSCA service template payload to check against - * - * @throws PfModelException the PfModel parsing exception - */ - private void validatePolicyTypeVersionExist(ToscaServiceTemplate body) throws PfModelException { - - List invalidPolicyTypeNames = new ArrayList<>(); - for (Entry policyType : body.getPolicyTypes().entrySet()) { - if (!"tosca.policies.Root".equals(policyType.getValue().getDerivedFrom()) - && policyType.getValue().getVersion() == null) { - invalidPolicyTypeNames.add(policyType.getKey()); - } - } - - if (!invalidPolicyTypeNames.isEmpty()) { - String errorMsg = "mandatory 'version' field is missing in policy types: " - + String.join(", ", invalidPolicyTypeNames); - throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMsg); - } - } - /** * Retrieves the specified version of the policy type. * diff --git a/main/src/test/java/org/onap/policy/api/main/rest/TestApiRestServer.java b/main/src/test/java/org/onap/policy/api/main/rest/TestApiRestServer.java index 291bddde..d531a0ad 100644 --- a/main/src/test/java/org/onap/policy/api/main/rest/TestApiRestServer.java +++ b/main/src/test/java/org/onap/policy/api/main/rest/TestApiRestServer.java @@ -616,7 +616,7 @@ public class TestApiRestServer { Response rawResponse = deleteResource(POLICYTYPES_TCA_POLICIES_VCPE_VERSION1, mediaType); assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus()); ErrorResponse error = rawResponse.readEntity(ErrorResponse.class); - assertEquals("policies for onap.restart.tca:1.0.0 do not exist", error.getErrorMessage()); + assertEquals("no policies found", error.getErrorMessage()); } @Test diff --git a/main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyGuardPolicyProvider.java b/main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyGuardPolicyProvider.java index 237da680..342d17b1 100644 --- a/main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyGuardPolicyProvider.java +++ b/main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyGuardPolicyProvider.java @@ -58,6 +58,7 @@ import org.onap.policy.models.provider.PolicyModelsProviderParameters; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; +import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyContent; import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput; import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput; @@ -131,7 +132,7 @@ public class TestLegacyGuardPolicyProvider { assertThatThrownBy(() -> { guardPolicyProvider.fetchGuardPolicy("dummy", "dummy"); - }).hasMessage("legacy policy version is not an integer"); + }).hasMessageContaining("parameter \"version\": value \"dummy.0.0\", does not match regular expression"); assertThatCode(() -> { ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder @@ -165,11 +166,11 @@ public class TestLegacyGuardPolicyProvider { assertThatThrownBy(() -> { guardPolicyProvider.fetchGuardPolicy("guard.frequency.scaleout", "1.0.0"); - }).hasMessage("legacy policy version is not an integer"); + }).hasMessageContaining("parameter \"version\": value \"1.0.0.0.0\", does not match regular expression"); assertThatThrownBy(() -> { guardPolicyProvider.fetchGuardPolicy("guard.frequency.scaleout", "latest"); - }).hasMessage("legacy policy version is not an integer"); + }).hasMessageContaining("parameter \"version\": value \"latest.0.0\", does not match regular expression"); assertThatCode(() -> { guardPolicyProvider.deleteGuardPolicy("guard.frequency.scaleout", "1"); @@ -260,8 +261,7 @@ public class TestLegacyGuardPolicyProvider { // Test validateDeleteEligibility exception path(!pdpGroups.isEmpty()) assertThatThrownBy(() -> { guardPolicyProvider.deleteGuardPolicy(POLICY_NAME, POLICY_VERSION); - }).hasMessageContaining("policy with ID " + POLICY_NAME + ":" + POLICY_VERSION - + " cannot be deleted as it is deployed in pdp groups"); + }).hasMessageContaining("policy is in use, it is deployed in PDP group group subgroup type"); } catch (Exception exc) { fail("Test should not throw an exception"); } @@ -290,19 +290,26 @@ public class TestLegacyGuardPolicyProvider { createdPolicy.get("guard.frequency.scaleout").getType()); assertEquals("1.0.0", createdPolicy.get("guard.frequency.scaleout").getVersion()); - assertThatThrownBy(() -> { - String badPolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_WITH_NO_VERSION); - LegacyGuardPolicyInput badPolicyToCreate = - standardCoder.decode(badPolicyString, LegacyGuardPolicyInput.class); - guardPolicyProvider.createGuardPolicy(badPolicyToCreate); - }).hasMessage("mandatory field 'policy-version' is missing in the policy: guard.frequency.scaleout"); + String defaultPolicyVersionString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_WITH_NO_VERSION); + LegacyGuardPolicyInput defaultVersionPolicy = + standardCoder.decode(defaultPolicyVersionString, LegacyGuardPolicyInput.class); + createdPolicy = guardPolicyProvider.createGuardPolicy(defaultVersionPolicy); + assertEquals("1.0.0", createdPolicy.get("guard.frequency.scaleout.noversion").getVersion()); + + assertThatCode(() -> { + String duplicatePolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE); + LegacyGuardPolicyInput duplicatePolicyToCreate = + standardCoder.decode(duplicatePolicyString, LegacyGuardPolicyInput.class); + guardPolicyProvider.createGuardPolicy(duplicatePolicyToCreate); + }).doesNotThrowAnyException(); assertThatThrownBy(() -> { String duplicatePolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE); LegacyGuardPolicyInput duplicatePolicyToCreate = standardCoder.decode(duplicatePolicyString, LegacyGuardPolicyInput.class); + duplicatePolicyToCreate.setContent(new LegacyGuardPolicyContent()); guardPolicyProvider.createGuardPolicy(duplicatePolicyToCreate); - }).hasMessage("guard policy guard.frequency.scaleout:1 already exists; its latest version is 1"); + }).hasMessageContaining("INVALID:entity in incoming fragment does not equal existing entity"); } @Test @@ -374,7 +381,7 @@ public class TestLegacyGuardPolicyProvider { databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0).getPolicies().size()); assertThatThrownBy(() -> { guardPolicyProvider.deleteGuardPolicy("guard.frequency.scaleout", "1"); - }).hasMessageContaining("cannot be deleted as it is deployed in pdp groups"); + }).hasMessageContaining("policy is in use, it is deployed in PDP group group subgroup type"); } catch (Exception exc) { fail("Test should not throw an exception"); } @@ -384,11 +391,11 @@ public class TestLegacyGuardPolicyProvider { public void testDeleteGuardPolicy() { assertThatThrownBy(() -> { guardPolicyProvider.deleteGuardPolicy("dummy", null); - }).hasMessage("legacy policy version is not an integer"); + }).hasMessageMatching("^policyVersion is marked .*on.*ull but is null$"); assertThatThrownBy(() -> { guardPolicyProvider.deleteGuardPolicy("dummy", "1.0.0"); - }).hasMessage("legacy policy version is not an integer"); + }).hasMessageContaining("parameter \"version\": value \"1.0.0.0.0\", does not match regular expression"); assertThatCode(() -> { ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder diff --git a/main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyOperationalPolicyProvider.java b/main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyOperationalPolicyProvider.java index f9fc666d..c0084956 100644 --- a/main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyOperationalPolicyProvider.java +++ b/main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyOperationalPolicyProvider.java @@ -128,7 +128,7 @@ public class TestLegacyOperationalPolicyProvider { assertThatThrownBy(() -> { operationalPolicyProvider.fetchOperationalPolicy("dummy", "dummy"); - }).hasMessage("legacy policy version is not an integer"); + }).hasMessageContaining("parameter \"version\": value \"dummy.0.0\", does not match regular expression"); ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class); @@ -151,12 +151,12 @@ public class TestLegacyOperationalPolicyProvider { assertThatThrownBy(() -> { operationalPolicyProvider.fetchOperationalPolicy("operational.restart", "1.0.0"); - }).hasMessage("legacy policy version is not an integer"); + }).hasMessageContaining("parameter \"version\": value \"1.0.0.0.0\", does not match regular expression"); assertThatThrownBy(() -> { operationalPolicyProvider.fetchOperationalPolicy("operational.restart", "latest"); ; - }).hasMessage("legacy policy version is not an integer"); + }).hasMessageContaining("parameter \"version\": value \"latest.0.0\", does not match regular expression"); operationalPolicyProvider.deleteOperationalPolicy("operational.restart", "1"); policyTypeProvider.deletePolicyType("onap.policies.controlloop.Operational", "1.0.0"); @@ -246,8 +246,7 @@ public class TestLegacyOperationalPolicyProvider { // Test validateDeleteEligibility exception path(!pdpGroups.isEmpty()) assertThatThrownBy(() -> { operationalPolicyProvider.deleteOperationalPolicy(POLICY_NAME, POLICY_VERSION); - }).hasMessageContaining("policy with ID " + POLICY_NAME + ":" + POLICY_VERSION - + " cannot be deleted as it is deployed in pdp groups"); + }).hasMessageContaining("policy is in use, it is deployed in PDP group group subgroup type"); } catch (Exception exc) { fail("Test should not throw an exception"); } @@ -274,19 +273,26 @@ public class TestLegacyOperationalPolicyProvider { assertEquals("operational.restart", createdPolicy.getPolicyId()); assertTrue(createdPolicy.getContent().startsWith("controlLoop%3A%0A%20%20version%3A%202.0.0%0A%20%20")); - assertThatThrownBy(() -> { - String badPolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_WITH_NO_VERSION); - LegacyOperationalPolicy badPolicyToCreate = - standardCoder.decode(badPolicyString, LegacyOperationalPolicy.class); - operationalPolicyProvider.createOperationalPolicy(badPolicyToCreate); - }).hasMessage("mandatory field 'policy-version' is missing in the policy: operational.scaleout"); + String defaultPolicyVersionString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_WITH_NO_VERSION); + LegacyOperationalPolicy defaultPolicyVersionPolicy = + standardCoder.decode(defaultPolicyVersionString, LegacyOperationalPolicy.class); + createdPolicy = operationalPolicyProvider.createOperationalPolicy(defaultPolicyVersionPolicy); + assertEquals("1", createdPolicy.getPolicyVersion()); + + assertThatCode(() -> { + String duplicatePolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE); + LegacyOperationalPolicy duplicatePolicyToCreate = + standardCoder.decode(duplicatePolicyString, LegacyOperationalPolicy.class); + operationalPolicyProvider.createOperationalPolicy(duplicatePolicyToCreate); + }).doesNotThrowAnyException(); assertThatThrownBy(() -> { String duplicatePolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE); LegacyOperationalPolicy duplicatePolicyToCreate = standardCoder.decode(duplicatePolicyString, LegacyOperationalPolicy.class); + duplicatePolicyToCreate.setContent("some other content"); operationalPolicyProvider.createOperationalPolicy(duplicatePolicyToCreate); - }).hasMessage("operational policy operational.restart:1 already exists; its latest version is 1"); + }).hasMessageContaining("INVALID:entity in incoming fragment does not equal existing entity"); } @Test @@ -357,7 +363,7 @@ public class TestLegacyOperationalPolicyProvider { databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0).getPolicies().size()); assertThatThrownBy(() -> { operationalPolicyProvider.deleteOperationalPolicy(policyId, policyVersion); - }).hasMessageContaining("cannot be deleted as it is deployed in pdp groups"); + }).hasMessageContaining("policy is in use, it is deployed in PDP group group subgroup type"); } catch (Exception exc) { fail("Test should not throw an exception"); } @@ -368,11 +374,11 @@ public class TestLegacyOperationalPolicyProvider { assertThatThrownBy(() -> { operationalPolicyProvider.deleteOperationalPolicy("dummy", null); - }).hasMessage("legacy policy version is not an integer"); + }).hasMessageMatching("^policyVersion is marked .*on.*ull but is null$"); assertThatThrownBy(() -> { operationalPolicyProvider.deleteOperationalPolicy("dummy", "dummy"); - }).hasMessage("legacy policy version is not an integer"); + }).hasMessageContaining("parameter \"version\": value \"dummy.0.0\", does not match regular expression"); assertThatCode(() -> { ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder 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 index 31be84a9..d9b01fdb 100644 --- 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 @@ -236,8 +236,7 @@ public class TestPolicyProvider { assertThatThrownBy(() -> { policyProvider.deletePolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", "onap.restart.tca", "1.0.0"); - }).hasMessageContaining("policy with ID " + policyId + ":" + policyVersion - + " cannot be deleted as it is deployed in pdp groups"); + }).hasMessageContaining("policy is in use, it is deployed in PDP group group subgroup type"); } catch (Exception exc) { fail("Test should not throw an exception"); } @@ -430,6 +429,6 @@ public class TestPolicyProvider { assertThatThrownBy(() -> { policyProvider.deletePolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", "onap.restart.tca", "1.0.0"); - }).hasMessageContaining("policies for onap.restart.tca:1.0.0 do not exist"); + }).hasMessageContaining("no policies found"); } } 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 index 3c64e640..82d25245 100644 --- 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 @@ -158,7 +158,7 @@ public class TestPolicyTypeProvider { standardYamlCoder.decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_WITH_NO_VERSION), ToscaServiceTemplate.class); policyTypeProvider.createPolicyType(badPolicyType); - }).hasMessage("mandatory 'version' field is missing in policy types: onap.policies.optimization.Resource"); + }).hasMessageContaining("INVALID:key version is a null version"); policyTypeProvider.deletePolicyType(POLICY_TYPE_NAME_MONITORING, POLICY_TYPE_VERSION); } @@ -204,8 +204,8 @@ public class TestPolicyTypeProvider { .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 with ID onap.policies.monitoring.cdap.tca.hi.lo.app:1.0.0 " - + "cannot be deleted as it is parameterized by policies onap.restart.tca:1.0.0"; + String exceptionMessage = "policy type onap.policies.monitoring.cdap.tca.hi.lo.app:1.0.0 is in use, " + + "it is referenced in policy onap.restart.tca:1.0.0"; assertThatThrownBy(() -> { policyTypeProvider.deletePolicyType("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0"); }).hasMessage(exceptionMessage); @@ -219,6 +219,6 @@ public class TestPolicyTypeProvider { assertThatThrownBy(() -> { policyTypeProvider.deletePolicyType("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0"); - }).hasMessage("policy types for onap.policies.monitoring.cdap.tca.hi.lo.app:1.0.0 do not exist"); + }).hasMessage("policy type onap.policies.monitoring.cdap.tca.hi.lo.app:1.0.0 not found"); } } diff --git a/main/src/test/resources/policies/vDNS.policy.guard.frequency.no.policyversion.json b/main/src/test/resources/policies/vDNS.policy.guard.frequency.no.policyversion.json index 346765ef..518f09e8 100644 --- a/main/src/test/resources/policies/vDNS.policy.guard.frequency.no.policyversion.json +++ b/main/src/test/resources/policies/vDNS.policy.guard.frequency.no.policyversion.json @@ -1,5 +1,5 @@ { - "policy-id" : "guard.frequency.scaleout", + "policy-id" : "guard.frequency.scaleout.noversion", "content" : { "actor": "SO", "recipe": "VF Module Create",