Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 18 | package org.onosproject.ui.topo; |
| 19 | |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame^] | 20 | import org.junit.BeforeClass; |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 21 | import org.junit.Test; |
| 22 | import org.onosproject.ui.topo.PropertyPanel.Prop; |
| 23 | |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame^] | 24 | import java.util.HashMap; |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 25 | import java.util.Iterator; |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame^] | 26 | import java.util.Map; |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 27 | |
| 28 | import static org.junit.Assert.assertEquals; |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame^] | 29 | import static org.junit.Assert.assertNull; |
| 30 | import static org.junit.Assert.fail; |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * Unit tests for {@link PropertyPanel}. |
| 34 | */ |
| 35 | public 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 Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame^] | 41 | private static final String SOME_IDENTIFICATION = "It's Me!"; |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 42 | |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame^] | 43 | 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 Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 65 | |
| 66 | private PropertyPanel pp; |
| 67 | |
| 68 | |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame^] | 69 | |
| 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 Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 78 | @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 Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame^] | 83 | assertNull("id?", pp.id()); |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 84 | 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 Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame^] | 101 | @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 Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 109 | Iterator<Prop> iter = pp.properties().iterator(); |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame^] | 110 | for (String k: keys) { |
| 111 | Prop exp = PROP_MAP.get(k); |
| 112 | Prop act = iter.next(); |
| 113 | assertEquals("Bad prop sequence", exp, act); |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame^] | 117 | 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 Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 133 | @Test |
| 134 | public void props() { |
| 135 | basic(); |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame^] | 136 | pp.addProp(KEY_A, VALUE_A) |
| 137 | .addProp(KEY_B, VALUE_B) |
| 138 | .addProp(KEY_C, VALUE_C); |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 139 | assertEquals("bad props", 3, pp.properties().size()); |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame^] | 140 | validateProps(KEY_A, KEY_B, KEY_C); |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 141 | } |
| 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 Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame^] | 154 | pp.removeProps(KEY_B, KEY_A); |
| 155 | pp.addProp(KEY_Z, VALUE_Z); |
| 156 | validateProps(KEY_C, KEY_Z); |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 157 | } |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame^] | 158 | |
| 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 Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 195 | } |