2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.pdp.persistence.provider;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotEquals;
28 import java.sql.Connection;
29 import java.sql.DriverManager;
30 import java.util.ArrayList;
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.dao.DaoParameters;
38 import org.onap.policy.models.dao.PfDao;
39 import org.onap.policy.models.dao.PfDaoFactory;
40 import org.onap.policy.models.dao.impl.DefaultPfDao;
41 import org.onap.policy.models.pdp.concepts.PdpGroups;
42 import org.onap.policy.models.pdp.persistence.concepts.JpaPdpGroup;
43 import org.onap.policy.models.pdp.persistence.provider.PdpProvider;
44 import org.onap.policy.models.tosca.simple.provider.SimpleToscaProvider;
47 * Test the {@link SimpleToscaProvider} class.
49 * @author Liam Fallon (liam.fallon@est.tech)
51 public class PdpProviderTest {
52 private Connection connection;
54 private StandardCoder standardCoder;
58 * Set up the DAO towards the database.
60 * @throws Exception on database errors
63 public void setupDao() throws Exception {
64 // Use the JDBC UI "jdbc:h2:mem:testdb" to test towards the h2 database
65 // Use the JDBC UI "jdbc:mariadb://localhost:3306/policy" to test towards a locally installed mariadb instance
66 connection = DriverManager.getConnection("jdbc:h2:mem:testdb", "policy", "P01icY");
68 final DaoParameters daoParameters = new DaoParameters();
69 daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
71 // Use the persistence unit ToscaConceptTest to test towards the h2 database
72 // Use the persistence unit ToscaConceptMariaDBTest to test towards a locally installed mariadb instance
73 daoParameters.setPersistenceUnit("ToscaConceptTest");
75 pfDao = new PfDaoFactory().createPfDao(daoParameters);
76 pfDao.init(daoParameters);
83 public void setupGson() {
84 standardCoder = new StandardCoder();
88 public void teardown() throws Exception {
94 public void testPoliciesGet() throws Exception {
95 assertThatThrownBy(() -> {
96 new PdpProvider().getPdpGroups(null, null, null);
97 }).hasMessage("dao is marked @NonNull but is null");
99 assertThatThrownBy(() -> {
100 new PdpProvider().getPdpGroups(null, null, "version");
101 }).hasMessage("dao is marked @NonNull but is null");
103 assertThatThrownBy(() -> {
104 new PdpProvider().getPdpGroups(null, "name", "version");
105 }).hasMessage("dao is marked @NonNull but is null");
107 String originalJson = ResourceUtils.getResourceAsString("testdata/PdpGroups0.json");
109 PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
111 PdpGroups createdPdpGroups0 = new PdpGroups();
112 createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups()));
113 String createdJson = standardCoder.encode(createdPdpGroups0);
114 assertEquals(originalJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", ""));
116 PdpGroups gotPdpGroups0 = new PdpGroups();
117 gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, "PdpGroup0", "1.2.3"));
119 String gotJson = standardCoder.encode(gotPdpGroups0);
121 assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
125 public void testPolicyCreate() throws Exception {
126 assertThatThrownBy(() -> {
127 new PdpProvider().createPdpGroups(null, null);
128 }).hasMessage("dao is marked @NonNull but is null");
130 assertThatThrownBy(() -> {
131 new PdpProvider().createPdpGroups(null, new ArrayList<>());
132 }).hasMessage("dao is marked @NonNull but is null");
134 assertThatThrownBy(() -> {
135 new PdpProvider().createPdpGroups(pfDao, null);
136 }).hasMessage("pdpGroups is marked @NonNull but is null");
138 String originalJson = ResourceUtils.getResourceAsString("testdata/PdpGroups0.json");
140 PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
142 PdpGroups createdPdpGroups0 = new PdpGroups();
143 createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups()));
144 String createdJson = standardCoder.encode(createdPdpGroups0);
145 assertEquals(originalJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", ""));
147 PdpGroups gotPdpGroups0 = new PdpGroups();
148 gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, "PdpGroup0", "1.2.3"));
150 String gotJson = standardCoder.encode(gotPdpGroups0);
151 assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
155 public void testPolicyCreateNoPdp() throws Exception {
156 String originalJson = ResourceUtils.getResourceAsString("testdata/PdpGroupsNoPDPs.json");
158 PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
160 PdpGroups createdPdpGroups0 = new PdpGroups();
161 createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups()));
162 assertNotEquals(pdpGroups0, createdPdpGroups0);
163 pdpGroups0.getGroups().get(0).getPdpSubgroups().get(0).setPdpInstances(new ArrayList<>());
164 String originalTweakedJson = standardCoder.encode(pdpGroups0);
165 String createdJson = standardCoder.encode(createdPdpGroups0);
166 assertEquals(originalTweakedJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", ""));
168 PdpGroups gotPdpGroups0 = new PdpGroups();
169 gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, "TestPdpGroup", "1.2.3"));
171 String gotJson = standardCoder.encode(gotPdpGroups0);
172 assertEquals(originalTweakedJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
175 * @Test public void testPolicyUpdate() throws Exception { try { new SimpleToscaProvider().updatePolicies(null,
176 * null); fail("test should throw an exception here"); } catch (Exception exc) {
177 * assertEquals("dao is marked @NonNull but is null", exc.getMessage()); }
179 * try { new SimpleToscaProvider().updatePolicies(null, new JpaToscaServiceTemplate());
180 * fail("test should throw an exception here"); } catch (Exception exc) {
181 * assertEquals("dao is marked @NonNull but is null", exc.getMessage()); }
183 * try { new SimpleToscaProvider().updatePolicies(pfDao, null); fail("test should throw an exception here"); } catch
184 * (Exception exc) { assertEquals("serviceTemplate is marked @NonNull but is null", exc.getMessage()); }
186 * ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
187 * ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
188 * ToscaServiceTemplate.class);
190 * JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate();
191 * originalServiceTemplate.fromAuthorative(toscaServiceTemplate);
193 * assertNotNull(originalServiceTemplate); JpaToscaServiceTemplate updatedServiceTemplate = new
194 * SimpleToscaProvider().updatePolicies(pfDao, originalServiceTemplate);
196 * assertEquals(originalServiceTemplate, updatedServiceTemplate); }
198 * @Test public void testPoliciesDelete() throws Exception { try { new SimpleToscaProvider().deletePolicy(null,
199 * null); fail("test should throw an exception here"); } catch (Exception exc) {
200 * assertEquals("dao is marked @NonNull but is null", exc.getMessage()); }
202 * try { new SimpleToscaProvider().deletePolicy(null, new PfConceptKey());
203 * fail("test should throw an exception here"); } catch (Exception exc) {
204 * assertEquals("dao is marked @NonNull but is null", exc.getMessage()); }
206 * try { new SimpleToscaProvider().deletePolicy(pfDao, null); fail("test should throw an exception here"); } catch
207 * (Exception exc) { assertEquals("policyKey is marked @NonNull but is null", exc.getMessage()); }
209 * ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
210 * ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
211 * ToscaServiceTemplate.class);
213 * JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate();
214 * originalServiceTemplate.fromAuthorative(toscaServiceTemplate);
216 * assertNotNull(originalServiceTemplate); JpaToscaServiceTemplate createdServiceTemplate = new
217 * SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate);
219 * assertEquals(originalServiceTemplate, createdServiceTemplate);
221 * PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0");
223 * JpaToscaServiceTemplate deletedServiceTemplate = new SimpleToscaProvider().deletePolicy(pfDao, new
224 * PfConceptKey(policyKey));
226 * assertEquals(originalServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey),
227 * deletedServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey));
229 * try { new SimpleToscaProvider().getPolicies(pfDao, new PfConceptKey(policyKey));
230 * fail("test should throw an exception here"); } catch (Exception exc) {
231 * assertEquals("policy not found: onap.restart.tca:1.0.0", exc.getMessage()); } }
233 * @Test public void testAssertPoliciesExist() throws PfModelException { JpaToscaServiceTemplate testServiceTemplate
234 * = new JpaToscaServiceTemplate();
236 * try { new SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate);
237 * fail("test should throw an exception here"); } catch (Exception exc) {
238 * assertEquals("topology template not specified on service template", exc.getMessage()); }
240 * testServiceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate()); try { new
241 * SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate); fail("test should throw an exception here"); }
242 * catch (Exception exc) { assertEquals("no policies specified on topology template of service template",
243 * exc.getMessage()); }
245 * testServiceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies()); try { new
246 * SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate); fail("test should throw an exception here"); }
247 * catch (Exception exc) {
248 * assertEquals("list of policies specified on topology template of service template is empty", exc.getMessage()); }