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.persistence.provider;
24 import static org.junit.Assert.assertEquals;
26 import java.sql.Connection;
27 import java.sql.DriverManager;
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.onap.policy.common.utils.coder.StandardCoder;
32 import org.onap.policy.common.utils.resources.ResourceUtils;
33 import org.onap.policy.models.dao.DaoParameters;
34 import org.onap.policy.models.dao.PfDao;
35 import org.onap.policy.models.dao.PfDaoFactory;
36 import org.onap.policy.models.dao.impl.DefaultPfDao;
37 import org.onap.policy.models.pdp.concepts.PdpGroups;
38 import org.onap.policy.models.pdp.persistence.provider.PdpProvider;
39 import org.onap.policy.models.tosca.simple.provider.SimpleToscaProvider;
42 * Test the {@link SimpleToscaProvider} class.
44 * @author Liam Fallon (liam.fallon@est.tech)
46 public class PdpProviderTest {
47 private Connection connection;
49 private StandardCoder standardCoder;
53 * Set up the DAO towards the database.
55 * @throws Exception on database errors
58 public void setupDao() throws Exception {
59 // Use the JDBC UI "jdbc:h2:mem:testdb" to test towards the h2 database
60 // Use the JDBC UI "jdbc:mariadb://localhost:3306/policy" to test towards a locally installed mariadb instance
61 connection = DriverManager.getConnection("jdbc:h2:mem:testdb", "policy", "P01icY");
63 final DaoParameters daoParameters = new DaoParameters();
64 daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
66 // Use the persistence unit ToscaConceptTest to test towards the h2 database
67 // Use the persistence unit ToscaConceptMariaDBTest to test towards a locally installed mariadb instance
68 daoParameters.setPersistenceUnit("ToscaConceptTest");
70 pfDao = new PfDaoFactory().createPfDao(daoParameters);
71 pfDao.init(daoParameters);
78 public void setupGson() {
79 standardCoder = new StandardCoder();
83 public void teardown() throws Exception {
89 public void testPoliciesGet() throws Exception {
91 * try { new PdpProvider().gePdpGroupst(null, null); fail("test should throw an exception here"); } catch
92 * (Exception exc) { assertEquals("dao is marked @NonNull but is null", exc.getMessage()); }
94 * try { new SimpleToscaProvider().getPolicies(null, new PfConceptKey());
95 * fail("test should throw an exception here"); } catch (Exception exc) {
96 * assertEquals("dao is marked @NonNull but is null", exc.getMessage()); }
98 * try { new SimpleToscaProvider().getPolicies(pfDao, null); fail("test should throw an exception here"); }
99 * catch (Exception exc) { assertEquals("policyKey is marked @NonNull but is null", exc.getMessage()); }
102 String originalJson = ResourceUtils.getResourceAsString("testdata/PdpGroups0.json");
104 PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
106 PdpGroups createdPdpGroups0 = new PdpGroups();
107 createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups()));
108 String createdJson = standardCoder.encode(createdPdpGroups0);
109 assertEquals(originalJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", ""));
111 PdpGroups gotPdpGroups0 = new PdpGroups();
112 gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, "PdpGroup0", "1.2.3"));
114 String gotJson = standardCoder.encode(gotPdpGroups0);
116 assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
120 * @Test public void testPolicyCreate() throws Exception { try { new SimpleToscaProvider().createPolicies(null,
121 * null); fail("test should throw an exception here"); } catch (Exception exc) {
122 * assertEquals("dao is marked @NonNull but is null", exc.getMessage()); }
124 * try { new SimpleToscaProvider().createPolicies(null, new JpaToscaServiceTemplate());
125 * fail("test should throw an exception here"); } catch (Exception exc) {
126 * assertEquals("dao is marked @NonNull but is null", exc.getMessage()); }
128 * try { new SimpleToscaProvider().createPolicies(pfDao, null); fail("test should throw an exception here"); } catch
129 * (Exception exc) { assertEquals("serviceTemplate is marked @NonNull but is null", exc.getMessage()); }
131 * ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
132 * ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
133 * ToscaServiceTemplate.class);
135 * JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate();
136 * originalServiceTemplate.fromAuthorative(toscaServiceTemplate);
138 * assertNotNull(originalServiceTemplate); JpaToscaServiceTemplate createdServiceTemplate = new
139 * SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate);
141 * assertEquals(originalServiceTemplate, createdServiceTemplate); }
143 * @Test public void testPolicyUpdate() throws Exception { try { new SimpleToscaProvider().updatePolicies(null,
144 * null); fail("test should throw an exception here"); } catch (Exception exc) {
145 * assertEquals("dao is marked @NonNull but is null", exc.getMessage()); }
147 * try { new SimpleToscaProvider().updatePolicies(null, new JpaToscaServiceTemplate());
148 * fail("test should throw an exception here"); } catch (Exception exc) {
149 * assertEquals("dao is marked @NonNull but is null", exc.getMessage()); }
151 * try { new SimpleToscaProvider().updatePolicies(pfDao, null); fail("test should throw an exception here"); } catch
152 * (Exception exc) { assertEquals("serviceTemplate is marked @NonNull but is null", exc.getMessage()); }
154 * ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
155 * ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
156 * ToscaServiceTemplate.class);
158 * JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate();
159 * originalServiceTemplate.fromAuthorative(toscaServiceTemplate);
161 * assertNotNull(originalServiceTemplate); JpaToscaServiceTemplate updatedServiceTemplate = new
162 * SimpleToscaProvider().updatePolicies(pfDao, originalServiceTemplate);
164 * assertEquals(originalServiceTemplate, updatedServiceTemplate); }
166 * @Test public void testPoliciesDelete() throws Exception { try { new SimpleToscaProvider().deletePolicy(null,
167 * null); fail("test should throw an exception here"); } catch (Exception exc) {
168 * assertEquals("dao is marked @NonNull but is null", exc.getMessage()); }
170 * try { new SimpleToscaProvider().deletePolicy(null, new PfConceptKey());
171 * fail("test should throw an exception here"); } catch (Exception exc) {
172 * assertEquals("dao is marked @NonNull but is null", exc.getMessage()); }
174 * try { new SimpleToscaProvider().deletePolicy(pfDao, null); fail("test should throw an exception here"); } catch
175 * (Exception exc) { assertEquals("policyKey is marked @NonNull but is null", exc.getMessage()); }
177 * ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
178 * ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
179 * ToscaServiceTemplate.class);
181 * JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate();
182 * originalServiceTemplate.fromAuthorative(toscaServiceTemplate);
184 * assertNotNull(originalServiceTemplate); JpaToscaServiceTemplate createdServiceTemplate = new
185 * SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate);
187 * assertEquals(originalServiceTemplate, createdServiceTemplate);
189 * PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0");
191 * JpaToscaServiceTemplate deletedServiceTemplate = new SimpleToscaProvider().deletePolicy(pfDao, new
192 * PfConceptKey(policyKey));
194 * assertEquals(originalServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey),
195 * deletedServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey));
197 * try { new SimpleToscaProvider().getPolicies(pfDao, new PfConceptKey(policyKey));
198 * fail("test should throw an exception here"); } catch (Exception exc) {
199 * assertEquals("policy not found: onap.restart.tca:1.0.0", exc.getMessage()); } }
201 * @Test public void testAssertPoliciesExist() throws PfModelException { JpaToscaServiceTemplate testServiceTemplate
202 * = new JpaToscaServiceTemplate();
204 * try { new SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate);
205 * fail("test should throw an exception here"); } catch (Exception exc) {
206 * assertEquals("topology template not specified on service template", exc.getMessage()); }
208 * testServiceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate()); try { new
209 * SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate); fail("test should throw an exception here"); }
210 * catch (Exception exc) { assertEquals("no policies specified on topology template of service template",
211 * exc.getMessage()); }
213 * testServiceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies()); try { new
214 * SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate); fail("test should throw an exception here"); }
215 * catch (Exception exc) {
216 * assertEquals("list of policies specified on topology template of service template is empty", exc.getMessage()); }