summaryrefslogtreecommitdiffstats
path: root/models-tosca/src/test
diff options
context:
space:
mode:
authorrameshiyer27 <ramesh.murugan.iyer@est.tech>2022-02-07 17:07:38 +0000
committerrameshiyer27 <ramesh.murugan.iyer@est.tech>2022-02-10 14:20:42 +0000
commit45b653fc5a8d641452247eca5c80cf580609e9bf (patch)
tree06cfd4529db05dc23cbd63d2fe2e53799eb07bdf /models-tosca/src/test
parent303d42453fbfcf2381f810b10a496b69aa8bc34b (diff)
Add policy metadataSet handling as node templates
Each metadataSet is represented as a 'node_template' in Tosca mapped to a specific node type. Support added for db operations of node templates as independent entities. Detailed documentation available here : https://wiki.onap.org/display/DW/Enable+Handling+of+Policy+Type+Metadata Issue-ID: POLICY-3832 Signed-off-by: zrrmmua <ramesh.murugan.iyer@est.tech> Change-Id: I6c189142b1778ba858aae27cd92d4f136d950208
Diffstat (limited to 'models-tosca/src/test')
-rw-r--r--models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderNodeTemplateTest.java315
-rw-r--r--models-tosca/src/test/resources/nodetemplates/TestCreateMetadataSet.json31
-rw-r--r--models-tosca/src/test/resources/nodetemplates/TestUpdateMetadataSet.json23
3 files changed, 369 insertions, 0 deletions
diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderNodeTemplateTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderNodeTemplateTest.java
new file mode 100644
index 000000000..21559344f
--- /dev/null
+++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderNodeTemplateTest.java
@@ -0,0 +1,315 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2022 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.assertThat;
+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.util.List;
+import java.util.Map;
+import java.util.Properties;
+import org.eclipse.persistence.config.PersistenceUnitProperties;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onap.policy.common.utils.coder.CoderException;
+import org.onap.policy.common.utils.coder.StandardCoder;
+import org.onap.policy.common.utils.coder.YamlJsonTranslator;
+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.ToscaEntityKey;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
+import org.yaml.snakeyaml.Yaml;
+
+/**
+ * Test of the {@link AuthorativeToscaProvider} class.
+ */
+public class AuthorativeToscaProviderNodeTemplateTest {
+
+
+ private static final String NODE_TEMPLATES_JSON = "nodetemplates/nodetemplates.metadatasets.input.tosca.json";
+ private static final String UPDATED_METADATA_SET_JSON = "nodetemplates/TestUpdateMetadataSet.json";
+ private static final String CREATE_METADATA_SET_JSON = "nodetemplates/TestCreateMetadataSet.json";
+ private static final String POLICY_WITH_METADATA_SET_REF = "policies/apex.policy.decisionmaker.input.tosca.yaml";
+ private static final String APEX_POLICY_TYPE_YAML = "policytypes/onap.policies.native.Apex.yaml";
+ private static final String DAO_IS_NULL = "^dao is marked .*on.*ull but is null$";
+ private static ToscaServiceTemplate toscaServiceTemplate;
+ private static ToscaServiceTemplate updatedToscaServiceTemplate;
+ private static ToscaServiceTemplate createToscaNodeTemplate;
+ private PfDao pfDao;
+ private StandardCoder standardCoder;
+ private AuthorativeToscaProvider authorativeToscaProvider = new AuthorativeToscaProvider();
+ private YamlJsonTranslator yamlJsonTranslator = new YamlJsonTranslator();
+
+ /**
+ * Read policy metadataSet input json.
+ * @throws Exception Coder exception
+ */
+ @Before
+ public void fetchToscaNodeTemplatesJson() throws Exception {
+ standardCoder = new StandardCoder();
+ toscaServiceTemplate =
+ standardCoder.decode(ResourceUtils.getResourceAsString(NODE_TEMPLATES_JSON), ToscaServiceTemplate.class);
+ updatedToscaServiceTemplate =
+ standardCoder.decode(ResourceUtils.getResourceAsString(UPDATED_METADATA_SET_JSON),
+ ToscaServiceTemplate.class);
+ createToscaNodeTemplate = standardCoder.decode(ResourceUtils.getResourceAsString(
+ CREATE_METADATA_SET_JSON), ToscaServiceTemplate.class);
+ }
+
+ /**
+ * Set up DAO towards the database.
+ *
+ * @throws Exception on database errors
+ */
+ @Before
+ public void setupDao() throws Exception {
+ final DaoParameters daoParameters = new DaoParameters();
+ daoParameters.setPluginClass(DefaultPfDao.class.getName());
+
+ daoParameters.setPersistenceUnit("ToscaConceptTest");
+
+ Properties jdbcProperties = new Properties();
+ jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, "policy");
+ jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "P01icY");
+
+ if (System.getProperty("USE-MARIADB") != null) {
+ jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.mariadb.jdbc.Driver");
+ jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:mariadb://localhost:3306/policy");
+ } else {
+ jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver");
+ jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL,
+ "jdbc:h2:mem:AuthorativeToscaProviderNodeTemplatesTest");
+ }
+
+ daoParameters.setJdbcProperties(jdbcProperties);
+
+ pfDao = new PfDaoFactory().createPfDao(daoParameters);
+ pfDao.init(daoParameters);
+ }
+
+ @After
+ public void teardown() {
+ pfDao.close();
+ }
+
+ @Test
+ public void testPolicyMetadataSetsGet() throws Exception {
+
+ assertThatThrownBy(() -> {
+ authorativeToscaProvider.getNodeTemplateMetadataSet(null, null, null);
+ }).hasMessageMatching(DAO_IS_NULL);
+
+ assertNotNull(toscaServiceTemplate);
+ authorativeToscaProvider.createToscaNodeTemplates(pfDao, toscaServiceTemplate);
+ List<Map<PfConceptKey, ToscaNodeTemplate>> gotPolicyMetadataSets1 = authorativeToscaProvider
+ .getToscaNodeTemplate(pfDao, null, null);
+ assertEquals(3, gotPolicyMetadataSets1.size());
+
+ //Fetch all metadataSet if id is null
+ List<Map<ToscaEntityKey, Map<String, Object>>> gotPolicyMetadataSets = authorativeToscaProvider
+ .getNodeTemplateMetadataSet(pfDao, null, null);
+ assertEquals(3, gotPolicyMetadataSets.size());
+
+ // Get filtered metadataSet
+ List<Map<ToscaEntityKey, Map<String, Object>>> filteredPolicyMetadataSet = authorativeToscaProvider
+ .getNodeTemplateMetadataSet(pfDao, "apexMetadata_adaptive", "2.3.1");
+ assertEquals(1, filteredPolicyMetadataSet.size());
+
+ //Get invalid metadataSet
+ List<Map<ToscaEntityKey, Map<String, Object>>> filteredMetadataSetInvalid = authorativeToscaProvider
+ .getNodeTemplateMetadataSet(pfDao, "invalidname", "1.0.0");
+ assertThat(filteredMetadataSetInvalid).isEmpty();
+ }
+
+ @Test
+ public void testToscaNodeTemplatesGet() throws Exception {
+
+ assertThatThrownBy(() -> {
+ authorativeToscaProvider.getToscaNodeTemplate(null, null, null);
+ }).hasMessageMatching(DAO_IS_NULL);
+
+ assertNotNull(toscaServiceTemplate);
+ authorativeToscaProvider.createToscaNodeTemplates(pfDao, toscaServiceTemplate);
+
+ //Fetch all node templates if id is null
+ List<Map<PfConceptKey, ToscaNodeTemplate>> gotToscaNodeTemplates = authorativeToscaProvider
+ .getToscaNodeTemplate(pfDao, null, null);
+ assertEquals(3, gotToscaNodeTemplates.size());
+
+ // Get filtered node templates
+ List<Map<PfConceptKey, ToscaNodeTemplate>> filteredNodeTemplates = authorativeToscaProvider
+ .getToscaNodeTemplate(pfDao, "apexMetadata_adaptive", "2.3.1");
+ assertEquals(1, filteredNodeTemplates.size());
+
+ //Get invalid node template
+ List<Map<PfConceptKey, ToscaNodeTemplate>> filteredNodeTemplatesInvalid = authorativeToscaProvider
+ .getToscaNodeTemplate(pfDao, "invalidname", "1.0.0");
+ assertThat(filteredNodeTemplatesInvalid).isEmpty();
+ }
+
+ @Test
+ public void testToscaNodeTemplatesCreate() throws Exception {
+ assertThatThrownBy(() -> {
+ authorativeToscaProvider.createToscaNodeTemplates(null, null);
+ }).hasMessageMatching(DAO_IS_NULL);
+
+ assertThatThrownBy(() -> {
+ authorativeToscaProvider.createToscaNodeTemplates(null, new ToscaServiceTemplate());
+ }).hasMessageMatching(DAO_IS_NULL);
+
+ assertThatThrownBy(() -> {
+ authorativeToscaProvider.createToscaNodeTemplates(pfDao, null);
+ }).hasMessageMatching("^toscaServiceTemplate is marked .*on.*ull but is null$");
+
+ ToscaServiceTemplate createdNodeTemplates =
+ authorativeToscaProvider.createToscaNodeTemplates(pfDao, toscaServiceTemplate);
+ assertThat(createdNodeTemplates.getToscaTopologyTemplate().getNodeTemplates()).hasSize(3);
+ assertThat(createdNodeTemplates.getToscaTopologyTemplate().getNodeTemplates().get("apexMetadata_grpc")
+ .getMetadata()).containsKey("threshold");
+
+ authorativeToscaProvider.createToscaNodeTemplates(pfDao, createToscaNodeTemplate);
+ assertThat(authorativeToscaProvider.getNodeTemplateMetadataSet(pfDao, null, null)).hasSize(4);
+
+ //Create node template with invalid node type
+ createToscaNodeTemplate.getToscaTopologyTemplate().getNodeTemplates().get("apexMetadata_adaptive")
+ .setType("invalid.type");
+ assertThatThrownBy(() -> {
+ authorativeToscaProvider.createToscaNodeTemplates(pfDao, createToscaNodeTemplate);
+ }).hasMessageMatching("^NODE_TYPE .* for toscaNodeTemplate .* does not exist$");
+
+ }
+
+ @Test
+ public void testToscaNodeTemplateUpdate() throws Exception {
+ assertThatThrownBy(() -> {
+ authorativeToscaProvider.updateToscaNodeTemplates(null, null);
+ }).hasMessageMatching(DAO_IS_NULL);
+
+ assertThatThrownBy(() -> {
+ authorativeToscaProvider.updateToscaNodeTemplates(null, new ToscaServiceTemplate());
+ }).hasMessageMatching(DAO_IS_NULL);
+
+ assertThatThrownBy(() -> {
+ authorativeToscaProvider.updateToscaNodeTemplates(pfDao, null);
+ }).hasMessageMatching("^serviceTemplate is marked non-null but is null$");
+
+ authorativeToscaProvider.createToscaNodeTemplates(pfDao, toscaServiceTemplate);
+ ToscaServiceTemplate updatedTemplate =
+ authorativeToscaProvider.updateToscaNodeTemplates(pfDao, updatedToscaServiceTemplate);
+ assertEquals("Updated Metadata set for GRPC",
+ updatedTemplate.getToscaTopologyTemplate().getNodeTemplates().get("apexMetadata_grpc")
+ .getDescription());
+
+ //Update nodeTemplate with invalid node type
+ updatedToscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates().get("apexMetadata_grpc")
+ .setTypeVersion("0.0.0");
+ assertThatThrownBy(() -> {
+ authorativeToscaProvider.updateToscaNodeTemplates(pfDao, updatedToscaServiceTemplate);
+ }).hasMessageMatching("^NODE_TYPE .* for toscaNodeTemplate .* does not exist$");
+ }
+
+ @Test
+ public void testToscaNodeTemplatetDelete() throws Exception {
+ assertThatThrownBy(() -> {
+ authorativeToscaProvider.deleteToscaNodeTemplate(null, null, null);
+ }).hasMessageMatching(DAO_IS_NULL);
+
+ assertThatThrownBy(() -> {
+ authorativeToscaProvider.deleteToscaNodeTemplate(null, null, "0.0.1");
+ }).hasMessageMatching(DAO_IS_NULL);
+
+ assertThatThrownBy(() -> {
+ authorativeToscaProvider.deleteToscaNodeTemplate(pfDao, null, null);
+ }).hasMessageMatching("^name is marked .*on.*ull but is null$");
+
+ assertThatThrownBy(() -> {
+ authorativeToscaProvider.deleteToscaNodeTemplate(pfDao, "name", null);
+ }).hasMessageMatching("^version is marked .*on.*ull but is null$");
+
+ authorativeToscaProvider.createToscaNodeTemplates(pfDao, toscaServiceTemplate);
+ assertThatThrownBy(() -> {
+ authorativeToscaProvider.deleteToscaNodeTemplate(pfDao, "dummyname", "1.0.1");
+ }).hasMessage("node template dummyname:1.0.1 not found");
+
+
+ ToscaServiceTemplate responseTemplate =
+ authorativeToscaProvider.deleteToscaNodeTemplate(pfDao, "apexMetadata_decisionMaker",
+ "1.0.0");
+
+ assertTrue(responseTemplate.getToscaTopologyTemplate().getNodeTemplates()
+ .containsKey("apexMetadata_decisionMaker"));
+ assertThat(responseTemplate.getToscaTopologyTemplate().getNodeTemplates()).hasSize(1);
+
+ assertThat(authorativeToscaProvider.getNodeTemplateMetadataSet(pfDao, null, null)).hasSize(2);
+
+ }
+
+ @Test
+ public void testNodeTemplatesWithExistingPolicy() throws Exception {
+ String policyString = ResourceUtils.getResourceAsString(POLICY_WITH_METADATA_SET_REF);
+ ToscaServiceTemplate policyServiceTemplate =
+ yamlJsonTranslator.fromYaml(policyString, ToscaServiceTemplate.class);
+
+ createPolicyTypes();
+ //Create policy with metadataSet reference in it
+ authorativeToscaProvider.createPolicies(pfDao, policyServiceTemplate);
+ assertThat(authorativeToscaProvider.getPolicyList(pfDao, null, null)).hasSize(1);
+ assertEquals("apexMetadata_decisionMaker", authorativeToscaProvider
+ .getPolicyList(pfDao, null, null).get(0).getMetadata().get("metadataSetName"));
+
+ //Create node templates
+ authorativeToscaProvider.createToscaNodeTemplates(pfDao, toscaServiceTemplate);
+
+ //delete node templates referenced in existing policy
+ assertThatThrownBy(() -> {
+ authorativeToscaProvider
+ .deleteToscaNodeTemplate(pfDao, "apexMetadata_decisionMaker", "1.0.0");
+ }).hasMessageEndingWith("Node template is in use, it is referenced in Tosca Policy "
+ + "operational.apex.decisionMaker version 1.0.0");
+
+ //delete unreferenced node template
+ authorativeToscaProvider.deleteToscaNodeTemplate(pfDao, "apexMetadata_adaptive", "2.3.1");
+ assertThat(authorativeToscaProvider.getNodeTemplateMetadataSet(pfDao, null, null)).hasSize(2);
+ }
+
+ private void createPolicyTypes() throws CoderException, PfModelException {
+ Object yamlObject =
+ new Yaml().load(ResourceUtils.getResourceAsString(APEX_POLICY_TYPE_YAML));
+ String yamlAsJsonString = new StandardCoder().encode(yamlObject);
+
+ ToscaServiceTemplate toscaServiceTemplatePolicyType =
+ standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
+
+ assertNotNull(toscaServiceTemplatePolicyType);
+ new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplatePolicyType);
+ }
+
+}
diff --git a/models-tosca/src/test/resources/nodetemplates/TestCreateMetadataSet.json b/models-tosca/src/test/resources/nodetemplates/TestCreateMetadataSet.json
new file mode 100644
index 000000000..8620d3ef0
--- /dev/null
+++ b/models-tosca/src/test/resources/nodetemplates/TestCreateMetadataSet.json
@@ -0,0 +1,31 @@
+{
+ "tosca_definitions_version": "tosca_simple_yaml_1_1_0",
+ "node_types": {
+ "org.onap.nodetypes.policy.MetadataSet2": {
+ "derived_from": "tosca.nodetypes.Root",
+ "version": "1.0.1"
+ }
+ },
+ "topology_template": {
+ "node_templates": {
+ "apexMetadata_adaptive": {
+ "version": "2.3.3",
+ "type": "org.onap.nodetypes.policy.MetadataSet2",
+ "type_version": "1.0.1",
+ "description": "Metadata set for an Adaptive Policy",
+ "metadata": {
+ "policyModel": {
+ "key": {
+ "name": "AdaptivePolicyModel",
+ "version": "1.2.2"
+ }
+ },
+ "radius": 1.23,
+ "height": 2.13,
+ "length": 46
+ }
+ }
+
+ }
+ }
+} \ No newline at end of file
diff --git a/models-tosca/src/test/resources/nodetemplates/TestUpdateMetadataSet.json b/models-tosca/src/test/resources/nodetemplates/TestUpdateMetadataSet.json
new file mode 100644
index 000000000..7fbad818c
--- /dev/null
+++ b/models-tosca/src/test/resources/nodetemplates/TestUpdateMetadataSet.json
@@ -0,0 +1,23 @@
+{
+ "tosca_definitions_version": "tosca_simple_yaml_1_1_0",
+ "topology_template": {
+ "node_templates": {
+ "apexMetadata_grpc": {
+ "version": "1.2.1",
+ "type": "org.onap.nodetypes.policy.MetadataSet",
+ "type_version": "1.0.0",
+ "description": "Updated Metadata set for GRPC",
+ "metadata": {
+ "policyModel": {
+ "key": {
+ "name": "GrpcPolicyModel",
+ "version": "1.0.1"
+ }
+ },
+ "threshold": 3.15,
+ "state": "active"
+ }
+ }
+ }
+ }
+} \ No newline at end of file