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.tosca.simple.provider;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.fail;
27 import java.sql.Connection;
28 import java.sql.DriverManager;
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.onap.policy.common.utils.coder.StandardCoder;
34 import org.onap.policy.common.utils.resources.ResourceUtils;
35 import org.onap.policy.models.base.PfConceptKey;
36 import org.onap.policy.models.base.PfModelException;
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.tosca.authorative.concepts.ToscaServiceTemplate;
42 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies;
43 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
44 import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate;
47 * Test the {@link SimpleToscaProvider} class.
49 * @author Liam Fallon (liam.fallon@est.tech)
51 public class SimpleToscaProviderTest {
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 {
96 new SimpleToscaProvider().getPolicies(null, null);
97 fail("test should throw an exception here");
98 } catch (Exception exc) {
99 assertEquals("dao is marked @NonNull but is null", exc.getMessage());
103 new SimpleToscaProvider().getPolicies(null, new PfConceptKey());
104 fail("test should throw an exception here");
105 } catch (Exception exc) {
106 assertEquals("dao is marked @NonNull but is null", exc.getMessage());
110 new SimpleToscaProvider().getPolicies(pfDao, null);
111 fail("test should throw an exception here");
112 } catch (Exception exc) {
113 assertEquals("policyKey is marked @NonNull but is null", exc.getMessage());
116 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
117 ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
118 ToscaServiceTemplate.class);
120 JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate();
121 originalServiceTemplate.fromAuthorative(toscaServiceTemplate);
123 assertNotNull(originalServiceTemplate);
124 JpaToscaServiceTemplate createdServiceTemplate =
125 new SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate);
127 assertEquals(originalServiceTemplate, createdServiceTemplate);
129 PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0");
131 JpaToscaServiceTemplate gotServiceTemplate =
132 new SimpleToscaProvider().getPolicies(pfDao, new PfConceptKey(policyKey));
134 assertEquals(originalServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey),
135 gotServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey));
140 public void testPolicyCreate() throws Exception {
142 new SimpleToscaProvider().createPolicies(null, null);
143 fail("test should throw an exception here");
144 } catch (Exception exc) {
145 assertEquals("dao is marked @NonNull but is null", exc.getMessage());
149 new SimpleToscaProvider().createPolicies(null, new JpaToscaServiceTemplate());
150 fail("test should throw an exception here");
151 } catch (Exception exc) {
152 assertEquals("dao is marked @NonNull but is null", exc.getMessage());
156 new SimpleToscaProvider().createPolicies(pfDao, null);
157 fail("test should throw an exception here");
158 } catch (Exception exc) {
159 assertEquals("serviceTemplate is marked @NonNull but is null", exc.getMessage());
162 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
163 ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
164 ToscaServiceTemplate.class);
166 JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate();
167 originalServiceTemplate.fromAuthorative(toscaServiceTemplate);
169 assertNotNull(originalServiceTemplate);
170 JpaToscaServiceTemplate createdServiceTemplate =
171 new SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate);
173 assertEquals(originalServiceTemplate, createdServiceTemplate);
177 public void testPolicyUpdate() throws Exception {
179 new SimpleToscaProvider().updatePolicies(null, null);
180 fail("test should throw an exception here");
181 } catch (Exception exc) {
182 assertEquals("dao is marked @NonNull but is null", exc.getMessage());
186 new SimpleToscaProvider().updatePolicies(null, new JpaToscaServiceTemplate());
187 fail("test should throw an exception here");
188 } catch (Exception exc) {
189 assertEquals("dao is marked @NonNull but is null", exc.getMessage());
193 new SimpleToscaProvider().updatePolicies(pfDao, null);
194 fail("test should throw an exception here");
195 } catch (Exception exc) {
196 assertEquals("serviceTemplate is marked @NonNull but is null", exc.getMessage());
199 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
200 ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
201 ToscaServiceTemplate.class);
203 JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate();
204 originalServiceTemplate.fromAuthorative(toscaServiceTemplate);
206 assertNotNull(originalServiceTemplate);
207 JpaToscaServiceTemplate updatedServiceTemplate =
208 new SimpleToscaProvider().updatePolicies(pfDao, originalServiceTemplate);
210 assertEquals(originalServiceTemplate, updatedServiceTemplate);
214 public void testPoliciesDelete() throws Exception {
216 new SimpleToscaProvider().deletePolicy(null, null);
217 fail("test should throw an exception here");
218 } catch (Exception exc) {
219 assertEquals("dao is marked @NonNull but is null", exc.getMessage());
223 new SimpleToscaProvider().deletePolicy(null, new PfConceptKey());
224 fail("test should throw an exception here");
225 } catch (Exception exc) {
226 assertEquals("dao is marked @NonNull but is null", exc.getMessage());
230 new SimpleToscaProvider().deletePolicy(pfDao, null);
231 fail("test should throw an exception here");
232 } catch (Exception exc) {
233 assertEquals("policyKey is marked @NonNull but is null", exc.getMessage());
236 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
237 ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
238 ToscaServiceTemplate.class);
240 JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate();
241 originalServiceTemplate.fromAuthorative(toscaServiceTemplate);
243 assertNotNull(originalServiceTemplate);
244 JpaToscaServiceTemplate createdServiceTemplate =
245 new SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate);
247 assertEquals(originalServiceTemplate, createdServiceTemplate);
249 PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0");
251 JpaToscaServiceTemplate deletedServiceTemplate =
252 new SimpleToscaProvider().deletePolicy(pfDao, new PfConceptKey(policyKey));
254 assertEquals(originalServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey),
255 deletedServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey));
258 new SimpleToscaProvider().getPolicies(pfDao, new PfConceptKey(policyKey));
259 fail("test should throw an exception here");
260 } catch (Exception exc) {
261 assertEquals("policy not found: onap.restart.tca:1.0.0", exc.getMessage());
266 public void testAssertPoliciesExist() throws PfModelException {
267 JpaToscaServiceTemplate testServiceTemplate = new JpaToscaServiceTemplate();
270 new SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate);
271 fail("test should throw an exception here");
272 } catch (Exception exc) {
273 assertEquals("topology template not specified on service template", exc.getMessage());
276 testServiceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
278 new SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate);
279 fail("test should throw an exception here");
280 } catch (Exception exc) {
281 assertEquals("no policies specified on topology template of service template", exc.getMessage());
284 testServiceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
286 new SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate);
287 fail("test should throw an exception here");
288 } catch (Exception exc) {
289 assertEquals("list of policies specified on topology template of service template is empty",