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