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 DatabaseClass.getDmaap().init(DMAAP_OBJECT_FACTORY.genDmaap());
55 testContainer = new FastJerseyTestContainer(new ResourceConfig()
56 .register(MR_ClusterResource.class).register(DcaeLocationResource.class));
61 public static void tearDownClass() throws Exception {
62 testContainer.destroy();
63 /*TODO: Cannot cleanup yet until still other Resources tests depends on the static DB content
65 DatabaseClass.getDmaap().remove();
66 DatabaseClass.clearDatabase();*/
70 public void setUpClusterAndLocation() {
71 DatabaseClass.clearDatabase();
75 public void getMrClusters_shouldReturnEmptyList_whenNoMrClustersInDataBase() {
77 Response resp = testContainer.target(MR_CLUSTERS_TARGET).request().get(Response.class);
80 assertEquals(HttpStatus.OK_200, resp.getStatus());
81 assertTrue(resp.hasEntity());
83 List<MR_Cluster> mrClusters = resp.readEntity(new GenericType<List<MR_Cluster>>() {
85 assertTrue(mrClusters.isEmpty());
89 public void addMrCluster_shouldReturnValidationError_whenDcaeLocationNameNotProvided() {
91 Entity<MR_Cluster> requestEntity = entity(new MR_Cluster(), APPLICATION_JSON);
94 Response resp = testContainer.target(MR_CLUSTERS_TARGET).request().post(requestEntity, Response.class);
97 assertEquals(HttpStatus.BAD_REQUEST_400, resp.getStatus());
98 assertTrue(resp.hasEntity());
99 ApiError errorObj = resp.readEntity(ApiError.class);
100 assertEquals("dcaeLocationName", errorObj.getFields());
104 public void addMrCluster_shouldReturnValidationError_whenFqdnNotProvided() {
106 MR_Cluster mr_cluster = new MR_Cluster();
107 mr_cluster.setDcaeLocationName("central-cloud");
108 Entity<MR_Cluster> requestEntity = entity(mr_cluster, APPLICATION_JSON);
111 Response resp = testContainer.target(MR_CLUSTERS_TARGET).request().post(requestEntity, Response.class);
114 assertEquals(HttpStatus.BAD_REQUEST_400, resp.getStatus());
115 assertTrue(resp.hasEntity());
116 ApiError errorObj = resp.readEntity(ApiError.class);
117 assertEquals("fqdn", errorObj.getFields());
121 public void addMrCluster_shouldAddMrClusterToDatabase() {
123 MR_Cluster mrCluster = DMAAP_OBJECT_FACTORY.genMR_Cluster("edge");
124 Entity<MR_Cluster> requestEntity = entity(mrCluster, APPLICATION_JSON);
127 Response resp = testContainer.target(MR_CLUSTERS_TARGET).request().post(requestEntity, Response.class);
130 assertEquals(HttpStatus.CREATED_201, resp.getStatus());
131 assertTrue(resp.hasEntity());
132 MR_Cluster respEntity = resp.readEntity(MR_Cluster.class);
133 assertTrue(respEntity.isStatusValid());
137 public void addMrCluster_shouldReturnInvalidMrCluster_whenClusterCannotBeAddedToDatabase() {
139 MR_Cluster mrCluster = DMAAP_OBJECT_FACTORY.genMR_Cluster("central");
140 Entity<MR_Cluster> requestEntity = entity(mrCluster, APPLICATION_JSON);
141 prepareDcaeLocationForCentralCluster();
144 Response resp = testContainer.target(MR_CLUSTERS_TARGET).request().post(requestEntity, Response.class);
147 assertEquals(HttpStatus.OK_200, resp.getStatus());
148 assertTrue(resp.hasEntity());
149 MR_Cluster respEntity = resp.readEntity(MR_Cluster.class);
150 assertFalse(respEntity.isStatusValid());
153 private void prepareDcaeLocationForCentralCluster() {
154 DcaeLocation centralDcaeLoc = DMAAP_OBJECT_FACTORY.genDcaeLocation("central");
155 centralDcaeLoc.setStatus(DmaapObject_Status.VALID);
156 DatabaseClass.getDcaeLocations().put(centralDcaeLoc.getDcaeLocationName(), centralDcaeLoc);
160 public void updateMrCluster_shouldReturnValidationError_whenDcaeLocationNameNotProvided() {
162 Entity<MR_Cluster> requestEntity = entity(new MR_Cluster(), APPLICATION_JSON);
165 Response resp = testContainer.target(MR_CLUSTERS_TARGET).path("clusterId")
166 .request().put(requestEntity, Response.class);
169 assertEquals(HttpStatus.BAD_REQUEST_400, resp.getStatus());
170 assertTrue(resp.hasEntity());
171 ApiError errorObj = resp.readEntity(ApiError.class);
172 assertEquals("dcaeLocationName", errorObj.getFields());
176 public void updateMrCluster_shouldReturnApiError_whenMrClusterWithGivenIdNotFound() {
178 MR_Cluster mr_cluster = new MR_Cluster();
179 mr_cluster.setDcaeLocationName("central-cloud");
180 Entity<MR_Cluster> requestEntity = entity(mr_cluster, APPLICATION_JSON);
183 Response resp = testContainer.target(MR_CLUSTERS_TARGET).path("notExistingMrCluster")
184 .request().put(requestEntity, Response.class);
187 assertEquals(HttpStatus.NOT_FOUND_404, resp.getStatus());
188 assertTrue(resp.hasEntity());
189 ApiError errorObj = resp.readEntity(ApiError.class);
190 assertEquals("dcaeLocationName", errorObj.getFields());
194 public void updateMrCluster_shouldUpdateClusterInDatabase() {
196 String newReplicationGroup = "someNewReplicationGroup";
197 prepareDcaeLocationForEdgeCluster();
198 String clusterId = provideExistingEdgeMRClusterId();
199 MR_Cluster changedMrCluster = DMAAP_OBJECT_FACTORY.genMR_Cluster("edge");
200 changedMrCluster.setReplicationGroup(newReplicationGroup);
201 Entity<MR_Cluster> requestEntity = entity(changedMrCluster, APPLICATION_JSON);
204 Response resp = testContainer.target(MR_CLUSTERS_TARGET).path(clusterId)
205 .request().put(requestEntity, Response.class);
208 assertEquals(HttpStatus.CREATED_201, resp.getStatus());
209 assertTrue(resp.hasEntity());
210 MR_Cluster respEntity = resp.readEntity(MR_Cluster.class);
211 assertTrue(respEntity.isStatusValid());
212 assertEquals(newReplicationGroup, respEntity.getReplicationGroup());
215 private void prepareDcaeLocationForEdgeCluster() {
216 DcaeLocation edgeDcaeLoc = DMAAP_OBJECT_FACTORY.genDcaeLocation("edge");
217 edgeDcaeLoc.setStatus(DmaapObject_Status.VALID);
218 DatabaseClass.getDcaeLocations().put(edgeDcaeLoc.getDcaeLocationName(), edgeDcaeLoc);
221 private String provideExistingEdgeMRClusterId() {
222 MR_Cluster cluster = DMAAP_OBJECT_FACTORY.genMR_Cluster("edge");
223 cluster.setStatus(DmaapObject_Status.VALID);
224 DatabaseClass.getMr_clusters().put(cluster.getDcaeLocationName(), cluster);
225 return cluster.getDcaeLocationName();
229 public void deleteMr_Cluster_shouldReturnApiError_whenTryingToDeleteNotExistingMrCluster() {
231 Response resp = testContainer.target(MR_CLUSTERS_TARGET).path("notExistingClusterId")
232 .request().delete(Response.class);
235 assertEquals(HttpStatus.NOT_FOUND_404, resp.getStatus());
236 assertTrue(resp.hasEntity());
237 ApiError errorObj = resp.readEntity(ApiError.class);
238 assertEquals("dcaeLocationName", errorObj.getFields());
242 public void deleteMr_Cluster_shouldRemoveMrClusterFromDatabase() {
244 String clusterId = provideExistingEdgeMRClusterId();
247 Response resp = testContainer.target(MR_CLUSTERS_TARGET).path(clusterId)
248 .request().delete(Response.class);
251 assertEquals(HttpStatus.NO_CONTENT_204, resp.getStatus());
252 assertFalse(resp.hasEntity());
256 public void getMr_Cluster_shouldReturnApiError_whenTryingToGetNotExistingMrCluster() {
258 Response resp = testContainer.target(MR_CLUSTERS_TARGET).path("notExistingClusterId")
259 .request().get(Response.class);
262 assertEquals(HttpStatus.OK_200, resp.getStatus());
263 assertTrue(resp.hasEntity());
264 ApiError errorObj = resp.readEntity(ApiError.class);
265 assertEquals("dcaeLocationName", errorObj.getFields());
269 public void getMr_Cluster_shouldReturnExistingMrCluster() {
271 String clusterId = provideExistingEdgeMRClusterId();
274 Response resp = testContainer.target(MR_CLUSTERS_TARGET).path(clusterId)
275 .request().get(Response.class);
278 assertEquals(HttpStatus.CREATED_201, resp.getStatus());
279 assertTrue(resp.hasEntity());
280 MR_Cluster mrCluster = resp.readEntity(MR_Cluster.class);
281 assertEquals(clusterId, mrCluster.getDcaeLocationName());