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