2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Nokia 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=========================================================
20 package org.onap.dmaap.dbcapi.resources;
22 import static javax.ws.rs.client.Entity.entity;
23 import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
28 import java.util.List;
29 import javax.ws.rs.client.Entity;
30 import javax.ws.rs.core.GenericType;
31 import javax.ws.rs.core.Response;
32 import org.eclipse.jetty.http.HttpStatus;
33 import org.glassfish.jersey.server.ResourceConfig;
34 import org.junit.AfterClass;
35 import org.junit.Before;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.onap.dmaap.dbcapi.database.DatabaseClass;
39 import org.onap.dmaap.dbcapi.model.ApiError;
40 import org.onap.dmaap.dbcapi.model.DcaeLocation;
41 import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status;
42 import org.onap.dmaap.dbcapi.model.MR_Cluster;
43 import org.onap.dmaap.dbcapi.testframework.DmaapObjectFactory;
45 public class MR_ClusterResourceTest {
47 private static final DmaapObjectFactory DMAAP_OBJECT_FACTORY = new DmaapObjectFactory();
48 private static FastJerseyTestContainer testContainer;
49 private static final String MR_CLUSTERS_TARGET = "mr_clusters";
52 public static void setUpClass() throws Exception {
53 System.setProperty("ConfigFile", "src/test/resources/dmaapbc.properties");
54 DatabaseClass.getDmaap().init(DMAAP_OBJECT_FACTORY.genDmaap());
56 testContainer = new FastJerseyTestContainer(new ResourceConfig()
57 .register(MR_ClusterResource.class).register(DcaeLocationResource.class));
62 public static void tearDownClass() throws Exception {
63 testContainer.destroy();
64 /*TODO: Cannot cleanup yet until still other Resources tests depends on the static DB content
66 DatabaseClass.getDmaap().remove();
67 DatabaseClass.clearDatabase();*/
71 public void setUpClusterAndLocation() {
72 DatabaseClass.clearDatabase();
76 public void getMrClusters_shouldReturnEmptyList_whenNoMrClustersInDataBase() {
78 Response resp = testContainer.target(MR_CLUSTERS_TARGET).request().get(Response.class);
81 assertEquals(HttpStatus.OK_200, resp.getStatus());
82 assertTrue(resp.hasEntity());
84 List<MR_Cluster> mrClusters = resp.readEntity(new GenericType<List<MR_Cluster>>() {
86 assertTrue(mrClusters.isEmpty());
90 public void addMrCluster_shouldReturnValidationError_whenDcaeLocationNameNotProvided() {
92 Entity<MR_Cluster> requestEntity = entity(new MR_Cluster(), APPLICATION_JSON);
95 Response resp = testContainer.target(MR_CLUSTERS_TARGET).request().post(requestEntity, Response.class);
98 assertEquals(HttpStatus.BAD_REQUEST_400, resp.getStatus());
99 assertTrue(resp.hasEntity());
100 ApiError errorObj = resp.readEntity(ApiError.class);
101 assertEquals("dcaeLocationName", errorObj.getFields());
105 public void addMrCluster_shouldReturnValidationError_whenFqdnNotProvided() {
107 MR_Cluster mr_cluster = new MR_Cluster();
108 mr_cluster.setDcaeLocationName("central-cloud");
109 Entity<MR_Cluster> requestEntity = entity(mr_cluster, APPLICATION_JSON);
112 Response resp = testContainer.target(MR_CLUSTERS_TARGET).request().post(requestEntity, Response.class);
115 assertEquals(HttpStatus.BAD_REQUEST_400, resp.getStatus());
116 assertTrue(resp.hasEntity());
117 ApiError errorObj = resp.readEntity(ApiError.class);
118 assertEquals("fqdn", errorObj.getFields());
122 public void addMrCluster_shouldAddMrClusterToDatabase() {
124 MR_Cluster mrCluster = DMAAP_OBJECT_FACTORY.genMR_Cluster("edge");
125 Entity<MR_Cluster> requestEntity = entity(mrCluster, APPLICATION_JSON);
128 Response resp = testContainer.target(MR_CLUSTERS_TARGET).request().post(requestEntity, Response.class);
131 assertEquals(HttpStatus.CREATED_201, resp.getStatus());
132 assertTrue(resp.hasEntity());
133 MR_Cluster respEntity = resp.readEntity(MR_Cluster.class);
134 assertTrue(respEntity.isStatusValid());
138 public void addMrCluster_shouldReturnInvalidMrCluster_whenClusterCannotBeAddedToDatabase() {
140 MR_Cluster mrCluster = DMAAP_OBJECT_FACTORY.genMR_Cluster("central");
141 Entity<MR_Cluster> requestEntity = entity(mrCluster, APPLICATION_JSON);
142 prepareDcaeLocationForCentralCluster();
145 Response resp = testContainer.target(MR_CLUSTERS_TARGET).request().post(requestEntity, Response.class);
148 assertEquals(HttpStatus.OK_200, resp.getStatus());
149 assertTrue(resp.hasEntity());
150 MR_Cluster respEntity = resp.readEntity(MR_Cluster.class);
151 assertFalse(respEntity.isStatusValid());
154 private void prepareDcaeLocationForCentralCluster() {
155 DcaeLocation centralDcaeLoc = DMAAP_OBJECT_FACTORY.genDcaeLocation("central");
156 centralDcaeLoc.setStatus(DmaapObject_Status.VALID);
157 DatabaseClass.getDcaeLocations().put(centralDcaeLoc.getDcaeLocationName(), centralDcaeLoc);
161 public void updateMrCluster_shouldReturnValidationError_whenDcaeLocationNameNotProvided() {
163 Entity<MR_Cluster> requestEntity = entity(new MR_Cluster(), APPLICATION_JSON);
166 Response resp = testContainer.target(MR_CLUSTERS_TARGET).path("clusterId")
167 .request().put(requestEntity, Response.class);
170 assertEquals(HttpStatus.BAD_REQUEST_400, resp.getStatus());
171 assertTrue(resp.hasEntity());
172 ApiError errorObj = resp.readEntity(ApiError.class);
173 assertEquals("dcaeLocationName", errorObj.getFields());
177 public void updateMrCluster_shouldReturnApiError_whenMrClusterWithGivenIdNotFound() {
179 MR_Cluster mr_cluster = new MR_Cluster();
180 mr_cluster.setDcaeLocationName("central-cloud");
181 Entity<MR_Cluster> requestEntity = entity(mr_cluster, APPLICATION_JSON);
184 Response resp = testContainer.target(MR_CLUSTERS_TARGET).path("notExistingMrCluster")
185 .request().put(requestEntity, Response.class);
188 assertEquals(HttpStatus.NOT_FOUND_404, resp.getStatus());
189 assertTrue(resp.hasEntity());
190 ApiError errorObj = resp.readEntity(ApiError.class);
191 assertEquals("dcaeLocationName", errorObj.getFields());
195 public void updateMrCluster_shouldUpdateClusterInDatabase() {
197 String newReplicationGroup = "someNewReplicationGroup";
198 prepareDcaeLocationForEdgeCluster();
199 String clusterId = provideExistingEdgeMRClusterId();
200 MR_Cluster changedMrCluster = DMAAP_OBJECT_FACTORY.genMR_Cluster("edge");
201 changedMrCluster.setReplicationGroup(newReplicationGroup);
202 Entity<MR_Cluster> requestEntity = entity(changedMrCluster, APPLICATION_JSON);
205 Response resp = testContainer.target(MR_CLUSTERS_TARGET).path(clusterId)
206 .request().put(requestEntity, Response.class);
209 assertEquals(HttpStatus.CREATED_201, resp.getStatus());
210 assertTrue(resp.hasEntity());
211 MR_Cluster respEntity = resp.readEntity(MR_Cluster.class);
212 assertTrue(respEntity.isStatusValid());
213 assertEquals(newReplicationGroup, respEntity.getReplicationGroup());
216 private void prepareDcaeLocationForEdgeCluster() {
217 DcaeLocation edgeDcaeLoc = DMAAP_OBJECT_FACTORY.genDcaeLocation("edge");
218 edgeDcaeLoc.setStatus(DmaapObject_Status.VALID);
219 DatabaseClass.getDcaeLocations().put(edgeDcaeLoc.getDcaeLocationName(), edgeDcaeLoc);
222 private String provideExistingEdgeMRClusterId() {
223 MR_Cluster cluster = DMAAP_OBJECT_FACTORY.genMR_Cluster("edge");
224 cluster.setStatus(DmaapObject_Status.VALID);
225 DatabaseClass.getMr_clusters().put(cluster.getDcaeLocationName(), cluster);
226 return cluster.getDcaeLocationName();
230 public void deleteMr_Cluster_shouldReturnApiError_whenTryingToDeleteNotExistingMrCluster() {
232 Response resp = testContainer.target(MR_CLUSTERS_TARGET).path("notExistingClusterId")
233 .request().delete(Response.class);
236 assertEquals(HttpStatus.NOT_FOUND_404, resp.getStatus());
237 assertTrue(resp.hasEntity());
238 ApiError errorObj = resp.readEntity(ApiError.class);
239 assertEquals("dcaeLocationName", errorObj.getFields());
243 public void deleteMr_Cluster_shouldRemoveMrClusterFromDatabase() {
245 String clusterId = provideExistingEdgeMRClusterId();
248 Response resp = testContainer.target(MR_CLUSTERS_TARGET).path(clusterId)
249 .request().delete(Response.class);
252 assertEquals(HttpStatus.NO_CONTENT_204, resp.getStatus());
253 assertFalse(resp.hasEntity());
257 public void getMr_Cluster_shouldReturnApiError_whenTryingToGetNotExistingMrCluster() {
259 Response resp = testContainer.target(MR_CLUSTERS_TARGET).path("notExistingClusterId")
260 .request().get(Response.class);
263 assertEquals(HttpStatus.OK_200, resp.getStatus());
264 assertTrue(resp.hasEntity());
265 ApiError errorObj = resp.readEntity(ApiError.class);
266 assertEquals("dcaeLocationName", errorObj.getFields());
270 public void getMr_Cluster_shouldReturnExistingMrCluster() {
272 String clusterId = provideExistingEdgeMRClusterId();
275 Response resp = testContainer.target(MR_CLUSTERS_TARGET).path(clusterId)
276 .request().get(Response.class);
279 assertEquals(HttpStatus.CREATED_201, resp.getStatus());
280 assertTrue(resp.hasEntity());
281 MR_Cluster mrCluster = resp.readEntity(MR_Cluster.class);
282 assertEquals(clusterId, mrCluster.getDcaeLocationName());