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 System.setProperty("ConfigFile", "src/test/resources/dmaapbc.properties");
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(DmaapResource.class)
62 .register(DR_PubResource.class)
63 .register(FeedResource.class));
66 testFeedCreator = new TestFeedCreator(testContainer);
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() {
80 DatabaseClass.clearDatabase();
84 public void getDr_Pub_test() {
85 Response resp = testContainer.target("dr_pubs").request().get(Response.class);
86 assertTrue(resp.getStatus() == 200);
87 assertTrue(resp.hasEntity());
91 public void addDr_Pub_shallReturnError_whenNoFeedIdAndFeedNameInPubProvided() {
93 DR_Pub drPub = new DR_Pub(DCAE_LOCATION_NAME, USERNAME, USRPWD, null, PUB_ID);
94 Entity<DR_Pub> requestedEntity = Entity.entity(drPub, MediaType.APPLICATION_JSON);
97 Response resp = testContainer.target("dr_pubs")
99 .post(requestedEntity, Response.class);
102 assertEquals(400, resp.getStatus());
103 ApiError responseError = resp.readEntity(ApiError.class);
104 assertNotNull(responseError);
105 assertEquals("feedName", responseError.getFields());
109 public void addDr_Pub_shallReturnError_whenFeedNameProvided_butFeedNotExist() {
111 DR_Pub drPub = new DR_Pub(DCAE_LOCATION_NAME, USERNAME, USRPWD, null, PUB_ID);
112 Entity<DR_Pub> requestedEntity = Entity.entity(drPub, MediaType.APPLICATION_JSON);
113 drPub.setFeedName("feed_name");
117 Response resp = testContainer.target("dr_pubs")
119 .post(requestedEntity, Response.class);
122 assertEquals(404, resp.getStatus());
123 ApiError responseError = resp.readEntity(ApiError.class);
124 assertNotNull(responseError);
125 assertEquals("feedName", responseError.getFields());
130 public void addDr_Pub_shallReturnError_whenFeedIdProvided_butFeedNotExist() {
132 DR_Pub drPub = new DR_Pub(DCAE_LOCATION_NAME, USERNAME, USRPWD, FEED_ID, PUB_ID);
133 Entity<DR_Pub> requestedEntity = Entity.entity(drPub, MediaType.APPLICATION_JSON);
136 Response resp = testContainer.target("dr_pubs")
138 .post(requestedEntity, Response.class);
141 assertEquals(404, resp.getStatus());
142 ApiError responseError = resp.readEntity(ApiError.class);
143 assertNotNull(responseError);
144 assertEquals("feedId=" + FEED_ID, responseError.getFields());
148 public void addDr_Pub_shallExecuteSuccessfully_whenValidFeedIdProvided() {
150 String feedId = assureFeedIsInDB();
151 DR_Pub drPub = new DR_Pub(DCAE_LOCATION_NAME, USERNAME, USRPWD, feedId);
152 Entity<DR_Pub> requestedEntity = Entity.entity(drPub, MediaType.APPLICATION_JSON);
155 Response resp = testContainer.target("dr_pubs")
157 .post(requestedEntity, Response.class);
160 assertEquals(201, resp.getStatus());
164 public void addDr_Pub_shallExecuteSuccessfully_whenValidFeedNameProvided() {
166 String feedName = "testFeed";
167 testFeedCreator.addFeed(feedName, "test feed");
168 DR_Pub drPub = new DR_Pub(DCAE_LOCATION_NAME, USERNAME, USRPWD, null, PUB_ID);
169 drPub.setFeedName(feedName);
170 Entity<DR_Pub> requestedEntity = Entity.entity(drPub, MediaType.APPLICATION_JSON);
173 Response resp = testContainer.target("dr_pubs")
175 .post(requestedEntity, Response.class);
178 assertEquals(201, resp.getStatus());
182 public void updateDr_Pub_shallExecuteSuccessfully_whenAddingNewPublisher() {
185 DR_Pub drPub = new DR_Pub(DCAE_LOCATION_NAME, USERNAME, USRPWD, "feedId", PUB_ID);
186 Entity<DR_Pub> reqEntity2 = Entity.entity(drPub, MediaType.APPLICATION_JSON);
189 Response resp = testContainer.target("dr_pubs")
192 .put(reqEntity2, Response.class);
195 assertEquals(200, resp.getStatus());
199 // When this test is included, the following error is generated:
200 Exception in thread "HTTP-Dispatcher" java.lang.AssertionError: State is not RESPONSE (REQUEST)
201 at jdk.httpserver/sun.net.httpserver.ServerImpl.responseCompleted(ServerImpl.java:814)
202 at jdk.httpserver/sun.net.httpserver.ServerImpl$Dispatcher.handleEvent(ServerImpl.java:297)
203 at jdk.httpserver/sun.net.httpserver.ServerImpl$Dispatcher.run(ServerImpl.java:356)
204 at java.base/java.lang.Thread.run(Thread.java:830)
205 // I can't figure it out, so created a Jira for now. DMAAP-1358
207 public void updateDr_Pub_shallReturnError_whenPathIsWrong() {
209 DR_Pub drPub = new DR_Pub(DCAE_LOCATION_NAME, USERNAME, USRPWD, FEED_ID, PUB_ID);
210 Entity<DR_Pub> reqEntity2 = Entity.entity(drPub, MediaType.APPLICATION_JSON);
213 Response resp = testContainer.target("dr_pubs")
216 .put(reqEntity2, Response.class);
219 assertEquals(405, resp.getStatus());
222 public void deleteDr_Pub_shouldDeleteObjectWithSuccess() {
224 String feedId = assureFeedIsInDB();
225 DR_Pub dr_pub = addPub(DCAE_LOCATION_NAME, USERNAME, USRPWD, feedId);
228 Response resp = testContainer.target("dr_pubs")
229 .path(dr_pub.getPubId())
234 assertEquals("Shall delete publisher with success", 204, resp.getStatus());
235 assertFalse("No entity object shall be returned", resp.hasEntity());
239 public void deleteDr_Pub_shouldReturnErrorResponse_whenGivenPubIdNotFound() {
241 String notExistingPubId = "6789";
244 Response resp = testContainer.target("dr_pubs")
245 .path(notExistingPubId)
250 assertEquals("Shall return error, when trying to delete not existing publisher", 404, resp.getStatus());
251 ApiError responseError = resp.readEntity(ApiError.class);
252 assertNotNull(responseError);
253 assertEquals("pubId", responseError.getFields());
257 public void get_shallReturnExistingObject() {
259 String feedId = assureFeedIsInDB();
260 DR_Pub dr_Pub = addPub(DCAE_LOCATION_NAME, USERNAME, USRPWD, feedId);
263 Response resp = testContainer.target("dr_pubs")
264 .path(dr_Pub.getPubId())
269 assertEquals("Publisher shall be found", 200, resp.getStatus());
270 assertEquals("Retrieved object shall be equal to eh one put into DB", dr_Pub, resp.readEntity(DR_Pub.class));
273 private DR_Pub addPub(String d, String un, String up, String feedId) {
274 DR_Pub dr_pub = new DR_Pub(d, un, up, feedId, "");
275 Entity<DR_Pub> reqEntity2 = Entity.entity(dr_pub, MediaType.APPLICATION_JSON);
276 Response resp = testContainer.target("dr_pubs").request().post(reqEntity2, Response.class);
277 System.out.println("POST dr_pubs resp=" + resp.getStatus());
278 assertTrue(resp.getStatus() == 201);
279 dr_pub = resp.readEntity(DR_Pub.class);
283 private String assureFeedIsInDB() {
284 Feed feed = testFeedCreator.addFeed("PublisherTestFeed", "feed for DR_Pub testing");
285 assertNotNull("Feed shall be added into DB properly", feed);
286 return feed.getFeedId();