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