blob: ff027bc910a9868343aef6483d690fa34c80beb5 [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;
42
43/**
44 * Unit tests for openstack telemetry config REST API.
45 */
46public class OpenstackTelemetryConfigWebResourceTest extends ResourceTest {
47
48 private static final String NAME = "grpc";
49
50 private static final TelemetryConfig.ConfigType TYPE = GRPC;
51
52 private static final String MANUFACTURER = "grpc.io";
53
54 private static final String SW_VERSION = "1.0";
55
56 private static final Map<String, String> PROP = Maps.newConcurrentMap();
57
58 private static final String PROP_KEY_1 = "key11";
59 private static final String PROP_KEY_2 = "key12";
60 private static final String PROP_VALUE_1 = "value11";
61 private static final String PROP_VALUE_2 = "value12";
62
63 private static final boolean ENABLED = true;
64
65 private final TelemetryConfigAdminService mockConfigAdminService =
66 createMock(TelemetryConfigAdminService.class);
67 private static final String PATH = "config";
68
69 /**
70 * Constructs an openstack telemetry config resource test instance.
71 */
72 public OpenstackTelemetryConfigWebResourceTest() {
73 super(ResourceConfig.forApplicationClass(OpenstackTelemetryWebApplication.class));
74 }
75
76 private TelemetryConfig telemetryConfig;
77
78 /**
79 * Sets up the global values for all the tests.
80 */
81 @Before
82 public void setUpTest() {
83 ServiceDirectory testDirectory =
84 new TestServiceDirectory()
85 .add(TelemetryConfigAdminService.class,
86 mockConfigAdminService);
87 setServiceDirectory(testDirectory);
88
89 PROP.put(PROP_KEY_1, PROP_VALUE_1);
90 PROP.put(PROP_KEY_2, PROP_VALUE_2);
91
92 telemetryConfig = new DefaultTelemetryConfig(NAME, TYPE, null,
93 MANUFACTURER, SW_VERSION, ENABLED, PROP);
94 }
95
96 /**
97 * Tests the results of the REST API PUT method by modifying config address.
98 */
99 @Test
100 public void testUpdateConfigAddressWithModifyOperation() {
101 expect(mockConfigAdminService.getConfig(anyString()))
102 .andReturn(telemetryConfig).once();
103 mockConfigAdminService.updateTelemetryConfig(telemetryConfig);
104 replay(mockConfigAdminService);
105
106 final WebTarget wt = target();
107 Response response = wt.path(PATH + "/address/test1/address1")
108 .request(MediaType.APPLICATION_JSON_TYPE)
109 .put(Entity.json(""));
110 final int status = response.getStatus();
111
112 assertEquals(200, status);
113
114 verify(mockConfigAdminService);
115 }
116
117 /**
118 * Tests the results of the REST API PUT method without modifying config address.
119 */
120 @Test
121 public void testUpdateConfigAddressWithoutOperation() {
122 expect(mockConfigAdminService.getConfig(anyString())).andReturn(null).once();
123 replay(mockConfigAdminService);
124
125 final WebTarget wt = target();
126 Response response = wt.path(PATH + "/address/test1/address1")
127 .request(MediaType.APPLICATION_JSON_TYPE)
128 .put(Entity.json(""));
129 final int status = response.getStatus();
130
131 assertEquals(304, status);
132
133 verify(mockConfigAdminService);
134 }
135
136 /**
137 * Tests the results of the REST API DELETE method by removing config.
138 */
139 @Test
140 public void testDeleteConfigWithModifyOperation() {
141 expect(mockConfigAdminService.getConfig(anyString()))
142 .andReturn(telemetryConfig).once();
143 mockConfigAdminService.removeTelemetryConfig(anyString());
144 replay(mockConfigAdminService);
145
146 final WebTarget wt = target();
147 Response response = wt.path(PATH + "/test1")
148 .request(MediaType.APPLICATION_JSON_TYPE)
149 .delete();
150 final int status = response.getStatus();
151
152 assertEquals(204, status);
153
154 verify(mockConfigAdminService);
155 }
156
157 /**
158 * Tests the results of the REST API DELETE method without removing config.
159 */
160 @Test
161 public void testDeleteConfigWithoutModifyOperation() {
162 expect(mockConfigAdminService.getConfig(anyString())).andReturn(null).once();
163 replay(mockConfigAdminService);
164
165 final WebTarget wt = target();
166 Response response = wt.path(PATH + "/test1")
167 .request(MediaType.APPLICATION_JSON_TYPE)
168 .delete();
169 final int status = response.getStatus();
170
171 assertEquals(304, status);
172
173 verify(mockConfigAdminService);
174 }
175
176 /**
177 * Tests the results of REST API PUT method with enabling the config.
178 */
179 @Test
180 public void testEnableConfig() {
181 expect(mockConfigAdminService.getConfig(anyString()))
182 .andReturn(telemetryConfig).once();
183 mockConfigAdminService.updateTelemetryConfig(telemetryConfig);
184 replay(mockConfigAdminService);
185
186 final WebTarget wt = target();
187 Response response = wt.path(PATH + "/enable/test1")
188 .request(MediaType.APPLICATION_JSON_TYPE)
189 .put(Entity.json(""));
190 final int status = response.getStatus();
191
192 assertEquals(200, status);
193
194 verify(mockConfigAdminService);
195 }
196
197 /**
198 * Tests the results of REST API PUT method with disabling the config.
199 */
200 @Test
201 public void testDisableConfig() {
202 expect(mockConfigAdminService.getConfig(anyString()))
203 .andReturn(telemetryConfig).once();
204 mockConfigAdminService.updateTelemetryConfig(telemetryConfig);
205 replay(mockConfigAdminService);
206
207 final WebTarget wt = target();
208 Response response = wt.path(PATH + "/disable/test1")
209 .request(MediaType.APPLICATION_JSON_TYPE)
210 .put(Entity.json(""));
211 final int status = response.getStatus();
212
213 assertEquals(200, status);
214
215 verify(mockConfigAdminService);
216 }
217}