2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.persistence.provider;
23 import static org.junit.Assert.assertEquals;
25 import java.sql.Connection;
26 import java.sql.DriverManager;
27 import java.util.ArrayList;
28 import java.util.LinkedHashMap;
29 import java.util.List;
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.policy.common.utils.coder.StandardCoder;
35 import org.onap.policy.common.utils.resources.ResourceUtils;
36 import org.onap.policy.models.dao.DaoParameters;
37 import org.onap.policy.models.dao.PfDao;
38 import org.onap.policy.models.dao.PfDaoFactory;
39 import org.onap.policy.models.dao.impl.DefaultPfDao;
40 import org.onap.policy.models.pdp.concepts.Pdp;
41 import org.onap.policy.models.pdp.concepts.PdpGroup;
42 import org.onap.policy.models.pdp.concepts.PdpGroups;
43 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
44 import org.onap.policy.models.pdp.concepts.ToscaPolicyTypeIdentifier;
45 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
46 import org.onap.policy.models.pdp.enums.PdpState;
47 import org.onap.policy.models.pdp.persistence.provider.PdpProvider;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
51 * Test the {@link SimpleToscaProvider} class.
53 * @author Liam Fallon (liam.fallon@est.tech)
55 public class PdpProviderTest {
56 private Connection connection;
58 private StandardCoder standardCoder;
62 * Set up the DAO towards the database.
64 * @throws Exception on database errors
67 public void setupDao() throws Exception {
68 // Use the JDBC UI "jdbc:h2:mem:testdb" to test towards the h2 database
69 // Use the JDBC UI "jdbc:mariadb://localhost:3306/policy" to test towards a locally installed mariadb instance
70 connection = DriverManager.getConnection("jdbc:h2:mem:testdb", "policy", "P01icY");
72 final DaoParameters daoParameters = new DaoParameters();
73 daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
75 // Use the persistence unit ToscaConceptTest to test towards the h2 database
76 // Use the persistence unit ToscaConceptMariaDBTest to test towards a locally installed mariadb instance
77 daoParameters.setPersistenceUnit("ToscaConceptTest");
79 pfDao = new PfDaoFactory().createPfDao(daoParameters);
80 pfDao.init(daoParameters);
87 public void setupGson() {
88 standardCoder = new StandardCoder();
92 public void teardown() throws Exception {
98 public void testPoliciesGet() throws Exception {
100 * try { new PdpProvider().gePdpGroupst(null, null); fail("test should throw an exception here"); } catch
101 * (Exception exc) { assertEquals("dao is marked @NonNull but is null", exc.getMessage()); }
103 * try { new SimpleToscaProvider().getPolicies(null, new PfConceptKey());
104 * fail("test should throw an exception here"); } catch (Exception exc) {
105 * assertEquals("dao is marked @NonNull but is null", exc.getMessage()); }
107 * try { new SimpleToscaProvider().getPolicies(pfDao, null); fail("test should throw an exception here"); }
108 * catch (Exception exc) { assertEquals("policyKey is marked @NonNull but is null", exc.getMessage()); }
111 String originalJson = ResourceUtils.getResourceAsString("testdata/PdpGroups0.json");
113 @SuppressWarnings("unchecked")
114 PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
116 PdpGroups createdPdpGroups0 = new PdpGroups();
117 createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups()));
118 String createdJson = standardCoder.encode(createdPdpGroups0);
119 assertEquals(originalJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", ""));
121 PdpGroups gotPdpGroups0 = new PdpGroups();
122 gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, "PdpGroup0", "1.2.3"));
124 String gotJson = standardCoder.encode(gotPdpGroups0);
126 assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
130 * @Test public void testPolicyCreate() throws Exception { try { new SimpleToscaProvider().createPolicies(null,
131 * null); fail("test should throw an exception here"); } catch (Exception exc) {
132 * assertEquals("dao is marked @NonNull but is null", exc.getMessage()); }
134 * try { new SimpleToscaProvider().createPolicies(null, new JpaToscaServiceTemplate());
135 * fail("test should throw an exception here"); } catch (Exception exc) {
136 * assertEquals("dao is marked @NonNull but is null", exc.getMessage()); }
138 * try { new SimpleToscaProvider().createPolicies(pfDao, null); fail("test should throw an exception here"); } catch
139 * (Exception exc) { assertEquals("serviceTemplate is marked @NonNull but is null", exc.getMessage()); }
141 * ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
142 * ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
143 * ToscaServiceTemplate.class);
145 * JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate();
146 * originalServiceTemplate.fromAuthorative(toscaServiceTemplate);
148 * assertNotNull(originalServiceTemplate); JpaToscaServiceTemplate createdServiceTemplate = new
149 * SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate);
151 * assertEquals(originalServiceTemplate, createdServiceTemplate); }
153 * @Test public void testPolicyUpdate() throws Exception { try { new SimpleToscaProvider().updatePolicies(null,
154 * null); fail("test should throw an exception here"); } catch (Exception exc) {
155 * assertEquals("dao is marked @NonNull but is null", exc.getMessage()); }
157 * try { new SimpleToscaProvider().updatePolicies(null, new JpaToscaServiceTemplate());
158 * fail("test should throw an exception here"); } catch (Exception exc) {
159 * assertEquals("dao is marked @NonNull but is null", exc.getMessage()); }
161 * try { new SimpleToscaProvider().updatePolicies(pfDao, null); fail("test should throw an exception here"); } catch
162 * (Exception exc) { assertEquals("serviceTemplate is marked @NonNull but is null", exc.getMessage()); }
164 * ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
165 * ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
166 * ToscaServiceTemplate.class);
168 * JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate();
169 * originalServiceTemplate.fromAuthorative(toscaServiceTemplate);
171 * assertNotNull(originalServiceTemplate); JpaToscaServiceTemplate updatedServiceTemplate = new
172 * SimpleToscaProvider().updatePolicies(pfDao, originalServiceTemplate);
174 * assertEquals(originalServiceTemplate, updatedServiceTemplate); }
176 * @Test public void testPoliciesDelete() throws Exception { try { new SimpleToscaProvider().deletePolicy(null,
177 * null); fail("test should throw an exception here"); } catch (Exception exc) {
178 * assertEquals("dao is marked @NonNull but is null", exc.getMessage()); }
180 * try { new SimpleToscaProvider().deletePolicy(null, new PfConceptKey());
181 * fail("test should throw an exception here"); } catch (Exception exc) {
182 * assertEquals("dao is marked @NonNull but is null", exc.getMessage()); }
184 * try { new SimpleToscaProvider().deletePolicy(pfDao, null); fail("test should throw an exception here"); } catch
185 * (Exception exc) { assertEquals("policyKey is marked @NonNull but is null", exc.getMessage()); }
187 * ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
188 * ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
189 * ToscaServiceTemplate.class);
191 * JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate();
192 * originalServiceTemplate.fromAuthorative(toscaServiceTemplate);
194 * assertNotNull(originalServiceTemplate); JpaToscaServiceTemplate createdServiceTemplate = new
195 * SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate);
197 * assertEquals(originalServiceTemplate, createdServiceTemplate);
199 * PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0");
201 * JpaToscaServiceTemplate deletedServiceTemplate = new SimpleToscaProvider().deletePolicy(pfDao, new
202 * PfConceptKey(policyKey));
204 * assertEquals(originalServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey),
205 * deletedServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey));
207 * try { new SimpleToscaProvider().getPolicies(pfDao, new PfConceptKey(policyKey));
208 * fail("test should throw an exception here"); } catch (Exception exc) {
209 * assertEquals("policy not found: onap.restart.tca:1.0.0", exc.getMessage()); } }
211 * @Test public void testAssertPoliciesExist() throws PfModelException { JpaToscaServiceTemplate testServiceTemplate
212 * = new JpaToscaServiceTemplate();
214 * try { new SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate);
215 * fail("test should throw an exception here"); } catch (Exception exc) {
216 * assertEquals("topology template not specified on service template", exc.getMessage()); }
218 * testServiceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate()); try { new
219 * SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate); fail("test should throw an exception here"); }
220 * catch (Exception exc) { assertEquals("no policies specified on topology template of service template",
221 * exc.getMessage()); }
223 * testServiceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies()); try { new
224 * SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate); fail("test should throw an exception here"); }
225 * catch (Exception exc) {
226 * assertEquals("list of policies specified on topology template of service template is empty", exc.getMessage()); }