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