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.onlab.packet.IpAddress; |
| 22 | import org.onosproject.cluster.ControllerNode; |
| 23 | import org.onosproject.cluster.DefaultControllerNode; |
| 24 | import org.onosproject.event.Event; |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 25 | import org.onosproject.event.EventDispatcher; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 26 | import org.onosproject.ui.impl.topo.model.UiModelEvent.Type; |
| 27 | import org.onosproject.ui.model.topo.UiElement; |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 28 | |
| 29 | import static org.junit.Assert.assertEquals; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 30 | import static org.junit.Assert.assertNotNull; |
| 31 | import static org.onosproject.cluster.NodeId.nodeId; |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Unit tests for {@link ModelCache}. |
| 35 | */ |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 36 | public class ModelCacheTest extends AbstractTopoModelTest { |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 37 | |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 38 | 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 Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 79 | |
| 80 | private ModelCache cache; |
| 81 | |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 82 | @Before |
| 83 | public void setUp() { |
| 84 | cache = new ModelCache(MOCK_SERVICES, dispatcher); |
| 85 | } |
| 86 | |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 87 | @Test |
| 88 | public void basic() { |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 89 | title("basic"); |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 90 | print(cache); |
| 91 | assertEquals("unex # members", 0, cache.clusterMemberCount()); |
| 92 | assertEquals("unex # regions", 0, cache.regionCount()); |
| 93 | } |
| 94 | |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 95 | @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 Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 128 | } |