Added Security Rule for Custom ICMP
[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         try {
60             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             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 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         try {
90             NodeResource.getOvsdbConnection(IDENTIFIER, this);
91             fail("Expected an ServiceUnavailableException to be thrown");
92         } catch (ServiceUnavailableException e) {
93             assertSame(ServiceUnavailableException.class, e.getClass());
94         }
95
96         try {
97             NodeResource.getOvsdbConnection(BAD_IDENTIFIER, this);
98             fail("Expected an ResourceNotFoundException to be thrown");
99         } catch (ResourceNotFoundException e) {
100             assertSame(ResourceNotFoundException.class, e.getClass());
101         }
102
103         Connection testConnection = NodeResource.getOvsdbConnection(IDENTIFIER, this);
104         assertNotNull("Connection " + OVS_IDENTIFIER + " is null", testConnection);
105     }
106
107     @Test
108     public void testGetNodes () {
109         ConnectionServiceImpl connectionService = new ConnectionServiceImpl();
110
111         PowerMockito.mockStatic(ServiceHelper.class);
112         when(ServiceHelper.getGlobalInstance(eq(OvsdbConnectionService.class), anyObject()))
113                 .thenReturn(connectionService)
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             String id = "";
125             List<String> ids = Lists.newArrayList();
126             ids.add(id);
127             assertEquals("there should be no nodes", ids.toString(), response.getEntity());
128         } catch (JsonProcessingException ex) {
129             fail("Exception should not have been caught");
130         }
131
132         // Check getNodes when there is a node
133         Connection connection = new Connection(IDENTIFIER, null);
134         connectionService.putOvsdbConnection(IDENTIFIER, connection);
135
136         try {
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();
142             ids.add(id);
143             assertEquals(OVS_IDENTIFIER + " should be found", ids.toString(), response.getEntity());
144         } catch (JsonProcessingException ex) {
145             fail("Exception should not have been caught");
146         }
147
148         // Check getNodes when there are multiple nodes
149         connection = new Connection(IDENTIFIER2, null);
150         connectionService.putOvsdbConnection(IDENTIFIER2, connection);
151
152         try {
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();
159             ids.add(id);
160             ids.add(id2);
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");
165         }
166     }
167 }