Add junit tests for endpoints
[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.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();
75     }
76
77     /**
78      * Closes up DB connections and deregisters API parameter group.
79      *
80      * @throws PfModelException the PfModel parsing exception
81      */
82     @AfterClass
83     public static void tearDown() throws PfModelException {
84
85         operationalPolicyProvider.close();
86         ParameterService.deregister(apiParamGroup);
87     }
88
89     @Test
90     public void testFetchOperationalPolicy() {
91
92         assertThatThrownBy(() -> {
93             operationalPolicyProvider.fetchOperationalPolicy("dummy", null);
94         }).hasMessage("no policy found for policy ID: dummy");
95
96         assertThatThrownBy(() -> {
97             operationalPolicyProvider.fetchOperationalPolicy("dummy", "dummy");
98         }).hasMessage("no policy found for policy ID: dummy");
99     }
100
101     @Test
102     public void testCreateOperationalPolicy() {
103
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();
113     }
114
115     @Test
116     public void testDeleteOperationalPolicy() {
117
118         assertThatThrownBy(() -> {
119             operationalPolicyProvider.deleteOperationalPolicy("dummy", null);
120         }).hasMessage("version is marked @NonNull but is null");
121
122         assertThatThrownBy(() -> {
123             operationalPolicyProvider.deleteOperationalPolicy("dummy", "dummy");
124         }).hasMessage("no policy found for policy ID: dummy");
125
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();
132
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();
141
142         assertThatThrownBy(() -> {
143             operationalPolicyProvider.deleteOperationalPolicy("operational.restart", "1.0.0");
144         }).hasMessage("no policy found for policy ID: operational.restart");
145     }
146 }