bf0308858eca9d7777641d2b8cbf36bd6b37ae3e
[dmaap/dbcapi.git] / 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(DR_PubResource.class)
61             .register(FeedResource.class));
62
63         testContainer.init();
64         testFeedCreator = new TestFeedCreator(testContainer);
65     }
66
67     @AfterClass
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
71
72         DatabaseClass.clearDatabase();
73         DatabaseClass.getDmaap().remove();*/
74     }
75
76     @Before
77     public void cleanupDatabase() {
78         DatabaseClass.clearDatabase();
79     }
80
81     @Test
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());
86     }
87
88     @Test
89     public void addDr_Pub_shallReturnError_whenNoFeedIdAndFeedNameInPubProvided() {
90         //given
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);
93
94         //when
95         Response resp = testContainer.target("dr_pubs")
96             .request()
97             .post(requestedEntity, Response.class);
98
99         //then
100         assertEquals(400, resp.getStatus());
101         ApiError responseError = resp.readEntity(ApiError.class);
102         assertNotNull(responseError);
103         assertEquals("feedName", responseError.getFields());
104     }
105
106     @Test
107     public void addDr_Pub_shallReturnError_whenFeedNameProvided_butFeedNotExist() {
108         //given
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");
112
113
114         //when
115         Response resp = testContainer.target("dr_pubs")
116             .request()
117             .post(requestedEntity, Response.class);
118
119         //then
120         assertEquals(404, resp.getStatus());
121         ApiError responseError = resp.readEntity(ApiError.class);
122         assertNotNull(responseError);
123         assertEquals("feedName", responseError.getFields());
124
125     }
126
127     @Test
128     public void addDr_Pub_shallReturnError_whenFeedIdProvided_butFeedNotExist() {
129         //given
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);
132
133         //when
134         Response resp = testContainer.target("dr_pubs")
135             .request()
136             .post(requestedEntity, Response.class);
137
138         //then
139         assertEquals(404, resp.getStatus());
140         ApiError responseError = resp.readEntity(ApiError.class);
141         assertNotNull(responseError);
142         assertEquals("feedId=" + FEED_ID, responseError.getFields());
143     }
144
145     @Test
146     public void addDr_Pub_shallExecuteSuccessfully_whenValidFeedIdProvided() {
147         //given
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);
151
152         //when
153         Response resp = testContainer.target("dr_pubs")
154             .request()
155             .post(requestedEntity, Response.class);
156
157         //then
158         assertEquals(201, resp.getStatus());
159     }
160
161     @Test
162     public void addDr_Pub_shallExecuteSuccessfully_whenValidFeedNameProvided() {
163         //given
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);
169
170         //when
171         Response resp = testContainer.target("dr_pubs")
172             .request()
173             .post(requestedEntity, Response.class);
174
175         //then
176         assertEquals(201, resp.getStatus());
177     }
178
179     @Test
180     public void updateDr_Pub_shallExecuteSuccessfully_whenAddingNewPublisher() {
181         //given
182         String pubId = "5";
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);
185
186         //when
187         Response resp = testContainer.target("dr_pubs")
188             .path(pubId)
189             .request()
190             .put(reqEntity2, Response.class);
191
192         //then
193         assertEquals(200, resp.getStatus());
194
195     }
196
197     @Test
198     public void updateDr_Pub_shallReturnError_whenPathIsWrong() {
199         //given
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);
202
203         //when
204         Response resp = testContainer.target("dr_pubs")
205             .path("")
206             .request()
207             .put(reqEntity2, Response.class);
208
209         //then
210         assertEquals(405, resp.getStatus());
211     }
212
213     @Test
214     public void deleteDr_Pub_shouldDeleteObjectWithSuccess() {
215         //given
216         String feedId = assureFeedIsInDB();
217         DR_Pub dr_pub = addPub(DCAE_LOCATION_NAME, USERNAME, USRPWD, feedId);
218
219         //when
220         Response resp = testContainer.target("dr_pubs")
221             .path(dr_pub.getPubId())
222             .request()
223             .delete();
224
225         //then
226         assertEquals("Shall delete subscription with success", 204, resp.getStatus());
227         assertFalse("No entity object shall be returned", resp.hasEntity());
228     }
229
230     @Test
231     public void deleteDr_Pub_shouldReturnErrorResponse_whenGivenPubIdNotFound() {
232         //given
233         String notExistingPubId = "6789";
234
235         //when
236         Response resp = testContainer.target("dr_pubs")
237             .path(notExistingPubId)
238             .request()
239             .delete();
240
241         //then
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());
246     }
247
248     @Test
249     public void get_shallReturnExistingObject() {
250         //given
251         String feedId = assureFeedIsInDB();
252         DR_Pub dr_Pub = addPub(DCAE_LOCATION_NAME, USERNAME, USRPWD, feedId);
253
254         //when
255         Response resp = testContainer.target("dr_pubs")
256                 .path(dr_Pub.getPubId())
257                 .request()
258                 .get();
259
260         //then
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));
263     }
264
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);
272         return dr_pub;
273     }
274
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();
279     }
280 }
281
282