Fix minor warnings in code
[policy/apex-pdp.git] / auth / cli-editor / src / test / java / org / onap / policy / apex / auth / clieditor / LogicBlockTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 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
26 import java.io.File;
27 import java.io.IOException;
28 import java.net.URL;
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
33 import org.onap.policy.apex.model.basicmodel.handling.ApexModelReader;
34 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
35 import org.onap.policy.common.utils.resources.ResourceUtils;
36
37 // TODO: Auto-generated Javadoc
38 /**
39  * The Class TestLogicBlock.
40  */
41 public class LogicBlockTest {
42     private String[] logicBlockArgs;
43     private String[] avroSchemaArgs;
44
45     private File tempLogicModelFile;
46     private File tempAvroModelFile;
47
48     /**
49      * Creates the temp files.
50      *
51      * @throws IOException Signals that an I/O exception has occurred.
52      */
53     @Before
54     public void createTempFiles() throws IOException {
55         tempLogicModelFile = File.createTempFile("TestLogicPolicyModel", ".json");
56         tempAvroModelFile = File.createTempFile("TestAvroPolicyModel", ".json");
57
58         logicBlockArgs = new String[] {"-c", "src/test/resources/scripts/LogicBlock.apex", "-o",
59                 tempLogicModelFile.getCanonicalPath(), "-if", "true", "-nl"};
60
61         avroSchemaArgs = new String[] {"-c", "src/test/resources/scripts/AvroSchema.apex", "-o",
62                 tempAvroModelFile.getCanonicalPath(), "-nl"};
63     }
64
65     /**
66      * Removes the temp files.
67      */
68     @After
69     public void removeTempFiles() {
70         tempLogicModelFile.delete();
71         tempAvroModelFile.delete();
72     }
73
74     /**
75      * Test logic block.
76      *
77      * @throws IOException Signals that an I/O exception has occurred.
78      * @throws ApexModelException if there is an Apex error
79      */
80     @Test
81     public void testLogicBlock() throws IOException, ApexModelException {
82         new ApexCommandLineEditorMain(logicBlockArgs);
83
84         // Read the file from disk
85         final ApexModelReader<AxPolicyModel> modelReader = new ApexModelReader<>(AxPolicyModel.class);
86         modelReader.setValidate(false);
87
88         final URL writtenModelUrl = ResourceUtils.getLocalFile(tempLogicModelFile.getCanonicalPath());
89         final AxPolicyModel writtenModel = modelReader.read(writtenModelUrl.openStream());
90
91         final URL compareModelUrl =
92                 ResourceUtils.getLocalFile("src/test/resources/compare/LogicBlockModel_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         assertEquals(writtenModel, compareModel);
100     }
101
102     /**
103      * Test avro schema.
104      *
105      * @throws IOException Signals that an I/O exception has occurred.
106      * @throws ApexModelException if there is an Apex error
107      */
108     @Test
109     public void testAvroSchema() throws IOException, ApexModelException {
110         new ApexCommandLineEditorMain(avroSchemaArgs);
111
112         // Read the file from disk
113         final ApexModelReader<AxPolicyModel> modelReader = new ApexModelReader<>(AxPolicyModel.class);
114         modelReader.setValidate(false);
115
116         final URL writtenModelUrl = ResourceUtils.getLocalFile(tempAvroModelFile.getCanonicalPath());
117         final AxPolicyModel writtenModel = modelReader.read(writtenModelUrl.openStream());
118
119         final URL compareModelUrl =
120                 ResourceUtils.getLocalFile("src/test/resources/compare/AvroSchemaModel_Compare.json");
121         final AxPolicyModel compareModel = modelReader.read(compareModelUrl.openStream());
122
123         // Ignore key info UUIDs
124         writtenModel.getKeyInformation().getKeyInfoMap().clear();
125         compareModel.getKeyInformation().getKeyInfoMap().clear();
126
127         assertEquals(writtenModel, compareModel);
128     }
129 }