aboutsummaryrefslogtreecommitdiffstats
path: root/tools/model-generator/src/test/java/org/onap/policy/apex/tools/model/generator/model2cli/Model2CliTest.java
blob: b27c168420659b17a8a9742d64ee545607b7cfcf (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2018 Ericsson. All rights reserved.
 *  Modifications Copyright (C) 2020,2022 Nordix Foundation.
 *  Modifications Copyright (C) 2021 AT&T Intellectual Property. 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.tools.model.generator.model2cli;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import org.junit.Test;

/**
 * Test the Model2Cli utility.
 */
public class Model2CliTest {
    @Test
    public void testModel2Cli() {
        final String[] cliArgs = {"-h"};

        assertThatCode(() -> Model2CliMain.main(cliArgs)).doesNotThrowAnyException();
    }

    @Test
    public void testModel2CliNoOptions() {
        final String[] cliArgs = new String[] {};

        final String outputString = runModel2Cli(cliArgs);

        assertTrue(outputString.contains("gen-model2cli: no '-m' model file given, cannot proceed (try -h for help)"));
    }

    @Test
    public void testModel2CliBadOptions() {
        assertThat(runModel2Cli(new String[] {"-zabbu"})).contains("usage: gen-model2cli");
    }

    @Test
    public void testModel2CliHelp() {
        assertThat(runModel2Cli(new String[] {"-h"})).contains("usage: gen-model2cli");
    }

    @Test
    public void testModel2CliVersion() {
        assertThat(runModel2Cli(new String[] {"-v"})).contains("gen-model2cli").doesNotContain("usage:");
    }

    @Test
    public void testModel2CliOverwrite() throws IOException {
        File tempFile = File.createTempFile("AvroModel", ".apex");
        tempFile.deleteOnExit();

        final String[] cliArgs = {"-m", "src/test/resources/models/AvroModel.json", "-o", tempFile.getCanonicalPath()};

        final String outputString = runModel2Cli(cliArgs);

        assertTrue(outputString.contains("gen-model2cli: error with '-o' option: \"file already exists\""));
    }

    @Test
    public void testModel2CliAadm() throws IOException {
        testModel2CliModel("target/examples/models/AADM", "AADMPolicyModel");
    }

    @Test
    public void testModel2CliAnomaly() throws IOException {
        testModel2CliModel("target/examples/models/Adaptive", "AnomalyDetectionPolicyModel");
    }

    @Test
    public void testModel2CliAutoLearn() throws IOException {
        testModel2CliModel("target/examples/models/Adaptive", "AutoLearnPolicyModel");
    }

    @Test
    public void testModel2CliJms() throws IOException {
        testModel2CliModel("target/examples/models/JMS", "JMSTestModel");
    }

    @Test
    public void testModel2CliMfp() throws IOException {
        testModel2CliModel("target/examples/models/MyFirstPolicy/2", "MyFirstPolicyModel_0.0.1");
    }

    @Test
    public void testModel2CliSample() throws IOException {
        testModel2CliModel("target/examples/models/SampleDomain", "SamplePolicyModelJAVASCRIPT");
    }

    /**
     * Run the application.
     *
     * @param cliArgs the command arguments
     * @return a string containing the command output
     */
    private String runModel2Cli(final String[] cliArgs) {
        final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
        final ByteArrayOutputStream baosErr = new ByteArrayOutputStream();

        new Model2CliMain(cliArgs, new PrintStream(baosOut, true), new PrintStream(baosErr, true));

        String outString = baosOut.toString();
        String errString = baosErr.toString();

        return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString;
    }

    /**
     * Test CLI generation.
     *
     * @param modelName the name of the model file
     */
    private void testModel2CliModel(final String modelPath, final String modelName) throws IOException {
        File tempFile = File.createTempFile(modelName, ".apex");
        tempFile.deleteOnExit();

        // @formatter:off
        final String[] cliArgs = {
            "-m",
            modelPath + "/" + modelName + ".json",
            "-o",
            tempFile.getCanonicalPath(),
            "-ow"
        };
        // @formatter:on
        runModel2Cli(cliArgs);

        assertTrue(tempFile.isFile());
        assertTrue(tempFile.length() > 0);
    }
}