blob: e6d1034c5b835ade28da44f0c73882c0ab1bd846 [file] [log] [blame]
Jian Lic4604302020-12-25 02:24:16 +09001/*
2 * Copyright 2020-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.kubevirtnode.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.onlab.packet.IpAddress;
24import org.onosproject.codec.CodecService;
25import org.onosproject.codec.impl.CodecManager;
26import org.onosproject.kubevirtnode.api.DefaultKubevirtApiConfig;
27import org.onosproject.kubevirtnode.api.KubevirtApiConfig;
28import org.onosproject.kubevirtnode.api.KubevirtApiConfigAdminService;
29import org.onosproject.kubevirtnode.codec.KubevirtApiConfigCodec;
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;
41import static org.easymock.EasyMock.expect;
42import static org.easymock.EasyMock.replay;
43import static org.easymock.EasyMock.verify;
44import static org.hamcrest.Matchers.is;
45import static org.junit.Assert.assertThat;
46import static org.onosproject.kubevirtnode.api.KubevirtApiConfig.Scheme.HTTPS;
47import static org.onosproject.kubevirtnode.api.KubevirtApiConfig.State.DISCONNECTED;
48
49/**
50 * Unit test for KubeVirt node REST API.
51 */
52public class KubevirtApiConfigWebResourceTest extends ResourceTest {
53
54 final KubevirtApiConfigAdminService mockConfigAdminService =
55 createMock(KubevirtApiConfigAdminService.class);
56 private static final String PATH = "api-config";
57
58 private KubevirtApiConfig kubevirtApiConfig;
59
60 /**
61 * Constructs a KubeVirt API config resource test instance.
62 */
63 public KubevirtApiConfigWebResourceTest() {
64 super(ResourceConfig.forApplicationClass(KubevirtNodeWebApplication.class));
65 }
66
67 /**
68 * Sets up the global values for all the tests.
69 */
70 @Before
71 public void setUpTest() {
72 final CodecManager codecService = new CodecManager();
73 codecService.activate();
74 codecService.registerCodec(KubevirtApiConfig.class, new KubevirtApiConfigCodec());
75 ServiceDirectory testDirectory =
76 new TestServiceDirectory()
77 .add(KubevirtApiConfigAdminService.class, mockConfigAdminService)
78 .add(CodecService.class, codecService);
79 setServiceDirectory(testDirectory);
80
81 kubevirtApiConfig = DefaultKubevirtApiConfig.builder()
82 .scheme(HTTPS)
83 .ipAddress(IpAddress.valueOf("10.134.34.223"))
84 .port(6443)
85 .state(DISCONNECTED)
86 .token("tokenMod")
87 .caCertData("caCertData")
88 .clientCertData("clientCertData")
89 .clientKeyData("clientKeyData")
90 .build();
91 }
92
93 /**
94 * Tests the results of the REST API POST method with creating new configs operation.
95 */
96 @Test
97 public void testCreateConfigWithCreateOperation() {
98 mockConfigAdminService.createApiConfig(anyObject());
99 replay(mockConfigAdminService);
100
101 final WebTarget wt = target();
102 InputStream jsonStream = KubevirtApiConfigWebResourceTest.class
103 .getResourceAsStream("kubevirt-api-config.json");
104 Response response = wt.path(PATH).request(MediaType.APPLICATION_JSON_TYPE)
105 .post(Entity.json(jsonStream));
106 final int status = response.getStatus();
107
108 assertThat(status, is(201));
109
110 verify(mockConfigAdminService);
111 }
112
113 /**
114 * Tests the results of the REST API POST method without creating new configs operation.
115 */
116 @Test
117 public void testCreateConfigWithNullConfig() {
118 mockConfigAdminService.createApiConfig(null);
119 replay(mockConfigAdminService);
120
121 final WebTarget wt = target();
122 InputStream jsonStream = KubevirtApiConfigWebResourceTest.class
123 .getResourceAsStream("kubevirt-api-config.json");
124 Response response = wt.path(PATH).request(MediaType.APPLICATION_JSON_TYPE)
125 .post(Entity.json(jsonStream));
126 final int status = response.getStatus();
127
128 assertThat(status, is(500));
129 }
130
131 /**
132 * Tests the results of the REST API DELETE method with deleting the configs.
133 */
134 @Test
135 public void testDeleteConfigsWithDeletionOperation() {
136 expect(mockConfigAdminService.apiConfig())
137 .andReturn(kubevirtApiConfig).once();
138 expect(mockConfigAdminService.removeApiConfig(anyString()))
139 .andReturn(kubevirtApiConfig).once();
140 replay(mockConfigAdminService);
141
142 String location = PATH + "/https://10.134.34.223:6443";
143
144 final WebTarget wt = target();
145 Response response = wt.path(location).request(
146 MediaType.APPLICATION_JSON_TYPE).delete();
147
148 final int status = response.getStatus();
149
150 assertThat(status, is(204));
151
152 verify(mockConfigAdminService);
153 }
154
155 /**
156 * Tests the results of the REST API DELETE method without deleting the configs.
157 */
158 @Test
159 public void testDeleteConfigsWithoutDeletionOperation() {
160 expect(mockConfigAdminService.apiConfig()).andReturn(null).once();
161 replay(mockConfigAdminService);
162
163 String location = PATH + "/https://test:8663";
164
165 final WebTarget wt = target();
166 Response response = wt.path(location).request(
167 MediaType.APPLICATION_JSON_TYPE).delete();
168
169 final int status = response.getStatus();
170
171 assertThat(status, is(304));
172
173 verify(mockConfigAdminService);
174 }
175}