From 942cd0a012a2a4e9fd44e680dc6c4c7f4bcf8c62 Mon Sep 17 00:00:00 2001 From: liamfallon Date: Tue, 9 Apr 2019 16:58:20 +0000 Subject: Add unit tests for policy type and policy filters THis review adds unit tests for the authorative concepts in the models-tosc module and adds unit tests for the authorative and and database providers. Issue-ID: POLICY-1095 Change-Id: If98deea99658056030dd47725b0c0a4266d84106 Signed-off-by: liamfallon --- .../tosca/simple/concepts/JpaToscaModel.java | 3 +- .../simple/concepts/JpaToscaServiceTemplate.java | 6 +- .../simple/concepts/JpaToscaTopologyTemplate.java | 3 +- .../concepts/ToscaPolicyFilterTest.java | 1 + .../mapping/ToscaServiceTemplateMappingTest.java | 106 ------ .../AuthorativeToscaProviderPolicyTest.java | 395 +++++++++++++++++++++ .../AuthorativeToscaProviderPolicyTypeTest.java | 395 +++++++++++++++++++++ .../provider/ToscaServiceTemplateMappingTest.java | 106 ++++++ .../simple/provider/SimpleToscaProviderTest.java | 1 - 9 files changed, 905 insertions(+), 111 deletions(-) delete mode 100644 models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/mapping/ToscaServiceTemplateMappingTest.java create mode 100644 models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTest.java create mode 100644 models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTypeTest.java create mode 100644 models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/ToscaServiceTemplateMappingTest.java (limited to 'models-tosca') diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaModel.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaModel.java index a322c167f..204ef5b9d 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaModel.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaModel.java @@ -24,6 +24,7 @@ import java.util.List; import javax.persistence.CascadeType; import javax.persistence.Entity; +import javax.persistence.FetchType; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.OneToOne; @@ -57,7 +58,7 @@ import org.onap.policy.models.base.PfValidationResult; public class JpaToscaModel extends PfModel { private static final long serialVersionUID = 8800599637708309945L; - @OneToOne(cascade = CascadeType.ALL) + @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) private JpaToscaServiceTemplates serviceTemplates; /** 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 fda0c8014..5f50e4a43 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 @@ -27,6 +27,7 @@ import java.util.List; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; +import javax.persistence.FetchType; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.OneToOne; @@ -70,14 +71,15 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType { + new AuthorativeToscaProvider().getPolicies(null, null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().getPolicyList(null, null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( + ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"), + ToscaServiceTemplate.class); + + assertNotNull(toscaServiceTemplate); + ToscaServiceTemplate createdServiceTemplate = + new AuthorativeToscaProvider().createPolicies(pfDao, toscaServiceTemplate); + + PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0"); + + ToscaPolicy beforePolicy = + toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + ToscaPolicy createdPolicy = + createdServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, createdPolicy)); + assertTrue(beforePolicy.getType().equals(createdPolicy.getType())); + + ToscaServiceTemplate gotServiceTemplate = + new AuthorativeToscaProvider().getPolicies(pfDao, policyKey.getName(), policyKey.getVersion()); + + ToscaPolicy gotPolicy = + gotServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicy)); + assertTrue(beforePolicy.getType().equals(gotPolicy.getType())); + + List gotPolicyList = + new AuthorativeToscaProvider().getPolicyList(pfDao, "onap.restart.tca", "1.0.0"); + assertEquals(1, gotPolicyList.size()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); + + gotPolicyList = new AuthorativeToscaProvider().getPolicyList(pfDao, "onap.restart.tca", null); + assertEquals(1, gotPolicyList.size()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); + + gotPolicyList = new AuthorativeToscaProvider().getPolicyList(pfDao, null, null); + assertEquals(1, gotPolicyList.size()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); + + gotPolicyList = new AuthorativeToscaProvider().getPolicyList(pfDao, null, "1.0.0"); + assertEquals(1, gotPolicyList.size()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); + } + + + @Test + public void testPoliciesGetFiltered() throws Exception { + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().getFilteredPolicies(null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().getFilteredPolicies(null, ToscaPolicyFilter.builder().build()); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().getFilteredPolicies(pfDao, null); + }).hasMessage("filter is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().getFilteredPolicyList(null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().getFilteredPolicyList(null, ToscaPolicyFilter.builder().build()); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().getFilteredPolicyList(pfDao, null); + }).hasMessage("filter is marked @NonNull but is null"); + + ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( + ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"), + ToscaServiceTemplate.class); + + assertNotNull(toscaServiceTemplate); + ToscaServiceTemplate createdServiceTemplate = + new AuthorativeToscaProvider().createPolicies(pfDao, toscaServiceTemplate); + + PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0"); + + ToscaPolicy beforePolicy = + toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + ToscaPolicy createdPolicy = + createdServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, createdPolicy)); + assertTrue(beforePolicy.getType().equals(createdPolicy.getType())); + + ToscaServiceTemplate gotServiceTemplate = + new AuthorativeToscaProvider().getFilteredPolicies(pfDao, ToscaPolicyFilter.builder().build()); + + ToscaPolicy gotPolicy = + gotServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicy)); + assertTrue(beforePolicy.getType().equals(gotPolicy.getType())); + + gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicies(pfDao, + ToscaPolicyFilter.builder().name(policyKey.getName()).build()); + + gotPolicy = gotServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicy)); + assertTrue(beforePolicy.getType().equals(gotPolicy.getType())); + + gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicies(pfDao, + ToscaPolicyFilter.builder().name(policyKey.getName()).version("1.0.0").build()); + + gotPolicy = gotServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicy)); + assertTrue(beforePolicy.getType().equals(gotPolicy.getType())); + + List gotPolicyList = + new AuthorativeToscaProvider().getPolicyList(pfDao, "onap.restart.tca", "1.0.0"); + assertEquals(1, gotPolicyList.size()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); + + gotPolicyList = + new AuthorativeToscaProvider().getFilteredPolicyList(pfDao, ToscaPolicyFilter.builder().build()); + assertEquals(1, gotPolicyList.size()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); + + gotPolicyList = new AuthorativeToscaProvider().getFilteredPolicyList(pfDao, + ToscaPolicyFilter.builder().name(policyKey.getName()).build()); + assertEquals(1, gotPolicyList.size()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); + + gotPolicyList = new AuthorativeToscaProvider().getFilteredPolicyList(pfDao, + ToscaPolicyFilter.builder().name(policyKey.getName()).version("1.0.0").build()); + assertEquals(1, gotPolicyList.size()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); + } + + @Test + public void testPolicyCreate() throws Exception { + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().createPolicies(null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().createPolicies(null, new ToscaServiceTemplate()); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().createPolicies(pfDao, null); + }).hasMessage("serviceTemplate is marked @NonNull but is null"); + + ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( + ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"), + ToscaServiceTemplate.class); + + assertNotNull(toscaServiceTemplate); + ToscaServiceTemplate createdServiceTemplate = + new AuthorativeToscaProvider().createPolicies(pfDao, toscaServiceTemplate); + + PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0"); + + ToscaPolicy beforePolicy = + toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + ToscaPolicy createdPolicy = + createdServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, createdPolicy)); + assertTrue(beforePolicy.getType().equals(createdPolicy.getType())); + } + + + @Test + public void testPolicyUpdate() throws Exception { + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().createPolicies(null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().updatePolicies(null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().updatePolicies(null, new ToscaServiceTemplate()); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().updatePolicies(pfDao, null); + }).hasMessage("serviceTemplate is marked @NonNull but is null"); + + ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( + ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"), + ToscaServiceTemplate.class); + + assertNotNull(toscaServiceTemplate); + ToscaServiceTemplate createdServiceTemplate = + new AuthorativeToscaProvider().createPolicies(pfDao, toscaServiceTemplate); + + PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0"); + + ToscaPolicy beforePolicy = + toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + ToscaPolicy createdPolicy = + createdServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, createdPolicy)); + assertTrue(beforePolicy.getType().equals(createdPolicy.getType())); + + ToscaServiceTemplate updatedServiceTemplate = + new AuthorativeToscaProvider().updatePolicies(pfDao, toscaServiceTemplate); + + ToscaPolicy updatedPolicy = + updatedServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, updatedPolicy)); + assertTrue(beforePolicy.getType().equals(updatedPolicy.getType())); + } + + @Test + public void testPoliciesDelete() throws Exception { + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().deletePolicy(null, null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().deletePolicy(null, null, "version"); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().deletePolicy(null, "name", null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().deletePolicy(null, "name", "version"); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().deletePolicy(pfDao, null, null); + }).hasMessage("name is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().deletePolicy(pfDao, null, "version"); + }).hasMessage("name is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().deletePolicy(pfDao, "name", null); + }).hasMessage("version is marked @NonNull but is null"); + + ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( + ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"), + ToscaServiceTemplate.class); + + assertNotNull(toscaServiceTemplate); + ToscaServiceTemplate createdServiceTemplate = + new AuthorativeToscaProvider().createPolicies(pfDao, toscaServiceTemplate); + + PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0"); + + ToscaPolicy beforePolicy = + toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + ToscaPolicy createdPolicy = + createdServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, createdPolicy)); + assertTrue(beforePolicy.getType().equals(createdPolicy.getType())); + + ToscaServiceTemplate deletedServiceTemplate = + new AuthorativeToscaProvider().deletePolicy(pfDao, policyKey.getName(), policyKey.getVersion()); + + ToscaPolicy deletedPolicy = + deletedServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, createdPolicy)); + assertTrue(beforePolicy.getType().equals(deletedPolicy.getType())); + + ToscaServiceTemplate gotServiceTemplate = + new AuthorativeToscaProvider().getPolicies(pfDao, policyKey.getName(), policyKey.getVersion()); + + assertEquals(0, gotServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).size()); + } + + @Test + public void testAssertPoliciesExist() throws PfModelException { + ToscaServiceTemplate testServiceTemplate = new ToscaServiceTemplate(); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().deletePolicy(pfDao, "name", null); + }).hasMessage("version is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().createPolicies(pfDao, testServiceTemplate); + }).hasMessage("topology template not specified on service template"); + + testServiceTemplate.setToscaTopologyTemplate(new ToscaTopologyTemplate()); + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().createPolicies(pfDao, testServiceTemplate); + }).hasMessage("no policies specified on topology template of service template"); + + testServiceTemplate.getToscaTopologyTemplate().setPolicies(new ArrayList<>()); + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().createPolicies(pfDao, testServiceTemplate); + }).hasMessage("An incoming list of concepts must have at least one entry"); + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTypeTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTypeTest.java new file mode 100644 index 000000000..fbb5abf62 --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTypeTest.java @@ -0,0 +1,395 @@ +/*- + * ============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.authorative.provider; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.util.ArrayList; +import java.util.List; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.common.utils.resources.ResourceUtils; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.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.ToscaPolicy; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter; +import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; +import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate; + +/** + * Test of the {@link AuthorativeToscaProvider} class. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class AuthorativeToscaProviderPolicyTypeTest { + private Connection connection; + private PfDao pfDao; + private StandardCoder standardCoder; + + /** + * Set up the DAO towards the database. + * + * @throws Exception on database errors + */ + @Before + public void setupDao() throws Exception { + // Use the JDBC UI "jdbc:h2:mem:testdb" to test towards the h2 database + // Use the JDBC UI "jdbc:mariadb://localhost:3306/policy" to test towards a locally installed mariadb instance + connection = DriverManager.getConnection("jdbc:h2:mem:testdb", "policy", "P01icY"); + + final DaoParameters daoParameters = new DaoParameters(); + daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName()); + + // Use the persistence unit ToscaConceptTest to test towards the h2 database + // Use the persistence unit ToscaConceptMariaDBTest to test towards a locally installed mariadb instance + daoParameters.setPersistenceUnit("ToscaConceptTest"); + + pfDao = new PfDaoFactory().createPfDao(daoParameters); + pfDao.init(daoParameters); + } + + /** + * Set up GSON. + */ + @Before + public void setupGson() { + standardCoder = new StandardCoder(); + } + + @After + public void teardown() throws Exception { + pfDao.close(); + connection.close(); + } + + @Test + public void testPoliciesGet() throws Exception { + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().getPolicies(null, null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().getPolicyList(null, null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( + ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"), + ToscaServiceTemplate.class); + + assertNotNull(toscaServiceTemplate); + ToscaServiceTemplate createdServiceTemplate = + new AuthorativeToscaProvider().createPolicies(pfDao, toscaServiceTemplate); + + PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0"); + + ToscaPolicy beforePolicy = + toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + ToscaPolicy createdPolicy = + createdServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, createdPolicy)); + assertTrue(beforePolicy.getType().equals(createdPolicy.getType())); + + ToscaServiceTemplate gotServiceTemplate = + new AuthorativeToscaProvider().getPolicies(pfDao, policyKey.getName(), policyKey.getVersion()); + + ToscaPolicy gotPolicy = + gotServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicy)); + assertTrue(beforePolicy.getType().equals(gotPolicy.getType())); + + List gotPolicyList = + new AuthorativeToscaProvider().getPolicyList(pfDao, "onap.restart.tca", "1.0.0"); + assertEquals(1, gotPolicyList.size()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); + + gotPolicyList = new AuthorativeToscaProvider().getPolicyList(pfDao, "onap.restart.tca", null); + assertEquals(1, gotPolicyList.size()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); + + gotPolicyList = new AuthorativeToscaProvider().getPolicyList(pfDao, null, null); + assertEquals(1, gotPolicyList.size()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); + + gotPolicyList = new AuthorativeToscaProvider().getPolicyList(pfDao, null, "1.0.0"); + assertEquals(1, gotPolicyList.size()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); + } + + + @Test + public void testPoliciesGetFiltered() throws Exception { + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().getFilteredPolicies(null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().getFilteredPolicies(null, ToscaPolicyFilter.builder().build()); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().getFilteredPolicies(pfDao, null); + }).hasMessage("filter is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().getFilteredPolicyList(null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().getFilteredPolicyList(null, ToscaPolicyFilter.builder().build()); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().getFilteredPolicyList(pfDao, null); + }).hasMessage("filter is marked @NonNull but is null"); + + ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( + ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"), + ToscaServiceTemplate.class); + + assertNotNull(toscaServiceTemplate); + ToscaServiceTemplate createdServiceTemplate = + new AuthorativeToscaProvider().createPolicies(pfDao, toscaServiceTemplate); + + PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0"); + + ToscaPolicy beforePolicy = + toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + ToscaPolicy createdPolicy = + createdServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, createdPolicy)); + assertTrue(beforePolicy.getType().equals(createdPolicy.getType())); + + ToscaServiceTemplate gotServiceTemplate = + new AuthorativeToscaProvider().getFilteredPolicies(pfDao, ToscaPolicyFilter.builder().build()); + + ToscaPolicy gotPolicy = + gotServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicy)); + assertTrue(beforePolicy.getType().equals(gotPolicy.getType())); + + gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicies(pfDao, + ToscaPolicyFilter.builder().name(policyKey.getName()).build()); + + gotPolicy = gotServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicy)); + assertTrue(beforePolicy.getType().equals(gotPolicy.getType())); + + gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicies(pfDao, + ToscaPolicyFilter.builder().name(policyKey.getName()).version("1.0.0").build()); + + gotPolicy = gotServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicy)); + assertTrue(beforePolicy.getType().equals(gotPolicy.getType())); + + List gotPolicyList = + new AuthorativeToscaProvider().getPolicyList(pfDao, "onap.restart.tca", "1.0.0"); + assertEquals(1, gotPolicyList.size()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); + + gotPolicyList = + new AuthorativeToscaProvider().getFilteredPolicyList(pfDao, ToscaPolicyFilter.builder().build()); + assertEquals(1, gotPolicyList.size()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); + + gotPolicyList = new AuthorativeToscaProvider().getFilteredPolicyList(pfDao, + ToscaPolicyFilter.builder().name(policyKey.getName()).build()); + assertEquals(1, gotPolicyList.size()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); + + gotPolicyList = new AuthorativeToscaProvider().getFilteredPolicyList(pfDao, + ToscaPolicyFilter.builder().name(policyKey.getName()).version("1.0.0").build()); + assertEquals(1, gotPolicyList.size()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); + } + + @Test + public void testPolicyCreate() throws Exception { + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().createPolicies(null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().createPolicies(null, new ToscaServiceTemplate()); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().createPolicies(pfDao, null); + }).hasMessage("serviceTemplate is marked @NonNull but is null"); + + ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( + ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"), + ToscaServiceTemplate.class); + + assertNotNull(toscaServiceTemplate); + ToscaServiceTemplate createdServiceTemplate = + new AuthorativeToscaProvider().createPolicies(pfDao, toscaServiceTemplate); + + PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0"); + + ToscaPolicy beforePolicy = + toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + ToscaPolicy createdPolicy = + createdServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, createdPolicy)); + assertTrue(beforePolicy.getType().equals(createdPolicy.getType())); + } + + + @Test + public void testPolicyUpdate() throws Exception { + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().createPolicies(null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().updatePolicies(null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().updatePolicies(null, new ToscaServiceTemplate()); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().updatePolicies(pfDao, null); + }).hasMessage("serviceTemplate is marked @NonNull but is null"); + + ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( + ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"), + ToscaServiceTemplate.class); + + assertNotNull(toscaServiceTemplate); + ToscaServiceTemplate createdServiceTemplate = + new AuthorativeToscaProvider().createPolicies(pfDao, toscaServiceTemplate); + + PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0"); + + ToscaPolicy beforePolicy = + toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + ToscaPolicy createdPolicy = + createdServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, createdPolicy)); + assertTrue(beforePolicy.getType().equals(createdPolicy.getType())); + + ToscaServiceTemplate updatedServiceTemplate = + new AuthorativeToscaProvider().updatePolicies(pfDao, toscaServiceTemplate); + + ToscaPolicy updatedPolicy = + updatedServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, updatedPolicy)); + assertTrue(beforePolicy.getType().equals(updatedPolicy.getType())); + } + + @Test + public void testPoliciesDelete() throws Exception { + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().deletePolicy(null, null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().deletePolicy(null, null, "version"); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().deletePolicy(null, "name", null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().deletePolicy(null, "name", "version"); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().deletePolicy(pfDao, null, null); + }).hasMessage("name is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().deletePolicy(pfDao, null, "version"); + }).hasMessage("name is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().deletePolicy(pfDao, "name", null); + }).hasMessage("version is marked @NonNull but is null"); + + ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( + ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"), + ToscaServiceTemplate.class); + + assertNotNull(toscaServiceTemplate); + ToscaServiceTemplate createdServiceTemplate = + new AuthorativeToscaProvider().createPolicies(pfDao, toscaServiceTemplate); + + PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0"); + + ToscaPolicy beforePolicy = + toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + ToscaPolicy createdPolicy = + createdServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, createdPolicy)); + assertTrue(beforePolicy.getType().equals(createdPolicy.getType())); + + ToscaServiceTemplate deletedServiceTemplate = + new AuthorativeToscaProvider().deletePolicy(pfDao, policyKey.getName(), policyKey.getVersion()); + + ToscaPolicy deletedPolicy = + deletedServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, createdPolicy)); + assertTrue(beforePolicy.getType().equals(deletedPolicy.getType())); + + ToscaServiceTemplate gotServiceTemplate = + new AuthorativeToscaProvider().getPolicies(pfDao, policyKey.getName(), policyKey.getVersion()); + + assertEquals(0, gotServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).size()); + } + + @Test + public void testAssertPoliciesExist() throws PfModelException { + ToscaServiceTemplate testServiceTemplate = new ToscaServiceTemplate(); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().deletePolicy(pfDao, "name", null); + }).hasMessage("version is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().createPolicies(pfDao, testServiceTemplate); + }).hasMessage("topology template not specified on service template"); + + testServiceTemplate.setToscaTopologyTemplate(new ToscaTopologyTemplate()); + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().createPolicies(pfDao, testServiceTemplate); + }).hasMessage("no policies specified on topology template of service template"); + + testServiceTemplate.getToscaTopologyTemplate().setPolicies(new ArrayList<>()); + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().createPolicies(pfDao, testServiceTemplate); + }).hasMessage("An incoming list of concepts must have at least one entry"); + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/ToscaServiceTemplateMappingTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/ToscaServiceTemplateMappingTest.java new file mode 100644 index 000000000..a4458a874 --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/ToscaServiceTemplateMappingTest.java @@ -0,0 +1,106 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy Model + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Modifications 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.authorative.provider; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.common.utils.resources.ResourceUtils; +import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; +import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.yaml.snakeyaml.Yaml; + +/** + * This class performs unit test of {@link PlainToscaServiceTemplateMapper}}. + * + * @author Chenfei Gao (cgao@research.att.com) + */ +public class ToscaServiceTemplateMappingTest { + private static final Logger LOGGER = LoggerFactory.getLogger(ToscaServiceTemplateMappingTest.class); + + private StandardCoder standardCoder; + + @Before + public void setUp() { + standardCoder = new StandardCoder(); + } + + @Test + public void testPlainToscaPolicies() throws Exception { + try { + String inputJson = ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"); + + ToscaServiceTemplate plainPolicies = standardCoder.decode(inputJson, ToscaServiceTemplate.class); + JpaToscaServiceTemplate internalPolicies = new JpaToscaServiceTemplate(); + internalPolicies.fromAuthorative(plainPolicies); + + assertTrue(internalPolicies.validate(new PfValidationResult()).isValid()); + ToscaServiceTemplate plainPolicies2 = internalPolicies.toAuthorative(); + + ToscaPolicy pp1 = plainPolicies.getToscaTopologyTemplate().getPolicies().get(0).values().iterator().next(); + ToscaPolicy pp2 = plainPolicies2.getToscaTopologyTemplate().getPolicies().get(0).values().iterator().next(); + + assertEquals(pp1.getProperties().keySet(), pp2.getProperties().keySet()); + + } catch (Exception e) { + LOGGER.warn("no exception should be thrown", e); + fail("no exception should be thrown"); + } + } + + @Test + public void testPlainToscaPolicyTypes() throws Exception { + try { + Yaml yaml = new Yaml(); + String inputYaml = ResourceUtils.getResourceAsString( + "policytypes/onap.policy.monitoring.cdap.tca.hi.lo.app.yaml"); + Object yamlObject = yaml.load(inputYaml); + String yamlAsJsonString = standardCoder.encode(yamlObject); + + ToscaServiceTemplate plainPolicyTypes = standardCoder.decode(yamlAsJsonString, + ToscaServiceTemplate.class); + JpaToscaServiceTemplate internalPolicyTypes = new JpaToscaServiceTemplate(); + internalPolicyTypes.fromAuthorative(plainPolicyTypes); + assertTrue(internalPolicyTypes.validate(new PfValidationResult()).isValid()); + ToscaServiceTemplate plainPolicyTypes2 = internalPolicyTypes.toAuthorative(); + JpaToscaServiceTemplate internalPolicyTypes2 = new JpaToscaServiceTemplate(); + internalPolicyTypes2.fromAuthorative(plainPolicyTypes2); + assertTrue(internalPolicyTypes2.validate(new PfValidationResult()).isValid()); + assertTrue(internalPolicyTypes.compareTo(internalPolicyTypes2) == 0); + + } catch (Exception e) { + LOGGER.warn("no exception should be thrown", e); + fail("no exception should be thrown"); + } + + } +} 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 dca34b08e..d94c39936 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 @@ -54,7 +54,6 @@ public class SimpleToscaProviderTest { private PfDao pfDao; private StandardCoder standardCoder; - /** * Set up the DAO towards the database. * -- cgit 1.2.3-korg