blob: 2cb2047049b2f4300ec198bfd340874b448610dd [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;
25import org.onosproject.k8snetworking.api.K8sNetworkAdminService;
26import org.onosproject.k8snetworking.api.K8sPort;
27import org.onosproject.k8snetworking.codec.K8sPortCodec;
28import org.onosproject.rest.resources.ResourceTest;
29
30import javax.ws.rs.client.Entity;
31import javax.ws.rs.client.WebTarget;
32import javax.ws.rs.core.MediaType;
33import javax.ws.rs.core.Response;
34import java.io.InputStream;
35
36import static org.easymock.EasyMock.anyObject;
37import static org.easymock.EasyMock.anyString;
38import static org.easymock.EasyMock.createMock;
39import static org.easymock.EasyMock.replay;
40import static org.easymock.EasyMock.verify;
41import static org.hamcrest.Matchers.is;
42import static org.junit.Assert.assertThat;
43
44/**
45 * Unit test for kubernetes port REST API.
46 */
47public class K8sPortWebResourceTest extends ResourceTest {
48
49 final K8sNetworkAdminService mockAdminService = createMock(K8sNetworkAdminService.class);
50 private static final String PATH = "port";
51
52 private K8sPort k8sPort;
53
54 /**
55 * Constructs a kubernetes networking resource test instance.
56 */
57 public K8sPortWebResourceTest() {
58 super(ResourceConfig.forApplicationClass(K8sNetworkingWebApplication.class));
59 }
60
61 /**
62 * Sets up the global values for all the tests.
63 */
64 @Before
65 public void setUpTest() {
66 final CodecManager codecService = new CodecManager();
67 codecService.activate();
68 codecService.registerCodec(K8sPort.class, new K8sPortCodec());
69 ServiceDirectory testDirectory =
70 new TestServiceDirectory()
71 .add(K8sNetworkAdminService.class, mockAdminService)
72 .add(CodecService.class, codecService);
73 setServiceDirectory(testDirectory);
74 }
75
76 /**
77 * Tests the results of the REST API POST method with creating new port operation.
78 */
79 @Test
80 public void testCreatePortWithCreateOperation() {
81 mockAdminService.createPort(anyObject());
82 replay(mockAdminService);
83
84 final WebTarget wt = target();
85 InputStream jsonStream = K8sPortWebResourceTest.class
86 .getResourceAsStream("k8s-port.json");
87 Response response = wt.path(PATH).request(MediaType.APPLICATION_JSON_TYPE)
88 .post(Entity.json(jsonStream));
89 final int status = response.getStatus();
90
91 assertThat(status, is(201));
92
93 verify(mockAdminService);
94 }
95
96 /**
97 * Tests the results of the REST API PUT method with modifying the port.
98 */
99 @Test
100 public void testUpdatePortWithModifyOperation() {
101 mockAdminService.updatePort(anyObject());
102 replay(mockAdminService);
103
104 String location = PATH + "/port-1";
105
106 final WebTarget wt = target();
107 InputStream jsonStream = K8sPortWebResourceTest.class
108 .getResourceAsStream("k8s-port.json");
109 Response response = wt.path(location)
110 .request(MediaType.APPLICATION_JSON_TYPE)
111 .put(Entity.json(jsonStream));
112 final int status = response.getStatus();
113
114 assertThat(status, is(200));
115
116 verify(mockAdminService);
117 }
118
119 /**
120 * Tests the results of the REST API DELETE method with deleting the port.
121 */
122 @Test
123 public void testDeletePortWithDeletionOperation() {
124 mockAdminService.removePort(anyString());
125 replay(mockAdminService);
126
127 String location = PATH + "/port-1";
128
129 final WebTarget wt = target();
130 Response response = wt.path(location).request(
131 MediaType.APPLICATION_JSON_TYPE).delete();
132
133 final int status = response.getStatus();
134
135 assertThat(status, is(204));
136
137 verify(mockAdminService);
138 }
139}