Changes for checkstyle 8.32
[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 //tag::import[]
36 import org.onap.policy.apex.tools.common.CliOptions;
37 import org.onap.policy.apex.tools.common.CliParser;
38 //end::import[]
39 import org.slf4j.ext.XLogger;
40 import org.slf4j.ext.XLoggerFactory;
41
42 /**
43  * Examples for documentation using {@link CliParser}.
44  *
45  * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
46  */
47 public class ExampleCliParserTest {
48
49     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ExampleCliParserTest.class);
50
51     /**
52      * Test example parser.
53      */
54     @Test
55     public void testExampleParser() {
56         final String[] args = new String[] { "-h" };
57
58         // tag::setApp[]
59         final String appName = "test-app";
60         final String appDescription = "a test app for documenting how to use the CLI utilities";
61         // end::setApp[]
62
63         // tag::setCli[]
64         final CliParser cli = new CliParser();
65         cli.addOption(CliOptions.HELP);
66         cli.addOption(CliOptions.VERSION);
67         cli.addOption(CliOptions.MODELFILE);
68         // end::setCli[]
69
70         // tag::parseCli[]
71         final CommandLine cmd = cli.parseCli(args);
72         // end::parseCli[]
73
74         // tag::processCliHelp[]
75         // help is an exit option, print usage and exit
76         if (cmd.hasOption('h') || cmd.hasOption("help")) {
77             final HelpFormatter formatter = new HelpFormatter();
78             LOGGER.info(appName + " v" + cli.getAppVersion() + " - " + appDescription);
79             formatter.printHelp(appName, cli.getOptions());
80             return;
81         }
82         // end::processCliHelp[]
83
84         // tag::processCliVersion[]
85         // version is an exit option, print version and exit
86         if (cmd.hasOption('v') || cmd.hasOption("version")) {
87             LOGGER.info(appName + " " + cli.getAppVersion());
88             return;
89         }
90         // end::processCliVersion[]
91
92         // tag::processCliModel[]
93         String modelFile = cmd.getOptionValue('m');
94         if (modelFile == null) {
95             modelFile = cmd.getOptionValue("model");
96         }
97         if (modelFile == null) {
98             LOGGER.error(appName + ": no model file given, cannot proceed (try -h for help)");
99             return;
100         }
101         // end::processCliModel[]
102
103         // tag::someStartPrint[]
104         LOGGER.info(appName + ": starting");
105         LOGGER.info(" --> model file: " + modelFile);
106         // end::someStartPrint[]
107
108         // tag::yourApp[]
109         // your code for the application here
110         // e.g.
111         // try {
112         // Model2Cli app = new Model2Cli(modelFile, !cmd.hasOption("sv"), appName);
113         // app.runApp();
114         // }
115         // catch(ApexException aex) {
116         // LOGGER.error(appName + ": caught APEX exception with message: " + aex.getMessage());
117         // }
118         // end::yourApp[]
119     }
120 }