Simon Hunt
Committed by Gerrit Code Review

ONOS-2186 - GUI Topo Overlay - (WIP)

- added unit tests for BiLink, BiLinkMap, Mod, and LinkHighlight.

Change-Id: I247103af2713731b20564718064eeda03d030df7
......@@ -17,6 +17,8 @@
package org.onosproject.ui.topo;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Partial implementation of the highlighting to apply to topology
* view elements.
......@@ -32,8 +34,8 @@ public abstract class AbstractHighlight {
* @param elementId element identifier
*/
public AbstractHighlight(TopoElementType type, String elementId) {
this.type = type;
this.elementId = elementId;
this.type = checkNotNull(type);
this.elementId = checkNotNull(elementId);
}
/**
......
......@@ -45,7 +45,7 @@ public abstract class BiLinkMap<B extends BiLink> {
* @param link the initial link
* @return a new instance
*/
public abstract B create(LinkKey key, Link link);
protected abstract B create(LinkKey key, Link link);
/**
* Adds the given link to our collection, returning the corresponding
......
......@@ -17,7 +17,6 @@
package org.onosproject.ui.topo;
import java.text.DecimalFormat;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
......@@ -29,8 +28,6 @@ import java.util.Set;
*/
public class Highlights {
private static final DecimalFormat DF0 = new DecimalFormat("#,###");
private final Set<DeviceHighlight> devices = new HashSet<>();
private final Set<HostHighlight> hosts = new HashSet<>();
private final Set<LinkHighlight> links = new HashSet<>();
......
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.onosproject.ui.topo;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
/**
* Unit tests for {@link BiLinkMap}.
*/
public class BiLinkMapTest extends BiLinkTestBase {
private ConcreteLink clink;
private ConcreteLinkMap linkMap;
@Before
public void setUp() {
linkMap = new ConcreteLinkMap();
}
@Test
public void basic() {
assertEquals("wrong map size", 0, linkMap.size());
assertTrue("unexpected links", linkMap.biLinks().isEmpty());
}
@Test
public void addSameLinkTwice() {
linkMap.add(LINK_AB);
assertEquals("wrong map size", 1, linkMap.size());
clink = linkMap.biLinks().iterator().next();
assertEquals("wrong link one", LINK_AB, clink.one());
assertNull("unexpected link two", clink.two());
linkMap.add(LINK_AB);
assertEquals("wrong map size", 1, linkMap.size());
clink = linkMap.biLinks().iterator().next();
assertEquals("wrong link one", LINK_AB, clink.one());
assertNull("unexpected link two", clink.two());
}
@Test
public void addPairOfLinks() {
linkMap.add(LINK_AB);
assertEquals("wrong map size", 1, linkMap.size());
clink = linkMap.biLinks().iterator().next();
assertEquals("wrong link one", LINK_AB, clink.one());
assertNull("unexpected link two", clink.two());
linkMap.add(LINK_BA);
assertEquals("wrong map size", 1, linkMap.size());
clink = linkMap.biLinks().iterator().next();
assertEquals("wrong link one", LINK_AB, clink.one());
assertEquals("wrong link two", LINK_BA, clink.two());
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.onosproject.ui.topo;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
/**
* Unit tests for {@link BiLink}.
*/
public class BiLinkTest extends BiLinkTestBase {
private static final String EXP_ID_AB = "device-a/1-device-b/2";
private BiLink blink;
@Test
public void basic() {
blink = new ConcreteLink(KEY_AB, LINK_AB);
assertEquals("wrong id", EXP_ID_AB, blink.linkId());
assertEquals("wrong key", KEY_AB, blink.key());
assertEquals("wrong link one", LINK_AB, blink.one());
assertNull("what?", blink.two());
blink.setOther(LINK_BA);
assertEquals("wrong link two", LINK_BA, blink.two());
}
@Test(expected = NullPointerException.class)
public void nullKey() {
new ConcreteLink(null, LINK_AB);
}
@Test(expected = NullPointerException.class)
public void nullLink() {
new ConcreteLink(KEY_AB, null);
}
@Test(expected = NullPointerException.class)
public void nullOther() {
blink = new ConcreteLink(KEY_AB, LINK_AB);
blink.setOther(null);
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.onosproject.ui.topo;
import org.onosproject.net.Annotations;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.DeviceId;
import org.onosproject.net.Link;
import org.onosproject.net.LinkKey;
import org.onosproject.net.PortNumber;
import org.onosproject.net.provider.ProviderId;
/**
* Base class for unit tests of {@link BiLink} and {@link BiLinkMap}.
*/
public abstract class BiLinkTestBase {
protected static class FakeLink implements Link {
private final ConnectPoint src;
private final ConnectPoint dst;
FakeLink(ConnectPoint src, ConnectPoint dst) {
this.src = src;
this.dst = dst;
}
@Override public ConnectPoint src() {
return src;
}
@Override public ConnectPoint dst() {
return dst;
}
@Override public Type type() {
return null;
}
@Override public State state() {
return null;
}
@Override public boolean isDurable() {
return false;
}
@Override public Annotations annotations() {
return null;
}
@Override public ProviderId providerId() {
return null;
}
}
protected static final DeviceId DEV_A_ID = DeviceId.deviceId("device-A");
protected static final DeviceId DEV_B_ID = DeviceId.deviceId("device-B");
protected static final PortNumber PORT_1 = PortNumber.portNumber(1);
protected static final PortNumber PORT_2 = PortNumber.portNumber(2);
protected static final ConnectPoint CP_A1 = new ConnectPoint(DEV_A_ID, PORT_1);
protected static final ConnectPoint CP_B2 = new ConnectPoint(DEV_B_ID, PORT_2);
protected static final LinkKey KEY_AB = LinkKey.linkKey(CP_A1, CP_B2);
protected static final LinkKey KEY_BA = LinkKey.linkKey(CP_B2, CP_A1);
protected static final Link LINK_AB = new FakeLink(CP_A1, CP_B2);
protected static final Link LINK_BA = new FakeLink(CP_B2, CP_A1);
protected static class ConcreteLink extends BiLink {
public ConcreteLink(LinkKey key, Link link) {
super(key, link);
}
@Override
public LinkHighlight highlight(Enum<?> type) {
return null;
}
}
protected static class ConcreteLinkMap extends BiLinkMap<ConcreteLink> {
@Override
public ConcreteLink create(LinkKey key, Link link) {
return new ConcreteLink(key, link);
}
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.onosproject.ui.topo;
import org.junit.Test;
import java.util.Iterator;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.onosproject.ui.topo.LinkHighlight.Flavor.*;
/**
* Unit tests for {@link LinkHighlight}.
*/
public class LinkHighlightTest {
private static final String LINK_ID = "link-id-for-testing";
private static final String LABEL = "some label";
private static final String EMPTY = "";
private static final String CUSTOM = "custom";
private static final String ANIMATED = "animated";
private static final String OPTICAL = "optical";
private LinkHighlight lh;
@Test
public void basic() {
lh = new LinkHighlight(LINK_ID, NO_HIGHLIGHT);
assertEquals("wrong flavor", NO_HIGHLIGHT, lh.flavor());
assertTrue("unexpected mods", lh.mods().isEmpty());
assertEquals("wrong css", "plain", lh.cssClasses());
assertEquals("wrong label", EMPTY, lh.label());
}
@Test
public void primaryOptical() {
lh = new LinkHighlight(LINK_ID, PRIMARY_HIGHLIGHT)
.addMod(LinkHighlight.MOD_OPTICAL);
assertEquals("wrong flavor", PRIMARY_HIGHLIGHT, lh.flavor());
assertEquals("missing mod", 1, lh.mods().size());
Mod m = lh.mods().iterator().next();
assertEquals("wrong mod", LinkHighlight.MOD_OPTICAL, m);
assertEquals("wrong css", "primary optical", lh.cssClasses());
assertEquals("wrong label", EMPTY, lh.label());
}
@Test
public void secondaryAnimatedWithLabel() {
lh = new LinkHighlight(LINK_ID, SECONDARY_HIGHLIGHT)
.addMod(LinkHighlight.MOD_ANIMATED)
.setLabel(LABEL);
assertEquals("wrong flavor", SECONDARY_HIGHLIGHT, lh.flavor());
assertEquals("missing mod", 1, lh.mods().size());
Mod m = lh.mods().iterator().next();
assertEquals("wrong mod", LinkHighlight.MOD_ANIMATED, m);
assertEquals("wrong css", "secondary animated", lh.cssClasses());
assertEquals("wrong label", LABEL, lh.label());
}
@Test
public void customMod() {
lh = new LinkHighlight(LINK_ID, PRIMARY_HIGHLIGHT)
.addMod(new Mod(CUSTOM));
assertEquals("missing mod", 1, lh.mods().size());
Mod m = lh.mods().iterator().next();
assertEquals("wrong mod", CUSTOM, m.toString());
assertEquals("wrong css", "primary custom", lh.cssClasses());
}
@Test
public void severalMods() {
lh = new LinkHighlight(LINK_ID, SECONDARY_HIGHLIGHT)
.addMod(LinkHighlight.MOD_OPTICAL)
.addMod(LinkHighlight.MOD_ANIMATED)
.addMod(new Mod(CUSTOM));
assertEquals("missing mods", 3, lh.mods().size());
Iterator<Mod> iter = lh.mods().iterator();
// NOTE: we know we are using TreeSet as backing => sorted order
assertEquals("wrong mod", ANIMATED, iter.next().toString());
assertEquals("wrong mod", CUSTOM, iter.next().toString());
assertEquals("wrong mod", OPTICAL, iter.next().toString());
assertEquals("wrong css", "secondary animated custom optical", lh.cssClasses());
}
@Test(expected = NullPointerException.class)
public void noFlavor() {
new LinkHighlight(LINK_ID, null);
}
@Test(expected = NullPointerException.class)
public void noIdentity() {
new LinkHighlight(null, PRIMARY_HIGHLIGHT);
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.onosproject.ui.topo;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Unit tests for {@link Mod}.
*/
public class ModTest {
private static final String AAA = "aaa";
private static final String BBB = "bbb";
private Mod mod1;
private Mod mod2;
@Test(expected = NullPointerException.class)
public void nullId() {
new Mod(null);
}
@Test
public void basic() {
mod1 = new Mod(AAA);
assertEquals("wrong id", AAA, mod1.toString());
}
@Test
public void equivalence() {
mod1 = new Mod(AAA);
mod2 = new Mod(AAA);
assertNotSame("oops", mod1, mod2);
assertEquals("not equivalent", mod1, mod2);
}
@Test
public void comparable() {
mod1 = new Mod(AAA);
mod2 = new Mod(BBB);
assertNotEquals("what?", mod1, mod2);
assertTrue(mod1.compareTo(mod2) < 0);
assertTrue(mod2.compareTo(mod1) > 0);
}
}