Committed by
Gerrit Code Review
ONOS-2186 - GUI Topo Overlay - (WIP)
- added unit tests for BiLink, BiLinkMap, Mod, and LinkHighlight. Change-Id: I247103af2713731b20564718064eeda03d030df7
Showing
8 changed files
with
419 additions
and
6 deletions
... | @@ -17,6 +17,8 @@ | ... | @@ -17,6 +17,8 @@ |
17 | 17 | ||
18 | package org.onosproject.ui.topo; | 18 | package org.onosproject.ui.topo; |
19 | 19 | ||
20 | +import static com.google.common.base.Preconditions.checkNotNull; | ||
21 | + | ||
20 | /** | 22 | /** |
21 | * Partial implementation of the highlighting to apply to topology | 23 | * Partial implementation of the highlighting to apply to topology |
22 | * view elements. | 24 | * view elements. |
... | @@ -32,8 +34,8 @@ public abstract class AbstractHighlight { | ... | @@ -32,8 +34,8 @@ public abstract class AbstractHighlight { |
32 | * @param elementId element identifier | 34 | * @param elementId element identifier |
33 | */ | 35 | */ |
34 | public AbstractHighlight(TopoElementType type, String elementId) { | 36 | public AbstractHighlight(TopoElementType type, String elementId) { |
35 | - this.type = type; | 37 | + this.type = checkNotNull(type); |
36 | - this.elementId = elementId; | 38 | + this.elementId = checkNotNull(elementId); |
37 | } | 39 | } |
38 | 40 | ||
39 | /** | 41 | /** | ... | ... |
... | @@ -45,7 +45,7 @@ public abstract class BiLinkMap<B extends BiLink> { | ... | @@ -45,7 +45,7 @@ public abstract class BiLinkMap<B extends BiLink> { |
45 | * @param link the initial link | 45 | * @param link the initial link |
46 | * @return a new instance | 46 | * @return a new instance |
47 | */ | 47 | */ |
48 | - public abstract B create(LinkKey key, Link link); | 48 | + protected abstract B create(LinkKey key, Link link); |
49 | 49 | ||
50 | /** | 50 | /** |
51 | * Adds the given link to our collection, returning the corresponding | 51 | * Adds the given link to our collection, returning the corresponding | ... | ... |
... | @@ -17,7 +17,6 @@ | ... | @@ -17,7 +17,6 @@ |
17 | 17 | ||
18 | package org.onosproject.ui.topo; | 18 | package org.onosproject.ui.topo; |
19 | 19 | ||
20 | -import java.text.DecimalFormat; | ||
21 | import java.util.Collections; | 20 | import java.util.Collections; |
22 | import java.util.HashSet; | 21 | import java.util.HashSet; |
23 | import java.util.Set; | 22 | import java.util.Set; |
... | @@ -29,8 +28,6 @@ import java.util.Set; | ... | @@ -29,8 +28,6 @@ import java.util.Set; |
29 | */ | 28 | */ |
30 | public class Highlights { | 29 | public class Highlights { |
31 | 30 | ||
32 | - private static final DecimalFormat DF0 = new DecimalFormat("#,###"); | ||
33 | - | ||
34 | private final Set<DeviceHighlight> devices = new HashSet<>(); | 31 | private final Set<DeviceHighlight> devices = new HashSet<>(); |
35 | private final Set<HostHighlight> hosts = new HashSet<>(); | 32 | private final Set<HostHighlight> hosts = new HashSet<>(); |
36 | private final Set<LinkHighlight> links = new HashSet<>(); | 33 | private final Set<LinkHighlight> links = new HashSet<>(); | ... | ... |
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 | + | ||
20 | +import org.junit.Before; | ||
21 | +import org.junit.Test; | ||
22 | + | ||
23 | +import static org.junit.Assert.assertEquals; | ||
24 | +import static org.junit.Assert.assertNull; | ||
25 | +import static org.junit.Assert.assertTrue; | ||
26 | + | ||
27 | +/** | ||
28 | + * Unit tests for {@link BiLinkMap}. | ||
29 | + */ | ||
30 | +public class BiLinkMapTest extends BiLinkTestBase { | ||
31 | + | ||
32 | + | ||
33 | + private ConcreteLink clink; | ||
34 | + private ConcreteLinkMap linkMap; | ||
35 | + | ||
36 | + @Before | ||
37 | + public void setUp() { | ||
38 | + linkMap = new ConcreteLinkMap(); | ||
39 | + } | ||
40 | + | ||
41 | + @Test | ||
42 | + public void basic() { | ||
43 | + assertEquals("wrong map size", 0, linkMap.size()); | ||
44 | + assertTrue("unexpected links", linkMap.biLinks().isEmpty()); | ||
45 | + } | ||
46 | + | ||
47 | + @Test | ||
48 | + public void addSameLinkTwice() { | ||
49 | + linkMap.add(LINK_AB); | ||
50 | + assertEquals("wrong map size", 1, linkMap.size()); | ||
51 | + clink = linkMap.biLinks().iterator().next(); | ||
52 | + assertEquals("wrong link one", LINK_AB, clink.one()); | ||
53 | + assertNull("unexpected link two", clink.two()); | ||
54 | + | ||
55 | + linkMap.add(LINK_AB); | ||
56 | + assertEquals("wrong map size", 1, linkMap.size()); | ||
57 | + clink = linkMap.biLinks().iterator().next(); | ||
58 | + assertEquals("wrong link one", LINK_AB, clink.one()); | ||
59 | + assertNull("unexpected link two", clink.two()); | ||
60 | + } | ||
61 | + | ||
62 | + @Test | ||
63 | + public void addPairOfLinks() { | ||
64 | + linkMap.add(LINK_AB); | ||
65 | + assertEquals("wrong map size", 1, linkMap.size()); | ||
66 | + clink = linkMap.biLinks().iterator().next(); | ||
67 | + assertEquals("wrong link one", LINK_AB, clink.one()); | ||
68 | + assertNull("unexpected link two", clink.two()); | ||
69 | + | ||
70 | + linkMap.add(LINK_BA); | ||
71 | + assertEquals("wrong map size", 1, linkMap.size()); | ||
72 | + clink = linkMap.biLinks().iterator().next(); | ||
73 | + assertEquals("wrong link one", LINK_AB, clink.one()); | ||
74 | + assertEquals("wrong link two", LINK_BA, clink.two()); | ||
75 | + } | ||
76 | +} |
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 | + | ||
20 | +import org.junit.Test; | ||
21 | + | ||
22 | +import static org.junit.Assert.assertEquals; | ||
23 | +import static org.junit.Assert.assertNull; | ||
24 | + | ||
25 | +/** | ||
26 | + * Unit tests for {@link BiLink}. | ||
27 | + */ | ||
28 | +public class BiLinkTest extends BiLinkTestBase { | ||
29 | + | ||
30 | + private static final String EXP_ID_AB = "device-a/1-device-b/2"; | ||
31 | + | ||
32 | + private BiLink blink; | ||
33 | + | ||
34 | + @Test | ||
35 | + public void basic() { | ||
36 | + blink = new ConcreteLink(KEY_AB, LINK_AB); | ||
37 | + assertEquals("wrong id", EXP_ID_AB, blink.linkId()); | ||
38 | + assertEquals("wrong key", KEY_AB, blink.key()); | ||
39 | + assertEquals("wrong link one", LINK_AB, blink.one()); | ||
40 | + assertNull("what?", blink.two()); | ||
41 | + | ||
42 | + blink.setOther(LINK_BA); | ||
43 | + assertEquals("wrong link two", LINK_BA, blink.two()); | ||
44 | + } | ||
45 | + | ||
46 | + @Test(expected = NullPointerException.class) | ||
47 | + public void nullKey() { | ||
48 | + new ConcreteLink(null, LINK_AB); | ||
49 | + } | ||
50 | + | ||
51 | + @Test(expected = NullPointerException.class) | ||
52 | + public void nullLink() { | ||
53 | + new ConcreteLink(KEY_AB, null); | ||
54 | + } | ||
55 | + | ||
56 | + @Test(expected = NullPointerException.class) | ||
57 | + public void nullOther() { | ||
58 | + blink = new ConcreteLink(KEY_AB, LINK_AB); | ||
59 | + blink.setOther(null); | ||
60 | + } | ||
61 | +} | ||
62 | + |
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 | + | ||
20 | +import org.onosproject.net.Annotations; | ||
21 | +import org.onosproject.net.ConnectPoint; | ||
22 | +import org.onosproject.net.DeviceId; | ||
23 | +import org.onosproject.net.Link; | ||
24 | +import org.onosproject.net.LinkKey; | ||
25 | +import org.onosproject.net.PortNumber; | ||
26 | +import org.onosproject.net.provider.ProviderId; | ||
27 | + | ||
28 | +/** | ||
29 | + * Base class for unit tests of {@link BiLink} and {@link BiLinkMap}. | ||
30 | + */ | ||
31 | +public abstract class BiLinkTestBase { | ||
32 | + | ||
33 | + protected static class FakeLink implements Link { | ||
34 | + private final ConnectPoint src; | ||
35 | + private final ConnectPoint dst; | ||
36 | + | ||
37 | + FakeLink(ConnectPoint src, ConnectPoint dst) { | ||
38 | + this.src = src; | ||
39 | + this.dst = dst; | ||
40 | + } | ||
41 | + | ||
42 | + @Override public ConnectPoint src() { | ||
43 | + return src; | ||
44 | + } | ||
45 | + @Override public ConnectPoint dst() { | ||
46 | + return dst; | ||
47 | + } | ||
48 | + | ||
49 | + @Override public Type type() { | ||
50 | + return null; | ||
51 | + } | ||
52 | + @Override public State state() { | ||
53 | + return null; | ||
54 | + } | ||
55 | + @Override public boolean isDurable() { | ||
56 | + return false; | ||
57 | + } | ||
58 | + @Override public Annotations annotations() { | ||
59 | + return null; | ||
60 | + } | ||
61 | + @Override public ProviderId providerId() { | ||
62 | + return null; | ||
63 | + } | ||
64 | + } | ||
65 | + | ||
66 | + protected static final DeviceId DEV_A_ID = DeviceId.deviceId("device-A"); | ||
67 | + protected static final DeviceId DEV_B_ID = DeviceId.deviceId("device-B"); | ||
68 | + protected static final PortNumber PORT_1 = PortNumber.portNumber(1); | ||
69 | + protected static final PortNumber PORT_2 = PortNumber.portNumber(2); | ||
70 | + | ||
71 | + protected static final ConnectPoint CP_A1 = new ConnectPoint(DEV_A_ID, PORT_1); | ||
72 | + protected static final ConnectPoint CP_B2 = new ConnectPoint(DEV_B_ID, PORT_2); | ||
73 | + | ||
74 | + protected static final LinkKey KEY_AB = LinkKey.linkKey(CP_A1, CP_B2); | ||
75 | + protected static final LinkKey KEY_BA = LinkKey.linkKey(CP_B2, CP_A1); | ||
76 | + | ||
77 | + protected static final Link LINK_AB = new FakeLink(CP_A1, CP_B2); | ||
78 | + protected static final Link LINK_BA = new FakeLink(CP_B2, CP_A1); | ||
79 | + | ||
80 | + protected static class ConcreteLink extends BiLink { | ||
81 | + public ConcreteLink(LinkKey key, Link link) { | ||
82 | + super(key, link); | ||
83 | + } | ||
84 | + @Override | ||
85 | + public LinkHighlight highlight(Enum<?> type) { | ||
86 | + return null; | ||
87 | + } | ||
88 | + } | ||
89 | + | ||
90 | + protected static class ConcreteLinkMap extends BiLinkMap<ConcreteLink> { | ||
91 | + @Override | ||
92 | + public ConcreteLink create(LinkKey key, Link link) { | ||
93 | + return new ConcreteLink(key, link); | ||
94 | + } | ||
95 | + } | ||
96 | + | ||
97 | + | ||
98 | +} |
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 | + | ||
20 | +import org.junit.Test; | ||
21 | + | ||
22 | +import java.util.Iterator; | ||
23 | + | ||
24 | +import static org.junit.Assert.assertEquals; | ||
25 | +import static org.junit.Assert.assertTrue; | ||
26 | +import static org.onosproject.ui.topo.LinkHighlight.Flavor.*; | ||
27 | + | ||
28 | +/** | ||
29 | + * Unit tests for {@link LinkHighlight}. | ||
30 | + */ | ||
31 | +public class LinkHighlightTest { | ||
32 | + | ||
33 | + private static final String LINK_ID = "link-id-for-testing"; | ||
34 | + private static final String LABEL = "some label"; | ||
35 | + private static final String EMPTY = ""; | ||
36 | + private static final String CUSTOM = "custom"; | ||
37 | + private static final String ANIMATED = "animated"; | ||
38 | + private static final String OPTICAL = "optical"; | ||
39 | + | ||
40 | + private LinkHighlight lh; | ||
41 | + | ||
42 | + @Test | ||
43 | + public void basic() { | ||
44 | + lh = new LinkHighlight(LINK_ID, NO_HIGHLIGHT); | ||
45 | + | ||
46 | + assertEquals("wrong flavor", NO_HIGHLIGHT, lh.flavor()); | ||
47 | + assertTrue("unexpected mods", lh.mods().isEmpty()); | ||
48 | + assertEquals("wrong css", "plain", lh.cssClasses()); | ||
49 | + assertEquals("wrong label", EMPTY, lh.label()); | ||
50 | + } | ||
51 | + | ||
52 | + @Test | ||
53 | + public void primaryOptical() { | ||
54 | + lh = new LinkHighlight(LINK_ID, PRIMARY_HIGHLIGHT) | ||
55 | + .addMod(LinkHighlight.MOD_OPTICAL); | ||
56 | + | ||
57 | + assertEquals("wrong flavor", PRIMARY_HIGHLIGHT, lh.flavor()); | ||
58 | + assertEquals("missing mod", 1, lh.mods().size()); | ||
59 | + Mod m = lh.mods().iterator().next(); | ||
60 | + assertEquals("wrong mod", LinkHighlight.MOD_OPTICAL, m); | ||
61 | + assertEquals("wrong css", "primary optical", lh.cssClasses()); | ||
62 | + assertEquals("wrong label", EMPTY, lh.label()); | ||
63 | + } | ||
64 | + | ||
65 | + @Test | ||
66 | + public void secondaryAnimatedWithLabel() { | ||
67 | + lh = new LinkHighlight(LINK_ID, SECONDARY_HIGHLIGHT) | ||
68 | + .addMod(LinkHighlight.MOD_ANIMATED) | ||
69 | + .setLabel(LABEL); | ||
70 | + | ||
71 | + assertEquals("wrong flavor", SECONDARY_HIGHLIGHT, lh.flavor()); | ||
72 | + assertEquals("missing mod", 1, lh.mods().size()); | ||
73 | + Mod m = lh.mods().iterator().next(); | ||
74 | + assertEquals("wrong mod", LinkHighlight.MOD_ANIMATED, m); | ||
75 | + assertEquals("wrong css", "secondary animated", lh.cssClasses()); | ||
76 | + assertEquals("wrong label", LABEL, lh.label()); | ||
77 | + } | ||
78 | + | ||
79 | + @Test | ||
80 | + public void customMod() { | ||
81 | + lh = new LinkHighlight(LINK_ID, PRIMARY_HIGHLIGHT) | ||
82 | + .addMod(new Mod(CUSTOM)); | ||
83 | + | ||
84 | + assertEquals("missing mod", 1, lh.mods().size()); | ||
85 | + Mod m = lh.mods().iterator().next(); | ||
86 | + assertEquals("wrong mod", CUSTOM, m.toString()); | ||
87 | + assertEquals("wrong css", "primary custom", lh.cssClasses()); | ||
88 | + } | ||
89 | + | ||
90 | + @Test | ||
91 | + public void severalMods() { | ||
92 | + lh = new LinkHighlight(LINK_ID, SECONDARY_HIGHLIGHT) | ||
93 | + .addMod(LinkHighlight.MOD_OPTICAL) | ||
94 | + .addMod(LinkHighlight.MOD_ANIMATED) | ||
95 | + .addMod(new Mod(CUSTOM)); | ||
96 | + | ||
97 | + assertEquals("missing mods", 3, lh.mods().size()); | ||
98 | + Iterator<Mod> iter = lh.mods().iterator(); | ||
99 | + // NOTE: we know we are using TreeSet as backing => sorted order | ||
100 | + assertEquals("wrong mod", ANIMATED, iter.next().toString()); | ||
101 | + assertEquals("wrong mod", CUSTOM, iter.next().toString()); | ||
102 | + assertEquals("wrong mod", OPTICAL, iter.next().toString()); | ||
103 | + assertEquals("wrong css", "secondary animated custom optical", lh.cssClasses()); | ||
104 | + } | ||
105 | + | ||
106 | + @Test(expected = NullPointerException.class) | ||
107 | + public void noFlavor() { | ||
108 | + new LinkHighlight(LINK_ID, null); | ||
109 | + } | ||
110 | + | ||
111 | + @Test(expected = NullPointerException.class) | ||
112 | + public void noIdentity() { | ||
113 | + new LinkHighlight(null, PRIMARY_HIGHLIGHT); | ||
114 | + } | ||
115 | + | ||
116 | +} |
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 | + | ||
20 | +import org.junit.Test; | ||
21 | + | ||
22 | +import static org.junit.Assert.*; | ||
23 | + | ||
24 | +/** | ||
25 | + * Unit tests for {@link Mod}. | ||
26 | + */ | ||
27 | +public class ModTest { | ||
28 | + | ||
29 | + private static final String AAA = "aaa"; | ||
30 | + private static final String BBB = "bbb"; | ||
31 | + | ||
32 | + private Mod mod1; | ||
33 | + private Mod mod2; | ||
34 | + | ||
35 | + @Test(expected = NullPointerException.class) | ||
36 | + public void nullId() { | ||
37 | + new Mod(null); | ||
38 | + } | ||
39 | + | ||
40 | + @Test | ||
41 | + public void basic() { | ||
42 | + mod1 = new Mod(AAA); | ||
43 | + assertEquals("wrong id", AAA, mod1.toString()); | ||
44 | + } | ||
45 | + | ||
46 | + @Test | ||
47 | + public void equivalence() { | ||
48 | + mod1 = new Mod(AAA); | ||
49 | + mod2 = new Mod(AAA); | ||
50 | + assertNotSame("oops", mod1, mod2); | ||
51 | + assertEquals("not equivalent", mod1, mod2); | ||
52 | + } | ||
53 | + | ||
54 | + @Test | ||
55 | + public void comparable() { | ||
56 | + mod1 = new Mod(AAA); | ||
57 | + mod2 = new Mod(BBB); | ||
58 | + assertNotEquals("what?", mod1, mod2); | ||
59 | + assertTrue(mod1.compareTo(mod2) < 0); | ||
60 | + assertTrue(mod2.compareTo(mod1) > 0); | ||
61 | + } | ||
62 | +} |
-
Please register or login to post a comment