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 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;
43 public class DR_SubResourceTest {
45 private static final DmaapObjectFactory DMAAP_OBJECT_FACTORY = new DmaapObjectFactory();
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;
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());
62 testContainer = new FastJerseyTest(new ResourceConfig()
63 .register(DR_SubResource.class)
64 .register(FeedResource.class)
65 .register(DmaapResource.class));
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
74 DatabaseClass.clearDatabase();
75 DatabaseClass.getDmaap().remove();*/
79 public void cleanupDatabase() throws Exception {
80 DatabaseClass.clearDatabase();
83 //TODO: figure out generic entity list unmarshall to check the entity list
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());
89 assertTrue(resp.getStatus() == 200);
90 assertTrue(resp.hasEntity());
94 public void addDr_Sub_shallReturnError_whenNoFeedIdAndFeedNameInSubProvided() {
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);
100 Response resp = testContainer.target("dr_subs")
102 .post(requestedEntity, Response.class);
105 assertEquals(400, resp.getStatus());
106 ApiError responseError = resp.readEntity(ApiError.class);
107 assertNotNull(responseError);
108 assertEquals("feedName", responseError.getFields());
112 public void addDr_Sub_shallReturnError_whenFeedNameProvided_butFeedNotExist() {
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);
120 Response resp = testContainer.target("dr_subs")
122 .post(requestedEntity, Response.class);
125 assertEquals(404, resp.getStatus());
126 ApiError responseError = resp.readEntity(ApiError.class);
127 assertNotNull(responseError);
128 assertEquals("feedName", responseError.getFields());
132 public void addDr_Sub_shallReturnError_whenFeedNameProvided_andManyFeedsWithTheSameNameInDB() {
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);
144 Response resp = testContainer.target("dr_subs")
146 .post(requestedEntity, Response.class);
149 assertEquals(409, resp.getStatus());
150 ApiError responseError = resp.readEntity(ApiError.class);
151 assertNotNull(responseError);
152 assertEquals("feedName", responseError.getFields());
156 public void addDr_Sub_shallReturnError_whenFeedIdProvided_butFeedNotExist() {
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);
162 Response resp = testContainer.target("dr_subs")
164 .post(requestedEntity, Response.class);
167 assertEquals(404, resp.getStatus());
168 ApiError responseError = resp.readEntity(ApiError.class);
169 assertNotNull(responseError);
170 assertTrue(responseError.getFields().contains("feedId"));
174 public void addDr_Sub_shallExecuteSuccessfully_whenValidFeedIdProvided() {
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);
181 Response resp = testContainer.target("dr_subs")
183 .post(requestedEntity, Response.class);
186 assertEquals(201, resp.getStatus());
187 assertTrue(resp.hasEntity());
188 DR_Sub created = resp.readEntity(DR_Sub.class);
189 assertSubscriptionExistInDB(created);
193 public void addDr_Sub_shallExecuteSuccessfully_whenValidFeedNameProvided() {
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);
202 Response resp = testContainer.target("dr_subs")
204 .post(requestedEntity, Response.class);
207 assertEquals(201, resp.getStatus());
208 assertTrue(resp.hasEntity());
209 DR_Sub created = resp.readEntity(DR_Sub.class);
210 assertSubscriptionExistInDB(created);
215 public void updateDr_Sub_shallReturnError_whenNoFeedIdInSubProvided() {
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);
222 Response resp = testContainer.target("dr_subs")
225 .put(requestedEntity, Response.class);
228 assertEquals(400, resp.getStatus());
229 ApiError responseError = resp.readEntity(ApiError.class);
230 assertNotNull(responseError);
231 assertEquals("feedId", responseError.getFields());
235 public void updateDr_Sub_shallReturnError_whenNoDCAELocationInSubProvided() {
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);
242 Response resp = testContainer.target("dr_subs")
245 .put(requestedEntity, Response.class);
248 assertEquals(400, resp.getStatus());
249 ApiError responseError = resp.readEntity(ApiError.class);
250 assertNotNull(responseError);
251 assertEquals("dcaeLocationName", responseError.getFields());
255 public void updateDr_Sub_shallReturnError_whenFeedWithGivenIdInSubNotExists() {
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);
262 Response resp = testContainer.target("dr_subs")
265 .put(requestedEntity, Response.class);
268 assertEquals(404, resp.getStatus());
269 assertNotNull(resp.readEntity(ApiError.class));
273 public void updateDr_Sub_shallReturnSuccess_whenAddingNewSubscription() {
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);
281 Response resp = testContainer.target("dr_subs")
284 .put(requestedEntity, Response.class);
287 assertEquals(200, resp.getStatus());
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());
298 assertSubscriptionExistInDB(createdDrSub);
302 public void updateDr_Sub_shallReturnSuccess_whenUpdatingExistingSubscription() {
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);
310 Response resp = testContainer.target("dr_subs")
311 .path(existingDrSub.getSubId())
313 .put(requestedEntity, Response.class);
316 assertEquals(200, resp.getStatus());
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());
327 assertSubscriptionExistInDB(updatedDrSub);
331 public void deleteDr_Sub_shouldDeleteSubscriptionWithSuccess() {
333 String feedId = assureFeedIsInDB();
334 DR_Sub dr_sub = addSub(DCAE_LOCATION_NAME, USERNAME, USRPWD, feedId);
337 Response resp = testContainer.target("dr_subs")
338 .path(dr_sub.getSubId())
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());
349 public void deleteDr_Sub_shouldReturnErrorResponse_whenGivenSubIdNotFound() {
351 String notExistingSubId = "6789";
354 Response resp = testContainer.target("dr_subs")
355 .path(notExistingSubId)
360 assertEquals("Shall return error, when trying to delete not existing subscription", 404, resp.getStatus());
361 assertNotNull(resp.readEntity(ApiError.class));
365 public void get_shallReturnExistingObject() {
367 String feedId = assureFeedIsInDB();
368 DR_Sub dr_sub = addSub(DCAE_LOCATION_NAME, USERNAME, USRPWD, feedId);
371 Response resp = testContainer.target("dr_subs")
372 .path(dr_sub.getSubId())
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));
382 public void get_shouldReturnError_whenSubWithIdNotFound() {
384 String notExistingSubId = "1234";
387 Response resp = testContainer.target("dr_subs")
388 .path(notExistingSubId)
393 assertEquals("Subscription shall not be found", 404, resp.getStatus());
394 assertNotNull(resp.readEntity(ApiError.class));
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);
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);
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);
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();
428 private void assertSubscriptionNotExistInDB(String subId) {
429 assertEquals(404, testContainer.target("dr_subs")
436 private void assertSubscriptionExistInDB(DR_Sub sub) {
437 Response response = testContainer.target("dr_subs")
438 .path(sub.getSubId())
441 assertEquals(200, response.getStatus());
442 assertTrue(response.hasEntity());
443 assertEquals(sub, response.readEntity(DR_Sub.class));
446 //TODO: move it outside class and use in other Resource integration tests
447 private static class FastJerseyTest extends JerseyTest {
449 FastJerseyTest(Application jaxrsApplication) {
450 super(jaxrsApplication);
453 void init() throws Exception {
457 void destroy() throws Exception {