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