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