aboutsummaryrefslogtreecommitdiffstats
path: root/tools/model-generator/src/test/java/org/onap/policy/apex/tools/model/generator/model2event/Model2EventTest.java
blob: c9180607ef4b2ea090b714e99334e7e0ff58b16f (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
154
155
156
157
158
159
160
161
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2018 Ericsson. All rights reserved.
 *  Modifications Copyright (C) 2020 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.model2event;

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 Model2Event utility.
 */
public class Model2EventTest {
    @Test
    public void testModel2Event() {
        final String[] EventArgs =
            { "-h" };

        assertThatCode(() -> Model2EventMain.main(EventArgs)).doesNotThrowAnyException();

    }

    @Test
    public void testModel2EventNoOptions() {
        final String[] EventArgs = new String[]
            {};

        final String outputString = runModel2Event(EventArgs);

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

    @Test
    public void testModel2EventBadOptions() {
        assertThat(runModel2Event(new String[] {"-zabbu"})).contains("usage: gen-model2event");
    }

    @Test
    public void testModel2EventHelp() {
        assertThat(runModel2Event(new String[] {"-h"})).contains("usage: gen-model2event");
    }

    @Test
    public void testModel2EventVersion() {
        assertThat(runModel2Event(new String[] {"-v"})).contains("gen-model2event").doesNotContain("usage:");
    }

    @Test
    public void testModel2EventNoType() {
        final String[] EventArgs =
            { "-m", "src/test/resources/models/AvroModel.json" };

        final String outputString = runModel2Event(EventArgs);

        assertTrue(outputString.contains("gen-model2event: no event type given, cannot proceed (try -h for help)"));
    }

    @Test
    public void testModel2EventBadType() {
        final String[] EventArgs =
            { "-m", "src/test/resources/models/AvroModel.json", "-t", "Zooby" };

        final String outputString = runModel2Event(EventArgs);

        assertTrue(outputString.contains("gen-model2event: unknown type <Zooby>, cannot proceed (try -h for help)"));
    }

    @Test
    public void testModel2EventAadm() throws IOException {
        testModel2EventModel("AADMPolicyModel");
    }

    @Test
    public void testModel2EventAnomaly() throws IOException {
        testModel2EventModel("AnomalyDetectionPolicyModel");
    }

    @Test
    public void testModel2EventAutoLearn() throws IOException {
        testModel2EventModel("AutoLearnPolicyModel");
    }

    @Test
    public void testModel2EventMfp() throws IOException {
        testModel2EventModel("MyFirstPolicyModel");
    }

    @Test
    public void testModel2EventSample() throws IOException {
        testModel2EventModel("SamplePolicyModelJAVASCRIPT");
    }

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

        new Model2EventMain(eventArgs, new PrintStream(baosOut, true));

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

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

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

        final String[] eventArgs0 =
            { "-m", "src/test/resources/models/" + modelName + ".json", "-t", "stimuli" };
        final String outputString0 = runModel2Event(eventArgs0);

        assertTrue(outputString0.contains("type: stimuli"));

        final String[] eventArgs1 = {"-m", "src/test/resources/models/" + modelName + ".json", "-t", "response" };
        final String outputString1 = runModel2Event(eventArgs1);

        assertTrue(outputString1.contains("type: response"));

        final String[] eventArgs2 = {"-m", "src/test/resources/models/" + modelName + ".json", "-t", "internal" };
        final String outputString2 = runModel2Event(eventArgs2);

        assertTrue(outputString2.contains("type: internal"));
    }
}