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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.model.modelapi;
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;
31 import java.io.IOException;
32 import java.sql.Connection;
33 import java.sql.DriverManager;
34 import java.util.UUID;
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;
44 * Test the apex model API.
46 * @author Liam Fallon (liam.fallon@ericsson.com)
48 public class ApexModelApiTest {
49 private Connection connection;
52 public void setup() throws Exception {
53 // Hold the h2 database up for entire tests
54 connection = DriverManager.getConnection("jdbc:h2:mem:testdb");
58 public void teardown() throws Exception {
59 // Close the h2 database after tests
64 public void testApexModelLoadFromFile() {
65 final ApexModel apexModel = new ApexModelFactory().createApexModel(null, false);
67 ApexApiResult result = apexModel.loadFromFile("src/main/resources/models/PolicyModel.json");
68 assertTrue(result.getResult().equals(ApexApiResult.Result.FAILED));
70 result = apexModel.loadFromFile("src/test/resources/models/PolicyModel.json");
71 assertTrue(result.getResult().equals(ApexApiResult.Result.SUCCESS));
73 result = apexModel.deleteModel();
74 assertTrue(result.getResult().equals(ApexApiResult.Result.SUCCESS));
76 result = apexModel.loadFromFile("src/test/resources/models/PolicyModel.xml");
77 assertTrue(result.getResult().equals(ApexApiResult.Result.SUCCESS));
79 result = apexModel.deleteModel();
80 assertTrue(result.getResult().equals(ApexApiResult.Result.SUCCESS));
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"));
88 public void testApexModelSaveToFile() throws IOException {
89 final ApexModel apexModel = new ApexModelFactory().createApexModel(null, false);
91 ApexApiResult result = apexModel.loadFromFile("src/test/resources/models/PolicyModel.json");
92 assertTrue(result.getResult().equals(ApexApiResult.Result.SUCCESS));
94 final File tempJsonModelFile = File.createTempFile("ApexModelTest", ".json");
95 result = apexModel.saveToFile(tempJsonModelFile.getCanonicalPath(), false);
96 assertTrue(result.getResult().equals(ApexApiResult.Result.SUCCESS));
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();
103 final File tempXmlModelFile = File.createTempFile("ApexModelTest", ".xml");
104 result = apexModel.saveToFile(tempXmlModelFile.getCanonicalPath(), true);
105 assertTrue(result.getResult().equals(ApexApiResult.Result.SUCCESS));
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();
114 public void testApexModelDatabase() throws IOException {
115 final ApexModel apexModel = new ApexModelFactory().createApexModel(null, false);
117 ApexApiResult result = apexModel.loadFromFile("src/test/resources/models/PolicyModel.json");
118 assertTrue(result.getResult().equals(ApexApiResult.Result.SUCCESS));
120 final DaoParameters DaoParameters = new DaoParameters();
121 DaoParameters.setPluginClass("org.onap.policy.apex.model.basicmodel.dao.impl.DefaultApexDao");
122 DaoParameters.setPersistenceUnit("DAOTest");
124 result = apexModel.saveToDatabase(DaoParameters);
125 assertTrue(result.getResult().equals(ApexApiResult.Result.SUCCESS));
127 result = apexModel.deleteModel();
128 assertTrue(result.getResult().equals(ApexApiResult.Result.SUCCESS));
130 result = apexModel.loadFromDatabase("PolicyModel", "0.0.1", DaoParameters);
131 assertTrue(result.getResult().equals(ApexApiResult.Result.SUCCESS));
133 result = apexModel.deleteModel();
134 assertTrue(result.getResult().equals(ApexApiResult.Result.SUCCESS));
136 result = apexModel.loadFromDatabase("PolicyModel", null, DaoParameters);
137 assertTrue(result.getResult().equals(ApexApiResult.Result.SUCCESS));
139 result = apexModel.deleteModel();
140 assertTrue(result.getResult().equals(ApexApiResult.Result.SUCCESS));
142 result = apexModel.loadFromDatabase("VPNPolicyModel", "0.0.1", DaoParameters);
143 assertTrue(result.getResult().equals(ApexApiResult.Result.FAILED));
147 public void testApexModelUrl() throws IOException {
148 ApexModel apexModel = new ApexModelFactory().createApexModel(null, false);
150 ApexApiResult result = null;
153 result = apexModel.readFromUrl(null);
154 fail("expecting an IllegalArgumentException");
155 } catch (final Exception e) {
156 assertTrue(e instanceof IllegalArgumentException);
160 result = apexModel.writeToUrl(null, true);
161 fail("expecting an IllegalArgumentException");
162 } catch (final Exception e) {
163 assertTrue(e instanceof IllegalArgumentException);
166 result = apexModel.readFromUrl("zooby/looby");
167 assertTrue(result.getResult().equals(ApexApiResult.Result.FAILED));
169 result = apexModel.writeToUrl("zooby/looby", true);
170 assertTrue(result.getResult().equals(ApexApiResult.Result.FAILED));
172 result = apexModel.readFromUrl("zooby://zooby/looby");
173 assertTrue(result.getResult().equals(ApexApiResult.Result.FAILED));
175 result = apexModel.writeToUrl("zooby://zooby/looby", false);
176 assertTrue(result.getResult().equals(ApexApiResult.Result.FAILED));
178 apexModel = new ApexModelFactory().createApexModel(null, false);
180 final File tempJsonModelFile = File.createTempFile("ApexModelTest", ".json");
181 result = apexModel.saveToFile(tempJsonModelFile.getCanonicalPath(), false);
182 assertTrue(result.getResult().equals(ApexApiResult.Result.SUCCESS));
184 final String tempFileUrlString = tempJsonModelFile.toURI().toString();
185 result = apexModel.readFromUrl(tempFileUrlString);
186 assertTrue(result.getResult().equals(ApexApiResult.Result.SUCCESS));
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"));
192 tempJsonModelFile.delete();
196 public void testApexModelMisc() throws IOException {
197 final ApexModelImpl apexModelImpl = (ApexModelImpl) new ApexModelFactory().createApexModel(null, false);
199 ApexApiResult result = null;
201 result = apexModelImpl.getModelKey();
202 assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
204 result = apexModelImpl.listModel();
205 assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
207 result = apexModelImpl.createModel("ModelName", "0.0.1", null, null);
208 assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
210 result = apexModelImpl.updateModel("ModelName", "0.0.1", UUID.randomUUID().toString(), "Model Description");
211 assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
213 apexModelImpl.deleteModel();
214 assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
216 final String modelString = TextFileUtils.getTextFileAsString("src/test/resources/models/PolicyModel.json");
217 result = apexModelImpl.loadFromString(modelString);
218 assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
220 final File tempFile = File.createTempFile("ApexModel", "json");
221 tempFile.deleteOnExit();
222 TextFileUtils.putStringAsFile(modelString, tempFile);
224 apexModelImpl.deleteModel();
225 assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
227 result = apexModelImpl.loadFromFile(tempFile.getCanonicalPath());
228 assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
230 result = apexModelImpl.saveToFile(null, false);
231 assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
233 result = apexModelImpl.analyse();
234 assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
236 result = apexModelImpl.validate();
237 assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
239 result = apexModelImpl.compare(tempFile.getCanonicalPath(), true, true);
240 assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
242 result = apexModelImpl.compareWithString(modelString, true, true);
243 assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
245 result = apexModelImpl.split("policy");
246 assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
248 result = apexModelImpl.split(tempFile.getCanonicalPath(), "policy");
249 assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
251 result = apexModelImpl.merge(tempFile.getCanonicalPath(), true);
252 assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
254 result = apexModelImpl.mergeWithString(modelString, true);
255 assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
257 assertNotEquals(0, apexModelImpl.hashCode());
258 assertNotNull(apexModelImpl.clone());
259 assertNotNull(apexModelImpl.build());