2 * ============LICENSE_START=======================================================
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
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 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;
32 import javax.ws.rs.client.Entity;
33 import javax.ws.rs.core.Response;
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;
42 public class DR_NodeResourceTest {
44 private static final DmaapObjectFactory DMAAP_OBJECT_FACTORY = new DmaapObjectFactory();
45 private static FastJerseyTestContainer testContainer;
48 public static void setUpClass() throws Exception {
49 DatabaseClass.getDmaap().init(DMAAP_OBJECT_FACTORY.genDmaap());
51 testContainer = new FastJerseyTestContainer(new ResourceConfig()
52 .register(DR_NodeResource.class));
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
61 DatabaseClass.clearDatabase();
62 DatabaseClass.getDmaap().remove();*/
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());
70 assertEquals(200, response.getStatus());
71 assertTrue(response.hasEntity());
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);
79 Response response = testContainer.target("dr_nodes")
81 .post(requestedEntity, Response.class);
83 assertEquals(400, response.getStatus());
84 ApiError responseError = response.readEntity(ApiError.class);
85 assertNotNull(responseError);
86 assertEquals("dcaeLocation, fqdn", responseError.getFields());
90 public void addDr_Node_shouldReturnError_whenDrNodeWithGiveFqdnAlreadyExists() {
91 DR_Node node = new DR_Node("fqdn", "location", "hostName", "1.0");
94 Response response = addDrNode(node);
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());
104 public void addDr_Node_shouldExecuteSuccessfully() {
105 DR_Node node = new DR_Node("fqdn", "location", "hostName", "1.0");
107 Response response = addDrNode(node);
109 assertEquals(200, response.getStatus());
110 assertTrue(response.hasEntity());
111 assertDrNodeExistInDB(response.readEntity(DR_Node.class));
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);
119 Response response = testContainer.target("dr_nodes")
122 .put(requestedEntity, Response.class);
124 assertEquals(400, response.getStatus());
125 ApiError responseError = response.readEntity(ApiError.class);
126 assertNotNull(responseError);
127 assertEquals("dcaeLocation, fqdn", responseError.getFields());
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);
135 Response response = testContainer.target("dr_nodes")
138 .put(requestedEntity, Response.class);
140 assertEquals(405, response.getStatus());
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);
148 Response response = testContainer.target("dr_nodes")
149 .path(node.getFqdn())
151 .put(requestedEntity, Response.class);
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());
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);
165 addDrNode(new DR_Node("fqdn", "old_location", "old_hostName", "old_1.0"));
166 Response response = testContainer.target("dr_nodes")
167 .path(toUpdate.getFqdn())
169 .put(requestedEntity, Response.class);
171 assertEquals(200, response.getStatus());
172 assertTrue(response.hasEntity());
173 assertEquals(toUpdate, response.readEntity(DR_Node.class));
177 public void deleteDr_Node_shouldReturnError_whenDrNodeForDeleteDoesNotExistInDb() {
178 Response response = testContainer.target("dr_nodes")
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());
191 public void deleteDr_Node_shouldReturnError_whenNoExistingFqdnProvided() {
192 Response response = testContainer.target("dr_nodes")
197 assertEquals(405, response.getStatus());
201 public void deleteDr_Node_shouldExecuteSuccessfully() {
202 DR_Node node = new DR_Node("fqdn", "location", "hostName", "1.0");
205 Response response = testContainer.target("dr_nodes")
210 assertEquals(204, response.getStatus());
214 public void getDr_Node_shouldReturnError_whenDrNodeForDeleteDoesNotExistInDb() {
215 Response response = testContainer.target("dr_nodes")
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());
227 private Response addDrNode(DR_Node node) {
228 return testContainer.target("dr_nodes")
230 .post(entity(node, APPLICATION_JSON), Response.class);
233 private void assertDrNodeExistInDB(DR_Node created) {
234 Response response = testContainer.target("dr_nodes")
235 .path(created.getFqdn())
238 assertEquals(200, response.getStatus());
239 assertTrue(response.hasEntity());
240 assertEquals(created, response.readEntity(DR_Node.class));
244 public void cleanupDatabase() {
245 DatabaseClass.clearDatabase();