Add replication capability to Shard
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / test / java / org / opendaylight / controller / remote / rpc / registry / RoutingTableTest.java
1 /*
2  * Copyright (c) 2014 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.remote.rpc.registry;
10
11 import junit.framework.Assert;
12 import org.junit.Test;
13 import org.opendaylight.controller.remote.rpc.RouteIdentifierImpl;
14 import org.opendaylight.controller.sal.connector.api.RpcRouter;
15 import org.opendaylight.yangtools.yang.common.QName;
16
17 import java.net.URI;
18 import java.net.URISyntaxException;
19 import java.util.HashSet;
20 import java.util.Set;
21
22 public class RoutingTableTest {
23
24   private RoutingTable<RpcRouter.RouteIdentifier<?, ?, ?>, String> routingTable =
25       new RoutingTable<>();
26
27   @Test
28   public void addGlobalRouteNullRouteIdTest() {
29     try {
30       routingTable.addGlobalRoute(null, null);
31
32       Assert.fail("Null pointer exception was not thrown.");
33     } catch (Exception e) {
34       Assert.assertEquals(NullPointerException.class.getName(), e.getClass().getName());
35       Assert.assertEquals("addGlobalRoute: routeId cannot be null!", e.getMessage());
36     }
37   }
38
39   @Test
40   public void addGlobalRouteNullRouteTest() {
41     try {
42       RouteIdentifierImpl routeId = new RouteIdentifierImpl(null, null, null);
43       routingTable.addGlobalRoute(routeId, null);
44
45       Assert.fail("Null pointer exception was not thrown.");
46     } catch (Exception e) {
47       Assert.assertEquals(NullPointerException.class.getName(), e.getClass().getName());
48       Assert.assertEquals("addGlobalRoute: route cannot be null!", e.getMessage());
49     }
50   }
51
52   @Test
53   public void getGlobalRouteNullTest() {
54     try {
55       routingTable.getGlobalRoute(null);
56
57       Assert.fail("Null pointer exception was not thrown.");
58     } catch (Exception e) {
59       Assert.assertEquals(NullPointerException.class.getName(), e.getClass().getName());
60       Assert.assertEquals("getGlobalRoute: routeId cannot be null!", e.getMessage());
61     }
62   }
63
64   @Test
65   public void getGlobalRouteTest() throws URISyntaxException {
66     QName type = new QName(new URI("actor1"), "actor1");
67     RouteIdentifierImpl routeId = new RouteIdentifierImpl(null, type, null);
68     String route = "actor1";
69
70     routingTable.addGlobalRoute(routeId, route);
71
72     String returnedRoute = routingTable.getGlobalRoute(routeId);
73
74     Assert.assertEquals(route, returnedRoute);
75
76   }
77
78   @Test
79   public void removeGlobalRouteTest() throws URISyntaxException {
80     QName type = new QName(new URI("actorRemove"), "actorRemove");
81     RouteIdentifierImpl routeId = new RouteIdentifierImpl(null, type, null);
82     String route = "actorRemove";
83
84     routingTable.addGlobalRoute(routeId, route);
85
86     String returnedRoute = routingTable.getGlobalRoute(routeId);
87
88     Assert.assertEquals(route, returnedRoute);
89
90     routingTable.removeGlobalRoute(routeId);
91
92     String deletedRoute = routingTable.getGlobalRoute(routeId);
93
94     Assert.assertNull(deletedRoute);
95   }
96
97   @Test
98   public void addRoutedRpcNullRouteIdTest() {
99     try {
100       routingTable.addRoutedRpc(null, null);
101
102       Assert.fail("Null pointer exception was not thrown.");
103     } catch (Exception e) {
104       Assert.assertEquals(NullPointerException.class.getName(), e.getClass().getName());
105       Assert.assertEquals("addRoute: routeId cannot be null", e.getMessage());
106     }
107   }
108
109   @Test
110   public void addRoutedRpcNullRouteTest() {
111     try {
112       RouteIdentifierImpl routeId = new RouteIdentifierImpl(null, null, null);
113
114       routingTable.addRoutedRpc(routeId, null);
115
116       Assert.fail("Null pointer exception was not thrown.");
117     } catch (Exception e) {
118       Assert.assertEquals(NullPointerException.class.getName(), e.getClass().getName());
119       Assert.assertEquals("addRoute: route cannot be null", e.getMessage());
120     }
121   }
122
123   @Test
124   public void getRoutedRpcNullTest() {
125     try {
126       routingTable.getRoutedRpc(null);
127
128       Assert.fail("Null pointer exception was not thrown.");
129     } catch (Exception e) {
130       Assert.assertEquals(NullPointerException.class.getName(), e.getClass().getName());
131       Assert.assertEquals("getRoutes: routeId cannot be null!", e.getMessage());
132     }
133   }
134
135   @Test
136   public void getRoutedRpcTest() throws URISyntaxException {
137     QName type = new QName(new URI("actor1"), "actor1");
138     RouteIdentifierImpl routeId = new RouteIdentifierImpl(null, type, null);
139     String route = "actor1";
140
141     routingTable.addRoutedRpc(routeId, route);
142
143     Set<String> routes = routingTable.getRoutedRpc(routeId);
144
145     Assert.assertEquals(1, routes.size());
146     Assert.assertTrue(routes.contains(route));
147
148   }
149
150   @Test
151   public void getLastRoutedRpcTest() throws URISyntaxException {
152     QName type = new QName(new URI("first1"), "first1");
153     RouteIdentifierImpl routeId = new RouteIdentifierImpl(null, type, null);
154     String route = "first1";
155
156     routingTable.addRoutedRpc(routeId, route);
157
158     String route2 = "second1";
159     routingTable.addRoutedRpc(routeId, route2);
160
161     String latest = routingTable.getLastAddedRoutedRpc(routeId);
162     Assert.assertEquals(route2, latest);
163
164   }
165
166   @Test
167   public void removeRoutedRpcTest() throws URISyntaxException {
168     QName type = new QName(new URI("remove"), "remove");
169     RouteIdentifierImpl routeId = new RouteIdentifierImpl(null, type, null);
170     String route = "remove";
171     routingTable.addRoutedRpc(routeId, route);
172
173     String latest = routingTable.getLastAddedRoutedRpc(routeId);
174     Assert.assertEquals(route, latest);
175
176     routingTable.removeRoute(routeId, route);
177     String removed = routingTable.getLastAddedRoutedRpc(routeId);
178     Assert.assertNull(removed);
179   }
180
181   @Test
182   public void removeRoutedRpcsTest() throws URISyntaxException {
183     QName type = new QName(new URI("remove1"), "remove1");
184     RouteIdentifierImpl routeId = new RouteIdentifierImpl(null, type, null);
185
186     QName type2 = new QName(new URI("remove2"), "remove2");
187     RouteIdentifierImpl routeId2 = new RouteIdentifierImpl(null, type2, null);
188
189     Set<RpcRouter.RouteIdentifier<?, ?, ?>> routeIds = new HashSet<>();
190     routeIds.add(routeId);
191     routeIds.add(routeId2);
192     String route = "remove1";
193
194     routingTable.addRoutedRpcs(routeIds, route);
195     String latest1 = routingTable.getLastAddedRoutedRpc(routeId);
196     Assert.assertEquals(route, latest1);
197
198     String latest2 = routingTable.getLastAddedRoutedRpc(routeId2);
199     Assert.assertEquals(route, latest2);
200
201     routingTable.removeRoutes(routeIds, route);
202     String removed1 = routingTable.getLastAddedRoutedRpc(routeId);
203     Assert.assertNull(removed1);
204
205     String removed2 = routingTable.getLastAddedRoutedRpc(routeId2);
206     Assert.assertNull(removed2);
207   }
208
209 }