blob: d36b6e62bf495476f544cae5f54a07f393d2ab69 [file] [log] [blame]
Jian Li7fe7eaf2018-12-31 17:00:33 +09001/*
2 * Copyright 2018-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.openstacktelemetry.web;
17
18import com.google.common.collect.Maps;
19import org.glassfish.jersey.server.ResourceConfig;
20import org.junit.Before;
21import org.junit.Test;
22import org.onlab.osgi.ServiceDirectory;
23import org.onlab.osgi.TestServiceDirectory;
24import org.onosproject.openstacktelemetry.api.DefaultTelemetryConfig;
25import org.onosproject.openstacktelemetry.api.TelemetryConfigAdminService;
26import org.onosproject.openstacktelemetry.api.config.TelemetryConfig;
27import org.onosproject.rest.resources.ResourceTest;
28
29import javax.ws.rs.client.Entity;
30import javax.ws.rs.client.WebTarget;
31import javax.ws.rs.core.MediaType;
32import javax.ws.rs.core.Response;
33import java.util.Map;
34
35import static junit.framework.TestCase.assertEquals;
36import static org.easymock.EasyMock.anyString;
37import static org.easymock.EasyMock.createMock;
38import static org.easymock.EasyMock.expect;
39import static org.easymock.EasyMock.replay;
40import static org.easymock.EasyMock.verify;
41import static org.onosproject.openstacktelemetry.api.config.TelemetryConfig.ConfigType.GRPC;
Jian Li667c6eb2019-01-07 23:01:12 +090042import static org.onosproject.openstacktelemetry.api.config.TelemetryConfig.Status.ENABLED;
Jian Li7fe7eaf2018-12-31 17:00:33 +090043
44/**
45 * Unit tests for openstack telemetry config REST API.
46 */
47public class OpenstackTelemetryConfigWebResourceTest extends ResourceTest {
48
49 private static final String NAME = "grpc";
50
51 private static final TelemetryConfig.ConfigType TYPE = GRPC;
52
53 private static final String MANUFACTURER = "grpc.io";
54
55 private static final String SW_VERSION = "1.0";
56
57 private static final Map<String, String> PROP = Maps.newConcurrentMap();
58
59 private static final String PROP_KEY_1 = "key11";
60 private static final String PROP_KEY_2 = "key12";
61 private static final String PROP_VALUE_1 = "value11";
62 private static final String PROP_VALUE_2 = "value12";
63
Jian Li667c6eb2019-01-07 23:01:12 +090064 private static final TelemetryConfig.Status STATUS = ENABLED;
Jian Li7fe7eaf2018-12-31 17:00:33 +090065
66 private final TelemetryConfigAdminService mockConfigAdminService =
67 createMock(TelemetryConfigAdminService.class);
68 private static final String PATH = "config";
69
70 /**
71 * Constructs an openstack telemetry config resource test instance.
72 */
73 public OpenstackTelemetryConfigWebResourceTest() {
74 super(ResourceConfig.forApplicationClass(OpenstackTelemetryWebApplication.class));
75 }
76
77 private TelemetryConfig telemetryConfig;
78
79 /**
80 * Sets up the global values for all the tests.
81 */
82 @Before
83 public void setUpTest() {
84 ServiceDirectory testDirectory =
85 new TestServiceDirectory()
86 .add(TelemetryConfigAdminService.class,
87 mockConfigAdminService);
88 setServiceDirectory(testDirectory);
89
90 PROP.put(PROP_KEY_1, PROP_VALUE_1);
91 PROP.put(PROP_KEY_2, PROP_VALUE_2);
92
93 telemetryConfig = new DefaultTelemetryConfig(NAME, TYPE, null,
Jian Li667c6eb2019-01-07 23:01:12 +090094 MANUFACTURER, SW_VERSION, STATUS, PROP);
Jian Li7fe7eaf2018-12-31 17:00:33 +090095 }
96
97 /**
98 * Tests the results of the REST API PUT method by modifying config address.
99 */
100 @Test
101 public void testUpdateConfigAddressWithModifyOperation() {
102 expect(mockConfigAdminService.getConfig(anyString()))
103 .andReturn(telemetryConfig).once();
104 mockConfigAdminService.updateTelemetryConfig(telemetryConfig);
105 replay(mockConfigAdminService);
106
107 final WebTarget wt = target();
108 Response response = wt.path(PATH + "/address/test1/address1")
109 .request(MediaType.APPLICATION_JSON_TYPE)
110 .put(Entity.json(""));
111 final int status = response.getStatus();
112
113 assertEquals(200, status);
114
115 verify(mockConfigAdminService);
116 }
117
118 /**
119 * Tests the results of the REST API PUT method without modifying config address.
120 */
121 @Test
122 public void testUpdateConfigAddressWithoutOperation() {
123 expect(mockConfigAdminService.getConfig(anyString())).andReturn(null).once();
124 replay(mockConfigAdminService);
125
126 final WebTarget wt = target();
127 Response response = wt.path(PATH + "/address/test1/address1")
128 .request(MediaType.APPLICATION_JSON_TYPE)
129 .put(Entity.json(""));
130 final int status = response.getStatus();
131
132 assertEquals(304, status);
133
134 verify(mockConfigAdminService);
135 }
136
137 /**
138 * Tests the results of the REST API DELETE method by removing config.
139 */
140 @Test
141 public void testDeleteConfigWithModifyOperation() {
142 expect(mockConfigAdminService.getConfig(anyString()))
143 .andReturn(telemetryConfig).once();
144 mockConfigAdminService.removeTelemetryConfig(anyString());
145 replay(mockConfigAdminService);
146
147 final WebTarget wt = target();
148 Response response = wt.path(PATH + "/test1")
149 .request(MediaType.APPLICATION_JSON_TYPE)
150 .delete();
151 final int status = response.getStatus();
152
153 assertEquals(204, status);
154
155 verify(mockConfigAdminService);
156 }
157
158 /**
159 * Tests the results of the REST API DELETE method without removing config.
160 */
161 @Test
162 public void testDeleteConfigWithoutModifyOperation() {
163 expect(mockConfigAdminService.getConfig(anyString())).andReturn(null).once();
164 replay(mockConfigAdminService);
165
166 final WebTarget wt = target();
167 Response response = wt.path(PATH + "/test1")
168 .request(MediaType.APPLICATION_JSON_TYPE)
169 .delete();
170 final int status = response.getStatus();
171
172 assertEquals(304, status);
173
174 verify(mockConfigAdminService);
175 }
176
177 /**
178 * Tests the results of REST API PUT method with enabling the config.
179 */
180 @Test
181 public void testEnableConfig() {
182 expect(mockConfigAdminService.getConfig(anyString()))
183 .andReturn(telemetryConfig).once();
184 mockConfigAdminService.updateTelemetryConfig(telemetryConfig);
185 replay(mockConfigAdminService);
186
187 final WebTarget wt = target();
188 Response response = wt.path(PATH + "/enable/test1")
189 .request(MediaType.APPLICATION_JSON_TYPE)
190 .put(Entity.json(""));
191 final int status = response.getStatus();
192
193 assertEquals(200, status);
194
195 verify(mockConfigAdminService);
196 }
197
198 /**
199 * Tests the results of REST API PUT method with disabling the config.
200 */
201 @Test
202 public void testDisableConfig() {
203 expect(mockConfigAdminService.getConfig(anyString()))
204 .andReturn(telemetryConfig).once();
205 mockConfigAdminService.updateTelemetryConfig(telemetryConfig);
206 replay(mockConfigAdminService);
207
208 final WebTarget wt = target();
209 Response response = wt.path(PATH + "/disable/test1")
210 .request(MediaType.APPLICATION_JSON_TYPE)
211 .put(Entity.json(""));
212 final int status = response.getStatus();
213
214 assertEquals(200, status);
215
216 verify(mockConfigAdminService);
217 }
218}