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.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;
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;
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());
59 testContainer = new FastJerseyTestContainer(new ResourceConfig()
60 .register(DR_SubResource.class)
61 .register(FeedResource.class));
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
70 DatabaseClass.clearDatabase();
71 DatabaseClass.getDmaap().remove();*/
75 public void cleanupDatabase() {
76 DatabaseClass.clearDatabase();
79 //TODO: figure out generic entity list unmarshall to check the entity list
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());
85 assertEquals(200, resp.getStatus());
86 assertTrue(resp.hasEntity());
90 public void addDr_Sub_shallReturnError_whenNoFeedIdAndFeedNameInSubProvided() {
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);
96 Response resp = testContainer.target("dr_subs")
98 .post(requestedEntity, Response.class);
101 assertEquals(400, resp.getStatus());
102 ApiError responseError = resp.readEntity(ApiError.class);
103 assertNotNull(responseError);
104 assertEquals("feedName", responseError.getFields());
108 public void addDr_Sub_shallReturnError_whenFeedNameProvided_butFeedNotExist() {
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);
116 Response resp = testContainer.target("dr_subs")
118 .post(requestedEntity, Response.class);
121 assertEquals(404, resp.getStatus());
122 ApiError responseError = resp.readEntity(ApiError.class);
123 assertNotNull(responseError);
124 assertEquals("feedName", responseError.getFields());
128 public void addDr_Sub_shallReturnError_whenFeedNameProvided_andManyFeedsWithTheSameNameInDB() {
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);
140 Response resp = testContainer.target("dr_subs")
142 .post(requestedEntity, Response.class);
145 assertEquals(409, resp.getStatus());
146 ApiError responseError = resp.readEntity(ApiError.class);
147 assertNotNull(responseError);
148 assertEquals("feedName", responseError.getFields());
152 public void addDr_Sub_shallReturnError_whenFeedIdProvided_butFeedNotExist() {
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);
158 Response resp = testContainer.target("dr_subs")
160 .post(requestedEntity, Response.class);
163 assertEquals(404, resp.getStatus());
164 ApiError responseError = resp.readEntity(ApiError.class);
165 assertNotNull(responseError);
166 assertTrue(responseError.getFields().contains("feedId"));
170 public void addDr_Sub_shallExecuteSuccessfully_whenValidFeedIdProvided() {
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);
177 Response resp = testContainer.target("dr_subs")
179 .post(requestedEntity, Response.class);
182 assertEquals(201, resp.getStatus());
183 assertTrue(resp.hasEntity());
184 DR_Sub created = resp.readEntity(DR_Sub.class);
185 assertSubscriptionExistInDB(created);
189 public void addDr_Sub_shallExecuteSuccessfully_whenValidFeedNameProvided() {
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);
198 Response resp = testContainer.target("dr_subs")
200 .post(requestedEntity, Response.class);
203 assertEquals(201, resp.getStatus());
204 assertTrue(resp.hasEntity());
205 DR_Sub created = resp.readEntity(DR_Sub.class);
206 assertSubscriptionExistInDB(created);
211 public void updateDr_Sub_shallReturnError_whenNoFeedIdInSubProvided() {
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);
218 Response resp = testContainer.target("dr_subs")
221 .put(requestedEntity, Response.class);
224 assertEquals(400, resp.getStatus());
225 ApiError responseError = resp.readEntity(ApiError.class);
226 assertNotNull(responseError);
227 assertEquals("feedId", responseError.getFields());
231 public void updateDr_Sub_shallReturnError_whenNoDCAELocationInSubProvided() {
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);
238 Response resp = testContainer.target("dr_subs")
241 .put(requestedEntity, Response.class);
244 assertEquals(400, resp.getStatus());
245 ApiError responseError = resp.readEntity(ApiError.class);
246 assertNotNull(responseError);
247 assertEquals("dcaeLocationName", responseError.getFields());
251 public void updateDr_Sub_shallReturnError_whenFeedWithGivenIdInSubNotExists() {
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);
258 Response resp = testContainer.target("dr_subs")
261 .put(requestedEntity, Response.class);
264 assertEquals(404, resp.getStatus());
265 assertNotNull(resp.readEntity(ApiError.class));
269 public void updateDr_Sub_shallReturnSuccess_whenAddingNewSubscription() {
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);
277 Response resp = testContainer.target("dr_subs")
280 .put(requestedEntity, Response.class);
283 assertEquals(200, resp.getStatus());
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());
294 assertSubscriptionExistInDB(createdDrSub);
298 public void updateDr_Sub_shallReturnSuccess_whenUpdatingExistingSubscription() {
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);
306 Response resp = testContainer.target("dr_subs")
307 .path(existingDrSub.getSubId())
309 .put(requestedEntity, Response.class);
312 assertEquals(200, resp.getStatus());
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());
323 assertSubscriptionExistInDB(updatedDrSub);
327 public void deleteDr_Sub_shouldDeleteSubscriptionWithSuccess() {
329 String feedId = assureFeedIsInDB();
330 DR_Sub dr_sub = addSub(DCAE_LOCATION_NAME, USERNAME, USRPWD, feedId);
333 Response resp = testContainer.target("dr_subs")
334 .path(dr_sub.getSubId())
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());
345 public void deleteDr_Sub_shouldReturnErrorResponse_whenGivenSubIdNotFound() {
347 String notExistingSubId = "6789";
350 Response resp = testContainer.target("dr_subs")
351 .path(notExistingSubId)
356 assertEquals("Shall return error, when trying to delete not existing subscription", 404, resp.getStatus());
357 assertNotNull(resp.readEntity(ApiError.class));
361 public void get_shallReturnExistingObject() {
363 String feedId = assureFeedIsInDB();
364 DR_Sub dr_sub = addSub(DCAE_LOCATION_NAME, USERNAME, USRPWD, feedId);
367 Response resp = testContainer.target("dr_subs")
368 .path(dr_sub.getSubId())
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));
378 public void get_shouldReturnError_whenSubWithIdNotFound() {
380 String notExistingSubId = "1234";
383 Response resp = testContainer.target("dr_subs")
384 .path(notExistingSubId)
389 assertEquals("Subscription shall not be found", 404, resp.getStatus());
390 assertNotNull(resp.readEntity(ApiError.class));
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);
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);
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);
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();
424 private void assertSubscriptionNotExistInDB(String subId) {
425 assertEquals(404, testContainer.target("dr_subs")
432 private void assertSubscriptionExistInDB(DR_Sub sub) {
433 Response response = testContainer.target("dr_subs")
434 .path(sub.getSubId())
437 assertEquals(200, response.getStatus());
438 assertTrue(response.hasEntity());
439 assertEquals(sub, response.readEntity(DR_Sub.class));