aboutsummaryrefslogtreecommitdiffstats
path: root/model/model-api/src/test/java/org/onap/policy/apex/model/modelapi/ApexModelApiTest.java
blob: f28d0e634f8e5689ceb0f250bb6757d69274e6db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 *  Modifications Copyright (C) 2020,2022 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.apex.model.modelapi;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.UUID;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onap.policy.apex.model.modelapi.impl.ApexModelImpl;
import org.onap.policy.common.utils.resources.TextFileUtils;

/**
 * Test the apex model API.
 *
 * @author Liam Fallon (liam.fallon@ericsson.com)
 */
public class ApexModelApiTest {
    private Connection connection;

    @Before
    public void setup() throws Exception {
        // Hold the h2 database up for entire tests
        connection = DriverManager.getConnection("jdbc:h2:mem:testdb");
    }

    @After
    public void teardown() throws Exception {
        // Close the h2 database after tests
        connection.close();
    }

    @Test
    public void testApexModelLoadFromFile() {
        final ApexModel apexModel = new ApexModelFactory().createApexModel(null);

        ApexApiResult result = apexModel.loadFromFile("src/main/resources/models/PolicyModel.json");
        assertEquals(ApexApiResult.Result.FAILED, result.getResult());

        result = apexModel.loadFromFile("src/test/resources/models/PolicyModel.json");
        assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());

        result = apexModel.deleteModel();
        assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());

        result = apexModel.loadFromFile("src/test/resources/models/PolicyModel.json");
        assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());

        result = apexModel.deleteModel();
        assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());

        result = apexModel.loadFromFile("src/test/resources/models/PolicyModel.junk");
        assertEquals(ApexApiResult.Result.FAILED, result.getResult());
        assertEquals("Unable to unmarshal Apex concept", result.getMessages().get(0).trim());
    }

    @Test
    public void testApexModelSaveToFile() throws IOException {
        final ApexModel apexModel = new ApexModelFactory().createApexModel(null);

        ApexApiResult result = apexModel.loadFromFile("src/test/resources/models/PolicyModel.json");
        assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());

        final File tempJsonModelFile = File.createTempFile("ApexModelTest", ".json");
        result = apexModel.saveToFile(tempJsonModelFile.getPath());
        assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());

        final ApexModel jsonApexModel = new ApexModelFactory().createApexModel(null);
        result = jsonApexModel.loadFromFile(tempJsonModelFile.getPath());
        assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
        tempJsonModelFile.delete();

        final File tempModelFile = File.createTempFile("ApexModelTest", ".json");
        result = apexModel.saveToFile(tempModelFile.getPath());
        assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());

        final ApexModel testApexModel = new ApexModelFactory().createApexModel(null);
        result = testApexModel.loadFromFile(tempModelFile.getPath());
        assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
        tempModelFile.delete();
    }

    @Test
    public void testApexModelUrl() throws IOException {
        final ApexModel apexModel = new ApexModelFactory().createApexModel(null);

        assertThatThrownBy(() -> apexModel.readFromUrl(null)).isInstanceOf(IllegalArgumentException.class);
        assertThatThrownBy(() -> apexModel.writeToUrl(null)).isInstanceOf(IllegalArgumentException.class);
        ApexApiResult result = null;
        result = apexModel.readFromUrl("zooby/looby");
        assertEquals(ApexApiResult.Result.FAILED, result.getResult());

        result = apexModel.writeToUrl("zooby/looby");
        assertEquals(ApexApiResult.Result.FAILED, result.getResult());

        result = apexModel.readFromUrl("zooby://zooby/looby");
        assertEquals(ApexApiResult.Result.FAILED, result.getResult());

        result = apexModel.writeToUrl("zooby://zooby/looby");
        assertEquals(ApexApiResult.Result.FAILED, result.getResult());

        final File tempJsonModelFile = File.createTempFile("ApexModelTest", "json");

        result = apexModel.saveToFile(tempJsonModelFile.getPath());
        assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());

        final String tempFileUrlString = tempJsonModelFile.toURI().toString();
        result = apexModel.readFromUrl(tempFileUrlString);
        assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());

        result = apexModel.writeToUrl(tempFileUrlString);
        assertEquals(ApexApiResult.Result.FAILED, result.getResult());
        assertEquals("protocol doesn't support output", result.getMessages().get(0));

        tempJsonModelFile.delete();
    }

    @Test
    public void testApexModelMisc() throws IOException {
        final ApexModelImpl apexModelImpl = (ApexModelImpl) new ApexModelFactory().createApexModel(null);

        ApexApiResult result = null;

        result = apexModelImpl.getModelKey();
        assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());

        result = apexModelImpl.listModel();
        assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());

        result = apexModelImpl.createModel("ModelName", "0.0.1", null, null);
        assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());

        result = apexModelImpl.updateModel("ModelName", "0.0.1", UUID.randomUUID().toString(), "Model Description");
        assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());

        apexModelImpl.deleteModel();
        assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());

        final String modelString = TextFileUtils.getTextFileAsString("src/test/resources/models/PolicyModel.json");
        result = apexModelImpl.loadFromString(modelString);
        assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());

        final File tempFile = File.createTempFile("ApexModel", "json");
        tempFile.deleteOnExit();
        TextFileUtils.putStringAsFile(modelString, tempFile);

        apexModelImpl.deleteModel();
        assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());

        result = apexModelImpl.loadFromFile(tempFile.getPath());
        assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());

        result = apexModelImpl.analyse();
        assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());

        result = apexModelImpl.validate();
        assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());

        result = apexModelImpl.compare(tempFile.getPath(), true, true);
        assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());

        result = apexModelImpl.compareWithString(modelString, true, true);
        assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());

        result = apexModelImpl.split("policy");
        assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());

        result = apexModelImpl.split(tempFile.getPath(), "policy");
        assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());

        result = apexModelImpl.merge(tempFile.getPath(), true);
        assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());

        result = apexModelImpl.mergeWithString(modelString, true);
        assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());

        assertNotEquals(0, apexModelImpl.hashCode());
        assertNotNull(apexModelImpl.getCopy());
        assertNotNull(apexModelImpl.build());
    }
}