blob: 7ca9ce55e0c89be95eedb23baf4d97b6f2fed118 [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
28import static org.junit.Assert.assertEquals;
Simon Hunt00a27ff2015-07-28 18:53:40 -070029import static org.junit.Assert.assertNull;
30import static org.junit.Assert.fail;
Simon Hunt629b99e2015-07-27 17:38:33 -070031
32/**
33 * Unit tests for {@link PropertyPanel}.
34 */
35public class PropertyPanelTest {
36
37 private static final String TITLE_ORIG = "Original Title";
38 private static final String TYPE_ORIG = "Original type ID";
39 private static final String TITLE_NEW = "New Title";
40 private static final String TYPE_NEW = "New type";
Simon Hunt00a27ff2015-07-28 18:53:40 -070041 private static final String SOME_IDENTIFICATION = "It's Me!";
Simon Hunt629b99e2015-07-27 17:38:33 -070042
Simon Hunt00a27ff2015-07-28 18:53:40 -070043 private static final String KEY_A = "A";
44 private static final String KEY_B = "B";
45 private static final String KEY_C = "C";
46 private static final String KEY_Z = "Z";
47 private static final String VALUE_A = "Hay";
48 private static final String VALUE_B = "Bee";
49 private static final String VALUE_C = "Sea";
50 private static final String VALUE_Z = "Zed";
51
52 private static final Map<String, Prop> PROP_MAP = new HashMap<>();
53
54 private static class FooClass {
55 private final String s;
56 FooClass(String s) {
57 this.s = s;
58 }
59
60 @Override
61 public String toString() {
62 return ">" + s + "<";
63 }
64 }
Simon Hunt629b99e2015-07-27 17:38:33 -070065
66 private PropertyPanel pp;
67
68
Simon Hunt00a27ff2015-07-28 18:53:40 -070069
70 @BeforeClass
71 public static void setUpClass() {
72 PROP_MAP.put(KEY_A, new Prop(KEY_A, VALUE_A));
73 PROP_MAP.put(KEY_B, new Prop(KEY_B, VALUE_B));
74 PROP_MAP.put(KEY_C, new Prop(KEY_C, VALUE_C));
75 PROP_MAP.put(KEY_Z, new Prop(KEY_Z, VALUE_Z));
76 }
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());
85 }
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
144 public void removeAllProps() {
145 props();
146 assertEquals("wrong props", 3, pp.properties().size());
147 pp.removeAllProps();
148 assertEquals("unexpected props", 0, pp.properties().size());
149 }
150
151 @Test
152 public void adjustProps() {
153 props();
Simon Hunt00a27ff2015-07-28 18:53:40 -0700154 pp.removeProps(KEY_B, KEY_A);
155 pp.addProp(KEY_Z, VALUE_Z);
156 validateProps(KEY_C, KEY_Z);
Simon Hunt629b99e2015-07-27 17:38:33 -0700157 }
Simon Hunt00a27ff2015-07-28 18:53:40 -0700158
159 @Test
160 public void intValues() {
161 basic();
162 pp.addProp(KEY_A, 200)
163 .addProp(KEY_B, 2000)
164 .addProp(KEY_C, 1234567);
165
166 validateProp(KEY_A, "200");
167 validateProp(KEY_B, "2,000");
168 validateProp(KEY_C, "1,234,567");
169 }
170
171 @Test
172 public void longValues() {
173 basic();
174 pp.addProp(KEY_A, 200L)
175 .addProp(KEY_B, 2000L)
176 .addProp(KEY_C, 1234567L)
177 .addProp(KEY_Z, Long.MAX_VALUE);
178
179 validateProp(KEY_A, "200");
180 validateProp(KEY_B, "2,000");
181 validateProp(KEY_C, "1,234,567");
182 validateProp(KEY_Z, "9,223,372,036,854,775,807");
183 }
184
185 @Test
186 public void objectValue() {
187 basic();
188 pp.addProp(KEY_A, new FooClass("a"))
189 .addProp(KEY_B, new FooClass("bxyyzy"), "[xz]");
190
191 validateProp(KEY_A, ">a<");
192 validateProp(KEY_B, ">byyy<");
193 }
194
Simon Hunt629b99e2015-07-27 17:38:33 -0700195}