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 com.google.gson.Gson;
29 import java.sql.Connection;
30 import java.sql.DriverManager;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
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.simple.concepts.ToscaPolicies;
43 import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate;
44 import org.onap.policy.models.tosca.simple.concepts.ToscaTopologyTemplate;
45 import org.onap.policy.models.tosca.simple.serialization.ToscaServiceTemplateMessageBodyHandler;
48 * Test the {@link SimpleToscaProvider} class.
50 * @author Liam Fallon (liam.fallon@est.tech)
52 public class SimpleToscaProviderTest {
53 private Connection connection;
59 * Set up the DAO towards the database.
61 * @throws Exception on database errors
64 public void setupDao() throws Exception {
65 // Use the JDBC UI "jdbc:h2:mem:testdb" to test towards the h2 database
66 // Use the JDBC UI "jdbc:mariadb://localhost:3306/policy" to test towards a locally installed mariadb instance
67 connection = DriverManager.getConnection("jdbc:h2:mem:testdb", "policy", "P01icY");
69 final DaoParameters daoParameters = new DaoParameters();
70 daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
72 // Use the persistence unit ToscaConceptTest to test towards the h2 database
73 // Use the persistence unit ToscaConceptMariaDBTest to test towards a locally installed mariadb instance
74 daoParameters.setPersistenceUnit("ToscaConceptTest");
76 pfDao = new PfDaoFactory().createPfDao(daoParameters);
77 pfDao.init(daoParameters);
84 public void setupGson() {
85 gson = new ToscaServiceTemplateMessageBodyHandler().getGson();
89 public void teardown() throws Exception {
95 public void testPoliciesGet() throws PfModelException {
97 new SimpleToscaProvider().getPolicies(null, null);
98 fail("test should throw an exception here");
99 } catch (Exception exc) {
100 assertEquals("dao is marked @NonNull but is null", exc.getMessage());
104 new SimpleToscaProvider().getPolicies(null, new PfConceptKey());
105 fail("test should throw an exception here");
106 } catch (Exception exc) {
107 assertEquals("dao is marked @NonNull but is null", exc.getMessage());
111 new SimpleToscaProvider().getPolicies(pfDao, null);
112 fail("test should throw an exception here");
113 } catch (Exception exc) {
114 assertEquals("policyKey is marked @NonNull but is null", exc.getMessage());
117 ToscaServiceTemplate originalServiceTemplate =
118 gson.fromJson(ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
119 ToscaServiceTemplate.class);
121 assertNotNull(originalServiceTemplate);
122 ToscaServiceTemplate createdServiceTemplate =
123 new SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate);
125 assertEquals(originalServiceTemplate, createdServiceTemplate);
127 PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0");
129 ToscaServiceTemplate gotServiceTemplate =
130 new SimpleToscaProvider().getPolicies(pfDao, new PfConceptKey(policyKey));
132 assertEquals(originalServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey),
133 gotServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey));
138 public void testPolicyCreate() throws PfModelException {
140 new SimpleToscaProvider().createPolicies(null, null);
141 fail("test should throw an exception here");
142 } catch (Exception exc) {
143 assertEquals("dao is marked @NonNull but is null", exc.getMessage());
147 new SimpleToscaProvider().createPolicies(null, new ToscaServiceTemplate());
148 fail("test should throw an exception here");
149 } catch (Exception exc) {
150 assertEquals("dao is marked @NonNull but is null", exc.getMessage());
154 new SimpleToscaProvider().createPolicies(pfDao, null);
155 fail("test should throw an exception here");
156 } catch (Exception exc) {
157 assertEquals("serviceTemplate is marked @NonNull but is null", exc.getMessage());
160 ToscaServiceTemplate originalServiceTemplate =
161 gson.fromJson(ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
162 ToscaServiceTemplate.class);
164 assertNotNull(originalServiceTemplate);
165 ToscaServiceTemplate createdServiceTemplate =
166 new SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate);
168 assertEquals(originalServiceTemplate, createdServiceTemplate);
172 public void testPolicyUpdate() throws PfModelException {
174 new SimpleToscaProvider().updatePolicies(null, null);
175 fail("test should throw an exception here");
176 } catch (Exception exc) {
177 assertEquals("dao is marked @NonNull but is null", exc.getMessage());
181 new SimpleToscaProvider().updatePolicies(null, new ToscaServiceTemplate());
182 fail("test should throw an exception here");
183 } catch (Exception exc) {
184 assertEquals("dao is marked @NonNull but is null", exc.getMessage());
188 new SimpleToscaProvider().updatePolicies(pfDao, null);
189 fail("test should throw an exception here");
190 } catch (Exception exc) {
191 assertEquals("serviceTemplate is marked @NonNull but is null", exc.getMessage());
194 ToscaServiceTemplate originalServiceTemplate =
195 gson.fromJson(ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
196 ToscaServiceTemplate.class);
198 assertNotNull(originalServiceTemplate);
199 ToscaServiceTemplate updatedServiceTemplate =
200 new SimpleToscaProvider().updatePolicies(pfDao, originalServiceTemplate);
202 assertEquals(originalServiceTemplate, updatedServiceTemplate);
206 public void testPoliciesDelete() throws PfModelException {
208 new SimpleToscaProvider().deletePolicies(null, null);
209 fail("test should throw an exception here");
210 } catch (Exception exc) {
211 assertEquals("dao is marked @NonNull but is null", exc.getMessage());
215 new SimpleToscaProvider().deletePolicies(null, new PfConceptKey());
216 fail("test should throw an exception here");
217 } catch (Exception exc) {
218 assertEquals("dao is marked @NonNull but is null", exc.getMessage());
222 new SimpleToscaProvider().deletePolicies(pfDao, null);
223 fail("test should throw an exception here");
224 } catch (Exception exc) {
225 assertEquals("policyKey is marked @NonNull but is null", exc.getMessage());
228 ToscaServiceTemplate originalServiceTemplate =
229 gson.fromJson(ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
230 ToscaServiceTemplate.class);
232 assertNotNull(originalServiceTemplate);
233 ToscaServiceTemplate createdServiceTemplate =
234 new SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate);
236 assertEquals(originalServiceTemplate, createdServiceTemplate);
238 PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0");
240 ToscaServiceTemplate deletedServiceTemplate =
241 new SimpleToscaProvider().deletePolicies(pfDao, new PfConceptKey(policyKey));
243 assertEquals(originalServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey),
244 deletedServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey));
247 new SimpleToscaProvider().getPolicies(pfDao, new PfConceptKey(policyKey));
248 fail("test should throw an exception here");
249 } catch (Exception exc) {
250 assertEquals("policy not found: onap.restart.tca:1.0.0", exc.getMessage());
255 public void testAssertPoliciesExist() throws PfModelException {
256 ToscaServiceTemplate testServiceTemplate = new ToscaServiceTemplate();
259 new SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate);
260 fail("test should throw an exception here");
261 } catch (Exception exc) {
262 assertEquals("topology template not specified on service template", exc.getMessage());
265 testServiceTemplate.setTopologyTemplate(new ToscaTopologyTemplate());
267 new SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate);
268 fail("test should throw an exception here");
269 } catch (Exception exc) {
270 assertEquals("no policies specified on topology template of service template", exc.getMessage());
273 testServiceTemplate.getTopologyTemplate().setPolicies(new ToscaPolicies());
275 new SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate);
276 fail("test should throw an exception here");
277 } catch (Exception exc) {
278 assertEquals("list of policies specified on topology template of service template is empty",