From 172b8dcd3a772c03ec1d233cfd6602d901c61b99 Mon Sep 17 00:00:00 2001 From: Pamela Dragosh Date: Thu, 5 Mar 2020 07:55:57 -0500 Subject: TOSCA Compliant Guard Policies Fixing the legacy guard policies and renaming them so we are able to differentiate them. Adding newer, cleaner TOSCA Guard Policies that match the operational guard policies. Removing legacy guard policies. Fixing JUnit so that they don't use indexes to test whether versions are changed correctly. Added back in new guard policies. Fixed the guard policy types to use camel case. Issue-ID: POLICY-2243 Change-Id: Ie611f26f73f41e64c0b467f524f470739158f437 Signed-off-by: Pamela Dragosh --- .../legacy/concepts/LegacyGuardPolicyContent.java | 112 ------ .../legacy/concepts/LegacyGuardPolicyInput.java | 46 --- .../legacy/concepts/LegacyGuardPolicyOutput.java | 38 -- .../legacy/mapping/LegacyGuardPolicyMapper.java | 165 --------- .../mapping/LegacyOperationalPolicyMapper.java | 2 +- .../tosca/legacy/provider/LegacyProvider.java | 97 ------ .../simple/concepts/JpaToscaServiceTemplate.java | 2 +- .../concepts/ToscaPolicyFilterTest.java | 81 ++++- .../concepts/ToscaPolicyTypeFilterTest.java | 18 +- .../legacy/concepts/LegacyGuardPolicyTest.java | 54 --- .../DummyBadLegacyGuardPolicyContent.java | 34 -- .../mapping/LegacyGuardPolicyMapperTest.java | 70 ---- .../provider/LegacyProvider4LegacyGuardTest.java | 388 --------------------- .../MonitoringPolicySerializationTest.java | 2 +- .../MonitoringPolicyTypeSerializationTest.java | 4 +- .../test/resources/onap.policies.NoVersion.yaml | 2 +- 16 files changed, 82 insertions(+), 1033 deletions(-) delete mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyContent.java delete mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyInput.java delete mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyOutput.java delete mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapper.java delete mode 100644 models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyTest.java delete mode 100644 models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/testconcepts/DummyBadLegacyGuardPolicyContent.java delete mode 100644 models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapperTest.java delete mode 100644 models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyGuardTest.java (limited to 'models-tosca') diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyContent.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyContent.java deleted file mode 100644 index 91ff150fc..000000000 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyContent.java +++ /dev/null @@ -1,112 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. - * ================================================================================ - * 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.models.tosca.legacy.concepts; - -import java.lang.reflect.Field; -import java.util.HashMap; -import java.util.Map; - -import javax.ws.rs.core.Response; - -import lombok.Data; - -import org.onap.policy.common.utils.coder.StandardCoder; -import org.onap.policy.models.base.PfModelRuntimeException; -import org.onap.policy.models.tosca.legacy.mapping.LegacyGuardPolicyMapper; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Content object of a Legacy Guard Policy. - * - * @author Liam Fallon (liam.fallon@est.tech) - */ -@Data -public class LegacyGuardPolicyContent { - private static final Logger LOGGER = LoggerFactory.getLogger(LegacyGuardPolicyMapper.class); - - private String actor; - private String recipe; - private String targets; - private String clname; - private String limit; - private String timeWindow; - private String timeUnits; - private String min; - private String max; - private String guardActiveStart; - private String guardActiveEnd; - - /** - * Get contents as a property map. - * - * @return the contents as a map. - */ - public Map getAsPropertyMap() { - final Map propertyMap = new HashMap<>(); - - final StandardCoder coder = new StandardCoder(); - - try { - for (Field field : this.getClass().getDeclaredFields()) { - if (field.get(this) != null && field.getType().equals(String.class)) { - propertyMap.put(field.getName(), coder.encode(field.get(this))); - } - } - } catch (Exception exc) { - String errorMessage = "could not convert content to a property map"; - LOGGER.warn(errorMessage, exc); - throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage, exc); - - } - - return propertyMap; - } - - /** - * Set the contents from a property map. - * - * @param propertyMap the incoming property map - */ - public void setContent(final Map propertyMap) { - final StandardCoder coder = new StandardCoder(); - - try { - // @formatter:off - setActor( coder.decode(propertyMap.get("actor"), String.class)); - setClname( coder.decode(propertyMap.get("clname"), String.class)); - setGuardActiveEnd( coder.decode(propertyMap.get("guardActiveEnd"), String.class)); - setGuardActiveStart(coder.decode(propertyMap.get("guardActiveStart"), String.class)); - setLimit( coder.decode(propertyMap.get("limit"), String.class)); - setMax( coder.decode(propertyMap.get("max"), String.class)); - setMin( coder.decode(propertyMap.get("min"), String.class)); - setRecipe( coder.decode(propertyMap.get("recipe"), String.class)); - setTargets( coder.decode(propertyMap.get("targets"), String.class)); - setTimeUnits( coder.decode(propertyMap.get("timeUnits"), String.class)); - setTimeWindow( coder.decode(propertyMap.get("timeWindow"), String.class)); - // @formatter:on - } catch (Exception exc) { - String errorMessage = "could not convert content to a property map"; - LOGGER.warn(errorMessage, exc); - throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage, exc); - } - } -} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyInput.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyInput.java deleted file mode 100644 index 819fcba75..000000000 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyInput.java +++ /dev/null @@ -1,46 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. - * Modifications Copyright (C) 2019 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.models.tosca.legacy.concepts; - -import com.google.gson.annotations.SerializedName; -import io.swagger.annotations.ApiModelProperty; -import lombok.Data; - -/** - * Definition of a legacy guard policy stored as a TOSCA policy. - * - * @author Liam Fallon (liam.fallon@est.tech) - */ -@Data -public class LegacyGuardPolicyInput { - - @ApiModelProperty(name = "policy-id") - @SerializedName("policy-id") - private String policyId; - - @ApiModelProperty(name = "policy-version") - @SerializedName("policy-version") - private String policyVersion; - - private LegacyGuardPolicyContent content; - -} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyOutput.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyOutput.java deleted file mode 100644 index 2fe3d88e4..000000000 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyOutput.java +++ /dev/null @@ -1,38 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. - * ================================================================================ - * 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.models.tosca.legacy.concepts; - -import java.util.Map; - -import lombok.Data; - -/** - * Body of a legacy guard policy output. - * - * @author Liam Fallon (liam.fallon@est.tech) - */ -@Data -public class LegacyGuardPolicyOutput { - private String type; - private String version; - private Map metadata; - private Map properties; -} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapper.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapper.java deleted file mode 100644 index b7ebdcec3..000000000 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapper.java +++ /dev/null @@ -1,165 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019-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. - * 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.models.tosca.legacy.mapping; - -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.Map.Entry; - -import javax.ws.rs.core.Response; - -import org.onap.policy.models.base.PfConceptKey; -import org.onap.policy.models.base.PfModelRuntimeException; -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; -import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies; -import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy; -import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate; -import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate; -import org.onap.policy.models.tosca.simple.mapping.JpaToscaServiceTemplateMapper; -import org.onap.policy.models.tosca.utils.ToscaUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * This class maps a legacy guard policy to and from a TOSCA service template. - * - * @author Liam Fallon (liam.fallon@est.tech) - */ -public class LegacyGuardPolicyMapper - implements JpaToscaServiceTemplateMapper> { - private static final Logger LOGGER = LoggerFactory.getLogger(LegacyGuardPolicyMapper.class); - - // Tag for metadata fields - private static final String POLICY_ID = "policy-id"; - private static final String POLICY_VERSION = "policy-version"; - - 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.blacklist.", - new PfConceptKey("onap.policies.controlloop.guard.Blacklist:1.0.0")); - } - - @Override - public JpaToscaServiceTemplate toToscaServiceTemplate(final LegacyGuardPolicyInput legacyGuardPolicyInput) { - PfConceptKey guardPolicyType = getGuardPolicyType(legacyGuardPolicyInput); - if (guardPolicyType == null) { - String errorMessage = - "policy type for guard policy \"" + legacyGuardPolicyInput.getPolicyId() + "\" unknown"; - LOGGER.warn(errorMessage); - throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); - } - - String version = legacyGuardPolicyInput.getPolicyVersion(); - if (version != null) { - version = version + ".0.0"; - } else { - version = guardPolicyType.getVersion(); - } - - PfConceptKey policyKey = new PfConceptKey(legacyGuardPolicyInput.getPolicyId(), version); - - final JpaToscaPolicy toscaPolicy = new JpaToscaPolicy(policyKey); - toscaPolicy.setType(guardPolicyType); - toscaPolicy.setProperties(legacyGuardPolicyInput.getContent().getAsPropertyMap()); - - final Map metadata = new LinkedHashMap<>(); - metadata.put(POLICY_ID, toscaPolicy.getKey().getName()); - metadata.put(POLICY_VERSION, Integer.toString(toscaPolicy.getKey().getMajorVersion())); - toscaPolicy.setMetadata(metadata); - - final JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate(); - serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_0_0"); - - serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate()); - - serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies()); - serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policyKey, toscaPolicy); - - return serviceTemplate; - } - - @Override - public Map - fromToscaServiceTemplate(final JpaToscaServiceTemplate serviceTemplate) { - ToscaUtils.assertPoliciesExist(serviceTemplate); - - final Map legacyGuardPolicyOutputMap = new LinkedHashMap<>(); - - for (JpaToscaPolicy toscaPolicy : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap() - .values()) { - - final LegacyGuardPolicyOutput legacyGuardPolicyOutput = new LegacyGuardPolicyOutput(); - legacyGuardPolicyOutput.setType(toscaPolicy.getType().getName()); - legacyGuardPolicyOutput.setVersion(toscaPolicy.getKey().getVersion()); - - if (toscaPolicy.getMetadata() == null) { - String errorMessage = "no metadata defined on TOSCA policy"; - LOGGER.warn(errorMessage); - throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); - } - - final Map metadata = new LinkedHashMap<>(toscaPolicy.getMetadata()); - - // if version exists, convert it to int - metadata.computeIfPresent(POLICY_VERSION, (key, val) -> Integer.parseInt(val.toString())); - - legacyGuardPolicyOutput.setMetadata(metadata); - - if (toscaPolicy.getProperties() == null) { - String errorMessage = "no properties defined on TOSCA policy"; - LOGGER.warn(errorMessage); - throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); - } - - final LegacyGuardPolicyContent content = new LegacyGuardPolicyContent(); - content.setContent(toscaPolicy.getProperties()); - - final Map propertiesMap = new LinkedHashMap<>(); - propertiesMap.put("content", content); - legacyGuardPolicyOutput.setProperties(propertiesMap); - - legacyGuardPolicyOutputMap.put(toscaPolicy.getKey().getName(), legacyGuardPolicyOutput); - } - - return legacyGuardPolicyOutputMap; - } - - private PfConceptKey getGuardPolicyType(final LegacyGuardPolicyInput legacyGuardPolicyInput) { - final String policyId = legacyGuardPolicyInput.getPolicyId(); - if (policyId == null) { - return null; - } - - for (Entry guardPolicyTypeEntry : GUARD_POLICY_TYPE_MAP.entrySet()) { - if (policyId.startsWith(guardPolicyTypeEntry.getKey())) { - return guardPolicyTypeEntry.getValue(); - } - } - - return null; - } -} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapper.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapper.java index 41792aeb9..de1bc12d7 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapper.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapper.java @@ -71,7 +71,7 @@ public class LegacyOperationalPolicyMapper toscaPolicy.getProperties().put(CONTENT_PROPERTY, legacyOperationalPolicy.getContent()); final JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate(); - serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_0_0"); + serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_1_0"); serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate()); diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider.java index dc8affc77..314c7728d 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider.java @@ -22,7 +22,6 @@ package org.onap.policy.models.tosca.legacy.provider; import java.util.List; -import java.util.Map; import javax.ws.rs.core.Response; @@ -32,10 +31,7 @@ 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.dao.PfDao; -import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput; -import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput; import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy; -import org.onap.policy.models.tosca.legacy.mapping.LegacyGuardPolicyMapper; import org.onap.policy.models.tosca.legacy.mapping.LegacyOperationalPolicyMapper; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy; @@ -151,99 +147,6 @@ public class LegacyProvider { return legacyOperationalPolicy; } - /** - * Get legacy guard policy. - * - * @param dao the DAO to use to access the database - * @param policyId ID of the policy. - * @param policyVersion version of the policy. - * @return the policies found - * @throws PfModelException on errors getting policies - */ - public Map getGuardPolicy(@NonNull final PfDao dao, @NonNull final String policyId, - final String policyVersion) throws PfModelException { - - LOGGER.debug("->getGuardPolicy: policyId={}, policyVersion={}", policyId, policyVersion); - - Map legacyGuardPolicyMap = - new LegacyGuardPolicyMapper().fromToscaServiceTemplate(getLegacyPolicy(dao, policyId, policyVersion)); - - LOGGER.debug("<-getGuardPolicy: policyId={}, policyVersion={}, legacyGuardPolicyMap={}", policyId, - policyVersion, legacyGuardPolicyMap); - return legacyGuardPolicyMap; - } - - /** - * Create legacy guard policy. - * - * @param dao the DAO to use to access the database - * @param legacyGuardPolicy the definition of the policy to be created. - * @return the created policy - * @throws PfModelException on errors creating policies - */ - public Map createGuardPolicy(@NonNull final PfDao dao, - @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException { - - LOGGER.debug("->createGuardPolicy: legacyGuardPolicy={}", legacyGuardPolicy); - - JpaToscaServiceTemplate incomingServiceTemplate = - new LegacyGuardPolicyMapper().toToscaServiceTemplate(legacyGuardPolicy); - JpaToscaServiceTemplate outgoingingServiceTemplate = - new SimpleToscaProvider().createPolicies(dao, incomingServiceTemplate); - - Map createdLegacyGuardPolicyMap = - new LegacyGuardPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate); - - LOGGER.debug("<-createGuardPolicy: createdLegacyGuardPolicyMap={}", createdLegacyGuardPolicyMap); - return createdLegacyGuardPolicyMap; - } - - /** - * Update legacy guard policy. - * - * @param dao the DAO to use to access the database - * @param legacyGuardPolicy the definition of the policy to be updated - * @return the updated policy - * @throws PfModelException on errors updating policies - */ - public Map updateGuardPolicy(@NonNull final PfDao dao, - @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException { - - LOGGER.debug("->updateGuardPolicy: legacyGuardPolicy={}", legacyGuardPolicy); - - JpaToscaServiceTemplate incomingServiceTemplate = - new LegacyGuardPolicyMapper().toToscaServiceTemplate(legacyGuardPolicy); - JpaToscaServiceTemplate outgoingingServiceTemplate = - new SimpleToscaProvider().updatePolicies(dao, incomingServiceTemplate); - - Map updatedLegacyGuardPolicyMap = - new LegacyGuardPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate); - - LOGGER.debug("<-updateGuardPolicy: updatedLegacyGuardPolicyMap={}", updatedLegacyGuardPolicyMap); - return updatedLegacyGuardPolicyMap; - } - - /** - * Delete legacy guard policy. - * - * @param dao the DAO to use to access the database - * @param policyId ID of the policy. - * @param policyVersion version of the policy. - * @return the deleted policy - * @throws PfModelException on errors deleting policies - */ - public Map deleteGuardPolicy(@NonNull final PfDao dao, - @NonNull final String policyId, @NonNull final String policyVersion) throws PfModelException { - - LOGGER.debug("->deleteGuardPolicy: policyId={}, policyVersion={}", policyId, policyVersion); - Map legacyGuardPolicyMap = new LegacyGuardPolicyMapper() - .fromToscaServiceTemplate(deleteLegacyPolicy(dao, policyId, policyVersion)); - - LOGGER.debug("<-deleteGuardPolicy: policyId={}, policyVersion={}, legacyGuardPolicyMap={}", policyId, - policyVersion, legacyGuardPolicyMap); - return legacyGuardPolicyMap; - } - /** * Get the JPA Policy for a policy ID and version. * diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplate.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplate.java index aa4f231c0..e2e27f0e2 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplate.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplate.java @@ -73,7 +73,7 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType { private static final long serialVersionUID = 8084846046148349401L; - public static final String DEFAULT_TOSCA_DEFINTIONS_VERISON = "tosca_simple_yaml_1_0_0"; + public static final String DEFAULT_TOSCA_DEFINTIONS_VERISON = "tosca_simple_yaml_1_1_0"; public static final String DEFAULT_NAME = "ToscaServiceTemplateSimple"; public static final String DEFAULT_VERSION = "1.0.0"; diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilterTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilterTest.java index 858ac09fe..9ee3ec8bb 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilterTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilterTest.java @@ -21,6 +21,7 @@ package org.onap.policy.models.tosca.authorative.concepts; +import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -62,12 +63,11 @@ public class ToscaPolicyFilterTest { "policies/vCPE.policy.monitoring.input.tosca.json", "policies/vCPE.policy.monitoring.input.tosca.yaml", "policies/vCPE.policy.operational.input.tosca.yaml", - "policies/vDNS.policy.guard.frequency.input.tosca.json", - "policies/vDNS.policy.guard.frequency.input.tosca.yaml", - "policies/vDNS.policy.guard.minmax.input.tosca.yaml", "policies/vDNS.policy.monitoring.input.tosca.json", "policies/vDNS.policy.monitoring.input.tosca.yaml", "policies/vDNS.policy.operational.input.tosca.yaml", + "policies/vDNS.policy.guard.frequencylimiter.input.tosca.yaml", + "policies/vDNS.policy.guard.minmaxvnfs.input.tosca.yaml", "policies/vFirewall.policy.monitoring.input.tosca.json", "policies/vFirewall.policy.monitoring.input.tosca.yaml", "policies/vFirewall.policy.operational.input.tosca.json", @@ -151,22 +151,67 @@ public class ToscaPolicyFilterTest { assertEquals(VERSION_100, filteredList.get(7).getVersion()); assertEquals(VERSION_100, filteredList.get(12).getVersion()); - assertEquals(23, policyList.size()); + assertEquals(22, policyList.size()); assertEquals(22, filteredList.size()); - policyList.get(10).setVersion("2.0.0"); - policyList.get(16).setVersion("3.4.5"); + // + // Change versions to a couple of policies + // + policyList.forEach(policy -> { + if ("onap.vfirewall.tca".equals(policy.getName())) { + policy.setVersion("2.0.0"); + } else if ("operational.modifyconfig".equals(policy.getName())) { + policy.setVersion("3.4.5"); + } + }); + // + // We'll still get back the same number of policies + // filteredList = filter.filter(policyList); assertEquals(22, filteredList.size()); - assertEquals("2.0.0", filteredList.get(12).getVersion()); - assertEquals("3.4.5", filteredList.get(14).getVersion()); - - policyList.get(10).setVersion(VERSION_100); - policyList.get(16).setVersion(VERSION_100); + // + // Assert that the correct versions are returned + // + policyList.forEach(policy -> { + if ("onap.vfirewall.tca".equals(policy.getName())) { + assertThat(policy.getVersion()).isEqualTo("2.0.0"); + } else if ("operational.modifyconfig".equals(policy.getName())) { + assertThat(policy.getVersion()).isEqualTo("3.4.5"); + } else if ("operational.scaleout".equals(policy.getName())) { + assertThat(policy.getVersion()).isEqualTo(VERSION_000); + } else { + assertThat(policy.getVersion()).isEqualTo(VERSION_100); + } + }); + + // + // Change versions back + // + policyList.forEach(policy -> { + if ("onap.vfirewall.tca".equals(policy.getName())) { + policy.setVersion(VERSION_100); + } else if ("operational.modifyconfig".equals(policy.getName())) { + policy.setVersion(VERSION_100); + } + }); + // + // We'll still get back the same number of policies + // filteredList = filter.filter(policyList); assertEquals(22, filteredList.size()); - assertEquals(VERSION_100, filteredList.get(12).getVersion()); - assertEquals(VERSION_100, filteredList.get(14).getVersion()); + // + // Assert that the correct versions are returned + // + policyList.forEach(policy -> { + // + // Should we fix this to be 1.0.0?? + // + if ("operational.scaleout".equals(policy.getName())) { + assertThat(policy.getVersion()).isEqualTo(VERSION_000); + } else { + assertThat(policy.getVersion()).isEqualTo(VERSION_100); + } + }); } @Test @@ -177,7 +222,7 @@ public class ToscaPolicyFilterTest { filter = ToscaPolicyFilter.builder().name("guard.frequency.scaleout").build(); filteredList = filter.filter(policyList); - assertEquals(2, filteredList.size()); + assertEquals(1, filteredList.size()); filter = ToscaPolicyFilter.builder().name("guard.frequency.scalein").build(); filteredList = filter.filter(policyList); @@ -193,7 +238,7 @@ public class ToscaPolicyFilterTest { filter = ToscaPolicyFilter.builder().name("operational.modifyconfig").version(VERSION_100).build(); filteredList = filter.filter(policyList); - assertEquals(0, filteredList.size()); + assertEquals(1, filteredList.size()); } @Test @@ -201,11 +246,11 @@ public class ToscaPolicyFilterTest { // null pattern ToscaPolicyFilter filter = ToscaPolicyFilter.builder().versionPrefix(null).build(); List filteredList = filter.filter(policyList); - assertEquals(23, filteredList.size()); + assertEquals(22, filteredList.size()); filter = ToscaPolicyFilter.builder().versionPrefix("1.").build(); filteredList = filter.filter(policyList); - assertEquals(21, filteredList.size()); + assertEquals(20, filteredList.size()); filter = ToscaPolicyFilter.builder().versionPrefix("100.").build(); filteredList = filter.filter(policyList); @@ -236,7 +281,7 @@ public class ToscaPolicyFilterTest { filter = ToscaPolicyFilter.builder().typeVersion(VERSION_000).build(); filteredList = filter.filter(policyList); - assertEquals(3, filteredList.size()); + assertEquals(0, filteredList.size()); filter = ToscaPolicyFilter.builder().type("onap.policies.optimization.resource.HpaPolicy") .typeVersion(VERSION_100).build(); diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeFilterTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeFilterTest.java index 0f038d3cd..6304091af 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeFilterTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeFilterTest.java @@ -63,15 +63,15 @@ public class ToscaPolicyTypeFilterTest { "policytypes/onap.policies.optimization.resource.VnfPolicy.yaml", "policytypes/onap.policies.optimization.resource.PciPolicy.yaml", "policytypes/onap.policies.optimization.resource.OptimizationPolicy.yaml", - "policytypes/onap.policies.controlloop.guard.Blacklist.yaml", + "policytypes/onap.policies.controlloop.guard.common.Blacklist.yaml", "policytypes/onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server.yaml", "policytypes/onap.policies.optimization.resource.HpaPolicy.yaml", "policytypes/onap.policies.optimization.resource.Vim_fit.yaml", "policytypes/onap.policies.optimization.service.SubscriberPolicy.yaml", "policytypes/onap.policies.optimization.resource.AffinityPolicy.yaml", "policytypes/onap.policies.optimization.service.QueryPolicy.yaml", - "policytypes/onap.policies.controlloop.guard.MinMax.yaml", - "policytypes/onap.policies.controlloop.guard.FrequencyLimiter.yaml", + "policytypes/onap.policies.controlloop.guard.common.MinMax.yaml", + "policytypes/onap.policies.controlloop.guard.common.FrequencyLimiter.yaml", "policytypes/onap.policies.controlloop.guard.coordination.FirstBlocksSecond.yaml", "policytypes/onap.policies.Optimization.yaml", "policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.yaml" @@ -149,8 +149,16 @@ public class ToscaPolicyTypeFilterTest { typeList.get(12).setVersion("2.0.0"); filteredList = filter.filter(typeList); assertEquals(20, filteredList.size()); - assertEquals("2.0.0", filteredList.get(11).getVersion()); - assertEquals(VERSION_100, filteredList.get(18).getVersion()); + // + // This seems to change around as to where this policy type + // got changed - perhaps we change this test to find a specific name + // to test for vs an index which never remains consistent? + // + assertEquals("2.0.0", filteredList.get(18).getVersion()); + // + // And now this index changes again?? + // + assertEquals(VERSION_100, filteredList.get(17).getVersion()); typeList.get(12).setVersion(VERSION_100); filteredList = filter.filter(typeList); diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyTest.java deleted file mode 100644 index e06692ab0..000000000 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyTest.java +++ /dev/null @@ -1,54 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.models.tosca.legacy.concepts; - -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -import java.util.HashMap; -import java.util.Map; - -import org.junit.Test; -import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput; -import org.onap.policy.models.tosca.legacy.concepts.testconcepts.DummyBadLegacyGuardPolicyContent; - -public class LegacyGuardPolicyTest { - - @Test - public void test() { - LegacyGuardPolicyInput guard = new LegacyGuardPolicyInput(); - assertNotNull(guard); - guard.setPolicyId("guard.frequency"); - assertEquals("guard.frequency", guard.getPolicyId()); - guard.setPolicyVersion("1"); - assertEquals("1", guard.getPolicyVersion()); - Map body = new HashMap<>(); - body.put("actor", "SO"); - LegacyGuardPolicyContent content = new LegacyGuardPolicyContent(); - content.setActor("SO"); - guard.setContent(content); - assertEquals("SO", guard.getContent().getActor()); - - DummyBadLegacyGuardPolicyContent dblgpc = new DummyBadLegacyGuardPolicyContent(); - assertThatThrownBy(dblgpc::getAsPropertyMap).hasMessage("could not convert content to a property map"); - } -} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/testconcepts/DummyBadLegacyGuardPolicyContent.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/testconcepts/DummyBadLegacyGuardPolicyContent.java deleted file mode 100644 index 1b149416c..000000000 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/testconcepts/DummyBadLegacyGuardPolicyContent.java +++ /dev/null @@ -1,34 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. - * ================================================================================ - * 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.models.tosca.legacy.concepts.testconcepts; - -import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyContent; - -/** - * Dummy {@link LegacyGuardPolicyContent} class to force exception for testing. - * - * @author Liam Fallon (liam.fallon@est.tech) - */ -public class DummyBadLegacyGuardPolicyContent extends LegacyGuardPolicyContent { - public static final String TO_TRIGGER_EXCEPTION = "Dummy"; - @SuppressWarnings("unused") - private final transient String aaa = "aaa"; -} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapperTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapperTest.java deleted file mode 100644 index 332552a73..000000000 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapperTest.java +++ /dev/null @@ -1,70 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019-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. - * 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.models.tosca.legacy.mapping; - -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertEquals; - -import java.util.LinkedHashMap; -import java.util.Map; - -import org.junit.Test; -import org.onap.policy.models.base.PfConceptKey; -import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput; -import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies; -import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy; -import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate; -import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate; - -/** - * Test the {@link LegacyGuardPolicyMapper} class. - * - * @author Liam Fallon (liam.fallon@est.tech) - */ -public class LegacyGuardPolicyMapperTest { - - @Test - public void testLegacyGuardPolicyMapper() { - JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate(); - serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate()); - serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies()); - - JpaToscaPolicy policy = new JpaToscaPolicy(new PfConceptKey("PolicyName", "2.0.0")); - serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policy.getKey(), policy); - - policy.setMetadata(null); - assertThatThrownBy(() -> { - new LegacyGuardPolicyMapper().fromToscaServiceTemplate(serviceTemplate); - }).hasMessageContaining("no metadata defined on TOSCA policy"); - - policy.setMetadata(new LinkedHashMap<>()); - policy.setProperties(null); - assertThatThrownBy(() -> { - new LegacyGuardPolicyMapper().fromToscaServiceTemplate(serviceTemplate); - }).hasMessageContaining("no properties defined on TOSCA policy"); - - policy.setProperties(new LinkedHashMap<>()); - Map guardPolicyMap = - new LegacyGuardPolicyMapper().fromToscaServiceTemplate(serviceTemplate); - LegacyGuardPolicyOutput guardPolicy = guardPolicyMap.values().iterator().next(); - assertEquals("2.0.0", guardPolicy.getVersion()); - } -} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyGuardTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyGuardTest.java deleted file mode 100644 index 047ef4891..000000000 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyGuardTest.java +++ /dev/null @@ -1,388 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019-2020 Nordix Foundation. - * Modifications Copyright (C) 2019 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.models.tosca.legacy.provider; - -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -import java.util.Map; -import java.util.Properties; - -import org.eclipse.persistence.config.PersistenceUnitProperties; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -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.base.PfModelException; -import org.onap.policy.models.dao.DaoParameters; -import org.onap.policy.models.dao.PfDao; -import org.onap.policy.models.dao.PfDaoFactory; -import org.onap.policy.models.dao.impl.DefaultPfDao; -import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; -import org.onap.policy.models.tosca.authorative.provider.AuthorativeToscaProvider; -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; -import org.yaml.snakeyaml.Yaml; - -/** - * Test the {@link LegacyProvider} class for legacy guard policies. - * - * @author Liam Fallon (liam.fallon@est.tech) - */ -public class LegacyProvider4LegacyGuardTest { - private static final String POLICY_ID_IS_NULL = "^policyId is marked .*on.*ull but is null$"; - private static final String VDNS_OUTPUT_JSON = "policies/vDNS.policy.guard.frequency.output.json"; - private static final String VDNS_INPUT_JSON = "policies/vDNS.policy.guard.frequency.input.json"; - private static final String LEGACY_POLICY_IS_NULL = "^legacyGuardPolicy is marked .*on.*ull but is null$"; - private static final String DAO_IS_NULL = "^dao is marked .*on.*ull but is null$"; - private PfDao pfDao; - private StandardCoder standardCoder; - - /** - * Set up the DAO towards the database. - * - * @throws Exception on database errors - */ - @Before - public void setupDao() throws Exception { - final DaoParameters daoParameters = new DaoParameters(); - daoParameters.setPluginClass(DefaultPfDao.class.getName()); - - daoParameters.setPersistenceUnit("ToscaConceptTest"); - - Properties jdbcProperties = new Properties(); - jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, "policy"); - jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "P01icY"); - - // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB - jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver"); - jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:h2:mem:testdb"); - - daoParameters.setJdbcProperties(jdbcProperties); - - pfDao = new PfDaoFactory().createPfDao(daoParameters); - pfDao.init(daoParameters); - } - - /** - * Set up standard coder. - */ - @Before - public void setupStandardCoder() { - standardCoder = new StandardCoder(); - } - - @After - public void teardown() { - pfDao.close(); - } - - @Test - public void testPoliciesGet() throws Exception { - assertThatThrownBy(() -> { - new LegacyProvider().getGuardPolicy(null, null, null); - }).hasMessageMatching(DAO_IS_NULL); - - assertThatThrownBy(() -> { - new LegacyProvider().getGuardPolicy(null, null, ""); - }).hasMessageMatching(DAO_IS_NULL); - - assertThatThrownBy(() -> { - new LegacyProvider().getGuardPolicy(pfDao, null, null); - }).hasMessageMatching(POLICY_ID_IS_NULL); - - assertThatThrownBy(() -> { - new LegacyProvider().getGuardPolicy(pfDao, "I Dont Exist", null); - }).hasMessage("no policy found for policy: I Dont Exist:null"); - - createPolicyTypes(); - - LegacyGuardPolicyInput originalGip = - standardCoder.decode(ResourceUtils.getResourceAsString(VDNS_INPUT_JSON), LegacyGuardPolicyInput.class); - - assertNotNull(originalGip); - - Map createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip); - - assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next()); - assertEquals(originalGip.getContent(), - createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next()); - - Map gotGopm = - new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), null); - - assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next()); - assertEquals(originalGip.getContent(), - gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next()); - - String expectedJsonOutput = ResourceUtils.getResourceAsString(VDNS_OUTPUT_JSON); - String actualJsonOutput = standardCoder.encode(gotGopm); - - assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", "")); - - gotGopm = new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), "1"); - - assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next()); - assertEquals(originalGip.getContent(), - gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next()); - - actualJsonOutput = standardCoder.encode(gotGopm); - - assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", "")); - - assertThatThrownBy(() -> { - new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), "2"); - }).hasMessage("no policy found for policy: guard.frequency.scaleout:2"); - } - - @Test - public void testPolicyCreate() throws Exception { - assertThatThrownBy(() -> { - new LegacyProvider().createGuardPolicy(null, null); - }).hasMessageMatching(DAO_IS_NULL); - - assertThatThrownBy(() -> { - new LegacyProvider().createGuardPolicy(null, new LegacyGuardPolicyInput()); - }).hasMessageMatching(DAO_IS_NULL); - - assertThatThrownBy(() -> { - new LegacyProvider().createGuardPolicy(pfDao, null); - }).hasMessageMatching(LEGACY_POLICY_IS_NULL); - - createPolicyTypes(); - - LegacyGuardPolicyInput originalGip = - standardCoder.decode(ResourceUtils.getResourceAsString(VDNS_INPUT_JSON), LegacyGuardPolicyInput.class); - - assertNotNull(originalGip); - - Map createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip); - - assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next()); - assertEquals(originalGip.getContent(), - createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next()); - - Map gotGopm = - new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), null); - - assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next()); - assertEquals(originalGip.getContent(), - gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next()); - - String expectedJsonOutput = ResourceUtils.getResourceAsString(VDNS_OUTPUT_JSON); - String actualJsonOutput = standardCoder.encode(gotGopm); - - assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", "")); - } - - @Test - public void testPolicyCreateBad() throws Exception { - assertThatThrownBy(() -> { - new LegacyProvider().createGuardPolicy(null, null); - }).hasMessageMatching(DAO_IS_NULL); - - assertThatThrownBy(() -> { - new LegacyProvider().createGuardPolicy(null, new LegacyGuardPolicyInput()); - }).hasMessageMatching(DAO_IS_NULL); - - assertThatThrownBy(() -> { - new LegacyProvider().createGuardPolicy(pfDao, null); - }).hasMessageMatching(LEGACY_POLICY_IS_NULL); - - createPolicyTypes(); - - LegacyGuardPolicyInput originalGip = - standardCoder.decode(ResourceUtils.getResourceAsString(VDNS_INPUT_JSON), LegacyGuardPolicyInput.class); - - assertNotNull(originalGip); - - originalGip.setPolicyId("i.do.not.exist"); - - assertThatThrownBy(() -> { - new LegacyProvider().createGuardPolicy(pfDao, originalGip); - }).hasMessage("policy type for guard policy \"i.do.not.exist\" unknown"); - } - - @Test - public void testPolicyUpdate() throws Exception { - assertThatThrownBy(() -> { - new LegacyProvider().updateGuardPolicy(null, null); - }).hasMessageMatching(DAO_IS_NULL); - - assertThatThrownBy(() -> { - new LegacyProvider().updateGuardPolicy(null, new LegacyGuardPolicyInput()); - }).hasMessageMatching(DAO_IS_NULL); - - assertThatThrownBy(() -> { - new LegacyProvider().updateGuardPolicy(pfDao, null); - }).hasMessageMatching(LEGACY_POLICY_IS_NULL); - - assertThatThrownBy(() -> { - new LegacyProvider().updateGuardPolicy(pfDao, new LegacyGuardPolicyInput()); - }).hasMessage("policy type for guard policy \"null\" unknown"); - - createPolicyTypes(); - - LegacyGuardPolicyInput originalGip = - standardCoder.decode(ResourceUtils.getResourceAsString(VDNS_INPUT_JSON), LegacyGuardPolicyInput.class); - - assertNotNull(originalGip); - - Map createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip); - assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next()); - assertEquals(originalGip.getContent(), - createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next()); - - Map gotGopm = - new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), null); - - assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next()); - assertEquals(originalGip.getContent(), - gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next()); - - originalGip.getContent().setRecipe("Roast Turkey"); - Map updatedGp = new LegacyProvider().updateGuardPolicy(pfDao, originalGip); - assertEquals(originalGip.getPolicyId(), updatedGp.keySet().iterator().next()); - assertEquals(originalGip.getContent(), - updatedGp.get(originalGip.getPolicyId()).getProperties().values().iterator().next()); - - Map gotUpdatedGopm = - new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), null); - assertEquals(originalGip.getPolicyId(), gotUpdatedGopm.keySet().iterator().next()); - assertEquals(originalGip.getContent(), - gotUpdatedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next()); - assertEquals("Roast Turkey", - gotUpdatedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next().getRecipe()); - } - - @Test - public void testPoliciesDelete() throws Exception { - assertThatThrownBy(() -> { - new LegacyProvider().deleteGuardPolicy(null, null, null); - }).hasMessageMatching(DAO_IS_NULL); - - assertThatThrownBy(() -> { - new LegacyProvider().deleteGuardPolicy(null, null, ""); - }).hasMessageMatching(DAO_IS_NULL); - - assertThatThrownBy(() -> { - new LegacyProvider().deleteGuardPolicy(null, "", null); - }).hasMessageMatching(DAO_IS_NULL); - - assertThatThrownBy(() -> { - new LegacyProvider().deleteGuardPolicy(null, "", ""); - }).hasMessageMatching(DAO_IS_NULL); - - assertThatThrownBy(() -> { - new LegacyProvider().deleteGuardPolicy(pfDao, null, null); - }).hasMessageMatching(POLICY_ID_IS_NULL); - - assertThatThrownBy(() -> { - new LegacyProvider().deleteGuardPolicy(pfDao, null, ""); - }).hasMessageMatching(POLICY_ID_IS_NULL); - - assertThatThrownBy(() -> { - new LegacyProvider().deleteGuardPolicy(pfDao, "", null); - }).hasMessageMatching("^policyVersion is marked .*on.*ull but is null$"); - - assertThatThrownBy(() -> { - new LegacyProvider().deleteGuardPolicy(pfDao, "IDontExist", "0"); - }).hasMessage("no policy found for policy: IDontExist:0"); - - createPolicyTypes(); - - LegacyGuardPolicyInput originalGip = - standardCoder.decode(ResourceUtils.getResourceAsString(VDNS_INPUT_JSON), LegacyGuardPolicyInput.class); - - assertNotNull(originalGip); - - Map createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip); - assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next()); - assertEquals(originalGip.getContent(), - createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next()); - - Map gotGopm = - new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), null); - - assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next()); - assertEquals(originalGip.getContent(), - gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next()); - - String expectedJsonOutput = ResourceUtils.getResourceAsString(VDNS_OUTPUT_JSON); - String actualJsonOutput = standardCoder.encode(gotGopm); - - assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", "")); - - assertThatThrownBy(() -> { - new LegacyProvider().deleteGuardPolicy(pfDao, originalGip.getPolicyId(), null); - }).hasMessageMatching("^policyVersion is marked .*on.*ull but is null$"); - - Map deletedGopm = - new LegacyProvider().deleteGuardPolicy(pfDao, originalGip.getPolicyId(), "1"); - assertEquals(originalGip.getPolicyId(), deletedGopm.keySet().iterator().next()); - assertEquals(originalGip.getContent(), - deletedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next()); - - assertThatThrownBy(() -> { - new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), null); - }).hasMessage("no policy found for policy: guard.frequency.scaleout:null"); - - LegacyGuardPolicyInput otherGip = new LegacyGuardPolicyInput(); - otherGip.setPolicyId("guard.blacklist.b0"); - otherGip.setPolicyVersion("1"); - otherGip.setContent(new LegacyGuardPolicyContent()); - - Map createdOtherGopm = new LegacyProvider().createGuardPolicy(pfDao, otherGip); - assertEquals(otherGip.getPolicyId(), createdOtherGopm.keySet().iterator().next()); - assertEquals(otherGip.getContent(), - createdOtherGopm.get(otherGip.getPolicyId()).getProperties().values().iterator().next()); - - assertThatThrownBy(() -> { - new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), null); - }).hasMessage("no policy found for policy: guard.frequency.scaleout:null"); - } - - private void createPolicyTypes() throws CoderException, PfModelException { - Object yamlObject = new Yaml().load( - ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.guard.FrequencyLimiter.yaml")); - String yamlAsJsonString = new StandardCoder().encode(yamlObject); - - ToscaServiceTemplate toscaServiceTemplatePolicyType = - standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class); - - assertNotNull(toscaServiceTemplatePolicyType); - new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplatePolicyType); - - yamlObject = new Yaml() - .load(ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.guard.Blacklist.yaml")); - yamlAsJsonString = new StandardCoder().encode(yamlObject); - - toscaServiceTemplatePolicyType = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class); - - assertNotNull(toscaServiceTemplatePolicyType); - new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplatePolicyType); - } -} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicySerializationTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicySerializationTest.java index 500ba9fa2..318eaeede 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicySerializationTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicySerializationTest.java @@ -57,7 +57,7 @@ public class MonitoringPolicySerializationTest { private static final String VERSION = "version"; - private static final String YAML_VERSION = "tosca_simple_yaml_1_0_0"; + private static final String YAML_VERSION = "tosca_simple_yaml_1_1_0"; private static final String DEFINITION_VERSION = "tosca_definitions_version"; diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicyTypeSerializationTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicyTypeSerializationTest.java index ef4123084..bf2030bd4 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicyTypeSerializationTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicyTypeSerializationTest.java @@ -145,7 +145,7 @@ public class MonitoringPolicyTypeSerializationTest { assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid()); // Check tosca_definitions_version - assertEquals("tosca_simple_yaml_1_0_0", serviceTemplate.getToscaDefinitionsVersion()); + assertEquals("tosca_simple_yaml_1_1_0", serviceTemplate.getToscaDefinitionsVersion()); // Check policy_types Map policyTypesConceptMap = serviceTemplate.getPolicyTypes().getConceptMap(); @@ -362,7 +362,7 @@ public class MonitoringPolicyTypeSerializationTest { assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid()); // Check tosca_definitions_version - assertEquals("tosca_simple_yaml_1_0_0", serviceTemplate.getToscaDefinitionsVersion()); + assertEquals("tosca_simple_yaml_1_1_0", serviceTemplate.getToscaDefinitionsVersion()); // Check policy_types Map policyTypesConceptMap = serviceTemplate.getPolicyTypes().getConceptMap(); diff --git a/models-tosca/src/test/resources/onap.policies.NoVersion.yaml b/models-tosca/src/test/resources/onap.policies.NoVersion.yaml index 2dda556a6..c8562dad0 100644 --- a/models-tosca/src/test/resources/onap.policies.NoVersion.yaml +++ b/models-tosca/src/test/resources/onap.policies.NoVersion.yaml @@ -1,4 +1,4 @@ -tosca_definitions_version: tosca_simple_yaml_1_0_0 +tosca_definitions_version: tosca_simple_yaml_1_1_0 policy_types: onap.policies.Optimization: derived_from: tosca.policies.Root -- cgit 1.2.3-korg