blob: 7e6cc227d4cf5de49d4a7d05a79036ea538513fd [file] [log] [blame]
Simon Hunt629b99e2015-07-27 17:38:33 -07001/*
2 * Copyright 2015 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.
Simon Hunt629b99e2015-07-27 17:38:33 -070015 */
16
17package org.onosproject.ui.topo;
18
Simon Hunt00a27ff2015-07-28 18:53:40 -070019import org.junit.BeforeClass;
Simon Hunt629b99e2015-07-27 17:38:33 -070020import org.junit.Test;
21import org.onosproject.ui.topo.PropertyPanel.Prop;
22
Simon Hunt00a27ff2015-07-28 18:53:40 -070023import java.util.HashMap;
Simon Hunt629b99e2015-07-27 17:38:33 -070024import java.util.Iterator;
Simon Hunt00a27ff2015-07-28 18:53:40 -070025import java.util.Map;
Simon Hunt629b99e2015-07-27 17:38:33 -070026
Simon Hunt3a0598f2015-08-04 19:59:04 -070027import static org.junit.Assert.*;
Simon Hunt629b99e2015-07-27 17:38:33 -070028
29/**
30 * Unit tests for {@link PropertyPanel}.
31 */
32public class PropertyPanelTest {
33
34 private static final String TITLE_ORIG = "Original Title";
35 private static final String TYPE_ORIG = "Original type ID";
36 private static final String TITLE_NEW = "New Title";
37 private static final String TYPE_NEW = "New type";
Simon Hunt00a27ff2015-07-28 18:53:40 -070038 private static final String SOME_IDENTIFICATION = "It's Me!";
Simon Hunt629b99e2015-07-27 17:38:33 -070039
Simon Hunt00a27ff2015-07-28 18:53:40 -070040 private static final String KEY_A = "A";
41 private static final String KEY_B = "B";
42 private static final String KEY_C = "C";
Simon Hunt3a0598f2015-08-04 19:59:04 -070043 private static final String SEP = "-";
Simon Hunt00a27ff2015-07-28 18:53:40 -070044 private static final String KEY_Z = "Z";
45 private static final String VALUE_A = "Hay";
46 private static final String VALUE_B = "Bee";
47 private static final String VALUE_C = "Sea";
48 private static final String VALUE_Z = "Zed";
49
50 private static final Map<String, Prop> PROP_MAP = new HashMap<>();
51
52 private static class FooClass {
53 private final String s;
54 FooClass(String s) {
55 this.s = s;
56 }
57
58 @Override
59 public String toString() {
60 return ">" + s + "<";
61 }
62 }
Simon Hunt629b99e2015-07-27 17:38:33 -070063
64 private PropertyPanel pp;
65
66
Simon Hunt00a27ff2015-07-28 18:53:40 -070067
68 @BeforeClass
69 public static void setUpClass() {
70 PROP_MAP.put(KEY_A, new Prop(KEY_A, VALUE_A));
71 PROP_MAP.put(KEY_B, new Prop(KEY_B, VALUE_B));
72 PROP_MAP.put(KEY_C, new Prop(KEY_C, VALUE_C));
73 PROP_MAP.put(KEY_Z, new Prop(KEY_Z, VALUE_Z));
Simon Hunt3a0598f2015-08-04 19:59:04 -070074 PROP_MAP.put(SEP, new PropertyPanel.Separator());
Simon Hunt00a27ff2015-07-28 18:53:40 -070075 }
76
Simon Hunt629b99e2015-07-27 17:38:33 -070077 @Test
78 public void basic() {
79 pp = new PropertyPanel(TITLE_ORIG, TYPE_ORIG);
80 assertEquals("wrong title", TITLE_ORIG, pp.title());
81 assertEquals("wrong type", TYPE_ORIG, pp.typeId());
Simon Hunt00a27ff2015-07-28 18:53:40 -070082 assertNull("id?", pp.id());
Simon Hunt629b99e2015-07-27 17:38:33 -070083 assertEquals("unexpected props", 0, pp.properties().size());
Simon Hunt3a0598f2015-08-04 19:59:04 -070084 assertEquals("unexpected buttons", 0, pp.buttons().size());
Simon Hunt629b99e2015-07-27 17:38:33 -070085 }
86
87 @Test
88 public void changeTitle() {
89 basic();
90 pp.title(TITLE_NEW);
91 assertEquals("wrong title", TITLE_NEW, pp.title());
92 }
93
94 @Test
95 public void changeType() {
96 basic();
97 pp.typeId(TYPE_NEW);
98 assertEquals("wrong type", TYPE_NEW, pp.typeId());
99 }
100
Simon Hunt00a27ff2015-07-28 18:53:40 -0700101 @Test
102 public void setId() {
103 basic();
104 pp.id(SOME_IDENTIFICATION);
105 assertEquals("wrong id", SOME_IDENTIFICATION, pp.id());
106 }
107
108 private void validateProps(String... keys) {
Simon Hunt629b99e2015-07-27 17:38:33 -0700109 Iterator<Prop> iter = pp.properties().iterator();
Simon Hunt00a27ff2015-07-28 18:53:40 -0700110 for (String k: keys) {
111 Prop exp = PROP_MAP.get(k);
112 Prop act = iter.next();
113 assertEquals("Bad prop sequence", exp, act);
Simon Hunt629b99e2015-07-27 17:38:33 -0700114 }
115 }
116
Simon Hunt00a27ff2015-07-28 18:53:40 -0700117 private void validateProp(String key, String expValue) {
118 Iterator<Prop> iter = pp.properties().iterator();
119 Prop prop = null;
120 while (iter.hasNext()) {
121 Prop p = iter.next();
122 if (p.key().equals(key)) {
123 prop = p;
124 break;
125 }
126 }
127 if (prop == null) {
128 fail("no prop found with key: " + key);
129 }
130 assertEquals("Wrong prop value", expValue, prop.value());
131 }
132
Simon Hunt629b99e2015-07-27 17:38:33 -0700133 @Test
134 public void props() {
135 basic();
Simon Hunt00a27ff2015-07-28 18:53:40 -0700136 pp.addProp(KEY_A, VALUE_A)
137 .addProp(KEY_B, VALUE_B)
138 .addProp(KEY_C, VALUE_C);
Simon Hunt629b99e2015-07-27 17:38:33 -0700139 assertEquals("bad props", 3, pp.properties().size());
Simon Hunt00a27ff2015-07-28 18:53:40 -0700140 validateProps(KEY_A, KEY_B, KEY_C);
Simon Hunt629b99e2015-07-27 17:38:33 -0700141 }
142
143 @Test
Simon Hunt3a0598f2015-08-04 19:59:04 -0700144 public void separator() {
145 props();
146 pp.addSeparator()
147 .addProp(KEY_Z, VALUE_Z);
148
149 assertEquals("bad props", 5, pp.properties().size());
150 validateProps(KEY_A, KEY_B, KEY_C, SEP, KEY_Z);
151 }
152
153 @Test
Simon Hunt629b99e2015-07-27 17:38:33 -0700154 public void removeAllProps() {
155 props();
156 assertEquals("wrong props", 3, pp.properties().size());
157 pp.removeAllProps();
158 assertEquals("unexpected props", 0, pp.properties().size());
159 }
160
161 @Test
162 public void adjustProps() {
163 props();
Simon Hunt00a27ff2015-07-28 18:53:40 -0700164 pp.removeProps(KEY_B, KEY_A);
165 pp.addProp(KEY_Z, VALUE_Z);
166 validateProps(KEY_C, KEY_Z);
Simon Hunt629b99e2015-07-27 17:38:33 -0700167 }
Simon Hunt00a27ff2015-07-28 18:53:40 -0700168
169 @Test
170 public void intValues() {
171 basic();
172 pp.addProp(KEY_A, 200)
173 .addProp(KEY_B, 2000)
174 .addProp(KEY_C, 1234567);
175
176 validateProp(KEY_A, "200");
177 validateProp(KEY_B, "2,000");
178 validateProp(KEY_C, "1,234,567");
179 }
180
181 @Test
182 public void longValues() {
183 basic();
184 pp.addProp(KEY_A, 200L)
185 .addProp(KEY_B, 2000L)
186 .addProp(KEY_C, 1234567L)
187 .addProp(KEY_Z, Long.MAX_VALUE);
188
189 validateProp(KEY_A, "200");
190 validateProp(KEY_B, "2,000");
191 validateProp(KEY_C, "1,234,567");
192 validateProp(KEY_Z, "9,223,372,036,854,775,807");
193 }
194
195 @Test
196 public void objectValue() {
197 basic();
198 pp.addProp(KEY_A, new FooClass("a"))
199 .addProp(KEY_B, new FooClass("bxyyzy"), "[xz]");
200
201 validateProp(KEY_A, ">a<");
202 validateProp(KEY_B, ">byyy<");
203 }
204
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700205 private static final ButtonId BD_A = new ButtonId(KEY_A);
206 private static final ButtonId BD_B = new ButtonId(KEY_B);
207 private static final ButtonId BD_C = new ButtonId(KEY_C);
208 private static final ButtonId BD_Z = new ButtonId(KEY_Z);
Simon Hunt3a0598f2015-08-04 19:59:04 -0700209
210 private void verifyButtons(String... keys) {
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700211 Iterator<ButtonId> iter = pp.buttons().iterator();
Simon Hunt3a0598f2015-08-04 19:59:04 -0700212 for (String k: keys) {
213 assertEquals("wrong button", k, iter.next().id());
214 }
215 assertFalse("too many buttons", iter.hasNext());
216 }
217
218 @Test
219 public void buttons() {
220 basic();
221 pp.addButton(BD_A)
222 .addButton(BD_B);
223 assertEquals("wrong buttons", 2, pp.buttons().size());
224 verifyButtons(KEY_A, KEY_B);
225
226 pp.removeButtons(BD_B)
227 .addButton(BD_C)
228 .addButton(BD_Z);
229 assertEquals("wrong buttons", 3, pp.buttons().size());
230 verifyButtons(KEY_A, KEY_C, KEY_Z);
231
232 pp.removeAllButtons()
233 .addButton(BD_B);
234 assertEquals("wrong buttons", 1, pp.buttons().size());
235 verifyButtons(KEY_B);
236 }
Simon Hunt629b99e2015-07-27 17:38:33 -0700237}