271904cca1c6b533acc4030538b4492b2e002d8d
[onosfw.git] /
1 /*
2  * Copyright 2014-2015 Open Networking Laboratory
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onosproject.vtnweb.resources;
17
18 import static org.easymock.EasyMock.anyObject;
19 import static org.easymock.EasyMock.createMock;
20 import static org.easymock.EasyMock.expect;
21 import static org.easymock.EasyMock.replay;
22 import static org.hamcrest.Matchers.containsString;
23 import static org.hamcrest.Matchers.is;
24 import static org.hamcrest.Matchers.notNullValue;
25 import static org.junit.Assert.assertThat;
26 import static org.junit.Assert.fail;
27
28 import java.io.InputStream;
29 import java.net.HttpURLConnection;
30 import java.util.HashSet;
31 import java.util.Objects;
32 import java.util.Set;
33
34 import javax.ws.rs.core.MediaType;
35
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.onlab.osgi.ServiceDirectory;
40 import org.onlab.osgi.TestServiceDirectory;
41 import org.onlab.rest.BaseResource;
42 import org.onosproject.vtnrsc.PortPair;
43 import org.onosproject.vtnrsc.PortPairId;
44 import org.onosproject.vtnrsc.TenantId;
45 import org.onosproject.vtnrsc.portpair.PortPairService;
46
47 import com.eclipsesource.json.JsonObject;
48 import com.sun.jersey.api.client.ClientResponse;
49 import com.sun.jersey.api.client.UniformInterfaceException;
50 import com.sun.jersey.api.client.WebResource;
51 /**
52  * Unit tests for port pair REST APIs.
53  */
54 public class PortPairResourceTest extends VtnResourceTest {
55
56     final PortPairService portPairService = createMock(PortPairService.class);
57
58     PortPairId portPairId1 = PortPairId.of("78dcd363-fc23-aeb6-f44b-56dc5e2fb3ae");
59     TenantId tenantId1 = TenantId.tenantId("d382007aa9904763a801f68ecf065cf5");
60
61     final MockPortPair portPair1 = new MockPortPair(portPairId1, tenantId1, "portPair1",
62                                                     "Mock port pair", "dace4513-24fc-4fae-af4b-321c5e2eb3d1",
63             "aef3478a-4a56-2a6e-cd3a-9dee4e2ec345");
64
65     /**
66      * Mock class for a port pair.
67      */
68     private static class MockPortPair implements PortPair {
69
70         private final PortPairId portPairId;
71         private final TenantId tenantId;
72         private final String name;
73         private final String description;
74         private final String ingress;
75         private final String egress;
76
77         public MockPortPair(PortPairId portPairId, TenantId tenantId,
78                             String name, String description,
79                             String ingress, String egress) {
80
81             this.portPairId = portPairId;
82             this.tenantId = tenantId;
83             this.name = name;
84             this.description = description;
85             this.ingress = ingress;
86             this.egress = egress;
87         }
88
89         @Override
90         public PortPairId portPairId() {
91             return portPairId;
92         }
93
94         @Override
95         public TenantId tenantId() {
96             return tenantId;
97         }
98
99         @Override
100         public String name() {
101             return name;
102         }
103
104         @Override
105         public String description() {
106             return description;
107         }
108
109         @Override
110         public String ingress() {
111             return ingress;
112         }
113
114         @Override
115         public String egress() {
116             return egress;
117         }
118
119         @Override
120         public boolean exactMatch(PortPair portPair) {
121             return this.equals(portPair) &&
122                     Objects.equals(this.portPairId, portPair.portPairId()) &&
123                     Objects.equals(this.tenantId, portPair.tenantId());
124         }
125     }
126
127     /**
128      * Sets up the global values for all the tests.
129      */
130     @Before
131     public void setUpTest() {
132         ServiceDirectory testDirectory = new TestServiceDirectory().add(PortPairService.class, portPairService);
133         BaseResource.setServiceDirectory(testDirectory);
134
135     }
136
137     /**
138      * Cleans up.
139      */
140     @After
141     public void tearDownTest() {
142     }
143
144     /**
145      * Tests the result of the rest api GET when there are no port pairs.
146      */
147     @Test
148     public void testPortPairsEmpty() {
149
150         expect(portPairService.getPortPairs()).andReturn(null).anyTimes();
151         replay(portPairService);
152         final WebResource rs = resource();
153         final String response = rs.path("port_pairs").get(String.class);
154         assertThat(response, is("{\"port_pairs\":[]}"));
155     }
156
157     /**
158      * Tests the result of a rest api GET for port pair id.
159      */
160     @Test
161     public void testGetPortPairId() {
162
163         final Set<PortPair> portPairs = new HashSet<>();
164         portPairs.add(portPair1);
165
166         expect(portPairService.exists(anyObject())).andReturn(true).anyTimes();
167         expect(portPairService.getPortPair(anyObject())).andReturn(portPair1).anyTimes();
168         replay(portPairService);
169
170         final WebResource rs = resource();
171         final String response = rs.path("port_pairs/78dcd363-fc23-aeb6-f44b-56dc5e2fb3ae").get(String.class);
172         final JsonObject result = JsonObject.readFrom(response);
173         assertThat(result, notNullValue());
174     }
175
176     /**
177      * Tests that a fetch of a non-existent port pair object throws an exception.
178      */
179     @Test
180     public void testBadGet() {
181         expect(portPairService.getPortPair(anyObject()))
182         .andReturn(null).anyTimes();
183         replay(portPairService);
184         WebResource rs = resource();
185         try {
186             rs.path("port_pairs/78dcd363-fc23-aeb6-f44b-56dc5aafb3ae").get(String.class);
187             fail("Fetch of non-existent port pair did not throw an exception");
188         } catch (UniformInterfaceException ex) {
189             assertThat(ex.getMessage(),
190                        containsString("returned a response status of"));
191         }
192     }
193
194     /**
195      * Tests creating a port pair with POST.
196      */
197     @Test
198     public void testPost() {
199
200         expect(portPairService.createPortPair(anyObject()))
201         .andReturn(true).anyTimes();
202         replay(portPairService);
203
204         WebResource rs = resource();
205         InputStream jsonStream = PortPairResourceTest.class.getResourceAsStream("post-PortPair.json");
206
207         ClientResponse response = rs.path("port_pairs")
208                 .type(MediaType.APPLICATION_JSON_TYPE)
209                 .post(ClientResponse.class, jsonStream);
210         assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK));
211     }
212
213     /**
214      * Tests deleting a port pair.
215      */
216     @Test
217     public void testDelete() {
218         expect(portPairService.removePortPair(anyObject()))
219         .andReturn(true).anyTimes();
220         replay(portPairService);
221
222         WebResource rs = resource();
223
224         String location = "port_pairs/78dcd363-fc23-aeb6-f44b-56dc5e2fb3ae";
225
226         ClientResponse deleteResponse = rs.path(location)
227                 .type(MediaType.APPLICATION_JSON_TYPE)
228                 .delete(ClientResponse.class);
229         assertThat(deleteResponse.getStatus(),
230                    is(HttpURLConnection.HTTP_NO_CONTENT));
231     }
232 }