f131d8f9ae406c6a6c2edb8f58a233e3fd822798
[dmaap/buscontroller.git] / dmaap-bc / 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_whenDrNodeForUpdateDoesNotExistInDb() {
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(node.getFqdn())
137                 .request()
138                 .put(requestedEntity, Response.class);
139
140         assertEquals(404, response.getStatus());
141         ApiError responseError = response.readEntity(ApiError.class);
142         assertNotNull(responseError);
143         assertEquals("fqdn", responseError.getFields());
144         assertEquals("Node " + node.getFqdn() + " does not exist", responseError.getMessage());
145     }
146
147     @Test
148     public void updateDr_Node_ShouldExecuteSuccessfully() {
149         DR_Node toUpdate = new DR_Node("fqdn", "location", "hostName", "1.0");
150         Entity<DR_Node> requestedEntity = entity(toUpdate, APPLICATION_JSON);
151
152         addDrNode(new DR_Node("fqdn", "old_location", "old_hostName", "old_1.0"));
153         Response response = testContainer.target("dr_nodes")
154                 .path(toUpdate.getFqdn())
155                 .request()
156                 .put(requestedEntity, Response.class);
157
158         assertEquals(200, response.getStatus());
159         assertTrue(response.hasEntity());
160         assertEquals(toUpdate, response.readEntity(DR_Node.class));
161     }
162
163     @Test
164     public void deleteDr_Node_shouldReturnError_whenDrNodeForDeleteDoesNotExistInDb() {
165         Response response = testContainer.target("dr_nodes")
166                 .path("fqdn")
167                 .request()
168                 .delete();
169
170         assertEquals(404, response.getStatus());
171         ApiError responseError = response.readEntity(ApiError.class);
172         assertNotNull(responseError);
173         assertEquals("fqdn", responseError.getFields());
174         assertEquals("Node fqdn does not exist", responseError.getMessage());
175     }
176
177     @Test
178     public void deleteDr_Node_shouldReturnError_whenNoExistingFqdnProvided() {
179         Response response = testContainer.target("dr_nodes")
180                 .path("")
181                 .request()
182                 .delete();
183
184         assertEquals(405, response.getStatus());
185     }
186
187     @Test
188     public void deleteDr_Node_shouldExecuteSuccessfully() {
189         DR_Node node = new DR_Node("fqdn", "location", "hostName", "1.0");
190
191         addDrNode(node);
192         Response response = testContainer.target("dr_nodes")
193                 .path("fqdn")
194                 .request()
195                 .delete();
196
197         assertEquals(204, response.getStatus());
198     }
199
200     @Test
201     public void getDr_Node_shouldReturnError_whenDrNodeForDeleteDoesNotExistInDb() {
202         Response response = testContainer.target("dr_nodes")
203                 .path("fqdn")
204                 .request()
205                 .get();
206
207         assertEquals(404, response.getStatus());
208         ApiError responseError = response.readEntity(ApiError.class);
209         assertNotNull(responseError);
210         assertEquals("fqdn", responseError.getFields());
211         assertEquals("Node fqdn does not exist", responseError.getMessage());
212     }
213
214     private Response addDrNode(DR_Node node) {
215         return testContainer.target("dr_nodes")
216                 .request()
217                 .post(entity(node, APPLICATION_JSON), Response.class);
218     }
219
220     private void assertDrNodeExistInDB(DR_Node created) {
221         Response response = testContainer.target("dr_nodes")
222                 .path(created.getFqdn())
223                 .request()
224                 .get();
225         assertEquals(200, response.getStatus());
226         assertTrue(response.hasEntity());
227         assertEquals(created, response.readEntity(DR_Node.class));
228     }
229
230     @Before
231     public void cleanupDatabase() {
232         DatabaseClass.clearDatabase();
233     }
234
235 }
236