Merge "Do not require context in ControlLoopOperationParams"
[policy/models.git] / models-provider / src / test / java / org / onap / policy / models / provider / impl / PolicyLegacyOperationalPersistenceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2020 Nordix Foundation.
4  *  Modifications Copyright (C) 2020 AT&T.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.provider.impl;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26
27 import java.net.URISyntaxException;
28 import java.util.Base64;
29 import java.util.Set;
30 import lombok.NonNull;
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.policy.common.utils.coder.CoderException;
35 import org.onap.policy.common.utils.coder.StandardCoder;
36 import org.onap.policy.common.utils.resources.ResourceUtils;
37 import org.onap.policy.models.base.PfModelException;
38 import org.onap.policy.models.provider.PolicyModelsProvider;
39 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
40 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
42 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
43 import org.yaml.snakeyaml.Yaml;
44
45 /**
46  * Test persistence of monitoring policies to and from the database.
47  *
48  * @author Liam Fallon (liam.fallon@est.tech)
49  */
50 public class PolicyLegacyOperationalPersistenceTest {
51     private StandardCoder standardCoder;
52
53     private PolicyModelsProvider databaseProvider;
54
55     // @formatter:off
56     private String[] policyInputResourceNames = {
57         "policies/vCPE.policy.operational.legacy.input.json",
58         "policies/vDNS.policy.operational.legacy.input.json",
59         "policies/vFirewall.policy.operational.legacy.input.json"
60     };
61
62     private String[] policyOutputResourceNames = {
63         "policies/vCPE.policy.operational.legacy.output.json",
64         "policies/vDNS.policy.operational.legacy.output.json",
65         "policies/vFirewall.policy.operational.legacy.output.json"
66     };
67     // @formatter:on
68
69     /**
70      * Initialize provider.
71      *
72      * @throws PfModelException on exceptions in the tests
73      * @throws CoderException on JSON encoding and decoding errors
74      */
75     @Before
76     public void setupParameters() throws Exception {
77         // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB
78
79         PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters();
80         parameters.setDatabaseDriver("org.h2.Driver");
81         parameters.setDatabaseUrl("jdbc:h2:mem:testdb");
82         parameters.setDatabaseUser("policy");
83         parameters.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
84         parameters.setPersistenceUnit("ToscaConceptTest");
85
86         databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
87
88         createPolicyTypes();
89     }
90
91     /**
92      * Set up standard coder.
93      */
94     @Before
95     public void setupStandardCoder() {
96         standardCoder = new StandardCoder();
97     }
98
99     @After
100     public void teardown() throws Exception {
101         databaseProvider.close();
102     }
103
104     @Test
105     public void testLegacyOperationalPolicyPersistence() throws Exception {
106         for (int i = 0; i < policyInputResourceNames.length; i++) {
107             String policyInputString = ResourceUtils.getResourceAsString(policyInputResourceNames[i]);
108             String policyOutputString = ResourceUtils.getResourceAsString(policyOutputResourceNames[i]);
109             testJsonStringPolicyPersistence(policyInputString, policyOutputString);
110         }
111     }
112
113     /**
114      * Check persistence of a policy.
115      *
116      * @param policyInputString the policy as a string
117      * @param policyOutputString the expected output string
118      * @throws Exception any exception thrown
119      */
120     public void testJsonStringPolicyPersistence(@NonNull final String policyInputString,
121             final String policyOutputString) throws Exception {
122         LegacyOperationalPolicy lop = standardCoder.decode(policyInputString, LegacyOperationalPolicy.class);
123
124         assertNotNull(lop);
125
126         LegacyOperationalPolicy createdLop = databaseProvider.createOperationalPolicy(lop);
127         assertEquals(createdLop, lop);
128
129         LegacyOperationalPolicy gotLop = databaseProvider.getOperationalPolicy(lop.getPolicyId(), null);
130         assertEquals(gotLop, lop);
131
132         LegacyOperationalPolicy updatedLop = databaseProvider.updateOperationalPolicy(lop);
133         assertEquals(gotLop, updatedLop);
134
135         LegacyOperationalPolicy deletedLop = databaseProvider.deleteOperationalPolicy(lop.getPolicyId(), "1");
136         assertEquals(gotLop, deletedLop);
137
138         String actualRetrievedJson = standardCoder.encode(gotLop);
139
140         // All of this dash/underscore stuff is to avoid a checkstyle error around escaping unicode characters
141         assertEquals(
142                 policyOutputString.replaceAll("\\s+", "").replaceAll("u0027", "_-_-_-_").replaceAll("\\\\_-_-_-_", "'"),
143                 actualRetrievedJson.replaceAll("\\s+", "").replaceAll("u0027", "_-_-_-_").replaceAll("\\\\_-_-_-_",
144                         "'"));
145     }
146
147     private void createPolicyTypes() throws CoderException, PfModelException, URISyntaxException {
148         Set<String> policyTypeResources = ResourceUtils.getDirectoryContents("policytypes");
149
150         for (String policyTypeResource : policyTypeResources) {
151             Object yamlObject = new Yaml().load(ResourceUtils.getResourceAsString(policyTypeResource));
152             String yamlAsJsonString = new StandardCoder().encode(yamlObject);
153
154             ToscaServiceTemplate toscaServiceTemplatePolicyType =
155                     standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
156
157             assertNotNull(toscaServiceTemplatePolicyType);
158             databaseProvider.createPolicyTypes(toscaServiceTemplatePolicyType);
159         }
160     }
161 }