Optimize version option output
[cli.git] / framework / src / test / java / org / onap / cli / fw / ad / OnapAuthClientCommandBasedTest.java
1 /*
2  * Copyright 2017 Huawei Technologies Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.cli.fw.ad;
18
19 import static org.junit.Assert.fail;
20
21 import org.junit.Test;
22 import org.onap.cli.fw.OnapCommand;
23 import org.onap.cli.fw.OnapCommandRegistrar;
24 import org.onap.cli.fw.cmd.OnapHttpCommand;
25 import org.onap.cli.fw.conf.Constants;
26 import org.onap.cli.fw.conf.OnapCommandConfg;
27 import org.onap.cli.fw.error.OnapCommandException;
28
29 public class OnapAuthClientCommandBasedTest {
30
31     @Test
32     public void internalCommandTest() {
33         try {
34             OnapCommand cmd = OnapCommandRegistrar.getRegistrar().get("sample-test");
35             cmd.getInfo().setService(OnapCommandConfg.getProductName());
36
37             cmd.execute();
38         } catch (OnapCommandException e) {
39             fail("Internal command failed to run");
40             e.printStackTrace();
41         }
42     }
43
44     @Test
45     public void yesCatalogYesAuthTest() throws OnapCommandException {
46         try {
47             OnapHttpCommand cmd = getCommand("sample-test-schema-yes-auth-yes-catalog.yaml");
48             cmd.getParametersMap().get(Constants.DEAFULT_PARAMETER_HOST_URL).setValue("http://localhost:8080");
49             cmd.getParametersMap().get(Constants.DEAFULT_PARAMETER_USERNAME).setValue("test");
50             cmd.getParametersMap().get(Constants.DEAFULT_PARAMETER_PASSWORD).setValue("password");
51
52             cmd.execute();
53         } catch (OnapCommandException e) {
54             fail("External command Yes Auth Yes Catalog failed to run");
55             e.printStackTrace();
56         }
57     }
58
59     @Test
60     public void yesCatalogNoAuthTest() throws OnapCommandException {
61         try {
62             OnapHttpCommand cmd = getCommand("sample-test-schema-no-auth-yes-catalog.yaml");
63             cmd.getParametersMap().get(Constants.DEAFULT_PARAMETER_HOST_URL).setValue("http://localhost:8080");
64
65             cmd.execute();
66         } catch (OnapCommandException e) {
67             fail("External command Yes Auth No Catalog failed to run");
68             e.printStackTrace();
69         }
70     }
71
72     @Test
73     public void noCatalogYesAuthTest() throws OnapCommandException {
74         try {
75             OnapHttpCommand cmd = getCommand("sample-test-schema-yes-auth-no-catalog.yaml");
76             cmd.getParametersMap().get(Constants.DEAFULT_PARAMETER_HOST_URL).setValue("http://localhost:8080");
77             cmd.getParametersMap().get(Constants.DEAFULT_PARAMETER_USERNAME).setValue("test");
78             cmd.getParametersMap().get(Constants.DEAFULT_PARAMETER_PASSWORD).setValue("password");
79
80             cmd.execute();
81         } catch (OnapCommandException e) {
82             fail("External command Yes Auth No Catalog failed to run");
83             e.printStackTrace();
84         }
85     }
86
87     @Test
88     public void noCatalogNoAuthTest() throws OnapCommandException {
89         try {
90             OnapHttpCommand cmd = getCommand("sample-test-schema-no-auth-no-catalog.yaml");
91             cmd.getParametersMap().get(Constants.DEAFULT_PARAMETER_HOST_URL).setValue("http://localhost:8080");
92
93             cmd.execute();
94         } catch (OnapCommandException e) {
95             fail("External command No Auth No Catalog failed to run");
96             e.printStackTrace();
97         }
98     }
99
100     private OnapHttpCommand getCommand(String yaml) throws OnapCommandException {
101         OnapHttpCommand cmd = new OnapHttpCommand() {
102             @Override
103             protected void processRequest() throws OnapCommandException {
104                 if (!this.getService().isModeDirect()) {
105                     String url = this.authClient.getServiceUrl();
106                     assert url.equals(this.getParametersMap().get(Constants.DEAFULT_PARAMETER_HOST_URL).getValue() + "/");
107                 }
108             }
109         };
110
111         cmd.initializeSchema(yaml);
112
113         return cmd;
114     }
115  }