Merge "Restructure for authorative models"
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / simple / provider / SimpleToscaProviderTest.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.tosca.simple.provider;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.fail;
26
27 import com.google.gson.Gson;
28
29 import java.sql.Connection;
30 import java.sql.DriverManager;
31
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.JpaToscaPolicies;
43 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
44 import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate;
45 import org.onap.policy.models.tosca.simple.serialization.ToscaServiceTemplateMessageBodyHandler;
46
47 /**
48  * Test the {@link SimpleToscaProvider} class.
49  *
50  * @author Liam Fallon (liam.fallon@est.tech)
51  */
52 public class SimpleToscaProviderTest {
53     private Connection connection;
54     private PfDao pfDao;
55     private Gson gson;
56
57
58     /**
59      * Set up the DAO towards the database.
60      *
61      * @throws Exception on database errors
62      */
63     @Before
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");
68
69         final DaoParameters daoParameters = new DaoParameters();
70         daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
71
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");
75
76         pfDao = new PfDaoFactory().createPfDao(daoParameters);
77         pfDao.init(daoParameters);
78     }
79
80     /**
81      * Set up GSON.
82      */
83     @Before
84     public void setupGson() {
85         gson = new ToscaServiceTemplateMessageBodyHandler().getGson();
86     }
87
88     @After
89     public void teardown() throws Exception {
90         pfDao.close();
91         connection.close();
92     }
93
94     @Test
95     public void testPoliciesGet() throws PfModelException {
96         try {
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());
101         }
102
103         try {
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());
108         }
109
110         try {
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());
115         }
116
117         JpaToscaServiceTemplate originalServiceTemplate =
118                 gson.fromJson(ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
119                         JpaToscaServiceTemplate.class);
120
121         assertNotNull(originalServiceTemplate);
122         JpaToscaServiceTemplate createdServiceTemplate =
123                 new SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate);
124
125         assertEquals(originalServiceTemplate, createdServiceTemplate);
126
127         PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0");
128
129         JpaToscaServiceTemplate gotServiceTemplate =
130                 new SimpleToscaProvider().getPolicies(pfDao, new PfConceptKey(policyKey));
131
132         assertEquals(originalServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey),
133                 gotServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey));
134
135     }
136
137     @Test
138     public void testPolicyCreate() throws PfModelException {
139         try {
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());
144         }
145
146         try {
147             new SimpleToscaProvider().createPolicies(null, new JpaToscaServiceTemplate());
148             fail("test should throw an exception here");
149         } catch (Exception exc) {
150             assertEquals("dao is marked @NonNull but is null", exc.getMessage());
151         }
152
153         try {
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());
158         }
159
160         JpaToscaServiceTemplate originalServiceTemplate =
161                 gson.fromJson(ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
162                         JpaToscaServiceTemplate.class);
163
164         assertNotNull(originalServiceTemplate);
165         JpaToscaServiceTemplate createdServiceTemplate =
166                 new SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate);
167
168         assertEquals(originalServiceTemplate, createdServiceTemplate);
169     }
170
171     @Test
172     public void testPolicyUpdate() throws PfModelException {
173         try {
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());
178         }
179
180         try {
181             new SimpleToscaProvider().updatePolicies(null, new JpaToscaServiceTemplate());
182             fail("test should throw an exception here");
183         } catch (Exception exc) {
184             assertEquals("dao is marked @NonNull but is null", exc.getMessage());
185         }
186
187         try {
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());
192         }
193
194         JpaToscaServiceTemplate originalServiceTemplate =
195                 gson.fromJson(ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
196                         JpaToscaServiceTemplate.class);
197
198         assertNotNull(originalServiceTemplate);
199         JpaToscaServiceTemplate updatedServiceTemplate =
200                 new SimpleToscaProvider().updatePolicies(pfDao, originalServiceTemplate);
201
202         assertEquals(originalServiceTemplate, updatedServiceTemplate);
203     }
204
205     @Test
206     public void testPoliciesDelete() throws PfModelException {
207         try {
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());
212         }
213
214         try {
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());
219         }
220
221         try {
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());
226         }
227
228         JpaToscaServiceTemplate originalServiceTemplate =
229                 gson.fromJson(ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
230                         JpaToscaServiceTemplate.class);
231
232         assertNotNull(originalServiceTemplate);
233         JpaToscaServiceTemplate createdServiceTemplate =
234                 new SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate);
235
236         assertEquals(originalServiceTemplate, createdServiceTemplate);
237
238         PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0");
239
240         JpaToscaServiceTemplate deletedServiceTemplate =
241                 new SimpleToscaProvider().deletePolicies(pfDao, new PfConceptKey(policyKey));
242
243         assertEquals(originalServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey),
244                 deletedServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey));
245
246         try {
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());
251         }
252     }
253
254     @Test
255     public void testAssertPoliciesExist() throws PfModelException {
256         JpaToscaServiceTemplate testServiceTemplate = new JpaToscaServiceTemplate();
257
258         try {
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());
263         }
264
265         testServiceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
266         try {
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());
271         }
272
273         testServiceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
274         try {
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",
279                     exc.getMessage());
280         }
281
282     }
283 }