2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.dmaap.dbcapi.resources;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.assertFalse;
26 import java.util.List;
27 import javax.ws.rs.client.Entity;
28 import javax.ws.rs.core.GenericType;
29 import javax.ws.rs.core.MediaType;
30 import javax.ws.rs.core.Response;
32 import org.eclipse.jetty.http.HttpStatus;
33 import org.glassfish.jersey.server.ResourceConfig;
34 import org.junit.AfterClass;
35 import org.junit.Before;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.onap.dmaap.dbcapi.database.DatabaseClass;
39 import org.onap.dmaap.dbcapi.model.ApiError;
40 import org.onap.dmaap.dbcapi.model.DcaeLocation;
41 import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status;
42 import org.onap.dmaap.dbcapi.model.FqtnType;
43 import org.onap.dmaap.dbcapi.model.MR_Cluster;
44 import org.onap.dmaap.dbcapi.model.ReplicationType;
45 import org.onap.dmaap.dbcapi.model.Topic;
46 import org.onap.dmaap.dbcapi.testframework.DmaapObjectFactory;
48 public class TopicResourceTest {
50 private static final DmaapObjectFactory DMAAP_OBJECT_FACTORY = new DmaapObjectFactory();
51 private static final String TOPICS_TARGET = "topics";
53 private static FastJerseyTestContainer testContainer;
56 public static void setUpClass() throws Exception {
57 //TODO: init is still needed here to assure that dmaap is not null
58 DatabaseClass.getDmaap().init(DMAAP_OBJECT_FACTORY.genDmaap());
60 testContainer = new FastJerseyTestContainer(new ResourceConfig()
61 .register(TopicResource.class));
66 public static void tearDownClass() throws Exception {
67 testContainer.destroy();
71 public void setUpClusterAndLocation() {
72 DatabaseClass.clearDatabase();
74 DcaeLocation centralDcaeLoc = DMAAP_OBJECT_FACTORY.genDcaeLocation("central");
75 centralDcaeLoc.setStatus(DmaapObject_Status.VALID);
76 DatabaseClass.getDcaeLocations().put(centralDcaeLoc.getDcaeLocationName(), centralDcaeLoc);
78 MR_Cluster cluster = DMAAP_OBJECT_FACTORY.genMR_Cluster("central");
79 cluster.setStatus(DmaapObject_Status.VALID);
80 DatabaseClass.getMr_clusters().put(cluster.getDcaeLocationName(), cluster);
84 public void getTopics_shouldReturnEmptyList_whenNoTopicsInDataBase() {
86 Response resp = testContainer.target(TOPICS_TARGET).request().get(Response.class);
89 assertEquals(HttpStatus.OK_200, resp.getStatus());
90 assertTrue(resp.hasEntity());
92 List<Topic> topics = resp.readEntity(new GenericType<List<Topic>>() {
94 assertTrue(topics.isEmpty());
98 public void getTopics_shouldReturnTopicsRegisteredInDataBase() {
100 Topic topic1 = DMAAP_OBJECT_FACTORY.genSimpleTopic("testTopic1");
101 Topic topic2 = DMAAP_OBJECT_FACTORY.genSimpleTopic("testTopic2");
102 DatabaseClass.getTopics().put(topic1.getFqtn(), topic1);
103 DatabaseClass.getTopics().put(topic2.getFqtn(), topic2);
106 Response resp = testContainer.target(TOPICS_TARGET).request().get(Response.class);
109 assertEquals(HttpStatus.OK_200, resp.getStatus());
110 assertTrue(resp.hasEntity());
112 List<Topic> topics = resp.readEntity(new GenericType<List<Topic>>() {
114 assertEquals(2, topics.size());
115 assertTrue(topics.contains(topic1));
116 assertTrue(topics.contains(topic2));
120 public void getTopics_shouldReturnValidationError_whenTopicNameIsInvalid() {
122 String topicName = "wrong Topic Name";
125 Response resp = testContainer.target(TOPICS_TARGET).path(topicName).request().get(Response.class);
128 assertEquals(HttpStatus.BAD_REQUEST_400, resp.getStatus());
129 assertTrue(resp.hasEntity());
130 ApiError errorObj = resp.readEntity(ApiError.class);
131 assertEquals("topicName", errorObj.getFields());
135 public void getTopic_shouldReturnError_whenRequestedTopicNotFound() {
137 String topicName = "notExistingTopic";
140 Response resp = testContainer.target(TOPICS_TARGET).path(topicName).request().get(Response.class);
143 assertEquals(HttpStatus.NOT_FOUND_404, resp.getStatus());
144 assertTrue(resp.hasEntity());
145 ApiError errorObj = resp.readEntity(ApiError.class);
146 assertEquals("fqtn", errorObj.getFields());
150 public void getTopic_shouldReturnTopicInformation_whenRequestedTopicExists() {
152 Topic topic1 = DMAAP_OBJECT_FACTORY.genSimpleTopic("testTopic1");
153 DatabaseClass.getTopics().put(topic1.getFqtn(), topic1);
156 Response resp = testContainer.target(TOPICS_TARGET).path(topic1.getFqtn()).request().get(Response.class);
159 assertEquals(HttpStatus.OK_200, resp.getStatus());
160 assertTrue(resp.hasEntity());
161 Topic retrievedTopic = resp.readEntity(Topic.class);
162 assertEquals(topic1, retrievedTopic);
167 public void deleteTopic_shouldReturnError_whenTopicNotFound() {
169 String topicName = "notExisting";
172 Response resp = testContainer.target(TOPICS_TARGET).path(topicName).request().delete(Response.class);
175 assertEquals(HttpStatus.NOT_FOUND_404, resp.getStatus());
176 assertTrue(resp.hasEntity());
177 ApiError errorObj = resp.readEntity(ApiError.class);
178 assertEquals("fqtn", errorObj.getFields());
182 public void deleteTopic_shouldDeleteTopicFromDataBase_whenFound() {
184 Topic topic = DMAAP_OBJECT_FACTORY.genSimpleTopic("testTopic");
185 DatabaseClass.getTopics().put(topic.getFqtn(), topic);
188 Response resp = testContainer.target(TOPICS_TARGET).path(topic.getFqtn()).request().delete(Response.class);
191 assertEquals(HttpStatus.NO_CONTENT_204, resp.getStatus());
192 assertFalse(resp.hasEntity());
196 public void addTopic_shouldReturnValidationError_whenTopicNameIsInvalid() {
198 Topic topic = DMAAP_OBJECT_FACTORY.genSimpleTopic("wrong topic name with spaces");
199 Entity<Topic> requestedEntity = Entity.entity(topic, MediaType.APPLICATION_JSON);
202 Response resp = testContainer.target(TOPICS_TARGET).request().post(requestedEntity, Response.class);
205 assertEquals(HttpStatus.BAD_REQUEST_400, resp.getStatus());
206 assertTrue(resp.hasEntity());
207 ApiError errorObj = resp.readEntity(ApiError.class);
208 assertEquals("topicName", errorObj.getFields());
212 public void addTopic_shouldReturnValidationError_whenTopicDescriptionNotProvided() {
214 Topic topic = DMAAP_OBJECT_FACTORY.genSimpleTopic("topicName");
215 topic.setTopicDescription(null);
216 Entity<Topic> requestedEntity = Entity.entity(topic, MediaType.APPLICATION_JSON);
219 Response resp = testContainer.target(TOPICS_TARGET).request().post(requestedEntity, Response.class);
222 assertEquals(HttpStatus.BAD_REQUEST_400, resp.getStatus());
223 assertTrue(resp.hasEntity());
224 ApiError errorObj = resp.readEntity(ApiError.class);
225 assertEquals("topicDescription", errorObj.getFields());
229 public void addTopic_shouldReturnValidationError_whenTopicOwnerNotProvided() {
231 Topic topic = DMAAP_OBJECT_FACTORY.genSimpleTopic("topicName");
232 topic.setOwner(null);
233 Entity<Topic> requestedEntity = Entity.entity(topic, MediaType.APPLICATION_JSON);
236 Response resp = testContainer.target(TOPICS_TARGET).request().post(requestedEntity, Response.class);
239 assertEquals(HttpStatus.BAD_REQUEST_400, resp.getStatus());
240 assertTrue(resp.hasEntity());
241 ApiError errorObj = resp.readEntity(ApiError.class);
242 assertEquals("owner", errorObj.getFields());
246 public void addTopic_shouldReturnError_whenTopicAlreadyExist() {
248 Topic topic = DMAAP_OBJECT_FACTORY.genSimpleTopic("topicName");
249 DatabaseClass.getTopics().put(topic.getFqtn(), topic);
250 Entity<Topic> requestedEntity = Entity.entity(topic, MediaType.APPLICATION_JSON);
253 Response resp = testContainer.target(TOPICS_TARGET).request().post(requestedEntity, Response.class);
256 assertEquals(HttpStatus.CONFLICT_409, resp.getStatus());
257 assertTrue(resp.hasEntity());
258 ApiError errorObj = resp.readEntity(ApiError.class);
259 assertEquals("fqtn", errorObj.getFields());
263 public void addTopic_shouldReturnExistingTopic_whenTopicAlreadyExist_andUseExistingQueryParamUsed() {
265 Topic topic = DMAAP_OBJECT_FACTORY.genSimpleTopic("topicName");
266 DatabaseClass.getTopics().put(topic.getFqtn(), topic);
267 Entity<Topic> requestedEntity = Entity.entity(topic, MediaType.APPLICATION_JSON);
270 Response resp = testContainer.target(TOPICS_TARGET).queryParam("useExisting", true).request()
271 .post(requestedEntity, Response.class);
274 assertEquals(HttpStatus.CREATED_201, resp.getStatus());
275 assertTrue(resp.hasEntity());
276 assertEquals(topic, resp.readEntity(Topic.class));
280 public void addTopic_shouldReturnError_whenAddingTopicWithInvalidGlobalMRclusterHostname() {
281 Topic topic = DMAAP_OBJECT_FACTORY.genSimpleTopic("topicName");
282 topic.setReplicationCase(ReplicationType.REPLICATION_CENTRAL_TO_GLOBAL);
283 topic.setGlobalMrURL("some.invalid.Glob$al.M@R.ur)l");
284 Entity<Topic> requestedEntity = Entity.entity(topic, MediaType.APPLICATION_JSON);
287 Response resp = testContainer.target(TOPICS_TARGET).request().post(requestedEntity, Response.class);
290 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR_500, resp.getStatus());
291 assertTrue(resp.hasEntity());
292 ApiError errorObj = resp.readEntity(ApiError.class);
293 assertEquals("globalMrURL", errorObj.getFields());
297 public void addTopic_shouldAddTopicWithDefaultOptionalValues_whenNotProvided() {
298 Topic topic = DMAAP_OBJECT_FACTORY.genSimpleTopic("topicName");
299 Entity<Topic> requestedEntity = Entity.entity(topic, MediaType.APPLICATION_JSON);
302 Response resp = testContainer.target(TOPICS_TARGET).request().post(requestedEntity, Response.class);
305 assertEquals(HttpStatus.CREATED_201, resp.getStatus());
306 assertTrue(resp.hasEntity());
307 Topic createdTopic = resp.readEntity(Topic.class);
308 assertEquals(topic, createdTopic);
309 assertEquals(FqtnType.FQTN_LEGACY_FORMAT, createdTopic.getFqtnStyle());
310 assertEquals("2", createdTopic.getPartitionCount());
311 assertEquals("1", createdTopic.getReplicationCount());
315 public void addTopic_shouldAddTopicWithOriginalOptionalValues_whenProvided() {
316 Topic topic = DMAAP_OBJECT_FACTORY.genSimpleTopic("topicName");
317 topic.setFqtnStyle(FqtnType.FQTN_PROJECTID_FORMAT);
318 topic.setFqtn(topic.genFqtn());
319 topic.setPartitionCount("6");
320 topic.setReplicationCount("9");
321 Entity<Topic> requestedEntity = Entity.entity(topic, MediaType.APPLICATION_JSON);
324 Response resp = testContainer.target(TOPICS_TARGET).request().post(requestedEntity, Response.class);
327 assertEquals(HttpStatus.CREATED_201, resp.getStatus());
328 assertTrue(resp.hasEntity());
329 Topic createdTopic = resp.readEntity(Topic.class);
330 assertEquals(topic, createdTopic);
331 assertEquals(FqtnType.FQTN_PROJECTID_FORMAT, createdTopic.getFqtnStyle());
332 assertEquals("6", createdTopic.getPartitionCount());
333 assertEquals("9", createdTopic.getReplicationCount());
337 public void updateTopic_shouldReturnError_withInformationThatItIsNotSupported() {
339 Topic topic = DMAAP_OBJECT_FACTORY.genSimpleTopic("topicName");
340 DatabaseClass.getTopics().put(topic.getFqtn(), topic);
341 topic.setOwner("newOwner");
342 Entity<Topic> requestedEntity = Entity.entity(topic, MediaType.APPLICATION_JSON);
345 Response resp = testContainer.target(TOPICS_TARGET).path(topic.getFqtn()).request()
346 .put(requestedEntity, Response.class);
349 assertEquals(HttpStatus.BAD_REQUEST_400, resp.getStatus());
350 assertTrue(resp.hasEntity());
351 ApiError errorObj = resp.readEntity(ApiError.class);
352 assertEquals(TopicResource.UNSUPPORTED_PUT_MSG, errorObj.getMessage());