31e75e5791b217e522c1969a8d2326119cfdb3c8
[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.basicmodel.handling.ApexModelStringWriter;
35 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
36 import org.onap.policy.common.utils.resources.ResourceUtils;
37
38 // TODO: Auto-generated Javadoc
39 /**
40  * The Class TestLogicBlock.
41  */
42 public class LogicBlockTest {
43     private String[] logicBlockArgs;
44     private String[] avroSchemaArgs;
45
46     private File tempLogicModelFile;
47     private File tempAvroModelFile;
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         tempLogicModelFile = File.createTempFile("TestLogicPolicyModel", ".json");
57         tempAvroModelFile = File.createTempFile("TestAvroPolicyModel", ".json");
58
59         logicBlockArgs = new String[] {"-c", "src/test/resources/scripts/LogicBlock.apex", "-o",
60                 tempLogicModelFile.getCanonicalPath(), "-if", "true", "-nl"};
61
62         avroSchemaArgs = new String[] {"-c", "src/test/resources/scripts/AvroSchema.apex", "-o",
63                 tempAvroModelFile.getCanonicalPath(), "-nl"};
64     }
65
66     /**
67      * Removes the temp files.
68      */
69     @After
70     public void removeTempFiles() {
71         tempLogicModelFile.delete();
72         tempAvroModelFile.delete();
73     }
74
75     /**
76      * Test logic block.
77      *
78      * @throws IOException Signals that an I/O exception has occurred.
79      * @throws ApexModelException if there is an Apex error
80      */
81     @Test
82     public void testLogicBlock() throws IOException, ApexModelException {
83         new ApexCommandLineEditorMain(logicBlockArgs);
84
85         // Read the file from disk
86         final ApexModelReader<AxPolicyModel> modelReader = new ApexModelReader<>(AxPolicyModel.class);
87         modelReader.setValidate(false);
88
89         final URL writtenModelUrl = ResourceUtils.getLocalFile(tempLogicModelFile.getCanonicalPath());
90         final AxPolicyModel writtenModel = modelReader.read(writtenModelUrl.openStream());
91
92         final URL compareModelUrl =
93                 ResourceUtils.getLocalFile("src/test/resources/compare/LogicBlockModel_Compare.json");
94         final AxPolicyModel compareModel = modelReader.read(compareModelUrl.openStream());
95
96         // Ignore key info UUIDs
97         writtenModel.getKeyInformation().getKeyInfoMap().clear();
98         compareModel.getKeyInformation().getKeyInfoMap().clear();
99
100         assertEquals(writtenModel, compareModel);
101     }
102
103     /**
104      * Test avro schema.
105      *
106      * @throws IOException Signals that an I/O exception has occurred.
107      * @throws ApexModelException if there is an Apex error
108      */
109     @Test
110     public void testAvroSchema() throws IOException, ApexModelException {
111         new ApexCommandLineEditorMain(avroSchemaArgs);
112
113         // Read the file from disk
114         final ApexModelReader<AxPolicyModel> modelReader = new ApexModelReader<>(AxPolicyModel.class);
115         modelReader.setValidate(false);
116
117         final URL writtenModelUrl = ResourceUtils.getLocalFile(tempAvroModelFile.getCanonicalPath());
118         final AxPolicyModel writtenModel = modelReader.read(writtenModelUrl.openStream());
119
120         final URL compareModelUrl =
121                 ResourceUtils.getLocalFile("src/test/resources/compare/AvroSchemaModel_Compare.json");
122         final AxPolicyModel compareModel = modelReader.read(compareModelUrl.openStream());
123
124         // Ignore key info UUIDs
125         writtenModel.getKeyInformation().getKeyInfoMap().clear();
126         compareModel.getKeyInformation().getKeyInfoMap().clear();
127
128         assertEquals(writtenModel, compareModel);
129     }
130 }