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