blob: b08ee4dc581b1c4f06d2197fe3d1d503633a49c5 [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.
15 *
16 */
17
18package org.onosproject.ui.topo;
19
Simon Hunt00a27ff2015-07-28 18:53:40 -070020import org.junit.BeforeClass;
Simon Hunt629b99e2015-07-27 17:38:33 -070021import org.junit.Test;
22import org.onosproject.ui.topo.PropertyPanel.Prop;
23
Simon Hunt00a27ff2015-07-28 18:53:40 -070024import java.util.HashMap;
Simon Hunt629b99e2015-07-27 17:38:33 -070025import java.util.Iterator;
Simon Hunt00a27ff2015-07-28 18:53:40 -070026import java.util.Map;
Simon Hunt629b99e2015-07-27 17:38:33 -070027
Simon Hunt3a0598f2015-08-04 19:59:04 -070028import static org.junit.Assert.*;
Simon Hunt629b99e2015-07-27 17:38:33 -070029
30/**
31 * Unit tests for {@link PropertyPanel}.
32 */
33public class PropertyPanelTest {
34
35 private static final String TITLE_ORIG = "Original Title";
36 private static final String TYPE_ORIG = "Original type ID";
37 private static final String TITLE_NEW = "New Title";
38 private static final String TYPE_NEW = "New type";
Simon Hunt00a27ff2015-07-28 18:53:40 -070039 private static final String SOME_IDENTIFICATION = "It's Me!";
Simon Hunt629b99e2015-07-27 17:38:33 -070040
Simon Hunt00a27ff2015-07-28 18:53:40 -070041 private static final String KEY_A = "A";
42 private static final String KEY_B = "B";
43 private static final String KEY_C = "C";
Simon Hunt3a0598f2015-08-04 19:59:04 -070044 private static final String SEP = "-";
Simon Hunt00a27ff2015-07-28 18:53:40 -070045 private static final String KEY_Z = "Z";
46 private static final String VALUE_A = "Hay";
47 private static final String VALUE_B = "Bee";
48 private static final String VALUE_C = "Sea";
49 private static final String VALUE_Z = "Zed";
50
51 private static final Map<String, Prop> PROP_MAP = new HashMap<>();
52
53 private static class FooClass {
54 private final String s;
55 FooClass(String s) {
56 this.s = s;
57 }
58
59 @Override
60 public String toString() {
61 return ">" + s + "<";
62 }
63 }
Simon Hunt629b99e2015-07-27 17:38:33 -070064
65 private PropertyPanel pp;
66
67
Simon Hunt00a27ff2015-07-28 18:53:40 -070068
69 @BeforeClass
70 public static void setUpClass() {
71 PROP_MAP.put(KEY_A, new Prop(KEY_A, VALUE_A));
72 PROP_MAP.put(KEY_B, new Prop(KEY_B, VALUE_B));
73 PROP_MAP.put(KEY_C, new Prop(KEY_C, VALUE_C));
74 PROP_MAP.put(KEY_Z, new Prop(KEY_Z, VALUE_Z));
Simon Hunt3a0598f2015-08-04 19:59:04 -070075 PROP_MAP.put(SEP, new PropertyPanel.Separator());
Simon Hunt00a27ff2015-07-28 18:53:40 -070076 }
77
Simon Hunt629b99e2015-07-27 17:38:33 -070078 @Test
79 public void basic() {
80 pp = new PropertyPanel(TITLE_ORIG, TYPE_ORIG);
81 assertEquals("wrong title", TITLE_ORIG, pp.title());
82 assertEquals("wrong type", TYPE_ORIG, pp.typeId());
Simon Hunt00a27ff2015-07-28 18:53:40 -070083 assertNull("id?", pp.id());
Simon Hunt629b99e2015-07-27 17:38:33 -070084 assertEquals("unexpected props", 0, pp.properties().size());
Simon Hunt3a0598f2015-08-04 19:59:04 -070085 assertEquals("unexpected buttons", 0, pp.buttons().size());
Simon Hunt629b99e2015-07-27 17:38:33 -070086 }
87
88 @Test
89 public void changeTitle() {
90 basic();
91 pp.title(TITLE_NEW);
92 assertEquals("wrong title", TITLE_NEW, pp.title());
93 }
94
95 @Test
96 public void changeType() {
97 basic();
98 pp.typeId(TYPE_NEW);
99 assertEquals("wrong type", TYPE_NEW, pp.typeId());
100 }
101
Simon Hunt00a27ff2015-07-28 18:53:40 -0700102 @Test
103 public void setId() {
104 basic();
105 pp.id(SOME_IDENTIFICATION);
106 assertEquals("wrong id", SOME_IDENTIFICATION, pp.id());
107 }
108
109 private void validateProps(String... keys) {
Simon Hunt629b99e2015-07-27 17:38:33 -0700110 Iterator<Prop> iter = pp.properties().iterator();
Simon Hunt00a27ff2015-07-28 18:53:40 -0700111 for (String k: keys) {
112 Prop exp = PROP_MAP.get(k);
113 Prop act = iter.next();
114 assertEquals("Bad prop sequence", exp, act);
Simon Hunt629b99e2015-07-27 17:38:33 -0700115 }
116 }
117
Simon Hunt00a27ff2015-07-28 18:53:40 -0700118 private void validateProp(String key, String expValue) {
119 Iterator<Prop> iter = pp.properties().iterator();
120 Prop prop = null;
121 while (iter.hasNext()) {
122 Prop p = iter.next();
123 if (p.key().equals(key)) {
124 prop = p;
125 break;
126 }
127 }
128 if (prop == null) {
129 fail("no prop found with key: " + key);
130 }
131 assertEquals("Wrong prop value", expValue, prop.value());
132 }
133
Simon Hunt629b99e2015-07-27 17:38:33 -0700134 @Test
135 public void props() {
136 basic();
Simon Hunt00a27ff2015-07-28 18:53:40 -0700137 pp.addProp(KEY_A, VALUE_A)
138 .addProp(KEY_B, VALUE_B)
139 .addProp(KEY_C, VALUE_C);
Simon Hunt629b99e2015-07-27 17:38:33 -0700140 assertEquals("bad props", 3, pp.properties().size());
Simon Hunt00a27ff2015-07-28 18:53:40 -0700141 validateProps(KEY_A, KEY_B, KEY_C);
Simon Hunt629b99e2015-07-27 17:38:33 -0700142 }
143
144 @Test
Simon Hunt3a0598f2015-08-04 19:59:04 -0700145 public void separator() {
146 props();
147 pp.addSeparator()
148 .addProp(KEY_Z, VALUE_Z);
149
150 assertEquals("bad props", 5, pp.properties().size());
151 validateProps(KEY_A, KEY_B, KEY_C, SEP, KEY_Z);
152 }
153
154 @Test
Simon Hunt629b99e2015-07-27 17:38:33 -0700155 public void removeAllProps() {
156 props();
157 assertEquals("wrong props", 3, pp.properties().size());
158 pp.removeAllProps();
159 assertEquals("unexpected props", 0, pp.properties().size());
160 }
161
162 @Test
163 public void adjustProps() {
164 props();
Simon Hunt00a27ff2015-07-28 18:53:40 -0700165 pp.removeProps(KEY_B, KEY_A);
166 pp.addProp(KEY_Z, VALUE_Z);
167 validateProps(KEY_C, KEY_Z);
Simon Hunt629b99e2015-07-27 17:38:33 -0700168 }
Simon Hunt00a27ff2015-07-28 18:53:40 -0700169
170 @Test
171 public void intValues() {
172 basic();
173 pp.addProp(KEY_A, 200)
174 .addProp(KEY_B, 2000)
175 .addProp(KEY_C, 1234567);
176
177 validateProp(KEY_A, "200");
178 validateProp(KEY_B, "2,000");
179 validateProp(KEY_C, "1,234,567");
180 }
181
182 @Test
183 public void longValues() {
184 basic();
185 pp.addProp(KEY_A, 200L)
186 .addProp(KEY_B, 2000L)
187 .addProp(KEY_C, 1234567L)
188 .addProp(KEY_Z, Long.MAX_VALUE);
189
190 validateProp(KEY_A, "200");
191 validateProp(KEY_B, "2,000");
192 validateProp(KEY_C, "1,234,567");
193 validateProp(KEY_Z, "9,223,372,036,854,775,807");
194 }
195
196 @Test
197 public void objectValue() {
198 basic();
199 pp.addProp(KEY_A, new FooClass("a"))
200 .addProp(KEY_B, new FooClass("bxyyzy"), "[xz]");
201
202 validateProp(KEY_A, ">a<");
203 validateProp(KEY_B, ">byyy<");
204 }
205
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700206 private static final ButtonId BD_A = new ButtonId(KEY_A);
207 private static final ButtonId BD_B = new ButtonId(KEY_B);
208 private static final ButtonId BD_C = new ButtonId(KEY_C);
209 private static final ButtonId BD_Z = new ButtonId(KEY_Z);
Simon Hunt3a0598f2015-08-04 19:59:04 -0700210
211 private void verifyButtons(String... keys) {
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700212 Iterator<ButtonId> iter = pp.buttons().iterator();
Simon Hunt3a0598f2015-08-04 19:59:04 -0700213 for (String k: keys) {
214 assertEquals("wrong button", k, iter.next().id());
215 }
216 assertFalse("too many buttons", iter.hasNext());
217 }
218
219 @Test
220 public void buttons() {
221 basic();
222 pp.addButton(BD_A)
223 .addButton(BD_B);
224 assertEquals("wrong buttons", 2, pp.buttons().size());
225 verifyButtons(KEY_A, KEY_B);
226
227 pp.removeButtons(BD_B)
228 .addButton(BD_C)
229 .addButton(BD_Z);
230 assertEquals("wrong buttons", 3, pp.buttons().size());
231 verifyButtons(KEY_A, KEY_C, KEY_Z);
232
233 pp.removeAllButtons()
234 .addButton(BD_B);
235 assertEquals("wrong buttons", 1, pp.buttons().size());
236 verifyButtons(KEY_B);
237 }
Simon Hunt629b99e2015-07-27 17:38:33 -0700238}