aboutsummaryrefslogtreecommitdiffstats
path: root/dmaap-bc/src/test/java/org/onap/dmaap/dbcapi/resources/DR_SubResourceTest.java
blob: 35c448282a37c4b14a2a38f894e9469636b7f19c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*-
 * ============LICENSE_START=======================================================
 * org.onap.dmaap
 * ================================================================================
 * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.dmaap.dbcapi.resources;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import org.glassfish.jersey.server.ResourceConfig;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.onap.dmaap.dbcapi.database.DatabaseClass;
import org.onap.dmaap.dbcapi.model.ApiError;
import org.onap.dmaap.dbcapi.model.DR_Sub;
import org.onap.dmaap.dbcapi.model.Feed;
import org.onap.dmaap.dbcapi.testframework.DmaapObjectFactory;

public class DR_SubResourceTest {

    private static final DmaapObjectFactory DMAAP_OBJECT_FACTORY = new DmaapObjectFactory();

    private static final String DCAE_LOCATION_NAME = "central-onap";
    private static final String USERNAME = "user1";
    private static final String USRPWD = "secretW0rd";
    private static final String DELIVERY_URL = "https://subscriber.onap.org/delivery/id";
    private static final String LOG_URL = "https://dr-prov/sublog/id";
    private static final String DELIVERY_URL_TEMPLATE = "https://subscriber.onap.org/delivery/";
    private static final String LOG_URL_TEMPLATE = "https://dr-prov/sublog/";
    private static FastJerseyTestContainer testContainer;
    private static TestFeedCreator testFeedCreator;

    @BeforeClass
    public static void setUpClass() throws Exception {
        System.setProperty("ConfigFile", "src/test/resources/dmaapbc.properties");
        //TODO: init is still needed here to assure that dmaap is not null
        DatabaseClass.getDmaap().init(DMAAP_OBJECT_FACTORY.genDmaap());

        testContainer = new FastJerseyTestContainer(new ResourceConfig()
            .register(DR_SubResource.class)
            .register(FeedResource.class));
        testContainer.init();
        testFeedCreator = new TestFeedCreator(testContainer);
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
        testContainer.destroy();
        /*TODO: Cannot cleanup yet until still other Resources tests depends on the static DB content

        DatabaseClass.clearDatabase();
        DatabaseClass.getDmaap().remove();*/
    }

    @Before
    public void cleanupDatabase() {
        DatabaseClass.clearDatabase();
    }

    //TODO: figure out generic entity list unmarshall to check the entity list
    @Test
    public void getDr_Subs_test() {
        Response resp = testContainer.target("dr_subs").request().get(Response.class);
        System.out.println("GET dr_subs resp=" + resp.getStatus());

        assertEquals(200, resp.getStatus());
        assertTrue(resp.hasEntity());
    }

    @Test
    public void addDr_Sub_shallReturnError_whenNoFeedIdAndFeedNameInSubProvided() {
        //given
        DR_Sub drSub = new DR_Sub(DCAE_LOCATION_NAME, USERNAME, USRPWD, null, DELIVERY_URL, LOG_URL, true);
        Entity<DR_Sub> requestedEntity = Entity.entity(drSub, MediaType.APPLICATION_JSON);

        //when
        Response resp = testContainer.target("dr_subs")
            .request()
            .post(requestedEntity, Response.class);

        //then
        assertEquals(400, resp.getStatus());
        ApiError responseError = resp.readEntity(ApiError.class);
        assertNotNull(responseError);
        assertEquals("feedName", responseError.getFields());
    }

    @Test
    public void addDr_Sub_shallReturnError_whenFeedNameProvided_butFeedNotExist() {
        //given
        String notExistingFeedName = "notRealFead";
        DR_Sub drSub = new DR_Sub(DCAE_LOCATION_NAME, USERNAME, USRPWD, null, DELIVERY_URL, LOG_URL, true);
        drSub.setFeedName(notExistingFeedName);
        Entity<DR_Sub> requestedEntity = Entity.entity(drSub, MediaType.APPLICATION_JSON);

        //when
        Response resp = testContainer.target("dr_subs")
            .request()
            .post(requestedEntity, Response.class);

        //then
        assertEquals(404, resp.getStatus());
        ApiError responseError = resp.readEntity(ApiError.class);
        assertNotNull(responseError);
        assertEquals("feedName", responseError.getFields());
    }

    @Test
    public void addDr_Sub_shallReturnError_whenFeedNameProvided_andManyFeedsWithTheSameNameInDB() {
        //given
        String notDistinctFeedName = "notDistinctFeedName";
        Feed feed1 = new Feed(notDistinctFeedName, "1.0", "description", "dgl", "unrestricted");
        Feed feed2 = new Feed(notDistinctFeedName, "2.0", "description", "dgl", "unrestricted");
        DatabaseClass.getFeeds().put("1", feed1);
        DatabaseClass.getFeeds().put("2", feed2);
        DR_Sub drSub = new DR_Sub(DCAE_LOCATION_NAME, USERNAME, USRPWD, null, DELIVERY_URL, LOG_URL, true);
        drSub.setFeedName(notDistinctFeedName);
        Entity<DR_Sub> requestedEntity = Entity.entity(drSub, MediaType.APPLICATION_JSON);

        //when
        Response resp = testContainer.target("dr_subs")
            .request()
            .post(requestedEntity, Response.class);

        //then
        assertEquals(409, resp.getStatus());
        ApiError responseError = resp.readEntity(ApiError.class);
        assertNotNull(responseError);
        assertEquals("feedName", responseError.getFields());
    }

    @Test
    public void addDr_Sub_shallReturnError_whenFeedIdProvided_butFeedNotExist() {
        //given
        DR_Sub drSub = new DR_Sub(DCAE_LOCATION_NAME, USERNAME, USRPWD, "someFakeFeedId", DELIVERY_URL, LOG_URL, true);
        Entity<DR_Sub> requestedEntity = Entity.entity(drSub, MediaType.APPLICATION_JSON);

        //when
        Response resp = testContainer.target("dr_subs")
            .request()
            .post(requestedEntity, Response.class);

        //then
        assertEquals(404, resp.getStatus());
        ApiError responseError = resp.readEntity(ApiError.class);
        assertNotNull(responseError);
        assertTrue(responseError.getFields().contains("feedId"));
    }

    @Test
    public void addDr_Sub_shallExecuteSuccessfully_whenValidFeedIdProvided() {
        //given
        String feedId = assureFeedIsInDB();
        DR_Sub drSub = new DR_Sub(DCAE_LOCATION_NAME, USERNAME, USRPWD, feedId, DELIVERY_URL, LOG_URL, true);
        Entity<DR_Sub> requestedEntity = Entity.entity(drSub, MediaType.APPLICATION_JSON);

        //when
        Response resp = testContainer.target("dr_subs")
            .request()
            .post(requestedEntity, Response.class);

        //then
        assertEquals(201, resp.getStatus());
        assertTrue(resp.hasEntity());
        DR_Sub created = resp.readEntity(DR_Sub.class);
        assertSubscriptionExistInDB(created);
    }

    @Test
    public void addDr_Sub_shallExecuteSuccessfully_whenValidFeedNameProvided() {
        //given
        String feedName = "testFeed";
        testFeedCreator.addFeed(feedName, "test feed");
        DR_Sub drSub = new DR_Sub(DCAE_LOCATION_NAME, USERNAME, USRPWD, null, DELIVERY_URL, LOG_URL, true);
        drSub.setFeedName(feedName);
        Entity<DR_Sub> requestedEntity = Entity.entity(drSub, MediaType.APPLICATION_JSON);

        //when
        Response resp = testContainer.target("dr_subs")
            .request()
            .post(requestedEntity, Response.class);

        //then
        assertEquals(201, resp.getStatus());
        assertTrue(resp.hasEntity());
        DR_Sub created = resp.readEntity(DR_Sub.class);
        assertSubscriptionExistInDB(created);
    }


    @Test
    public void updateDr_Sub_shallReturnError_whenNoFeedIdInSubProvided() {
        //given
        String subId = "1234";
        DR_Sub drSub = new DR_Sub(DCAE_LOCATION_NAME, USERNAME, USRPWD, null, DELIVERY_URL, LOG_URL, true);
        Entity<DR_Sub> requestedEntity = Entity.entity(drSub, MediaType.APPLICATION_JSON);

        //when
        Response resp = testContainer.target("dr_subs")
            .path(subId)
            .request()
            .put(requestedEntity, Response.class);

        //then
        assertEquals(400, resp.getStatus());
        ApiError responseError = resp.readEntity(ApiError.class);
        assertNotNull(responseError);
        assertEquals("feedId", responseError.getFields());
    }

    @Test
    public void updateDr_Sub_shallReturnError_whenNoDCAELocationInSubProvided() {
        //given
        String subId = "1234";
        DR_Sub drSub = new DR_Sub(null, USERNAME, USRPWD, "someFeedId", DELIVERY_URL, LOG_URL, true);
        Entity<DR_Sub> requestedEntity = Entity.entity(drSub, MediaType.APPLICATION_JSON);

        //when
        Response resp = testContainer.target("dr_subs")
            .path(subId)
            .request()
            .put(requestedEntity, Response.class);

        //then
        assertEquals(400, resp.getStatus());
        ApiError responseError = resp.readEntity(ApiError.class);
        assertNotNull(responseError);
        assertEquals("dcaeLocationName", responseError.getFields());
    }

    @Test
    public void updateDr_Sub_shallReturnError_whenFeedWithGivenIdInSubNotExists() {
        //given
        String subId = "1234";
        DR_Sub drSub = new DR_Sub(DCAE_LOCATION_NAME, USERNAME, USRPWD, "someFeedId", DELIVERY_URL, LOG_URL, true);
        Entity<DR_Sub> requestedEntity = Entity.entity(drSub, MediaType.APPLICATION_JSON);

        //when
        Response resp = testContainer.target("dr_subs")
            .path(subId)
            .request()
            .put(requestedEntity, Response.class);

        //then
        assertEquals(404, resp.getStatus());
        assertNotNull(resp.readEntity(ApiError.class));
    }

    @Test
    public void updateDr_Sub_shallReturnSuccess_whenAddingNewSubscription() {
        //given
        String subId = "31";
        String feedId = assureFeedIsInDB();
        DR_Sub drSub = new DR_Sub(DCAE_LOCATION_NAME, USERNAME, USRPWD, feedId, null, null, true);
        Entity<DR_Sub> requestedEntity = Entity.entity(drSub, MediaType.APPLICATION_JSON);

        //when
        Response resp = testContainer.target("dr_subs")
            .path(subId)
            .request()
            .put(requestedEntity, Response.class);

        //then
        assertEquals(200, resp.getStatus());

        DR_Sub createdDrSub = resp.readEntity(DR_Sub.class);
        assertNotNull(createdDrSub.getLastMod());
        assertEquals(subId, createdDrSub.getSubId());
        assertEquals(DCAE_LOCATION_NAME, createdDrSub.getDcaeLocationName());
        assertEquals(USERNAME, createdDrSub.getUsername());
        assertEquals(USRPWD, createdDrSub.getUserpwd());
        assertEquals(DELIVERY_URL_TEMPLATE + subId, createdDrSub.getDeliveryURL());
        assertEquals(LOG_URL_TEMPLATE + subId, createdDrSub.getLogURL());

        assertSubscriptionExistInDB(createdDrSub);
    }

    @Test
    public void updateDr_Sub_shallReturnSuccess_whenUpdatingExistingSubscription() {
        //given
        String feedId = assureFeedIsInDB();
        DR_Sub existingDrSub = addSub(DCAE_LOCATION_NAME, USERNAME, USRPWD, feedId);
        DR_Sub drSubUpdate = new DR_Sub("newDcaeLocationName", "newUserName", "newUserPwd", feedId, null, null, false);
        Entity<DR_Sub> requestedEntity = Entity.entity(drSubUpdate, MediaType.APPLICATION_JSON);

        //when
        Response resp = testContainer.target("dr_subs")
            .path(existingDrSub.getSubId())
            .request()
            .put(requestedEntity, Response.class);

        //then
        assertEquals(200, resp.getStatus());

        DR_Sub updatedDrSub = resp.readEntity(DR_Sub.class);
        assertNotNull(updatedDrSub.getLastMod());
        assertEquals(existingDrSub.getSubId(), updatedDrSub.getSubId());
        assertEquals(drSubUpdate.getDcaeLocationName(), updatedDrSub.getDcaeLocationName());
        assertEquals(drSubUpdate.getUsername(), updatedDrSub.getUsername());
        assertEquals(drSubUpdate.getUserpwd(), updatedDrSub.getUserpwd());
        assertEquals(DELIVERY_URL_TEMPLATE + existingDrSub.getSubId(), updatedDrSub.getDeliveryURL());
        assertEquals(LOG_URL_TEMPLATE + existingDrSub.getSubId(), updatedDrSub.getLogURL());

        assertSubscriptionExistInDB(updatedDrSub);
    }

    @Test
    public void deleteDr_Sub_shouldDeleteSubscriptionWithSuccess() {
        //given
        String feedId = assureFeedIsInDB();
        DR_Sub dr_sub = addSub(DCAE_LOCATION_NAME, USERNAME, USRPWD, feedId);

        //when
        Response resp = testContainer.target("dr_subs")
            .path(dr_sub.getSubId())
            .request()
            .delete();

        //then
        assertEquals("Shall delete subscription with success", 204, resp.getStatus());
        assertFalse("No entity object shall be returned",resp.hasEntity());
        assertSubscriptionNotExistInDB(dr_sub.getSubId());
    }

    @Test
    public void deleteDr_Sub_shouldReturnErrorResponse_whenGivenSubIdNotFound() {
        //given
        String notExistingSubId = "6789";

        //when
        Response resp = testContainer.target("dr_subs")
            .path(notExistingSubId)
            .request()
            .delete();

        //then
        assertEquals("Shall return error, when trying to delete not existing subscription", 404, resp.getStatus());
        assertNotNull(resp.readEntity(ApiError.class));
    }

    @Test
    public void get_shallReturnExistingObject() {
        //given
        String feedId = assureFeedIsInDB();
        DR_Sub dr_sub = addSub(DCAE_LOCATION_NAME, USERNAME, USRPWD, feedId);

        //when
        Response resp = testContainer.target("dr_subs")
            .path(dr_sub.getSubId())
            .request()
            .get();

        //ten
        assertEquals("Subscription shall be found",200, resp.getStatus());
        assertEquals("Retrieved object shall be equal to eh one put into DB", dr_sub, resp.readEntity(DR_Sub.class));
    }

    @Test
    public void get_shouldReturnError_whenSubWithIdNotFound() {
        //given
        String notExistingSubId = "1234";

        //when
        Response resp = testContainer.target("dr_subs")
            .path(notExistingSubId)
            .request()
            .get();

        //then
        assertEquals("Subscription shall not be found", 404, resp.getStatus());
        assertNotNull(resp.readEntity(ApiError.class));
    }

    private DR_Sub addSub(String d, String un, String up, String feedId) {
        DR_Sub dr_sub = new DR_Sub(d, un, up, feedId,
            "https://subscriber.onap.org/foo", "https://dr-prov/sublog", true);

        Entity<DR_Sub> reqEntity2 = Entity.entity(dr_sub, MediaType.APPLICATION_JSON);
        Response resp = testContainer.target("dr_subs").request().post(reqEntity2, Response.class);
        System.out.println("POST dr_subs resp=" + resp.getStatus());
        assertEquals(201, resp.getStatus());
        dr_sub = resp.readEntity(DR_Sub.class);

        return dr_sub;
    }

    private String assureFeedIsInDB() {
        Feed feed = testFeedCreator.addFeed("SubscriberTestFeed", "feed for DR_Sub testing");
        assertNotNull("Feed shall be added into DB properly", feed);
        return feed.getFeedId();
    }


    private void assertSubscriptionNotExistInDB(String subId) {
        assertEquals(404, testContainer.target("dr_subs")
            .path(subId)
            .request()
            .get()
            .getStatus());
    }

    private void assertSubscriptionExistInDB(DR_Sub sub) {
        Response response = testContainer.target("dr_subs")
            .path(sub.getSubId())
            .request()
            .get();
        assertEquals(200, response.getStatus());
        assertTrue(response.hasEntity());
        assertEquals(sub, response.readEntity(DR_Sub.class));
    }
}