Return only the Identifier and not the whole Node object
[netvirt.git] / northbound / src / test / java / org / opendaylight / ovsdb / northbound / NodeResourceTest.java
1 /*
2  *  Copyright (C) 2015 Red Hat, Inc.
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  *  Authors : Sam Hague
9  */
10 package org.opendaylight.ovsdb.northbound;
11
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;
19
20 import com.fasterxml.jackson.core.JsonProcessingException;
21 import javax.ws.rs.core.Response;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.opendaylight.controller.northbound.commons.exception.ResourceNotFoundException;
25 import org.opendaylight.controller.northbound.commons.exception.ServiceUnavailableException;
26 import org.opendaylight.ovsdb.plugin.api.Connection;
27 import org.opendaylight.ovsdb.plugin.api.OvsdbConnectionService;
28 import org.opendaylight.ovsdb.plugin.impl.ConnectionServiceImpl;
29 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
31 import org.powermock.api.mockito.PowerMockito;
32 import org.powermock.core.classloader.annotations.PrepareForTest;
33 import org.powermock.modules.junit4.PowerMockRunner;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 @RunWith(PowerMockRunner.class)
38 @PrepareForTest(ServiceHelper.class)
39 public class NodeResourceTest {
40     static final Logger LOG = LoggerFactory.getLogger(NodeResourceTest.class);
41     private static final String OVS = "OVS";
42     private static final String IDENTIFIER = "192.168.120.31:45001";
43     private static final String OVS_IDENTIFIER = OVS + "|" + IDENTIFIER;
44     private static final String BAD_IDENTIFIER = "BAD" + "|" + IDENTIFIER;
45
46     @Test
47     public void testGetOvsdbNode () {
48         ConnectionServiceImpl connectionService = new ConnectionServiceImpl();
49         Connection connection = new Connection(IDENTIFIER, null);
50         connectionService.putOvsdbConnection(IDENTIFIER, connection);
51
52         PowerMockito.mockStatic(ServiceHelper.class);
53         when(ServiceHelper.getGlobalInstance(eq(OvsdbConnectionService.class), anyObject()))
54                 .thenReturn(null)
55                 .thenReturn(connectionService)
56                 .thenReturn(connectionService);
57
58         Node node = null;
59         try {
60             node = NodeResource.getOvsdbNode(IDENTIFIER, this);
61             fail("Expected an ServiceUnavailableException to be thrown");
62         } catch (ServiceUnavailableException e) {
63             assertSame(ServiceUnavailableException.class, e.getClass());
64         }
65
66         try {
67             node = NodeResource.getOvsdbNode(BAD_IDENTIFIER, this);
68             fail("Expected an ResourceNotFoundException to be thrown");
69         } catch (ResourceNotFoundException e) {
70             assertSame(ResourceNotFoundException.class, e.getClass());
71         }
72
73         node = NodeResource.getOvsdbNode(OVS_IDENTIFIER, this);
74         assertNotNull("Node " + OVS_IDENTIFIER + " is null", node);
75     }
76
77     @Test
78     public void testGetOvsdbConnection () {
79         ConnectionServiceImpl connectionService = new ConnectionServiceImpl();
80         Connection connection = new Connection(IDENTIFIER, null);
81         connectionService.putOvsdbConnection(IDENTIFIER, connection);
82
83         PowerMockito.mockStatic(ServiceHelper.class);
84         when(ServiceHelper.getGlobalInstance(eq(OvsdbConnectionService.class), anyObject()))
85                 .thenReturn(null)
86                 .thenReturn(connectionService)
87                 .thenReturn(connectionService);
88
89         Connection testConnection = null;
90         try {
91             testConnection = NodeResource.getOvsdbConnection(IDENTIFIER, this);
92             fail("Expected an ServiceUnavailableException to be thrown");
93         } catch (ServiceUnavailableException e) {
94             assertSame(ServiceUnavailableException.class, e.getClass());
95         }
96
97         try {
98             testConnection = NodeResource.getOvsdbConnection(BAD_IDENTIFIER, this);
99             fail("Expected an ResourceNotFoundException to be thrown");
100         } catch (ResourceNotFoundException e) {
101             assertSame(ResourceNotFoundException.class, e.getClass());
102         }
103
104         testConnection = NodeResource.getOvsdbConnection(IDENTIFIER, this);
105         assertNotNull("Connection " + OVS_IDENTIFIER + " is null", testConnection);
106     }
107
108     @Test
109     public void testGetNodes () {
110         ConnectionServiceImpl connectionService = new ConnectionServiceImpl();
111
112         PowerMockito.mockStatic(ServiceHelper.class);
113         when(ServiceHelper.getGlobalInstance(eq(OvsdbConnectionService.class), anyObject()))
114                 .thenReturn(connectionService)
115                 .thenReturn(connectionService);
116
117         NodeResource nodeResource = new NodeResource();
118
119         // Check getNodes when there are no nodes
120         try {
121             Response response = nodeResource.getNodes();
122             assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
123             assertNotNull("entity should not be null", response.getEntity());
124             // TODO find way to extract entity as a string. using the below fails with:
125             // org.junit.ComparisonFailure: expected:<[OVS|192.168.120.31:45001]> but was:<[["OVS|192.168.120.31:45001"]]>
126             //assertEquals("", response.getEntity());
127             // In its place the 200, not null and println are sufficient
128             LOG.info("null response entity: " + response.getEntity());
129         } catch (JsonProcessingException ex) {
130             fail("Exception should not have been caught");
131         }
132
133         // Check getNodes when there is a node
134         Connection connection = new Connection(IDENTIFIER, null);
135         connectionService.putOvsdbConnection(IDENTIFIER, connection);
136
137         try {
138             Response response = nodeResource.getNodes();
139             assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
140             assertNotNull("entity should not be null", response.getEntity());
141             // TODO find way to extract entity as a string. using the below fails with:
142             // org.junit.ComparisonFailure: expected:<[OVS|192.168.120.31:45001]> but was:<[["OVS|192.168.120.31:45001"]]>
143             // In its place the 200, not null and println are sufficient
144             //assertEquals(OVS_IDENTIFIER, response.getEntity());
145             LOG.info(OVS_IDENTIFIER + " response entity: " + response.getEntity());
146         } catch (JsonProcessingException ex) {
147             fail("Exception should not have been caught");
148         }
149     }
150 }