2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-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;
25 import static org.junit.Assert.assertTrue;
28 import java.io.IOException;
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
35 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
36 import org.onap.policy.apex.model.basicmodel.handling.ApexModelReader;
37 import org.onap.policy.apex.model.basicmodel.handling.ApexModelStringWriter;
38 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
39 import org.onap.policy.common.utils.resources.ResourceUtils;
42 * The Class TestCLIEditorScripting.
44 * @author Liam Fallon (liam.fallon@ericsson.com)
46 public class CommandLineEditorScriptingTest {
48 private File tempModelFile;
49 private File tempLogFile;
51 private String[] samplePolicyArgs;
53 private String[] samplePolicyMapArgs;
58 * @throws IOException Signals that an I/O exception has occurred.
61 public void initialiseArgs() throws IOException {
62 tempModelFile = File.createTempFile("SampleLBPolicyMap", ".json");
63 tempLogFile = File.createTempFile("SampleLBPolicyMap", ".log");
65 samplePolicyArgs = new String[] {"-c", "src/test/resources/scripts/SampleLBPolicy.apex", "-o",
66 tempModelFile.getAbsolutePath(), "-l", tempLogFile.getAbsolutePath()};
68 samplePolicyMapArgs = new String[] {"-c", "src/test/resources/scripts/SampleLBPolicy_WithMap.apex", "-o",
69 tempModelFile.getAbsolutePath(), "-l", tempLogFile.getAbsolutePath()};
73 * Removes the generated files.
76 public void removeGeneratedFiles() {
77 tempModelFile.delete();
82 * Test sample FLB policy script.
84 * @throws IOException Signals that an I/O exception has occurred.
85 * @throws ApexModelException if there is an Apex error
88 public void testSamplePolicyScript() throws IOException, ApexModelException {
89 final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(samplePolicyArgs);
90 assertEquals(0, cliEditor.getErrorCount());
92 // Read the file from disk
93 final ApexModelReader<AxPolicyModel> modelReader = new ApexModelReader<>(AxPolicyModel.class);
95 final URL writtenModelUrl = ResourceUtils.getLocalFile(tempModelFile.getCanonicalPath());
96 final AxPolicyModel writtenModel = modelReader.read(writtenModelUrl.openStream());
98 final URL compareModelUrl =
99 ResourceUtils.getLocalFile("src/test/resources/compare/FLBPolicyModel_Compare.json");
100 final AxPolicyModel compareModel = modelReader.read(compareModelUrl.openStream());
102 final URL compareModelNoAlbumsUrl =
103 ResourceUtils.getLocalFile("src/test/resources/compare/FLBPolicyModel_noAlbums_Compare.json");
104 final AxPolicyModel compareNoAlbumsModel = modelReader.read(compareModelNoAlbumsUrl.openStream());
106 // Ignore key info UUIDs
107 writtenModel.getKeyInformation().getKeyInfoMap().clear();
108 compareModel.getKeyInformation().getKeyInfoMap().clear();
109 compareNoAlbumsModel.getKeyInformation().getKeyInfoMap().clear();
111 assertEquals(writtenModel, compareModel);
112 assertEquals(writtenModel, compareNoAlbumsModel);
113 assertEquals(compareModel, compareNoAlbumsModel);
117 * Test sample FLB map policy script.
119 * @throws IOException Signals that an I/O exception has occurred.
120 * @throws ApexModelException if there is an Apex error
123 public void testSampleMapPolicyScript() throws IOException, ApexModelException {
124 tempModelFile.delete();
126 final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(samplePolicyMapArgs);
127 assertEquals(0, cliEditor.getErrorCount());
129 assertTrue(tempModelFile.isFile());
131 // Read the file from disk
132 final ApexModelReader<AxPolicyModel> modelReader = new ApexModelReader<>(AxPolicyModel.class);
134 final URL writtenModelUrl = ResourceUtils.getLocalFile(tempModelFile.getCanonicalPath());
135 final AxPolicyModel writtenModel = modelReader.read(writtenModelUrl.openStream());
137 final AxValidationResult validationResult = new AxValidationResult();
138 writtenModel.validate(validationResult);
139 assertEquals(AxValidationResult.ValidationResult.OBSERVATION, validationResult.getValidationResult());