e6d948f35764786f341ed64af867d9581a941902
[policy/apex-pdp.git] / model / model-api / src / test / java / org / onap / policy / apex / model / modelapi / ApexModelApiTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020 Nordix Foundation.
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.apex.model.modelapi;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assert.fail;
29
30 import java.io.File;
31 import java.io.IOException;
32 import java.sql.Connection;
33 import java.sql.DriverManager;
34 import java.util.UUID;
35
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.onap.policy.apex.model.basicmodel.dao.DaoParameters;
40 import org.onap.policy.apex.model.modelapi.impl.ApexModelImpl;
41 import org.onap.policy.common.utils.resources.TextFileUtils;
42
43 /**
44  * Test the apex model API.
45  *
46  * @author Liam Fallon (liam.fallon@ericsson.com)
47  */
48 public class ApexModelApiTest {
49     private Connection connection;
50
51     @Before
52     public void setup() throws Exception {
53         // Hold the h2 database up for entire tests
54         connection = DriverManager.getConnection("jdbc:h2:mem:testdb");
55     }
56
57     @After
58     public void teardown() throws Exception {
59         // Close the h2 database after tests
60         connection.close();
61     }
62
63     @Test
64     public void testApexModelLoadFromFile() {
65         final ApexModel apexModel = new ApexModelFactory().createApexModel(null, false);
66
67         ApexApiResult result = apexModel.loadFromFile("src/main/resources/models/PolicyModel.json");
68         assertTrue(result.getResult().equals(ApexApiResult.Result.FAILED));
69
70         result = apexModel.loadFromFile("src/test/resources/models/PolicyModel.json");
71         assertTrue(result.getResult().equals(ApexApiResult.Result.SUCCESS));
72
73         result = apexModel.deleteModel();
74         assertTrue(result.getResult().equals(ApexApiResult.Result.SUCCESS));
75
76         result = apexModel.loadFromFile("src/test/resources/models/PolicyModel.xml");
77         assertTrue(result.getResult().equals(ApexApiResult.Result.SUCCESS));
78
79         result = apexModel.deleteModel();
80         assertTrue(result.getResult().equals(ApexApiResult.Result.SUCCESS));
81
82         result = apexModel.loadFromFile("src/test/resources/models/PolicyModel.junk");
83         assertTrue(result.getResult().equals(ApexApiResult.Result.FAILED));
84         assertTrue(result.getMessages().get(0).equals("format of input for Apex concept is neither JSON nor XML"));
85     }
86
87     @Test
88     public void testApexModelSaveToFile() throws IOException {
89         final ApexModel apexModel = new ApexModelFactory().createApexModel(null, false);
90
91         ApexApiResult result = apexModel.loadFromFile("src/test/resources/models/PolicyModel.json");
92         assertTrue(result.getResult().equals(ApexApiResult.Result.SUCCESS));
93
94         final File tempJsonModelFile = File.createTempFile("ApexModelTest", ".json");
95         result = apexModel.saveToFile(tempJsonModelFile.getCanonicalPath(), false);
96         assertTrue(result.getResult().equals(ApexApiResult.Result.SUCCESS));
97
98         final ApexModel jsonApexModel = new ApexModelFactory().createApexModel(null, false);
99         result = jsonApexModel.loadFromFile(tempJsonModelFile.getCanonicalPath());
100         assertTrue(result.getResult().equals(ApexApiResult.Result.SUCCESS));
101         tempJsonModelFile.delete();
102
103         final File tempXmlModelFile = File.createTempFile("ApexModelTest", ".xml");
104         result = apexModel.saveToFile(tempXmlModelFile.getCanonicalPath(), true);
105         assertTrue(result.getResult().equals(ApexApiResult.Result.SUCCESS));
106
107         final ApexModel xmlApexModel = new ApexModelFactory().createApexModel(null, false);
108         result = xmlApexModel.loadFromFile(tempXmlModelFile.getCanonicalPath());
109         assertTrue(result.getResult().equals(ApexApiResult.Result.SUCCESS));
110         tempXmlModelFile.delete();
111     }
112
113     @Test
114     public void testApexModelDatabase() throws IOException {
115         final ApexModel apexModel = new ApexModelFactory().createApexModel(null, false);
116
117         ApexApiResult result = apexModel.loadFromFile("src/test/resources/models/PolicyModel.json");
118         assertTrue(result.getResult().equals(ApexApiResult.Result.SUCCESS));
119
120         final DaoParameters DaoParameters = new DaoParameters();
121         DaoParameters.setPluginClass("org.onap.policy.apex.model.basicmodel.dao.impl.DefaultApexDao");
122         DaoParameters.setPersistenceUnit("DAOTest");
123
124         result = apexModel.saveToDatabase(DaoParameters);
125         assertTrue(result.getResult().equals(ApexApiResult.Result.SUCCESS));
126
127         result = apexModel.deleteModel();
128         assertTrue(result.getResult().equals(ApexApiResult.Result.SUCCESS));
129
130         result = apexModel.loadFromDatabase("PolicyModel", "0.0.1", DaoParameters);
131         assertTrue(result.getResult().equals(ApexApiResult.Result.SUCCESS));
132
133         result = apexModel.deleteModel();
134         assertTrue(result.getResult().equals(ApexApiResult.Result.SUCCESS));
135
136         result = apexModel.loadFromDatabase("PolicyModel", null, DaoParameters);
137         assertTrue(result.getResult().equals(ApexApiResult.Result.SUCCESS));
138
139         result = apexModel.deleteModel();
140         assertTrue(result.getResult().equals(ApexApiResult.Result.SUCCESS));
141
142         result = apexModel.loadFromDatabase("VPNPolicyModel", "0.0.1", DaoParameters);
143         assertTrue(result.getResult().equals(ApexApiResult.Result.FAILED));
144     }
145
146     @Test
147     public void testApexModelUrl() throws IOException {
148         ApexModel apexModel = new ApexModelFactory().createApexModel(null, false);
149
150         ApexApiResult result = null;
151
152         try {
153             result = apexModel.readFromUrl(null);
154             fail("expecting an IllegalArgumentException");
155         } catch (final Exception e) {
156             assertTrue(e instanceof IllegalArgumentException);
157         }
158
159         try {
160             result = apexModel.writeToUrl(null, true);
161             fail("expecting an IllegalArgumentException");
162         } catch (final Exception e) {
163             assertTrue(e instanceof IllegalArgumentException);
164         }
165
166         result = apexModel.readFromUrl("zooby/looby");
167         assertTrue(result.getResult().equals(ApexApiResult.Result.FAILED));
168
169         result = apexModel.writeToUrl("zooby/looby", true);
170         assertTrue(result.getResult().equals(ApexApiResult.Result.FAILED));
171
172         result = apexModel.readFromUrl("zooby://zooby/looby");
173         assertTrue(result.getResult().equals(ApexApiResult.Result.FAILED));
174
175         result = apexModel.writeToUrl("zooby://zooby/looby", false);
176         assertTrue(result.getResult().equals(ApexApiResult.Result.FAILED));
177
178         apexModel = new ApexModelFactory().createApexModel(null, false);
179
180         final File tempJsonModelFile = File.createTempFile("ApexModelTest", ".json");
181         result = apexModel.saveToFile(tempJsonModelFile.getCanonicalPath(), false);
182         assertTrue(result.getResult().equals(ApexApiResult.Result.SUCCESS));
183
184         final String tempFileUrlString = tempJsonModelFile.toURI().toString();
185         result = apexModel.readFromUrl(tempFileUrlString);
186         assertTrue(result.getResult().equals(ApexApiResult.Result.SUCCESS));
187
188         result = apexModel.writeToUrl(tempFileUrlString, false);
189         assertTrue(result.getResult().equals(ApexApiResult.Result.FAILED));
190         assertTrue(result.getMessages().get(0).equals("protocol doesn't support output"));
191
192         tempJsonModelFile.delete();
193     }
194
195     @Test
196     public void testApexModelMisc() throws IOException {
197         final ApexModelImpl apexModelImpl = (ApexModelImpl) new ApexModelFactory().createApexModel(null, false);
198
199         ApexApiResult result = null;
200
201         result = apexModelImpl.getModelKey();
202         assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
203
204         result = apexModelImpl.listModel();
205         assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
206
207         result = apexModelImpl.createModel("ModelName", "0.0.1", null, null);
208         assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
209
210         result = apexModelImpl.updateModel("ModelName", "0.0.1", UUID.randomUUID().toString(), "Model Description");
211         assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
212
213         apexModelImpl.deleteModel();
214         assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
215
216         final String modelString = TextFileUtils.getTextFileAsString("src/test/resources/models/PolicyModel.json");
217         result = apexModelImpl.loadFromString(modelString);
218         assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
219
220         final File tempFile = File.createTempFile("ApexModel", "json");
221         tempFile.deleteOnExit();
222         TextFileUtils.putStringAsFile(modelString, tempFile);
223
224         apexModelImpl.deleteModel();
225         assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
226
227         result = apexModelImpl.loadFromFile(tempFile.getCanonicalPath());
228         assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
229
230         result = apexModelImpl.saveToFile(null, false);
231         assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
232
233         result = apexModelImpl.analyse();
234         assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
235
236         result = apexModelImpl.validate();
237         assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
238
239         result = apexModelImpl.compare(tempFile.getCanonicalPath(), true, true);
240         assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
241
242         result = apexModelImpl.compareWithString(modelString, true, true);
243         assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
244
245         result = apexModelImpl.split("policy");
246         assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
247
248         result = apexModelImpl.split(tempFile.getCanonicalPath(), "policy");
249         assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
250
251         result = apexModelImpl.merge(tempFile.getCanonicalPath(), true);
252         assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
253
254         result = apexModelImpl.mergeWithString(modelString, true);
255         assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
256
257         assertNotEquals(0, apexModelImpl.hashCode());
258         assertNotNull(apexModelImpl.clone());
259         assertNotNull(apexModelImpl.build());
260     }
261 }