Simon Hunt | 21281fd | 2017-03-30 22:28:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Laboratory |
| 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 | |
| 19 | import org.junit.Test; |
| 20 | import org.onosproject.net.ConnectPoint; |
| 21 | import org.onosproject.net.DefaultLink; |
| 22 | import org.onosproject.net.Link; |
| 23 | import org.onosproject.net.LinkKey; |
| 24 | import org.onosproject.net.provider.ProviderId; |
| 25 | |
| 26 | import static org.junit.Assert.assertEquals; |
| 27 | import static org.junit.Assert.assertFalse; |
| 28 | import static org.junit.Assert.assertTrue; |
| 29 | import static org.onosproject.net.ConnectPoint.deviceConnectPoint; |
| 30 | |
| 31 | /** |
| 32 | * Unit tests for {@link TopoUtils}. |
| 33 | */ |
| 34 | public class TopoUtilsTest { |
| 35 | private static final String AM_WL = "wrong label"; |
| 36 | private static final String AM_WM = "wrong magnitude"; |
| 37 | private static final String AM_CL = "clipped?"; |
| 38 | private static final String AM_NCL = "not clipped?"; |
| 39 | |
| 40 | private static final ConnectPoint CP_FU = deviceConnectPoint("fu:001/3"); |
| 41 | private static final ConnectPoint CP_BAH = deviceConnectPoint("bah:002/5"); |
| 42 | |
| 43 | private static final Link LINK_FU_BAH = DefaultLink.builder() |
| 44 | .src(CP_FU) |
| 45 | .dst(CP_BAH) |
| 46 | .type(Link.Type.DIRECT) |
| 47 | .providerId(ProviderId.NONE) |
| 48 | .build(); |
| 49 | |
| 50 | private static final Link LINK_BAH_FU = DefaultLink.builder() |
| 51 | .src(CP_BAH) |
| 52 | .dst(CP_FU) |
| 53 | .type(Link.Type.DIRECT) |
| 54 | .providerId(ProviderId.NONE) |
| 55 | .build(); |
| 56 | |
| 57 | private TopoUtils.ValueLabel vl; |
| 58 | |
| 59 | @Test |
| 60 | public void linkStringFuBah() { |
| 61 | String compact = TopoUtils.compactLinkString(LINK_FU_BAH); |
| 62 | assertEquals("wrong link id", "fu:001/3-bah:002/5", compact); |
| 63 | } |
| 64 | |
| 65 | @Test |
| 66 | public void linkStringBahFu() { |
| 67 | String compact = TopoUtils.compactLinkString(LINK_BAH_FU); |
| 68 | assertEquals("wrong link id", "bah:002/5-fu:001/3", compact); |
| 69 | } |
| 70 | |
| 71 | @Test |
| 72 | public void canonLinkKey() { |
| 73 | LinkKey fb = TopoUtils.canonicalLinkKey(LINK_FU_BAH); |
| 74 | LinkKey bf = TopoUtils.canonicalLinkKey(LINK_BAH_FU); |
| 75 | assertEquals("not canonical", fb, bf); |
| 76 | } |
| 77 | |
| 78 | @Test |
| 79 | public void formatSmallBytes() { |
| 80 | vl = TopoUtils.formatBytes(1_000L); |
| 81 | assertEquals(AM_WM, TopoUtils.Magnitude.ONE, vl.magnitude()); |
| 82 | assertEquals(AM_WL, "1,000 B", vl.toString()); |
| 83 | } |
| 84 | |
| 85 | @Test |
| 86 | public void formatKiloBytes() { |
| 87 | vl = TopoUtils.formatBytes(2_000L); |
| 88 | assertEquals(AM_WM, TopoUtils.Magnitude.KILO, vl.magnitude()); |
| 89 | assertEquals(AM_WL, "1.95 KB", vl.toString()); |
| 90 | } |
| 91 | |
| 92 | @Test |
| 93 | public void formatMegaBytes() { |
| 94 | vl = TopoUtils.formatBytes(3_000_000L); |
| 95 | assertEquals(AM_WM, TopoUtils.Magnitude.MEGA, vl.magnitude()); |
| 96 | assertEquals(AM_WL, "2.86 MB", vl.toString()); |
| 97 | } |
| 98 | |
| 99 | @Test |
| 100 | public void formatGigaBytes() { |
| 101 | vl = TopoUtils.formatBytes(4_000_000_000L); |
| 102 | assertEquals(AM_WM, TopoUtils.Magnitude.GIGA, vl.magnitude()); |
| 103 | assertEquals(AM_WL, "3.73 GB", vl.toString()); |
| 104 | } |
| 105 | |
| 106 | @Test |
| 107 | public void formatTeraBytes() { |
| 108 | vl = TopoUtils.formatBytes(5_000_000_000_000L); |
| 109 | assertEquals(AM_WM, TopoUtils.Magnitude.GIGA, vl.magnitude()); |
| 110 | assertEquals(AM_WL, "4,656.61 GB", vl.toString()); |
| 111 | } |
| 112 | |
| 113 | @Test |
| 114 | public void formatPacketRateSmall() { |
| 115 | vl = TopoUtils.formatPacketRate(37); |
| 116 | assertEquals(AM_WL, "37 pps", vl.toString()); |
| 117 | } |
| 118 | |
| 119 | @Test |
| 120 | public void formatPacketRateKilo() { |
| 121 | vl = TopoUtils.formatPacketRate(1024); |
| 122 | assertEquals(AM_WL, "1 Kpps", vl.toString()); |
| 123 | } |
| 124 | |
| 125 | @Test |
| 126 | public void formatPacketRateKilo2() { |
| 127 | vl = TopoUtils.formatPacketRate(1034); |
| 128 | assertEquals(AM_WL, "1.01 Kpps", vl.toString()); |
| 129 | } |
| 130 | |
| 131 | @Test |
| 132 | public void formatPacketRateMega() { |
| 133 | vl = TopoUtils.formatPacketRate(9_000_000); |
| 134 | assertEquals(AM_WL, "8.58 Mpps", vl.toString()); |
| 135 | } |
| 136 | |
| 137 | // remember for the following method calls, the input is in bytes! |
| 138 | @Test |
| 139 | public void formatClippedBitsSmall() { |
| 140 | vl = TopoUtils.formatClippedBitRate(8); |
| 141 | assertEquals(AM_WL, "64 bps", vl.toString()); |
| 142 | assertFalse(AM_CL, vl.clipped()); |
| 143 | } |
| 144 | |
| 145 | @Test |
| 146 | public void formatClippedBitsKilo() { |
| 147 | vl = TopoUtils.formatClippedBitRate(2_004); |
| 148 | assertEquals(AM_WL, "15.66 Kbps", vl.toString()); |
| 149 | assertFalse(AM_CL, vl.clipped()); |
| 150 | } |
| 151 | |
| 152 | @Test |
| 153 | public void formatClippedBitsMega() { |
| 154 | vl = TopoUtils.formatClippedBitRate(3_123_123); |
| 155 | assertEquals(AM_WL, "23.83 Mbps", vl.toString()); |
| 156 | assertFalse(AM_CL, vl.clipped()); |
| 157 | } |
| 158 | |
| 159 | @Test |
| 160 | public void formatClippedBitsGiga() { |
| 161 | vl = TopoUtils.formatClippedBitRate(500_000_000); |
| 162 | assertEquals(AM_WL, "3.73 Gbps", vl.toString()); |
| 163 | assertFalse(AM_CL, vl.clipped()); |
| 164 | } |
| 165 | |
| 166 | @Test |
| 167 | public void formatClippedBitsGigaExceedThreshold() { |
| 168 | vl = TopoUtils.formatClippedBitRate(5_000_000_000L); |
| 169 | // approx. 37.25 Gbps |
| 170 | assertEquals(AM_WL, "10 Gbps", vl.toString()); |
| 171 | assertTrue(AM_NCL, vl.clipped()); |
| 172 | } |
| 173 | |
| 174 | @Test |
| 175 | public void formatNoFlows() { |
| 176 | String f = TopoUtils.formatFlows(0); |
| 177 | assertEquals(AM_WL, "", f); |
| 178 | } |
| 179 | |
| 180 | @Test |
| 181 | public void formatNegativeFlows() { |
| 182 | String f = TopoUtils.formatFlows(-3); |
| 183 | assertEquals(AM_WL, "", f); |
| 184 | } |
| 185 | |
| 186 | @Test |
| 187 | public void formatOneFlow() { |
| 188 | String f = TopoUtils.formatFlows(1); |
| 189 | assertEquals(AM_WL, "1 flow", f); |
| 190 | } |
| 191 | |
| 192 | @Test |
| 193 | public void formatManyFlows() { |
| 194 | String f = TopoUtils.formatFlows(42); |
| 195 | assertEquals(AM_WL, "42 flows", f); |
| 196 | } |
| 197 | } |