Simon Hunt

GUI Topo -- added NodeBadge model object.

Change-Id: Ic6c04429f81162f6e4c15836b342d21864e2387d
/*
* 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;
/**
* Designates a badge to be applied to a node in the topology view.
*/
public final class NodeBadge {
private static final String EMPTY = "";
/** Designates the type of badge. */
public enum Type {
INFO("i"),
WARN("w"),
ERROR("e"),
CHECK_MARK("/"),
X_MARK("X"),
NUMBER("n");
private String code;
Type(String code) {
this.code = code;
}
@Override
public String toString() {
return "{" + code + "}";
}
/** Returns the type's code in string form. */
public String code() {
return code;
}
}
private final Type type;
private final String message;
// only instantiated through static methods.
private NodeBadge(Type type, String message) {
this.type = type;
this.message = message;
}
@Override
public String toString() {
return "{Badge " + type + " \"" + message + "\"}";
}
/**
* Returns the badge type.
*
* @return badge type
*/
public Type type() {
return type;
}
/**
* Returns the message associated with the badge.
*
* @return associated message
*/
public String message() {
return message;
}
private static String nonNull(String s) {
return s == null ? EMPTY : s;
}
/**
* Returns an informational badge, with associated message.
*
* @param message the message
* @return INFO type node badge
*/
public static NodeBadge info(String message) {
return new NodeBadge(Type.INFO, nonNull(message));
}
/**
* Returns a warning badge, with associated message.
*
* @param message the message
* @return WARN type node badge
*/
public static NodeBadge warn(String message) {
return new NodeBadge(Type.WARN, nonNull(message));
}
/**
* Returns an error badge, with associated message.
*
* @param message the message
* @return ERROR type node badge
*/
public static NodeBadge error(String message) {
return new NodeBadge(Type.ERROR, nonNull(message));
}
/**
* Returns a check-mark badge, with associated message.
*
* @param message the message
* @return CHECK_MARK type node badge
*/
public static NodeBadge checkMark(String message) {
return new NodeBadge(Type.CHECK_MARK, nonNull(message));
}
/**
* Returns an X-mark badge, with associated message.
*
* @param message the message
* @return X_MARK type node badge
*/
public static NodeBadge xMark(String message) {
return new NodeBadge(Type.X_MARK, nonNull(message));
}
/**
* Returns a numeric badge.
*
* @param n the number
* @return NUMBER type node badge
*/
public static NodeBadge number(int n) {
// TODO: consider constraints, e.g. 1 <= n <= 99
return new NodeBadge(Type.NUMBER, Integer.toString(n));
}
}
/*
* 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;
/**
* Unit tests for {@link NodeBadge}.
*/
public class NodeBadgeTest {
private static final String SOME_MSG = "a msg";
private static final String WR_T = "wrong type";
private static final String WR_M = "wrong message";
private static final String WR_SF = "wrong string format";
private NodeBadge badge;
private String expStr(String t) {
return "{Badge {" + t + "} \"" + SOME_MSG + "\"}";
}
private String expNumStr(String n) {
return "{Badge {n} \"" + n + "\"}";
}
@Test
public void info() {
badge = NodeBadge.info(SOME_MSG);
assertEquals(WR_T, NodeBadge.Type.INFO, badge.type());
assertEquals(WR_M, SOME_MSG, badge.message());
assertEquals(WR_SF, expStr("i"), badge.toString());
}
@Test
public void warn() {
badge = NodeBadge.warn(SOME_MSG);
assertEquals(WR_T, NodeBadge.Type.WARN, badge.type());
assertEquals(WR_M, SOME_MSG, badge.message());
assertEquals(WR_SF, expStr("w"), badge.toString());
}
@Test
public void error() {
badge = NodeBadge.error(SOME_MSG);
assertEquals(WR_T, NodeBadge.Type.ERROR, badge.type());
assertEquals(WR_M, SOME_MSG, badge.message());
assertEquals(WR_SF, expStr("e"), badge.toString());
}
@Test
public void checkMark() {
badge = NodeBadge.checkMark(SOME_MSG);
assertEquals(WR_T, NodeBadge.Type.CHECK_MARK, badge.type());
assertEquals(WR_M, SOME_MSG, badge.message());
assertEquals(WR_SF, expStr("/"), badge.toString());
}
@Test
public void xMark() {
badge = NodeBadge.xMark(SOME_MSG);
assertEquals(WR_T, NodeBadge.Type.X_MARK, badge.type());
assertEquals(WR_M, SOME_MSG, badge.message());
assertEquals(WR_SF, expStr("X"), badge.toString());
}
@Test
public void number0() {
badge = NodeBadge.number(0);
assertEquals(WR_T, NodeBadge.Type.NUMBER, badge.type());
assertEquals(WR_M, "0", badge.message());
assertEquals(WR_SF, expNumStr("0"), badge.toString());
}
@Test
public void number5() {
badge = NodeBadge.number(5);
assertEquals(WR_T, NodeBadge.Type.NUMBER, badge.type());
assertEquals(WR_M, "5", badge.message());
assertEquals(WR_SF, expNumStr("5"), badge.toString());
}
@Test
public void number99() {
badge = NodeBadge.number(99);
assertEquals(WR_T, NodeBadge.Type.NUMBER, badge.type());
assertEquals(WR_M, "99", badge.message());
assertEquals(WR_SF, expNumStr("99"), badge.toString());
}
@Test
public void badgeTypes() {
assertEquals(WR_SF, "i", NodeBadge.Type.INFO.code());
assertEquals(WR_SF, "w", NodeBadge.Type.WARN.code());
assertEquals(WR_SF, "e", NodeBadge.Type.ERROR.code());
assertEquals(WR_SF, "/", NodeBadge.Type.CHECK_MARK.code());
assertEquals(WR_SF, "X", NodeBadge.Type.X_MARK.code());
assertEquals(WR_SF, "n", NodeBadge.Type.NUMBER.code());
}
}