75a880a4f1c30b915b593e077cc0b4a08417516d
[policy/apex-pdp.git] / auth / cli-editor / src / test / java / org / onap / policy / apex / auth / clieditor / CommandLineEditorScriptingTest.java
1 /*-
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
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 org.junit.After;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
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.basicmodel.handling.ApexModelStringWriter;
38 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
39 import org.onap.policy.common.utils.resources.ResourceUtils;
40
41 /**
42  * The Class TestCLIEditorScripting.
43  *
44  * @author Liam Fallon (liam.fallon@ericsson.com)
45  */
46 public class CommandLineEditorScriptingTest {
47
48     private File tempModelFile;
49     private File tempLogFile;
50
51     private String[] samplePolicyArgs;
52
53     private String[] samplePolicyMapArgs;
54
55     /**
56      * Initialise args.
57      *
58      * @throws IOException Signals that an I/O exception has occurred.
59      */
60     @Before
61     public void initialiseArgs() throws IOException {
62         tempModelFile = File.createTempFile("SampleLBPolicyMap", ".json");
63         tempLogFile = File.createTempFile("SampleLBPolicyMap", ".log");
64
65         samplePolicyArgs = new String[] {"-c", "src/test/resources/scripts/SampleLBPolicy.apex", "-o",
66             tempModelFile.getAbsolutePath(), "-l", tempLogFile.getAbsolutePath()};
67
68         samplePolicyMapArgs = new String[] {"-c", "src/test/resources/scripts/SampleLBPolicy_WithMap.apex", "-o",
69             tempModelFile.getAbsolutePath(), "-l", tempLogFile.getAbsolutePath()};
70     }
71
72     /**
73      * Removes the generated files.
74      */
75     @After
76     public void removeGeneratedFiles() {
77         tempModelFile.delete();
78         tempLogFile.delete();
79     }
80
81     /**
82      * Test sample FLB policy script.
83      *
84      * @throws IOException        Signals that an I/O exception has occurred.
85      * @throws ApexModelException if there is an Apex error
86      */
87     @Test
88     public void testSamplePolicyScript() throws IOException, ApexModelException {
89         final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(samplePolicyArgs);
90         assertEquals(0, cliEditor.getErrorCount());
91
92         // Read the file from disk
93         final ApexModelReader<AxPolicyModel> modelReader = new ApexModelReader<>(AxPolicyModel.class);
94
95         final URL writtenModelUrl = ResourceUtils.getLocalFile(tempModelFile.getCanonicalPath());
96         final AxPolicyModel writtenModel = modelReader.read(writtenModelUrl.openStream());
97
98         final URL compareModelUrl =
99             ResourceUtils.getLocalFile("src/test/resources/compare/FLBPolicyModel_Compare.json");
100         final AxPolicyModel compareModel = modelReader.read(compareModelUrl.openStream());
101
102         final URL compareModelNoAlbumsUrl =
103             ResourceUtils.getLocalFile("src/test/resources/compare/FLBPolicyModel_noAlbums_Compare.json");
104         final AxPolicyModel compareNoAlbumsModel = modelReader.read(compareModelNoAlbumsUrl.openStream());
105
106         // Ignore key info UUIDs
107         writtenModel.getKeyInformation().getKeyInfoMap().clear();
108         compareModel.getKeyInformation().getKeyInfoMap().clear();
109         compareNoAlbumsModel.getKeyInformation().getKeyInfoMap().clear();
110
111         assertEquals(writtenModel, compareModel);
112         assertEquals(writtenModel, compareNoAlbumsModel);
113         assertEquals(compareModel, compareNoAlbumsModel);
114     }
115
116     /**
117      * Test sample FLB map policy script.
118      *
119      * @throws IOException        Signals that an I/O exception has occurred.
120      * @throws ApexModelException if there is an Apex error
121      */
122     @Test
123     public void testSampleMapPolicyScript() throws IOException, ApexModelException {
124         tempModelFile.delete();
125
126         final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(samplePolicyMapArgs);
127         assertEquals(0, cliEditor.getErrorCount());
128
129         assertTrue(tempModelFile.isFile());
130
131         // Read the file from disk
132         final ApexModelReader<AxPolicyModel> modelReader = new ApexModelReader<>(AxPolicyModel.class);
133
134         final URL writtenModelUrl = ResourceUtils.getLocalFile(tempModelFile.getCanonicalPath());
135         final AxPolicyModel writtenModel = modelReader.read(writtenModelUrl.openStream());
136
137         final AxValidationResult validationResult = new AxValidationResult();
138         writtenModel.validate(validationResult);
139         assertEquals(AxValidationResult.ValidationResult.OBSERVATION, validationResult.getValidationResult());
140     }
141 }