blob: f04dd9e0b3c0d98007efa81f7a9be9a0549f7dc6 [file] [log] [blame]
Simon Hunt4f3a4072016-10-17 17:52:11 -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.net.config.basics;
18
19import com.fasterxml.jackson.databind.JsonNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import org.junit.Before;
22import org.junit.Test;
23import org.onosproject.net.config.InvalidFieldException;
24import org.onosproject.net.region.RegionId;
25import org.onosproject.ui.model.topo.UiTopoLayoutId;
26
27import static org.junit.Assert.assertEquals;
28import static org.junit.Assert.assertNull;
29import static org.onosproject.net.config.basics.BasicElementConfig.ZERO_THRESHOLD;
30import static org.onosproject.net.config.basics.BasicUiTopoLayoutConfig.GEOMAP;
31import static org.onosproject.net.config.basics.BasicUiTopoLayoutConfig.OFFSET_X;
32import static org.onosproject.net.config.basics.BasicUiTopoLayoutConfig.OFFSET_Y;
33import static org.onosproject.net.config.basics.BasicUiTopoLayoutConfig.SCALE;
34import static org.onosproject.net.config.basics.BasicUiTopoLayoutConfig.SPRITES;
35import static org.onosproject.net.region.RegionId.regionId;
36import static org.onosproject.ui.model.topo.UiTopoLayoutId.layoutId;
37
38/**
39 * Test class for {@link BasicUiTopoLayoutConfig}.
40 */
41public class BasicUiTopoLayoutConfigTest extends AbstractConfigTest {
42
43 private static final String LAYOUT_JSON = "configs.layouts.1.json";
44
45 private static final String L_DEFAULT = "_default_";
46 private static final String L1 = "l1";
47 private static final String L2 = "l2";
48 private static final String L3 = "l3";
49
50 private static final RegionId R1 = regionId("r1");
51 private static final RegionId R2 = regionId("r2");
52 private static final RegionId R3 = regionId("r3");
53
54 private static final String UK = "uk";
55 private static final String UK_BRIGHTON = "uk-brighton";
56 private static final String UK_LONDON = "uk-london";
57 private static final String UK_LONDON_WEST = "uk-london-westminster";
58
59 private static final RegionId NEW_REGION = regionId("new-region");
60 private static final UiTopoLayoutId NEW_PARENT = layoutId("new-parent");
61 private static final String NEW_MAP = "new-geomap";
62 private static final String NEW_SPR = "new-sprites";
63
64
65 private JsonNode data;
66 private BasicUiTopoLayoutConfig cfg;
67
68 @Before
69 public void setUp() {
70 data = getTestJson(LAYOUT_JSON);
71 }
72
73 private JsonNode getL(String key) {
74 return data.get("layouts").get(key).get("basic");
75 }
76
77 private void loadLayout(String lid) {
78 JsonNode node = getL(lid);
79 print(JSON_LOADED, node);
80
81 cfg = new BasicUiTopoLayoutConfig();
82 cfg.init(layoutId(lid), lid, node, mapper, delegate);
83 }
84
85 private void checkLayout(RegionId expRegion, UiTopoLayoutId expParent,
86 String expGeo, String expSpr) {
87 print(CHECKING_S, cfg);
88 assertEquals("wrong region", expRegion, cfg.region());
89 assertEquals("wrong parent", expParent, cfg.parent());
90 assertEquals("wrong geomap", expGeo, cfg.geomap());
91 assertEquals("wrong sprites", expSpr, cfg.sprites());
92 }
93
94 private void checkScale(double expScale, double expOffx, double expOffy) {
95 assertEquals(SCALE, expScale, cfg.scale(), ZERO_THRESHOLD);
96 assertEquals(OFFSET_X, expOffx, cfg.offsetX(), ZERO_THRESHOLD);
97 assertEquals(OFFSET_Y, expOffy, cfg.offsetY(), ZERO_THRESHOLD);
98 }
99
100 @Test
101 public void layoutConfigDefault() {
102 loadLayout(L_DEFAULT);
103 checkLayout(null, UiTopoLayoutId.DEFAULT_ID, UK, null);
104 checkScale(1.2, -50, 0);
105 }
106
107 @Test
108 public void layoutConfigOne() {
109 loadLayout(L1);
110 checkLayout(R1, UiTopoLayoutId.DEFAULT_ID, UK_BRIGHTON, null);
111 checkScale(0.9, 200, -45);
112 }
113
114 @Test
115 public void layoutConfigTwo() {
116 loadLayout(L2);
117 checkLayout(R2, UiTopoLayoutId.DEFAULT_ID, UK_LONDON, null);
118 checkScale(1, 0, 0);
119 }
120
121 @Test
122 public void layoutConfigThree() {
123 loadLayout(L3);
124 checkLayout(R3, layoutId(L2), null, UK_LONDON_WEST);
125 checkScale(1, 0, 0);
126 }
127
128 private ObjectNode tmpNode(String... props) {
129 return new TmpJson().props(props).node();
130 }
131
132 private BasicUiTopoLayoutConfig cfgFromJson(ObjectNode json) {
133 BasicUiTopoLayoutConfig cfg = new BasicUiTopoLayoutConfig();
134 cfg.init(layoutId("foo"), BASIC, json, mapper, delegate);
135 return cfg;
136 }
137
138 @Test(expected = InvalidFieldException.class)
139 public void cantHaveGeoAndSprite() {
140 cfg = cfgFromJson(tmpNode(GEOMAP, SPRITES));
141 cfg.isValid();
142 }
143
144 @Test(expected = InvalidFieldException.class)
145 public void cantSetGeoIfSpritesAreSet() {
146 cfg = cfgFromJson(tmpNode(SPRITES));
147 cfg.geomap("map-name");
148 }
149
150 @Test(expected = InvalidFieldException.class)
151 public void cantSetSpritesIfGeoIsSet() {
152 cfg = cfgFromJson(tmpNode(GEOMAP));
153 cfg.sprites("sprites-name");
154 }
155
156 @Test
157 public void setRegion() {
158 loadLayout(L1);
159 assertEquals("not region-1", R1, cfg.region());
160 cfg.region(NEW_REGION);
161 assertEquals("not new region", NEW_REGION, cfg.region());
162 cfg.region(null);
163 assertNull("region not cleared", cfg.region());
164 }
165
166 @Test
167 public void setParent() {
168 loadLayout(L1);
169 assertEquals("parent not default layout", UiTopoLayoutId.DEFAULT_ID, cfg.parent());
170 cfg.parent(NEW_PARENT);
171 assertEquals("not new parent", NEW_PARENT, cfg.parent());
172 cfg.parent(null);
173 assertEquals("parent not reset to default", UiTopoLayoutId.DEFAULT_ID, cfg.parent());
174 }
175
176 @Test
177 public void setGeomap() {
178 loadLayout(L1);
179 assertEquals("map not brighton", UK_BRIGHTON, cfg.geomap());
180 cfg.geomap(NEW_MAP);
181 assertEquals("not new map", NEW_MAP, cfg.geomap());
182 cfg.geomap(null);
183 assertNull("geomap not cleared", cfg.geomap());
184 }
185
186 @Test
187 public void setSprites() {
188 loadLayout(L3);
189 assertEquals("sprites not westminster", UK_LONDON_WEST, cfg.sprites());
190 cfg.sprites(NEW_SPR);
191 assertEquals("not new sprites", NEW_SPR, cfg.sprites());
192 cfg.sprites(null);
193 assertNull("sprites not cleared", cfg.sprites());
194 }
195
196 @Test
197 public void setScaleAndOffset() {
198 loadLayout(L1);
199 assertEquals("wrong init scale", 0.9, cfg.scale(), ZERO_THRESHOLD);
200 assertEquals("wrong init x-offset", 200, cfg.offsetX(), ZERO_THRESHOLD);
201 assertEquals("wrong init y-offset", -45, cfg.offsetY(), ZERO_THRESHOLD);
202 cfg.scale(3.14).offsetX(12.0).offsetY(13.0);
203 assertEquals("wrong new scale", 3.14, cfg.scale(), ZERO_THRESHOLD);
204 assertEquals("wrong new x-offset", 12, cfg.offsetX(), ZERO_THRESHOLD);
205 assertEquals("wrong new y-offset", 13, cfg.offsetY(), ZERO_THRESHOLD);
206 cfg.scale(null).offsetX(null).offsetY(null);
207 assertEquals("wrong default scale", 1, cfg.scale(), ZERO_THRESHOLD);
208 assertEquals("wrong default x-offset", 0, cfg.offsetX(), ZERO_THRESHOLD);
209 assertEquals("wrong default y-offset", 0, cfg.offsetY(), ZERO_THRESHOLD);
210 }
211}