Merge "Add PdpMessage.appliesTo()"
[policy/models.git] / models-pdp / src / test / java / org / onap / policy / models / pdp / persistence / provider / PdpProviderTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.pdp.persistence.provider;
23
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotEquals;
27
28 import java.sql.Connection;
29 import java.sql.DriverManager;
30 import java.util.ArrayList;
31
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.onap.policy.common.utils.coder.StandardCoder;
36 import org.onap.policy.common.utils.resources.ResourceUtils;
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.pdp.concepts.PdpGroups;
42 import org.onap.policy.models.pdp.persistence.concepts.JpaPdpGroup;
43 import org.onap.policy.models.pdp.persistence.provider.PdpProvider;
44 import org.onap.policy.models.tosca.simple.provider.SimpleToscaProvider;
45
46 /**
47  * Test the {@link SimpleToscaProvider} class.
48  *
49  * @author Liam Fallon (liam.fallon@est.tech)
50  */
51 public class PdpProviderTest {
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         assertThatThrownBy(() -> {
96             new PdpProvider().getPdpGroups(null, null, null);
97         }).hasMessage("dao is marked @NonNull but is null");
98
99         assertThatThrownBy(() -> {
100             new PdpProvider().getPdpGroups(null, null, "version");
101         }).hasMessage("dao is marked @NonNull but is null");
102
103         assertThatThrownBy(() -> {
104             new PdpProvider().getPdpGroups(null, "name", "version");
105         }).hasMessage("dao is marked @NonNull but is null");
106
107         String originalJson = ResourceUtils.getResourceAsString("testdata/PdpGroups0.json");
108
109         PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
110
111         PdpGroups createdPdpGroups0 = new PdpGroups();
112         createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups()));
113         String createdJson = standardCoder.encode(createdPdpGroups0);
114         assertEquals(originalJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", ""));
115
116         PdpGroups gotPdpGroups0 = new PdpGroups();
117         gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, "PdpGroup0", "1.2.3"));
118
119         String gotJson = standardCoder.encode(gotPdpGroups0);
120
121         assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
122     }
123
124     @Test
125     public void testPolicyCreate() throws Exception {
126         assertThatThrownBy(() -> {
127             new PdpProvider().createPdpGroups(null, null);
128         }).hasMessage("dao is marked @NonNull but is null");
129
130         assertThatThrownBy(() -> {
131             new PdpProvider().createPdpGroups(null, new ArrayList<>());
132         }).hasMessage("dao is marked @NonNull but is null");
133
134         assertThatThrownBy(() -> {
135             new PdpProvider().createPdpGroups(pfDao, null);
136         }).hasMessage("pdpGroups is marked @NonNull but is null");
137
138         String originalJson = ResourceUtils.getResourceAsString("testdata/PdpGroups0.json");
139
140         PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
141
142         PdpGroups createdPdpGroups0 = new PdpGroups();
143         createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups()));
144         String createdJson = standardCoder.encode(createdPdpGroups0);
145         assertEquals(originalJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", ""));
146
147         PdpGroups gotPdpGroups0 = new PdpGroups();
148         gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, "PdpGroup0", "1.2.3"));
149
150         String gotJson = standardCoder.encode(gotPdpGroups0);
151         assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
152     }
153
154     @Test
155     public void testPolicyCreateNoPdp() throws Exception {
156         String originalJson = ResourceUtils.getResourceAsString("testdata/PdpGroupsNoPDPs.json");
157
158         PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
159
160         PdpGroups createdPdpGroups0 = new PdpGroups();
161         createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups()));
162         assertNotEquals(pdpGroups0, createdPdpGroups0);
163         pdpGroups0.getGroups().get(0).getPdpSubgroups().get(0).setPdpInstances(new ArrayList<>());
164         String originalTweakedJson = standardCoder.encode(pdpGroups0);
165         String createdJson = standardCoder.encode(createdPdpGroups0);
166         assertEquals(originalTweakedJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", ""));
167
168         PdpGroups gotPdpGroups0 = new PdpGroups();
169         gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, "TestPdpGroup", "1.2.3"));
170
171         String gotJson = standardCoder.encode(gotPdpGroups0);
172         assertEquals(originalTweakedJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
173     }
174     /*
175      * @Test public void testPolicyUpdate() throws Exception { try { new SimpleToscaProvider().updatePolicies(null,
176      * null); fail("test should throw an exception here"); } catch (Exception exc) {
177      * assertEquals("dao is marked @NonNull but is null", exc.getMessage()); }
178      *
179      * try { new SimpleToscaProvider().updatePolicies(null, new JpaToscaServiceTemplate());
180      * fail("test should throw an exception here"); } catch (Exception exc) {
181      * assertEquals("dao is marked @NonNull but is null", exc.getMessage()); }
182      *
183      * try { new SimpleToscaProvider().updatePolicies(pfDao, null); fail("test should throw an exception here"); } catch
184      * (Exception exc) { assertEquals("serviceTemplate is marked @NonNull but is null", exc.getMessage()); }
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); JpaToscaServiceTemplate updatedServiceTemplate = new
194      * SimpleToscaProvider().updatePolicies(pfDao, originalServiceTemplate);
195      *
196      * assertEquals(originalServiceTemplate, updatedServiceTemplate); }
197      *
198      * @Test public void testPoliciesDelete() throws Exception { try { new SimpleToscaProvider().deletePolicy(null,
199      * null); fail("test should throw an exception here"); } catch (Exception exc) {
200      * assertEquals("dao is marked @NonNull but is null", exc.getMessage()); }
201      *
202      * try { new SimpleToscaProvider().deletePolicy(null, new PfConceptKey());
203      * fail("test should throw an exception here"); } catch (Exception exc) {
204      * assertEquals("dao is marked @NonNull but is null", exc.getMessage()); }
205      *
206      * try { new SimpleToscaProvider().deletePolicy(pfDao, null); fail("test should throw an exception here"); } catch
207      * (Exception exc) { assertEquals("policyKey is marked @NonNull but is null", exc.getMessage()); }
208      *
209      * ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
210      * ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
211      * ToscaServiceTemplate.class);
212      *
213      * JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate();
214      * originalServiceTemplate.fromAuthorative(toscaServiceTemplate);
215      *
216      * assertNotNull(originalServiceTemplate); JpaToscaServiceTemplate createdServiceTemplate = new
217      * SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate);
218      *
219      * assertEquals(originalServiceTemplate, createdServiceTemplate);
220      *
221      * PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0");
222      *
223      * JpaToscaServiceTemplate deletedServiceTemplate = new SimpleToscaProvider().deletePolicy(pfDao, new
224      * PfConceptKey(policyKey));
225      *
226      * assertEquals(originalServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey),
227      * deletedServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey));
228      *
229      * try { new SimpleToscaProvider().getPolicies(pfDao, new PfConceptKey(policyKey));
230      * fail("test should throw an exception here"); } catch (Exception exc) {
231      * assertEquals("policy not found: onap.restart.tca:1.0.0", exc.getMessage()); } }
232      *
233      * @Test public void testAssertPoliciesExist() throws PfModelException { JpaToscaServiceTemplate testServiceTemplate
234      * = new JpaToscaServiceTemplate();
235      *
236      * try { new SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate);
237      * fail("test should throw an exception here"); } catch (Exception exc) {
238      * assertEquals("topology template not specified on service template", exc.getMessage()); }
239      *
240      * testServiceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate()); try { new
241      * SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate); fail("test should throw an exception here"); }
242      * catch (Exception exc) { assertEquals("no policies specified on topology template of service template",
243      * exc.getMessage()); }
244      *
245      * testServiceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies()); try { new
246      * SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate); fail("test should throw an exception here"); }
247      * catch (Exception exc) {
248      * assertEquals("list of policies specified on topology template of service template is empty", exc.getMessage()); }
249      *
250      * }
251      */
252 }