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