diff options
Diffstat (limited to 'models-tosca')
5 files changed, 238 insertions, 25 deletions
diff --git a/models-tosca/pom.xml b/models-tosca/pom.xml index b04d2a5ad..eaa1f475d 100644 --- a/models-tosca/pom.xml +++ b/models-tosca/pom.xml @@ -1,6 +1,7 @@ <!-- ============LICENSE_START======================================================= Copyright (C) 2019 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. @@ -64,6 +65,12 @@ </dependency> <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-collections4</artifactId> + <version>4.4</version> + </dependency> + + <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>test</scope> diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java index cce6fd9ee..0a7983c47 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java @@ -29,6 +29,7 @@ import javax.ws.rs.core.Response; import lombok.NonNull; +import org.apache.commons.collections4.CollectionUtils; import org.onap.policy.models.base.PfConcept; import org.onap.policy.models.base.PfConceptFilter; import org.onap.policy.models.base.PfConceptKey; @@ -187,6 +188,14 @@ public class SimpleToscaProvider { List<JpaToscaPolicyType> jpaPolicyTypeList = dao.getFiltered(JpaToscaPolicyType.class, name, version); serviceTemplate.getPolicyTypes().getConceptMap().putAll(asConceptMap(jpaPolicyTypeList)); + // Return all data types + // TODO: In the next review, return just the data types used by the policy types on the policy type list + List<JpaToscaDataType> jpaDataTypeList = dao.getFiltered(JpaToscaDataType.class, null, null); + if (!CollectionUtils.isEmpty(jpaDataTypeList)) { + serviceTemplate.setDataTypes(new JpaToscaDataTypes()); + serviceTemplate.getDataTypes().getConceptMap().putAll(asConceptMap(jpaDataTypeList)); + } + LOGGER.debug("<-getPolicyTypes: name={}, version={}, serviceTemplate={}", name, version, serviceTemplate); return serviceTemplate; } @@ -205,6 +214,11 @@ public class SimpleToscaProvider { ToscaUtils.assertPolicyTypesExist(serviceTemplate); + // Create the data types on the policy type + if (ToscaUtils.doDataTypesExist(serviceTemplate)) { + createDataTypes(dao, serviceTemplate); + } + for (JpaToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) { dao.create(policyType); } @@ -237,6 +251,11 @@ public class SimpleToscaProvider { ToscaUtils.assertPolicyTypesExist(serviceTemplate); + // Update the data types on the policy type + if (ToscaUtils.doDataTypesExist(serviceTemplate)) { + updateDataTypes(dao, serviceTemplate); + } + for (JpaToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) { dao.update(policyType); } @@ -444,7 +463,7 @@ public class SimpleToscaProvider { // Policy type version is not specified, get the latest version from the database List<JpaToscaPolicyType> jpaPolicyTypeList = dao.getFiltered(JpaToscaPolicyType.class, policyTypeName, null); - if (jpaPolicyTypeList.isEmpty()) { + if (CollectionUtils.isEmpty(jpaPolicyTypeList)) { return null; } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java index 23a428b65..71158f0d1 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java @@ -43,22 +43,86 @@ public final class ToscaUtils { } /** - * Check if data types have been specified correctly. + * Assert that data types have been specified correctly. * * @param serviceTemplate the service template containing data types to be checked */ public static void assertDataTypesExist(final JpaToscaServiceTemplate serviceTemplate) { + String message = checkDataTypesExist(serviceTemplate); + if (message != null) { + LOGGER.warn(message); + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, message); + } + } + + /** + * Assert that policy types have been specified correctly. + * + * @param serviceTemplate the service template containing policy types to be checked + */ + public static void assertPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) { + String message = checkPolicyTypesExist(serviceTemplate); + if (message != null) { + LOGGER.warn(message); + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, message); + } + } + + /** + * Assert that policies have been specified correctly. + * + * @param serviceTemplate the service template containing policy types to be checked + */ + public static void assertPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) { + String message = checkPoliciesExist(serviceTemplate); + if (message != null) { + LOGGER.warn(message); + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, message); + } + } + + /** + * Check that data types have been specified correctly. + * + * @param serviceTemplate the service template containing data types to be checked + */ + public static boolean doDataTypesExist(final JpaToscaServiceTemplate serviceTemplate) { + return checkDataTypesExist(serviceTemplate) == null; + } + + /** + * Check that policy types have been specified correctly. + * + * @param serviceTemplate the service template containing policy types to be checked + */ + public static boolean doPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) { + return checkPolicyTypesExist(serviceTemplate) == null; + } + + /** + * Check that policies have been specified correctly. + * + * @param serviceTemplate the service template containing policy types to be checked + */ + public static boolean doPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) { + return checkPoliciesExist(serviceTemplate) == null; + } + + /** + * Check if data types have been specified correctly. + * + * @param serviceTemplate the service template containing data types to be checked + */ + public static String checkDataTypesExist(final JpaToscaServiceTemplate serviceTemplate) { if (serviceTemplate.getDataTypes() == null) { - String errorMessage = "no data types specified on service template"; - LOGGER.warn(errorMessage); - throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); + return "no data types specified on service template"; } if (serviceTemplate.getDataTypes().getConceptMap().isEmpty()) { - String errorMessage = "list of data types specified on service template is empty"; - LOGGER.warn(errorMessage); - throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); + return "list of data types specified on service template is empty"; } + + return null; } /** @@ -66,18 +130,16 @@ public final class ToscaUtils { * * @param serviceTemplate the service template containing policy types to be checked */ - public static void assertPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) { + public static String checkPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) { if (serviceTemplate.getPolicyTypes() == null) { - String errorMessage = "no policy types specified on service template"; - LOGGER.warn(errorMessage); - throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); + return "no policy types specified on service template"; } if (serviceTemplate.getPolicyTypes().getConceptMap().isEmpty()) { - String errorMessage = "list of policy types specified on service template is empty"; - LOGGER.warn(errorMessage); - throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); + return "list of policy types specified on service template is empty"; } + + return null; } /** @@ -85,23 +147,19 @@ public final class ToscaUtils { * * @param serviceTemplate the service template containing policy types to be checked */ - public static void assertPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) { + public static String checkPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) { if (serviceTemplate.getTopologyTemplate() == null) { - String errorMessage = "topology template not specified on service template"; - LOGGER.warn(errorMessage); - throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); + return "topology template not specified on service template"; } if (serviceTemplate.getTopologyTemplate().getPolicies() == null) { - String errorMessage = "no policies specified on topology template of service template"; - LOGGER.warn(errorMessage); - throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); + return "no policies specified on topology template of service template"; } if (serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().isEmpty()) { - String errorMessage = "list of policies specified on topology template of service template is empty"; - LOGGER.warn(errorMessage); - throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); + return "list of policies specified on topology template of service template is empty"; } + + return null; } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProviderTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProviderTest.java index db6dc52b6..63ebcbd24 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProviderTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProviderTest.java @@ -46,6 +46,8 @@ import org.onap.policy.models.tosca.authorative.provider.AuthorativeToscaProvide import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataType; import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataTypes; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes; import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate; import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate; import org.yaml.snakeyaml.Yaml; @@ -114,6 +116,7 @@ public class SimpleToscaProviderTest { JpaToscaServiceTemplate createdServiceTemplate = new SimpleToscaProvider().createDataTypes(pfDao, serviceTemplate); + assertEquals(1, createdServiceTemplate.getDataTypes().getConceptMap().size()); assertEquals(dataType0, createdServiceTemplate.getDataTypes().get(dataType0Key)); assertEquals(null, createdServiceTemplate.getDataTypes().get(dataType0Key).getDescription()); @@ -143,6 +146,101 @@ public class SimpleToscaProviderTest { } @Test + public void testCreateUpdateGetDeletePolicyType() throws PfModelException { + JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate(); + + PfConceptKey dataType0Key = new PfConceptKey("DataType0", "0.0.1"); + JpaToscaDataType dataType0 = new JpaToscaDataType(); + dataType0.setKey(dataType0Key); + serviceTemplate.setDataTypes(new JpaToscaDataTypes()); + serviceTemplate.getDataTypes().getConceptMap().put(dataType0Key, dataType0); + + PfConceptKey policyType0Key = new PfConceptKey("PolicyType0", "0.0.1"); + JpaToscaPolicyType policyType0 = new JpaToscaPolicyType(); + policyType0.setKey(policyType0Key); + serviceTemplate.setPolicyTypes(new JpaToscaPolicyTypes()); + serviceTemplate.getPolicyTypes().getConceptMap().put(policyType0Key, policyType0); + + JpaToscaServiceTemplate createdServiceTemplate = + new SimpleToscaProvider().createPolicyTypes(pfDao, serviceTemplate); + + assertEquals(1, createdServiceTemplate.getPolicyTypes().getConceptMap().size()); + assertEquals(policyType0, createdServiceTemplate.getPolicyTypes().get(policyType0Key)); + assertEquals(null, createdServiceTemplate.getPolicyTypes().get(policyType0Key).getDescription()); + + policyType0.setDescription("Updated Description"); + + JpaToscaServiceTemplate updatedServiceTemplate = + new SimpleToscaProvider().updatePolicyTypes(pfDao, serviceTemplate); + + assertEquals(policyType0, updatedServiceTemplate.getPolicyTypes().get(policyType0Key)); + assertEquals("Updated Description", + updatedServiceTemplate.getPolicyTypes().get(policyType0Key).getDescription()); + + JpaToscaServiceTemplate gotServiceTemplate = + new SimpleToscaProvider().getPolicyTypes(pfDao, policyType0Key.getName(), policyType0Key.getVersion()); + + assertEquals(policyType0, gotServiceTemplate.getPolicyTypes().get(policyType0Key)); + assertEquals("Updated Description", gotServiceTemplate.getPolicyTypes().get(policyType0Key).getDescription()); + + JpaToscaServiceTemplate deletedServiceTemplate = + new SimpleToscaProvider().deletePolicyType(pfDao, policyType0Key); + + assertEquals(policyType0, deletedServiceTemplate.getPolicyTypes().get(policyType0Key)); + assertEquals("Updated Description", + deletedServiceTemplate.getPolicyTypes().get(policyType0Key).getDescription()); + + JpaToscaServiceTemplate doesNotExistServiceTemplate = + new SimpleToscaProvider().deletePolicyType(pfDao, policyType0Key); + + assertEquals(null, doesNotExistServiceTemplate.getPolicyTypes().get(policyType0Key)); + } + + @Test + public void testCreateUpdateGetDeletePolicyTypeWithDataType() throws PfModelException { + JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate(); + + PfConceptKey policyType0Key = new PfConceptKey("PolicyType0", "0.0.1"); + JpaToscaPolicyType policyType0 = new JpaToscaPolicyType(); + policyType0.setKey(policyType0Key); + serviceTemplate.setPolicyTypes(new JpaToscaPolicyTypes()); + serviceTemplate.getPolicyTypes().getConceptMap().put(policyType0Key, policyType0); + + JpaToscaServiceTemplate createdServiceTemplate = + new SimpleToscaProvider().createPolicyTypes(pfDao, serviceTemplate); + + assertEquals(policyType0, createdServiceTemplate.getPolicyTypes().get(policyType0Key)); + assertEquals(null, createdServiceTemplate.getPolicyTypes().get(policyType0Key).getDescription()); + + policyType0.setDescription("Updated Description"); + + JpaToscaServiceTemplate updatedServiceTemplate = + new SimpleToscaProvider().updatePolicyTypes(pfDao, serviceTemplate); + + assertEquals(policyType0, updatedServiceTemplate.getPolicyTypes().get(policyType0Key)); + assertEquals("Updated Description", + updatedServiceTemplate.getPolicyTypes().get(policyType0Key).getDescription()); + + JpaToscaServiceTemplate gotServiceTemplate = + new SimpleToscaProvider().getPolicyTypes(pfDao, policyType0Key.getName(), policyType0Key.getVersion()); + + assertEquals(policyType0, gotServiceTemplate.getPolicyTypes().get(policyType0Key)); + assertEquals("Updated Description", gotServiceTemplate.getPolicyTypes().get(policyType0Key).getDescription()); + + JpaToscaServiceTemplate deletedServiceTemplate = + new SimpleToscaProvider().deletePolicyType(pfDao, policyType0Key); + + assertEquals(policyType0, deletedServiceTemplate.getPolicyTypes().get(policyType0Key)); + assertEquals("Updated Description", + deletedServiceTemplate.getPolicyTypes().get(policyType0Key).getDescription()); + + JpaToscaServiceTemplate doesNotExistServiceTemplate = + new SimpleToscaProvider().deletePolicyType(pfDao, policyType0Key); + + assertEquals(null, doesNotExistServiceTemplate.getPolicyTypes().get(policyType0Key)); + } + + @Test public void testPoliciesGet() throws Exception { ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON), ToscaServiceTemplate.class); diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/utils/ToscaUtilsTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/utils/ToscaUtilsTest.java index b7c814a5e..82636940c 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/utils/ToscaUtilsTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/utils/ToscaUtilsTest.java @@ -22,6 +22,9 @@ package org.onap.policy.models.tosca.utils; import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import org.junit.Test; import org.onap.policy.models.base.PfConceptKey; @@ -42,39 +45,56 @@ public class ToscaUtilsTest { public void testAssertDataTypes() { JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate(); + assertFalse(ToscaUtils.doDataTypesExist(jpaToscaServiceTemplate)); + assertEquals("no data types specified on service template", + ToscaUtils.checkDataTypesExist(jpaToscaServiceTemplate)); assertThatThrownBy(() -> { ToscaUtils.assertDataTypesExist(jpaToscaServiceTemplate); }).hasMessage("no data types specified on service template"); jpaToscaServiceTemplate.setDataTypes(new JpaToscaDataTypes()); + assertFalse(ToscaUtils.doDataTypesExist(jpaToscaServiceTemplate)); + assertEquals("list of data types specified on service template is empty", + ToscaUtils.checkDataTypesExist(jpaToscaServiceTemplate)); assertThatThrownBy(() -> { ToscaUtils.assertDataTypesExist(jpaToscaServiceTemplate); }).hasMessage("list of data types specified on service template is empty"); jpaToscaServiceTemplate.getDataTypes().getConceptMap().put(new PfConceptKey(), null); + assertTrue(ToscaUtils.doDataTypesExist(jpaToscaServiceTemplate)); + assertEquals(null, ToscaUtils.checkDataTypesExist(jpaToscaServiceTemplate)); assertThatCode(() -> { ToscaUtils.assertDataTypesExist(jpaToscaServiceTemplate); }).doesNotThrowAnyException(); + } @Test public void testAssertPolicyTypes() { JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate(); + assertFalse(ToscaUtils.doPolicyTypesExist(jpaToscaServiceTemplate)); + assertEquals("no policy types specified on service template", + ToscaUtils.checkPolicyTypesExist(jpaToscaServiceTemplate)); assertThatThrownBy(() -> { ToscaUtils.assertPolicyTypesExist(jpaToscaServiceTemplate); }).hasMessage("no policy types specified on service template"); jpaToscaServiceTemplate.setPolicyTypes(new JpaToscaPolicyTypes()); + assertFalse(ToscaUtils.doPolicyTypesExist(jpaToscaServiceTemplate)); + assertEquals("list of policy types specified on service template is empty", + ToscaUtils.checkPolicyTypesExist(jpaToscaServiceTemplate)); assertThatThrownBy(() -> { ToscaUtils.assertPolicyTypesExist(jpaToscaServiceTemplate); }).hasMessage("list of policy types specified on service template is empty"); jpaToscaServiceTemplate.getPolicyTypes().getConceptMap().put(new PfConceptKey(), null); + assertTrue(ToscaUtils.doPolicyTypesExist(jpaToscaServiceTemplate)); + assertEquals(null, ToscaUtils.checkPolicyTypesExist(jpaToscaServiceTemplate)); assertThatCode(() -> { ToscaUtils.assertPolicyTypesExist(jpaToscaServiceTemplate); }).doesNotThrowAnyException(); @@ -84,24 +104,35 @@ public class ToscaUtilsTest { public void testAssertPolicies() { JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate(); + assertFalse(ToscaUtils.doPoliciesExist(jpaToscaServiceTemplate)); + assertEquals("topology template not specified on service template", + ToscaUtils.checkPoliciesExist(jpaToscaServiceTemplate)); assertThatThrownBy(() -> { ToscaUtils.assertPoliciesExist(jpaToscaServiceTemplate); }).hasMessage("topology template not specified on service template"); jpaToscaServiceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate()); + assertFalse(ToscaUtils.doPoliciesExist(jpaToscaServiceTemplate)); + assertEquals("no policies specified on topology template of service template", + ToscaUtils.checkPoliciesExist(jpaToscaServiceTemplate)); assertThatThrownBy(() -> { ToscaUtils.assertPoliciesExist(jpaToscaServiceTemplate); }).hasMessage("no policies specified on topology template of service template"); jpaToscaServiceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies()); + assertFalse(ToscaUtils.doPoliciesExist(jpaToscaServiceTemplate)); + assertEquals("list of policies specified on topology template of service template is empty", + ToscaUtils.checkPoliciesExist(jpaToscaServiceTemplate)); assertThatThrownBy(() -> { ToscaUtils.assertPoliciesExist(jpaToscaServiceTemplate); }).hasMessage("list of policies specified on topology template of service template is empty"); jpaToscaServiceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(new PfConceptKey(), null); + assertTrue(ToscaUtils.doPoliciesExist(jpaToscaServiceTemplate)); + assertEquals(null, ToscaUtils.checkPoliciesExist(jpaToscaServiceTemplate)); assertThatCode(() -> { ToscaUtils.assertPoliciesExist(jpaToscaServiceTemplate); }).doesNotThrowAnyException(); |