aboutsummaryrefslogtreecommitdiffstats
path: root/dmaap-bc/src/test/java/org/onap/dmaap/dbcapi/resources/DR_PubResourceTest.java
blob: 84a4a6d4814ea86456ef8626a1e195dda7acb1c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*-
 * ============LICENSE_START=======================================================
 * org.onap.dmaap
 * ================================================================================
 * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.dmaap.dbcapi.resources;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import org.glassfish.jersey.server.ResourceConfig;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.onap.dmaap.dbcapi.database.DatabaseClass;
import org.onap.dmaap.dbcapi.model.ApiError;
import org.onap.dmaap.dbcapi.model.DR_Pub;
import org.onap.dmaap.dbcapi.model.Feed;
import org.onap.dmaap.dbcapi.testframework.DmaapObjectFactory;

public class DR_PubResourceTest {

    private static final DmaapObjectFactory DMAAP_OBJECT_FACTORY = new DmaapObjectFactory();

    private static final String DCAE_LOCATION_NAME = "central-onap";
    private static final String USERNAME = "user1";
    private static final String USRPWD = "secretW0rd";
    private static final String FEED_ID = "someFakeFeedId";
    private static final String PUB_ID = "0";
    private static FastJerseyTestContainer testContainer;
    private static TestFeedCreator testFeedCreator;

    @BeforeClass
    public static void setUpClass() throws Exception {
        System.setProperty("ConfigFile", "src/test/resources/dmaapbc.properties");
        //TODO: init is still needed here to assure that dmaap is not null
        DatabaseClass.getDmaap().init(DMAAP_OBJECT_FACTORY.genDmaap());

        testContainer = new FastJerseyTestContainer(new ResourceConfig()
            .register(DmaapResource.class)
            .register(DR_PubResource.class)
            .register(FeedResource.class));

        testContainer.init();
        testFeedCreator = new TestFeedCreator(testContainer);
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
        testContainer.destroy();
        /*TODO: Cannot cleanup yet until still other Resources tests depends on the static DB content

        DatabaseClass.clearDatabase();
        DatabaseClass.getDmaap().remove();*/
    }

    @Before
    public void cleanupDatabase() {
        DatabaseClass.clearDatabase();
    }

    @Test
    public void getDr_Pub_test() {
        Response resp = testContainer.target("dr_pubs").request().get(Response.class);
        assertTrue(resp.getStatus() == 200);
        assertTrue(resp.hasEntity());
    }

    @Test
    public void addDr_Pub_shallReturnError_whenNoFeedIdAndFeedNameInPubProvided() {
        //given
        DR_Pub drPub = new DR_Pub(DCAE_LOCATION_NAME, USERNAME, USRPWD, null, PUB_ID);
        Entity<DR_Pub> requestedEntity = Entity.entity(drPub, MediaType.APPLICATION_JSON);

        //when
        Response resp = testContainer.target("dr_pubs")
            .request()
            .post(requestedEntity, Response.class);

        //then
        assertEquals(400, resp.getStatus());
        ApiError responseError = resp.readEntity(ApiError.class);
        assertNotNull(responseError);
        assertEquals("feedName", responseError.getFields());
    }

    @Test
    public void addDr_Pub_shallReturnError_whenFeedNameProvided_butFeedNotExist() {
        //given
        DR_Pub drPub = new DR_Pub(DCAE_LOCATION_NAME, USERNAME, USRPWD, null, PUB_ID);
        Entity<DR_Pub> requestedEntity = Entity.entity(drPub, MediaType.APPLICATION_JSON);
        drPub.setFeedName("feed_name");


        //when
        Response resp = testContainer.target("dr_pubs")
            .request()
            .post(requestedEntity, Response.class);

        //then
        assertEquals(404, resp.getStatus());
        ApiError responseError = resp.readEntity(ApiError.class);
        assertNotNull(responseError);
        assertEquals("feedName", responseError.getFields());

    }

    @Test
    public void addDr_Pub_shallReturnError_whenFeedIdProvided_butFeedNotExist() {
        //given
        DR_Pub drPub = new DR_Pub(DCAE_LOCATION_NAME, USERNAME, USRPWD, FEED_ID, PUB_ID);
        Entity<DR_Pub> requestedEntity = Entity.entity(drPub, MediaType.APPLICATION_JSON);

        //when
        Response resp = testContainer.target("dr_pubs")
            .request()
            .post(requestedEntity, Response.class);

        //then
        assertEquals(404, resp.getStatus());
        ApiError responseError = resp.readEntity(ApiError.class);
        assertNotNull(responseError);
        assertEquals("feedId=" + FEED_ID, responseError.getFields());
    }

    @Test
    public void addDr_Pub_shallExecuteSuccessfully_whenValidFeedIdProvided() {
        //given
        String feedId = assureFeedIsInDB();
        DR_Pub drPub = new DR_Pub(DCAE_LOCATION_NAME, USERNAME, USRPWD, feedId);
        Entity<DR_Pub> requestedEntity = Entity.entity(drPub, MediaType.APPLICATION_JSON);

        //when
        Response resp = testContainer.target("dr_pubs")
            .request()
            .post(requestedEntity, Response.class);

        //then
        assertEquals(201, resp.getStatus());
    }

    @Test
    public void addDr_Pub_shallExecuteSuccessfully_whenValidFeedNameProvided() {
        //given
        String feedName = "testFeed";
        testFeedCreator.addFeed(feedName, "test feed");
        DR_Pub drPub = new DR_Pub(DCAE_LOCATION_NAME, USERNAME, USRPWD, null, PUB_ID);
        drPub.setFeedName(feedName);
        Entity<DR_Pub> requestedEntity = Entity.entity(drPub, MediaType.APPLICATION_JSON);

        //when
        Response resp = testContainer.target("dr_pubs")
            .request()
            .post(requestedEntity, Response.class);

        //then
        assertEquals(201, resp.getStatus());
    }

    @Test
    public void updateDr_Pub_shallExecuteSuccessfully_whenAddingNewPublisher() {
        //given
        String pubId = "5";
        DR_Pub drPub = new DR_Pub(DCAE_LOCATION_NAME, USERNAME, USRPWD, "feedId", PUB_ID);
        Entity<DR_Pub> reqEntity2 = Entity.entity(drPub, MediaType.APPLICATION_JSON);

        //when
        Response resp = testContainer.target("dr_pubs")
            .path(pubId)
            .request()
            .put(reqEntity2, Response.class);

        //then
        assertEquals(200, resp.getStatus());

    }
 /*//
 //   When this test is included, the following error is generated:
 Exception in thread "HTTP-Dispatcher" java.lang.AssertionError: State is not RESPONSE (REQUEST)
    at jdk.httpserver/sun.net.httpserver.ServerImpl.responseCompleted(ServerImpl.java:814)
    at jdk.httpserver/sun.net.httpserver.ServerImpl$Dispatcher.handleEvent(ServerImpl.java:297)
    at jdk.httpserver/sun.net.httpserver.ServerImpl$Dispatcher.run(ServerImpl.java:356)
    at java.base/java.lang.Thread.run(Thread.java:830)
//  I can't figure it out, so created a Jira for now.  DMAAP-1358
    @Test
    public void updateDr_Pub_shallReturnError_whenPathIsWrong() {
        //given
        DR_Pub drPub = new DR_Pub(DCAE_LOCATION_NAME, USERNAME, USRPWD, FEED_ID, PUB_ID);
        Entity<DR_Pub> reqEntity2 = Entity.entity(drPub, MediaType.APPLICATION_JSON);

        //when
        Response resp = testContainer.target("dr_pubs")
            .path("")
            .request()
            .put(reqEntity2, Response.class);

        //then
        assertEquals(405, resp.getStatus());
    }*/
    @Test
    public void deleteDr_Pub_shouldDeleteObjectWithSuccess() {
        //given
        String feedId = assureFeedIsInDB();
        DR_Pub dr_pub = addPub(DCAE_LOCATION_NAME, USERNAME, USRPWD, feedId);

        //when
        Response resp = testContainer.target("dr_pubs")
            .path(dr_pub.getPubId())
            .request()
            .delete();

        //then
        assertEquals("Shall delete publisher with success", 204, resp.getStatus());
        assertFalse("No entity object shall be returned", resp.hasEntity());
    }

    @Test
    public void deleteDr_Pub_shouldReturnErrorResponse_whenGivenPubIdNotFound() {
        //given
        String notExistingPubId = "6789";

        //when
        Response resp = testContainer.target("dr_pubs")
            .path(notExistingPubId)
            .request()
            .delete();

        //then
        assertEquals("Shall return error, when trying to delete not existing publisher", 404, resp.getStatus());
        ApiError responseError = resp.readEntity(ApiError.class);
        assertNotNull(responseError);
        assertEquals("pubId", responseError.getFields());
    }

    @Test
    public void get_shallReturnExistingObject() {
        //given
        String feedId = assureFeedIsInDB();
        DR_Pub dr_Pub = addPub(DCAE_LOCATION_NAME, USERNAME, USRPWD, feedId);

        //when
        Response resp = testContainer.target("dr_pubs")
                .path(dr_Pub.getPubId())
                .request()
                .get();

        //then
        assertEquals("Publisher shall be found", 200, resp.getStatus());
        assertEquals("Retrieved object shall be equal to eh one put into DB", dr_Pub, resp.readEntity(DR_Pub.class));
    }

    private DR_Pub addPub(String d, String un, String up, String feedId) {
        DR_Pub dr_pub = new DR_Pub(d, un, up, feedId, "");
        Entity<DR_Pub> reqEntity2 = Entity.entity(dr_pub, MediaType.APPLICATION_JSON);
        Response resp = testContainer.target("dr_pubs").request().post(reqEntity2, Response.class);
        System.out.println("POST dr_pubs resp=" + resp.getStatus());
        assertTrue(resp.getStatus() == 201);
        dr_pub = resp.readEntity(DR_Pub.class);
        return dr_pub;
    }

    private String assureFeedIsInDB() {
        Feed feed = testFeedCreator.addFeed("PublisherTestFeed", "feed for DR_Pub testing");
        assertNotNull("Feed shall be added into DB properly", feed);
        return feed.getFeedId();
    }


}