[DMAAP-BC] Consolidate bus controller repos
[dmaap/buscontroller.git] / dmaap-bc / src / test / java / org / onap / dmaap / dbcapi / resources / TopicResourceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20 package org.onap.dmaap.dbcapi.resources;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.assertFalse;
25
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;
31
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;
47
48 public class TopicResourceTest {
49
50     private static final DmaapObjectFactory DMAAP_OBJECT_FACTORY = new DmaapObjectFactory();
51     private static final String TOPICS_TARGET = "topics";
52
53     private static FastJerseyTestContainer testContainer;
54
55     @BeforeClass
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());
59
60         testContainer = new FastJerseyTestContainer(new ResourceConfig()
61             .register(TopicResource.class));
62         testContainer.init();
63     }
64
65     @AfterClass
66     public static void tearDownClass() throws Exception {
67         testContainer.destroy();
68     }
69
70     @Before
71     public void setUpClusterAndLocation() {
72         DatabaseClass.clearDatabase();
73
74         DcaeLocation centralDcaeLoc = DMAAP_OBJECT_FACTORY.genDcaeLocation("central");
75         centralDcaeLoc.setStatus(DmaapObject_Status.VALID);
76         DatabaseClass.getDcaeLocations().put(centralDcaeLoc.getDcaeLocationName(), centralDcaeLoc);
77
78         MR_Cluster cluster = DMAAP_OBJECT_FACTORY.genMR_Cluster("central");
79         cluster.setStatus(DmaapObject_Status.VALID);
80         DatabaseClass.getMr_clusters().put(cluster.getDcaeLocationName(), cluster);
81     }
82
83     @Test
84     public void getTopics_shouldReturnEmptyList_whenNoTopicsInDataBase() {
85         //when
86         Response resp = testContainer.target(TOPICS_TARGET).request().get(Response.class);
87
88         //then
89         assertEquals(HttpStatus.OK_200, resp.getStatus());
90         assertTrue(resp.hasEntity());
91
92         List<Topic> topics = resp.readEntity(new GenericType<List<Topic>>() {
93         });
94         assertTrue(topics.isEmpty());
95     }
96
97     @Test
98     public void getTopics_shouldReturnTopicsRegisteredInDataBase() {
99         //given
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);
104
105         //when
106         Response resp = testContainer.target(TOPICS_TARGET).request().get(Response.class);
107
108         //then
109         assertEquals(HttpStatus.OK_200, resp.getStatus());
110         assertTrue(resp.hasEntity());
111
112         List<Topic> topics = resp.readEntity(new GenericType<List<Topic>>() {
113         });
114         assertEquals(2, topics.size());
115         assertTrue(topics.contains(topic1));
116         assertTrue(topics.contains(topic2));
117     }
118
119     @Test
120     public void getTopics_shouldReturnValidationError_whenTopicNameIsInvalid() {
121         //given
122         String topicName = "wrong Topic Name";
123
124         //when
125         Response resp = testContainer.target(TOPICS_TARGET).path(topicName).request().get(Response.class);
126
127         //then
128         assertEquals(HttpStatus.BAD_REQUEST_400, resp.getStatus());
129         assertTrue(resp.hasEntity());
130         ApiError errorObj = resp.readEntity(ApiError.class);
131         assertEquals("topicName", errorObj.getFields());
132     }
133
134     @Test
135     public void getTopic_shouldReturnError_whenRequestedTopicNotFound() {
136         //given
137         String topicName = "notExistingTopic";
138
139         //when
140         Response resp = testContainer.target(TOPICS_TARGET).path(topicName).request().get(Response.class);
141
142         //then
143         assertEquals(HttpStatus.NOT_FOUND_404, resp.getStatus());
144         assertTrue(resp.hasEntity());
145         ApiError errorObj = resp.readEntity(ApiError.class);
146         assertEquals("fqtn", errorObj.getFields());
147     }
148
149     @Test
150     public void getTopic_shouldReturnTopicInformation_whenRequestedTopicExists() {
151         //given
152         Topic topic1 = DMAAP_OBJECT_FACTORY.genSimpleTopic("testTopic1");
153         DatabaseClass.getTopics().put(topic1.getFqtn(), topic1);
154
155         //when
156         Response resp = testContainer.target(TOPICS_TARGET).path(topic1.getFqtn()).request().get(Response.class);
157
158         //then
159         assertEquals(HttpStatus.OK_200, resp.getStatus());
160         assertTrue(resp.hasEntity());
161         Topic retrievedTopic = resp.readEntity(Topic.class);
162         assertEquals(topic1, retrievedTopic);
163     }
164
165
166     @Test
167     public void deleteTopic_shouldReturnError_whenTopicNotFound() {
168         //given
169         String topicName = "notExisting";
170
171         //when
172         Response resp = testContainer.target(TOPICS_TARGET).path(topicName).request().delete(Response.class);
173
174         //then
175         assertEquals(HttpStatus.NOT_FOUND_404, resp.getStatus());
176         assertTrue(resp.hasEntity());
177         ApiError errorObj = resp.readEntity(ApiError.class);
178         assertEquals("fqtn", errorObj.getFields());
179     }
180
181     @Test
182     public void deleteTopic_shouldDeleteTopicFromDataBase_whenFound() {
183         //given
184         Topic topic = DMAAP_OBJECT_FACTORY.genSimpleTopic("testTopic");
185         DatabaseClass.getTopics().put(topic.getFqtn(), topic);
186
187         //when
188         Response resp = testContainer.target(TOPICS_TARGET).path(topic.getFqtn()).request().delete(Response.class);
189
190         //then
191         assertEquals(HttpStatus.NO_CONTENT_204, resp.getStatus());
192         assertFalse(resp.hasEntity());
193     }
194
195     @Test
196     public void addTopic_shouldReturnValidationError_whenTopicNameIsInvalid() {
197         //given
198         Topic topic = DMAAP_OBJECT_FACTORY.genSimpleTopic("wrong topic name with spaces");
199         Entity<Topic> requestedEntity = Entity.entity(topic, MediaType.APPLICATION_JSON);
200
201         //when
202         Response resp = testContainer.target(TOPICS_TARGET).request().post(requestedEntity, Response.class);
203
204         //then
205         assertEquals(HttpStatus.BAD_REQUEST_400, resp.getStatus());
206         assertTrue(resp.hasEntity());
207         ApiError errorObj = resp.readEntity(ApiError.class);
208         assertEquals("topicName", errorObj.getFields());
209     }
210
211     @Test
212     public void addTopic_shouldReturnValidationError_whenTopicDescriptionNotProvided() {
213         //given
214         Topic topic = DMAAP_OBJECT_FACTORY.genSimpleTopic("topicName");
215         topic.setTopicDescription(null);
216         Entity<Topic> requestedEntity = Entity.entity(topic, MediaType.APPLICATION_JSON);
217
218         //when
219         Response resp = testContainer.target(TOPICS_TARGET).request().post(requestedEntity, Response.class);
220
221         //then
222         assertEquals(HttpStatus.BAD_REQUEST_400, resp.getStatus());
223         assertTrue(resp.hasEntity());
224         ApiError errorObj = resp.readEntity(ApiError.class);
225         assertEquals("topicDescription", errorObj.getFields());
226     }
227
228     @Test
229     public void addTopic_shouldReturnValidationError_whenTopicOwnerNotProvided() {
230         //given
231         Topic topic = DMAAP_OBJECT_FACTORY.genSimpleTopic("topicName");
232         topic.setOwner(null);
233         Entity<Topic> requestedEntity = Entity.entity(topic, MediaType.APPLICATION_JSON);
234
235         //when
236         Response resp = testContainer.target(TOPICS_TARGET).request().post(requestedEntity, Response.class);
237
238         //then
239         assertEquals(HttpStatus.BAD_REQUEST_400, resp.getStatus());
240         assertTrue(resp.hasEntity());
241         ApiError errorObj = resp.readEntity(ApiError.class);
242         assertEquals("owner", errorObj.getFields());
243     }
244
245     @Test
246     public void addTopic_shouldReturnError_whenTopicAlreadyExist() {
247         //given
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);
251
252         //when
253         Response resp = testContainer.target(TOPICS_TARGET).request().post(requestedEntity, Response.class);
254
255         //then
256         assertEquals(HttpStatus.CONFLICT_409, resp.getStatus());
257         assertTrue(resp.hasEntity());
258         ApiError errorObj = resp.readEntity(ApiError.class);
259         assertEquals("fqtn", errorObj.getFields());
260     }
261
262     @Test
263     public void addTopic_shouldReturnExistingTopic_whenTopicAlreadyExist_andUseExistingQueryParamUsed() {
264         //given
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);
268
269         //when
270         Response resp = testContainer.target(TOPICS_TARGET).queryParam("useExisting", true).request()
271             .post(requestedEntity, Response.class);
272
273         //then
274         assertEquals(HttpStatus.CREATED_201, resp.getStatus());
275         assertTrue(resp.hasEntity());
276         assertEquals(topic, resp.readEntity(Topic.class));
277     }
278
279     @Test
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);
285
286         //when
287         Response resp = testContainer.target(TOPICS_TARGET).request().post(requestedEntity, Response.class);
288
289         //then
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());
294     }
295
296     @Test
297     public void addTopic_shouldAddTopicWithDefaultOptionalValues_whenNotProvided() {
298         Topic topic = DMAAP_OBJECT_FACTORY.genSimpleTopic("topicName");
299         Entity<Topic> requestedEntity = Entity.entity(topic, MediaType.APPLICATION_JSON);
300
301         //when
302         Response resp = testContainer.target(TOPICS_TARGET).request().post(requestedEntity, Response.class);
303
304         //then
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());
312     }
313
314     @Test
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);
322
323         //when
324         Response resp = testContainer.target(TOPICS_TARGET).request().post(requestedEntity, Response.class);
325
326         //then
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());
334     }
335
336     @Test
337     public void updateTopic_shouldReturnError_withInformationThatItIsNotSupported() {
338         //given
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);
343
344         //when
345         Response resp = testContainer.target(TOPICS_TARGET).path(topic.getFqtn()).request()
346             .put(requestedEntity, Response.class);
347
348         //then
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());
353     }
354
355 }
356