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 System.setProperty("ConfigFile", "src/test/resources/dmaapbc.properties");
58 //TODO: init is still needed here to assure that dmaap is not null
59 DatabaseClass.getDmaap().init(DMAAP_OBJECT_FACTORY.genDmaap());
61 testContainer = new FastJerseyTestContainer(new ResourceConfig()
62 .register(TopicResource.class));
67 public static void tearDownClass() throws Exception {
68 testContainer.destroy();
72 public void setUpClusterAndLocation() {
73 DatabaseClass.clearDatabase();
75 DcaeLocation centralDcaeLoc = DMAAP_OBJECT_FACTORY.genDcaeLocation("central");
76 centralDcaeLoc.setStatus(DmaapObject_Status.VALID);
77 DatabaseClass.getDcaeLocations().put(centralDcaeLoc.getDcaeLocationName(), centralDcaeLoc);
79 MR_Cluster cluster = DMAAP_OBJECT_FACTORY.genMR_Cluster("central");
80 cluster.setStatus(DmaapObject_Status.VALID);
81 DatabaseClass.getMr_clusters().put(cluster.getDcaeLocationName(), cluster);
85 public void getTopics_shouldReturnEmptyList_whenNoTopicsInDataBase() {
87 Response resp = testContainer.target(TOPICS_TARGET).request().get(Response.class);
90 assertEquals(HttpStatus.OK_200, resp.getStatus());
91 assertTrue(resp.hasEntity());
93 List<Topic> topics = resp.readEntity(new GenericType<List<Topic>>() {
95 assertTrue(topics.isEmpty());
99 public void getTopics_shouldReturnTopicsRegisteredInDataBase() {
101 Topic topic1 = DMAAP_OBJECT_FACTORY.genSimpleTopic("testTopic1");
102 Topic topic2 = DMAAP_OBJECT_FACTORY.genSimpleTopic("testTopic2");
103 DatabaseClass.getTopics().put(topic1.getFqtn(), topic1);
104 DatabaseClass.getTopics().put(topic2.getFqtn(), topic2);
107 Response resp = testContainer.target(TOPICS_TARGET).request().get(Response.class);
110 assertEquals(HttpStatus.OK_200, resp.getStatus());
111 assertTrue(resp.hasEntity());
113 List<Topic> topics = resp.readEntity(new GenericType<List<Topic>>() {
115 assertEquals(2, topics.size());
116 assertTrue(topics.contains(topic1));
117 assertTrue(topics.contains(topic2));
121 public void getTopics_shouldReturnValidationError_whenTopicNameIsInvalid() {
123 String topicName = "wrong Topic Name";
126 Response resp = testContainer.target(TOPICS_TARGET).path(topicName).request().get(Response.class);
129 assertEquals(HttpStatus.BAD_REQUEST_400, resp.getStatus());
130 assertTrue(resp.hasEntity());
131 ApiError errorObj = resp.readEntity(ApiError.class);
132 assertEquals("topicName", errorObj.getFields());
136 public void getTopic_shouldReturnError_whenRequestedTopicNotFound() {
138 String topicName = "notExistingTopic";
141 Response resp = testContainer.target(TOPICS_TARGET).path(topicName).request().get(Response.class);
144 assertEquals(HttpStatus.NOT_FOUND_404, resp.getStatus());
145 assertTrue(resp.hasEntity());
146 ApiError errorObj = resp.readEntity(ApiError.class);
147 assertEquals("fqtn", errorObj.getFields());
151 public void getTopic_shouldReturnTopicInformation_whenRequestedTopicExists() {
153 Topic topic1 = DMAAP_OBJECT_FACTORY.genSimpleTopic("testTopic1");
154 DatabaseClass.getTopics().put(topic1.getFqtn(), topic1);
157 Response resp = testContainer.target(TOPICS_TARGET).path(topic1.getFqtn()).request().get(Response.class);
160 assertEquals(HttpStatus.OK_200, resp.getStatus());
161 assertTrue(resp.hasEntity());
162 Topic retrievedTopic = resp.readEntity(Topic.class);
163 assertEquals(topic1, retrievedTopic);
168 public void deleteTopic_shouldReturnError_whenTopicNotFound() {
170 String topicName = "notExisting";
173 Response resp = testContainer.target(TOPICS_TARGET).path(topicName).request().delete(Response.class);
176 assertEquals(HttpStatus.NOT_FOUND_404, resp.getStatus());
177 assertTrue(resp.hasEntity());
178 ApiError errorObj = resp.readEntity(ApiError.class);
179 assertEquals("fqtn", errorObj.getFields());
183 public void deleteTopic_shouldDeleteTopicFromDataBase_whenFound() {
185 Topic topic = DMAAP_OBJECT_FACTORY.genSimpleTopic("testTopic");
186 DatabaseClass.getTopics().put(topic.getFqtn(), topic);
189 Response resp = testContainer.target(TOPICS_TARGET).path(topic.getFqtn()).request().delete(Response.class);
192 assertEquals(HttpStatus.NO_CONTENT_204, resp.getStatus());
193 assertFalse(resp.hasEntity());
197 public void addTopic_shouldReturnValidationError_whenTopicNameIsInvalid() {
199 Topic topic = DMAAP_OBJECT_FACTORY.genSimpleTopic("wrong topic name with spaces");
200 Entity<Topic> requestedEntity = Entity.entity(topic, MediaType.APPLICATION_JSON);
203 Response resp = testContainer.target(TOPICS_TARGET).request().post(requestedEntity, Response.class);
206 assertEquals(HttpStatus.BAD_REQUEST_400, resp.getStatus());
207 assertTrue(resp.hasEntity());
208 ApiError errorObj = resp.readEntity(ApiError.class);
209 assertEquals("topicName", errorObj.getFields());
213 public void addTopic_shouldReturnValidationError_whenTopicDescriptionNotProvided() {
215 Topic topic = DMAAP_OBJECT_FACTORY.genSimpleTopic("topicName");
216 topic.setTopicDescription(null);
217 Entity<Topic> requestedEntity = Entity.entity(topic, MediaType.APPLICATION_JSON);
220 Response resp = testContainer.target(TOPICS_TARGET).request().post(requestedEntity, Response.class);
223 assertEquals(HttpStatus.BAD_REQUEST_400, resp.getStatus());
224 assertTrue(resp.hasEntity());
225 ApiError errorObj = resp.readEntity(ApiError.class);
226 assertEquals("topicDescription", errorObj.getFields());
230 public void addTopic_shouldReturnValidationError_whenTopicOwnerNotProvided() {
232 Topic topic = DMAAP_OBJECT_FACTORY.genSimpleTopic("topicName");
233 topic.setOwner(null);
234 Entity<Topic> requestedEntity = Entity.entity(topic, MediaType.APPLICATION_JSON);
237 Response resp = testContainer.target(TOPICS_TARGET).request().post(requestedEntity, Response.class);
240 assertEquals(HttpStatus.BAD_REQUEST_400, resp.getStatus());
241 assertTrue(resp.hasEntity());
242 ApiError errorObj = resp.readEntity(ApiError.class);
243 assertEquals("owner", errorObj.getFields());
247 public void addTopic_shouldReturnError_whenTopicAlreadyExist() {
249 Topic topic = DMAAP_OBJECT_FACTORY.genSimpleTopic("topicName");
250 DatabaseClass.getTopics().put(topic.getFqtn(), topic);
251 Entity<Topic> requestedEntity = Entity.entity(topic, MediaType.APPLICATION_JSON);
254 Response resp = testContainer.target(TOPICS_TARGET).request().post(requestedEntity, Response.class);
257 assertEquals(HttpStatus.CONFLICT_409, resp.getStatus());
258 assertTrue(resp.hasEntity());
259 ApiError errorObj = resp.readEntity(ApiError.class);
260 assertEquals("fqtn", errorObj.getFields());
264 public void addTopic_shouldReturnExistingTopic_whenTopicAlreadyExist_andUseExistingQueryParamUsed() {
266 Topic topic = DMAAP_OBJECT_FACTORY.genSimpleTopic("topicName");
267 DatabaseClass.getTopics().put(topic.getFqtn(), topic);
268 Entity<Topic> requestedEntity = Entity.entity(topic, MediaType.APPLICATION_JSON);
271 Response resp = testContainer.target(TOPICS_TARGET).queryParam("useExisting", true).request()
272 .post(requestedEntity, Response.class);
275 assertEquals(HttpStatus.CREATED_201, resp.getStatus());
276 assertTrue(resp.hasEntity());
277 assertEquals(topic, resp.readEntity(Topic.class));
281 public void addTopic_shouldReturnError_whenAddingTopicWithInvalidGlobalMRclusterHostname() {
282 Topic topic = DMAAP_OBJECT_FACTORY.genSimpleTopic("topicName");
283 topic.setReplicationCase(ReplicationType.REPLICATION_CENTRAL_TO_GLOBAL);
284 topic.setGlobalMrURL("some.invalid.Glob$al.M@R.ur)l");
285 Entity<Topic> requestedEntity = Entity.entity(topic, MediaType.APPLICATION_JSON);
288 Response resp = testContainer.target(TOPICS_TARGET).request().post(requestedEntity, Response.class);
291 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR_500, resp.getStatus());
292 assertTrue(resp.hasEntity());
293 ApiError errorObj = resp.readEntity(ApiError.class);
294 assertEquals("globalMrURL", errorObj.getFields());
298 public void addTopic_shouldAddTopicWithDefaultOptionalValues_whenNotProvided() {
299 Topic topic = DMAAP_OBJECT_FACTORY.genSimpleTopic("topicName");
300 Entity<Topic> requestedEntity = Entity.entity(topic, MediaType.APPLICATION_JSON);
303 Response resp = testContainer.target(TOPICS_TARGET).request().post(requestedEntity, Response.class);
306 assertEquals(HttpStatus.CREATED_201, resp.getStatus());
307 assertTrue(resp.hasEntity());
308 Topic createdTopic = resp.readEntity(Topic.class);
309 assertEquals(topic, createdTopic);
310 assertEquals(FqtnType.FQTN_LEGACY_FORMAT, createdTopic.getFqtnStyle());
311 assertEquals("2", createdTopic.getPartitionCount());
312 assertEquals("1", createdTopic.getReplicationCount());
316 public void addTopic_shouldAddTopicWithOriginalOptionalValues_whenProvided() {
317 Topic topic = DMAAP_OBJECT_FACTORY.genSimpleTopic("topicName");
318 topic.setFqtnStyle(FqtnType.FQTN_PROJECTID_FORMAT);
319 topic.setFqtn(topic.genFqtn());
320 topic.setPartitionCount("6");
321 topic.setReplicationCount("9");
322 Entity<Topic> requestedEntity = Entity.entity(topic, MediaType.APPLICATION_JSON);
325 Response resp = testContainer.target(TOPICS_TARGET).request().post(requestedEntity, Response.class);
328 assertEquals(HttpStatus.CREATED_201, resp.getStatus());
329 assertTrue(resp.hasEntity());
330 Topic createdTopic = resp.readEntity(Topic.class);
331 assertEquals(topic, createdTopic);
332 assertEquals(FqtnType.FQTN_PROJECTID_FORMAT, createdTopic.getFqtnStyle());
333 assertEquals("6", createdTopic.getPartitionCount());
334 assertEquals("9", createdTopic.getReplicationCount());
338 public void updateTopic_shouldReturnError_withInformationThatItIsNotSupported() {
340 Topic topic = DMAAP_OBJECT_FACTORY.genSimpleTopic("topicName");
341 DatabaseClass.getTopics().put(topic.getFqtn(), topic);
342 topic.setOwner("newOwner");
343 Entity<Topic> requestedEntity = Entity.entity(topic, MediaType.APPLICATION_JSON);
346 Response resp = testContainer.target(TOPICS_TARGET).path(topic.getFqtn()).request()
347 .put(requestedEntity, Response.class);
350 assertEquals(HttpStatus.BAD_REQUEST_400, resp.getStatus());
351 assertTrue(resp.hasEntity());
352 ApiError errorObj = resp.readEntity(ApiError.class);
353 assertEquals(TopicResource.UNSUPPORTED_PUT_MSG, errorObj.getMessage());