3 * ============LICENSE_START=======================================================
5 * ================================================================================
6 * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package org.onap.dmaap.dbcapi.resources;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
28 import javax.ws.rs.client.Entity;
29 import javax.ws.rs.core.MediaType;
30 import javax.ws.rs.core.Response;
31 import org.glassfish.jersey.server.ResourceConfig;
32 import org.junit.AfterClass;
33 import org.junit.Before;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.onap.dmaap.dbcapi.database.DatabaseClass;
37 import org.onap.dmaap.dbcapi.model.ApiError;
38 import org.onap.dmaap.dbcapi.model.DR_Pub;
39 import org.onap.dmaap.dbcapi.model.Feed;
40 import org.onap.dmaap.dbcapi.testframework.DmaapObjectFactory;
42 public class DR_PubResourceTest {
44 private static final DmaapObjectFactory DMAAP_OBJECT_FACTORY = new DmaapObjectFactory();
46 private static final String DCAE_LOCATION_NAME = "central-onap";
47 private static final String USERNAME = "user1";
48 private static final String USRPWD = "secretW0rd";
49 private static final String FEED_ID = "someFakeFeedId";
50 private static final String PUB_ID = "0";
51 private static FastJerseyTestContainer testContainer;
52 private static TestFeedCreator testFeedCreator;
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_PubResource.class)
61 .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();
82 public void getDr_Pub_test() {
83 Response resp = testContainer.target("dr_pubs").request().get(Response.class);
84 assertTrue(resp.getStatus() == 200);
85 assertTrue(resp.hasEntity());
89 public void addDr_Pub_shallReturnError_whenNoFeedIdAndFeedNameInPubProvided() {
91 DR_Pub drPub = new DR_Pub(DCAE_LOCATION_NAME, USERNAME, USRPWD, null, PUB_ID);
92 Entity<DR_Pub> requestedEntity = Entity.entity(drPub, MediaType.APPLICATION_JSON);
95 Response resp = testContainer.target("dr_pubs")
97 .post(requestedEntity, Response.class);
100 assertEquals(400, resp.getStatus());
101 ApiError responseError = resp.readEntity(ApiError.class);
102 assertNotNull(responseError);
103 assertEquals("feedName", responseError.getFields());
107 public void addDr_Pub_shallReturnError_whenFeedNameProvided_butFeedNotExist() {
109 DR_Pub drPub = new DR_Pub(DCAE_LOCATION_NAME, USERNAME, USRPWD, null, PUB_ID);
110 Entity<DR_Pub> requestedEntity = Entity.entity(drPub, MediaType.APPLICATION_JSON);
111 drPub.setFeedName("feed_name");
115 Response resp = testContainer.target("dr_pubs")
117 .post(requestedEntity, Response.class);
120 assertEquals(404, resp.getStatus());
121 ApiError responseError = resp.readEntity(ApiError.class);
122 assertNotNull(responseError);
123 assertEquals("feedName", responseError.getFields());
128 public void addDr_Pub_shallReturnError_whenFeedIdProvided_butFeedNotExist() {
130 DR_Pub drPub = new DR_Pub(DCAE_LOCATION_NAME, USERNAME, USRPWD, FEED_ID, PUB_ID);
131 Entity<DR_Pub> requestedEntity = Entity.entity(drPub, MediaType.APPLICATION_JSON);
134 Response resp = testContainer.target("dr_pubs")
136 .post(requestedEntity, Response.class);
139 assertEquals(404, resp.getStatus());
140 ApiError responseError = resp.readEntity(ApiError.class);
141 assertNotNull(responseError);
142 assertEquals("feedId=" + FEED_ID, responseError.getFields());
146 public void addDr_Pub_shallExecuteSuccessfully_whenValidFeedIdProvided() {
148 String feedId = assureFeedIsInDB();
149 DR_Pub drPub = new DR_Pub(DCAE_LOCATION_NAME, USERNAME, USRPWD, feedId);
150 Entity<DR_Pub> requestedEntity = Entity.entity(drPub, MediaType.APPLICATION_JSON);
153 Response resp = testContainer.target("dr_pubs")
155 .post(requestedEntity, Response.class);
158 assertEquals(201, resp.getStatus());
162 public void addDr_Pub_shallExecuteSuccessfully_whenValidFeedNameProvided() {
164 String feedName = "testFeed";
165 testFeedCreator.addFeed(feedName, "test feed");
166 DR_Pub drPub = new DR_Pub(DCAE_LOCATION_NAME, USERNAME, USRPWD, null, PUB_ID);
167 drPub.setFeedName(feedName);
168 Entity<DR_Pub> requestedEntity = Entity.entity(drPub, MediaType.APPLICATION_JSON);
171 Response resp = testContainer.target("dr_pubs")
173 .post(requestedEntity, Response.class);
176 assertEquals(201, resp.getStatus());
180 public void updateDr_Pub_shallExecuteSuccessfully_whenAddingNewPublisher() {
183 DR_Pub drPub = new DR_Pub(DCAE_LOCATION_NAME, USERNAME, USRPWD, "feedId", PUB_ID);
184 Entity<DR_Pub> reqEntity2 = Entity.entity(drPub, MediaType.APPLICATION_JSON);
187 Response resp = testContainer.target("dr_pubs")
190 .put(reqEntity2, Response.class);
193 assertEquals(200, resp.getStatus());
198 public void updateDr_Pub_shallReturnError_whenPathIsWrong() {
200 DR_Pub drPub = new DR_Pub(DCAE_LOCATION_NAME, USERNAME, USRPWD, FEED_ID, PUB_ID);
201 Entity<DR_Pub> reqEntity2 = Entity.entity(drPub, MediaType.APPLICATION_JSON);
204 Response resp = testContainer.target("dr_pubs")
207 .put(reqEntity2, Response.class);
210 assertEquals(405, resp.getStatus());
214 public void deleteDr_Pub_shouldDeleteObjectWithSuccess() {
216 String feedId = assureFeedIsInDB();
217 DR_Pub dr_pub = addPub(DCAE_LOCATION_NAME, USERNAME, USRPWD, feedId);
220 Response resp = testContainer.target("dr_pubs")
221 .path(dr_pub.getPubId())
226 assertEquals("Shall delete subscription with success", 204, resp.getStatus());
227 assertFalse("No entity object shall be returned", resp.hasEntity());
231 public void deleteDr_Pub_shouldReturnErrorResponse_whenGivenPubIdNotFound() {
233 String notExistingPubId = "6789";
236 Response resp = testContainer.target("dr_pubs")
237 .path(notExistingPubId)
242 assertEquals("Shall return error, when trying to delete not existing subscription", 404, resp.getStatus());
243 ApiError responseError = resp.readEntity(ApiError.class);
244 assertNotNull(responseError);
245 assertEquals("pubId", responseError.getFields());
249 public void get_shallReturnExistingObject() {
251 String feedId = assureFeedIsInDB();
252 DR_Pub dr_Pub = addPub(DCAE_LOCATION_NAME, USERNAME, USRPWD, feedId);
255 Response resp = testContainer.target("dr_pubs")
256 .path(dr_Pub.getPubId())
261 assertEquals("Subscription shall be found", 200, resp.getStatus());
262 assertEquals("Retrieved object shall be equal to eh one put into DB", dr_Pub, resp.readEntity(DR_Pub.class));
265 private DR_Pub addPub(String d, String un, String up, String feedId) {
266 DR_Pub dr_pub = new DR_Pub(d, un, up, feedId, "");
267 Entity<DR_Pub> reqEntity2 = Entity.entity(dr_pub, MediaType.APPLICATION_JSON);
268 Response resp = testContainer.target("dr_pubs").request().post(reqEntity2, Response.class);
269 System.out.println("POST dr_pubs resp=" + resp.getStatus());
270 assertTrue(resp.getStatus() == 201);
271 dr_pub = resp.readEntity(DR_Pub.class);
275 private String assureFeedIsInDB() {
276 Feed feed = testFeedCreator.addFeed("SubscriberTestFeed", "feed for DR_Sub testing");
277 assertNotNull("Feed shall be added into DB properly", feed);
278 return feed.getFeedId();