2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.remote.rpc.registry;
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;
18 import java.net.URISyntaxException;
19 import java.util.HashSet;
22 public class RoutingTableTest {
24 private RoutingTable<RpcRouter.RouteIdentifier<?, ?, ?>, String> routingTable =
28 public void addGlobalRouteNullRouteIdTest() {
30 routingTable.addGlobalRoute(null, null);
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());
40 public void addGlobalRouteNullRouteTest() {
42 RouteIdentifierImpl routeId = new RouteIdentifierImpl(null, null, null);
43 routingTable.addGlobalRoute(routeId, null);
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());
53 public void getGlobalRouteNullTest() {
55 routingTable.getGlobalRoute(null);
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());
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";
70 routingTable.addGlobalRoute(routeId, route);
72 String returnedRoute = routingTable.getGlobalRoute(routeId);
74 Assert.assertEquals(route, returnedRoute);
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";
84 routingTable.addGlobalRoute(routeId, route);
86 String returnedRoute = routingTable.getGlobalRoute(routeId);
88 Assert.assertEquals(route, returnedRoute);
90 routingTable.removeGlobalRoute(routeId);
92 String deletedRoute = routingTable.getGlobalRoute(routeId);
94 Assert.assertNull(deletedRoute);
98 public void addRoutedRpcNullRouteIdTest() {
100 routingTable.addRoutedRpc(null, null);
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());
110 public void addRoutedRpcNullRouteTest() {
112 RouteIdentifierImpl routeId = new RouteIdentifierImpl(null, null, null);
114 routingTable.addRoutedRpc(routeId, null);
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());
124 public void getRoutedRpcNullTest() {
126 routingTable.getRoutedRpc(null);
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());
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";
141 routingTable.addRoutedRpc(routeId, route);
143 Set<String> routes = routingTable.getRoutedRpc(routeId);
145 Assert.assertEquals(1, routes.size());
146 Assert.assertTrue(routes.contains(route));
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";
156 routingTable.addRoutedRpc(routeId, route);
158 String route2 = "second1";
159 routingTable.addRoutedRpc(routeId, route2);
161 String latest = routingTable.getLastAddedRoutedRpc(routeId);
162 Assert.assertEquals(route2, latest);
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);
173 String latest = routingTable.getLastAddedRoutedRpc(routeId);
174 Assert.assertEquals(route, latest);
176 routingTable.removeRoute(routeId, route);
177 String removed = routingTable.getLastAddedRoutedRpc(routeId);
178 Assert.assertNull(removed);
182 public void removeRoutedRpcsTest() throws URISyntaxException {
183 QName type = new QName(new URI("remove1"), "remove1");
184 RouteIdentifierImpl routeId = new RouteIdentifierImpl(null, type, null);
186 QName type2 = new QName(new URI("remove2"), "remove2");
187 RouteIdentifierImpl routeId2 = new RouteIdentifierImpl(null, type2, null);
189 Set<RpcRouter.RouteIdentifier<?, ?, ?>> routeIds = new HashSet<>();
190 routeIds.add(routeId);
191 routeIds.add(routeId2);
192 String route = "remove1";
194 routingTable.addRoutedRpcs(routeIds, route);
195 String latest1 = routingTable.getLastAddedRoutedRpc(routeId);
196 Assert.assertEquals(route, latest1);
198 String latest2 = routingTable.getLastAddedRoutedRpc(routeId2);
199 Assert.assertEquals(route, latest2);
201 routingTable.removeRoutes(routeIds, route);
202 String removed1 = routingTable.getLastAddedRoutedRpc(routeId);
203 Assert.assertNull(removed1);
205 String removed2 = routingTable.getLastAddedRoutedRpc(routeId2);
206 Assert.assertNull(removed2);