Refactor to authorative TOSCA serializtion
[policy/models.git] / models-provider / src / test / java / org / onap / policy / models / provider / impl / PolicyToscaPersistenceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.provider.impl;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.fail;
26
27 import com.google.gson.GsonBuilder;
28
29 import java.util.Base64;
30
31 import lombok.NonNull;
32
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.onap.policy.common.utils.coder.StandardCoder;
37 import org.onap.policy.common.utils.resources.ResourceUtils;
38 import org.onap.policy.models.base.PfModelException;
39 import org.onap.policy.models.provider.PolicyModelsProvider;
40 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
41 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.yaml.snakeyaml.Yaml;
47
48 /**
49  * Test persistence of monitoring policies to and from the database.
50  *
51  * @author Liam Fallon (liam.fallon@est.tech)
52  */
53 public class PolicyToscaPersistenceTest {
54     // Logger for this class
55     private static final Logger LOGGER = LoggerFactory.getLogger(PolicyToscaPersistenceTest.class);
56
57     private StandardCoder standardCoder;
58
59     private PolicyModelsProvider databaseProvider;
60
61     // @formatter:off
62     private String[] policyResourceNames = {
63         "policies/vCPE.policy.monitoring.input.tosca.json",
64         "policies/vCPE.policy.monitoring.input.tosca.yaml",
65         "policies/vCPE.policy.operational.input.tosca.yaml",
66         "policies/vDNS.policy.guard.frequency.input.tosca.json",
67         "policies/vDNS.policy.guard.frequency.input.tosca.yaml",
68         "policies/vDNS.policy.monitoring.input.tosca.json",
69         "policies/vDNS.policy.monitoring.input.tosca.yaml",
70         "policies/vDNS.policy.operational.input.tosca.yaml",
71         "policies/vFirewall.policy.monitoring.input.tosca.json",
72         "policies/vFirewall.policy.monitoring.input.tosca.yaml",
73         "policies/vFirewall.policy.operational.input.tosca.json",
74         "policies/vFirewall.policy.operational.input.tosca.yaml"
75     };
76     // @formatter:on
77
78     /**
79      * Initialize provider.
80      *
81      * @throws PfModelException on exceptions in the tests
82      */
83     @Before
84     public void setupParameters() throws PfModelException {
85         PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters();
86         parameters.setDatabaseUrl("jdbc:h2:mem:testdb");
87         parameters.setDatabaseUser("policy");
88         parameters.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
89         parameters.setPersistenceUnit("ToscaConceptTest");
90
91         databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
92         databaseProvider.init();
93     }
94
95     /**
96      * Set up the standard coder.
97      */
98     @Before
99     public void setupStandardCoder() {
100         standardCoder = new StandardCoder();
101     }
102
103     @After
104     public void teardown() throws Exception {
105         databaseProvider.close();
106     }
107
108     @Test
109     public void testPolicyPersistence() {
110         try {
111             for (String policyResourceName : policyResourceNames) {
112                 String policyString = ResourceUtils.getResourceAsString(policyResourceName);
113
114                 if (policyResourceName.endsWith("yaml")) {
115                     testYamlStringPolicyPersistence(policyString);
116                 } else {
117                     testJsonStringPolicyPersistence(policyString);
118                 }
119             }
120         } catch (Exception exc) {
121             LOGGER.warn("error processing policies", exc);
122             fail("test should not throw an exception");
123         }
124     }
125
126     private void testYamlStringPolicyPersistence(final String policyString) throws Exception {
127         Object yamlObject = new Yaml().load(policyString);
128         String yamlAsJsonString = new GsonBuilder().setPrettyPrinting().create().toJson(yamlObject);
129
130         testJsonStringPolicyPersistence(yamlAsJsonString);
131     }
132
133     /**
134      * Check persistence of a policy.
135      *
136      * @param policyString the policy as a string
137      * @throws Exception any exception thrown
138      */
139     public void testJsonStringPolicyPersistence(@NonNull final String policyString) throws Exception {
140         ToscaServiceTemplate serviceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class);
141
142         assertNotNull(serviceTemplate);
143
144         databaseProvider.createPolicies(serviceTemplate);
145
146         for (String policyKey : serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).keySet()) {
147             ToscaPolicy incomingPolicy = serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey);
148             ToscaPolicy databasePolicy =
149                     databaseProvider.getPolicies(incomingPolicy.getName(), incomingPolicy.getVersion())
150                             .getToscaTopologyTemplate().getPolicies().get(0).get(policyKey);
151             assertEquals(incomingPolicy.getType(), databasePolicy.getType());
152         }
153     }
154 }