aboutsummaryrefslogtreecommitdiffstats
path: root/examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/TestMfpModelCli.java
blob: 0d6b4737cc7f0fac0f5b075cc39e576a89ba4595 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.apex.examples.myfirstpolicy;

import static org.junit.Assert.assertEquals;

import java.io.File;
import java.io.IOException;

import org.junit.BeforeClass;
import org.junit.Test;
import org.onap.policy.apex.auth.clieditor.ApexCLIEditorMain;
import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
import org.onap.policy.apex.model.basicmodel.handling.ApexModelReader;
import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
import org.onap.policy.apex.model.utilities.TextFileUtils;

/**
 * Test MyFirstPolicyModel CLI.
 */
public class TestMfpModelCli {
    private static AxPolicyModel testApexModel1;
    private static AxPolicyModel testApexModel2;

    /**
     * Setup the test.
     *
     * @throws Exception if there is an error
     */
    @BeforeClass
    public static void setup() throws Exception {
        testApexModel1 = new TestMfpModelCreator.TestMfp1ModelCreator().getModel();
        testApexModel2 = new TestMfpModelCreator.TestMfp2ModelCreator().getModel();
    }

    /**
     * Test CLI policy.
     *
     * @throws IOException Signals that an I/O exception has occurred.
     * @throws ApexModelException ifd there is an Apex Error
     */
    @Test
    public void testCliPolicy() throws IOException, ApexModelException {

        final File tempLogFile1 = File.createTempFile("TestMyFirstPolicy1CLI", ".log");
        final File tempModelFile1 = File.createTempFile("TestMyFirstPolicy1CLI", ".json");
        final File tempLogFile2 = File.createTempFile("TestMyFirstPolicy2CLI", ".log");
        final File tempModelFile2 = File.createTempFile("TestMyFirstPolicy2CLI", ".json");
        final String[] testApexModel1CliArgs =
            { "-c", "src/main/resources/examples/models/MyFirstPolicy/1/MyFirstPolicyModel_0.0.1.apex", "-l",
                        tempLogFile1.getAbsolutePath(), "-o", tempModelFile1.getAbsolutePath() };
        final String[] testApexModel2CliArgs =
            { "-c", "src/main/resources/examples/models/MyFirstPolicy/2/MyFirstPolicyModel_0.0.1.apex", "-l",
                        tempLogFile2.getAbsolutePath(), "-o", tempModelFile2.getAbsolutePath() };

        new ApexCLIEditorMain(testApexModel1CliArgs);
        new ApexCLIEditorMain(testApexModel2CliArgs);

        final ApexModelReader<AxPolicyModel> reader = new ApexModelReader<>(AxPolicyModel.class);
        AxPolicyModel generatedmodel = reader.read(TextFileUtils.getTextFileAsString(tempModelFile1.getAbsolutePath()));

        assertEquals("Model generated from the CLI (" + testApexModel1CliArgs[1] + ") into file "
                + tempModelFile1.getAbsolutePath() + " is not the same as the test Model for "
                + testApexModel1.getKey(), testApexModel1, generatedmodel);

        generatedmodel = reader.read(TextFileUtils.getTextFileAsString(tempModelFile2.getAbsolutePath()));
        assertEquals("Model generated from the CLI (" + testApexModel2CliArgs[1] + ") into file "
                + tempModelFile2.getAbsolutePath() + " is not the same as the test Model for "
                + testApexModel2.getKey(), testApexModel2, generatedmodel);

        tempLogFile1.delete();
        tempModelFile1.delete();

        tempLogFile2.delete();
        tempModelFile2.delete();

    }
}