blob: 9d81c309830bdb7d2ad6526bc8c39c1d49f3c1f9 [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;
21import org.apache.karaf.shell.console.CommandSessionHolder;
22import org.apache.karaf.shell.console.completer.ArgumentCompleter;
23import org.junit.Before;
24import org.junit.Test;
25import org.onosproject.incubator.net.intf.Interface;
26import org.onosproject.net.EncapsulationType;
27import org.onosproject.vpls.VplsTest;
28import org.onosproject.vpls.cli.VplsCommandEnum;
29
30import java.io.InputStream;
31import java.io.PrintStream;
32import java.util.Arrays;
33import java.util.List;
34import java.util.stream.Collectors;
35
36import static org.junit.Assert.*;
37
38public class VplsCommandCompleterTest extends VplsTest {
39 private static final String VPLS_CMD = "vpls";
40 private TestCommandSession commandSession;
41
42 @Before
43 public void setup() {
44 commandSession = new TestCommandSession();
45 CommandSessionHolder.setSession(commandSession);
46 }
47
48 /**
49 * Test VPLS command completer.
50 */
51 @Test
52 public void testCommandCompleter() {
53 VplsCommandCompleter commandCompleter = new VplsCommandCompleter();
54 List<String> choices = commandCompleter.choices();
55 List<String> expected = VplsCommandEnum.toStringList();
56 assertEquals(expected, choices);
57 }
58
59 /**
60 * Test VPLS name completer.
61 */
62 @Test
63 public void testNameCompleter() {
64 VplsNameCompleter vplsNameCompleter = new VplsNameCompleter();
65 vplsNameCompleter.vpls = new TestVpls();
66 ((TestVpls) vplsNameCompleter.vpls).initSampleData();
67 List<String> choices = vplsNameCompleter.choices();
68 List<String> expected = ImmutableList.of(VPLS1, VPLS2);
69
70 // Can not ensure the order, use contains all instead of equals
71 assertEquals(choices.size(), expected.size());
72 assertTrue(choices.containsAll(expected));
73 }
74
75 /**
76 * Test VPLS option arguments completer.
77 */
78 @Test
79 public void testOptArgCompleter() {
80 VplsOptArgCompleter completer = new VplsOptArgCompleter();
81 completer.vpls = new TestVpls();
82 ((TestVpls) completer.vpls).initSampleData();
83 completer.interfaceService = new TestInterfaceService();
84
85 // Add interface to VPLS
86 commandSession.updateArguments(VPLS_CMD, VplsCommandEnum.ADD_IFACE.toString(), VPLS1);
87
88 List<String> choices = completer.choices();
89 List<String> expected = ImmutableList.of(V300H1.name(),
90 V300H2.name(),
91 V400H1.name(),
92 VNONEH1.name(),
93 VNONEH2.name(),
94 VNONEH3.name());
95
96 // Can not ensure the order, use contains all instead of equals
97 assertEquals(choices.size(), expected.size());
98 assertTrue(choices.containsAll(expected));
99
100 // Removes interface from VPLS
101 commandSession.updateArguments(VPLS_CMD, VplsCommandEnum.REMOVE_IFACE.toString(), VPLS1);
102 choices = completer.choices();
103 expected = completer.vpls.getVpls(VPLS1).interfaces().stream()
104 .map(Interface::name)
105 .collect(Collectors.toList());
106
107 // Can not ensure the order, use contains all instead of equals
108 assertEquals(choices.size(), expected.size());
109 assertTrue(choices.containsAll(expected));
110
111 // Sets encapsulation
112 commandSession.updateArguments(VPLS_CMD, VplsCommandEnum.SET_ENCAP.toString(), VPLS1);
113 choices = completer.choices();
114 expected = Arrays.stream(EncapsulationType.values())
115 .map(Enum::toString)
116 .collect(Collectors.toList());
117
118 // Can not ensure the order, use contains all instead of equals
119 assertEquals(choices.size(), expected.size());
120 assertTrue(choices.containsAll(expected));
121 }
122
123 /**
124 * Test command session.
125 */
126 class TestCommandSession implements CommandSession {
127 ArgumentCompleter.ArgumentList argumentList;
128 public TestCommandSession() {
129 String[] emptyStringArr = new String[0];
130 argumentList = new ArgumentCompleter.ArgumentList(emptyStringArr,
131 0,
132 0,
133 0);
134 }
135
136 /**
137 * Updates argument list for the command session.
138 *
139 * @param args new arguments
140 */
141 public void updateArguments(String... args) {
142 argumentList = new ArgumentCompleter.ArgumentList(args,
143 0,
144 0,
145 0);
146 }
147
148 @Override
149 public Object execute(CharSequence charSequence) throws Exception {
150 return null;
151 }
152
153 @Override
154 public void close() {
155
156 }
157
158 @Override
159 public InputStream getKeyboard() {
160 return null;
161 }
162
163 @Override
164 public PrintStream getConsole() {
165 return null;
166 }
167
168 @Override
169 public Object get(String s) {
170 return argumentList;
171 }
172
173 @Override
174 public void put(String s, Object o) {
175
176 }
177
178 @Override
179 public CharSequence format(Object o, int i) {
180 return null;
181 }
182
183 @Override
184 public Object convert(Class<?> aClass, Object o) {
185 return null;
186 }
187 }
188}