Merge "AbstractConfigTest - exposed BundleContext and ServiceRegistration mock."
[controller.git] / opendaylight / md-sal / zeromq-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(),1);
107
108     }
109     @Test
110     public void testRemoveGlobalRoute()throws Exception {
111
112         ConcurrentMap concurrentMap = createRoutingTableCache();
113
114         Assert.assertNotNull(concurrentMap);
115         RpcRouter.RouteIdentifier<QName, QName, InstanceIdentifier> routeIdentifier =  mock(RpcRouter.RouteIdentifier.class);
116         InstanceIdentifier identifier = mock(InstanceIdentifier.class);
117         when(routeIdentifier.getContext()).thenReturn(QNAME);
118         when(routeIdentifier.getRoute()).thenReturn(identifier);
119
120         rti.addGlobalRoute(routeIdentifier, "172.27.12.1:5000");
121
122         Set<String> globalService = new HashSet<String>();
123         globalService.add("172.27.12.1:5000");
124
125         when(concurrentMap.get(routeIdentifier)).thenReturn(globalService);
126         ConcurrentMap latestCache = rti.getRoutingTableCache();
127
128         Assert.assertEquals(concurrentMap,latestCache);
129
130         Set<String> servicesGlobal =  rti.getRoutes(routeIdentifier);
131
132
133         Assert.assertEquals(servicesGlobal.size(),1);
134
135         Assert.assertEquals(servicesGlobal.iterator().next(),"172.27.12.1:5000");
136
137         rti.removeGlobalRoute(routeIdentifier);
138
139         Assert.assertNotNull(rti.getRoutes(routeIdentifier));
140
141
142     }
143
144     private ConcurrentMap createRoutingTableCache() throws Exception {
145
146         //here init
147         Component c = mock(Component.class);
148
149         when(ics.existCache(
150                 RoutingTableImpl.ROUTING_TABLE_GLOBAL_CACHE)).thenReturn(false);
151
152         when(ics.createCache(RoutingTableImpl.ROUTING_TABLE_GLOBAL_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(concurrentMapMock);
153          rti.setClusterGlobalServices(this.ics);
154         rti.init(c);
155
156         Assert.assertEquals(concurrentMapMock,rti.getRoutingTableCache() );
157         return concurrentMapMock;
158
159     }
160
161
162     @Test
163     public void testCreateRoutingTableCacheReturnExistingCache() throws Exception {
164         ConcurrentMap concurrentMap = createRoutingTableCache();
165
166         //OK here we should try creating again the cache but this time it should return the existing one
167         when(ics.existCache(
168                 RoutingTableImpl.ROUTING_TABLE_GLOBAL_CACHE)).thenReturn(true);
169
170         when(ics.getCache(
171                 RoutingTableImpl.ROUTING_TABLE_GLOBAL_CACHE)).thenReturn(concurrentMap);
172
173
174         //here init
175         Component c = mock(Component.class);
176
177         rti.init(c);
178
179         Assert.assertEquals(concurrentMap,rti.getRoutingTableCache());
180
181
182
183
184
185     }
186
187     private class RouteChangeListenerImpl<I,R> implements RouteChangeListener<I,R>{
188
189         @Override
190         public void onRouteUpdated(I key, R new_value) {
191             //To change body of implemented methods use File | Settings | File Templates.
192         }
193
194         @Override
195         public void onRouteDeleted(I key) {
196             //To change body of implemented methods use File | Settings | File Templates.
197         }
198     }
199
200 }