[DMAAP-BC] Consolidate bus controller repos
[dmaap/buscontroller.git] / dmaap-bc / src / test / java / org / onap / dmaap / dbcapi / resources / DR_PubResourceTest.java
1
2 /*-
3  * ============LICENSE_START=======================================================
4  * org.onap.dmaap
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21 package org.onap.dmaap.dbcapi.resources;
22
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;
27
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;
41
42 public class DR_PubResourceTest {
43
44     private static final DmaapObjectFactory DMAAP_OBJECT_FACTORY = new DmaapObjectFactory();
45
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;
53
54     @BeforeClass
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());
58
59         testContainer = new FastJerseyTestContainer(new ResourceConfig()
60             .register(DmaapResource.class)
61             .register(DR_PubResource.class)
62             .register(FeedResource.class));
63
64         testContainer.init();
65         testFeedCreator = new TestFeedCreator(testContainer);
66     }
67
68     @AfterClass
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
72
73         DatabaseClass.clearDatabase();
74         DatabaseClass.getDmaap().remove();*/
75     }
76
77     @Before
78     public void cleanupDatabase() {
79         DatabaseClass.clearDatabase();
80     }
81
82     @Test
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());
87     }
88
89     @Test
90     public void addDr_Pub_shallReturnError_whenNoFeedIdAndFeedNameInPubProvided() {
91         //given
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);
94
95         //when
96         Response resp = testContainer.target("dr_pubs")
97             .request()
98             .post(requestedEntity, Response.class);
99
100         //then
101         assertEquals(400, resp.getStatus());
102         ApiError responseError = resp.readEntity(ApiError.class);
103         assertNotNull(responseError);
104         assertEquals("feedName", responseError.getFields());
105     }
106
107     @Test
108     public void addDr_Pub_shallReturnError_whenFeedNameProvided_butFeedNotExist() {
109         //given
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");
113
114
115         //when
116         Response resp = testContainer.target("dr_pubs")
117             .request()
118             .post(requestedEntity, Response.class);
119
120         //then
121         assertEquals(404, resp.getStatus());
122         ApiError responseError = resp.readEntity(ApiError.class);
123         assertNotNull(responseError);
124         assertEquals("feedName", responseError.getFields());
125
126     }
127
128     @Test
129     public void addDr_Pub_shallReturnError_whenFeedIdProvided_butFeedNotExist() {
130         //given
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);
133
134         //when
135         Response resp = testContainer.target("dr_pubs")
136             .request()
137             .post(requestedEntity, Response.class);
138
139         //then
140         assertEquals(404, resp.getStatus());
141         ApiError responseError = resp.readEntity(ApiError.class);
142         assertNotNull(responseError);
143         assertEquals("feedId=" + FEED_ID, responseError.getFields());
144     }
145
146     @Test
147     public void addDr_Pub_shallExecuteSuccessfully_whenValidFeedIdProvided() {
148         //given
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);
152
153         //when
154         Response resp = testContainer.target("dr_pubs")
155             .request()
156             .post(requestedEntity, Response.class);
157
158         //then
159         assertEquals(201, resp.getStatus());
160     }
161
162     @Test
163     public void addDr_Pub_shallExecuteSuccessfully_whenValidFeedNameProvided() {
164         //given
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);
170
171         //when
172         Response resp = testContainer.target("dr_pubs")
173             .request()
174             .post(requestedEntity, Response.class);
175
176         //then
177         assertEquals(201, resp.getStatus());
178     }
179
180     @Test
181     public void updateDr_Pub_shallExecuteSuccessfully_whenAddingNewPublisher() {
182         //given
183         String pubId = "5";
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);
186
187         //when
188         Response resp = testContainer.target("dr_pubs")
189             .path(pubId)
190             .request()
191             .put(reqEntity2, Response.class);
192
193         //then
194         assertEquals(200, resp.getStatus());
195
196     }
197  /*//
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
205     @Test
206     public void updateDr_Pub_shallReturnError_whenPathIsWrong() {
207         //given
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);
210
211         //when
212         Response resp = testContainer.target("dr_pubs")
213             .path("")
214             .request()
215             .put(reqEntity2, Response.class);
216
217         //then
218         assertEquals(405, resp.getStatus());
219     }*/
220     @Test
221     public void deleteDr_Pub_shouldDeleteObjectWithSuccess() {
222         //given
223         String feedId = assureFeedIsInDB();
224         DR_Pub dr_pub = addPub(DCAE_LOCATION_NAME, USERNAME, USRPWD, feedId);
225
226         //when
227         Response resp = testContainer.target("dr_pubs")
228             .path(dr_pub.getPubId())
229             .request()
230             .delete();
231
232         //then
233         assertEquals("Shall delete publisher with success", 204, resp.getStatus());
234         assertFalse("No entity object shall be returned", resp.hasEntity());
235     }
236
237     @Test
238     public void deleteDr_Pub_shouldReturnErrorResponse_whenGivenPubIdNotFound() {
239         //given
240         String notExistingPubId = "6789";
241
242         //when
243         Response resp = testContainer.target("dr_pubs")
244             .path(notExistingPubId)
245             .request()
246             .delete();
247
248         //then
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());
253     }
254
255     @Test
256     public void get_shallReturnExistingObject() {
257         //given
258         String feedId = assureFeedIsInDB();
259         DR_Pub dr_Pub = addPub(DCAE_LOCATION_NAME, USERNAME, USRPWD, feedId);
260
261         //when
262         Response resp = testContainer.target("dr_pubs")
263                 .path(dr_Pub.getPubId())
264                 .request()
265                 .get();
266
267         //then
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));
270     }
271
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);
279         return dr_pub;
280     }
281
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();
286     }
287
288
289 }
290
291