Merge "Fix bugs on filters"
[policy/models.git] / models-pdp / src / test / java / org / onap / policy / models / 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.persistence.provider;
23
24 import static org.junit.Assert.assertEquals;
25
26 import java.sql.Connection;
27 import java.sql.DriverManager;
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.onap.policy.common.utils.coder.StandardCoder;
32 import org.onap.policy.common.utils.resources.ResourceUtils;
33 import org.onap.policy.models.dao.DaoParameters;
34 import org.onap.policy.models.dao.PfDao;
35 import org.onap.policy.models.dao.PfDaoFactory;
36 import org.onap.policy.models.dao.impl.DefaultPfDao;
37 import org.onap.policy.models.pdp.concepts.PdpGroups;
38 import org.onap.policy.models.pdp.persistence.provider.PdpProvider;
39 import org.onap.policy.models.tosca.simple.provider.SimpleToscaProvider;
40
41 /**
42  * Test the {@link SimpleToscaProvider} class.
43  *
44  * @author Liam Fallon (liam.fallon@est.tech)
45  */
46 public class PdpProviderTest {
47     private Connection connection;
48     private PfDao pfDao;
49     private StandardCoder standardCoder;
50
51
52     /**
53      * Set up the DAO towards the database.
54      *
55      * @throws Exception on database errors
56      */
57     @Before
58     public void setupDao() throws Exception {
59         // Use the JDBC UI "jdbc:h2:mem:testdb" to test towards the h2 database
60         // Use the JDBC UI "jdbc:mariadb://localhost:3306/policy" to test towards a locally installed mariadb instance
61         connection = DriverManager.getConnection("jdbc:h2:mem:testdb", "policy", "P01icY");
62
63         final DaoParameters daoParameters = new DaoParameters();
64         daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
65
66         // Use the persistence unit ToscaConceptTest to test towards the h2 database
67         // Use the persistence unit ToscaConceptMariaDBTest to test towards a locally installed mariadb instance
68         daoParameters.setPersistenceUnit("ToscaConceptTest");
69
70         pfDao = new PfDaoFactory().createPfDao(daoParameters);
71         pfDao.init(daoParameters);
72     }
73
74     /**
75      * Set up GSON.
76      */
77     @Before
78     public void setupGson() {
79         standardCoder = new StandardCoder();
80     }
81
82     @After
83     public void teardown() throws Exception {
84         pfDao.close();
85         connection.close();
86     }
87
88     @Test
89     public void testPoliciesGet() throws Exception {
90         /*
91          * try { new PdpProvider().gePdpGroupst(null, null); fail("test should throw an exception here"); } catch
92          * (Exception exc) { assertEquals("dao is marked @NonNull but is null", exc.getMessage()); }
93          *
94          * try { new SimpleToscaProvider().getPolicies(null, new PfConceptKey());
95          * fail("test should throw an exception here"); } catch (Exception exc) {
96          * assertEquals("dao is marked @NonNull but is null", exc.getMessage()); }
97          *
98          * try { new SimpleToscaProvider().getPolicies(pfDao, null); fail("test should throw an exception here"); }
99          * catch (Exception exc) { assertEquals("policyKey is marked @NonNull but is null", exc.getMessage()); }
100          */
101
102         String originalJson = ResourceUtils.getResourceAsString("testdata/PdpGroups0.json");
103
104         PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
105
106         PdpGroups createdPdpGroups0 = new PdpGroups();
107         createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups()));
108         String createdJson = standardCoder.encode(createdPdpGroups0);
109         assertEquals(originalJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", ""));
110
111         PdpGroups gotPdpGroups0 = new PdpGroups();
112         gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, "PdpGroup0", "1.2.3"));
113
114         String gotJson = standardCoder.encode(gotPdpGroups0);
115
116         assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
117
118     }
119     /*
120      * @Test public void testPolicyCreate() throws Exception { try { new SimpleToscaProvider().createPolicies(null,
121      * null); fail("test should throw an exception here"); } catch (Exception exc) {
122      * assertEquals("dao is marked @NonNull but is null", exc.getMessage()); }
123      *
124      * try { new SimpleToscaProvider().createPolicies(null, new JpaToscaServiceTemplate());
125      * fail("test should throw an exception here"); } catch (Exception exc) {
126      * assertEquals("dao is marked @NonNull but is null", exc.getMessage()); }
127      *
128      * try { new SimpleToscaProvider().createPolicies(pfDao, null); fail("test should throw an exception here"); } catch
129      * (Exception exc) { assertEquals("serviceTemplate is marked @NonNull but is null", exc.getMessage()); }
130      *
131      * ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
132      * ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
133      * ToscaServiceTemplate.class);
134      *
135      * JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate();
136      * originalServiceTemplate.fromAuthorative(toscaServiceTemplate);
137      *
138      * assertNotNull(originalServiceTemplate); JpaToscaServiceTemplate createdServiceTemplate = new
139      * SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate);
140      *
141      * assertEquals(originalServiceTemplate, createdServiceTemplate); }
142      *
143      * @Test public void testPolicyUpdate() throws Exception { try { new SimpleToscaProvider().updatePolicies(null,
144      * null); fail("test should throw an exception here"); } catch (Exception exc) {
145      * assertEquals("dao is marked @NonNull but is null", exc.getMessage()); }
146      *
147      * try { new SimpleToscaProvider().updatePolicies(null, new JpaToscaServiceTemplate());
148      * fail("test should throw an exception here"); } catch (Exception exc) {
149      * assertEquals("dao is marked @NonNull but is null", exc.getMessage()); }
150      *
151      * try { new SimpleToscaProvider().updatePolicies(pfDao, null); fail("test should throw an exception here"); } catch
152      * (Exception exc) { assertEquals("serviceTemplate is marked @NonNull but is null", exc.getMessage()); }
153      *
154      * ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
155      * ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
156      * ToscaServiceTemplate.class);
157      *
158      * JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate();
159      * originalServiceTemplate.fromAuthorative(toscaServiceTemplate);
160      *
161      * assertNotNull(originalServiceTemplate); JpaToscaServiceTemplate updatedServiceTemplate = new
162      * SimpleToscaProvider().updatePolicies(pfDao, originalServiceTemplate);
163      *
164      * assertEquals(originalServiceTemplate, updatedServiceTemplate); }
165      *
166      * @Test public void testPoliciesDelete() throws Exception { try { new SimpleToscaProvider().deletePolicy(null,
167      * null); fail("test should throw an exception here"); } catch (Exception exc) {
168      * assertEquals("dao is marked @NonNull but is null", exc.getMessage()); }
169      *
170      * try { new SimpleToscaProvider().deletePolicy(null, new PfConceptKey());
171      * fail("test should throw an exception here"); } catch (Exception exc) {
172      * assertEquals("dao is marked @NonNull but is null", exc.getMessage()); }
173      *
174      * try { new SimpleToscaProvider().deletePolicy(pfDao, null); fail("test should throw an exception here"); } catch
175      * (Exception exc) { assertEquals("policyKey is marked @NonNull but is null", exc.getMessage()); }
176      *
177      * ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
178      * ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
179      * ToscaServiceTemplate.class);
180      *
181      * JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate();
182      * originalServiceTemplate.fromAuthorative(toscaServiceTemplate);
183      *
184      * assertNotNull(originalServiceTemplate); JpaToscaServiceTemplate createdServiceTemplate = new
185      * SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate);
186      *
187      * assertEquals(originalServiceTemplate, createdServiceTemplate);
188      *
189      * PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0");
190      *
191      * JpaToscaServiceTemplate deletedServiceTemplate = new SimpleToscaProvider().deletePolicy(pfDao, new
192      * PfConceptKey(policyKey));
193      *
194      * assertEquals(originalServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey),
195      * deletedServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey));
196      *
197      * try { new SimpleToscaProvider().getPolicies(pfDao, new PfConceptKey(policyKey));
198      * fail("test should throw an exception here"); } catch (Exception exc) {
199      * assertEquals("policy not found: onap.restart.tca:1.0.0", exc.getMessage()); } }
200      *
201      * @Test public void testAssertPoliciesExist() throws PfModelException { JpaToscaServiceTemplate testServiceTemplate
202      * = new JpaToscaServiceTemplate();
203      *
204      * try { new SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate);
205      * fail("test should throw an exception here"); } catch (Exception exc) {
206      * assertEquals("topology template not specified on service template", exc.getMessage()); }
207      *
208      * testServiceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate()); try { new
209      * SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate); fail("test should throw an exception here"); }
210      * catch (Exception exc) { assertEquals("no policies specified on topology template of service template",
211      * exc.getMessage()); }
212      *
213      * testServiceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies()); try { new
214      * SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate); fail("test should throw an exception here"); }
215      * catch (Exception exc) {
216      * assertEquals("list of policies specified on topology template of service template is empty", exc.getMessage()); }
217      *
218      * }
219      */
220 }