f458a9fb9bb4653575a8d88b90e36fc101fb8443
[policy/apex-pdp.git] / auth / cli-editor / src / test / java / org / onap / policy / apex / auth / clieditor / FileMacroTest.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.auth.clieditor;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26
27 import java.io.File;
28 import java.io.IOException;
29 import java.net.URL;
30 import java.nio.file.Paths;
31
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;
40
41 /**
42  * Test FileMacro in the CLI.
43  */
44 public class FileMacroTest {
45     private String[] fileMacroArgs;
46
47     private File tempModelFile;
48     private File tempLogFile;
49
50     /**
51      * Creates the temp files.
52      *
53      * @throws IOException Signals that an I/O exception has occurred.
54      */
55     @Before
56     public void createTempFiles() throws IOException {
57         tempModelFile = File.createTempFile("TestPolicyModel", ".json");
58         tempLogFile = File.createTempFile("TestPolicyModel", ".log");
59
60         fileMacroArgs = new String[] {"-c", "src/test/resources/scripts/FileMacro.apex", "-l",
61             tempLogFile.getCanonicalPath(), "-o", tempModelFile.getCanonicalPath(), "-if", "true"};
62     }
63
64     /**
65      * Removes the generated models.
66      */
67     @After
68     public void removeGeneratedModels() {
69         tempModelFile.delete();
70     }
71
72     /**
73      * Test logic block macro in CLI scripts.
74      *
75      * @throws IOException Signals that an I/O exception has occurred.
76      * @throws ApexModelException if there is an Apex error
77      */
78     @Test
79     public void testLogicBlock() throws IOException, ApexModelException {
80         final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(fileMacroArgs);
81         // We expect eight errors
82         assertEquals(8, cliEditor.getErrorCount());
83
84         // Read the file from disk
85         final ApexModelReader<AxPolicyModel> modelReader = new ApexModelReader<>(AxPolicyModel.class);
86         modelReader.setValidateFlag(false);
87
88         final URL writtenModelUrl = ResourceUtils.getLocalFile(tempModelFile.getCanonicalPath());
89         final AxPolicyModel writtenModel = modelReader.read(writtenModelUrl.openStream());
90
91         final URL compareModelUrl =
92                 ResourceUtils.getLocalFile("src/test/resources/compare/FileMacroModel_Compare.json");
93         final AxPolicyModel compareModel = modelReader.read(compareModelUrl.openStream());
94
95         // Ignore key info UUIDs
96         writtenModel.getKeyInformation().getKeyInfoMap().clear();
97         compareModel.getKeyInformation().getKeyInfoMap().clear();
98
99         assertTrue(writtenModel.equals(compareModel));
100
101         // The output event is in this file
102         final File outputLogFile = new File(tempLogFile.getCanonicalPath());
103
104         final String outputLogString = TextFileUtils.getTextFileAsString(outputLogFile.getCanonicalPath())
105                 .replace(Paths.get("").toAbsolutePath().toString() + File.separator, "").replaceAll("\\s+", "");
106
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+", "");
110
111         // Check what we got is what we expected to get
112         assertEquals(outputLogCompareString, outputLogString);
113     }
114 }