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.setDatabaseDriver("org.h2.Driver");
68 providerParams.setDatabaseUrl("jdbc:h2:mem:testdb");
69 providerParams.setDatabaseUser("policy");
70 providerParams.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
71 providerParams.setPersistenceUnit("ToscaConceptTest");
72 apiParamGroup = new ApiParameterGroup("ApiGroup", null, providerParams);
73 ParameterService.register(apiParamGroup, true);
74 operationalPolicyProvider = new LegacyOperationalPolicyProvider();
78 * Closes up DB connections and deregisters API parameter group.
80 * @throws PfModelException the PfModel parsing exception
83 public static void tearDown() throws PfModelException {
85 operationalPolicyProvider.close();
86 ParameterService.deregister(apiParamGroup);
90 public void testFetchOperationalPolicy() {
92 assertThatThrownBy(() -> {
93 operationalPolicyProvider.fetchOperationalPolicy("dummy", null);
94 }).hasMessage("no policy found for policy ID: dummy");
96 assertThatThrownBy(() -> {
97 operationalPolicyProvider.fetchOperationalPolicy("dummy", "dummy");
98 }).hasMessage("no policy found for policy ID: dummy");
102 public void testCreateOperationalPolicy() {
104 assertThatCode(() -> {
105 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
106 LegacyOperationalPolicy policyToCreate = standardCoder.decode(policyString, LegacyOperationalPolicy.class);
107 LegacyOperationalPolicy createdPolicy = operationalPolicyProvider.createOperationalPolicy(policyToCreate);
108 assertNotNull(createdPolicy);
109 assertEquals("operational.restart", createdPolicy.getPolicyId());
110 assertTrue(createdPolicy.getContent()
111 .startsWith("controlLoop%3A%0A%20%20version%3A%202.0.0%0A%20%20"));
112 }).doesNotThrowAnyException();
116 public void testDeleteOperationalPolicy() {
118 assertThatThrownBy(() -> {
119 operationalPolicyProvider.deleteOperationalPolicy("dummy", null);
120 }).hasMessage("version is marked @NonNull but is null");
122 assertThatThrownBy(() -> {
123 operationalPolicyProvider.deleteOperationalPolicy("dummy", "dummy");
124 }).hasMessage("no policy found for policy ID: dummy");
126 assertThatCode(() -> {
127 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
128 LegacyOperationalPolicy policyToCreate = standardCoder.decode(policyString, LegacyOperationalPolicy.class);
129 LegacyOperationalPolicy createdPolicy = operationalPolicyProvider.createOperationalPolicy(policyToCreate);
130 assertNotNull(createdPolicy);
131 }).doesNotThrowAnyException();
133 assertThatCode(() -> {
134 LegacyOperationalPolicy deletedPolicy = operationalPolicyProvider
135 .deleteOperationalPolicy("operational.restart", "1.0.0");
136 assertNotNull(deletedPolicy);
137 assertEquals("operational.restart", deletedPolicy.getPolicyId());
138 assertTrue(deletedPolicy.getContent()
139 .startsWith("controlLoop%3A%0A%20%20version%3A%202.0.0%0A%20%20"));
140 }).doesNotThrowAnyException();
142 assertThatThrownBy(() -> {
143 operationalPolicyProvider.deleteOperationalPolicy("operational.restart", "1.0.0");
144 }).hasMessage("no policy found for policy ID: operational.restart");