c9b032f3d377ff3f4cd008b099a41149d613cfb7
[ovsdb.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 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;
36
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;
46
47     @Test
48     public void testGetOvsdbNode () {
49         ConnectionServiceImpl connectionService = new ConnectionServiceImpl();
50         Connection connection = new Connection(IDENTIFIER, null);
51         connectionService.putOvsdbConnection(IDENTIFIER, connection);
52
53         PowerMockito.mockStatic(ServiceHelper.class);
54         when(ServiceHelper.getGlobalInstance(eq(OvsdbConnectionService.class), anyObject()))
55                 .thenReturn(null)
56                 .thenReturn(connectionService)
57                 .thenReturn(connectionService);
58
59         Node node = null;
60         try {
61             node = NodeResource.getOvsdbNode(IDENTIFIER, this);
62             fail("Expected an ServiceUnavailableException to be thrown");
63         } catch (ServiceUnavailableException e) {
64             assertSame(ServiceUnavailableException.class, e.getClass());
65         }
66
67         try {
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());
72         }
73
74         node = NodeResource.getOvsdbNode(OVS_IDENTIFIER, this);
75         assertNotNull("Node " + OVS_IDENTIFIER + " is null", node);
76     }
77
78     @Test
79     public void testGetOvsdbConnection () {
80         ConnectionServiceImpl connectionService = new ConnectionServiceImpl();
81         Connection connection = new Connection(IDENTIFIER, null);
82         connectionService.putOvsdbConnection(IDENTIFIER, connection);
83
84         PowerMockito.mockStatic(ServiceHelper.class);
85         when(ServiceHelper.getGlobalInstance(eq(OvsdbConnectionService.class), anyObject()))
86                 .thenReturn(null)
87                 .thenReturn(connectionService)
88                 .thenReturn(connectionService);
89
90         Connection testConnection = null;
91         try {
92             testConnection = NodeResource.getOvsdbConnection(IDENTIFIER, this);
93             fail("Expected an ServiceUnavailableException to be thrown");
94         } catch (ServiceUnavailableException e) {
95             assertSame(ServiceUnavailableException.class, e.getClass());
96         }
97
98         try {
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());
103         }
104
105         testConnection = NodeResource.getOvsdbConnection(IDENTIFIER, this);
106         assertNotNull("Connection " + OVS_IDENTIFIER + " is null", testConnection);
107     }
108
109     @Test
110     public void testGetNodes () {
111         ConnectionServiceImpl connectionService = new ConnectionServiceImpl();
112
113         PowerMockito.mockStatic(ServiceHelper.class);
114         when(ServiceHelper.getGlobalInstance(eq(OvsdbConnectionService.class), anyObject()))
115                 .thenReturn(connectionService)
116                 .thenReturn(connectionService)
117                 .thenReturn(connectionService);
118
119         NodeResource nodeResource = new NodeResource();
120
121         // Check getNodes when there are no nodes
122         try {
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();
128             ids.add(id);
129             assertEquals("there should be no nodes", ids.toString(), response.getEntity());
130         } catch (JsonProcessingException ex) {
131             fail("Exception should not have been caught");
132         }
133
134         // Check getNodes when there is a node
135         Connection connection = new Connection(IDENTIFIER, null);
136         connectionService.putOvsdbConnection(IDENTIFIER, connection);
137
138         try {
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();
144             ids.add(id);
145             assertEquals(OVS_IDENTIFIER + " should be found", ids.toString(), response.getEntity());
146         } catch (JsonProcessingException ex) {
147             fail("Exception should not have been caught");
148         }
149
150         // Check getNodes when there are multiple nodes
151         connection = new Connection(IDENTIFIER2, null);
152         connectionService.putOvsdbConnection(IDENTIFIER2, connection);
153
154         try {
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();
161             ids.add(id);
162             ids.add(id2);
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");
167         }
168     }
169 }