13b89ea8d57bbffdef9b8de046cde794cea559dd
[dmaap/buscontroller.git] / dmaap-bc / src / test / java / org / onap / dmaap / dbcapi / resources / DR_SubResourceTest.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.assertFalse;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26
27 import javax.ws.rs.client.Entity;
28 import javax.ws.rs.core.MediaType;
29 import javax.ws.rs.core.Response;
30 import org.glassfish.jersey.server.ResourceConfig;
31 import org.junit.AfterClass;
32 import org.junit.Before;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.onap.dmaap.dbcapi.database.DatabaseClass;
36 import org.onap.dmaap.dbcapi.model.ApiError;
37 import org.onap.dmaap.dbcapi.model.DR_Sub;
38 import org.onap.dmaap.dbcapi.model.Feed;
39 import org.onap.dmaap.dbcapi.testframework.DmaapObjectFactory;
40
41 public class DR_SubResourceTest {
42
43     private static final DmaapObjectFactory DMAAP_OBJECT_FACTORY = new DmaapObjectFactory();
44
45     private static final String DCAE_LOCATION_NAME = "central-onap";
46     private static final String USERNAME = "user1";
47     private static final String USRPWD = "secretW0rd";
48     private static final String DELIVERY_URL = "https://subscriber.onap.org/delivery/id";
49     private static final String LOG_URL = "https://dr-prov/sublog/id";
50     private static final String DELIVERY_URL_TEMPLATE = "https://subscriber.onap.org/delivery/";
51     private static final String LOG_URL_TEMPLATE = "https://dr-prov/sublog/";
52     private static FastJerseyTestContainer testContainer;
53     private static TestFeedCreator testFeedCreator;
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(DR_SubResource.class)
62             .register(FeedResource.class));
63         testContainer.init();
64         testFeedCreator = new TestFeedCreator(testContainer);
65     }
66
67     @AfterClass
68     public static void tearDownClass() throws Exception {
69         testContainer.destroy();
70         /*TODO: Cannot cleanup yet until still other Resources tests depends on the static DB content
71
72         DatabaseClass.clearDatabase();
73         DatabaseClass.getDmaap().remove();*/
74     }
75
76     @Before
77     public void cleanupDatabase() {
78         DatabaseClass.clearDatabase();
79     }
80
81     //TODO: figure out generic entity list unmarshall to check the entity list
82     @Test
83     public void getDr_Subs_test() {
84         Response resp = testContainer.target("dr_subs").request().get(Response.class);
85         System.out.println("GET dr_subs resp=" + resp.getStatus());
86
87         assertEquals(200, resp.getStatus());
88         assertTrue(resp.hasEntity());
89     }
90
91     @Test
92     public void addDr_Sub_shallReturnError_whenNoFeedIdAndFeedNameInSubProvided() {
93         //given
94         DR_Sub drSub = new DR_Sub(DCAE_LOCATION_NAME, USERNAME, USRPWD, null, DELIVERY_URL, LOG_URL, true);
95         Entity<DR_Sub> requestedEntity = Entity.entity(drSub, MediaType.APPLICATION_JSON);
96
97         //when
98         Response resp = testContainer.target("dr_subs")
99             .request()
100             .post(requestedEntity, Response.class);
101
102         //then
103         assertEquals(400, resp.getStatus());
104         ApiError responseError = resp.readEntity(ApiError.class);
105         assertNotNull(responseError);
106         assertEquals("feedName", responseError.getFields());
107     }
108
109     @Test
110     public void addDr_Sub_shallReturnError_whenFeedNameProvided_butFeedNotExist() {
111         //given
112         String notExistingFeedName = "notRealFead";
113         DR_Sub drSub = new DR_Sub(DCAE_LOCATION_NAME, USERNAME, USRPWD, null, DELIVERY_URL, LOG_URL, true);
114         drSub.setFeedName(notExistingFeedName);
115         Entity<DR_Sub> requestedEntity = Entity.entity(drSub, MediaType.APPLICATION_JSON);
116
117         //when
118         Response resp = testContainer.target("dr_subs")
119             .request()
120             .post(requestedEntity, Response.class);
121
122         //then
123         assertEquals(404, resp.getStatus());
124         ApiError responseError = resp.readEntity(ApiError.class);
125         assertNotNull(responseError);
126         assertEquals("feedName", responseError.getFields());
127     }
128
129     @Test
130     public void addDr_Sub_shallReturnError_whenFeedNameProvided_andManyFeedsWithTheSameNameInDB() {
131         //given
132         String notDistinctFeedName = "notDistinctFeedName";
133         Feed feed1 = new Feed(notDistinctFeedName, "1.0", "description", "dgl", "unrestricted");
134         Feed feed2 = new Feed(notDistinctFeedName, "2.0", "description", "dgl", "unrestricted");
135         DatabaseClass.getFeeds().put("1", feed1);
136         DatabaseClass.getFeeds().put("2", feed2);
137         DR_Sub drSub = new DR_Sub(DCAE_LOCATION_NAME, USERNAME, USRPWD, null, DELIVERY_URL, LOG_URL, true);
138         drSub.setFeedName(notDistinctFeedName);
139         Entity<DR_Sub> requestedEntity = Entity.entity(drSub, MediaType.APPLICATION_JSON);
140
141         //when
142         Response resp = testContainer.target("dr_subs")
143             .request()
144             .post(requestedEntity, Response.class);
145
146         //then
147         assertEquals(409, resp.getStatus());
148         ApiError responseError = resp.readEntity(ApiError.class);
149         assertNotNull(responseError);
150         assertEquals("feedName", responseError.getFields());
151     }
152
153     @Test
154     public void addDr_Sub_shallReturnError_whenFeedIdProvided_butFeedNotExist() {
155         //given
156         DR_Sub drSub = new DR_Sub(DCAE_LOCATION_NAME, USERNAME, USRPWD, "someFakeFeedId", DELIVERY_URL, LOG_URL, true);
157         Entity<DR_Sub> requestedEntity = Entity.entity(drSub, MediaType.APPLICATION_JSON);
158
159         //when
160         Response resp = testContainer.target("dr_subs")
161             .request()
162             .post(requestedEntity, Response.class);
163
164         //then
165         assertEquals(404, resp.getStatus());
166         ApiError responseError = resp.readEntity(ApiError.class);
167         assertNotNull(responseError);
168         assertTrue(responseError.getFields().contains("feedId"));
169     }
170
171     @Test
172     public void addDr_Sub_shallExecuteSuccessfully_whenValidFeedIdProvided() {
173         //given
174         String feedId = assureFeedIsInDB();
175         DR_Sub drSub = new DR_Sub(DCAE_LOCATION_NAME, USERNAME, USRPWD, feedId, DELIVERY_URL, LOG_URL, true);
176         Entity<DR_Sub> requestedEntity = Entity.entity(drSub, MediaType.APPLICATION_JSON);
177
178         //when
179         Response resp = testContainer.target("dr_subs")
180             .request()
181             .post(requestedEntity, Response.class);
182
183         //then
184         assertEquals(201, resp.getStatus());
185         assertTrue(resp.hasEntity());
186         DR_Sub created = resp.readEntity(DR_Sub.class);
187         assertSubscriptionExistInDB(created);
188     }
189
190     @Test
191     public void addDr_Sub_shallExecuteSuccessfully_whenValidFeedNameProvided() {
192         //given
193         String feedName = "testFeed";
194         testFeedCreator.addFeed(feedName, "test feed");
195         DR_Sub drSub = new DR_Sub(DCAE_LOCATION_NAME, USERNAME, USRPWD, null, DELIVERY_URL, LOG_URL, true);
196         drSub.setFeedName(feedName);
197         Entity<DR_Sub> requestedEntity = Entity.entity(drSub, MediaType.APPLICATION_JSON);
198
199         //when
200         Response resp = testContainer.target("dr_subs")
201             .request()
202             .post(requestedEntity, Response.class);
203
204         //then
205         assertEquals(201, resp.getStatus());
206         assertTrue(resp.hasEntity());
207         DR_Sub created = resp.readEntity(DR_Sub.class);
208         assertSubscriptionExistInDB(created);
209     }
210
211
212     @Test
213     public void updateDr_Sub_shallReturnError_whenNoFeedIdInSubProvided() {
214         //given
215         String subId = "1234";
216         DR_Sub drSub = new DR_Sub(DCAE_LOCATION_NAME, USERNAME, USRPWD, null, DELIVERY_URL, LOG_URL, true);
217         Entity<DR_Sub> requestedEntity = Entity.entity(drSub, MediaType.APPLICATION_JSON);
218
219         //when
220         Response resp = testContainer.target("dr_subs")
221             .path(subId)
222             .request()
223             .put(requestedEntity, Response.class);
224
225         //then
226         assertEquals(400, resp.getStatus());
227         ApiError responseError = resp.readEntity(ApiError.class);
228         assertNotNull(responseError);
229         assertEquals("feedId", responseError.getFields());
230     }
231
232     @Test
233     public void updateDr_Sub_shallReturnError_whenNoDCAELocationInSubProvided() {
234         //given
235         String subId = "1234";
236         DR_Sub drSub = new DR_Sub(null, USERNAME, USRPWD, "someFeedId", DELIVERY_URL, LOG_URL, true);
237         Entity<DR_Sub> requestedEntity = Entity.entity(drSub, MediaType.APPLICATION_JSON);
238
239         //when
240         Response resp = testContainer.target("dr_subs")
241             .path(subId)
242             .request()
243             .put(requestedEntity, Response.class);
244
245         //then
246         assertEquals(400, resp.getStatus());
247         ApiError responseError = resp.readEntity(ApiError.class);
248         assertNotNull(responseError);
249         assertEquals("dcaeLocationName", responseError.getFields());
250     }
251
252     @Test
253     public void updateDr_Sub_shallReturnError_whenFeedWithGivenIdInSubNotExists() {
254         //given
255         String subId = "1234";
256         DR_Sub drSub = new DR_Sub(DCAE_LOCATION_NAME, USERNAME, USRPWD, "someFeedId", DELIVERY_URL, LOG_URL, true);
257         Entity<DR_Sub> requestedEntity = Entity.entity(drSub, MediaType.APPLICATION_JSON);
258
259         //when
260         Response resp = testContainer.target("dr_subs")
261             .path(subId)
262             .request()
263             .put(requestedEntity, Response.class);
264
265         //then
266         assertEquals(404, resp.getStatus());
267         assertNotNull(resp.readEntity(ApiError.class));
268     }
269
270     @Test
271     public void updateDr_Sub_shallReturnSuccess_whenAddingNewSubscription() {
272         //given
273         String subId = "31";
274         String feedId = assureFeedIsInDB();
275         DR_Sub drSub = new DR_Sub(DCAE_LOCATION_NAME, USERNAME, USRPWD, feedId, null, null, true);
276         Entity<DR_Sub> requestedEntity = Entity.entity(drSub, MediaType.APPLICATION_JSON);
277
278         //when
279         Response resp = testContainer.target("dr_subs")
280             .path(subId)
281             .request()
282             .put(requestedEntity, Response.class);
283
284         //then
285         assertEquals(200, resp.getStatus());
286
287         DR_Sub createdDrSub = resp.readEntity(DR_Sub.class);
288         assertNotNull(createdDrSub.getLastMod());
289         assertEquals(subId, createdDrSub.getSubId());
290         assertEquals(DCAE_LOCATION_NAME, createdDrSub.getDcaeLocationName());
291         assertEquals(USERNAME, createdDrSub.getUsername());
292         assertEquals(USRPWD, createdDrSub.getUserpwd());
293         assertEquals(DELIVERY_URL_TEMPLATE + subId, createdDrSub.getDeliveryURL());
294         assertEquals(LOG_URL_TEMPLATE + subId, createdDrSub.getLogURL());
295
296         assertSubscriptionExistInDB(createdDrSub);
297     }
298
299     @Test
300     public void updateDr_Sub_shallReturnSuccess_whenUpdatingExistingSubscription() {
301         //given
302         String feedId = assureFeedIsInDB();
303         DR_Sub existingDrSub = addSub(DCAE_LOCATION_NAME, USERNAME, USRPWD, feedId);
304         DR_Sub drSubUpdate = new DR_Sub("newDcaeLocationName", "newUserName", "newUserPwd", feedId, null, null, false);
305         Entity<DR_Sub> requestedEntity = Entity.entity(drSubUpdate, MediaType.APPLICATION_JSON);
306
307         //when
308         Response resp = testContainer.target("dr_subs")
309             .path(existingDrSub.getSubId())
310             .request()
311             .put(requestedEntity, Response.class);
312
313         //then
314         assertEquals(200, resp.getStatus());
315
316         DR_Sub updatedDrSub = resp.readEntity(DR_Sub.class);
317         assertNotNull(updatedDrSub.getLastMod());
318         assertEquals(existingDrSub.getSubId(), updatedDrSub.getSubId());
319         assertEquals(drSubUpdate.getDcaeLocationName(), updatedDrSub.getDcaeLocationName());
320         assertEquals(drSubUpdate.getUsername(), updatedDrSub.getUsername());
321         assertEquals(drSubUpdate.getUserpwd(), updatedDrSub.getUserpwd());
322         assertEquals(DELIVERY_URL_TEMPLATE + existingDrSub.getSubId(), updatedDrSub.getDeliveryURL());
323         assertEquals(LOG_URL_TEMPLATE + existingDrSub.getSubId(), updatedDrSub.getLogURL());
324
325         assertSubscriptionExistInDB(updatedDrSub);
326     }
327
328     @Test
329     public void deleteDr_Sub_shouldDeleteSubscriptionWithSuccess() {
330         //given
331         String feedId = assureFeedIsInDB();
332         DR_Sub dr_sub = addSub(DCAE_LOCATION_NAME, USERNAME, USRPWD, feedId);
333
334         //when
335         Response resp = testContainer.target("dr_subs")
336             .path(dr_sub.getSubId())
337             .request()
338             .delete();
339
340         //then
341         assertEquals("Shall delete subscription with success", 204, resp.getStatus());
342         assertFalse("No entity object shall be returned",resp.hasEntity());
343         assertSubscriptionNotExistInDB(dr_sub.getSubId());
344     }
345
346     @Test
347     public void deleteDr_Sub_shouldReturnErrorResponse_whenGivenSubIdNotFound() {
348         //given
349         String notExistingSubId = "6789";
350
351         //when
352         Response resp = testContainer.target("dr_subs")
353             .path(notExistingSubId)
354             .request()
355             .delete();
356
357         //then
358         assertEquals("Shall return error, when trying to delete not existing subscription", 404, resp.getStatus());
359         assertNotNull(resp.readEntity(ApiError.class));
360     }
361
362     @Test
363     public void get_shallReturnExistingObject() {
364         //given
365         String feedId = assureFeedIsInDB();
366         DR_Sub dr_sub = addSub(DCAE_LOCATION_NAME, USERNAME, USRPWD, feedId);
367
368         //when
369         Response resp = testContainer.target("dr_subs")
370             .path(dr_sub.getSubId())
371             .request()
372             .get();
373
374         //ten
375         assertEquals("Subscription shall be found",200, resp.getStatus());
376         assertEquals("Retrieved object shall be equal to eh one put into DB", dr_sub, resp.readEntity(DR_Sub.class));
377     }
378
379     @Test
380     public void get_shouldReturnError_whenSubWithIdNotFound() {
381         //given
382         String notExistingSubId = "1234";
383
384         //when
385         Response resp = testContainer.target("dr_subs")
386             .path(notExistingSubId)
387             .request()
388             .get();
389
390         //then
391         assertEquals("Subscription shall not be found", 404, resp.getStatus());
392         assertNotNull(resp.readEntity(ApiError.class));
393     }
394
395     private DR_Sub addSub(String d, String un, String up, String feedId) {
396         DR_Sub dr_sub = new DR_Sub(d, un, up, feedId,
397             "https://subscriber.onap.org/foo", "https://dr-prov/sublog", true);
398
399         Entity<DR_Sub> reqEntity2 = Entity.entity(dr_sub, MediaType.APPLICATION_JSON);
400         Response resp = testContainer.target("dr_subs").request().post(reqEntity2, Response.class);
401         System.out.println("POST dr_subs resp=" + resp.getStatus());
402         assertEquals(201, resp.getStatus());
403         dr_sub = resp.readEntity(DR_Sub.class);
404
405         return dr_sub;
406     }
407
408     private String assureFeedIsInDB() {
409         Feed feed = testFeedCreator.addFeed("SubscriberTestFeed", "feed for DR_Sub testing");
410         assertNotNull("Feed shall be added into DB properly", feed);
411         return feed.getFeedId();
412     }
413
414
415     private void assertSubscriptionNotExistInDB(String subId) {
416         assertEquals(404, testContainer.target("dr_subs")
417             .path(subId)
418             .request()
419             .get()
420             .getStatus());
421     }
422
423     private void assertSubscriptionExistInDB(DR_Sub sub) {
424         Response response = testContainer.target("dr_subs")
425             .path(sub.getSubId())
426             .request()
427             .get();
428         assertEquals(200, response.getStatus());
429         assertTrue(response.hasEntity());
430         assertEquals(sub, response.readEntity(DR_Sub.class));
431     }
432 }
433
434