856a789586823ab3a8b4ddbc40edba46a25b8ee0
[dmaap/dbcapi.git] / src / test / java / org / onap / dmaap / dbcapi / resources / DR_NodeResourceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
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
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 org.glassfish.jersey.server.ResourceConfig;
23 import org.junit.AfterClass;
24 import org.junit.Before;
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.DR_Node;
30 import org.onap.dmaap.dbcapi.testframework.DmaapObjectFactory;
31
32 import javax.ws.rs.client.Entity;
33 import javax.ws.rs.core.Response;
34
35 import static javax.ws.rs.client.Entity.entity;
36 import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
37 import static org.junit.Assert.assertEquals;
38 import static org.junit.Assert.assertNotNull;
39 import static org.junit.Assert.assertTrue;
40
41
42 public class DR_NodeResourceTest {
43
44     private static final DmaapObjectFactory DMAAP_OBJECT_FACTORY = new DmaapObjectFactory();
45     private static FastJerseyTestContainer testContainer;
46
47     @BeforeClass
48     public static void setUpClass() throws Exception {
49         DatabaseClass.getDmaap().init(DMAAP_OBJECT_FACTORY.genDmaap());
50
51         testContainer = new FastJerseyTestContainer(new ResourceConfig()
52                 .register(DR_NodeResource.class));
53         testContainer.init();
54     }
55
56     @AfterClass
57     public static void tearDownClass() throws Exception {
58         testContainer.destroy();
59         /*TODO: Cannot cleanup yet until still other Resources tests depends on the static DB content
60
61         DatabaseClass.clearDatabase();
62         DatabaseClass.getDmaap().remove();*/
63     }
64
65     @Test
66     public void getDr_Nodes_test() {
67         Response response = testContainer.target("dr_nodes").request().get(Response.class);
68         System.out.println("GET dr_subs response=" + response.getStatus());
69
70         assertEquals(200, response.getStatus());
71         assertTrue(response.hasEntity());
72     }
73
74     @Test
75     public void addDr_Node_shouldReturnError_whenNoLocationAndFqdnProvided() {
76         DR_Node node = new DR_Node(null, null, "hostName", "1.0");
77         Entity<DR_Node> requestedEntity = entity(node, APPLICATION_JSON);
78
79         Response response = testContainer.target("dr_nodes")
80                 .request()
81                 .post(requestedEntity, Response.class);
82
83         assertEquals(400, response.getStatus());
84         ApiError responseError = response.readEntity(ApiError.class);
85         assertNotNull(responseError);
86         assertEquals("dcaeLocation, fqdn", responseError.getFields());
87     }
88
89     @Test
90     public void addDr_Node_shouldReturnError_whenDrNodeWithGiveFqdnAlreadyExists() {
91         DR_Node node = new DR_Node("fqdn", "location", "hostName", "1.0");
92
93         addDrNode(node);
94         Response response = addDrNode(node);
95
96         assertEquals(409, response.getStatus());
97         ApiError responseError = response.readEntity(ApiError.class);
98         assertNotNull(responseError);
99         assertEquals("fqdn", responseError.getFields());
100         assertEquals("Node fqdn already exists", responseError.getMessage());
101     }
102
103     @Test
104     public void addDr_Node_shouldExecuteSuccessfully() {
105         DR_Node node = new DR_Node("fqdn", "location", "hostName", "1.0");
106
107         Response response = addDrNode(node);
108
109         assertEquals(200, response.getStatus());
110         assertTrue(response.hasEntity());
111         assertDrNodeExistInDB(response.readEntity(DR_Node.class));
112     }
113
114     @Test
115     public void updateDr_Node_shouldReturnError_whenNoLocationAndFqdnProvided() {
116         DR_Node node = new DR_Node(null, null, "hostName", "1.0");
117         Entity<DR_Node> requestedEntity = entity(node, APPLICATION_JSON);
118
119         Response response = testContainer.target("dr_nodes")
120                 .path("fqdn")
121                 .request()
122                 .put(requestedEntity, Response.class);
123
124         assertEquals(400, response.getStatus());
125         ApiError responseError = response.readEntity(ApiError.class);
126         assertNotNull(responseError);
127         assertEquals("dcaeLocation, fqdn", responseError.getFields());
128     }
129
130     @Test
131     public void updateDr_Node_shouldReturnError_whenNoExistingFqdnProvided() {
132         DR_Node node = new DR_Node("fqdn", "location", "hostName", "1.0");
133         Entity<DR_Node> requestedEntity = entity(node, APPLICATION_JSON);
134
135         Response response = testContainer.target("dr_nodes")
136                 .path("")
137                 .request()
138                 .put(requestedEntity, Response.class);
139
140         assertEquals(405, response.getStatus());
141     }
142
143     @Test
144     public void updateDr_Node_shouldReturnError_whenDrNodeForUpdateDoesNotExistInDb() {
145         DR_Node node = new DR_Node("fqdn", "location", "hostName", "1.0");
146         Entity<DR_Node> requestedEntity = entity(node, APPLICATION_JSON);
147
148         Response response = testContainer.target("dr_nodes")
149                 .path(node.getFqdn())
150                 .request()
151                 .put(requestedEntity, Response.class);
152
153         assertEquals(404, response.getStatus());
154         ApiError responseError = response.readEntity(ApiError.class);
155         assertNotNull(responseError);
156         assertEquals("fqdn", responseError.getFields());
157         assertEquals("Node " + node.getFqdn() + " does not exist", responseError.getMessage());
158     }
159
160     @Test
161     public void updateDr_Node_ShouldExecuteSuccessfully() {
162         DR_Node toUpdate = new DR_Node("fqdn", "location", "hostName", "1.0");
163         Entity<DR_Node> requestedEntity = entity(toUpdate, APPLICATION_JSON);
164
165         addDrNode(new DR_Node("fqdn", "old_location", "old_hostName", "old_1.0"));
166         Response response = testContainer.target("dr_nodes")
167                 .path(toUpdate.getFqdn())
168                 .request()
169                 .put(requestedEntity, Response.class);
170
171         assertEquals(200, response.getStatus());
172         assertTrue(response.hasEntity());
173         assertEquals(toUpdate, response.readEntity(DR_Node.class));
174     }
175
176     @Test
177     public void deleteDr_Node_shouldReturnError_whenDrNodeForDeleteDoesNotExistInDb() {
178         Response response = testContainer.target("dr_nodes")
179                 .path("fqdn")
180                 .request()
181                 .delete();
182
183         assertEquals(404, response.getStatus());
184         ApiError responseError = response.readEntity(ApiError.class);
185         assertNotNull(responseError);
186         assertEquals("fqdn", responseError.getFields());
187         assertEquals("Node fqdn does not exist", responseError.getMessage());
188     }
189
190     @Test
191     public void deleteDr_Node_shouldReturnError_whenNoExistingFqdnProvided() {
192         Response response = testContainer.target("dr_nodes")
193                 .path("")
194                 .request()
195                 .delete();
196
197         assertEquals(405, response.getStatus());
198     }
199
200     @Test
201     public void deleteDr_Node_shouldExecuteSuccessfully() {
202         DR_Node node = new DR_Node("fqdn", "location", "hostName", "1.0");
203
204         addDrNode(node);
205         Response response = testContainer.target("dr_nodes")
206                 .path("fqdn")
207                 .request()
208                 .delete();
209
210         assertEquals(204, response.getStatus());
211     }
212
213     @Test
214     public void getDr_Node_shouldReturnError_whenDrNodeForDeleteDoesNotExistInDb() {
215         Response response = testContainer.target("dr_nodes")
216                 .path("fqdn")
217                 .request()
218                 .get();
219
220         assertEquals(404, response.getStatus());
221         ApiError responseError = response.readEntity(ApiError.class);
222         assertNotNull(responseError);
223         assertEquals("fqdn", responseError.getFields());
224         assertEquals("Node fqdn does not exist", responseError.getMessage());
225     }
226
227     private Response addDrNode(DR_Node node) {
228         return testContainer.target("dr_nodes")
229                 .request()
230                 .post(entity(node, APPLICATION_JSON), Response.class);
231     }
232
233     private void assertDrNodeExistInDB(DR_Node created) {
234         Response response = testContainer.target("dr_nodes")
235                 .path(created.getFqdn())
236                 .request()
237                 .get();
238         assertEquals(200, response.getStatus());
239         assertTrue(response.hasEntity());
240         assertEquals(created, response.readEntity(DR_Node.class));
241     }
242
243     @Before
244     public void cleanupDatabase() {
245         DatabaseClass.clearDatabase();
246     }
247
248 }
249