Merge "Changed codec for Identityref in JSON transformation"
[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.Iterator;
25 import java.util.Set;
26 import java.util.concurrent.ConcurrentMap;
27
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.when;
30
31 /**
32  * @author: syedbahm
33  */
34 public class RoutingTableImplTest {
35
36     private IClusterGlobalServices ics =  mock(IClusterGlobalServices.class);
37     private RoutingTableImpl rti = new RoutingTableImpl();
38
39     private final URI namespace = URI.create("http://cisco.com/example");
40     private final QName QNAME = new QName(namespace,"global");
41
42     ConcurrentMap concurrentMapMock = mock(ConcurrentMap.class);
43
44
45     @Test
46     public void testAddGlobalRoute() throws Exception {
47         ConcurrentMap concurrentMap = createRoutingTableCache();
48
49         Assert.assertNotNull(concurrentMap);
50         RpcRouter.RouteIdentifier<QName, QName, InstanceIdentifier> routeIdentifier =  mock(RpcRouter.RouteIdentifier.class);
51         InstanceIdentifier identifier = mock(InstanceIdentifier.class);
52         when(routeIdentifier.getType()).thenReturn(QNAME);
53         when(routeIdentifier.getRoute()).thenReturn(identifier);
54
55         rti.addGlobalRoute(routeIdentifier, "172.27.12.1:5000");
56
57         Set<String> globalService = new HashSet<String>();
58         globalService.add("172.27.12.1:5000");
59
60         when(concurrentMap.get(routeIdentifier)).thenReturn(globalService);
61         ConcurrentMap latestCache = rti.getRoutingTableCache();
62
63         Assert.assertEquals(concurrentMap,latestCache);
64
65         Set<String> servicesGlobal = (Set<String>)latestCache.get(routeIdentifier);
66         Assert.assertEquals(servicesGlobal.size(),1);
67
68         Assert.assertEquals(servicesGlobal.iterator().next(),"172.27.12.1:5000");
69
70     }
71
72     @Test
73     public void testGetRoutes() throws Exception {
74         ConcurrentMap concurrentMap = createRoutingTableCache();
75
76         Assert.assertNotNull(concurrentMap);
77         RpcRouter.RouteIdentifier<QName, QName, InstanceIdentifier> routeIdentifier =  mock(RpcRouter.RouteIdentifier.class);
78         InstanceIdentifier identifier = mock(InstanceIdentifier.class);
79         when(routeIdentifier.getContext()).thenReturn(QNAME);
80         when(routeIdentifier.getRoute()).thenReturn(identifier);
81
82         rti.addGlobalRoute(routeIdentifier, "172.27.12.1:5000");
83
84         String globalService =   "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         Iterator<String> iterator = servicesGlobal.iterator();
96         while(iterator.hasNext()){
97         Assert.assertEquals(iterator.next(),"172.27.12.1:5000");
98         }
99
100
101     }
102     @Test
103     public void testRegisterRouteChangeListener() throws Exception {
104         Assert.assertEquals(rti.getRegisteredRouteChangeListeners().size(),0);
105         rti.registerRouteChangeListener(new RouteChangeListenerImpl());
106
107         Assert.assertEquals(rti.getRegisteredRouteChangeListeners().size(),0); //old should not work
108         //what about the new approach - using whiteboard pattern
109         rti.setRouteChangeListener(new RouteChangeListenerImpl());
110
111         Assert.assertEquals(rti.getRegisteredRouteChangeListeners().size(),1); //should not work
112
113
114     }
115     @Test
116     public void testRemoveGlobalRoute()throws Exception {
117
118         ConcurrentMap concurrentMap = createRoutingTableCache();
119
120         Assert.assertNotNull(concurrentMap);
121         RpcRouter.RouteIdentifier<QName, QName, InstanceIdentifier> routeIdentifier =  mock(RpcRouter.RouteIdentifier.class);
122         InstanceIdentifier identifier = mock(InstanceIdentifier.class);
123         when(routeIdentifier.getContext()).thenReturn(QNAME);
124         when(routeIdentifier.getRoute()).thenReturn(identifier);
125
126         rti.addGlobalRoute(routeIdentifier, "172.27.12.1:5000");
127
128         String globalService =   "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 }