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;
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
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;
38 import org.onap.policy.common.utils.resources.TextFileUtils;
41 * Test FileMacro in the CLI.
43 public class FileMacroTest {
44 private String[] fileMacroArgs;
46 private File tempModelFile;
47 private File tempLogFile;
50 * Creates the temp files.
52 * @throws IOException Signals that an I/O exception has occurred.
55 public void createTempFiles() throws IOException {
56 tempModelFile = File.createTempFile("TestPolicyModel", ".json");
57 tempLogFile = File.createTempFile("TestPolicyModel", ".log");
59 fileMacroArgs = new String[] {"-c", "src/test/resources/scripts/FileMacro.apex", "-l",
60 tempLogFile.getCanonicalPath(), "-o", tempModelFile.getCanonicalPath(), "-if", "true"};
64 * Removes the generated models.
67 public void removeGeneratedModels() {
68 tempModelFile.delete();
72 * Test logic block macro in CLI scripts.
74 * @throws IOException Signals that an I/O exception has occurred.
75 * @throws ApexModelException if there is an Apex error
78 public void testLogicBlock() throws IOException, ApexModelException {
79 final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(fileMacroArgs);
80 // We expect eight errors
81 assertEquals(8, cliEditor.getErrorCount());
83 // Read the file from disk
84 final ApexModelReader<AxPolicyModel> modelReader = new ApexModelReader<>(AxPolicyModel.class);
85 modelReader.setValidateFlag(false);
87 final URL writtenModelUrl = ResourceUtils.getLocalFile(tempModelFile.getCanonicalPath());
88 final AxPolicyModel writtenModel = modelReader.read(writtenModelUrl.openStream());
90 final URL compareModelUrl =
91 ResourceUtils.getLocalFile("src/test/resources/compare/FileMacroModel_Compare.json");
92 final AxPolicyModel compareModel = modelReader.read(compareModelUrl.openStream());
94 // Ignore key info UUIDs
95 writtenModel.getKeyInformation().getKeyInfoMap().clear();
96 compareModel.getKeyInformation().getKeyInfoMap().clear();
98 assertTrue(writtenModel.equals(compareModel));
100 // The output event is in this file
101 final File outputLogFile = new File(tempLogFile.getCanonicalPath());
103 final String outputLogString = TextFileUtils.getTextFileAsString(outputLogFile.getCanonicalPath())
104 .replace(Paths.get("").toAbsolutePath().toString() + File.separator, "").replaceAll("\\s+", "");
106 // We compare the log to what we expect to get
107 final String outputLogCompareString = TextFileUtils
108 .getTextFileAsString("src/test/resources/compare/FileMacro_Compare.log").replaceAll("\\s+", "");
110 // Check what we got is what we expected to get
111 assertEquals(outputLogCompareString, outputLogString);