blob: 77153c395352ac6e0aecd28f735ddaa8e8ec172c [file] [log] [blame]
Jian Lia80b1582019-01-25 12:47:42 +09001/*
2 * Copyright 2019-present Open Networking Foundation
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 */
16package org.onosproject.k8snetworking.web;
17
18import org.glassfish.jersey.server.ResourceConfig;
19import org.junit.Before;
20import org.junit.Test;
21import org.onlab.osgi.ServiceDirectory;
22import org.onlab.osgi.TestServiceDirectory;
23import org.onosproject.codec.CodecService;
24import org.onosproject.codec.impl.CodecManager;
Jian Liec0f7482019-02-12 13:14:29 +090025import org.onosproject.k8snetworking.api.DefaultK8sNetwork;
Jian Lia80b1582019-01-25 12:47:42 +090026import org.onosproject.k8snetworking.api.K8sNetwork;
27import org.onosproject.k8snetworking.api.K8sNetworkAdminService;
28import org.onosproject.k8snetworking.codec.K8sNetworkCodec;
29import org.onosproject.rest.resources.ResourceTest;
30
31import javax.ws.rs.client.Entity;
32import javax.ws.rs.client.WebTarget;
33import javax.ws.rs.core.MediaType;
34import javax.ws.rs.core.Response;
35import java.io.InputStream;
36
37import static org.easymock.EasyMock.anyObject;
38import static org.easymock.EasyMock.anyString;
39import static org.easymock.EasyMock.createMock;
Jian Liec0f7482019-02-12 13:14:29 +090040import static org.easymock.EasyMock.expect;
Jian Lia80b1582019-01-25 12:47:42 +090041import static org.easymock.EasyMock.replay;
42import static org.easymock.EasyMock.verify;
43import static org.hamcrest.Matchers.is;
44import static org.junit.Assert.assertThat;
45
46/**
47 * Unit tests for kubernetes network REST API.
48 */
49public class K8sNetworkWebResourceTest extends ResourceTest {
50
51 final K8sNetworkAdminService mockAdminService = createMock(K8sNetworkAdminService.class);
52 private static final String PATH = "network";
53
54 private K8sNetwork k8sNetwork;
55
56 /**
57 * Constructs a kubernetes networking resource test instance.
58 */
59 public K8sNetworkWebResourceTest() {
60 super(ResourceConfig.forApplicationClass(K8sNetworkingWebApplication.class));
61 }
62
63 /**
64 * Sets up the global values for all the tests.
65 */
66 @Before
67 public void setUpTest() {
68 final CodecManager codecService = new CodecManager();
69 codecService.activate();
70 codecService.registerCodec(K8sNetwork.class, new K8sNetworkCodec());
71 ServiceDirectory testDirectory =
72 new TestServiceDirectory()
73 .add(K8sNetworkAdminService.class, mockAdminService)
74 .add(CodecService.class, codecService);
75 setServiceDirectory(testDirectory);
Jian Liec0f7482019-02-12 13:14:29 +090076
77 k8sNetwork = DefaultK8sNetwork.builder()
78 .networkId("sona-network")
79 .name("sona-network")
80 .type(K8sNetwork.Type.VXLAN)
81 .segmentId("1")
Jian Li2778ffa2019-05-07 13:21:52 +090082 .cidr("10.10.0.0/24")
Jian Liec0f7482019-02-12 13:14:29 +090083 .mtu(1500)
84 .build();
Jian Lia80b1582019-01-25 12:47:42 +090085 }
86
87 /**
88 * Tests the results of the REST API POST method with creating new network operation.
89 */
90 @Test
91 public void testCreateNetworkWithCreateOperation() {
92 mockAdminService.createNetwork(anyObject());
93 replay(mockAdminService);
94
95 final WebTarget wt = target();
96 InputStream jsonStream = K8sNetworkWebResourceTest.class
97 .getResourceAsStream("k8s-network.json");
98 Response response = wt.path(PATH).request(MediaType.APPLICATION_JSON_TYPE)
99 .post(Entity.json(jsonStream));
100 final int status = response.getStatus();
101
102 assertThat(status, is(201));
103
104 verify(mockAdminService);
105 }
106
107 /**
108 * Tests the results of the REST API PUT method with modifying the network.
109 */
110 @Test
111 public void testUpdateNetworkWithModifyOperation() {
112 mockAdminService.updateNetwork(anyObject());
113 replay(mockAdminService);
114
115 String location = PATH + "/network-1";
116
117 final WebTarget wt = target();
118 InputStream jsonStream = K8sNetworkWebResourceTest.class
119 .getResourceAsStream("k8s-network.json");
120 Response response = wt.path(location)
121 .request(MediaType.APPLICATION_JSON_TYPE)
122 .put(Entity.json(jsonStream));
123 final int status = response.getStatus();
124
125 assertThat(status, is(200));
126
127 verify(mockAdminService);
128 }
129
130 /**
131 * Tests the results of the REST API DELETE method with deleting the network.
132 */
133 @Test
134 public void testDeleteNetworkWithDeletionOperation() {
135 mockAdminService.removeNetwork(anyString());
136 replay(mockAdminService);
137
138 String location = PATH + "/network-1";
139
140 final WebTarget wt = target();
141 Response response = wt.path(location).request(
142 MediaType.APPLICATION_JSON_TYPE).delete();
143
144 final int status = response.getStatus();
145
146 assertThat(status, is(204));
147
148 verify(mockAdminService);
149 }
Jian Liec0f7482019-02-12 13:14:29 +0900150
151 /**
152 * Tests the results of checking network existence.
153 */
154 @Test
155 public void testHasNetworkWithValidNetwork() {
156 expect(mockAdminService.network(anyString())).andReturn(k8sNetwork);
157 replay(mockAdminService);
158
159 String location = PATH + "/exist/network-1";
160
161 final WebTarget wt = target();
162 String response = wt.path(location).request(
163 MediaType.APPLICATION_JSON_TYPE).get(String.class);
164
165 assertThat(response, is("{\"result\":true}"));
166
167 verify((mockAdminService));
168 }
169
170 /**
171 * Tests the results of checking network existence.
172 */
173 @Test
174 public void testHasNetworkWithNullNetwork() {
175 expect(mockAdminService.network(anyString())).andReturn(null);
176 replay(mockAdminService);
177
178 String location = PATH + "/exist/network-1";
179
180 final WebTarget wt = target();
181 String response = wt.path(location).request(
182 MediaType.APPLICATION_JSON_TYPE).get(String.class);
183
184 assertThat(response, is("{\"result\":false}"));
185
186 verify((mockAdminService));
187 }
Jian Lia80b1582019-01-25 12:47:42 +0900188}