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