Changes for checkstyle 8.32
[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 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;
39
40 /**
41  * Test FileMacro in the CLI.
42  */
43 public class FileMacroTest {
44     private String[] fileMacroArgs;
45
46     private File tempModelFile;
47     private File tempLogFile;
48
49     /**
50      * Creates the temp files.
51      *
52      * @throws IOException Signals that an I/O exception has occurred.
53      */
54     @Before
55     public void createTempFiles() throws IOException {
56         tempModelFile = File.createTempFile("TestPolicyModel", ".json");
57         tempLogFile = File.createTempFile("TestPolicyModel", ".log");
58
59         fileMacroArgs = new String[] {"-c", "src/test/resources/scripts/FileMacro.apex", "-l",
60             tempLogFile.getCanonicalPath(), "-o", tempModelFile.getCanonicalPath(), "-if", "true"};
61     }
62
63     /**
64      * Removes the generated models.
65      */
66     @After
67     public void removeGeneratedModels() {
68         tempModelFile.delete();
69     }
70
71     /**
72      * Test logic block macro in CLI scripts.
73      *
74      * @throws IOException Signals that an I/O exception has occurred.
75      * @throws ApexModelException if there is an Apex error
76      */
77     @Test
78     public void testLogicBlock() throws IOException, ApexModelException {
79         final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(fileMacroArgs);
80         // We expect eight errors
81         assertEquals(8, cliEditor.getErrorCount());
82
83         // Read the file from disk
84         final ApexModelReader<AxPolicyModel> modelReader = new ApexModelReader<>(AxPolicyModel.class);
85         modelReader.setValidateFlag(false);
86
87         final URL writtenModelUrl = ResourceUtils.getLocalFile(tempModelFile.getCanonicalPath());
88         final AxPolicyModel writtenModel = modelReader.read(writtenModelUrl.openStream());
89
90         final URL compareModelUrl =
91                 ResourceUtils.getLocalFile("src/test/resources/compare/FileMacroModel_Compare.json");
92         final AxPolicyModel compareModel = modelReader.read(compareModelUrl.openStream());
93
94         // Ignore key info UUIDs
95         writtenModel.getKeyInformation().getKeyInfoMap().clear();
96         compareModel.getKeyInformation().getKeyInfoMap().clear();
97
98         assertTrue(writtenModel.equals(compareModel));
99
100         // The output event is in this file
101         final File outputLogFile = new File(tempLogFile.getCanonicalPath());
102
103         final String outputLogString = TextFileUtils.getTextFileAsString(outputLogFile.getCanonicalPath())
104                 .replace(Paths.get("").toAbsolutePath().toString() + File.separator, "").replaceAll("\\s+", "");
105
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+", "");
109
110         // Check what we got is what we expected to get
111         assertEquals(outputLogCompareString, outputLogString);
112     }
113 }