Changes for checkstyle 8.32
[policy/apex-pdp.git] / examples / examples-myfirstpolicy / src / test / java / org / onap / policy / apex / examples / myfirstpolicy / MfpModelCliTest.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.examples.myfirstpolicy;
23
24 import static org.junit.Assert.assertEquals;
25
26 import java.io.File;
27 import java.io.IOException;
28 import org.junit.BeforeClass;
29 import org.junit.Test;
30 import org.onap.policy.apex.auth.clieditor.ApexCommandLineEditorMain;
31 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
32 import org.onap.policy.apex.model.basicmodel.handling.ApexModelReader;
33 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
34 import org.onap.policy.common.utils.resources.TextFileUtils;
35
36 /**
37  * Test MyFirstPolicyModel CLI.
38  */
39 public class MfpModelCliTest {
40     private static AxPolicyModel testApexModel1;
41     private static AxPolicyModel testApexModel2;
42
43     /**
44      * Setup the test.
45      *
46      * @throws Exception if there is an error
47      */
48     @BeforeClass
49     public static void setup() throws Exception {
50         testApexModel1 = new TestMfpModelCreator.TestMfp1ModelCreator().getModel();
51         testApexModel2 = new TestMfpModelCreator.TestMfp2ModelCreator().getModel();
52     }
53
54     /**
55      * Test CLI policy.
56      *
57      * @throws IOException Signals that an I/O exception has occurred.
58      * @throws ApexModelException ifd there is an Apex Error
59      */
60     @Test
61     public void testCliPolicy() throws IOException, ApexModelException {
62
63         final File tempLogFile1 = File.createTempFile("TestMyFirstPolicy1CLI", ".log");
64         final File tempModelFile1 = File.createTempFile("TestMyFirstPolicy1CLI", ".json");
65         final File tempLogFile2 = File.createTempFile("TestMyFirstPolicy2CLI", ".log");
66         final File tempModelFile2 = File.createTempFile("TestMyFirstPolicy2CLI", ".json");
67         // @formatter:off
68         final String[] testApexModel1CliArgs = {
69             "-c",
70             "src/main/resources/examples/models/MyFirstPolicy/1/MyFirstPolicyModelMvel_0.0.1.apex",
71             "-l",
72             tempLogFile1.getAbsolutePath(),
73             "-o",
74             tempModelFile1.getAbsolutePath()
75         };
76         final String[] testApexModel2CliArgs = {
77             "-c",
78             "src/main/resources/examples/models/MyFirstPolicy/2/MyFirstPolicyModel_0.0.1.apex",
79             "-l",
80             tempLogFile2.getAbsolutePath(),
81             "-o",
82             tempModelFile2.getAbsolutePath()
83         };
84         // @formatter:on
85
86         new ApexCommandLineEditorMain(testApexModel1CliArgs);
87         new ApexCommandLineEditorMain(testApexModel2CliArgs);
88
89         final ApexModelReader<AxPolicyModel> reader = new ApexModelReader<>(AxPolicyModel.class);
90         AxPolicyModel generatedmodel = reader.read(TextFileUtils.getTextFileAsString(tempModelFile1.getAbsolutePath()));
91
92         assertEquals("Model generated from the CLI (" + testApexModel1CliArgs[1] + ") into file "
93                 + tempModelFile1.getAbsolutePath() + " is not the same as the test Model for "
94                 + testApexModel1.getKey(), testApexModel1, generatedmodel);
95
96         tempLogFile1.delete();
97         tempModelFile1.delete();
98
99         generatedmodel = reader.read(TextFileUtils.getTextFileAsString(tempModelFile2.getAbsolutePath()));
100         assertEquals("Model generated from the CLI (" + testApexModel2CliArgs[1] + ") into file "
101                 + tempModelFile2.getAbsolutePath() + " is not the same as the test Model for "
102                 + testApexModel2.getKey(), testApexModel2, generatedmodel);
103
104         tempLogFile2.delete();
105         tempModelFile2.delete();
106
107     }
108 }