Add missing entry_schema for operational policy type
[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
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.CoderException;
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.ToscaServiceTemplate;
44 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
45 import org.yaml.snakeyaml.Yaml;
46
47 /**
48  * Test persistence of monitoring policies to and from the database.
49  *
50  * @author Liam Fallon (liam.fallon@est.tech)
51  */
52 public class PolicyLegacyOperationalPersistenceTest {
53     private StandardCoder standardCoder;
54
55     private PolicyModelsProvider databaseProvider;
56
57     // @formatter:off
58     private String[] policyInputResourceNames = {
59         "policies/vCPE.policy.operational.legacy.input.json",
60         "policies/vDNS.policy.operational.legacy.input.json",
61         "policies/vFirewall.policy.operational.legacy.input.json"
62     };
63
64     private String[] policyOutputResourceNames = {
65         "policies/vCPE.policy.operational.legacy.output.json",
66         "policies/vDNS.policy.operational.legacy.output.json",
67         "policies/vFirewall.policy.operational.legacy.output.json"
68     };
69     // @formatter:on
70
71     /**
72      * Initialize provider.
73      *
74      * @throws PfModelException on exceptions in the tests
75      * @throws CoderException on JSON encoding and decoding errors
76      */
77     @Before
78     public void setupParameters() throws Exception {
79         // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB
80
81         PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters();
82         parameters.setDatabaseDriver("org.h2.Driver");
83         parameters.setDatabaseUrl("jdbc:h2:mem:testdb");
84         parameters.setDatabaseUser("policy");
85         parameters.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
86         parameters.setPersistenceUnit("ToscaConceptTest");
87
88         databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
89
90         createPolicyTypes();
91     }
92
93     /**
94      * Set up standard coder.
95      */
96     @Before
97     public void setupStandardCoder() {
98         standardCoder = new StandardCoder();
99     }
100
101     @After
102     public void teardown() throws Exception {
103         databaseProvider.close();
104     }
105
106     @Test
107     public void testLegacyOperationalPolicyPersistence() throws Exception {
108         for (int i = 0; i < policyInputResourceNames.length; i++) {
109             String policyInputString = ResourceUtils.getResourceAsString(policyInputResourceNames[i]);
110             String policyOutputString = ResourceUtils.getResourceAsString(policyOutputResourceNames[i]);
111             testJsonStringPolicyPersistence(policyInputString, policyOutputString);
112         }
113     }
114
115     /**
116      * Check persistence of a policy.
117      *
118      * @param policyInputString the policy as a string
119      * @param policyOutputString the expected output string
120      * @throws Exception any exception thrown
121      */
122     public void testJsonStringPolicyPersistence(@NonNull final String policyInputString,
123             final String policyOutputString) throws Exception {
124         LegacyOperationalPolicy lop = standardCoder.decode(policyInputString, LegacyOperationalPolicy.class);
125
126         assertNotNull(lop);
127
128         LegacyOperationalPolicy createdLop = databaseProvider.createOperationalPolicy(lop);
129         assertEquals(createdLop, lop);
130
131         LegacyOperationalPolicy gotLop = databaseProvider.getOperationalPolicy(lop.getPolicyId(), null);
132         assertEquals(gotLop, lop);
133
134         LegacyOperationalPolicy updatedLop = databaseProvider.updateOperationalPolicy(lop);
135         assertEquals(gotLop, updatedLop);
136
137         LegacyOperationalPolicy deletedLop = databaseProvider.deleteOperationalPolicy(lop.getPolicyId(), "1");
138         assertEquals(gotLop, deletedLop);
139
140         String actualRetrievedJson = standardCoder.encode(gotLop);
141
142         // All of this dash/underscore stuff is to avoid a checkstyle error around escaping unicode characters
143         assertEquals(
144                 policyOutputString.replaceAll("\\s+", "").replaceAll("u0027", "_-_-_-_").replaceAll("\\\\_-_-_-_", "'"),
145                 actualRetrievedJson.replaceAll("\\s+", "").replaceAll("u0027", "_-_-_-_").replaceAll("\\\\_-_-_-_",
146                         "'"));
147     }
148
149     private void createPolicyTypes() throws CoderException, PfModelException, URISyntaxException {
150         Set<String> policyTypeResources = ResourceUtils.getDirectoryContents("policytypes");
151
152         for (String policyTypeResource : policyTypeResources) {
153             Object yamlObject = new Yaml().load(ResourceUtils.getResourceAsString(policyTypeResource));
154             String yamlAsJsonString = new StandardCoder().encode(yamlObject);
155
156             ToscaServiceTemplate toscaServiceTemplatePolicyType =
157                     standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
158
159             assertNotNull(toscaServiceTemplatePolicyType);
160             databaseProvider.createPolicyTypes(toscaServiceTemplatePolicyType);
161         }
162     }
163 }