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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.provider.impl;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.fail;
27 import java.util.Base64;
30 import lombok.NonNull;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
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.legacy.concepts.LegacyGuardPolicyInput;
42 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 * Test persistence of monitoring policies to and from the database.
49 * @author Liam Fallon (liam.fallon@est.tech)
51 public class PolicyLegacyGuardPersistenceTest {
52 // Logger for this class
53 private static final Logger LOGGER = LoggerFactory.getLogger(PolicyLegacyGuardPersistenceTest.class);
55 private StandardCoder standardCoder;
57 private PolicyModelsProvider databaseProvider;
60 private String[] policyInputResourceNames = {
61 "policies/vDNS.policy.guard.frequency.input.json",
62 "policies/vDNS.policy.guard.minmax.input.json"
65 private String[] policyOutputResourceNames = {
66 "policies/vDNS.policy.guard.frequency.output.json",
67 "policies/vDNS.policy.guard.minmax.output.json"
72 * Initialize provider.
74 * @throws PfModelException on exceptions in the tests
77 public void setupParameters() throws PfModelException {
78 PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters();
79 parameters.setDatabaseUrl("jdbc:h2:mem:testdb");
80 parameters.setDatabaseUser("policy");
81 parameters.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
82 parameters.setPersistenceUnit("ToscaConceptTest");
84 databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
85 databaseProvider.init();
89 * Set up standard coder.
92 public void setupStandardCoder() {
93 standardCoder = new StandardCoder();
97 public void teardown() throws Exception {
98 databaseProvider.close();
102 public void testPolicyPersistence() {
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);
109 } catch (Exception exc) {
110 LOGGER.warn("error processing policies", exc);
111 fail("test should not throw an exception");
116 * Check persistence of a policy.
118 * @param policyInputString the policy as a string
119 * @param policyOutputString the expected output string
120 * @throws Exception any exception thrown
122 public void testJsonStringPolicyPersistence(@NonNull final String policyInputString,
123 final String policyOutputString) throws Exception {
124 LegacyGuardPolicyInput gip = standardCoder.decode(policyInputString, LegacyGuardPolicyInput.class);
128 Map<String, LegacyGuardPolicyOutput> createdGopm = databaseProvider.createGuardPolicy(gip);
129 assertEquals(gip.getPolicyId(), createdGopm.keySet().iterator().next());
130 assertEquals(gip.getContent(),
131 createdGopm.get(gip.getPolicyId()).getProperties().values().iterator().next());
133 Map<String, LegacyGuardPolicyOutput> gotGopm = databaseProvider.getGuardPolicy(gip.getPolicyId());
134 assertEquals(gip.getPolicyId(), gotGopm.keySet().iterator().next());
135 assertEquals(gip.getContent(),
136 gotGopm.get(gip.getPolicyId()).getProperties().values().iterator().next());
138 String actualRetrievedJson = standardCoder.encode(gotGopm);
140 // All of this dash/underscore stuff is to avoid a checkstyle error around escaping unicode characters
141 assertEquals(policyOutputString.replaceAll("\\s+", ""), actualRetrievedJson.replaceAll("\\s+", ""));