blob: 5a72b4917d90a19fe5137881dbd0512728e4b5be [file] [log] [blame]
Jian Lif2483072020-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")
Daniel Park2ba66e22022-04-27 12:33:45 +090090 .datacenterId("DB")
91 .clusterId("BD-MEH-CT01")
Jian Lif2483072020-12-25 02:24:16 +090092 .build();
93 }
94
95 /**
96 * Tests the results of the REST API POST method with creating new configs operation.
97 */
98 @Test
99 public void testCreateConfigWithCreateOperation() {
100 mockConfigAdminService.createApiConfig(anyObject());
101 replay(mockConfigAdminService);
102
103 final WebTarget wt = target();
104 InputStream jsonStream = KubevirtApiConfigWebResourceTest.class
105 .getResourceAsStream("kubevirt-api-config.json");
106 Response response = wt.path(PATH).request(MediaType.APPLICATION_JSON_TYPE)
107 .post(Entity.json(jsonStream));
108 final int status = response.getStatus();
109
110 assertThat(status, is(201));
111
112 verify(mockConfigAdminService);
113 }
114
115 /**
116 * Tests the results of the REST API POST method without creating new configs operation.
117 */
118 @Test
119 public void testCreateConfigWithNullConfig() {
120 mockConfigAdminService.createApiConfig(null);
121 replay(mockConfigAdminService);
122
123 final WebTarget wt = target();
124 InputStream jsonStream = KubevirtApiConfigWebResourceTest.class
125 .getResourceAsStream("kubevirt-api-config.json");
126 Response response = wt.path(PATH).request(MediaType.APPLICATION_JSON_TYPE)
127 .post(Entity.json(jsonStream));
128 final int status = response.getStatus();
129
130 assertThat(status, is(500));
131 }
132
133 /**
134 * Tests the results of the REST API DELETE method with deleting the configs.
135 */
136 @Test
137 public void testDeleteConfigsWithDeletionOperation() {
138 expect(mockConfigAdminService.apiConfig())
139 .andReturn(kubevirtApiConfig).once();
140 expect(mockConfigAdminService.removeApiConfig(anyString()))
141 .andReturn(kubevirtApiConfig).once();
142 replay(mockConfigAdminService);
143
144 String location = PATH + "/https://10.134.34.223:6443";
145
146 final WebTarget wt = target();
147 Response response = wt.path(location).request(
148 MediaType.APPLICATION_JSON_TYPE).delete();
149
150 final int status = response.getStatus();
151
152 assertThat(status, is(204));
153
154 verify(mockConfigAdminService);
155 }
156
157 /**
158 * Tests the results of the REST API DELETE method without deleting the configs.
159 */
160 @Test
161 public void testDeleteConfigsWithoutDeletionOperation() {
162 expect(mockConfigAdminService.apiConfig()).andReturn(null).once();
163 replay(mockConfigAdminService);
164
165 String location = PATH + "/https://test:8663";
166
167 final WebTarget wt = target();
168 Response response = wt.path(location).request(
169 MediaType.APPLICATION_JSON_TYPE).delete();
170
171 final int status = response.getStatus();
172
173 assertThat(status, is(304));
174
175 verify(mockConfigAdminService);
176 }
177}