Sonar Fixes policy/models, removing model-yaml
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / authorative / provider / ToscaServiceTemplateMappingTest.java
index a4458a8..5258053 100644 (file)
@@ -2,8 +2,8 @@
  * ============LICENSE_START=======================================================
  * ONAP Policy Model
  * ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2019 Nordix Foundation.
+ * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2019-2020 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -25,18 +25,17 @@ 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.coder.YamlJsonTranslator;
 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.onap.policy.models.tosca.utils.ToscaServiceTemplateUtils;
 import org.yaml.snakeyaml.Yaml;
 
 /**
@@ -45,9 +44,9 @@ import org.yaml.snakeyaml.Yaml;
  * @author Chenfei Gao (cgao@research.att.com)
  */
 public class ToscaServiceTemplateMappingTest {
-    private static final Logger LOGGER = LoggerFactory.getLogger(ToscaServiceTemplateMappingTest.class);
 
     private StandardCoder standardCoder;
+    private YamlJsonTranslator yamlJsonTranslator = new YamlJsonTranslator();
 
     @Before
     public void setUp() {
@@ -56,51 +55,48 @@ public class ToscaServiceTemplateMappingTest {
 
     @Test
     public void testPlainToscaPolicies() throws Exception {
-        try {
-            String inputJson = ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json");
+        String policyTypeInputJson =
+                ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.tcagen2.yaml");
+        ToscaServiceTemplate plainPolicyTypes =
+                yamlJsonTranslator.fromYaml(policyTypeInputJson, ToscaServiceTemplate.class);
 
-            ToscaServiceTemplate plainPolicies = standardCoder.decode(inputJson, ToscaServiceTemplate.class);
-            JpaToscaServiceTemplate internalPolicies = new JpaToscaServiceTemplate();
-            internalPolicies.fromAuthorative(plainPolicies);
+        String policyInputJson = ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json");
+        ToscaServiceTemplate plainPolicies = standardCoder.decode(policyInputJson, ToscaServiceTemplate.class);
 
-            assertTrue(internalPolicies.validate(new PfValidationResult()).isValid());
-            ToscaServiceTemplate plainPolicies2 = internalPolicies.toAuthorative();
+        JpaToscaServiceTemplate policyTypeServiceTemplate = new JpaToscaServiceTemplate();
+        policyTypeServiceTemplate.fromAuthorative(plainPolicyTypes);
 
-            ToscaPolicy pp1 = plainPolicies.getToscaTopologyTemplate().getPolicies().get(0).values().iterator().next();
-            ToscaPolicy pp2 = plainPolicies2.getToscaTopologyTemplate().getPolicies().get(0).values().iterator().next();
+        JpaToscaServiceTemplate policyFragmentServiceTemplate = new JpaToscaServiceTemplate();
+        policyFragmentServiceTemplate.fromAuthorative(plainPolicies);
 
-            assertEquals(pp1.getProperties().keySet(), pp2.getProperties().keySet());
+        JpaToscaServiceTemplate internalServiceTemplate =
+                ToscaServiceTemplateUtils.addFragment(policyTypeServiceTemplate, policyFragmentServiceTemplate);
 
-        } catch (Exception e) {
-            LOGGER.warn("no exception should be thrown", e);
-            fail("no exception should be thrown");
-        }
+        assertTrue(internalServiceTemplate.validate(new PfValidationResult()).isValid());
+        ToscaServiceTemplate plainPolicies2 = internalServiceTemplate.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());
     }
 
     @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");
-        }
-
+        Yaml yaml = new Yaml();
+        String inputYaml =
+                ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.tcagen2.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());
+        assertEquals(0, internalPolicyTypes.compareTo(internalPolicyTypes2));
     }
 }