diff options
author | liamfallon <liam.fallon@est.tech> | 2019-04-09 16:58:20 +0000 |
---|---|---|
committer | liamfallon <liam.fallon@est.tech> | 2019-04-09 16:58:20 +0000 |
commit | 942cd0a012a2a4e9fd44e680dc6c4c7f4bcf8c62 (patch) | |
tree | 532643c15d2a4dc0823fce3c0372ac1ee07e444b /models-tosca/src | |
parent | e678e5af567040022f23ed7a1ba1723b82434418 (diff) |
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 <liam.fallon@est.tech>
Diffstat (limited to 'models-tosca/src')
8 files changed, 800 insertions, 6 deletions
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<ToscaServiceTemp @SerializedName("tosca_definitions_version") private String toscaDefinitionsVersion; - @OneToOne(cascade = CascadeType.ALL) + @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) @SerializedName("data_types") private JpaToscaDataTypes dataTypes; - @OneToOne(cascade = CascadeType.ALL) + @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) @SerializedName("policy_types") private JpaToscaPolicyTypes policyTypes; + @Column @SerializedName("topology_template") private JpaToscaTopologyTemplate topologyTemplate; diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTopologyTemplate.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTopologyTemplate.java index 3476258cf..b1c3f209d 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTopologyTemplate.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTopologyTemplate.java @@ -26,6 +26,7 @@ import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.EmbeddedId; import javax.persistence.Entity; +import javax.persistence.FetchType; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.OneToOne; @@ -68,7 +69,7 @@ public class JpaToscaTopologyTemplate extends PfConcept implements PfAuthorative @Column(name = "description") private String description; - @OneToOne(cascade = CascadeType.ALL) + @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) private JpaToscaPolicies policies; /** 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 4653296b7..84a0df00b 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 @@ -95,6 +95,7 @@ public class ToscaPolicyFilterTest { if (policy.getName() == null) { policy.setName(policyEntry.getKey()); } + if (policy.getVersion() == null) { policy.setVersion(PfKey.NULL_KEY_VERSION); } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTest.java new file mode 100644 index 000000000..441bbcb18 --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTest.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 AuthorativeToscaProviderPolicyTest { + 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<ToscaPolicy> 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<ToscaPolicy> 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<ToscaPolicy> 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<ToscaPolicy> 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/mapping/ToscaServiceTemplateMappingTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/ToscaServiceTemplateMappingTest.java index 1bac0b973..a4458a874 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/mapping/ToscaServiceTemplateMappingTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/ToscaServiceTemplateMappingTest.java @@ -21,7 +21,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.models.tosca.authorative.mapping; +package org.onap.policy.models.tosca.authorative.provider; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; 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. * |