723ad87392ecef57a6666183a64097933f619dab
[policy/apex-pdp.git] / tools / tools-common / src / test / java / org / onap / policy / apex / tools / common / docs / ExampleCliParserTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.tools.common.docs;
22
23 ////
24 ////NOTE: This file contains tags for ASCIIDOC
25 ////DO NOT REMOVE any of those tag lines, e.g.
26 //////tag::**
27 //////end::**
28 ////
29 ////DO NOT auto-refresh imports or organise imports!
30 ////
31
32 import org.apache.commons.cli.CommandLine;
33 import org.apache.commons.cli.HelpFormatter;
34 import org.junit.Test;
35
36 //tag::import[]
37 import org.onap.policy.apex.tools.common.CliOptions;
38 import org.onap.policy.apex.tools.common.CliParser;
39 //end::import[]
40
41 import org.slf4j.ext.XLogger;
42 import org.slf4j.ext.XLoggerFactory;
43
44 /**
45  * Examples for documentation using {@link CliParser}.
46  *
47  * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
48  */
49 public class ExampleCliParserTest {
50
51     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ExampleCliParserTest.class);
52
53     /**
54      * Test example parser.
55      */
56     @Test
57     public void testExampleParser() {
58         final String[] args = new String[] { "-h" };
59
60         // tag::setApp[]
61         final String appName = "test-app";
62         final String appDescription = "a test app for documenting how to use the CLI utilities";
63         // end::setApp[]
64
65         // tag::setCli[]
66         final CliParser cli = new CliParser();
67         cli.addOption(CliOptions.HELP);
68         cli.addOption(CliOptions.VERSION);
69         cli.addOption(CliOptions.MODELFILE);
70         // end::setCli[]
71
72         // tag::parseCli[]
73         final CommandLine cmd = cli.parseCli(args);
74         // end::parseCli[]
75
76         // tag::processCliHelp[]
77         // help is an exit option, print usage and exit
78         if (cmd.hasOption('h') || cmd.hasOption("help")) {
79             final HelpFormatter formatter = new HelpFormatter();
80             LOGGER.info(appName + " v" + cli.getAppVersion() + " - " + appDescription);
81             formatter.printHelp(appName, cli.getOptions());
82             return;
83         }
84         // end::processCliHelp[]
85
86         // tag::processCliVersion[]
87         // version is an exit option, print version and exit
88         if (cmd.hasOption('v') || cmd.hasOption("version")) {
89             LOGGER.info(appName + " " + cli.getAppVersion());
90             return;
91         }
92         // end::processCliVersion[]
93
94         // tag::processCliModel[]
95         String modelFile = cmd.getOptionValue('m');
96         if (modelFile == null) {
97             modelFile = cmd.getOptionValue("model");
98         }
99         if (modelFile == null) {
100             LOGGER.error(appName + ": no model file given, cannot proceed (try -h for help)");
101             return;
102         }
103         // end::processCliModel[]
104
105         // tag::someStartPrint[]
106         LOGGER.info(appName + ": starting");
107         LOGGER.info(" --> model file: " + modelFile);
108         // end::someStartPrint[]
109
110         // tag::yourApp[]
111         // your code for the application here
112         // e.g.
113         // try {
114         // Model2Cli app = new Model2Cli(modelFile, !cmd.hasOption("sv"), appName);
115         // app.runApp();
116         // }
117         // catch(ApexException aex) {
118         // LOGGER.error(appName + ": caught APEX exception with message: " + aex.getMessage());
119         // }
120         // end::yourApp[]
121     }
122 }