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