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