Simon Hunt | d0fa284 | 2016-10-24 18:04:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Simon Hunt | d0fa284 | 2016-10-24 18:04:05 -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.model.topo; |
| 18 | |
| 19 | import org.junit.Test; |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 20 | import org.onosproject.net.Annotations; |
| 21 | import org.onosproject.net.DefaultAnnotations; |
Simon Hunt | d0fa284 | 2016-10-24 18:04:05 -0700 | [diff] [blame] | 22 | import org.onosproject.net.region.DefaultRegion; |
| 23 | import org.onosproject.net.region.Region; |
| 24 | import org.onosproject.net.region.RegionId; |
| 25 | |
| 26 | import static org.junit.Assert.assertEquals; |
| 27 | import static org.junit.Assert.assertFalse; |
| 28 | import static org.junit.Assert.assertNull; |
| 29 | import static org.junit.Assert.assertTrue; |
| 30 | import static org.junit.Assert.fail; |
| 31 | import static org.onosproject.net.region.Region.Type.CAMPUS; |
| 32 | import static org.onosproject.net.region.RegionId.regionId; |
| 33 | import static org.onosproject.ui.model.topo.UiTopoLayout.E_GEOMAP_SET; |
| 34 | import static org.onosproject.ui.model.topo.UiTopoLayout.E_ROOT_PARENT; |
| 35 | import static org.onosproject.ui.model.topo.UiTopoLayout.E_ROOT_REGION; |
| 36 | import static org.onosproject.ui.model.topo.UiTopoLayout.E_SPRITES_SET; |
| 37 | import static org.onosproject.ui.model.topo.UiTopoLayoutId.layoutId; |
| 38 | |
| 39 | /** |
| 40 | * Unit tests for {@link UiTopoLayout}. |
| 41 | */ |
| 42 | public class UiTopoLayoutTest { |
| 43 | |
| 44 | private static final String AM_NOEX = "no exception thrown"; |
| 45 | private static final String AM_WREXMSG = "wrong exception message"; |
| 46 | |
| 47 | private static final double DELTA = Double.MIN_VALUE * 2.0; |
| 48 | |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 49 | private static final Annotations NO_ANNOTS = DefaultAnnotations.EMPTY; |
Simon Hunt | d0fa284 | 2016-10-24 18:04:05 -0700 | [diff] [blame] | 50 | private static final UiTopoLayoutId OTHER_ID = layoutId("other-id"); |
| 51 | private static final RegionId REGION_ID = regionId("some-region"); |
| 52 | private static final Region REGION = |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 53 | new DefaultRegion(REGION_ID, "Region-1", CAMPUS, NO_ANNOTS, null); |
Simon Hunt | d0fa284 | 2016-10-24 18:04:05 -0700 | [diff] [blame] | 54 | private static final String GEOMAP = "geo1"; |
| 55 | private static final String SPRITE = "spr1"; |
| 56 | |
| 57 | |
| 58 | private UiTopoLayout layout; |
| 59 | |
| 60 | private void mkRootLayout() { |
| 61 | layout = new UiTopoLayout(UiTopoLayoutId.DEFAULT_ID); |
| 62 | } |
| 63 | |
| 64 | private void mkOtherLayout() { |
| 65 | layout = new UiTopoLayout(OTHER_ID); |
| 66 | } |
| 67 | |
| 68 | @Test(expected = NullPointerException.class) |
| 69 | public void nullIdentifier() { |
| 70 | layout = new UiTopoLayout(null); |
| 71 | } |
| 72 | |
| 73 | @Test |
| 74 | public void rootLayout() { |
| 75 | mkRootLayout(); |
| 76 | assertEquals("wrong id", UiTopoLayoutId.DEFAULT_ID, layout.id()); |
| 77 | assertEquals("wrong parent (not self)", |
| 78 | UiTopoLayoutId.DEFAULT_ID, layout.parent()); |
| 79 | assertTrue("should be root", layout.isRoot()); |
| 80 | |
| 81 | assertNull("unexpected region", layout.region()); |
| 82 | assertEquals("unexpected region id", UiRegion.NULL_ID, layout.regionId()); |
| 83 | } |
| 84 | |
| 85 | @Test |
| 86 | public void otherLayout() { |
| 87 | mkOtherLayout(); |
| 88 | assertEquals("wrong id", OTHER_ID, layout.id()); |
| 89 | assertEquals("not null parent", null, layout.parent()); |
| 90 | assertFalse("should NOT be root", layout.isRoot()); |
| 91 | |
| 92 | // check attribute default values... |
| 93 | assertNull("unexpected region", layout.region()); |
| 94 | assertNull("unexpected region id", layout.regionId()); |
| 95 | assertNull("unexpected geomap", layout.geomap()); |
| 96 | assertNull("unexpected sprites", layout.sprites()); |
| 97 | assertEquals("non-unity scale", 1.0, layout.scale(), DELTA); |
| 98 | assertEquals("non-zero x-off", 0.0, layout.offsetX(), DELTA); |
| 99 | assertEquals("non-zero y-off", 0.0, layout.offsetY(), DELTA); |
| 100 | } |
| 101 | |
| 102 | @Test |
| 103 | public void setRegionOnRoot() { |
| 104 | mkRootLayout(); |
| 105 | try { |
| 106 | layout.region(REGION); |
| 107 | fail(AM_NOEX); |
| 108 | } catch (IllegalArgumentException e) { |
| 109 | assertEquals(AM_WREXMSG, E_ROOT_REGION, e.getMessage()); |
| 110 | } |
| 111 | |
| 112 | try { |
| 113 | layout.region(null); |
| 114 | fail(AM_NOEX); |
| 115 | } catch (IllegalArgumentException e) { |
| 116 | assertEquals(AM_WREXMSG, E_ROOT_REGION, e.getMessage()); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | @Test |
| 121 | public void setRegionOnOther() { |
| 122 | mkOtherLayout(); |
| 123 | layout.region(REGION); |
| 124 | assertEquals("wrong region", REGION, layout.region()); |
| 125 | assertEquals("wrong region id", REGION_ID, layout.regionId()); |
| 126 | |
| 127 | layout.region(null); |
| 128 | assertEquals("non-null region", null, layout.region()); |
| 129 | assertEquals("non-null region id", null, layout.regionId()); |
| 130 | } |
| 131 | |
| 132 | @Test |
| 133 | public void setParentOnRoot() { |
| 134 | mkRootLayout(); |
| 135 | try { |
| 136 | layout.parent(OTHER_ID); |
| 137 | fail(AM_NOEX); |
| 138 | } catch (IllegalArgumentException e) { |
| 139 | assertEquals(AM_WREXMSG, E_ROOT_PARENT, e.getMessage()); |
| 140 | } |
| 141 | |
| 142 | try { |
| 143 | layout.parent(null); |
| 144 | fail(AM_NOEX); |
| 145 | } catch (IllegalArgumentException e) { |
| 146 | assertEquals(AM_WREXMSG, E_ROOT_PARENT, e.getMessage()); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | @Test |
| 151 | public void setParentOnOther() { |
| 152 | mkOtherLayout(); |
| 153 | layout.parent(OTHER_ID); |
| 154 | assertEquals("wrong parent", OTHER_ID, layout.parent()); |
| 155 | |
| 156 | layout.parent(null); |
| 157 | assertEquals("non-null parent", null, layout.parent()); |
| 158 | } |
| 159 | |
| 160 | @Test |
| 161 | public void setGeomap() { |
| 162 | mkRootLayout(); |
| 163 | assertEquals("geo to start", null, layout.geomap()); |
| 164 | layout.geomap(GEOMAP); |
| 165 | assertEquals("wrong geo", GEOMAP, layout.geomap()); |
| 166 | } |
| 167 | |
| 168 | @Test |
| 169 | public void setGeomapAfterSprites() { |
| 170 | mkRootLayout(); |
| 171 | layout.sprites(SPRITE); |
| 172 | assertEquals("geo to start", null, layout.geomap()); |
| 173 | try { |
| 174 | layout.geomap(GEOMAP); |
| 175 | fail(AM_NOEX); |
| 176 | } catch (IllegalArgumentException e) { |
| 177 | assertEquals(AM_WREXMSG, E_SPRITES_SET, e.getMessage()); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | @Test |
| 182 | public void setSprites() { |
| 183 | mkRootLayout(); |
| 184 | assertEquals("sprite to start", null, layout.sprites()); |
| 185 | layout.sprites(SPRITE); |
| 186 | assertEquals("wrong sprite", SPRITE, layout.sprites()); |
| 187 | } |
| 188 | |
| 189 | @Test |
| 190 | public void setSpritesAfterGeomap() { |
| 191 | mkRootLayout(); |
| 192 | layout.geomap(GEOMAP); |
| 193 | assertEquals("sprites to start", null, layout.sprites()); |
| 194 | try { |
| 195 | layout.sprites(SPRITE); |
| 196 | fail(AM_NOEX); |
| 197 | } catch (IllegalArgumentException e) { |
| 198 | assertEquals(AM_WREXMSG, E_GEOMAP_SET, e.getMessage()); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | @Test |
| 203 | public void setScale() { |
| 204 | mkRootLayout(); |
| 205 | layout.scale(3.0); |
| 206 | assertEquals("wrong scale", 3.0, layout.scale(), DELTA); |
| 207 | layout.scale(0.05); |
| 208 | assertEquals("wrong scale", 0.05, layout.scale(), DELTA); |
| 209 | } |
| 210 | |
| 211 | @Test(expected = IllegalArgumentException.class) |
| 212 | public void scaleTooSmall() { |
| 213 | mkRootLayout(); |
| 214 | layout.scale(0.0099); |
| 215 | } |
| 216 | |
| 217 | @Test(expected = IllegalArgumentException.class) |
| 218 | public void scaleTooBig() { |
| 219 | mkRootLayout(); |
| 220 | layout.scale(100.009); |
| 221 | } |
| 222 | |
| 223 | @Test |
| 224 | public void setXOff() { |
| 225 | mkOtherLayout(); |
| 226 | layout.offsetX(23.4); |
| 227 | assertEquals("wrong x-offset", 23.4, layout.offsetX(), DELTA); |
| 228 | } |
| 229 | |
| 230 | @Test |
| 231 | public void setYOff() { |
| 232 | mkOtherLayout(); |
| 233 | layout.offsetY(2.71828); |
| 234 | assertEquals("wrong y-offset", 2.71828, layout.offsetY(), DELTA); |
| 235 | } |
| 236 | |
| 237 | } |