2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.dmaap.dbcapi.resources;
23 import org.glassfish.jersey.server.ResourceConfig;
24 import org.junit.AfterClass;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27 import org.onap.dmaap.dbcapi.database.DatabaseClass;
28 import org.onap.dmaap.dbcapi.model.ApiError;
29 import org.onap.dmaap.dbcapi.model.MR_Client;
30 import org.onap.dmaap.dbcapi.model.MR_Cluster;
31 import org.onap.dmaap.dbcapi.model.Topic;
32 import org.onap.dmaap.dbcapi.testframework.DmaapObjectFactory;
34 import javax.ws.rs.client.Entity;
35 import javax.ws.rs.core.Response;
37 import static javax.ws.rs.client.Entity.entity;
38 import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
39 import static org.junit.Assert.assertEquals;
40 import static org.junit.Assert.assertNotNull;
41 import static org.junit.Assert.assertTrue;
43 public class MR_ClientResourceTest {
45 private static final DmaapObjectFactory DMAAP_OBJECT_FACTORY = new DmaapObjectFactory();
46 private static FastJerseyTestContainer testContainer;
49 public static void setUpClass() throws Exception {
50 DatabaseClass.getDmaap().init(DMAAP_OBJECT_FACTORY.genDmaap());
52 testContainer = new FastJerseyTestContainer(new ResourceConfig()
53 .register(MR_ClientResource.class).register(MR_ClusterResource.class).register(TopicResource.class));
58 public static void tearDownClass() throws Exception {
59 testContainer.destroy();
60 /*TODO: Cannot cleanup yet until still other Resources tests depends on the static DB content
62 DatabaseClass.getDmaap().remove();
63 DatabaseClass.clearDatabase();*/
67 public void addMr_Client_shouldReturnErrorWhenNoFqtnProvided() {
68 Entity<MR_Client> requestedEntity = entity(
69 new MR_Client("dcaeLocation", null, "clientRole", new String[]{"GET"}), APPLICATION_JSON);
71 Response response = testContainer.target("mr_clients")
73 .post(requestedEntity, Response.class);
75 assertEquals(400, response.getStatus());
76 ApiError responseError = response.readEntity(ApiError.class);
77 assertNotNull(responseError);
78 assertEquals("fqtn", responseError.getFields());
82 public void addMr_Client_shouldReturnErrorWhenNoLocationProvided() {
83 Entity<MR_Client> requestedEntity = entity(
84 new MR_Client(null, "fqtn", "clientRole", new String[]{"GET"}), APPLICATION_JSON);
86 Response response = testContainer.target("mr_clients")
88 .post(requestedEntity, Response.class);
90 assertEquals(400, response.getStatus());
91 ApiError responseError = response.readEntity(ApiError.class);
92 assertNotNull(responseError);
93 assertEquals("dcaeLocationName", responseError.getFields());
97 public void addMr_Client_shouldReturnErrorWhenNoClientRoleProvided() {
98 Entity<MR_Client> requestedEntity = entity(
99 new MR_Client("dcaeLocation", "fqtn", null, new String[]{"GET"}), APPLICATION_JSON);
101 Response response = testContainer.target("mr_clients")
103 .post(requestedEntity, Response.class);
105 assertEquals(400, response.getStatus());
106 ApiError responseError = response.readEntity(ApiError.class);
107 assertNotNull(responseError);
108 assertEquals("clientRole or clientIdentity", responseError.getFields());
112 public void addMr_Client_shouldReturnErrorWhenNoActionsProvided() {
113 Entity<MR_Client> requestedEntity = entity(
114 new MR_Client("dcaeLocation", "fqtn", "clientRole", null), APPLICATION_JSON);
116 Response response = testContainer.target("mr_clients")
118 .post(requestedEntity, Response.class);
120 assertEquals(400, response.getStatus());
121 ApiError responseError = response.readEntity(ApiError.class);
122 assertNotNull(responseError);
123 assertEquals("action", responseError.getFields());
127 public void addMr_Client_shouldReturnErrorWhereThereIsNoMrClusterAvailable() {
128 Entity<MR_Client> requestedEntity = entity(
129 new MR_Client("dcaeLocationName", "fqtn", "clientRole", new String[]{"GET"}), APPLICATION_JSON);
131 Response response = testContainer.target("mr_clients")
133 .post(requestedEntity, Response.class);
135 assertEquals(400, response.getStatus());
136 ApiError responseError = response.readEntity(ApiError.class);
137 assertNotNull(responseError);
138 assertEquals("dcaeLocationName", responseError.getFields());
142 public void addMr_Client_shouldReturnErrorWhereThereIsNoTopicForFqtnAvailable() {
143 Entity<MR_Client> requestedEntity = entity(
144 new MR_Client("dcaeLocation", "fqtn", "clientRole", new String[]{"GET"}), APPLICATION_JSON);
146 createMrCluster(new MR_Cluster("dcaeLocation", "fqdn", "protocol", "port"));
148 Response response = testContainer.target("mr_clients")
150 .post(requestedEntity, Response.class);
152 assertEquals(404, response.getStatus());
153 ApiError responseError = response.readEntity(ApiError.class);
154 assertNotNull(responseError);
155 assertEquals("fqtn", responseError.getFields());
159 public void addMr_Client_shouldAddMrClient() {
160 Entity<MR_Client> requestedEntity = entity(
161 new MR_Client("dcaeLocation2", "testTopic", "clientRole", new String[]{"GET"}), APPLICATION_JSON);
163 createMrCluster(new MR_Cluster("dcaeLocation2", "fqdn", "protocol", "port"));
164 createTopic("testTopic");
166 Response response = testContainer.target("mr_clients")
168 .post(requestedEntity, Response.class);
170 assertEquals(200, response.getStatus());
171 assertTrue(response.hasEntity());
172 assertMRClientExistInDB(response.readEntity(MR_Client.class));
176 public void deleteMr_Client_shouldDeleteMrClient() {
177 Entity<MR_Client> requestedEntity = entity(
178 new MR_Client("dcaeLocation3", "testTopic", "clientRole", new String[]{"GET"}), APPLICATION_JSON);
179 createMrCluster(new MR_Cluster("dcaeLocation3", "fqdn", "protocol", "port"));
180 createTopic("testTopic");
182 Response response = testContainer.target("mr_clients")
184 .post(requestedEntity, Response.class);
185 assertEquals(200, response.getStatus());
187 MR_Client mrClientEntity = response.readEntity(MR_Client.class);
188 Response deleteResponse = testContainer.target("mr_clients")
189 .path(mrClientEntity.getMrClientId())
193 assertEquals(204, deleteResponse.getStatus());
194 assertMrClientNotExistInDB(mrClientEntity.getMrClientId());
198 public void deleteMr_Clients_shouldReturnMethodNotAllowedCodeWhenClientIdIsMissing() {
199 Response deleteResponse = testContainer.target("mr_clients")
203 assertEquals(405, deleteResponse.getStatus());
207 public void getMr_Client_shouldReturnErrorWhenThereIsNoClient() {
208 Response response = testContainer.target("mr_clients")
209 .path("not_existing_id")
213 assertEquals(404, response.getStatus());
214 ApiError responseError = response.readEntity(ApiError.class);
215 assertNotNull(responseError);
216 assertEquals("mrClientId", responseError.getFields());
220 public void updateMr_Client_shouldReturnErrorWhenNoFqtnProvided() {
221 MR_Client mrClient = new MR_Client("dcaeLocation", null, "clientRole", new String[]{"GET"});
222 Entity<MR_Client> requestedEntity = entity(mrClient, APPLICATION_JSON);
224 Response response = testContainer.target("mr_clients")
225 .path(mrClient.getMrClientId())
227 .put(requestedEntity, Response.class);
229 assertEquals(400, response.getStatus());
230 ApiError responseError = response.readEntity(ApiError.class);
231 assertNotNull(responseError);
232 assertEquals("fqtn", responseError.getFields());
236 public void updateMr_Client_shouldUpdate() {
237 Entity<MR_Client> requestedEntity = entity(
238 new MR_Client("dcaeLocation4", "testTopic", "clientRole", new String[]{"GET"}), APPLICATION_JSON);
240 createMrCluster(new MR_Cluster("dcaeLocation4", "fqdn", "protocol", "port"));
241 createTopic("testTopic");
243 Response response = testContainer.target("mr_clients")
245 .post(requestedEntity, Response.class);
246 MR_Client createdMrClient = response.readEntity(MR_Client.class);
247 createdMrClient.setDcaeLocationName("updatedDcaeLocation");
250 Response updateResponse = testContainer.target("mr_clients")
251 .path(createdMrClient.getMrClientId())
253 .put(requestedEntity, Response.class);
255 assertEquals(200, updateResponse.getStatus());
256 assertTrue(updateResponse.hasEntity());
257 assertMRClientExistInDB(updateResponse.readEntity(MR_Client.class));
262 public void getMr_Clients_test() {
263 Response response = testContainer.target("mr_clients").request().get(Response.class);
264 System.out.println("GET dr_subs response=" + response.getStatus());
266 assertEquals(200, response.getStatus());
267 assertTrue(response.hasEntity());
271 private void createMrCluster(MR_Cluster cluster) {
272 Response response = testContainer.target("mr_clusters")
274 .post(entity(cluster, APPLICATION_JSON), Response.class);
275 assertEquals(201, response.getStatus());
278 private void createTopic(String tname) {
279 Topic topic = new Topic();
280 topic.setFqtn(tname);
281 topic.setFqtn(tname);
282 DatabaseClass.getTopics().put(topic.getFqtn(), topic);
285 private void assertMRClientExistInDB(MR_Client created) {
286 Response response = testContainer.target("mr_clients")
287 .path(created.getMrClientId())
290 assertEquals(200, response.getStatus());
291 assertTrue(response.hasEntity());
292 MR_Client receivedMrClient = response.readEntity(MR_Client.class);
293 assertEquals(created.getFqtn(), receivedMrClient.getFqtn());
294 assertEquals(created.getDcaeLocationName(), receivedMrClient.getDcaeLocationName());
297 private void assertMrClientNotExistInDB(String clientId) {
298 assertEquals(404, testContainer.target("mr_clients")