ce78b767113772a709f061e7d9d2a6b6ae120b9b
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.examples.myfirstpolicy;
22
23 import static org.junit.Assert.assertEquals;
24
25 import java.io.File;
26 import java.io.IOException;
27
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.apex.model.utilities.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         final String[] testApexModel1CliArgs =
68             { "-c", "src/main/resources/examples/models/MyFirstPolicy/1/MyFirstPolicyModel_0.0.1.apex", "-l",
69                         tempLogFile1.getAbsolutePath(), "-o", tempModelFile1.getAbsolutePath() };
70         final String[] testApexModel2CliArgs =
71             { "-c", "src/main/resources/examples/models/MyFirstPolicy/2/MyFirstPolicyModel_0.0.1.apex", "-l",
72                         tempLogFile2.getAbsolutePath(), "-o", tempModelFile2.getAbsolutePath() };
73
74         new ApexCommandLineEditorMain(testApexModel1CliArgs);
75         new ApexCommandLineEditorMain(testApexModel2CliArgs);
76
77         final ApexModelReader<AxPolicyModel> reader = new ApexModelReader<>(AxPolicyModel.class);
78         AxPolicyModel generatedmodel = reader.read(TextFileUtils.getTextFileAsString(tempModelFile1.getAbsolutePath()));
79
80         assertEquals("Model generated from the CLI (" + testApexModel1CliArgs[1] + ") into file "
81                 + tempModelFile1.getAbsolutePath() + " is not the same as the test Model for "
82                 + testApexModel1.getKey(), testApexModel1, generatedmodel);
83
84         generatedmodel = reader.read(TextFileUtils.getTextFileAsString(tempModelFile2.getAbsolutePath()));
85         assertEquals("Model generated from the CLI (" + testApexModel2CliArgs[1] + ") into file "
86                 + tempModelFile2.getAbsolutePath() + " is not the same as the test Model for "
87                 + testApexModel2.getKey(), testApexModel2, generatedmodel);
88
89         tempLogFile1.delete();
90         tempModelFile1.delete();
91
92         tempLogFile2.delete();
93         tempModelFile2.delete();
94
95     }
96 }