Simplify method isMutualExclusive in Subnet.
[controller.git] / opendaylight / md-sal / remoterpc-routingtable / implementation / src / test / java / org / opendaylight / controller / sal / connector / remoterpc / impl / RoutingTableImplTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.sal.connector.remoterpc.impl;
10
11 import junit.framework.Assert;
12 import org.apache.felix.dm.Component;
13 import org.junit.Test;
14 import org.opendaylight.controller.clustering.services.IClusterGlobalServices;
15 import org.opendaylight.controller.clustering.services.IClusterServices;
16 import org.opendaylight.controller.sal.connector.api.RpcRouter;
17 import org.opendaylight.controller.sal.connector.remoterpc.api.RouteChangeListener;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
20
21 import java.net.URI;
22 import java.util.EnumSet;
23 import java.util.HashSet;
24 import java.util.Set;
25 import java.util.concurrent.ConcurrentMap;
26
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
29
30 /**
31  * @author: syedbahm
32  */
33 public class RoutingTableImplTest {
34
35     private IClusterGlobalServices ics =  mock(IClusterGlobalServices.class);
36     private RoutingTableImpl rti = new RoutingTableImpl();
37
38     private final URI namespace = URI.create("http://cisco.com/example");
39     private final QName QNAME = new QName(namespace,"global");
40
41     ConcurrentMap concurrentMapMock = mock(ConcurrentMap.class);
42
43
44     @Test
45     public void testAddGlobalRoute() throws Exception {
46         ConcurrentMap concurrentMap = createRoutingTableCache();
47
48         Assert.assertNotNull(concurrentMap);
49         RpcRouter.RouteIdentifier<QName, QName, InstanceIdentifier> routeIdentifier =  mock(RpcRouter.RouteIdentifier.class);
50         InstanceIdentifier identifier = mock(InstanceIdentifier.class);
51         when(routeIdentifier.getType()).thenReturn(QNAME);
52         when(routeIdentifier.getRoute()).thenReturn(identifier);
53
54         rti.addGlobalRoute(routeIdentifier, "172.27.12.1:5000");
55
56         Set<String> globalService = new HashSet<String>();
57         globalService.add("172.27.12.1:5000");
58
59         when(concurrentMap.get(routeIdentifier)).thenReturn(globalService);
60         ConcurrentMap latestCache = rti.getRoutingTableCache();
61
62         Assert.assertEquals(concurrentMap,latestCache);
63
64         Set<String> servicesGlobal = (Set<String>)latestCache.get(routeIdentifier);
65         Assert.assertEquals(servicesGlobal.size(),1);
66
67         Assert.assertEquals(servicesGlobal.iterator().next(),"172.27.12.1:5000");
68
69     }
70
71     @Test
72     public void testGetRoutes() throws Exception {
73         ConcurrentMap concurrentMap = createRoutingTableCache();
74
75         Assert.assertNotNull(concurrentMap);
76         RpcRouter.RouteIdentifier<QName, QName, InstanceIdentifier> routeIdentifier =  mock(RpcRouter.RouteIdentifier.class);
77         InstanceIdentifier identifier = mock(InstanceIdentifier.class);
78         when(routeIdentifier.getContext()).thenReturn(QNAME);
79         when(routeIdentifier.getRoute()).thenReturn(identifier);
80
81         rti.addGlobalRoute(routeIdentifier, "172.27.12.1:5000");
82
83         Set<String> globalService = new HashSet<String>();
84         globalService.add("172.27.12.1:5000");
85
86         when(concurrentMap.get(routeIdentifier)).thenReturn(globalService);
87         ConcurrentMap latestCache = rti.getRoutingTableCache();
88
89         Assert.assertEquals(concurrentMap,latestCache);
90
91         Set<String> servicesGlobal =  rti.getRoutes(routeIdentifier);
92
93
94         Assert.assertEquals(servicesGlobal.size(),1);
95
96         Assert.assertEquals(servicesGlobal.iterator().next(),"172.27.12.1:5000");
97
98
99
100     }
101     @Test
102     public void testRegisterRouteChangeListener() throws Exception {
103         Assert.assertEquals(rti.getRegisteredRouteChangeListeners().size(),0);
104         rti.registerRouteChangeListener(new RouteChangeListenerImpl());
105
106         Assert.assertEquals(rti.getRegisteredRouteChangeListeners().size(),0); //old should not work
107         //what about the new approach - using whiteboard pattern
108         rti.setRouteChangeListener(new RouteChangeListenerImpl());
109
110         Assert.assertEquals(rti.getRegisteredRouteChangeListeners().size(),1); //should not work
111
112
113     }
114     @Test
115     public void testRemoveGlobalRoute()throws Exception {
116
117         ConcurrentMap concurrentMap = createRoutingTableCache();
118
119         Assert.assertNotNull(concurrentMap);
120         RpcRouter.RouteIdentifier<QName, QName, InstanceIdentifier> routeIdentifier =  mock(RpcRouter.RouteIdentifier.class);
121         InstanceIdentifier identifier = mock(InstanceIdentifier.class);
122         when(routeIdentifier.getContext()).thenReturn(QNAME);
123         when(routeIdentifier.getRoute()).thenReturn(identifier);
124
125         rti.addGlobalRoute(routeIdentifier, "172.27.12.1:5000");
126
127         Set<String> globalService = new HashSet<String>();
128         globalService.add("172.27.12.1:5000");
129
130         when(concurrentMap.get(routeIdentifier)).thenReturn(globalService);
131         ConcurrentMap latestCache = rti.getRoutingTableCache();
132
133         Assert.assertEquals(concurrentMap,latestCache);
134
135         Set<String> servicesGlobal =  rti.getRoutes(routeIdentifier);
136
137
138         Assert.assertEquals(servicesGlobal.size(),1);
139
140         Assert.assertEquals(servicesGlobal.iterator().next(),"172.27.12.1:5000");
141
142         rti.removeGlobalRoute(routeIdentifier);
143
144         Assert.assertNotNull(rti.getRoutes(routeIdentifier));
145
146
147     }
148
149     private ConcurrentMap createRoutingTableCache() throws Exception {
150
151         //here init
152         Component c = mock(Component.class);
153
154         when(ics.existCache(
155                 RoutingTableImpl.ROUTING_TABLE_GLOBAL_CACHE)).thenReturn(false);
156
157         when(ics.createCache(RoutingTableImpl.ROUTING_TABLE_GLOBAL_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(concurrentMapMock);
158          rti.setClusterGlobalServices(this.ics);
159         rti.init(c);
160
161         Assert.assertEquals(concurrentMapMock,rti.getRoutingTableCache() );
162         return concurrentMapMock;
163
164     }
165
166
167     @Test
168     public void testCreateRoutingTableCacheReturnExistingCache() throws Exception {
169         ConcurrentMap concurrentMap = createRoutingTableCache();
170
171         //OK here we should try creating again the cache but this time it should return the existing one
172         when(ics.existCache(
173                 RoutingTableImpl.ROUTING_TABLE_GLOBAL_CACHE)).thenReturn(true);
174
175         when(ics.getCache(
176                 RoutingTableImpl.ROUTING_TABLE_GLOBAL_CACHE)).thenReturn(concurrentMap);
177
178
179         //here init
180         Component c = mock(Component.class);
181
182         rti.init(c);
183
184         Assert.assertEquals(concurrentMap,rti.getRoutingTableCache());
185
186
187
188
189
190     }
191
192     private class RouteChangeListenerImpl<I,R> implements RouteChangeListener<I,R>{
193
194         @Override
195         public void onRouteUpdated(I key, R new_value) {
196             //To change body of implemented methods use File | Settings | File Templates.
197         }
198
199         @Override
200         public void onRouteDeleted(I key) {
201             //To change body of implemented methods use File | Settings | File Templates.
202         }
203     }
204
205 }