2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.api.main.rest.provider;
25 import static org.assertj.core.api.Assertions.assertThatCode;
26 import static org.assertj.core.api.Assertions.assertThatThrownBy;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertTrue;
31 import java.util.Base64;
32 import org.junit.AfterClass;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.onap.policy.api.main.parameters.ApiParameterGroup;
36 import org.onap.policy.common.parameters.ParameterService;
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.PolicyModelsProviderParameters;
41 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
44 * This class performs unit test of {@link LegacyOperationalPolicyProvider}
46 * @author Chenfei Gao (cgao@research.att.com)
48 public class TestLegacyOperationalPolicyProvider {
50 private static LegacyOperationalPolicyProvider operationalPolicyProvider;
51 private static PolicyModelsProviderParameters providerParams;
52 private static ApiParameterGroup apiParamGroup;
53 private static StandardCoder standardCoder;
55 private static final String POLICY_RESOURCE = "policies/vCPE.policy.operational.input.json";
58 * Initializes parameters.
60 * @throws PfModelException the PfModel parsing exception
63 public static void setupParameters() throws PfModelException {
65 standardCoder = new StandardCoder();
66 providerParams = new PolicyModelsProviderParameters();
67 providerParams.setDatabaseUrl("jdbc:h2:mem:testdb");
68 providerParams.setDatabaseUser("policy");
69 providerParams.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
70 providerParams.setPersistenceUnit("ToscaConceptTest");
71 apiParamGroup = new ApiParameterGroup("ApiGroup", null, providerParams);
72 ParameterService.register(apiParamGroup, true);
73 operationalPolicyProvider = new LegacyOperationalPolicyProvider();
77 * Closes up DB connections and deregisters API parameter group.
79 * @throws PfModelException the PfModel parsing exception
82 public static void tearDown() throws PfModelException {
84 operationalPolicyProvider.close();
85 ParameterService.deregister(apiParamGroup);
89 public void testFetchOperationalPolicy() {
91 assertThatThrownBy(() -> {
92 operationalPolicyProvider.fetchOperationalPolicy("dummy", null);
93 }).hasMessage("no policy found for policy ID: dummy");
95 assertThatThrownBy(() -> {
96 operationalPolicyProvider.fetchOperationalPolicy("dummy", "dummy");
97 }).hasMessage("no policy found for policy ID: dummy");
101 public void testCreateOperationalPolicy() {
103 assertThatCode(() -> {
104 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
105 LegacyOperationalPolicy policyToCreate = standardCoder.decode(policyString, LegacyOperationalPolicy.class);
106 LegacyOperationalPolicy createdPolicy = operationalPolicyProvider.createOperationalPolicy(policyToCreate);
107 assertNotNull(createdPolicy);
108 assertEquals("operational.restart", createdPolicy.getPolicyId());
109 assertTrue(createdPolicy.getContent()
110 .startsWith("controlLoop%3A%0A%20%20version%3A%202.0.0%0A%20%20"));
111 }).doesNotThrowAnyException();
115 public void testDeleteOperationalPolicy() {
117 assertThatThrownBy(() -> {
118 operationalPolicyProvider.deleteOperationalPolicy("dummy", null);
119 }).hasMessage("version is marked @NonNull but is null");
121 assertThatThrownBy(() -> {
122 operationalPolicyProvider.deleteOperationalPolicy("dummy", "dummy");
123 }).hasMessage("no policy found for policy ID: dummy");
125 assertThatCode(() -> {
126 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
127 LegacyOperationalPolicy policyToCreate = standardCoder.decode(policyString, LegacyOperationalPolicy.class);
128 LegacyOperationalPolicy createdPolicy = operationalPolicyProvider.createOperationalPolicy(policyToCreate);
129 assertNotNull(createdPolicy);
130 }).doesNotThrowAnyException();
132 assertThatCode(() -> {
133 LegacyOperationalPolicy deletedPolicy = operationalPolicyProvider
134 .deleteOperationalPolicy("operational.restart", "1.0.0");
135 assertNotNull(deletedPolicy);
136 assertEquals("operational.restart", deletedPolicy.getPolicyId());
137 assertTrue(deletedPolicy.getContent()
138 .startsWith("controlLoop%3A%0A%20%20version%3A%202.0.0%0A%20%20"));
139 }).doesNotThrowAnyException();
141 assertThatThrownBy(() -> {
142 operationalPolicyProvider.deleteOperationalPolicy("operational.restart", "1.0.0");
143 }).hasMessage("no policy found for policy ID: operational.restart");