7f90a0b582c22720458be5b946ad29481b14bc0c
[policy/models.git] / models-provider / src / test / java / org / onap / policy / models / provider / impl / PolicyTypePersistenceTest.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 import java.util.List;
31
32 import lombok.NonNull;
33
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.onap.policy.common.utils.coder.StandardCoder;
38 import org.onap.policy.common.utils.resources.ResourceUtils;
39 import org.onap.policy.models.base.PfModelException;
40 import org.onap.policy.models.provider.PolicyModelsProvider;
41 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
42 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47 import org.yaml.snakeyaml.Yaml;
48
49 /**
50  * Test persistence of monitoring policies to and from the database.
51  *
52  * @author Liam Fallon (liam.fallon@est.tech)
53  */
54 public class PolicyTypePersistenceTest {
55     // Logger for this class
56     private static final Logger LOGGER = LoggerFactory.getLogger(PolicyTypePersistenceTest.class);
57
58     private StandardCoder standardCoder;
59
60     private PolicyModelsProvider databaseProvider;
61
62     // @formatter:off
63     private String[] policyTypeResourceNames = {
64         "policytypes/onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server.yaml",
65         "policytypes/onap.policies.optimization.AffinityPolicy.yaml",
66         "policytypes/onap.policies.optimization.DistancePolicy.yaml",
67         "policytypes/onap.policies.optimization.HpaPolicy.yaml",
68         "policytypes/onap.policies.optimization.OptimizationPolicy.yaml",
69         "policytypes/onap.policies.optimization.PciPolicy.yaml",
70         "policytypes/onap.policies.optimization.QueryPolicy.yaml",
71         "policytypes/onap.policies.optimization.SubscriberPolicy.yaml",
72         "policytypes/onap.policies.optimization.Vim_fit.yaml",
73         "policytypes/onap.policies.optimization.VnfPolicy.yaml",
74         "policytypes/onap.policy.monitoring.cdap.tca.hi.lo.app.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     }
93
94     /**
95      * Set up GSON.
96      */
97     @Before
98     public void setupGson() {
99         standardCoder = new StandardCoder();
100     }
101
102     @After
103     public void teardown() throws Exception {
104         databaseProvider.close();
105     }
106
107     @Test
108     public void testPolicyPersistence() {
109         try {
110             for (String policyTypeResourceName : policyTypeResourceNames) {
111                 String policyTypeString = ResourceUtils.getResourceAsString(policyTypeResourceName);
112
113                 if (policyTypeResourceName.endsWith("yaml")) {
114                     testYamlStringPolicyTypePersistence(policyTypeString);
115                 } else {
116                     testJsonStringPolicyTypePersistence(policyTypeString);
117                 }
118             }
119         } catch (Exception exc) {
120             LOGGER.warn("error processing policies", exc);
121             fail("test should not throw an exception");
122         }
123     }
124
125     private void testYamlStringPolicyTypePersistence(final String policyTypeString) throws Exception {
126         Object yamlObject = new Yaml().load(policyTypeString);
127         String yamlAsJsonString = new GsonBuilder().setPrettyPrinting().create().toJson(yamlObject);
128
129         testJsonStringPolicyTypePersistence(yamlAsJsonString);
130     }
131
132     /**
133      * Check persistence of a policy.
134      *
135      * @param policyTypeString the policy as a string
136      * @throws Exception any exception thrown
137      */
138     public void testJsonStringPolicyTypePersistence(@NonNull final String policyTypeString) throws Exception {
139         ToscaServiceTemplate serviceTemplate = standardCoder.decode(policyTypeString, ToscaServiceTemplate.class);
140
141         assertNotNull(serviceTemplate);
142         ToscaPolicyType inPolicyType = serviceTemplate.getPolicyTypes().get(0).values().iterator().next();
143
144         databaseProvider.createPolicyTypes(serviceTemplate);
145
146         List<ToscaPolicyType> policyTypeList =
147                 databaseProvider.getPolicyTypeList(inPolicyType.getName(), inPolicyType.getVersion());
148
149         assertEquals(1, policyTypeList.size());
150         assertEquals(inPolicyType.getName(), policyTypeList.get(0).getName());
151     }
152 }