2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020,2022 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.auth.clieditor;
24 import static org.junit.Assert.assertEquals;
27 import java.io.IOException;
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
33 import org.onap.policy.apex.model.basicmodel.handling.ApexModelReader;
34 import org.onap.policy.apex.model.basicmodel.handling.ApexModelStringWriter;
35 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
36 import org.onap.policy.common.utils.resources.ResourceUtils;
38 // TODO: Auto-generated Javadoc
40 * The Class TestLogicBlock.
42 public class LogicBlockTest {
43 private String[] logicBlockArgs;
44 private String[] avroSchemaArgs;
46 private File tempLogicModelFile;
47 private File tempAvroModelFile;
50 * Creates the temp files.
52 * @throws IOException Signals that an I/O exception has occurred.
55 public void createTempFiles() throws IOException {
56 tempLogicModelFile = File.createTempFile("TestLogicPolicyModel", ".json");
57 tempAvroModelFile = File.createTempFile("TestAvroPolicyModel", ".json");
59 logicBlockArgs = new String[] {"-c", "src/test/resources/scripts/LogicBlock.apex", "-o",
60 tempLogicModelFile.getCanonicalPath(), "-if", "true", "-nl"};
62 avroSchemaArgs = new String[] {"-c", "src/test/resources/scripts/AvroSchema.apex", "-o",
63 tempAvroModelFile.getCanonicalPath(), "-nl"};
67 * Removes the temp files.
70 public void removeTempFiles() {
71 tempLogicModelFile.delete();
72 tempAvroModelFile.delete();
78 * @throws IOException Signals that an I/O exception has occurred.
79 * @throws ApexModelException if there is an Apex error
82 public void testLogicBlock() throws IOException, ApexModelException {
83 new ApexCommandLineEditorMain(logicBlockArgs);
85 // Read the file from disk
86 final ApexModelReader<AxPolicyModel> modelReader = new ApexModelReader<>(AxPolicyModel.class);
87 modelReader.setValidate(false);
89 final URL writtenModelUrl = ResourceUtils.getLocalFile(tempLogicModelFile.getCanonicalPath());
90 final AxPolicyModel writtenModel = modelReader.read(writtenModelUrl.openStream());
92 final URL compareModelUrl =
93 ResourceUtils.getLocalFile("src/test/resources/compare/LogicBlockModel_Compare.json");
94 final AxPolicyModel compareModel = modelReader.read(compareModelUrl.openStream());
96 // Ignore key info UUIDs
97 writtenModel.getKeyInformation().getKeyInfoMap().clear();
98 compareModel.getKeyInformation().getKeyInfoMap().clear();
100 assertEquals(writtenModel, compareModel);
106 * @throws IOException Signals that an I/O exception has occurred.
107 * @throws ApexModelException if there is an Apex error
110 public void testAvroSchema() throws IOException, ApexModelException {
111 new ApexCommandLineEditorMain(avroSchemaArgs);
113 // Read the file from disk
114 final ApexModelReader<AxPolicyModel> modelReader = new ApexModelReader<>(AxPolicyModel.class);
115 modelReader.setValidate(false);
117 final URL writtenModelUrl = ResourceUtils.getLocalFile(tempAvroModelFile.getCanonicalPath());
118 final AxPolicyModel writtenModel = modelReader.read(writtenModelUrl.openStream());
120 final URL compareModelUrl =
121 ResourceUtils.getLocalFile("src/test/resources/compare/AvroSchemaModel_Compare.json");
122 final AxPolicyModel compareModel = modelReader.read(compareModelUrl.openStream());
124 // Ignore key info UUIDs
125 writtenModel.getKeyInformation().getKeyInfoMap().clear();
126 compareModel.getKeyInformation().getKeyInfoMap().clear();
128 assertEquals(writtenModel, compareModel);