[DMAAP-BC] Consolidate bus controller repos
[dmaap/buscontroller.git] / dmaap-bc / src / test / java / org / onap / dmaap / dbcapi / resources / MR_ClusterResourceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20 package org.onap.dmaap.dbcapi.resources;
21
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;
27
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;
44
45 public class MR_ClusterResourceTest {
46
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";
50
51         @BeforeClass
52         public static void setUpClass() throws Exception {
53                 DatabaseClass.getDmaap().init(DMAAP_OBJECT_FACTORY.genDmaap());
54
55                 testContainer = new FastJerseyTestContainer(new ResourceConfig()
56                         .register(MR_ClusterResource.class).register(DcaeLocationResource.class));
57                 testContainer.init();
58         }
59
60         @AfterClass
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
64
65         DatabaseClass.getDmaap().remove();
66         DatabaseClass.clearDatabase();*/
67         }
68
69         @Before
70         public void setUpClusterAndLocation() {
71                 DatabaseClass.clearDatabase();
72         }
73
74         @Test
75         public void getMrClusters_shouldReturnEmptyList_whenNoMrClustersInDataBase() {
76                 //when
77                 Response resp = testContainer.target(MR_CLUSTERS_TARGET).request().get(Response.class);
78
79                 //then
80                 assertEquals(HttpStatus.OK_200, resp.getStatus());
81                 assertTrue(resp.hasEntity());
82
83                 List<MR_Cluster> mrClusters = resp.readEntity(new GenericType<List<MR_Cluster>>() {
84                 });
85                 assertTrue(mrClusters.isEmpty());
86         }
87
88         @Test
89         public void addMrCluster_shouldReturnValidationError_whenDcaeLocationNameNotProvided() {
90                 //given
91                 Entity<MR_Cluster> requestEntity = entity(new MR_Cluster(), APPLICATION_JSON);
92
93                 //when
94                 Response resp = testContainer.target(MR_CLUSTERS_TARGET).request().post(requestEntity, Response.class);
95
96                 //then
97                 assertEquals(HttpStatus.BAD_REQUEST_400, resp.getStatus());
98                 assertTrue(resp.hasEntity());
99                 ApiError errorObj = resp.readEntity(ApiError.class);
100                 assertEquals("dcaeLocationName", errorObj.getFields());
101         }
102
103         @Test
104         public void addMrCluster_shouldReturnValidationError_whenFqdnNotProvided() {
105                 //given
106                 MR_Cluster mr_cluster = new MR_Cluster();
107                 mr_cluster.setDcaeLocationName("central-cloud");
108                 Entity<MR_Cluster> requestEntity = entity(mr_cluster, APPLICATION_JSON);
109
110                 //when
111                 Response resp = testContainer.target(MR_CLUSTERS_TARGET).request().post(requestEntity, Response.class);
112
113                 //then
114                 assertEquals(HttpStatus.BAD_REQUEST_400, resp.getStatus());
115                 assertTrue(resp.hasEntity());
116                 ApiError errorObj = resp.readEntity(ApiError.class);
117                 assertEquals("fqdn", errorObj.getFields());
118         }
119
120         @Test
121         public void addMrCluster_shouldAddMrClusterToDatabase() {
122                 //given
123                 MR_Cluster mrCluster = DMAAP_OBJECT_FACTORY.genMR_Cluster("edge");
124                 Entity<MR_Cluster> requestEntity = entity(mrCluster, APPLICATION_JSON);
125
126                 //when
127                 Response resp = testContainer.target(MR_CLUSTERS_TARGET).request().post(requestEntity, Response.class);
128
129                 //then
130                 assertEquals(HttpStatus.CREATED_201, resp.getStatus());
131                 assertTrue(resp.hasEntity());
132                 MR_Cluster respEntity = resp.readEntity(MR_Cluster.class);
133                 assertTrue(respEntity.isStatusValid());
134         }
135
136         @Test
137         public void addMrCluster_shouldReturnInvalidMrCluster_whenClusterCannotBeAddedToDatabase() {
138                 //given
139                 MR_Cluster mrCluster = DMAAP_OBJECT_FACTORY.genMR_Cluster("central");
140                 Entity<MR_Cluster> requestEntity = entity(mrCluster, APPLICATION_JSON);
141                 prepareDcaeLocationForCentralCluster();
142
143                 //when
144                 Response resp = testContainer.target(MR_CLUSTERS_TARGET).request().post(requestEntity, Response.class);
145
146                 //then
147                 assertEquals(HttpStatus.OK_200, resp.getStatus());
148                 assertTrue(resp.hasEntity());
149                 MR_Cluster respEntity = resp.readEntity(MR_Cluster.class);
150                 assertFalse(respEntity.isStatusValid());
151         }
152
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);
157         }
158
159         @Test
160         public void updateMrCluster_shouldReturnValidationError_whenDcaeLocationNameNotProvided() {
161                 //given
162                 Entity<MR_Cluster> requestEntity = entity(new MR_Cluster(), APPLICATION_JSON);
163
164                 //when
165                 Response resp = testContainer.target(MR_CLUSTERS_TARGET).path("clusterId")
166                         .request().put(requestEntity, Response.class);
167
168                 //then
169                 assertEquals(HttpStatus.BAD_REQUEST_400, resp.getStatus());
170                 assertTrue(resp.hasEntity());
171                 ApiError errorObj = resp.readEntity(ApiError.class);
172                 assertEquals("dcaeLocationName", errorObj.getFields());
173         }
174
175         @Test
176         public void updateMrCluster_shouldReturnApiError_whenMrClusterWithGivenIdNotFound() {
177                 //given
178                 MR_Cluster mr_cluster = new MR_Cluster();
179                 mr_cluster.setDcaeLocationName("central-cloud");
180                 Entity<MR_Cluster> requestEntity = entity(mr_cluster, APPLICATION_JSON);
181
182                 //when
183                 Response resp = testContainer.target(MR_CLUSTERS_TARGET).path("notExistingMrCluster")
184                         .request().put(requestEntity, Response.class);
185
186                 //then
187                 assertEquals(HttpStatus.NOT_FOUND_404, resp.getStatus());
188                 assertTrue(resp.hasEntity());
189                 ApiError errorObj = resp.readEntity(ApiError.class);
190                 assertEquals("dcaeLocationName", errorObj.getFields());
191         }
192
193         @Test
194         public void updateMrCluster_shouldUpdateClusterInDatabase() {
195                 //given
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);
202
203                 //when
204                 Response resp = testContainer.target(MR_CLUSTERS_TARGET).path(clusterId)
205                         .request().put(requestEntity, Response.class);
206
207                 //then
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());
213         }
214
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);
219         }
220
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();
226         }
227
228         @Test
229         public void deleteMr_Cluster_shouldReturnApiError_whenTryingToDeleteNotExistingMrCluster() {
230                 //when
231                 Response resp = testContainer.target(MR_CLUSTERS_TARGET).path("notExistingClusterId")
232                         .request().delete(Response.class);
233
234                 //then
235                 assertEquals(HttpStatus.NOT_FOUND_404, resp.getStatus());
236                 assertTrue(resp.hasEntity());
237                 ApiError errorObj = resp.readEntity(ApiError.class);
238                 assertEquals("dcaeLocationName", errorObj.getFields());
239         }
240
241         @Test
242         public void deleteMr_Cluster_shouldRemoveMrClusterFromDatabase() {
243                 //given
244                 String clusterId = provideExistingEdgeMRClusterId();
245
246                 //when
247                 Response resp = testContainer.target(MR_CLUSTERS_TARGET).path(clusterId)
248                         .request().delete(Response.class);
249
250                 //then
251                 assertEquals(HttpStatus.NO_CONTENT_204, resp.getStatus());
252                 assertFalse(resp.hasEntity());
253         }
254
255         @Test
256         public void getMr_Cluster_shouldReturnApiError_whenTryingToGetNotExistingMrCluster() {
257                 //when
258                 Response resp = testContainer.target(MR_CLUSTERS_TARGET).path("notExistingClusterId")
259                         .request().get(Response.class);
260
261                 //then
262                 assertEquals(HttpStatus.OK_200, resp.getStatus());
263                 assertTrue(resp.hasEntity());
264                 ApiError errorObj = resp.readEntity(ApiError.class);
265                 assertEquals("dcaeLocationName", errorObj.getFields());
266         }
267
268         @Test
269         public void getMr_Cluster_shouldReturnExistingMrCluster() {
270                 //given
271                 String clusterId = provideExistingEdgeMRClusterId();
272
273                 //when
274                 Response resp = testContainer.target(MR_CLUSTERS_TARGET).path(clusterId)
275                         .request().get(Response.class);
276
277                 //then
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());
282         }
283
284 }