24a9f60ce9c56f8c7e4761f5f79679ce75b6eada
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 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.tools.model.generator.model2cli;
23
24 import static org.assertj.core.api.Assertions.assertThatCode;
25 import static org.junit.Assert.assertTrue;
26
27 import java.io.ByteArrayOutputStream;
28 import java.io.File;
29 import java.io.IOException;
30 import java.io.PrintStream;
31 import org.junit.Test;
32
33 /**
34  * Test the Model2Cli utility.
35  */
36 public class Model2CliTest {
37     @Test
38     public void testModel2Cli() {
39         final String[] cliArgs = {"-h"};
40
41         assertThatCode(() -> Model2CliMain.main(cliArgs)).doesNotThrowAnyException();
42     }
43
44     @Test
45     public void testModel2CliNoOptions() {
46         final String[] cliArgs = new String[] {};
47
48         final String outputString = runModel2Cli(cliArgs);
49
50         assertTrue(outputString.contains("gen-model2cli: no '-m' model file given, cannot proceed (try -h for help)"));
51     }
52
53     @Test
54     public void testModel2CliBadOptions() {
55         final String[] cliArgs = {"-zabbu"};
56
57         final String outputString = runModel2Cli(cliArgs);
58
59         assertTrue(outputString.contains("usage: gen-model2cli"));
60     }
61
62     @Test
63     public void testModel2CliHelp() {
64         final String[] cliArgs = {"-h"};
65
66         final String outputString = runModel2Cli(cliArgs);
67
68         assertTrue(outputString.contains("usage: gen-model2cli"));
69     }
70
71     @Test
72     public void testModel2CliVersion() {
73         final String[] cliArgs = {"-v"};
74
75         final String outputString = runModel2Cli(cliArgs);
76
77         assertTrue(outputString.contains("gen-model2cli"));
78     }
79
80     @Test
81     public void testModel2CliOverwrite() throws IOException {
82         File tempFile = File.createTempFile("AvroModel", ".apex");
83         tempFile.deleteOnExit();
84
85         final String[] cliArgs = {"-m", "src/test/resources/models/AvroModel.json", "-o", tempFile.getCanonicalPath()};
86
87         final String outputString = runModel2Cli(cliArgs);
88
89         assertTrue(outputString.contains("gen-model2cli: error with '-o' option: \"file already exists\""));
90     }
91
92     @Test
93     public void testModel2CliAvro() throws IOException {
94         testModel2CliModel("target/examples/models/pcvs/vpnsla", "vpnsla");
95     }
96
97     @Test
98     public void testModel2CliAadm() throws IOException {
99         testModel2CliModel("target/examples/models/AADM", "AADMPolicyModel");
100     }
101
102     @Test
103     public void testModel2CliAnomaly() throws IOException {
104         testModel2CliModel("target/examples/models/Adaptive", "AnomalyDetectionPolicyModel");
105     }
106
107     @Test
108     public void testModel2CliAutoLearn() throws IOException {
109         testModel2CliModel("target/examples/models/Adaptive", "AutoLearnPolicyModel");
110     }
111
112     @Test
113     public void testModel2CliJms() throws IOException {
114         testModel2CliModel("target/examples/models/JMS", "JMSTestModel");
115     }
116
117     @Test
118     public void testModel2CliMfp() throws IOException {
119         testModel2CliModel("target/examples/models/MyFirstPolicy/2", "MyFirstPolicyModel_0.0.1");
120     }
121
122     @Test
123     public void testModel2CliSample() throws IOException {
124         testModel2CliModel("target/examples/models/SampleDomain", "SamplePolicyModelJAVASCRIPT");
125     }
126
127     /**
128      * Run the application.
129      *
130      * @param cliArgs the command arguments
131      * @return a string containing the command output
132      */
133     private String runModel2Cli(final String[] cliArgs) {
134         final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
135         final ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
136
137         new Model2CliMain(cliArgs, new PrintStream(baosOut, true), new PrintStream(baosErr, true));
138
139         String outString = baosOut.toString();
140         String errString = baosErr.toString();
141
142         return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString;
143     }
144
145     /**
146      * Test CLI generation.
147      *
148      * @param modelName the name of the model file
149      */
150     private void testModel2CliModel(final String modelPath, final String modelName) throws IOException {
151         File tempFile = File.createTempFile(modelName, ".apex");
152         tempFile.deleteOnExit();
153
154         // @formatter:off
155         final String[] cliArgs = {
156             "-m",
157             modelPath + "/" + modelName + ".json",
158             "-o",
159             tempFile.getCanonicalPath(),
160             "-ow"
161         };
162         // @formatter:on
163         runModel2Cli(cliArgs);
164
165         assertTrue(tempFile.isFile());
166         assertTrue(tempFile.length() > 0);
167
168     }
169 }