b05907919073424bca9eb2608375e2a71260e567
[cli.git] / framework / src / test / java / org / onap / cli / fw / defaultParameter / TestDefaultParameterSection.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.defaultParameter;
18
19 import org.junit.Test;
20 import org.onap.cli.fw.OnapCommand;
21 import org.onap.cli.fw.error.OnapCommandException;
22 import org.onap.cli.fw.error.OnapCommandInvalidDefaultParameter;
23 import org.onap.cli.fw.error.OnapCommandInvalidSchema;
24 import org.onap.cli.fw.input.OnapCommandParameter;
25 import org.onap.cli.fw.utils.OnapCommandUtils;
26
27 import java.util.List;
28 import java.util.stream.Collectors;
29
30 import static org.junit.Assert.assertFalse;
31 import static org.junit.Assert.assertTrue;
32
33
34 public class TestDefaultParameterSection {
35     @Test
36     public void checkOnlyInclude() throws OnapCommandException {
37         OnapCommand cmd = new OnapCommand() {
38             @Override
39             protected void run() throws OnapCommandException {}
40         };
41
42         OnapCommandUtils.loadSchema(cmd, "sample-test-include-param.yaml", true, false);
43         List<String> parameters = cmd.getParameters().stream().map(p -> p.getName()).collect(Collectors.toList());
44         assertTrue(parameters.contains("host-username"));
45         assertTrue(parameters.contains("host-password"));
46         assertTrue(parameters.contains("host-url"));
47     }
48
49     @Test
50     public void checkOnlyExclude() throws OnapCommandException {
51         OnapCommand cmd = new OnapCommand() {
52             @Override
53             protected void run() throws OnapCommandException {}
54         };
55
56         OnapCommandUtils.loadSchema(cmd, "sample-test-exclude-param.yaml", true, false);
57         List<String> parameters = cmd.getParameters().stream().map(p -> p.getName()).collect(Collectors.toList());
58         assertTrue(parameters.contains("host-username"));
59         assertTrue(parameters.contains("host-password"));
60         assertTrue(parameters.contains("host-url"));
61         assertFalse(parameters.contains("long"));
62         assertFalse(parameters.contains("format"));
63         assertTrue(parameters.contains("debug"));
64     }
65
66     @Test
67     public void checkBothIncludeAndExclude() throws OnapCommandException {
68         OnapCommand cmd = new OnapCommand() {
69             @Override
70             protected void run() throws OnapCommandException {}
71         };
72
73         OnapCommandUtils.loadSchema(cmd, "sample-test-include-exclude.yaml", true, false);
74         List<String> parameters = cmd.getParameters().stream().map(p -> p.getName()).collect(Collectors.toList());
75
76         assertTrue(parameters.contains("host-username"));
77         assertTrue(parameters.contains("host-password"));
78         assertTrue(parameters.contains("host-url"));
79     }
80
81     @Test
82     public void checkDefaultSectionAbsent() throws OnapCommandException {
83         OnapCommand cmd = new OnapCommand() {
84             @Override
85             protected void run() throws OnapCommandException {}
86         };
87
88         OnapCommandUtils.loadSchema(cmd, "onap-test-schema.yaml", true, false);
89         List<String> parameters = cmd.getParameters().stream().map(p -> p.getName()).collect(Collectors.toList());
90
91         assertFalse(parameters.contains("host-username"));
92         assertFalse(parameters.contains("host-password"));
93         assertTrue(parameters.contains("host-url"));
94         assertTrue(parameters.contains("debug"));
95         assertTrue(parameters.contains("long"));
96         assertTrue(parameters.contains("format"));
97     }
98
99     @Test(expected = OnapCommandInvalidDefaultParameter.class)
100     public void checkInvalidDefaultArgument() throws OnapCommandException {
101         OnapCommand cmd = new OnapCommand() {
102             @Override
103             protected void run() throws OnapCommandException {}
104         };
105
106         OnapCommandUtils.loadSchema(cmd, "sample-test-invalid-default-parameter.yaml", true, false);
107     }
108
109     @Test(expected = OnapCommandInvalidDefaultParameter.class)
110     public void checkInvalidDefaultArgumentNotExist() throws OnapCommandException {
111         OnapCommand cmd = new OnapCommand() {
112             @Override
113             protected void run() throws OnapCommandException {}
114         };
115
116         OnapCommandUtils.loadSchema(cmd, "sample-test-invalid-default-params-not-exist.yaml", true, false);
117     }
118
119     @Test(expected = OnapCommandInvalidSchema.class)
120     public void checkDefaltwithNoExcludeAndInclude() throws OnapCommandException {
121         OnapCommand cmd = new OnapCommand() {
122             @Override
123             protected void run() throws OnapCommandException {}
124         };
125
126         OnapCommandUtils.loadSchema(cmd, "sample-test-import-def-param-false.yaml", true, false);
127     }
128
129     @Test(expected = OnapCommandInvalidDefaultParameter.class)
130     public void checkInvalidIncludeNoAuth() throws OnapCommandException {
131         OnapCommand cmd = new OnapCommand() {
132             @Override
133             protected void run() throws OnapCommandException {}
134         };
135
136         OnapCommandUtils.loadSchema(cmd, "sample-test-invalid-include-noauth.yaml", true, false);
137     }
138
139     @Test(expected = OnapCommandInvalidDefaultParameter.class)
140     public void checkInvalidExcludeNoAuth() throws OnapCommandException {
141         OnapCommand cmd = new OnapCommand() {
142             @Override
143             protected void run() throws OnapCommandException {}
144         };
145
146         OnapCommandUtils.loadSchema(cmd, "sample-test-invalid-exclude-noauth.yaml", true, false);
147     }
148 }