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);
61 node = NodeResource.getOvsdbNode(IDENTIFIER, this);
62 fail("Expected an ServiceUnavailableException to be thrown");
63 } catch (ServiceUnavailableException e) {
64 assertSame(ServiceUnavailableException.class, e.getClass());
68 node = NodeResource.getOvsdbNode(BAD_IDENTIFIER, this);
69 fail("Expected an ResourceNotFoundException to be thrown");
70 } catch (ResourceNotFoundException e) {
71 assertSame(ResourceNotFoundException.class, e.getClass());
74 node = NodeResource.getOvsdbNode(OVS_IDENTIFIER, this);
75 assertNotNull("Node " + OVS_IDENTIFIER + " is null", node);
79 public void testGetOvsdbConnection () {
80 ConnectionServiceImpl connectionService = new ConnectionServiceImpl();
81 Connection connection = new Connection(IDENTIFIER, null);
82 connectionService.putOvsdbConnection(IDENTIFIER, connection);
84 PowerMockito.mockStatic(ServiceHelper.class);
85 when(ServiceHelper.getGlobalInstance(eq(OvsdbConnectionService.class), anyObject()))
87 .thenReturn(connectionService)
88 .thenReturn(connectionService);
90 Connection testConnection = null;
92 testConnection = NodeResource.getOvsdbConnection(IDENTIFIER, this);
93 fail("Expected an ServiceUnavailableException to be thrown");
94 } catch (ServiceUnavailableException e) {
95 assertSame(ServiceUnavailableException.class, e.getClass());
99 testConnection = NodeResource.getOvsdbConnection(BAD_IDENTIFIER, this);
100 fail("Expected an ResourceNotFoundException to be thrown");
101 } catch (ResourceNotFoundException e) {
102 assertSame(ResourceNotFoundException.class, e.getClass());
105 testConnection = NodeResource.getOvsdbConnection(IDENTIFIER, this);
106 assertNotNull("Connection " + OVS_IDENTIFIER + " is null", testConnection);
110 public void testGetNodes () {
111 ConnectionServiceImpl connectionService = new ConnectionServiceImpl();
113 PowerMockito.mockStatic(ServiceHelper.class);
114 when(ServiceHelper.getGlobalInstance(eq(OvsdbConnectionService.class), anyObject()))
115 .thenReturn(connectionService)
116 .thenReturn(connectionService)
117 .thenReturn(connectionService);
119 NodeResource nodeResource = new NodeResource();
121 // Check getNodes when there are no nodes
123 Response response = nodeResource.getNodes();
124 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
125 assertNotNull("entity should not be null", response.getEntity());
126 String id = new String();
127 List<String> ids = Lists.newArrayList();
129 assertEquals("there should be no nodes", ids.toString(), response.getEntity());
130 } catch (JsonProcessingException ex) {
131 fail("Exception should not have been caught");
134 // Check getNodes when there is a node
135 Connection connection = new Connection(IDENTIFIER, null);
136 connectionService.putOvsdbConnection(IDENTIFIER, connection);
139 Response response = nodeResource.getNodes();
140 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
141 assertNotNull("entity should not be null", response.getEntity());
142 String id = new String("\"" + OVS_IDENTIFIER + "\"");
143 List<String> ids = Lists.newArrayList();
145 assertEquals(OVS_IDENTIFIER + " should be found", ids.toString(), response.getEntity());
146 } catch (JsonProcessingException ex) {
147 fail("Exception should not have been caught");
150 // Check getNodes when there are multiple nodes
151 connection = new Connection(IDENTIFIER2, null);
152 connectionService.putOvsdbConnection(IDENTIFIER2, connection);
155 Response response = nodeResource.getNodes();
156 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
157 assertNotNull("entity should not be null", response.getEntity());
158 String id = new String("\"" + OVS_IDENTIFIER + "\"");
159 String id2 = new String("\"" + OVS_IDENTIFIER2 + "\"");
160 List<String> ids = Lists.newArrayList();
163 assertEquals(OVS_IDENTIFIER + " and " + OVS_IDENTIFIER2 + " should be found",
164 ids.toString().replaceAll("\\s",""), response.getEntity());
165 } catch (JsonProcessingException ex) {
166 fail("Exception should not have been caught");