Merge "Add extra methods to Provider interface"
[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 java.sql.Connection;
28 import java.sql.DriverManager;
29
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;
45
46 /**
47  * Test the {@link SimpleToscaProvider} class.
48  *
49  * @author Liam Fallon (liam.fallon@est.tech)
50  */
51 public class SimpleToscaProviderTest {
52     private Connection connection;
53     private PfDao pfDao;
54     private StandardCoder standardCoder;
55
56
57     /**
58      * Set up the DAO towards the database.
59      *
60      * @throws Exception on database errors
61      */
62     @Before
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");
67
68         final DaoParameters daoParameters = new DaoParameters();
69         daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
70
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");
74
75         pfDao = new PfDaoFactory().createPfDao(daoParameters);
76         pfDao.init(daoParameters);
77     }
78
79     /**
80      * Set up GSON.
81      */
82     @Before
83     public void setupGson() {
84         standardCoder = new StandardCoder();
85     }
86
87     @After
88     public void teardown() throws Exception {
89         pfDao.close();
90         connection.close();
91     }
92
93     @Test
94     public void testPoliciesGet() throws Exception {
95         try {
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());
100         }
101
102         try {
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());
107         }
108
109         try {
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());
114         }
115
116         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
117                 ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
118                 ToscaServiceTemplate.class);
119
120         JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate();
121         originalServiceTemplate.fromAuthorative(toscaServiceTemplate);
122
123         assertNotNull(originalServiceTemplate);
124         JpaToscaServiceTemplate createdServiceTemplate =
125                 new SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate);
126
127         assertEquals(originalServiceTemplate, createdServiceTemplate);
128
129         PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0");
130
131         JpaToscaServiceTemplate gotServiceTemplate =
132                 new SimpleToscaProvider().getPolicies(pfDao, new PfConceptKey(policyKey));
133
134         assertEquals(originalServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey),
135                 gotServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey));
136
137     }
138
139     @Test
140     public void testPolicyCreate() throws Exception {
141         try {
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());
146         }
147
148         try {
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());
153         }
154
155         try {
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());
160         }
161
162         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
163                 ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
164                 ToscaServiceTemplate.class);
165
166         JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate();
167         originalServiceTemplate.fromAuthorative(toscaServiceTemplate);
168
169         assertNotNull(originalServiceTemplate);
170         JpaToscaServiceTemplate createdServiceTemplate =
171                 new SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate);
172
173         assertEquals(originalServiceTemplate, createdServiceTemplate);
174     }
175
176     @Test
177     public void testPolicyUpdate() throws Exception {
178         try {
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());
183         }
184
185         try {
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());
190         }
191
192         try {
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());
197         }
198
199         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
200                 ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
201                 ToscaServiceTemplate.class);
202
203         JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate();
204         originalServiceTemplate.fromAuthorative(toscaServiceTemplate);
205
206         assertNotNull(originalServiceTemplate);
207         JpaToscaServiceTemplate updatedServiceTemplate =
208                 new SimpleToscaProvider().updatePolicies(pfDao, originalServiceTemplate);
209
210         assertEquals(originalServiceTemplate, updatedServiceTemplate);
211     }
212
213     @Test
214     public void testPoliciesDelete() throws Exception {
215         try {
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());
220         }
221
222         try {
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());
227         }
228
229         try {
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());
234         }
235
236         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
237                 ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
238                 ToscaServiceTemplate.class);
239
240         JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate();
241         originalServiceTemplate.fromAuthorative(toscaServiceTemplate);
242
243         assertNotNull(originalServiceTemplate);
244         JpaToscaServiceTemplate createdServiceTemplate =
245                 new SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate);
246
247         assertEquals(originalServiceTemplate, createdServiceTemplate);
248
249         PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0");
250
251         JpaToscaServiceTemplate deletedServiceTemplate =
252                 new SimpleToscaProvider().deletePolicy(pfDao, new PfConceptKey(policyKey));
253
254         assertEquals(originalServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey),
255                 deletedServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey));
256
257         try {
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());
262         }
263     }
264
265     @Test
266     public void testAssertPoliciesExist() throws PfModelException {
267         JpaToscaServiceTemplate testServiceTemplate = new JpaToscaServiceTemplate();
268
269         try {
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());
274         }
275
276         testServiceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
277         try {
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());
282         }
283
284         testServiceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
285         try {
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",
290                     exc.getMessage());
291         }
292
293     }
294 }