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 static jakarta.ws.rs.client.Entity.entity;
23 import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
28 import jakarta.ws.rs.client.Entity;
29 import jakarta.ws.rs.core.Response;
30 import org.glassfish.jersey.server.ResourceConfig;
31 import org.junit.AfterClass;
32 import org.junit.Before;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.onap.dmaap.dbcapi.database.DatabaseClass;
36 import org.onap.dmaap.dbcapi.model.ApiError;
37 import org.onap.dmaap.dbcapi.model.DR_Node;
38 import org.onap.dmaap.dbcapi.testframework.DmaapObjectFactory;
41 public class DR_NodeResourceTest {
43 private static final DmaapObjectFactory DMAAP_OBJECT_FACTORY = new DmaapObjectFactory();
44 private static FastJerseyTestContainer testContainer;
47 public static void setUpClass() throws Exception {
48 System.setProperty("ConfigFile", "src/test/resources/dmaapbc.properties");
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_whenDrNodeForUpdateDoesNotExistInDb() {
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")
136 .path(node.getFqdn())
138 .put(requestedEntity, Response.class);
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());
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);
152 addDrNode(new DR_Node("fqdn", "old_location", "old_hostName", "old_1.0"));
153 Response response = testContainer.target("dr_nodes")
154 .path(toUpdate.getFqdn())
156 .put(requestedEntity, Response.class);
158 assertEquals(200, response.getStatus());
159 assertTrue(response.hasEntity());
160 assertEquals(toUpdate, response.readEntity(DR_Node.class));
164 public void deleteDr_Node_shouldReturnError_whenDrNodeForDeleteDoesNotExistInDb() {
165 Response response = testContainer.target("dr_nodes")
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());
178 public void deleteDr_Node_shouldReturnError_whenNoExistingFqdnProvided() {
179 Response response = testContainer.target("dr_nodes")
184 assertEquals(405, response.getStatus());
188 public void deleteDr_Node_shouldExecuteSuccessfully() {
189 DR_Node node = new DR_Node("fqdn", "location", "hostName", "1.0");
192 Response response = testContainer.target("dr_nodes")
197 assertEquals(204, response.getStatus());
201 public void getDr_Node_shouldReturnError_whenDrNodeForDeleteDoesNotExistInDb() {
202 Response response = testContainer.target("dr_nodes")
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());
214 private Response addDrNode(DR_Node node) {
215 return testContainer.target("dr_nodes")
217 .post(entity(node, APPLICATION_JSON), Response.class);
220 private void assertDrNodeExistInDB(DR_Node created) {
221 Response response = testContainer.target("dr_nodes")
222 .path(created.getFqdn())
225 assertEquals(200, response.getStatus());
226 assertTrue(response.hasEntity());
227 assertEquals(created, response.readEntity(DR_Node.class));
231 public void cleanupDatabase() {
232 DatabaseClass.clearDatabase();