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
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 java.nio.file.Paths;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
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.policymodel.concepts.AxPolicyModel;
38 import org.onap.policy.common.utils.resources.ResourceUtils;
39 import org.onap.policy.common.utils.resources.TextFileUtils;
42 * Test FileMacro in the CLI.
44 public class FileMacroTest {
45 private String[] fileMacroArgs;
47 private File tempModelFile;
48 private File tempLogFile;
51 * Creates the temp files.
53 * @throws IOException Signals that an I/O exception has occurred.
56 public void createTempFiles() throws IOException {
57 tempModelFile = File.createTempFile("TestPolicyModel", ".json");
58 tempLogFile = File.createTempFile("TestPolicyModel", ".log");
60 fileMacroArgs = new String[] {"-c", "src/test/resources/scripts/FileMacro.apex", "-l",
61 tempLogFile.getCanonicalPath(), "-o", tempModelFile.getCanonicalPath(), "-if", "true"};
65 * Removes the generated models.
68 public void removeGeneratedModels() {
69 tempModelFile.delete();
73 * Test logic block macro in CLI scripts.
75 * @throws IOException Signals that an I/O exception has occurred.
76 * @throws ApexModelException if there is an Apex error
79 public void testLogicBlock() throws IOException, ApexModelException {
80 final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(fileMacroArgs);
81 // We expect eight errors
82 assertEquals(8, cliEditor.getErrorCount());
84 // Read the file from disk
85 final ApexModelReader<AxPolicyModel> modelReader = new ApexModelReader<>(AxPolicyModel.class);
86 modelReader.setValidateFlag(false);
88 final URL writtenModelUrl = ResourceUtils.getLocalFile(tempModelFile.getCanonicalPath());
89 final AxPolicyModel writtenModel = modelReader.read(writtenModelUrl.openStream());
91 final URL compareModelUrl =
92 ResourceUtils.getLocalFile("src/test/resources/compare/FileMacroModel_Compare.json");
93 final AxPolicyModel compareModel = modelReader.read(compareModelUrl.openStream());
95 // Ignore key info UUIDs
96 writtenModel.getKeyInformation().getKeyInfoMap().clear();
97 compareModel.getKeyInformation().getKeyInfoMap().clear();
99 assertTrue(writtenModel.equals(compareModel));
101 // The output event is in this file
102 final File outputLogFile = new File(tempLogFile.getCanonicalPath());
104 final String outputLogString = TextFileUtils.getTextFileAsString(outputLogFile.getCanonicalPath())
105 .replace(Paths.get("").toAbsolutePath().toString() + File.separator, "").replaceAll("\\s+", "");
107 // We compare the log to what we expect to get
108 final String outputLogCompareString = TextFileUtils
109 .getTextFileAsString("src/test/resources/compare/FileMacro_Compare.log").replaceAll("\\s+", "");
111 // Check what we got is what we expected to get
112 assertEquals(outputLogCompareString, outputLogString);