Tests for DR_NodeResource & bugfixes 28/84328/1
authorpkaras <piotr.karas@nokia.com>
Fri, 5 Apr 2019 11:49:58 +0000 (13:49 +0200)
committerpkaras <piotr.karas@nokia.com>
Fri, 5 Apr 2019 11:49:58 +0000 (13:49 +0200)
Change-Id: I7e1767b63e3cd091718bbf1d0d38e5232b7ae7e9
Issue-ID: DMAAP-1149
Signed-off-by: piotr.karas <piotr.karas@nokia.com>
src/main/java/org/onap/dmaap/dbcapi/model/DR_Node.java
src/main/java/org/onap/dmaap/dbcapi/resources/DR_NodeResource.java
src/main/java/org/onap/dmaap/dbcapi/service/DR_NodeService.java
src/test/java/org/onap/dmaap/dbcapi/resources/DR_NodeResourceTest.java
src/test/java/org/onap/dmaap/dbcapi/resources/DR_SubResourceTest.java
src/test/java/org/onap/dmaap/dbcapi/resources/FastJerseyTestContainer.java [new file with mode: 0644]

index a85f040..4b2ef90 100644 (file)
@@ -21,6 +21,7 @@
 package org.onap.dmaap.dbcapi.model;
 
 import javax.xml.bind.annotation.XmlRootElement;
+import java.util.Objects;
 
 @XmlRootElement
 public class DR_Node extends DmaapObject {
@@ -75,4 +76,19 @@ public class DR_Node extends DmaapObject {
                this.version = version;
        }
 
+       @Override
+       public boolean equals(Object o) {
+               if (this == o) return true;
+               if (o == null || getClass() != o.getClass()) return false;
+               DR_Node dr_node = (DR_Node) o;
+               return Objects.equals(fqdn, dr_node.fqdn) &&
+                               Objects.equals(dcaeLocationName, dr_node.dcaeLocationName) &&
+                               Objects.equals(hostName, dr_node.hostName) &&
+                               Objects.equals(version, dr_node.version);
+       }
+
+       @Override
+       public int hashCode() {
+               return Objects.hash(fqdn, dcaeLocationName, hostName, version);
+       }
 }
index 49dc69a..d29f353 100644 (file)
@@ -118,10 +118,11 @@ public class DR_NodeResource extends BaseLoggingClass {
                ApiService resp = new ApiService();
 
                try {
-                       checker.required( "dcaeLocation", name);
+                       checker.required( "dcaeLocation", node.getDcaeLocationName());
                        checker.required( "fqdn", node.getFqdn());
                } catch ( RequiredFieldException rfe ) {
-                       return responseBuilder.error(rfe.getApiError());
+                       return responseBuilder.error(new ApiError(BAD_REQUEST.getStatusCode(),
+                                       "missing required field", "dcaeLocation, fqdn"));
                }
                node.setFqdn(name);
                DR_Node nNode = dr_nodeService.updateDr_Node(node, resp.getErr());
@@ -143,15 +144,8 @@ public class DR_NodeResource extends BaseLoggingClass {
        public Response deleteDr_Node( 
                        @PathParam("fqdn") String name
                        ){
-
                ApiService resp = new ApiService();
 
-               try {
-                       checker.required( "fqdn", name);
-               } catch ( RequiredFieldException rfe ) {
-                       logger.debug( rfe.getApiError().toString() );
-                       return responseBuilder.error(rfe.getApiError());
-               }
                dr_nodeService.removeDr_Node(name, resp.getErr());
                if ( resp.getErr().is2xx() ) {
                        return responseBuilder.success(NO_CONTENT.getStatusCode(), null);
index b478dca..9181154 100644 (file)
@@ -215,11 +215,11 @@ public class DR_NodeService extends BaseLoggingClass {
        }
        
        public DR_Node updateDr_Node( DR_Node node, ApiError apiError ) {
-               DR_Node old = dr_nodes.get( node );
+               DR_Node old = dr_nodes.get( node.getFqdn() );
                if ( old == null ) {
                        apiError.setCode(Status.NOT_FOUND.getStatusCode());
                        apiError.setFields( "fqdn");
-                       apiError.setMessage( "Node " + node + " does not exist");
+                       apiError.setMessage( "Node " + node.getFqdn() + " does not exist");
                        return null;
                }
                node.setLastMod();
index 01ef6ae..856a789 100644 (file)
@@ -7,9 +7,9 @@
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  */
 package org.onap.dmaap.dbcapi.resources;
 
-import org.onap.dmaap.dbcapi.model.*;
-import org.onap.dmaap.dbcapi.service.*;
-import org.onap.dmaap.dbcapi.testframework.DmaapObjectFactory;
-
-import static org.junit.Assert.*;
-
-import org.junit.After;
+import org.glassfish.jersey.server.ResourceConfig;
+import org.junit.AfterClass;
 import org.junit.Before;
+import org.junit.BeforeClass;
 import org.junit.Test;
-import java.util.*;
-import java.sql.*;
+import org.onap.dmaap.dbcapi.database.DatabaseClass;
+import org.onap.dmaap.dbcapi.model.ApiError;
+import org.onap.dmaap.dbcapi.model.DR_Node;
+import org.onap.dmaap.dbcapi.testframework.DmaapObjectFactory;
 
-import org.glassfish.jersey.test.JerseyTest;
-import org.glassfish.jersey.server.ResourceConfig;
 import javax.ws.rs.client.Entity;
-import javax.ws.rs.core.Application;
 import javax.ws.rs.core.Response;
-import javax.ws.rs.core.MediaType;
-import javax.ws.rs.Path;
-import javax.ws.rs.GET;
-
-
-public class DR_NodeResourceTest extends JerseyTest {
-
-       static DmaapObjectFactory factory = new DmaapObjectFactory();
-       static String entry_path = "dr_nodes";
-
-       @Override
-       protected Application configure() {
-               return new ResourceConfig( DR_NodeResource.class );
-       }
-
-       private static final String  fmt = "%24s: %s%n";
-
-
-
-/*  may conflict with test framework! 
-       @Before
-       public void preTest() throws Exception {
-       }
-
-       @After
-       public void tearDown() throws Exception {
-       }
-*/
-
-
-       @Test
-       public void GetTest() {
-               Response resp = target( entry_path ).request().get( Response.class );
-               System.out.println( "GET " + entry_path + " resp=" + resp.getStatus() );
-
-               assertTrue( resp.getStatus() == 200 );
-       }
-       @Test
-       public void PostTest() {
-               DR_Node node = factory.genDR_Node( "central" );
-               Entity<DR_Node> reqEntity = Entity.entity( node, MediaType.APPLICATION_JSON );
-               Response resp = target( entry_path ).request().post( reqEntity, Response.class );
-               System.out.println( "POST " + entry_path + " resp=" + resp.getStatus() + " " + resp.readEntity( String.class ) );
-               assertTrue( resp.getStatus() == 200 );
-       }
-
-       @Test
-       public void PutTest() {
-
-/*
-               try {
-                       DcaeLocation loc = factory.genDcaeLocation( "central" );
-                       Entity<DcaeLocation> reqEntity = Entity.entity( loc, MediaType.APPLICATION_JSON );
-                       Response resp = target( "dcaeLocations").request().post( reqEntity, Response.class );
-                       System.out.println( "POST dcaeLocation resp=" + resp.getStatus() + " " + resp.readEntity( String.class ));
-                       assertTrue( resp.getStatus() == 201 );
-               } catch (Exception e ) {
-               }
-*/
-
-               DR_Node node = factory.genDR_Node( "central" );
-               Entity<DR_Node> reqEntity = Entity.entity( node, MediaType.APPLICATION_JSON );
-               Response resp = target( entry_path ).request().post( reqEntity, Response.class );
-
-               // first, add it 
-               System.out.println( "POST " + entry_path + " resp=" + resp.getStatus() + " " + resp.readEntity( String.class ) );
-               assertTrue( resp.getStatus() == 200 );
-
-               // now change a field
-               node.setVersion( "1.0.2" );
-               reqEntity = Entity.entity( node, MediaType.APPLICATION_JSON );
-
-               // update  currently fails...
-               resp = target( entry_path )
-                                       .path( node.getFqdn())
-                                       .request()
-                                       .put( reqEntity, Response.class );
-               System.out.println( "PUT " + entry_path + "/" + node.getFqdn() + " resp=" + resp.getStatus() + " " + resp.readEntity(String.class));
-               assertTrue( resp.getStatus() == 404 );
-
-       }
-
-
 
+import static javax.ws.rs.client.Entity.entity;
+import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+
+public class DR_NodeResourceTest {
+
+    private static final DmaapObjectFactory DMAAP_OBJECT_FACTORY = new DmaapObjectFactory();
+    private static FastJerseyTestContainer testContainer;
+
+    @BeforeClass
+    public static void setUpClass() throws Exception {
+        DatabaseClass.getDmaap().init(DMAAP_OBJECT_FACTORY.genDmaap());
+
+        testContainer = new FastJerseyTestContainer(new ResourceConfig()
+                .register(DR_NodeResource.class));
+        testContainer.init();
+    }
+
+    @AfterClass
+    public static void tearDownClass() throws Exception {
+        testContainer.destroy();
+        /*TODO: Cannot cleanup yet until still other Resources tests depends on the static DB content
+
+        DatabaseClass.clearDatabase();
+        DatabaseClass.getDmaap().remove();*/
+    }
+
+    @Test
+    public void getDr_Nodes_test() {
+        Response response = testContainer.target("dr_nodes").request().get(Response.class);
+        System.out.println("GET dr_subs response=" + response.getStatus());
+
+        assertEquals(200, response.getStatus());
+        assertTrue(response.hasEntity());
+    }
+
+    @Test
+    public void addDr_Node_shouldReturnError_whenNoLocationAndFqdnProvided() {
+        DR_Node node = new DR_Node(null, null, "hostName", "1.0");
+        Entity<DR_Node> requestedEntity = entity(node, APPLICATION_JSON);
+
+        Response response = testContainer.target("dr_nodes")
+                .request()
+                .post(requestedEntity, Response.class);
+
+        assertEquals(400, response.getStatus());
+        ApiError responseError = response.readEntity(ApiError.class);
+        assertNotNull(responseError);
+        assertEquals("dcaeLocation, fqdn", responseError.getFields());
+    }
+
+    @Test
+    public void addDr_Node_shouldReturnError_whenDrNodeWithGiveFqdnAlreadyExists() {
+        DR_Node node = new DR_Node("fqdn", "location", "hostName", "1.0");
+
+        addDrNode(node);
+        Response response = addDrNode(node);
+
+        assertEquals(409, response.getStatus());
+        ApiError responseError = response.readEntity(ApiError.class);
+        assertNotNull(responseError);
+        assertEquals("fqdn", responseError.getFields());
+        assertEquals("Node fqdn already exists", responseError.getMessage());
+    }
+
+    @Test
+    public void addDr_Node_shouldExecuteSuccessfully() {
+        DR_Node node = new DR_Node("fqdn", "location", "hostName", "1.0");
+
+        Response response = addDrNode(node);
+
+        assertEquals(200, response.getStatus());
+        assertTrue(response.hasEntity());
+        assertDrNodeExistInDB(response.readEntity(DR_Node.class));
+    }
+
+    @Test
+    public void updateDr_Node_shouldReturnError_whenNoLocationAndFqdnProvided() {
+        DR_Node node = new DR_Node(null, null, "hostName", "1.0");
+        Entity<DR_Node> requestedEntity = entity(node, APPLICATION_JSON);
+
+        Response response = testContainer.target("dr_nodes")
+                .path("fqdn")
+                .request()
+                .put(requestedEntity, Response.class);
+
+        assertEquals(400, response.getStatus());
+        ApiError responseError = response.readEntity(ApiError.class);
+        assertNotNull(responseError);
+        assertEquals("dcaeLocation, fqdn", responseError.getFields());
+    }
+
+    @Test
+    public void updateDr_Node_shouldReturnError_whenNoExistingFqdnProvided() {
+        DR_Node node = new DR_Node("fqdn", "location", "hostName", "1.0");
+        Entity<DR_Node> requestedEntity = entity(node, APPLICATION_JSON);
+
+        Response response = testContainer.target("dr_nodes")
+                .path("")
+                .request()
+                .put(requestedEntity, Response.class);
+
+        assertEquals(405, response.getStatus());
+    }
+
+    @Test
+    public void updateDr_Node_shouldReturnError_whenDrNodeForUpdateDoesNotExistInDb() {
+        DR_Node node = new DR_Node("fqdn", "location", "hostName", "1.0");
+        Entity<DR_Node> requestedEntity = entity(node, APPLICATION_JSON);
+
+        Response response = testContainer.target("dr_nodes")
+                .path(node.getFqdn())
+                .request()
+                .put(requestedEntity, Response.class);
+
+        assertEquals(404, response.getStatus());
+        ApiError responseError = response.readEntity(ApiError.class);
+        assertNotNull(responseError);
+        assertEquals("fqdn", responseError.getFields());
+        assertEquals("Node " + node.getFqdn() + " does not exist", responseError.getMessage());
+    }
+
+    @Test
+    public void updateDr_Node_ShouldExecuteSuccessfully() {
+        DR_Node toUpdate = new DR_Node("fqdn", "location", "hostName", "1.0");
+        Entity<DR_Node> requestedEntity = entity(toUpdate, APPLICATION_JSON);
+
+        addDrNode(new DR_Node("fqdn", "old_location", "old_hostName", "old_1.0"));
+        Response response = testContainer.target("dr_nodes")
+                .path(toUpdate.getFqdn())
+                .request()
+                .put(requestedEntity, Response.class);
+
+        assertEquals(200, response.getStatus());
+        assertTrue(response.hasEntity());
+        assertEquals(toUpdate, response.readEntity(DR_Node.class));
+    }
+
+    @Test
+    public void deleteDr_Node_shouldReturnError_whenDrNodeForDeleteDoesNotExistInDb() {
+        Response response = testContainer.target("dr_nodes")
+                .path("fqdn")
+                .request()
+                .delete();
+
+        assertEquals(404, response.getStatus());
+        ApiError responseError = response.readEntity(ApiError.class);
+        assertNotNull(responseError);
+        assertEquals("fqdn", responseError.getFields());
+        assertEquals("Node fqdn does not exist", responseError.getMessage());
+    }
+
+    @Test
+    public void deleteDr_Node_shouldReturnError_whenNoExistingFqdnProvided() {
+        Response response = testContainer.target("dr_nodes")
+                .path("")
+                .request()
+                .delete();
+
+        assertEquals(405, response.getStatus());
+    }
+
+    @Test
+    public void deleteDr_Node_shouldExecuteSuccessfully() {
+        DR_Node node = new DR_Node("fqdn", "location", "hostName", "1.0");
+
+        addDrNode(node);
+        Response response = testContainer.target("dr_nodes")
+                .path("fqdn")
+                .request()
+                .delete();
+
+        assertEquals(204, response.getStatus());
+    }
+
+    @Test
+    public void getDr_Node_shouldReturnError_whenDrNodeForDeleteDoesNotExistInDb() {
+        Response response = testContainer.target("dr_nodes")
+                .path("fqdn")
+                .request()
+                .get();
+
+        assertEquals(404, response.getStatus());
+        ApiError responseError = response.readEntity(ApiError.class);
+        assertNotNull(responseError);
+        assertEquals("fqdn", responseError.getFields());
+        assertEquals("Node fqdn does not exist", responseError.getMessage());
+    }
+
+    private Response addDrNode(DR_Node node) {
+        return testContainer.target("dr_nodes")
+                .request()
+                .post(entity(node, APPLICATION_JSON), Response.class);
+    }
+
+    private void assertDrNodeExistInDB(DR_Node created) {
+        Response response = testContainer.target("dr_nodes")
+                .path(created.getFqdn())
+                .request()
+                .get();
+        assertEquals(200, response.getStatus());
+        assertTrue(response.hasEntity());
+        assertEquals(created, response.readEntity(DR_Node.class));
+    }
+
+    @Before
+    public void cleanupDatabase() {
+        DatabaseClass.clearDatabase();
+    }
 
 }
 
index f9513a6..f812b3d 100644 (file)
@@ -25,11 +25,9 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
 import javax.ws.rs.client.Entity;
-import javax.ws.rs.core.Application;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 import org.glassfish.jersey.server.ResourceConfig;
-import org.glassfish.jersey.test.JerseyTest;
 import org.junit.AfterClass;
 import org.junit.Before;
 import org.junit.BeforeClass;
@@ -51,18 +49,16 @@ public class DR_SubResourceTest {
     private static final String LOG_URL = "https://dr-prov/sublog/id";
     private static final String DELIVERY_URL_TEMPLATE = "https://subscriber.onap.org/delivery/";
     private static final String LOG_URL_TEMPLATE = "https://dr-prov/sublog/";
-    private static FastJerseyTest testContainer;
+    private static FastJerseyTestContainer testContainer;
 
     @BeforeClass
     public static void setUpClass() throws Exception {
         //TODO: init is still needed here to assure that dmaap is not null
         DatabaseClass.getDmaap().init(DMAAP_OBJECT_FACTORY.genDmaap());
-        DatabaseClass.getDmaap().update(DMAAP_OBJECT_FACTORY.genDmaap());
 
-        testContainer = new FastJerseyTest(new ResourceConfig()
+        testContainer = new FastJerseyTestContainer(new ResourceConfig()
             .register(DR_SubResource.class)
-            .register(FeedResource.class)
-            .register(DmaapResource.class));
+            .register(FeedResource.class));
         testContainer.init();
     }
 
@@ -76,7 +72,7 @@ public class DR_SubResourceTest {
     }
 
     @Before
-    public void cleanupDatabase() throws Exception {
+    public void cleanupDatabase() {
         DatabaseClass.clearDatabase();
     }
 
@@ -86,7 +82,7 @@ public class DR_SubResourceTest {
         Response resp = testContainer.target("dr_subs").request().get(Response.class);
         System.out.println("GET dr_subs resp=" + resp.getStatus());
 
-        assertTrue(resp.getStatus() == 200);
+        assertEquals(200, resp.getStatus());
         assertTrue(resp.hasEntity());
     }
 
@@ -412,7 +408,7 @@ public class DR_SubResourceTest {
         Entity<DR_Sub> reqEntity2 = Entity.entity(dr_sub, MediaType.APPLICATION_JSON);
         Response resp = testContainer.target("dr_subs").request().post(reqEntity2, Response.class);
         System.out.println("POST dr_subs resp=" + resp.getStatus());
-        assertTrue(resp.getStatus() == 201);
+        assertEquals(201, resp.getStatus());
         dr_sub = resp.readEntity(DR_Sub.class);
 
         return dr_sub;
@@ -442,22 +438,6 @@ public class DR_SubResourceTest {
         assertTrue(response.hasEntity());
         assertEquals(sub, response.readEntity(DR_Sub.class));
     }
-
-    //TODO: move it outside class and use in other Resource integration tests
-    private static class FastJerseyTest extends JerseyTest {
-
-        FastJerseyTest(Application jaxrsApplication) {
-            super(jaxrsApplication);
-        }
-
-        void init() throws Exception {
-            this.setUp();
-        }
-
-        void destroy() throws Exception {
-            this.tearDown();
-        }
-    }
 }
 
 
diff --git a/src/test/java/org/onap/dmaap/dbcapi/resources/FastJerseyTestContainer.java b/src/test/java/org/onap/dmaap/dbcapi/resources/FastJerseyTestContainer.java
new file mode 100644 (file)
index 0000000..8d38a9f
--- /dev/null
@@ -0,0 +1,39 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * org.onap.dmaap
+ * ================================================================================
+ * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+package org.onap.dmaap.dbcapi.resources;
+
+import org.glassfish.jersey.test.JerseyTest;
+
+import javax.ws.rs.core.Application;
+
+class FastJerseyTestContainer extends JerseyTest {
+
+    FastJerseyTestContainer(Application jaxrsApplication) {
+        super(jaxrsApplication);
+    }
+
+    void init() throws Exception {
+        this.setUp();
+    }
+
+    void destroy() throws Exception {
+        this.tearDown();
+    }
+}
\ No newline at end of file