Modify delete safety net and DB lab setup
[policy/api.git] / main / src / test / java / org / onap / policy / api / main / rest / provider / TestLegacyOperationalPolicyProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy API
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.api.main.rest.provider;
24
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;
30
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;
42
43 /**
44  * This class performs unit test of {@link LegacyOperationalPolicyProvider}
45  *
46  * @author Chenfei Gao (cgao@research.att.com)
47  */
48 public class TestLegacyOperationalPolicyProvider {
49
50     private static LegacyOperationalPolicyProvider operationalPolicyProvider;
51     private static PolicyModelsProviderParameters providerParams;
52     private static ApiParameterGroup apiParamGroup;
53     private static StandardCoder standardCoder;
54
55     private static final String POLICY_RESOURCE = "policies/vCPE.policy.operational.input.json";
56
57     /**
58      * Initializes parameters.
59      *
60      * @throws PfModelException the PfModel parsing exception
61      */
62     @BeforeClass
63     public static void setupParameters() throws PfModelException {
64
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();
74     }
75
76     /**
77      * Closes up DB connections and deregisters API parameter group.
78      *
79      * @throws PfModelException the PfModel parsing exception
80      */
81     @AfterClass
82     public static void tearDown() throws PfModelException {
83
84         operationalPolicyProvider.close();
85         ParameterService.deregister(apiParamGroup);
86     }
87
88     @Test
89     public void testFetchOperationalPolicy() {
90
91         assertThatThrownBy(() -> {
92             operationalPolicyProvider.fetchOperationalPolicy("dummy", null);
93         }).hasMessage("no policy found for policy ID: dummy");
94
95         assertThatThrownBy(() -> {
96             operationalPolicyProvider.fetchOperationalPolicy("dummy", "dummy");
97         }).hasMessage("no policy found for policy ID: dummy");
98     }
99
100     @Test
101     public void testCreateOperationalPolicy() {
102
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();
112     }
113
114     @Test
115     public void testDeleteOperationalPolicy() {
116
117         assertThatThrownBy(() -> {
118             operationalPolicyProvider.deleteOperationalPolicy("dummy", null);
119         }).hasMessage("version is marked @NonNull but is null");
120
121         assertThatThrownBy(() -> {
122             operationalPolicyProvider.deleteOperationalPolicy("dummy", "dummy");
123         }).hasMessage("no policy found for policy ID: dummy");
124
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();
131
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();
140
141         assertThatThrownBy(() -> {
142             operationalPolicyProvider.deleteOperationalPolicy("operational.restart", "1.0.0");
143         }).hasMessage("no policy found for policy ID: operational.restart");
144     }
145 }