2 * Copyright (C) 2015 Red Hat, Inc.
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
10 package org.opendaylight.ovsdb.northbound;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertSame;
15 import static org.junit.Assert.fail;
16 import static org.mockito.Matchers.anyObject;
17 import static org.mockito.Matchers.eq;
18 import static org.mockito.Mockito.when;
20 import com.fasterxml.jackson.core.JsonProcessingException;
21 import com.google.common.collect.Lists;
22 import java.util.List;
23 import javax.ws.rs.core.Response;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.opendaylight.controller.northbound.commons.exception.ResourceNotFoundException;
27 import org.opendaylight.controller.northbound.commons.exception.ServiceUnavailableException;
28 import org.opendaylight.ovsdb.plugin.api.Connection;
29 import org.opendaylight.ovsdb.plugin.api.OvsdbConnectionService;
30 import org.opendaylight.ovsdb.plugin.impl.ConnectionServiceImpl;
31 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
33 import org.powermock.api.mockito.PowerMockito;
34 import org.powermock.core.classloader.annotations.PrepareForTest;
35 import org.powermock.modules.junit4.PowerMockRunner;
37 @RunWith(PowerMockRunner.class)
38 @PrepareForTest(ServiceHelper.class)
39 public class NodeResourceTest {
40 private static final String OVS = "OVS";
41 private static final String IDENTIFIER = "192.168.120.31:45001";
42 private static final String IDENTIFIER2 = "192.168.120.31:45002";
43 private static final String OVS_IDENTIFIER = OVS + "|" + IDENTIFIER;
44 private static final String OVS_IDENTIFIER2 = OVS + "|" + IDENTIFIER2;
45 private static final String BAD_IDENTIFIER = "BAD" + "|" + IDENTIFIER;
48 public void testGetOvsdbNode () {
49 ConnectionServiceImpl connectionService = new ConnectionServiceImpl();
50 Connection connection = new Connection(IDENTIFIER, null);
51 connectionService.putOvsdbConnection(IDENTIFIER, connection);
53 PowerMockito.mockStatic(ServiceHelper.class);
54 when(ServiceHelper.getGlobalInstance(eq(OvsdbConnectionService.class), anyObject()))
56 .thenReturn(connectionService)
57 .thenReturn(connectionService);
60 NodeResource.getOvsdbNode(IDENTIFIER, this);
61 fail("Expected an ServiceUnavailableException to be thrown");
62 } catch (ServiceUnavailableException e) {
63 assertSame(ServiceUnavailableException.class, e.getClass());
67 NodeResource.getOvsdbNode(BAD_IDENTIFIER, this);
68 fail("Expected an ResourceNotFoundException to be thrown");
69 } catch (ResourceNotFoundException e) {
70 assertSame(ResourceNotFoundException.class, e.getClass());
73 Node node = NodeResource.getOvsdbNode(OVS_IDENTIFIER, this);
74 assertNotNull("Node " + OVS_IDENTIFIER + " is null", node);
78 public void testGetOvsdbConnection () {
79 ConnectionServiceImpl connectionService = new ConnectionServiceImpl();
80 Connection connection = new Connection(IDENTIFIER, null);
81 connectionService.putOvsdbConnection(IDENTIFIER, connection);
83 PowerMockito.mockStatic(ServiceHelper.class);
84 when(ServiceHelper.getGlobalInstance(eq(OvsdbConnectionService.class), anyObject()))
86 .thenReturn(connectionService)
87 .thenReturn(connectionService);
90 NodeResource.getOvsdbConnection(IDENTIFIER, this);
91 fail("Expected an ServiceUnavailableException to be thrown");
92 } catch (ServiceUnavailableException e) {
93 assertSame(ServiceUnavailableException.class, e.getClass());
97 NodeResource.getOvsdbConnection(BAD_IDENTIFIER, this);
98 fail("Expected an ResourceNotFoundException to be thrown");
99 } catch (ResourceNotFoundException e) {
100 assertSame(ResourceNotFoundException.class, e.getClass());
103 Connection testConnection = NodeResource.getOvsdbConnection(IDENTIFIER, this);
104 assertNotNull("Connection " + OVS_IDENTIFIER + " is null", testConnection);
108 public void testGetNodes () {
109 ConnectionServiceImpl connectionService = new ConnectionServiceImpl();
111 PowerMockito.mockStatic(ServiceHelper.class);
112 when(ServiceHelper.getGlobalInstance(eq(OvsdbConnectionService.class), anyObject()))
113 .thenReturn(connectionService)
114 .thenReturn(connectionService)
115 .thenReturn(connectionService);
117 NodeResource nodeResource = new NodeResource();
119 // Check getNodes when there are no nodes
121 Response response = nodeResource.getNodes();
122 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
123 assertNotNull("entity should not be null", response.getEntity());
125 List<String> ids = Lists.newArrayList();
127 assertEquals("there should be no nodes", ids.toString(), response.getEntity());
128 } catch (JsonProcessingException ex) {
129 fail("Exception should not have been caught");
132 // Check getNodes when there is a node
133 Connection connection = new Connection(IDENTIFIER, null);
134 connectionService.putOvsdbConnection(IDENTIFIER, connection);
137 Response response = nodeResource.getNodes();
138 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
139 assertNotNull("entity should not be null", response.getEntity());
140 String id = "\"" + OVS_IDENTIFIER + "\"";
141 List<String> ids = Lists.newArrayList();
143 assertEquals(OVS_IDENTIFIER + " should be found", ids.toString(), response.getEntity());
144 } catch (JsonProcessingException ex) {
145 fail("Exception should not have been caught");
148 // Check getNodes when there are multiple nodes
149 connection = new Connection(IDENTIFIER2, null);
150 connectionService.putOvsdbConnection(IDENTIFIER2, connection);
153 Response response = nodeResource.getNodes();
154 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
155 assertNotNull("entity should not be null", response.getEntity());
156 String id = "\"" + OVS_IDENTIFIER + "\"";
157 String id2 = "\"" + OVS_IDENTIFIER2 + "\"";
158 List<String> ids = Lists.newArrayList();
161 assertEquals(OVS_IDENTIFIER + " and " + OVS_IDENTIFIER2 + " should be found",
162 ids.toString().replaceAll("\\s",""), response.getEntity());
163 } catch (JsonProcessingException ex) {
164 fail("Exception should not have been caught");