blob: c2720878938708a6e6b4240968e26d2d50bd05e9 [file] [log] [blame]
Simon Hunt338a3b42016-04-14 09:43:52 -07001/*
2 * Copyright 2016-present 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
17package org.onosproject.ui.impl.topo.model;
18
Simon Hunt642bc452016-05-04 19:34:45 -070019import org.junit.Before;
Simon Hunt338a3b42016-04-14 09:43:52 -070020import org.junit.Test;
Simon Hunt642bc452016-05-04 19:34:45 -070021import org.onlab.packet.IpAddress;
22import org.onosproject.cluster.ControllerNode;
23import org.onosproject.cluster.DefaultControllerNode;
24import org.onosproject.event.Event;
Simon Hunt338a3b42016-04-14 09:43:52 -070025import org.onosproject.event.EventDispatcher;
Simon Hunt642bc452016-05-04 19:34:45 -070026import org.onosproject.ui.impl.topo.model.UiModelEvent.Type;
27import org.onosproject.ui.model.topo.UiElement;
Simon Hunt338a3b42016-04-14 09:43:52 -070028
29import static org.junit.Assert.assertEquals;
Simon Hunt642bc452016-05-04 19:34:45 -070030import static org.junit.Assert.assertNotNull;
31import static org.onosproject.cluster.NodeId.nodeId;
Simon Hunt338a3b42016-04-14 09:43:52 -070032
33/**
34 * Unit tests for {@link ModelCache}.
35 */
Simon Hunt642bc452016-05-04 19:34:45 -070036public class ModelCacheTest extends AbstractTopoModelTest {
Simon Hunt338a3b42016-04-14 09:43:52 -070037
Simon Hunt642bc452016-05-04 19:34:45 -070038 private class TestEvDisp implements EventDispatcher {
39
40 private Event<Type, UiElement> lastEvent = null;
41 private int eventCount = 0;
42
43 @Override
44 public void post(Event event) {
45 lastEvent = event;
46 eventCount++;
47// print("Event dispatched: %s", event);
48 }
49
50 private void assertEventCount(int exp) {
51 assertEquals("unex event count", exp, eventCount);
52 }
53
54 private void assertLast(Type expEventType, String expId) {
55 assertNotNull("no last event", lastEvent);
56 assertEquals("unex event type", expEventType, lastEvent.type());
57 assertEquals("unex element ID", expId, lastEvent.subject().idAsString());
58 }
59 }
60
61 private static IpAddress ip(String s) {
62 return IpAddress.valueOf(s);
63 }
64
65 private static ControllerNode cnode(String id, String ip) {
66 return new DefaultControllerNode(nodeId(id), ip(ip));
67 }
68
69 private static final String C1 = "C1";
70 private static final String C2 = "C2";
71 private static final String C3 = "C3";
72
73 private static final ControllerNode NODE_1 = cnode(C1, "10.0.0.1");
74 private static final ControllerNode NODE_2 = cnode(C2, "10.0.0.2");
75 private static final ControllerNode NODE_3 = cnode(C3, "10.0.0.3");
76
77
78 private final TestEvDisp dispatcher = new TestEvDisp();
Simon Hunt338a3b42016-04-14 09:43:52 -070079
80 private ModelCache cache;
81
Simon Hunt642bc452016-05-04 19:34:45 -070082 @Before
83 public void setUp() {
84 cache = new ModelCache(MOCK_SERVICES, dispatcher);
85 }
86
Simon Hunt338a3b42016-04-14 09:43:52 -070087 @Test
88 public void basic() {
Simon Hunt642bc452016-05-04 19:34:45 -070089 title("basic");
Simon Hunt338a3b42016-04-14 09:43:52 -070090 print(cache);
91 assertEquals("unex # members", 0, cache.clusterMemberCount());
92 assertEquals("unex # regions", 0, cache.regionCount());
93 }
94
Simon Hunt642bc452016-05-04 19:34:45 -070095 @Test
96 public void addAndRemoveClusterMember() {
97 title("addAndRemoveClusterMember");
98 print(cache);
99 assertEquals("unex # members", 0, cache.clusterMemberCount());
100 dispatcher.assertEventCount(0);
101
102 cache.addOrUpdateClusterMember(NODE_1);
103 print(cache);
104 assertEquals("unex # members", 1, cache.clusterMemberCount());
105 dispatcher.assertEventCount(1);
106 dispatcher.assertLast(Type.CLUSTER_MEMBER_ADDED_OR_UPDATED, C1);
107
108 cache.removeClusterMember(NODE_1);
109 print(cache);
110 assertEquals("unex # members", 0, cache.clusterMemberCount());
111 dispatcher.assertEventCount(2);
112 dispatcher.assertLast(Type.CLUSTER_MEMBER_REMOVED, C1);
113 }
114
115 @Test
116 public void createThreeNodeCluster() {
117 title("createThreeNodeCluster");
118 cache.addOrUpdateClusterMember(NODE_1);
119 dispatcher.assertLast(Type.CLUSTER_MEMBER_ADDED_OR_UPDATED, C1);
120 cache.addOrUpdateClusterMember(NODE_2);
121 dispatcher.assertLast(Type.CLUSTER_MEMBER_ADDED_OR_UPDATED, C2);
122 cache.addOrUpdateClusterMember(NODE_3);
123 dispatcher.assertLast(Type.CLUSTER_MEMBER_ADDED_OR_UPDATED, C3);
124 dispatcher.assertEventCount(3);
125 print(cache);
126 }
127
Simon Hunt338a3b42016-04-14 09:43:52 -0700128}