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.AxValidationResult;
34 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
35 import org.onap.policy.apex.model.basicmodel.handling.ApexModelReader;
36 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
37 import org.onap.policy.common.utils.resources.ResourceUtils;
40 * The Class TestCLIEditorScripting.
42 * @author Liam Fallon (liam.fallon@ericsson.com)
44 public class CommandLineEditorScriptingTest {
46 private File tempModelFile;
47 private File tempLogFile;
49 private String[] samplePolicyArgs;
51 private String[] samplePolicyMapArgs;
56 * @throws IOException Signals that an I/O exception has occurred.
59 public void initialiseArgs() throws IOException {
60 tempModelFile = File.createTempFile("SampleLBPolicyMap", ".json");
61 tempLogFile = File.createTempFile("SampleLBPolicyMap", ".log");
63 samplePolicyArgs = new String[] {"-c", "src/test/resources/scripts/SampleLBPolicy.apex", "-o",
64 tempModelFile.getAbsolutePath(), "-l", tempLogFile.getAbsolutePath()};
66 samplePolicyMapArgs = new String[] {"-c", "src/test/resources/scripts/SampleLBPolicy_WithMap.apex", "-o",
67 tempModelFile.getAbsolutePath(), "-l", tempLogFile.getAbsolutePath()};
71 * Removes the generated files.
74 public void removeGeneratedFiles() {
75 tempModelFile.delete();
80 * Test sample FLB policy script.
82 * @throws IOException Signals that an I/O exception has occurred.
83 * @throws ApexModelException if there is an Apex error
86 public void testSamplePolicyScript() throws IOException, ApexModelException {
87 final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(samplePolicyArgs);
88 assertEquals(0, cliEditor.getErrorCount());
90 // Read the file from disk
91 final ApexModelReader<AxPolicyModel> modelReader = new ApexModelReader<>(AxPolicyModel.class);
93 final URL writtenModelUrl = ResourceUtils.getLocalFile(tempModelFile.getCanonicalPath());
94 final AxPolicyModel writtenModel = modelReader.read(writtenModelUrl.openStream());
96 final URL compareModelUrl =
97 ResourceUtils.getLocalFile("src/test/resources/compare/FLBPolicyModel_Compare.json");
98 final AxPolicyModel compareModel = modelReader.read(compareModelUrl.openStream());
100 final URL compareModelNoAlbumsUrl =
101 ResourceUtils.getLocalFile("src/test/resources/compare/FLBPolicyModel_noAlbums_Compare.json");
102 final AxPolicyModel compareNoAlbumsModel = modelReader.read(compareModelNoAlbumsUrl.openStream());
104 // Ignore key info UUIDs
105 writtenModel.getKeyInformation().getKeyInfoMap().clear();
106 compareModel.getKeyInformation().getKeyInfoMap().clear();
107 compareNoAlbumsModel.getKeyInformation().getKeyInfoMap().clear();
109 assertEquals(writtenModel, compareModel);
110 assertEquals(writtenModel, compareNoAlbumsModel);
111 assertEquals(compareModel, compareNoAlbumsModel);
115 * Test sample FLB map policy script.
117 * @throws IOException Signals that an I/O exception has occurred.
118 * @throws ApexModelException if there is an Apex error
121 public void testSampleMapPolicyScript() throws IOException, ApexModelException {
122 tempModelFile.delete();
124 final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(samplePolicyMapArgs);
125 assertEquals(0, cliEditor.getErrorCount());
127 assertTrue(tempModelFile.isFile());
129 // Read the file from disk
130 final ApexModelReader<AxPolicyModel> modelReader = new ApexModelReader<>(AxPolicyModel.class);
132 final URL writtenModelUrl = ResourceUtils.getLocalFile(tempModelFile.getCanonicalPath());
133 final AxPolicyModel writtenModel = modelReader.read(writtenModelUrl.openStream());
135 final AxValidationResult validationResult = new AxValidationResult();
136 writtenModel.validate(validationResult);
137 assertEquals(AxValidationResult.ValidationResult.OBSERVATION, validationResult.getValidationResult());