Merge "migrate model-impl from drools-applications"
[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 com.google.gson.Gson;
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.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.legacy.concepts.LegacyOperationalPolicy;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
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     // Logger for this class
52     private static final Logger LOGGER = LoggerFactory.getLogger(PolicyLegacyOperationalPersistenceTest.class);
53
54     private Gson gson;
55
56     private PolicyModelsProvider databaseProvider;
57
58     // @formatter:off
59     private String[] policyInputResourceNames = {
60         "policies/vCPE.policy.operational.input.json",
61         "policies/vDNS.policy.operational.input.json",
62         "policies/vFirewall.policy.operational.input.json"
63     };
64
65     private String[] policyOutputResourceNames = {
66         "policies/vCPE.policy.operational.output.json",
67         "policies/vDNS.policy.operational.output.json",
68         "policies/vFirewall.policy.operational.output.json"
69     };
70     // @formatter:on
71
72     /**
73      * Initialize provider.
74      *
75      * @throws PfModelException on exceptions in the tests
76      */
77     @Before
78     public void setupParameters() throws PfModelException {
79         PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters();
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         databaseProvider.init();
87     }
88
89     /**
90      * Set up GSON.
91      */
92     @Before
93     public void setupGson() {
94         gson = new Gson();
95     }
96
97     @After
98     public void teardown() throws Exception {
99         databaseProvider.close();
100     }
101
102     @Test
103     public void testPolicyPersistence() {
104         try {
105             for (int i = 0; i < policyInputResourceNames.length; i++) {
106                 String policyInputString = ResourceUtils.getResourceAsString(policyInputResourceNames[i]);
107                 String policyOutputString = ResourceUtils.getResourceAsString(policyOutputResourceNames[i]);
108                 testJsonStringPolicyPersistence(policyInputString, policyOutputString);
109             }
110         } catch (Exception exc) {
111             LOGGER.warn("error processing policies", exc);
112             fail("test should not throw an exception");
113         }
114     }
115
116     /**
117      * Check persistence of a policy.
118      *
119      * @param policyInputString the policy as a string
120      * @param policyOutputString the expected output string
121      * @throws Exception any exception thrown
122      */
123     public void testJsonStringPolicyPersistence(@NonNull final String policyInputString,
124             final String policyOutputString) throws Exception {
125         LegacyOperationalPolicy lop = gson.fromJson(policyInputString, LegacyOperationalPolicy.class);
126
127         assertNotNull(lop);
128
129         LegacyOperationalPolicy createdLop = databaseProvider.createOperationalPolicy(lop);
130         assertEquals(createdLop, lop);
131
132         LegacyOperationalPolicy gotLop = databaseProvider.getOperationalPolicy(lop.getPolicyId());
133         assertEquals(gotLop, lop);
134
135         String actualRetrievedJson = gson.toJson(gotLop);
136
137         // All of this dash/underscore stuff is to avoid a checkstyle error around escaping unicode characters
138         assertEquals(
139                 policyOutputString.replaceAll("\\s+", "").replaceAll("u0027", "_-_-_-_").replaceAll("\\\\_-_-_-_", "'"),
140                 actualRetrievedJson.replaceAll("\\s+", "").replaceAll("u0027", "_-_-_-_").replaceAll("\\\\_-_-_-_",
141                         "'"));
142     }
143 }