Fix database properties
[policy/models.git] / models-provider / src / test / java / org / onap / policy / models / provider / impl / PolicyLegacyOperationalPersistenceTest.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 java.util.Base64;
28
29 import lombok.NonNull;
30
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.policy.common.utils.coder.StandardCoder;
35 import org.onap.policy.common.utils.resources.ResourceUtils;
36 import org.onap.policy.models.base.PfModelException;
37 import org.onap.policy.models.provider.PolicyModelsProvider;
38 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
39 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
40 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 /**
45  * Test persistence of monitoring policies to and from the database.
46  *
47  * @author Liam Fallon (liam.fallon@est.tech)
48  */
49 public class PolicyLegacyOperationalPersistenceTest {
50     // Logger for this class
51     private static final Logger LOGGER = LoggerFactory.getLogger(PolicyLegacyOperationalPersistenceTest.class);
52
53     private StandardCoder standardCoder;
54
55     private PolicyModelsProvider databaseProvider;
56
57     // @formatter:off
58     private String[] policyInputResourceNames = {
59         "policies/vCPE.policy.operational.input.json",
60         "policies/vDNS.policy.operational.input.json",
61         "policies/vFirewall.policy.operational.input.json"
62     };
63
64     private String[] policyOutputResourceNames = {
65         "policies/vCPE.policy.operational.output.json",
66         "policies/vDNS.policy.operational.output.json",
67         "policies/vFirewall.policy.operational.output.json"
68     };
69     // @formatter:on
70
71     /**
72      * Initialize provider.
73      *
74      * @throws PfModelException on exceptions in the tests
75      */
76     @Before
77     public void setupParameters() throws PfModelException {
78         PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters();
79         parameters.setDatabaseDriver("org.h2.Driver");
80         parameters.setDatabaseUrl("jdbc:h2:mem:testdb");
81         parameters.setDatabaseUser("policy");
82         parameters.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
83         parameters.setPersistenceUnit("ToscaConceptTest");
84
85         databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
86     }
87
88     /**
89      * Set up standard coder.
90      */
91     @Before
92     public void setupStandardCoder() {
93         standardCoder = new StandardCoder();
94     }
95
96     @After
97     public void teardown() throws Exception {
98         databaseProvider.close();
99     }
100
101     @Test
102     public void testPolicyPersistence() {
103         try {
104             for (int i = 0; i < policyInputResourceNames.length; i++) {
105                 String policyInputString = ResourceUtils.getResourceAsString(policyInputResourceNames[i]);
106                 String policyOutputString = ResourceUtils.getResourceAsString(policyOutputResourceNames[i]);
107                 testJsonStringPolicyPersistence(policyInputString, policyOutputString);
108             }
109         } catch (Exception exc) {
110             LOGGER.warn("error processing policies", exc);
111             fail("test should not throw an exception");
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());
132         assertEquals(gotLop, lop);
133
134         LegacyOperationalPolicy updatedLop = databaseProvider.updateOperationalPolicy(lop);
135         assertEquals(gotLop, updatedLop);
136
137         LegacyOperationalPolicy deletedLop = databaseProvider.deleteOperationalPolicy(lop.getPolicyId());
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 }