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;
53 private static TestFeedCreator testFeedCreator;
56 public static void setUpClass() throws Exception {
57 //TODO: init is still needed here to assure that dmaap is not null
58 DatabaseClass.getDmaap().init(DMAAP_OBJECT_FACTORY.genDmaap());
60 testContainer = new FastJerseyTestContainer(new ResourceConfig()
61 .register(DR_SubResource.class)
62 .register(FeedResource.class));
64 testFeedCreator = new TestFeedCreator(testContainer);
68 public static void tearDownClass() throws Exception {
69 testContainer.destroy();
70 /*TODO: Cannot cleanup yet until still other Resources tests depends on the static DB content
72 DatabaseClass.clearDatabase();
73 DatabaseClass.getDmaap().remove();*/
77 public void cleanupDatabase() {
78 DatabaseClass.clearDatabase();
81 //TODO: figure out generic entity list unmarshall to check the entity list
83 public void getDr_Subs_test() {
84 Response resp = testContainer.target("dr_subs").request().get(Response.class);
85 System.out.println("GET dr_subs resp=" + resp.getStatus());
87 assertEquals(200, resp.getStatus());
88 assertTrue(resp.hasEntity());
92 public void addDr_Sub_shallReturnError_whenNoFeedIdAndFeedNameInSubProvided() {
94 DR_Sub drSub = new DR_Sub(DCAE_LOCATION_NAME, USERNAME, USRPWD, null, DELIVERY_URL, LOG_URL, true);
95 Entity<DR_Sub> requestedEntity = Entity.entity(drSub, MediaType.APPLICATION_JSON);
98 Response resp = testContainer.target("dr_subs")
100 .post(requestedEntity, Response.class);
103 assertEquals(400, resp.getStatus());
104 ApiError responseError = resp.readEntity(ApiError.class);
105 assertNotNull(responseError);
106 assertEquals("feedName", responseError.getFields());
110 public void addDr_Sub_shallReturnError_whenFeedNameProvided_butFeedNotExist() {
112 String notExistingFeedName = "notRealFead";
113 DR_Sub drSub = new DR_Sub(DCAE_LOCATION_NAME, USERNAME, USRPWD, null, DELIVERY_URL, LOG_URL, true);
114 drSub.setFeedName(notExistingFeedName);
115 Entity<DR_Sub> requestedEntity = Entity.entity(drSub, MediaType.APPLICATION_JSON);
118 Response resp = testContainer.target("dr_subs")
120 .post(requestedEntity, Response.class);
123 assertEquals(404, resp.getStatus());
124 ApiError responseError = resp.readEntity(ApiError.class);
125 assertNotNull(responseError);
126 assertEquals("feedName", responseError.getFields());
130 public void addDr_Sub_shallReturnError_whenFeedNameProvided_andManyFeedsWithTheSameNameInDB() {
132 String notDistinctFeedName = "notDistinctFeedName";
133 Feed feed1 = new Feed(notDistinctFeedName, "1.0", "description", "dgl", "unrestricted");
134 Feed feed2 = new Feed(notDistinctFeedName, "2.0", "description", "dgl", "unrestricted");
135 DatabaseClass.getFeeds().put("1", feed1);
136 DatabaseClass.getFeeds().put("2", feed2);
137 DR_Sub drSub = new DR_Sub(DCAE_LOCATION_NAME, USERNAME, USRPWD, null, DELIVERY_URL, LOG_URL, true);
138 drSub.setFeedName(notDistinctFeedName);
139 Entity<DR_Sub> requestedEntity = Entity.entity(drSub, MediaType.APPLICATION_JSON);
142 Response resp = testContainer.target("dr_subs")
144 .post(requestedEntity, Response.class);
147 assertEquals(409, resp.getStatus());
148 ApiError responseError = resp.readEntity(ApiError.class);
149 assertNotNull(responseError);
150 assertEquals("feedName", responseError.getFields());
154 public void addDr_Sub_shallReturnError_whenFeedIdProvided_butFeedNotExist() {
156 DR_Sub drSub = new DR_Sub(DCAE_LOCATION_NAME, USERNAME, USRPWD, "someFakeFeedId", DELIVERY_URL, LOG_URL, true);
157 Entity<DR_Sub> requestedEntity = Entity.entity(drSub, MediaType.APPLICATION_JSON);
160 Response resp = testContainer.target("dr_subs")
162 .post(requestedEntity, Response.class);
165 assertEquals(404, resp.getStatus());
166 ApiError responseError = resp.readEntity(ApiError.class);
167 assertNotNull(responseError);
168 assertTrue(responseError.getFields().contains("feedId"));
172 public void addDr_Sub_shallExecuteSuccessfully_whenValidFeedIdProvided() {
174 String feedId = assureFeedIsInDB();
175 DR_Sub drSub = new DR_Sub(DCAE_LOCATION_NAME, USERNAME, USRPWD, feedId, DELIVERY_URL, LOG_URL, true);
176 Entity<DR_Sub> requestedEntity = Entity.entity(drSub, MediaType.APPLICATION_JSON);
179 Response resp = testContainer.target("dr_subs")
181 .post(requestedEntity, Response.class);
184 assertEquals(201, resp.getStatus());
185 assertTrue(resp.hasEntity());
186 DR_Sub created = resp.readEntity(DR_Sub.class);
187 assertSubscriptionExistInDB(created);
191 public void addDr_Sub_shallExecuteSuccessfully_whenValidFeedNameProvided() {
193 String feedName = "testFeed";
194 testFeedCreator.addFeed(feedName, "test feed");
195 DR_Sub drSub = new DR_Sub(DCAE_LOCATION_NAME, USERNAME, USRPWD, null, DELIVERY_URL, LOG_URL, true);
196 drSub.setFeedName(feedName);
197 Entity<DR_Sub> requestedEntity = Entity.entity(drSub, MediaType.APPLICATION_JSON);
200 Response resp = testContainer.target("dr_subs")
202 .post(requestedEntity, Response.class);
205 assertEquals(201, resp.getStatus());
206 assertTrue(resp.hasEntity());
207 DR_Sub created = resp.readEntity(DR_Sub.class);
208 assertSubscriptionExistInDB(created);
213 public void updateDr_Sub_shallReturnError_whenNoFeedIdInSubProvided() {
215 String subId = "1234";
216 DR_Sub drSub = new DR_Sub(DCAE_LOCATION_NAME, USERNAME, USRPWD, null, DELIVERY_URL, LOG_URL, true);
217 Entity<DR_Sub> requestedEntity = Entity.entity(drSub, MediaType.APPLICATION_JSON);
220 Response resp = testContainer.target("dr_subs")
223 .put(requestedEntity, Response.class);
226 assertEquals(400, resp.getStatus());
227 ApiError responseError = resp.readEntity(ApiError.class);
228 assertNotNull(responseError);
229 assertEquals("feedId", responseError.getFields());
233 public void updateDr_Sub_shallReturnError_whenNoDCAELocationInSubProvided() {
235 String subId = "1234";
236 DR_Sub drSub = new DR_Sub(null, USERNAME, USRPWD, "someFeedId", DELIVERY_URL, LOG_URL, true);
237 Entity<DR_Sub> requestedEntity = Entity.entity(drSub, MediaType.APPLICATION_JSON);
240 Response resp = testContainer.target("dr_subs")
243 .put(requestedEntity, Response.class);
246 assertEquals(400, resp.getStatus());
247 ApiError responseError = resp.readEntity(ApiError.class);
248 assertNotNull(responseError);
249 assertEquals("dcaeLocationName", responseError.getFields());
253 public void updateDr_Sub_shallReturnError_whenFeedWithGivenIdInSubNotExists() {
255 String subId = "1234";
256 DR_Sub drSub = new DR_Sub(DCAE_LOCATION_NAME, USERNAME, USRPWD, "someFeedId", DELIVERY_URL, LOG_URL, true);
257 Entity<DR_Sub> requestedEntity = Entity.entity(drSub, MediaType.APPLICATION_JSON);
260 Response resp = testContainer.target("dr_subs")
263 .put(requestedEntity, Response.class);
266 assertEquals(404, resp.getStatus());
267 assertNotNull(resp.readEntity(ApiError.class));
271 public void updateDr_Sub_shallReturnSuccess_whenAddingNewSubscription() {
274 String feedId = assureFeedIsInDB();
275 DR_Sub drSub = new DR_Sub(DCAE_LOCATION_NAME, USERNAME, USRPWD, feedId, null, null, true);
276 Entity<DR_Sub> requestedEntity = Entity.entity(drSub, MediaType.APPLICATION_JSON);
279 Response resp = testContainer.target("dr_subs")
282 .put(requestedEntity, Response.class);
285 assertEquals(200, resp.getStatus());
287 DR_Sub createdDrSub = resp.readEntity(DR_Sub.class);
288 assertNotNull(createdDrSub.getLastMod());
289 assertEquals(subId, createdDrSub.getSubId());
290 assertEquals(DCAE_LOCATION_NAME, createdDrSub.getDcaeLocationName());
291 assertEquals(USERNAME, createdDrSub.getUsername());
292 assertEquals(USRPWD, createdDrSub.getUserpwd());
293 assertEquals(DELIVERY_URL_TEMPLATE + subId, createdDrSub.getDeliveryURL());
294 assertEquals(LOG_URL_TEMPLATE + subId, createdDrSub.getLogURL());
296 assertSubscriptionExistInDB(createdDrSub);
300 public void updateDr_Sub_shallReturnSuccess_whenUpdatingExistingSubscription() {
302 String feedId = assureFeedIsInDB();
303 DR_Sub existingDrSub = addSub(DCAE_LOCATION_NAME, USERNAME, USRPWD, feedId);
304 DR_Sub drSubUpdate = new DR_Sub("newDcaeLocationName", "newUserName", "newUserPwd", feedId, null, null, false);
305 Entity<DR_Sub> requestedEntity = Entity.entity(drSubUpdate, MediaType.APPLICATION_JSON);
308 Response resp = testContainer.target("dr_subs")
309 .path(existingDrSub.getSubId())
311 .put(requestedEntity, Response.class);
314 assertEquals(200, resp.getStatus());
316 DR_Sub updatedDrSub = resp.readEntity(DR_Sub.class);
317 assertNotNull(updatedDrSub.getLastMod());
318 assertEquals(existingDrSub.getSubId(), updatedDrSub.getSubId());
319 assertEquals(drSubUpdate.getDcaeLocationName(), updatedDrSub.getDcaeLocationName());
320 assertEquals(drSubUpdate.getUsername(), updatedDrSub.getUsername());
321 assertEquals(drSubUpdate.getUserpwd(), updatedDrSub.getUserpwd());
322 assertEquals(DELIVERY_URL_TEMPLATE + existingDrSub.getSubId(), updatedDrSub.getDeliveryURL());
323 assertEquals(LOG_URL_TEMPLATE + existingDrSub.getSubId(), updatedDrSub.getLogURL());
325 assertSubscriptionExistInDB(updatedDrSub);
329 public void deleteDr_Sub_shouldDeleteSubscriptionWithSuccess() {
331 String feedId = assureFeedIsInDB();
332 DR_Sub dr_sub = addSub(DCAE_LOCATION_NAME, USERNAME, USRPWD, feedId);
335 Response resp = testContainer.target("dr_subs")
336 .path(dr_sub.getSubId())
341 assertEquals("Shall delete subscription with success", 204, resp.getStatus());
342 assertFalse("No entity object shall be returned",resp.hasEntity());
343 assertSubscriptionNotExistInDB(dr_sub.getSubId());
347 public void deleteDr_Sub_shouldReturnErrorResponse_whenGivenSubIdNotFound() {
349 String notExistingSubId = "6789";
352 Response resp = testContainer.target("dr_subs")
353 .path(notExistingSubId)
358 assertEquals("Shall return error, when trying to delete not existing subscription", 404, resp.getStatus());
359 assertNotNull(resp.readEntity(ApiError.class));
363 public void get_shallReturnExistingObject() {
365 String feedId = assureFeedIsInDB();
366 DR_Sub dr_sub = addSub(DCAE_LOCATION_NAME, USERNAME, USRPWD, feedId);
369 Response resp = testContainer.target("dr_subs")
370 .path(dr_sub.getSubId())
375 assertEquals("Subscription shall be found",200, resp.getStatus());
376 assertEquals("Retrieved object shall be equal to eh one put into DB", dr_sub, resp.readEntity(DR_Sub.class));
380 public void get_shouldReturnError_whenSubWithIdNotFound() {
382 String notExistingSubId = "1234";
385 Response resp = testContainer.target("dr_subs")
386 .path(notExistingSubId)
391 assertEquals("Subscription shall not be found", 404, resp.getStatus());
392 assertNotNull(resp.readEntity(ApiError.class));
395 private DR_Sub addSub(String d, String un, String up, String feedId) {
396 DR_Sub dr_sub = new DR_Sub(d, un, up, feedId,
397 "https://subscriber.onap.org/foo", "https://dr-prov/sublog", true);
399 Entity<DR_Sub> reqEntity2 = Entity.entity(dr_sub, MediaType.APPLICATION_JSON);
400 Response resp = testContainer.target("dr_subs").request().post(reqEntity2, Response.class);
401 System.out.println("POST dr_subs resp=" + resp.getStatus());
402 assertEquals(201, resp.getStatus());
403 dr_sub = resp.readEntity(DR_Sub.class);
408 private String assureFeedIsInDB() {
409 Feed feed = testFeedCreator.addFeed("SubscriberTestFeed", "feed for DR_Sub testing");
410 assertNotNull("Feed shall be added into DB properly", feed);
411 return feed.getFeedId();
415 private void assertSubscriptionNotExistInDB(String subId) {
416 assertEquals(404, testContainer.target("dr_subs")
423 private void assertSubscriptionExistInDB(DR_Sub sub) {
424 Response response = testContainer.target("dr_subs")
425 .path(sub.getSubId())
428 assertEquals(200, response.getStatus());
429 assertTrue(response.hasEntity());
430 assertEquals(sub, response.readEntity(DR_Sub.class));