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(DmaapResource.class)
61 .register(DR_PubResource.class)
62 .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();
83 public void getDr_Pub_test() {
84 Response resp = testContainer.target("dr_pubs").request().get(Response.class);
85 assertTrue(resp.getStatus() == 200);
86 assertTrue(resp.hasEntity());
90 public void addDr_Pub_shallReturnError_whenNoFeedIdAndFeedNameInPubProvided() {
92 DR_Pub drPub = new DR_Pub(DCAE_LOCATION_NAME, USERNAME, USRPWD, null, PUB_ID);
93 Entity<DR_Pub> requestedEntity = Entity.entity(drPub, MediaType.APPLICATION_JSON);
96 Response resp = testContainer.target("dr_pubs")
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_Pub_shallReturnError_whenFeedNameProvided_butFeedNotExist() {
110 DR_Pub drPub = new DR_Pub(DCAE_LOCATION_NAME, USERNAME, USRPWD, null, PUB_ID);
111 Entity<DR_Pub> requestedEntity = Entity.entity(drPub, MediaType.APPLICATION_JSON);
112 drPub.setFeedName("feed_name");
116 Response resp = testContainer.target("dr_pubs")
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());
129 public void addDr_Pub_shallReturnError_whenFeedIdProvided_butFeedNotExist() {
131 DR_Pub drPub = new DR_Pub(DCAE_LOCATION_NAME, USERNAME, USRPWD, FEED_ID, PUB_ID);
132 Entity<DR_Pub> requestedEntity = Entity.entity(drPub, MediaType.APPLICATION_JSON);
135 Response resp = testContainer.target("dr_pubs")
137 .post(requestedEntity, Response.class);
140 assertEquals(404, resp.getStatus());
141 ApiError responseError = resp.readEntity(ApiError.class);
142 assertNotNull(responseError);
143 assertEquals("feedId=" + FEED_ID, responseError.getFields());
147 public void addDr_Pub_shallExecuteSuccessfully_whenValidFeedIdProvided() {
149 String feedId = assureFeedIsInDB();
150 DR_Pub drPub = new DR_Pub(DCAE_LOCATION_NAME, USERNAME, USRPWD, feedId);
151 Entity<DR_Pub> requestedEntity = Entity.entity(drPub, MediaType.APPLICATION_JSON);
154 Response resp = testContainer.target("dr_pubs")
156 .post(requestedEntity, Response.class);
159 assertEquals(201, resp.getStatus());
163 public void addDr_Pub_shallExecuteSuccessfully_whenValidFeedNameProvided() {
165 String feedName = "testFeed";
166 testFeedCreator.addFeed(feedName, "test feed");
167 DR_Pub drPub = new DR_Pub(DCAE_LOCATION_NAME, USERNAME, USRPWD, null, PUB_ID);
168 drPub.setFeedName(feedName);
169 Entity<DR_Pub> requestedEntity = Entity.entity(drPub, MediaType.APPLICATION_JSON);
172 Response resp = testContainer.target("dr_pubs")
174 .post(requestedEntity, Response.class);
177 assertEquals(201, resp.getStatus());
181 public void updateDr_Pub_shallExecuteSuccessfully_whenAddingNewPublisher() {
184 DR_Pub drPub = new DR_Pub(DCAE_LOCATION_NAME, USERNAME, USRPWD, "feedId", PUB_ID);
185 Entity<DR_Pub> reqEntity2 = Entity.entity(drPub, MediaType.APPLICATION_JSON);
188 Response resp = testContainer.target("dr_pubs")
191 .put(reqEntity2, Response.class);
194 assertEquals(200, resp.getStatus());
198 // When this test is included, the following error is generated:
199 Exception in thread "HTTP-Dispatcher" java.lang.AssertionError: State is not RESPONSE (REQUEST)
200 at jdk.httpserver/sun.net.httpserver.ServerImpl.responseCompleted(ServerImpl.java:814)
201 at jdk.httpserver/sun.net.httpserver.ServerImpl$Dispatcher.handleEvent(ServerImpl.java:297)
202 at jdk.httpserver/sun.net.httpserver.ServerImpl$Dispatcher.run(ServerImpl.java:356)
203 at java.base/java.lang.Thread.run(Thread.java:830)
204 // I can't figure it out, so created a Jira for now. DMAAP-1358
206 public void updateDr_Pub_shallReturnError_whenPathIsWrong() {
208 DR_Pub drPub = new DR_Pub(DCAE_LOCATION_NAME, USERNAME, USRPWD, FEED_ID, PUB_ID);
209 Entity<DR_Pub> reqEntity2 = Entity.entity(drPub, MediaType.APPLICATION_JSON);
212 Response resp = testContainer.target("dr_pubs")
215 .put(reqEntity2, Response.class);
218 assertEquals(405, resp.getStatus());
221 public void deleteDr_Pub_shouldDeleteObjectWithSuccess() {
223 String feedId = assureFeedIsInDB();
224 DR_Pub dr_pub = addPub(DCAE_LOCATION_NAME, USERNAME, USRPWD, feedId);
227 Response resp = testContainer.target("dr_pubs")
228 .path(dr_pub.getPubId())
233 assertEquals("Shall delete publisher with success", 204, resp.getStatus());
234 assertFalse("No entity object shall be returned", resp.hasEntity());
238 public void deleteDr_Pub_shouldReturnErrorResponse_whenGivenPubIdNotFound() {
240 String notExistingPubId = "6789";
243 Response resp = testContainer.target("dr_pubs")
244 .path(notExistingPubId)
249 assertEquals("Shall return error, when trying to delete not existing publisher", 404, resp.getStatus());
250 ApiError responseError = resp.readEntity(ApiError.class);
251 assertNotNull(responseError);
252 assertEquals("pubId", responseError.getFields());
256 public void get_shallReturnExistingObject() {
258 String feedId = assureFeedIsInDB();
259 DR_Pub dr_Pub = addPub(DCAE_LOCATION_NAME, USERNAME, USRPWD, feedId);
262 Response resp = testContainer.target("dr_pubs")
263 .path(dr_Pub.getPubId())
268 assertEquals("Publisher shall be found", 200, resp.getStatus());
269 assertEquals("Retrieved object shall be equal to eh one put into DB", dr_Pub, resp.readEntity(DR_Pub.class));
272 private DR_Pub addPub(String d, String un, String up, String feedId) {
273 DR_Pub dr_pub = new DR_Pub(d, un, up, feedId, "");
274 Entity<DR_Pub> reqEntity2 = Entity.entity(dr_pub, MediaType.APPLICATION_JSON);
275 Response resp = testContainer.target("dr_pubs").request().post(reqEntity2, Response.class);
276 System.out.println("POST dr_pubs resp=" + resp.getStatus());
277 assertTrue(resp.getStatus() == 201);
278 dr_pub = resp.readEntity(DR_Pub.class);
282 private String assureFeedIsInDB() {
283 Feed feed = testFeedCreator.addFeed("PublisherTestFeed", "feed for DR_Pub testing");
284 assertNotNull("Feed shall be added into DB properly", feed);
285 return feed.getFeedId();