blob: bf865a4b92ae4d20a8ca37aaf9f2577eec7f0632 [file] [log] [blame]
Yi Tsengf4e13e32017-03-30 15:38:39 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Yi Tsengf4e13e32017-03-30 15:38:39 -07003 *
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
17package org.onosproject.vpls.cli.completer;
18
19import com.google.common.collect.ImmutableList;
20import org.apache.felix.service.command.CommandSession;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070021import org.apache.felix.service.command.Job;
22import org.apache.felix.service.command.JobListener;
23//import org.apache.karaf.shell.console.CommandSessionHolder;
24//import org.apache.karaf.shell.console.completer.ArgumentCompleter;
Yi Tsengf4e13e32017-03-30 15:38:39 -070025import org.junit.Before;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070026import org.junit.Ignore;
Yi Tsengf4e13e32017-03-30 15:38:39 -070027import org.junit.Test;
Ray Milkeyfacf2862017-08-03 11:58:29 -070028import org.onosproject.net.intf.Interface;
Yi Tsengf4e13e32017-03-30 15:38:39 -070029import org.onosproject.net.EncapsulationType;
30import org.onosproject.vpls.VplsTest;
31import org.onosproject.vpls.cli.VplsCommandEnum;
32
33import java.io.InputStream;
34import java.io.PrintStream;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070035import java.nio.file.Path;
Yi Tsengf4e13e32017-03-30 15:38:39 -070036import java.util.Arrays;
37import java.util.List;
38import java.util.stream.Collectors;
39
40import static org.junit.Assert.*;
41
Ray Milkey86ad7bb2018-09-27 12:32:28 -070042@Ignore("FIXME implement for new karaf CLI classes")
Yi Tsengf4e13e32017-03-30 15:38:39 -070043public class VplsCommandCompleterTest extends VplsTest {
44 private static final String VPLS_CMD = "vpls";
45 private TestCommandSession commandSession;
46
47 @Before
48 public void setup() {
49 commandSession = new TestCommandSession();
Ray Milkey86ad7bb2018-09-27 12:32:28 -070050 //CommandSessionHolder.setSession(commandSession);
Yi Tsengf4e13e32017-03-30 15:38:39 -070051 }
52
53 /**
54 * Test VPLS command completer.
55 */
56 @Test
57 public void testCommandCompleter() {
58 VplsCommandCompleter commandCompleter = new VplsCommandCompleter();
59 List<String> choices = commandCompleter.choices();
60 List<String> expected = VplsCommandEnum.toStringList();
61 assertEquals(expected, choices);
62 }
63
64 /**
65 * Test VPLS name completer.
66 */
67 @Test
68 public void testNameCompleter() {
69 VplsNameCompleter vplsNameCompleter = new VplsNameCompleter();
70 vplsNameCompleter.vpls = new TestVpls();
71 ((TestVpls) vplsNameCompleter.vpls).initSampleData();
72 List<String> choices = vplsNameCompleter.choices();
73 List<String> expected = ImmutableList.of(VPLS1, VPLS2);
74
75 // Can not ensure the order, use contains all instead of equals
76 assertEquals(choices.size(), expected.size());
77 assertTrue(choices.containsAll(expected));
78 }
79
80 /**
81 * Test VPLS option arguments completer.
82 */
83 @Test
84 public void testOptArgCompleter() {
85 VplsOptArgCompleter completer = new VplsOptArgCompleter();
86 completer.vpls = new TestVpls();
87 ((TestVpls) completer.vpls).initSampleData();
88 completer.interfaceService = new TestInterfaceService();
89
90 // Add interface to VPLS
91 commandSession.updateArguments(VPLS_CMD, VplsCommandEnum.ADD_IFACE.toString(), VPLS1);
92
93 List<String> choices = completer.choices();
94 List<String> expected = ImmutableList.of(V300H1.name(),
95 V300H2.name(),
96 V400H1.name(),
97 VNONEH1.name(),
98 VNONEH2.name(),
99 VNONEH3.name());
100
101 // Can not ensure the order, use contains all instead of equals
102 assertEquals(choices.size(), expected.size());
103 assertTrue(choices.containsAll(expected));
104
105 // Removes interface from VPLS
106 commandSession.updateArguments(VPLS_CMD, VplsCommandEnum.REMOVE_IFACE.toString(), VPLS1);
107 choices = completer.choices();
108 expected = completer.vpls.getVpls(VPLS1).interfaces().stream()
109 .map(Interface::name)
110 .collect(Collectors.toList());
111
112 // Can not ensure the order, use contains all instead of equals
113 assertEquals(choices.size(), expected.size());
114 assertTrue(choices.containsAll(expected));
115
116 // Sets encapsulation
117 commandSession.updateArguments(VPLS_CMD, VplsCommandEnum.SET_ENCAP.toString(), VPLS1);
118 choices = completer.choices();
119 expected = Arrays.stream(EncapsulationType.values())
120 .map(Enum::toString)
121 .collect(Collectors.toList());
122
123 // Can not ensure the order, use contains all instead of equals
124 assertEquals(choices.size(), expected.size());
125 assertTrue(choices.containsAll(expected));
126 }
127
128 /**
129 * Test command session.
130 */
131 class TestCommandSession implements CommandSession {
Ray Milkey86ad7bb2018-09-27 12:32:28 -0700132 //ArgumentCompleter.ArgumentList argumentList;
133
134 @Override
135 public Path currentDir() {
136 return null;
137 }
138
139 @Override
140 public void currentDir(Path path) {
141
142 }
143
144 @Override
145 public ClassLoader classLoader() {
146 return null;
147 }
148
149 @Override
150 public void classLoader(ClassLoader classLoader) {
151
152 }
153
154 @Override
155 public List<Job> jobs() {
156 return null;
157 }
158
159 @Override
160 public Job foregroundJob() {
161 return null;
162 }
163
164 @Override
165 public void setJobListener(JobListener jobListener) {
166
167 }
168
Yi Tsengf4e13e32017-03-30 15:38:39 -0700169 public TestCommandSession() {
170 String[] emptyStringArr = new String[0];
Ray Milkey86ad7bb2018-09-27 12:32:28 -0700171 //argumentList = new ArgumentCompleter.ArgumentList(emptyStringArr,
172 // 0,
173 // 0,
174 // 0);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700175 }
176
177 /**
178 * Updates argument list for the command session.
179 *
180 * @param args new arguments
181 */
182 public void updateArguments(String... args) {
Ray Milkey86ad7bb2018-09-27 12:32:28 -0700183 //argumentList = new ArgumentCompleter.ArgumentList(args,
184 // 0,
185 // 0,
186 // 0);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700187 }
188
189 @Override
190 public Object execute(CharSequence charSequence) throws Exception {
191 return null;
192 }
193
194 @Override
195 public void close() {
196
197 }
198
199 @Override
200 public InputStream getKeyboard() {
201 return null;
202 }
203
204 @Override
205 public PrintStream getConsole() {
206 return null;
207 }
208
209 @Override
210 public Object get(String s) {
Ray Milkey86ad7bb2018-09-27 12:32:28 -0700211 return "";
212 //return argumentList;
Yi Tsengf4e13e32017-03-30 15:38:39 -0700213 }
214
215 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -0700216 public String put(String s, Object o) {
217 return "";
Yi Tsengf4e13e32017-03-30 15:38:39 -0700218 }
219
220 @Override
221 public CharSequence format(Object o, int i) {
222 return null;
223 }
224
225 @Override
226 public Object convert(Class<?> aClass, Object o) {
227 return null;
228 }
229 }
230}