Simon Hunt | c4ca710 | 2017-04-08 22:28:04 -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 | c4ca710 | 2017-04-08 22:28:04 -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 | |
| 18 | package org.onosproject.ui.topo; |
| 19 | |
| 20 | import org.junit.Test; |
| 21 | import org.onosproject.ui.AbstractUiTest; |
| 22 | |
Simon Hunt | 8f60ff8 | 2017-04-24 17:19:30 -0700 | [diff] [blame] | 23 | import java.util.ArrayList; |
| 24 | import java.util.List; |
| 25 | |
| 26 | import static com.google.common.collect.ImmutableList.of; |
Simon Hunt | c4ca710 | 2017-04-08 22:28:04 -0700 | [diff] [blame] | 27 | import static org.junit.Assert.*; |
Simon Hunt | 8f60ff8 | 2017-04-24 17:19:30 -0700 | [diff] [blame] | 28 | import static org.onosproject.ui.topo.LayoutLocation.*; |
Simon Hunt | c4ca710 | 2017-04-08 22:28:04 -0700 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Unit tests for {@link LayoutLocation}. |
| 32 | */ |
| 33 | public class LayoutLocationTest extends AbstractUiTest { |
| 34 | |
| 35 | private static final String SOME_ID = "foo"; |
Simon Hunt | 8f60ff8 | 2017-04-24 17:19:30 -0700 | [diff] [blame] | 36 | private static final String OTHER_ID = "bar"; |
Simon Hunt | c4ca710 | 2017-04-08 22:28:04 -0700 | [diff] [blame] | 37 | private static final double SQRT2 = 1.414; |
| 38 | private static final double PI = 3.142; |
| 39 | private static final double ZERO = 0.0; |
| 40 | |
Simon Hunt | f27a929 | 2017-05-04 17:36:26 -0700 | [diff] [blame] | 41 | private static final String COMPACT_LL_1 = "foo,geo,3.142,1.414"; |
| 42 | private static final String COMPACT_LL_2 = "bar,grid,1.414,3.142"; |
Simon Hunt | 8f60ff8 | 2017-04-24 17:19:30 -0700 | [diff] [blame] | 43 | |
| 44 | private static final String COMPACT_LIST = COMPACT_LL_1 + "~" + COMPACT_LL_2; |
| 45 | |
Simon Hunt | c4ca710 | 2017-04-08 22:28:04 -0700 | [diff] [blame] | 46 | private LayoutLocation ll; |
Simon Hunt | 8f60ff8 | 2017-04-24 17:19:30 -0700 | [diff] [blame] | 47 | private LayoutLocation ll2; |
Simon Hunt | c4ca710 | 2017-04-08 22:28:04 -0700 | [diff] [blame] | 48 | |
| 49 | @Test |
| 50 | public void basic() { |
| 51 | ll = layoutLocation(SOME_ID, Type.GRID, SQRT2, PI); |
| 52 | print(ll); |
| 53 | assertEquals("bad id", SOME_ID, ll.id()); |
| 54 | assertEquals("bad type", Type.GRID, ll.locType()); |
| 55 | assertEquals("bad Y", SQRT2, ll.latOrY(), TOLERANCE); |
| 56 | assertEquals("bad X", PI, ll.longOrX(), TOLERANCE); |
| 57 | assertFalse("bad origin check", ll.isOrigin()); |
| 58 | } |
| 59 | |
| 60 | @Test |
| 61 | public void createGeoLocFromStringType() { |
| 62 | ll = layoutLocation(SOME_ID, "geo", SQRT2, PI); |
| 63 | assertEquals("bad type - not geo", Type.GEO, ll.locType()); |
| 64 | } |
| 65 | |
| 66 | @Test |
| 67 | public void createGridLocFromStringType() { |
| 68 | ll = layoutLocation(SOME_ID, "grid", SQRT2, PI); |
| 69 | assertEquals("bad type - not grid", Type.GRID, ll.locType()); |
| 70 | } |
| 71 | |
| 72 | @Test |
| 73 | public void zeroLatitude() { |
| 74 | ll = layoutLocation(SOME_ID, Type.GEO, ZERO, PI); |
| 75 | assertFalse("shouldn't be origin for zero latitude", ll.isOrigin()); |
| 76 | } |
| 77 | |
| 78 | @Test |
| 79 | public void zeroLongitude() { |
| 80 | ll = layoutLocation(SOME_ID, Type.GEO, PI, ZERO); |
| 81 | assertFalse("shouldn't be origin for zero longitude", ll.isOrigin()); |
| 82 | } |
| 83 | |
| 84 | @Test |
| 85 | public void origin() { |
| 86 | ll = layoutLocation(SOME_ID, Type.GRID, ZERO, ZERO); |
| 87 | assertTrue("should be origin", ll.isOrigin()); |
| 88 | } |
| 89 | |
| 90 | @Test(expected = IllegalArgumentException.class) |
| 91 | public void badType() { |
| 92 | layoutLocation(SOME_ID, "foo", ZERO, PI); |
| 93 | } |
| 94 | |
| 95 | @Test(expected = NullPointerException.class) |
| 96 | public void nullId() { |
| 97 | layoutLocation(null, Type.GRID, PI, PI); |
| 98 | } |
Simon Hunt | 8f60ff8 | 2017-04-24 17:19:30 -0700 | [diff] [blame] | 99 | |
| 100 | @Test |
| 101 | public void compactString() { |
| 102 | ll = layoutLocation(SOME_ID, Type.GEO, PI, SQRT2); |
| 103 | String s = ll.toCompactListString(); |
| 104 | assertEquals("wrong compactness", COMPACT_LL_1, s); |
| 105 | } |
| 106 | |
| 107 | @Test |
| 108 | public void fromCompactStringTest() { |
| 109 | ll = fromCompactString(COMPACT_LL_1); |
| 110 | verifyLL1(ll); |
| 111 | } |
| 112 | |
| 113 | private void verifyLL1(LayoutLocation ll) { |
| 114 | assertEquals("LL1 bad id", SOME_ID, ll.id()); |
| 115 | assertEquals("LL1 bad type", Type.GEO, ll.locType()); |
| 116 | assertEquals("LL1 bad Y", PI, ll.latOrY(), TOLERANCE); |
| 117 | assertEquals("LL1 bad X", SQRT2, ll.longOrX(), TOLERANCE); |
| 118 | } |
| 119 | |
| 120 | private void verifyLL2(LayoutLocation ll) { |
| 121 | assertEquals("LL1 bad id", OTHER_ID, ll.id()); |
| 122 | assertEquals("LL1 bad type", Type.GRID, ll.locType()); |
| 123 | assertEquals("LL1 bad Y", SQRT2, ll.latOrY(), TOLERANCE); |
| 124 | assertEquals("LL1 bad X", PI, ll.longOrX(), TOLERANCE); |
| 125 | } |
| 126 | |
| 127 | |
| 128 | @Test(expected = IllegalArgumentException.class) |
| 129 | public void badCompactTooShort() { |
| 130 | fromCompactString("one,two,three"); |
| 131 | } |
| 132 | |
| 133 | @Test(expected = IllegalArgumentException.class) |
| 134 | public void badCompactTooLong() { |
| 135 | fromCompactString("one,two,three,4,5"); |
| 136 | } |
| 137 | |
| 138 | @Test(expected = IllegalArgumentException.class) |
| 139 | public void badCompactNoId() { |
| 140 | fromCompactString(",GEO,1,2"); |
| 141 | } |
| 142 | |
| 143 | @Test(expected = IllegalArgumentException.class) |
| 144 | public void badCompactUnparsableY() { |
| 145 | fromCompactString("foo,GEO,yyy,2.3"); |
| 146 | } |
| 147 | |
| 148 | @Test(expected = IllegalArgumentException.class) |
| 149 | public void badCompactUnparsableX() { |
| 150 | fromCompactString("foo,GEO,0.2,xxx"); |
| 151 | } |
| 152 | |
| 153 | @Test |
| 154 | public void toCompactList() { |
| 155 | ll = layoutLocation(SOME_ID, Type.GEO, PI, SQRT2); |
| 156 | ll2 = layoutLocation(OTHER_ID, Type.GRID, SQRT2, PI); |
| 157 | String compact = toCompactListString(ll, ll2); |
| 158 | print(compact); |
| 159 | assertEquals("wrong list encoding", COMPACT_LIST, compact); |
| 160 | } |
| 161 | |
| 162 | @Test |
| 163 | public void toCompactList2() { |
| 164 | ll = layoutLocation(SOME_ID, Type.GEO, PI, SQRT2); |
| 165 | ll2 = layoutLocation(OTHER_ID, Type.GRID, SQRT2, PI); |
| 166 | List<LayoutLocation> locs = of(ll, ll2); |
| 167 | String compact = toCompactListString(locs); |
| 168 | print(compact); |
| 169 | assertEquals("wrong list encoding", COMPACT_LIST, compact); |
| 170 | } |
| 171 | |
| 172 | @Test |
| 173 | public void fromCompactList() { |
| 174 | List<LayoutLocation> locs = fromCompactListString(COMPACT_LIST); |
| 175 | ll = locs.get(0); |
| 176 | ll2 = locs.get(1); |
| 177 | verifyLL1(ll); |
| 178 | verifyLL2(ll2); |
| 179 | } |
| 180 | |
| 181 | @Test |
| 182 | public void fromCompactListNull() { |
| 183 | List<LayoutLocation> locs = fromCompactListString(null); |
| 184 | assertEquals("non-empty list", 0, locs.size()); |
| 185 | } |
| 186 | |
| 187 | @Test |
| 188 | public void fromCompactListEmpty() { |
| 189 | List<LayoutLocation> locs = fromCompactListString(""); |
| 190 | assertEquals("non-empty list", 0, locs.size()); |
| 191 | } |
| 192 | |
| 193 | @Test |
| 194 | public void toCompactListStringNullList() { |
| 195 | String s = toCompactListString((List<LayoutLocation>) null); |
| 196 | assertEquals("not empty string", "", s); |
| 197 | } |
| 198 | |
| 199 | @Test |
| 200 | public void toCompactListStringNullArray() { |
| 201 | String s = toCompactListString((LayoutLocation[]) null); |
| 202 | assertEquals("not empty string", "", s); |
| 203 | } |
| 204 | |
| 205 | @Test |
| 206 | public void toCompactListStringEmptyArray() { |
| 207 | String s = toCompactListString(); |
| 208 | assertEquals("not empty string", "", s); |
| 209 | } |
| 210 | |
| 211 | @Test |
| 212 | public void toCompactListStringEmptyList() { |
| 213 | String s = toCompactListString(new ArrayList<>()); |
| 214 | assertEquals("not empty string", "", s); |
| 215 | } |
Simon Hunt | c4ca710 | 2017-04-08 22:28:04 -0700 | [diff] [blame] | 216 | } |