Simon Hunt

GUI Topo -- added NodeBadge model object.

Change-Id: Ic6c04429f81162f6e4c15836b342d21864e2387d
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 +package org.onosproject.ui.topo;
18 +
19 +/**
20 + * Designates a badge to be applied to a node in the topology view.
21 + */
22 +public final class NodeBadge {
23 +
24 + private static final String EMPTY = "";
25 +
26 + /** Designates the type of badge. */
27 + public enum Type {
28 + INFO("i"),
29 + WARN("w"),
30 + ERROR("e"),
31 + CHECK_MARK("/"),
32 + X_MARK("X"),
33 + NUMBER("n");
34 +
35 + private String code;
36 +
37 + Type(String code) {
38 + this.code = code;
39 + }
40 +
41 + @Override
42 + public String toString() {
43 + return "{" + code + "}";
44 + }
45 +
46 + /** Returns the type's code in string form. */
47 + public String code() {
48 + return code;
49 + }
50 + }
51 +
52 + private final Type type;
53 + private final String message;
54 +
55 + // only instantiated through static methods.
56 + private NodeBadge(Type type, String message) {
57 + this.type = type;
58 + this.message = message;
59 + }
60 +
61 + @Override
62 + public String toString() {
63 + return "{Badge " + type + " \"" + message + "\"}";
64 + }
65 +
66 + /**
67 + * Returns the badge type.
68 + *
69 + * @return badge type
70 + */
71 + public Type type() {
72 + return type;
73 + }
74 +
75 + /**
76 + * Returns the message associated with the badge.
77 + *
78 + * @return associated message
79 + */
80 + public String message() {
81 + return message;
82 + }
83 +
84 + private static String nonNull(String s) {
85 + return s == null ? EMPTY : s;
86 + }
87 +
88 + /**
89 + * Returns an informational badge, with associated message.
90 + *
91 + * @param message the message
92 + * @return INFO type node badge
93 + */
94 + public static NodeBadge info(String message) {
95 + return new NodeBadge(Type.INFO, nonNull(message));
96 + }
97 +
98 + /**
99 + * Returns a warning badge, with associated message.
100 + *
101 + * @param message the message
102 + * @return WARN type node badge
103 + */
104 + public static NodeBadge warn(String message) {
105 + return new NodeBadge(Type.WARN, nonNull(message));
106 + }
107 +
108 + /**
109 + * Returns an error badge, with associated message.
110 + *
111 + * @param message the message
112 + * @return ERROR type node badge
113 + */
114 + public static NodeBadge error(String message) {
115 + return new NodeBadge(Type.ERROR, nonNull(message));
116 + }
117 +
118 + /**
119 + * Returns a check-mark badge, with associated message.
120 + *
121 + * @param message the message
122 + * @return CHECK_MARK type node badge
123 + */
124 + public static NodeBadge checkMark(String message) {
125 + return new NodeBadge(Type.CHECK_MARK, nonNull(message));
126 + }
127 +
128 + /**
129 + * Returns an X-mark badge, with associated message.
130 + *
131 + * @param message the message
132 + * @return X_MARK type node badge
133 + */
134 + public static NodeBadge xMark(String message) {
135 + return new NodeBadge(Type.X_MARK, nonNull(message));
136 + }
137 +
138 + /**
139 + * Returns a numeric badge.
140 + *
141 + * @param n the number
142 + * @return NUMBER type node badge
143 + */
144 + public static NodeBadge number(int n) {
145 + // TODO: consider constraints, e.g. 1 <= n <= 99
146 + return new NodeBadge(Type.NUMBER, Integer.toString(n));
147 + }
148 +
149 +}
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 +package org.onosproject.ui.topo;
18 +
19 +import org.junit.Test;
20 +
21 +import static org.junit.Assert.assertEquals;
22 +
23 +/**
24 + * Unit tests for {@link NodeBadge}.
25 + */
26 +public class NodeBadgeTest {
27 +
28 + private static final String SOME_MSG = "a msg";
29 + private static final String WR_T = "wrong type";
30 + private static final String WR_M = "wrong message";
31 + private static final String WR_SF = "wrong string format";
32 +
33 + private NodeBadge badge;
34 +
35 + private String expStr(String t) {
36 + return "{Badge {" + t + "} \"" + SOME_MSG + "\"}";
37 + }
38 +
39 + private String expNumStr(String n) {
40 + return "{Badge {n} \"" + n + "\"}";
41 + }
42 +
43 + @Test
44 + public void info() {
45 + badge = NodeBadge.info(SOME_MSG);
46 + assertEquals(WR_T, NodeBadge.Type.INFO, badge.type());
47 + assertEquals(WR_M, SOME_MSG, badge.message());
48 + assertEquals(WR_SF, expStr("i"), badge.toString());
49 + }
50 +
51 + @Test
52 + public void warn() {
53 + badge = NodeBadge.warn(SOME_MSG);
54 + assertEquals(WR_T, NodeBadge.Type.WARN, badge.type());
55 + assertEquals(WR_M, SOME_MSG, badge.message());
56 + assertEquals(WR_SF, expStr("w"), badge.toString());
57 + }
58 +
59 + @Test
60 + public void error() {
61 + badge = NodeBadge.error(SOME_MSG);
62 + assertEquals(WR_T, NodeBadge.Type.ERROR, badge.type());
63 + assertEquals(WR_M, SOME_MSG, badge.message());
64 + assertEquals(WR_SF, expStr("e"), badge.toString());
65 + }
66 +
67 + @Test
68 + public void checkMark() {
69 + badge = NodeBadge.checkMark(SOME_MSG);
70 + assertEquals(WR_T, NodeBadge.Type.CHECK_MARK, badge.type());
71 + assertEquals(WR_M, SOME_MSG, badge.message());
72 + assertEquals(WR_SF, expStr("/"), badge.toString());
73 + }
74 +
75 + @Test
76 + public void xMark() {
77 + badge = NodeBadge.xMark(SOME_MSG);
78 + assertEquals(WR_T, NodeBadge.Type.X_MARK, badge.type());
79 + assertEquals(WR_M, SOME_MSG, badge.message());
80 + assertEquals(WR_SF, expStr("X"), badge.toString());
81 + }
82 +
83 + @Test
84 + public void number0() {
85 + badge = NodeBadge.number(0);
86 + assertEquals(WR_T, NodeBadge.Type.NUMBER, badge.type());
87 + assertEquals(WR_M, "0", badge.message());
88 + assertEquals(WR_SF, expNumStr("0"), badge.toString());
89 + }
90 +
91 + @Test
92 + public void number5() {
93 + badge = NodeBadge.number(5);
94 + assertEquals(WR_T, NodeBadge.Type.NUMBER, badge.type());
95 + assertEquals(WR_M, "5", badge.message());
96 + assertEquals(WR_SF, expNumStr("5"), badge.toString());
97 + }
98 +
99 + @Test
100 + public void number99() {
101 + badge = NodeBadge.number(99);
102 + assertEquals(WR_T, NodeBadge.Type.NUMBER, badge.type());
103 + assertEquals(WR_M, "99", badge.message());
104 + assertEquals(WR_SF, expNumStr("99"), badge.toString());
105 + }
106 +
107 + @Test
108 + public void badgeTypes() {
109 + assertEquals(WR_SF, "i", NodeBadge.Type.INFO.code());
110 + assertEquals(WR_SF, "w", NodeBadge.Type.WARN.code());
111 + assertEquals(WR_SF, "e", NodeBadge.Type.ERROR.code());
112 + assertEquals(WR_SF, "/", NodeBadge.Type.CHECK_MARK.code());
113 + assertEquals(WR_SF, "X", NodeBadge.Type.X_MARK.code());
114 + assertEquals(WR_SF, "n", NodeBadge.Type.NUMBER.code());
115 + }
116 +}