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