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