blob: 752f28b582ab36f208afffca861237857f5ef180 [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.onosproject.event.Event;
Simon Hunt338a3b42016-04-14 09:43:52 -070022import org.onosproject.event.EventDispatcher;
Simon Huntc0f20c12016-05-09 09:30:20 -070023import org.onosproject.net.DeviceId;
Simon Hunt642bc452016-05-04 19:34:45 -070024import org.onosproject.ui.impl.topo.model.UiModelEvent.Type;
Simon Huntc0f20c12016-05-09 09:30:20 -070025import org.onosproject.ui.model.topo.UiClusterMember;
Simon Hunt642bc452016-05-04 19:34:45 -070026import org.onosproject.ui.model.topo.UiElement;
Simon Hunt338a3b42016-04-14 09:43:52 -070027
28import static org.junit.Assert.assertEquals;
Simon Huntc0f20c12016-05-09 09:30:20 -070029import static org.junit.Assert.assertFalse;
Simon Hunt642bc452016-05-04 19:34:45 -070030import static org.junit.Assert.assertNotNull;
Simon Huntc0f20c12016-05-09 09:30:20 -070031import static org.junit.Assert.assertTrue;
Simon Hunt642bc452016-05-04 19:34:45 -070032import static org.onosproject.cluster.NodeId.nodeId;
Simon Hunt338a3b42016-04-14 09:43:52 -070033
34/**
35 * Unit tests for {@link ModelCache}.
36 */
Simon Hunt642bc452016-05-04 19:34:45 -070037public class ModelCacheTest extends AbstractTopoModelTest {
Simon Hunt338a3b42016-04-14 09:43:52 -070038
Simon Hunt642bc452016-05-04 19:34:45 -070039 private class TestEvDisp implements EventDispatcher {
40
41 private Event<Type, UiElement> lastEvent = null;
42 private int eventCount = 0;
43
44 @Override
45 public void post(Event event) {
46 lastEvent = event;
47 eventCount++;
48// print("Event dispatched: %s", event);
49 }
50
51 private void assertEventCount(int exp) {
52 assertEquals("unex event count", exp, eventCount);
53 }
54
55 private void assertLast(Type expEventType, String expId) {
56 assertNotNull("no last event", lastEvent);
57 assertEquals("unex event type", expEventType, lastEvent.type());
58 assertEquals("unex element ID", expId, lastEvent.subject().idAsString());
59 }
60 }
61
Simon Hunt642bc452016-05-04 19:34:45 -070062
63 private final TestEvDisp dispatcher = new TestEvDisp();
Simon Hunt338a3b42016-04-14 09:43:52 -070064
65 private ModelCache cache;
66
Simon Hunt642bc452016-05-04 19:34:45 -070067 @Before
68 public void setUp() {
69 cache = new ModelCache(MOCK_SERVICES, dispatcher);
70 }
71
Simon Hunt338a3b42016-04-14 09:43:52 -070072 @Test
73 public void basic() {
Simon Hunt642bc452016-05-04 19:34:45 -070074 title("basic");
Simon Hunt338a3b42016-04-14 09:43:52 -070075 print(cache);
76 assertEquals("unex # members", 0, cache.clusterMemberCount());
77 assertEquals("unex # regions", 0, cache.regionCount());
78 }
79
Simon Hunt642bc452016-05-04 19:34:45 -070080 @Test
81 public void addAndRemoveClusterMember() {
82 title("addAndRemoveClusterMember");
83 print(cache);
84 assertEquals("unex # members", 0, cache.clusterMemberCount());
85 dispatcher.assertEventCount(0);
86
Simon Huntc0f20c12016-05-09 09:30:20 -070087 cache.addOrUpdateClusterMember(CNODE_1);
Simon Hunt642bc452016-05-04 19:34:45 -070088 print(cache);
89 assertEquals("unex # members", 1, cache.clusterMemberCount());
90 dispatcher.assertEventCount(1);
91 dispatcher.assertLast(Type.CLUSTER_MEMBER_ADDED_OR_UPDATED, C1);
92
Simon Huntc0f20c12016-05-09 09:30:20 -070093 cache.removeClusterMember(CNODE_1);
Simon Hunt642bc452016-05-04 19:34:45 -070094 print(cache);
95 assertEquals("unex # members", 0, cache.clusterMemberCount());
96 dispatcher.assertEventCount(2);
97 dispatcher.assertLast(Type.CLUSTER_MEMBER_REMOVED, C1);
98 }
99
100 @Test
101 public void createThreeNodeCluster() {
102 title("createThreeNodeCluster");
Simon Huntc0f20c12016-05-09 09:30:20 -0700103 cache.addOrUpdateClusterMember(CNODE_1);
Simon Hunt642bc452016-05-04 19:34:45 -0700104 dispatcher.assertLast(Type.CLUSTER_MEMBER_ADDED_OR_UPDATED, C1);
Simon Huntc0f20c12016-05-09 09:30:20 -0700105 cache.addOrUpdateClusterMember(CNODE_2);
Simon Hunt642bc452016-05-04 19:34:45 -0700106 dispatcher.assertLast(Type.CLUSTER_MEMBER_ADDED_OR_UPDATED, C2);
Simon Huntc0f20c12016-05-09 09:30:20 -0700107 cache.addOrUpdateClusterMember(CNODE_3);
Simon Hunt642bc452016-05-04 19:34:45 -0700108 dispatcher.assertLast(Type.CLUSTER_MEMBER_ADDED_OR_UPDATED, C3);
109 dispatcher.assertEventCount(3);
110 print(cache);
111 }
112
Simon Huntc0f20c12016-05-09 09:30:20 -0700113 @Test
114 public void addNodeThenExamineIt() {
115 title("addNodeThenExamineIt");
116 cache.addOrUpdateClusterMember(CNODE_1);
117 dispatcher.assertLast(Type.CLUSTER_MEMBER_ADDED_OR_UPDATED, C1);
118
119 UiClusterMember member = cache.accessClusterMember(nodeId(C1));
120 print(member);
121 // see AbstractUiImplTest Mock Environment for expected values...
122 assertEquals("wrong id str", C1, member.idAsString());
123 assertEquals("wrong id", nodeId(C1), member.id());
124 assertEquals("wrong dev count", 3, member.deviceCount());
125 assertEquals("not online", true, member.isOnline());
126 assertEquals("not ready", true, member.isReady());
127
128 assertMasterOf(member, DEVID_1, DEVID_2, DEVID_3);
129 assertNotMasterOf(member, DEVID_4, DEVID_6, DEVID_9);
130 }
131
132 private void assertMasterOf(UiClusterMember member, DeviceId... ids) {
133 for (DeviceId id : ids) {
134 assertTrue("not master of " + id, member.masterOf(id));
135 }
136 }
137
138 private void assertNotMasterOf(UiClusterMember member, DeviceId... ids) {
139 for (DeviceId id : ids) {
140 assertFalse("? master of " + id, member.masterOf(id));
141 }
142 }
143
144
145 @Test
146 public void addNodeAndDevices() {
147 title("addNodeAndDevices");
148 cache.addOrUpdateClusterMember(CNODE_1);
149 cache.addOrUpdateDevice(DEV_1);
150 cache.addOrUpdateDevice(DEV_2);
151 cache.addOrUpdateDevice(DEV_3);
152 print(cache);
153 }
154
155 @Test
156 public void addRegions() {
157 title("addRegions");
158 cache.addOrUpdateRegion(REGION_1);
159 print(cache);
160 }
161
162 @Test
163 public void load() {
164 title("load");
165 cache.load();
166 print(cache);
167 }
Simon Hunt338a3b42016-04-14 09:43:52 -0700168}