2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.dmaap.dbcapi.resources;
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;
27 import jakarta.ws.rs.client.Entity;
28 import jakarta.ws.rs.core.MediaType;
29 import jakarta.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;
41 public class DR_SubResourceTest {
43 private static final DmaapObjectFactory DMAAP_OBJECT_FACTORY = new DmaapObjectFactory();
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;
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());
61 testContainer = new FastJerseyTestContainer(new ResourceConfig()
62 .register(DR_SubResource.class)
63 .register(FeedResource.class));
65 testFeedCreator = new TestFeedCreator(testContainer);
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
73 DatabaseClass.clearDatabase();
74 DatabaseClass.getDmaap().remove();*/
78 public void cleanupDatabase() {
79 DatabaseClass.clearDatabase();
82 //TODO: figure out generic entity list unmarshall to check the entity list
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());
88 assertEquals(200, resp.getStatus());
89 assertTrue(resp.hasEntity());
93 public void addDr_Sub_shallReturnError_whenNoFeedIdAndFeedNameInSubProvided() {
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);
99 Response resp = testContainer.target("dr_subs")
101 .post(requestedEntity, Response.class);
104 assertEquals(400, resp.getStatus());
105 ApiError responseError = resp.readEntity(ApiError.class);
106 assertNotNull(responseError);
107 assertEquals("feedName", responseError.getFields());
111 public void addDr_Sub_shallReturnError_whenFeedNameProvided_butFeedNotExist() {
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);
119 Response resp = testContainer.target("dr_subs")
121 .post(requestedEntity, Response.class);
124 assertEquals(404, resp.getStatus());
125 ApiError responseError = resp.readEntity(ApiError.class);
126 assertNotNull(responseError);
127 assertEquals("feedName", responseError.getFields());
131 public void addDr_Sub_shallReturnError_whenFeedNameProvided_andManyFeedsWithTheSameNameInDB() {
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);
143 Response resp = testContainer.target("dr_subs")
145 .post(requestedEntity, Response.class);
148 assertEquals(409, resp.getStatus());
149 ApiError responseError = resp.readEntity(ApiError.class);
150 assertNotNull(responseError);
151 assertEquals("feedName", responseError.getFields());
155 public void addDr_Sub_shallReturnError_whenFeedIdProvided_butFeedNotExist() {
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);
161 Response resp = testContainer.target("dr_subs")
163 .post(requestedEntity, Response.class);
166 assertEquals(404, resp.getStatus());
167 ApiError responseError = resp.readEntity(ApiError.class);
168 assertNotNull(responseError);
169 assertTrue(responseError.getFields().contains("feedId"));
173 public void addDr_Sub_shallExecuteSuccessfully_whenValidFeedIdProvided() {
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);
180 Response resp = testContainer.target("dr_subs")
182 .post(requestedEntity, Response.class);
185 assertEquals(201, resp.getStatus());
186 assertTrue(resp.hasEntity());
187 DR_Sub created = resp.readEntity(DR_Sub.class);
188 assertSubscriptionExistInDB(created);
192 public void addDr_Sub_shallExecuteSuccessfully_whenValidFeedNameProvided() {
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);
201 Response resp = testContainer.target("dr_subs")
203 .post(requestedEntity, Response.class);
206 assertEquals(201, resp.getStatus());
207 assertTrue(resp.hasEntity());
208 DR_Sub created = resp.readEntity(DR_Sub.class);
209 assertSubscriptionExistInDB(created);
214 public void updateDr_Sub_shallReturnError_whenNoFeedIdInSubProvided() {
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);
221 Response resp = testContainer.target("dr_subs")
224 .put(requestedEntity, Response.class);
227 assertEquals(400, resp.getStatus());
228 ApiError responseError = resp.readEntity(ApiError.class);
229 assertNotNull(responseError);
230 assertEquals("feedId", responseError.getFields());
234 public void updateDr_Sub_shallReturnError_whenNoDCAELocationInSubProvided() {
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);
241 Response resp = testContainer.target("dr_subs")
244 .put(requestedEntity, Response.class);
247 assertEquals(400, resp.getStatus());
248 ApiError responseError = resp.readEntity(ApiError.class);
249 assertNotNull(responseError);
250 assertEquals("dcaeLocationName", responseError.getFields());
254 public void updateDr_Sub_shallReturnError_whenFeedWithGivenIdInSubNotExists() {
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);
261 Response resp = testContainer.target("dr_subs")
264 .put(requestedEntity, Response.class);
267 assertEquals(404, resp.getStatus());
268 assertNotNull(resp.readEntity(ApiError.class));
272 public void updateDr_Sub_shallReturnSuccess_whenAddingNewSubscription() {
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);
280 Response resp = testContainer.target("dr_subs")
283 .put(requestedEntity, Response.class);
286 assertEquals(200, resp.getStatus());
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());
297 assertSubscriptionExistInDB(createdDrSub);
301 public void updateDr_Sub_shallReturnSuccess_whenUpdatingExistingSubscription() {
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);
309 Response resp = testContainer.target("dr_subs")
310 .path(existingDrSub.getSubId())
312 .put(requestedEntity, Response.class);
315 assertEquals(200, resp.getStatus());
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());
326 assertSubscriptionExistInDB(updatedDrSub);
330 public void deleteDr_Sub_shouldDeleteSubscriptionWithSuccess() {
332 String feedId = assureFeedIsInDB();
333 DR_Sub dr_sub = addSub(DCAE_LOCATION_NAME, USERNAME, USRPWD, feedId);
336 Response resp = testContainer.target("dr_subs")
337 .path(dr_sub.getSubId())
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());
348 public void deleteDr_Sub_shouldReturnErrorResponse_whenGivenSubIdNotFound() {
350 String notExistingSubId = "6789";
353 Response resp = testContainer.target("dr_subs")
354 .path(notExistingSubId)
359 assertEquals("Shall return error, when trying to delete not existing subscription", 404, resp.getStatus());
360 assertNotNull(resp.readEntity(ApiError.class));
364 public void get_shallReturnExistingObject() {
366 String feedId = assureFeedIsInDB();
367 DR_Sub dr_sub = addSub(DCAE_LOCATION_NAME, USERNAME, USRPWD, feedId);
370 Response resp = testContainer.target("dr_subs")
371 .path(dr_sub.getSubId())
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));
381 public void get_shouldReturnError_whenSubWithIdNotFound() {
383 String notExistingSubId = "1234";
386 Response resp = testContainer.target("dr_subs")
387 .path(notExistingSubId)
392 assertEquals("Subscription shall not be found", 404, resp.getStatus());
393 assertNotNull(resp.readEntity(ApiError.class));
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);
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);
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();
416 private void assertSubscriptionNotExistInDB(String subId) {
417 assertEquals(404, testContainer.target("dr_subs")
424 private void assertSubscriptionExistInDB(DR_Sub sub) {
425 Response response = testContainer.target("dr_subs")
426 .path(sub.getSubId())
429 assertEquals(200, response.getStatus());
430 assertTrue(response.hasEntity());
431 assertEquals(sub, response.readEntity(DR_Sub.class));