Add filter obejcts for concepts
[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.assertTrue;
26 import static org.junit.Assert.fail;
27
28 import java.sql.Connection;
29 import java.sql.DriverManager;
30
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;
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 StandardCoder standardCoder;
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         standardCoder = new StandardCoder();
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 Exception {
96         try {
97             new SimpleToscaProvider().getPolicies(null, 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         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
104                 ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
105                 ToscaServiceTemplate.class);
106
107         JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate();
108         originalServiceTemplate.fromAuthorative(toscaServiceTemplate);
109
110         assertNotNull(originalServiceTemplate);
111         JpaToscaServiceTemplate createdServiceTemplate =
112                 new SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate);
113
114         assertEquals(originalServiceTemplate, createdServiceTemplate);
115
116         PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0");
117
118         JpaToscaServiceTemplate gotServiceTemplate =
119                 new SimpleToscaProvider().getPolicies(pfDao, policyKey.getName(), policyKey.getVersion());
120
121         assertEquals(originalServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey),
122                 gotServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey));
123
124     }
125
126     @Test
127     public void testPolicyCreate() throws Exception {
128         try {
129             new SimpleToscaProvider().createPolicies(null, null);
130             fail("test should throw an exception here");
131         } catch (Exception exc) {
132             assertEquals("dao is marked @NonNull but is null", exc.getMessage());
133         }
134
135         try {
136             new SimpleToscaProvider().createPolicies(null, new JpaToscaServiceTemplate());
137             fail("test should throw an exception here");
138         } catch (Exception exc) {
139             assertEquals("dao is marked @NonNull but is null", exc.getMessage());
140         }
141
142         try {
143             new SimpleToscaProvider().createPolicies(pfDao, null);
144             fail("test should throw an exception here");
145         } catch (Exception exc) {
146             assertEquals("serviceTemplate is marked @NonNull but is null", exc.getMessage());
147         }
148
149         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
150                 ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
151                 ToscaServiceTemplate.class);
152
153         JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate();
154         originalServiceTemplate.fromAuthorative(toscaServiceTemplate);
155
156         assertNotNull(originalServiceTemplate);
157         JpaToscaServiceTemplate createdServiceTemplate =
158                 new SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate);
159
160         assertEquals(originalServiceTemplate, createdServiceTemplate);
161     }
162
163     @Test
164     public void testPolicyUpdate() throws Exception {
165         try {
166             new SimpleToscaProvider().updatePolicies(null, null);
167             fail("test should throw an exception here");
168         } catch (Exception exc) {
169             assertEquals("dao is marked @NonNull but is null", exc.getMessage());
170         }
171
172         try {
173             new SimpleToscaProvider().updatePolicies(null, new JpaToscaServiceTemplate());
174             fail("test should throw an exception here");
175         } catch (Exception exc) {
176             assertEquals("dao is marked @NonNull but is null", exc.getMessage());
177         }
178
179         try {
180             new SimpleToscaProvider().updatePolicies(pfDao, null);
181             fail("test should throw an exception here");
182         } catch (Exception exc) {
183             assertEquals("serviceTemplate is marked @NonNull but is null", exc.getMessage());
184         }
185
186         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
187                 ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
188                 ToscaServiceTemplate.class);
189
190         JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate();
191         originalServiceTemplate.fromAuthorative(toscaServiceTemplate);
192
193         assertNotNull(originalServiceTemplate);
194         JpaToscaServiceTemplate updatedServiceTemplate =
195                 new SimpleToscaProvider().updatePolicies(pfDao, originalServiceTemplate);
196
197         assertEquals(originalServiceTemplate, updatedServiceTemplate);
198     }
199
200     @Test
201     public void testPoliciesDelete() throws Exception {
202         try {
203             new SimpleToscaProvider().deletePolicy(null, null);
204             fail("test should throw an exception here");
205         } catch (Exception exc) {
206             assertEquals("dao is marked @NonNull but is null", exc.getMessage());
207         }
208
209         try {
210             new SimpleToscaProvider().deletePolicy(null, new PfConceptKey());
211             fail("test should throw an exception here");
212         } catch (Exception exc) {
213             assertEquals("dao is marked @NonNull but is null", exc.getMessage());
214         }
215
216         try {
217             new SimpleToscaProvider().deletePolicy(pfDao, null);
218             fail("test should throw an exception here");
219         } catch (Exception exc) {
220             assertEquals("policyKey is marked @NonNull but is null", exc.getMessage());
221         }
222
223         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
224                 ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
225                 ToscaServiceTemplate.class);
226
227         JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate();
228         originalServiceTemplate.fromAuthorative(toscaServiceTemplate);
229
230         assertNotNull(originalServiceTemplate);
231         JpaToscaServiceTemplate createdServiceTemplate =
232                 new SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate);
233
234         assertEquals(originalServiceTemplate, createdServiceTemplate);
235
236         PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0");
237
238         JpaToscaServiceTemplate deletedServiceTemplate =
239                 new SimpleToscaProvider().deletePolicy(pfDao, new PfConceptKey(policyKey));
240
241         assertEquals(originalServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey),
242                 deletedServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey));
243
244         assertTrue(new SimpleToscaProvider().getPolicies(pfDao, policyKey.getName(), policyKey.getVersion())
245                 .getTopologyTemplate().getPolicies().getConceptMap().isEmpty());
246     }
247
248     @Test
249     public void testAssertPoliciesExist() throws PfModelException {
250         JpaToscaServiceTemplate testServiceTemplate = new JpaToscaServiceTemplate();
251
252         try {
253             new SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate);
254             fail("test should throw an exception here");
255         } catch (Exception exc) {
256             assertEquals("topology template not specified on service template", exc.getMessage());
257         }
258
259         testServiceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
260         try {
261             new SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate);
262             fail("test should throw an exception here");
263         } catch (Exception exc) {
264             assertEquals("no policies specified on topology template of service template", exc.getMessage());
265         }
266
267         testServiceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
268         try {
269             new SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate);
270             fail("test should throw an exception here");
271         } catch (Exception exc) {
272             assertEquals("list of policies specified on topology template of service template is empty",
273                     exc.getMessage());
274         }
275
276     }
277 }