Simon Hunt | 21281fd | 2017-03-30 22:28:28 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Simon Hunt | 21281fd | 2017-03-30 22:28:28 -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.ui.topo; |
| 18 | |
Szymon Furman | d2a9dd3 | 2017-06-20 15:35:27 +0200 | [diff] [blame] | 19 | import org.junit.AfterClass; |
| 20 | import org.junit.BeforeClass; |
Simon Hunt | 21281fd | 2017-03-30 22:28:28 -0700 | [diff] [blame] | 21 | import org.junit.Test; |
| 22 | import org.onosproject.net.ConnectPoint; |
| 23 | import org.onosproject.net.DefaultLink; |
| 24 | import org.onosproject.net.Link; |
| 25 | import org.onosproject.net.LinkKey; |
| 26 | import org.onosproject.net.provider.ProviderId; |
Simon Hunt | f01826c | 2017-05-09 17:28:13 -0700 | [diff] [blame] | 27 | import org.onosproject.ui.AbstractUiTest; |
Simon Hunt | 21281fd | 2017-03-30 22:28:28 -0700 | [diff] [blame] | 28 | |
Szymon Furman | d2a9dd3 | 2017-06-20 15:35:27 +0200 | [diff] [blame] | 29 | import java.util.Locale; |
| 30 | |
Simon Hunt | 21281fd | 2017-03-30 22:28:28 -0700 | [diff] [blame] | 31 | import static org.junit.Assert.assertEquals; |
| 32 | import static org.junit.Assert.assertFalse; |
| 33 | import static org.junit.Assert.assertTrue; |
| 34 | import static org.onosproject.net.ConnectPoint.deviceConnectPoint; |
| 35 | |
| 36 | /** |
| 37 | * Unit tests for {@link TopoUtils}. |
| 38 | */ |
Simon Hunt | f01826c | 2017-05-09 17:28:13 -0700 | [diff] [blame] | 39 | public class TopoUtilsTest extends AbstractUiTest { |
Simon Hunt | 21281fd | 2017-03-30 22:28:28 -0700 | [diff] [blame] | 40 | private static final String AM_WL = "wrong label"; |
| 41 | private static final String AM_WM = "wrong magnitude"; |
| 42 | private static final String AM_CL = "clipped?"; |
| 43 | private static final String AM_NCL = "not clipped?"; |
| 44 | |
| 45 | private static final ConnectPoint CP_FU = deviceConnectPoint("fu:001/3"); |
| 46 | private static final ConnectPoint CP_BAH = deviceConnectPoint("bah:002/5"); |
| 47 | |
Szymon Furman | d2a9dd3 | 2017-06-20 15:35:27 +0200 | [diff] [blame] | 48 | private static Locale systemLocale; |
| 49 | |
| 50 | @BeforeClass |
| 51 | public static void classSetup() { |
| 52 | systemLocale = Locale.getDefault(); |
| 53 | Locale.setDefault(Locale.US); |
| 54 | } |
| 55 | |
| 56 | @AfterClass |
| 57 | public static void classTeardown() { |
| 58 | Locale.setDefault(systemLocale); |
| 59 | } |
| 60 | |
Simon Hunt | 21281fd | 2017-03-30 22:28:28 -0700 | [diff] [blame] | 61 | private static final Link LINK_FU_BAH = DefaultLink.builder() |
| 62 | .src(CP_FU) |
| 63 | .dst(CP_BAH) |
| 64 | .type(Link.Type.DIRECT) |
| 65 | .providerId(ProviderId.NONE) |
| 66 | .build(); |
| 67 | |
| 68 | private static final Link LINK_BAH_FU = DefaultLink.builder() |
| 69 | .src(CP_BAH) |
| 70 | .dst(CP_FU) |
| 71 | .type(Link.Type.DIRECT) |
| 72 | .providerId(ProviderId.NONE) |
| 73 | .build(); |
| 74 | |
Simon Hunt | f01826c | 2017-05-09 17:28:13 -0700 | [diff] [blame] | 75 | private static final Link LINK_7_TO_3 = DefaultLink.builder() |
| 76 | .src(deviceConnectPoint("of:0000000000000007/2")) |
| 77 | .dst(deviceConnectPoint("of:0000000000000003/2")) |
| 78 | .type(Link.Type.DIRECT) |
| 79 | .providerId(ProviderId.NONE) |
| 80 | .build(); |
| 81 | |
| 82 | private static final Link LINK_3_TO_7 = DefaultLink.builder() |
| 83 | .src(deviceConnectPoint("of:0000000000000003/2")) |
| 84 | .dst(deviceConnectPoint("of:0000000000000007/2")) |
| 85 | .type(Link.Type.DIRECT) |
| 86 | .providerId(ProviderId.NONE) |
| 87 | .build(); |
| 88 | |
| 89 | |
Simon Hunt | 21281fd | 2017-03-30 22:28:28 -0700 | [diff] [blame] | 90 | private TopoUtils.ValueLabel vl; |
| 91 | |
| 92 | @Test |
| 93 | public void linkStringFuBah() { |
| 94 | String compact = TopoUtils.compactLinkString(LINK_FU_BAH); |
| 95 | assertEquals("wrong link id", "fu:001/3-bah:002/5", compact); |
| 96 | } |
| 97 | |
| 98 | @Test |
| 99 | public void linkStringBahFu() { |
| 100 | String compact = TopoUtils.compactLinkString(LINK_BAH_FU); |
| 101 | assertEquals("wrong link id", "bah:002/5-fu:001/3", compact); |
| 102 | } |
| 103 | |
| 104 | @Test |
| 105 | public void canonLinkKey() { |
| 106 | LinkKey fb = TopoUtils.canonicalLinkKey(LINK_FU_BAH); |
| 107 | LinkKey bf = TopoUtils.canonicalLinkKey(LINK_BAH_FU); |
| 108 | assertEquals("not canonical", fb, bf); |
| 109 | } |
| 110 | |
| 111 | @Test |
Simon Hunt | f01826c | 2017-05-09 17:28:13 -0700 | [diff] [blame] | 112 | public void canon723() { |
| 113 | LinkKey lk1 = TopoUtils.canonicalLinkKey(LINK_7_TO_3); |
| 114 | print(lk1); |
| 115 | LinkKey lk2 = TopoUtils.canonicalLinkKey(LINK_3_TO_7); |
| 116 | print(lk2); |
| 117 | assertEquals("not canonical 3/7", lk1, lk2); |
| 118 | } |
| 119 | |
| 120 | @Test |
Simon Hunt | 21281fd | 2017-03-30 22:28:28 -0700 | [diff] [blame] | 121 | public void formatSmallBytes() { |
| 122 | vl = TopoUtils.formatBytes(1_000L); |
| 123 | assertEquals(AM_WM, TopoUtils.Magnitude.ONE, vl.magnitude()); |
| 124 | assertEquals(AM_WL, "1,000 B", vl.toString()); |
| 125 | } |
| 126 | |
| 127 | @Test |
| 128 | public void formatKiloBytes() { |
| 129 | vl = TopoUtils.formatBytes(2_000L); |
| 130 | assertEquals(AM_WM, TopoUtils.Magnitude.KILO, vl.magnitude()); |
| 131 | assertEquals(AM_WL, "1.95 KB", vl.toString()); |
| 132 | } |
| 133 | |
| 134 | @Test |
| 135 | public void formatMegaBytes() { |
| 136 | vl = TopoUtils.formatBytes(3_000_000L); |
| 137 | assertEquals(AM_WM, TopoUtils.Magnitude.MEGA, vl.magnitude()); |
| 138 | assertEquals(AM_WL, "2.86 MB", vl.toString()); |
| 139 | } |
| 140 | |
| 141 | @Test |
| 142 | public void formatGigaBytes() { |
| 143 | vl = TopoUtils.formatBytes(4_000_000_000L); |
| 144 | assertEquals(AM_WM, TopoUtils.Magnitude.GIGA, vl.magnitude()); |
| 145 | assertEquals(AM_WL, "3.73 GB", vl.toString()); |
| 146 | } |
| 147 | |
| 148 | @Test |
| 149 | public void formatTeraBytes() { |
| 150 | vl = TopoUtils.formatBytes(5_000_000_000_000L); |
| 151 | assertEquals(AM_WM, TopoUtils.Magnitude.GIGA, vl.magnitude()); |
| 152 | assertEquals(AM_WL, "4,656.61 GB", vl.toString()); |
| 153 | } |
| 154 | |
| 155 | @Test |
| 156 | public void formatPacketRateSmall() { |
| 157 | vl = TopoUtils.formatPacketRate(37); |
| 158 | assertEquals(AM_WL, "37 pps", vl.toString()); |
| 159 | } |
| 160 | |
| 161 | @Test |
| 162 | public void formatPacketRateKilo() { |
| 163 | vl = TopoUtils.formatPacketRate(1024); |
| 164 | assertEquals(AM_WL, "1 Kpps", vl.toString()); |
| 165 | } |
| 166 | |
| 167 | @Test |
| 168 | public void formatPacketRateKilo2() { |
| 169 | vl = TopoUtils.formatPacketRate(1034); |
| 170 | assertEquals(AM_WL, "1.01 Kpps", vl.toString()); |
| 171 | } |
| 172 | |
| 173 | @Test |
| 174 | public void formatPacketRateMega() { |
| 175 | vl = TopoUtils.formatPacketRate(9_000_000); |
| 176 | assertEquals(AM_WL, "8.58 Mpps", vl.toString()); |
| 177 | } |
| 178 | |
| 179 | // remember for the following method calls, the input is in bytes! |
| 180 | @Test |
| 181 | public void formatClippedBitsSmall() { |
| 182 | vl = TopoUtils.formatClippedBitRate(8); |
| 183 | assertEquals(AM_WL, "64 bps", vl.toString()); |
| 184 | assertFalse(AM_CL, vl.clipped()); |
| 185 | } |
| 186 | |
| 187 | @Test |
| 188 | public void formatClippedBitsKilo() { |
| 189 | vl = TopoUtils.formatClippedBitRate(2_004); |
| 190 | assertEquals(AM_WL, "15.66 Kbps", vl.toString()); |
| 191 | assertFalse(AM_CL, vl.clipped()); |
| 192 | } |
| 193 | |
| 194 | @Test |
| 195 | public void formatClippedBitsMega() { |
| 196 | vl = TopoUtils.formatClippedBitRate(3_123_123); |
| 197 | assertEquals(AM_WL, "23.83 Mbps", vl.toString()); |
| 198 | assertFalse(AM_CL, vl.clipped()); |
| 199 | } |
| 200 | |
| 201 | @Test |
| 202 | public void formatClippedBitsGiga() { |
| 203 | vl = TopoUtils.formatClippedBitRate(500_000_000); |
| 204 | assertEquals(AM_WL, "3.73 Gbps", vl.toString()); |
| 205 | assertFalse(AM_CL, vl.clipped()); |
| 206 | } |
| 207 | |
| 208 | @Test |
| 209 | public void formatClippedBitsGigaExceedThreshold() { |
Andrea Campanella | d7c9336 | 2017-09-13 15:53:04 +0200 | [diff] [blame] | 210 | vl = TopoUtils.formatClippedBitRate(15_000_000_000L); |
| 211 | // approx. 111.75 Gbps |
| 212 | assertEquals(AM_WL, "100 Gbps", vl.toString()); |
Simon Hunt | 21281fd | 2017-03-30 22:28:28 -0700 | [diff] [blame] | 213 | assertTrue(AM_NCL, vl.clipped()); |
| 214 | } |
| 215 | |
| 216 | @Test |
| 217 | public void formatNoFlows() { |
| 218 | String f = TopoUtils.formatFlows(0); |
| 219 | assertEquals(AM_WL, "", f); |
| 220 | } |
| 221 | |
| 222 | @Test |
| 223 | public void formatNegativeFlows() { |
| 224 | String f = TopoUtils.formatFlows(-3); |
| 225 | assertEquals(AM_WL, "", f); |
| 226 | } |
| 227 | |
| 228 | @Test |
| 229 | public void formatOneFlow() { |
| 230 | String f = TopoUtils.formatFlows(1); |
| 231 | assertEquals(AM_WL, "1 flow", f); |
| 232 | } |
| 233 | |
| 234 | @Test |
| 235 | public void formatManyFlows() { |
| 236 | String f = TopoUtils.formatFlows(42); |
| 237 | assertEquals(AM_WL, "42 flows", f); |
| 238 | } |
| 239 | } |