Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.ui.impl.topo.model; |
| 18 | |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 19 | import org.junit.Before; |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 20 | import org.junit.Test; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 21 | import org.onosproject.event.Event; |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 22 | import org.onosproject.event.EventDispatcher; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 23 | import org.onosproject.net.DeviceId; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 24 | import org.onosproject.ui.impl.topo.model.UiModelEvent.Type; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 25 | import org.onosproject.ui.model.topo.UiClusterMember; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 26 | import org.onosproject.ui.model.topo.UiElement; |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 27 | |
| 28 | import static org.junit.Assert.assertEquals; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 29 | import static org.junit.Assert.assertFalse; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 30 | import static org.junit.Assert.assertNotNull; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 31 | import static org.junit.Assert.assertTrue; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 32 | import static org.onosproject.cluster.NodeId.nodeId; |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Unit tests for {@link ModelCache}. |
| 36 | */ |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 37 | public class ModelCacheTest extends AbstractTopoModelTest { |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 38 | |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 39 | 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 Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 62 | |
| 63 | private final TestEvDisp dispatcher = new TestEvDisp(); |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 64 | |
| 65 | private ModelCache cache; |
| 66 | |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 67 | @Before |
| 68 | public void setUp() { |
| 69 | cache = new ModelCache(MOCK_SERVICES, dispatcher); |
| 70 | } |
| 71 | |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 72 | @Test |
| 73 | public void basic() { |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 74 | title("basic"); |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 75 | print(cache); |
| 76 | assertEquals("unex # members", 0, cache.clusterMemberCount()); |
| 77 | assertEquals("unex # regions", 0, cache.regionCount()); |
| 78 | } |
| 79 | |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 80 | @Test |
| 81 | public void addAndRemoveClusterMember() { |
| 82 | title("addAndRemoveClusterMember"); |
| 83 | print(cache); |
| 84 | assertEquals("unex # members", 0, cache.clusterMemberCount()); |
| 85 | dispatcher.assertEventCount(0); |
| 86 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 87 | cache.addOrUpdateClusterMember(CNODE_1); |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 88 | 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 Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 93 | cache.removeClusterMember(CNODE_1); |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 94 | 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 Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 103 | cache.addOrUpdateClusterMember(CNODE_1); |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 104 | dispatcher.assertLast(Type.CLUSTER_MEMBER_ADDED_OR_UPDATED, C1); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 105 | cache.addOrUpdateClusterMember(CNODE_2); |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 106 | dispatcher.assertLast(Type.CLUSTER_MEMBER_ADDED_OR_UPDATED, C2); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 107 | cache.addOrUpdateClusterMember(CNODE_3); |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 108 | dispatcher.assertLast(Type.CLUSTER_MEMBER_ADDED_OR_UPDATED, C3); |
| 109 | dispatcher.assertEventCount(3); |
| 110 | print(cache); |
| 111 | } |
| 112 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 113 | @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 Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 168 | } |