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; |
| 18 | |
| 19 | import org.junit.Before; |
| 20 | import org.junit.Test; |
| 21 | import org.onosproject.net.EncapsulationType; |
| 22 | import org.onosproject.vpls.VplsTest; |
| 23 | import org.onosproject.vpls.api.VplsData; |
| 24 | |
| 25 | import java.io.ByteArrayOutputStream; |
| 26 | import java.io.PrintStream; |
| 27 | import java.util.Collection; |
| 28 | |
| 29 | import static org.junit.Assert.*; |
| 30 | |
| 31 | /** |
| 32 | * Test for {@link VplsCommand}. |
| 33 | */ |
| 34 | public class VplsCommandTest extends VplsTest { |
| 35 | private static final String NEW_LINE = "\n"; |
| 36 | private static final String SHOW_ALL_RES = "----------------\n" + |
| 37 | "VPLS name: \u001B[1mvpls2\u001B[0m\n" + |
| 38 | "Associated interfaces: [v200h2, v200h1]\n" + |
| 39 | "Encapsulation: NONE\n" + |
| 40 | "State: ADDED\n" + |
| 41 | "----------------\n" + |
| 42 | "VPLS name: \u001B[1mvpls1\u001B[0m\n" + |
| 43 | "Associated interfaces: [v100h1, v100h2]\n" + |
| 44 | "Encapsulation: NONE\n" + |
| 45 | "State: ADDED\n" + |
| 46 | "----------------\n"; |
| 47 | private static final String SHOW_ONE_RES = "VPLS name: \u001B[1mvpls1\u001B[0m\n" + |
| 48 | "Associated interfaces: [v100h1, v100h2]\n" + |
| 49 | "Encapsulation: NONE\n" + |
| 50 | "State: ADDED\n"; |
| 51 | private static final String IFACE_ALREADY_USED = |
| 52 | "\u001B[31mInterface \u001B[1mv200h1\u001B[0m\u001B[31m already associated " + |
| 53 | "to VPLS \u001B[1mvpls2\u001B[0m\u001B[31m\u001B[0m\n"; |
| 54 | private static final String LIST_OUTPUT = VPLS1 + NEW_LINE + VPLS2 + NEW_LINE; |
| 55 | VplsCommand vplsCommand; |
| 56 | |
| 57 | @Before |
| 58 | public void setup() { |
| 59 | vplsCommand = new VplsCommand(); |
| 60 | vplsCommand.vpls = new TestVpls(); |
| 61 | vplsCommand.interfaceService = new TestInterfaceService(); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Creates a new VPLS. |
| 66 | */ |
| 67 | @Test |
| 68 | public void testCreate() { |
| 69 | vplsCommand.command = VplsCommandEnum.CREATE.toString(); |
| 70 | vplsCommand.vplsName = VPLS1; |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 71 | vplsCommand.doExecute(); |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 72 | Collection<VplsData> vplss = vplsCommand.vpls.getAllVpls(); |
| 73 | assertEquals(1, vplss.size()); |
| 74 | VplsData result = vplss.iterator().next(); |
| 75 | VplsData expected = VplsData.of(VPLS1, EncapsulationType.NONE); |
| 76 | assertEquals(expected, result); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Adds new network interface to a VPLS. |
| 81 | */ |
| 82 | @Test |
| 83 | public void testAddIf() { |
| 84 | vplsCommand.create(VPLS1); |
| 85 | |
| 86 | vplsCommand.command = VplsCommandEnum.ADD_IFACE.toString(); |
| 87 | vplsCommand.vplsName = VPLS1; |
| 88 | vplsCommand.optArg = V100H1.name(); |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 89 | vplsCommand.doExecute(); |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 90 | |
| 91 | vplsCommand.command = VplsCommandEnum.ADD_IFACE.toString(); |
| 92 | vplsCommand.vplsName = VPLS1; |
| 93 | vplsCommand.optArg = V200H1.name(); |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 94 | vplsCommand.doExecute(); |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 95 | |
| 96 | Collection<VplsData> vplss = vplsCommand.vpls.getAllVpls(); |
| 97 | assertEquals(1, vplss.size()); |
| 98 | VplsData result = vplss.iterator().next(); |
| 99 | assertEquals(2, result.interfaces().size()); |
| 100 | assertTrue(result.interfaces().contains(V100H1)); |
| 101 | assertTrue(result.interfaces().contains(V200H1)); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Removes network interface from a VPLS. |
| 106 | */ |
| 107 | @Test |
| 108 | public void testRemIf() { |
| 109 | vplsCommand.create(VPLS1); |
| 110 | vplsCommand.addIface(VPLS1, V100H1.name()); |
| 111 | vplsCommand.addIface(VPLS1, V200H1.name()); |
| 112 | |
| 113 | vplsCommand.command = VplsCommandEnum.REMOVE_IFACE.toString(); |
| 114 | vplsCommand.vplsName = VPLS1; |
| 115 | vplsCommand.optArg = V200H1.name(); |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 116 | vplsCommand.doExecute(); |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 117 | |
| 118 | Collection<VplsData> vplss = vplsCommand.vpls.getAllVpls(); |
| 119 | assertEquals(1, vplss.size()); |
| 120 | VplsData result = vplss.iterator().next(); |
| 121 | assertEquals(1, result.interfaces().size()); |
| 122 | assertTrue(result.interfaces().contains(V100H1)); |
| 123 | assertFalse(result.interfaces().contains(V200H1)); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Lists all VPLS names. |
| 128 | */ |
| 129 | @Test |
| 130 | public void testList() { |
| 131 | ((TestVpls) vplsCommand.vpls).initSampleData(); |
| 132 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 133 | PrintStream ps = new PrintStream(baos); |
| 134 | System.setOut(ps); |
| 135 | vplsCommand.command = VplsCommandEnum.LIST.toString(); |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 136 | vplsCommand.doExecute(); |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 137 | String result = baos.toString(); |
| 138 | |
| 139 | assertEquals(LIST_OUTPUT, result); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Sets encapsulation to a VPLS. |
| 144 | */ |
| 145 | @Test |
| 146 | public void testSetEncap() { |
| 147 | ((TestVpls) vplsCommand.vpls).initSampleData(); |
| 148 | |
| 149 | // Sets to NONE |
| 150 | vplsCommand.command = VplsCommandEnum.SET_ENCAP.toString(); |
| 151 | vplsCommand.vplsName = VPLS1; |
| 152 | vplsCommand.optArg = EncapsulationType.NONE.name(); |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 153 | vplsCommand.doExecute(); |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 154 | VplsData result = vplsCommand.vpls.getVpls(VPLS1); |
| 155 | assertEquals(result.encapsulationType(), EncapsulationType.NONE); |
| 156 | |
| 157 | // Sets to VLAN |
| 158 | vplsCommand.command = VplsCommandEnum.SET_ENCAP.toString(); |
| 159 | vplsCommand.vplsName = VPLS1; |
| 160 | vplsCommand.optArg = EncapsulationType.VLAN.name(); |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 161 | vplsCommand.doExecute(); |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 162 | result = vplsCommand.vpls.getVpls(VPLS1); |
| 163 | assertEquals(result.encapsulationType(), EncapsulationType.VLAN); |
| 164 | |
| 165 | // Sets to MPLS |
| 166 | vplsCommand.command = VplsCommandEnum.SET_ENCAP.toString(); |
| 167 | vplsCommand.vplsName = VPLS1; |
| 168 | vplsCommand.optArg = EncapsulationType.MPLS.name(); |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 169 | vplsCommand.doExecute(); |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 170 | result = vplsCommand.vpls.getVpls(VPLS1); |
| 171 | assertEquals(result.encapsulationType(), EncapsulationType.MPLS); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Deletes a VPLS. |
| 176 | */ |
| 177 | @Test |
| 178 | public void testDelete() { |
| 179 | ((TestVpls) vplsCommand.vpls).initSampleData(); |
| 180 | vplsCommand.command = VplsCommandEnum.DELETE.toString(); |
| 181 | vplsCommand.vplsName = VPLS1; |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 182 | vplsCommand.doExecute(); |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 183 | Collection<VplsData> vplss = vplsCommand.vpls.getAllVpls(); |
| 184 | assertEquals(1, vplss.size()); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Shows all VPLS information. |
| 189 | */ |
| 190 | @Test |
| 191 | public void testShowAll() { |
| 192 | ((TestVpls) vplsCommand.vpls).initSampleData(); |
| 193 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 194 | PrintStream ps = new PrintStream(baos); |
| 195 | System.setOut(ps); |
| 196 | vplsCommand.command = VplsCommandEnum.SHOW.toString(); |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 197 | vplsCommand.doExecute(); |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 198 | String result = baos.toString(); |
| 199 | assertEquals(SHOW_ALL_RES, result); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Shows a VPLS information. |
| 204 | */ |
| 205 | @Test |
| 206 | public void testShowOne() { |
| 207 | ((TestVpls) vplsCommand.vpls).initSampleData(); |
| 208 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 209 | PrintStream ps = new PrintStream(baos); |
| 210 | System.setOut(ps); |
| 211 | vplsCommand.command = VplsCommandEnum.SHOW.toString(); |
| 212 | vplsCommand.vplsName = VPLS1; |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 213 | vplsCommand.doExecute(); |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 214 | String result = baos.toString(); |
| 215 | assertEquals(SHOW_ONE_RES, result); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Adds a network interface which already related to another VPLS to a VPLS. |
| 220 | */ |
| 221 | @Test |
| 222 | public void testIfaceAssociated() { |
| 223 | ((TestVpls) vplsCommand.vpls).initSampleData(); |
| 224 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 225 | PrintStream ps = new PrintStream(baos); |
| 226 | System.setOut(ps); |
| 227 | vplsCommand.command = VplsCommandEnum.ADD_IFACE.toString(); |
| 228 | vplsCommand.vplsName = VPLS1; |
| 229 | vplsCommand.optArg = V200H1.name(); |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 230 | vplsCommand.doExecute(); |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 231 | |
| 232 | String result = baos.toString(); |
| 233 | assertEquals(IFACE_ALREADY_USED, result); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Removes all VPLS. |
| 238 | */ |
| 239 | @Test |
| 240 | public void testClean() { |
| 241 | ((TestVpls) vplsCommand.vpls).initSampleData(); |
| 242 | vplsCommand.command = VplsCommandEnum.CLEAN.toString(); |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 243 | vplsCommand.doExecute(); |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 244 | Collection<VplsData> vplss = vplsCommand.vpls.getAllVpls(); |
| 245 | assertEquals(0, vplss.size()); |
| 246 | } |
| 247 | } |