[DMAAP-BC] Fix failing jenkins
[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         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());
59
60         testContainer = new FastJerseyTestContainer(new ResourceConfig()
61             .register(DmaapResource.class)
62             .register(DR_PubResource.class)
63             .register(FeedResource.class));
64
65         testContainer.init();
66         testFeedCreator = new TestFeedCreator(testContainer);
67     }
68
69     @AfterClass
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
73
74         DatabaseClass.clearDatabase();
75         DatabaseClass.getDmaap().remove();*/
76     }
77
78     @Before
79     public void cleanupDatabase() {
80         DatabaseClass.clearDatabase();
81     }
82
83     @Test
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());
88     }
89
90     @Test
91     public void addDr_Pub_shallReturnError_whenNoFeedIdAndFeedNameInPubProvided() {
92         //given
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);
95
96         //when
97         Response resp = testContainer.target("dr_pubs")
98             .request()
99             .post(requestedEntity, Response.class);
100
101         //then
102         assertEquals(400, resp.getStatus());
103         ApiError responseError = resp.readEntity(ApiError.class);
104         assertNotNull(responseError);
105         assertEquals("feedName", responseError.getFields());
106     }
107
108     @Test
109     public void addDr_Pub_shallReturnError_whenFeedNameProvided_butFeedNotExist() {
110         //given
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");
114
115
116         //when
117         Response resp = testContainer.target("dr_pubs")
118             .request()
119             .post(requestedEntity, Response.class);
120
121         //then
122         assertEquals(404, resp.getStatus());
123         ApiError responseError = resp.readEntity(ApiError.class);
124         assertNotNull(responseError);
125         assertEquals("feedName", responseError.getFields());
126
127     }
128
129     @Test
130     public void addDr_Pub_shallReturnError_whenFeedIdProvided_butFeedNotExist() {
131         //given
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);
134
135         //when
136         Response resp = testContainer.target("dr_pubs")
137             .request()
138             .post(requestedEntity, Response.class);
139
140         //then
141         assertEquals(404, resp.getStatus());
142         ApiError responseError = resp.readEntity(ApiError.class);
143         assertNotNull(responseError);
144         assertEquals("feedId=" + FEED_ID, responseError.getFields());
145     }
146
147     @Test
148     public void addDr_Pub_shallExecuteSuccessfully_whenValidFeedIdProvided() {
149         //given
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);
153
154         //when
155         Response resp = testContainer.target("dr_pubs")
156             .request()
157             .post(requestedEntity, Response.class);
158
159         //then
160         assertEquals(201, resp.getStatus());
161     }
162
163     @Test
164     public void addDr_Pub_shallExecuteSuccessfully_whenValidFeedNameProvided() {
165         //given
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);
171
172         //when
173         Response resp = testContainer.target("dr_pubs")
174             .request()
175             .post(requestedEntity, Response.class);
176
177         //then
178         assertEquals(201, resp.getStatus());
179     }
180
181     @Test
182     public void updateDr_Pub_shallExecuteSuccessfully_whenAddingNewPublisher() {
183         //given
184         String pubId = "5";
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);
187
188         //when
189         Response resp = testContainer.target("dr_pubs")
190             .path(pubId)
191             .request()
192             .put(reqEntity2, Response.class);
193
194         //then
195         assertEquals(200, resp.getStatus());
196
197     }
198  /*//
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
206     @Test
207     public void updateDr_Pub_shallReturnError_whenPathIsWrong() {
208         //given
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);
211
212         //when
213         Response resp = testContainer.target("dr_pubs")
214             .path("")
215             .request()
216             .put(reqEntity2, Response.class);
217
218         //then
219         assertEquals(405, resp.getStatus());
220     }*/
221     @Test
222     public void deleteDr_Pub_shouldDeleteObjectWithSuccess() {
223         //given
224         String feedId = assureFeedIsInDB();
225         DR_Pub dr_pub = addPub(DCAE_LOCATION_NAME, USERNAME, USRPWD, feedId);
226
227         //when
228         Response resp = testContainer.target("dr_pubs")
229             .path(dr_pub.getPubId())
230             .request()
231             .delete();
232
233         //then
234         assertEquals("Shall delete publisher with success", 204, resp.getStatus());
235         assertFalse("No entity object shall be returned", resp.hasEntity());
236     }
237
238     @Test
239     public void deleteDr_Pub_shouldReturnErrorResponse_whenGivenPubIdNotFound() {
240         //given
241         String notExistingPubId = "6789";
242
243         //when
244         Response resp = testContainer.target("dr_pubs")
245             .path(notExistingPubId)
246             .request()
247             .delete();
248
249         //then
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());
254     }
255
256     @Test
257     public void get_shallReturnExistingObject() {
258         //given
259         String feedId = assureFeedIsInDB();
260         DR_Pub dr_Pub = addPub(DCAE_LOCATION_NAME, USERNAME, USRPWD, feedId);
261
262         //when
263         Response resp = testContainer.target("dr_pubs")
264                 .path(dr_Pub.getPubId())
265                 .request()
266                 .get();
267
268         //then
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));
271     }
272
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);
280         return dr_pub;
281     }
282
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();
287     }
288
289
290 }
291
292