Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 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.onosproject.vpls.cli.completer; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import org.apache.felix.service.command.CommandSession; |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame^] | 21 | import org.apache.felix.service.command.Job; |
| 22 | import org.apache.felix.service.command.JobListener; |
| 23 | //import org.apache.karaf.shell.console.CommandSessionHolder; |
| 24 | //import org.apache.karaf.shell.console.completer.ArgumentCompleter; |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 25 | import org.junit.Before; |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame^] | 26 | import org.junit.Ignore; |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 27 | import org.junit.Test; |
Ray Milkey | facf286 | 2017-08-03 11:58:29 -0700 | [diff] [blame] | 28 | import org.onosproject.net.intf.Interface; |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 29 | import org.onosproject.net.EncapsulationType; |
| 30 | import org.onosproject.vpls.VplsTest; |
| 31 | import org.onosproject.vpls.cli.VplsCommandEnum; |
| 32 | |
| 33 | import java.io.InputStream; |
| 34 | import java.io.PrintStream; |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame^] | 35 | import java.nio.file.Path; |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 36 | import java.util.Arrays; |
| 37 | import java.util.List; |
| 38 | import java.util.stream.Collectors; |
| 39 | |
| 40 | import static org.junit.Assert.*; |
| 41 | |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame^] | 42 | @Ignore("FIXME implement for new karaf CLI classes") |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 43 | public 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 Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame^] | 50 | //CommandSessionHolder.setSession(commandSession); |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 51 | } |
| 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 Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame^] | 132 | //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 Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 169 | public TestCommandSession() { |
| 170 | String[] emptyStringArr = new String[0]; |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame^] | 171 | //argumentList = new ArgumentCompleter.ArgumentList(emptyStringArr, |
| 172 | // 0, |
| 173 | // 0, |
| 174 | // 0); |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Updates argument list for the command session. |
| 179 | * |
| 180 | * @param args new arguments |
| 181 | */ |
| 182 | public void updateArguments(String... args) { |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame^] | 183 | //argumentList = new ArgumentCompleter.ArgumentList(args, |
| 184 | // 0, |
| 185 | // 0, |
| 186 | // 0); |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 187 | } |
| 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 Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame^] | 211 | return ""; |
| 212 | //return argumentList; |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | @Override |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame^] | 216 | public String put(String s, Object o) { |
| 217 | return ""; |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 218 | } |
| 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 | } |