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.assertTrue;
26 import static org.junit.Assert.fail;
28 import java.sql.Connection;
29 import java.sql.DriverManager;
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.base.PfConceptKey;
37 import org.onap.policy.models.base.PfModelException;
38 import org.onap.policy.models.dao.DaoParameters;
39 import org.onap.policy.models.dao.PfDao;
40 import org.onap.policy.models.dao.PfDaoFactory;
41 import org.onap.policy.models.dao.impl.DefaultPfDao;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
43 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies;
44 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
45 import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate;
48 * Test the {@link SimpleToscaProvider} class.
50 * @author Liam Fallon (liam.fallon@est.tech)
52 public class SimpleToscaProviderTest {
53 private Connection connection;
55 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, null);
97 fail("test should throw an exception here");
98 } catch (Exception exc) {
99 assertEquals("dao is marked @NonNull but is null", exc.getMessage());
102 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
103 ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
104 ToscaServiceTemplate.class);
106 JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate();
107 originalServiceTemplate.fromAuthorative(toscaServiceTemplate);
109 assertNotNull(originalServiceTemplate);
110 JpaToscaServiceTemplate createdServiceTemplate =
111 new SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate);
113 assertEquals(originalServiceTemplate, createdServiceTemplate);
115 PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0");
117 JpaToscaServiceTemplate gotServiceTemplate =
118 new SimpleToscaProvider().getPolicies(pfDao, policyKey.getName(), policyKey.getVersion());
120 assertEquals(originalServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey),
121 gotServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey));
126 public void testPolicyCreate() throws Exception {
128 new SimpleToscaProvider().createPolicies(null, null);
129 fail("test should throw an exception here");
130 } catch (Exception exc) {
131 assertEquals("dao is marked @NonNull but is null", exc.getMessage());
135 new SimpleToscaProvider().createPolicies(null, new JpaToscaServiceTemplate());
136 fail("test should throw an exception here");
137 } catch (Exception exc) {
138 assertEquals("dao is marked @NonNull but is null", exc.getMessage());
142 new SimpleToscaProvider().createPolicies(pfDao, null);
143 fail("test should throw an exception here");
144 } catch (Exception exc) {
145 assertEquals("serviceTemplate is marked @NonNull but is null", exc.getMessage());
148 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
149 ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
150 ToscaServiceTemplate.class);
152 JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate();
153 originalServiceTemplate.fromAuthorative(toscaServiceTemplate);
155 assertNotNull(originalServiceTemplate);
156 JpaToscaServiceTemplate createdServiceTemplate =
157 new SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate);
159 assertEquals(originalServiceTemplate, createdServiceTemplate);
163 public void testPolicyUpdate() throws Exception {
165 new SimpleToscaProvider().updatePolicies(null, null);
166 fail("test should throw an exception here");
167 } catch (Exception exc) {
168 assertEquals("dao is marked @NonNull but is null", exc.getMessage());
172 new SimpleToscaProvider().updatePolicies(null, new JpaToscaServiceTemplate());
173 fail("test should throw an exception here");
174 } catch (Exception exc) {
175 assertEquals("dao is marked @NonNull but is null", exc.getMessage());
179 new SimpleToscaProvider().updatePolicies(pfDao, null);
180 fail("test should throw an exception here");
181 } catch (Exception exc) {
182 assertEquals("serviceTemplate is marked @NonNull but is null", exc.getMessage());
185 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
186 ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
187 ToscaServiceTemplate.class);
189 JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate();
190 originalServiceTemplate.fromAuthorative(toscaServiceTemplate);
192 assertNotNull(originalServiceTemplate);
193 JpaToscaServiceTemplate updatedServiceTemplate =
194 new SimpleToscaProvider().updatePolicies(pfDao, originalServiceTemplate);
196 assertEquals(originalServiceTemplate, updatedServiceTemplate);
200 public void testPoliciesDelete() throws Exception {
202 new SimpleToscaProvider().deletePolicy(null, null);
203 fail("test should throw an exception here");
204 } catch (Exception exc) {
205 assertEquals("dao is marked @NonNull but is null", exc.getMessage());
209 new SimpleToscaProvider().deletePolicy(null, new PfConceptKey());
210 fail("test should throw an exception here");
211 } catch (Exception exc) {
212 assertEquals("dao is marked @NonNull but is null", exc.getMessage());
216 new SimpleToscaProvider().deletePolicy(pfDao, null);
217 fail("test should throw an exception here");
218 } catch (Exception exc) {
219 assertEquals("policyKey is marked @NonNull but is null", exc.getMessage());
222 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
223 ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
224 ToscaServiceTemplate.class);
226 JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate();
227 originalServiceTemplate.fromAuthorative(toscaServiceTemplate);
229 assertNotNull(originalServiceTemplate);
230 JpaToscaServiceTemplate createdServiceTemplate =
231 new SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate);
233 assertEquals(originalServiceTemplate, createdServiceTemplate);
235 PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0");
237 JpaToscaServiceTemplate deletedServiceTemplate =
238 new SimpleToscaProvider().deletePolicy(pfDao, new PfConceptKey(policyKey));
240 assertEquals(originalServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey),
241 deletedServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey));
243 assertTrue(new SimpleToscaProvider().getPolicies(pfDao, policyKey.getName(), policyKey.getVersion())
244 .getTopologyTemplate().getPolicies().getConceptMap().isEmpty());
248 public void testAssertPoliciesExist() throws PfModelException {
249 JpaToscaServiceTemplate testServiceTemplate = new JpaToscaServiceTemplate();
252 new SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate);
253 fail("test should throw an exception here");
254 } catch (Exception exc) {
255 assertEquals("topology template not specified on service template", exc.getMessage());
258 testServiceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
260 new SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate);
261 fail("test should throw an exception here");
262 } catch (Exception exc) {
263 assertEquals("no policies specified on topology template of service template", exc.getMessage());
266 testServiceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
268 new SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate);
269 fail("test should throw an exception here");
270 } catch (Exception exc) {
271 assertEquals("list of policies specified on topology template of service template is empty",