Refactor tests for TopicResource class
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / resources / TopicResource.java
index 95f9d33..01926b7 100644 (file)
@@ -35,6 +35,7 @@ import javax.ws.rs.PUT;
 import javax.ws.rs.Path;
 import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
 import javax.ws.rs.core.GenericEntity;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
@@ -42,14 +43,14 @@ import javax.ws.rs.core.Response.Status;
 
 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
 import org.onap.dmaap.dbcapi.model.ApiError;
-import org.onap.dmaap.dbcapi.model.DR_Pub;
 import org.onap.dmaap.dbcapi.model.ReplicationType;
 import org.onap.dmaap.dbcapi.model.FqtnType;
 import org.onap.dmaap.dbcapi.model.Topic;
-import org.onap.dmaap.dbcapi.service.ApiService;
 import org.onap.dmaap.dbcapi.service.TopicService;
 import org.onap.dmaap.dbcapi.util.DmaapConfig;
 
+import static javax.ws.rs.core.Response.Status.CREATED;
+
 @Path("/topics")
 @Api( value= "topics", description = "Endpoint for retreiving MR Topics" )
 @Consumes(MediaType.APPLICATION_JSON)
@@ -57,11 +58,19 @@ import org.onap.dmaap.dbcapi.util.DmaapConfig;
 @Authorization
 public class TopicResource extends BaseLoggingClass {
        private static FqtnType defaultTopicStyle;
-       TopicService mr_topicService = new TopicService();
+       private static String defaultPartitionCount;
+       private static String defaultReplicationCount;
+       private TopicService mr_topicService = new TopicService();
+       private ResponseBuilder responseBuilder = new ResponseBuilder();
+       private RequiredChecker checker = new RequiredChecker();
+       static final String UNSUPPORTED_PUT_MSG = "Method /PUT not supported for /topics";
        
        public TopicResource() {
                DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
-               defaultTopicStyle = FqtnType.Validator( p.getProperty("MR.topicStyle", "FQTN_LEGACY_FORMAT"));
+               defaultTopicStyle = FqtnType.Validator( p.getProperty("MR.topicStyle", "FQTN_LEGACY_FORMAT"));
+               defaultPartitionCount = p.getProperty( "MR.partitionCount", "2");
+               defaultReplicationCount = p.getProperty( "MR.replicationCount", "1");
+               
                logger.info( "Setting defaultTopicStyle=" + defaultTopicStyle );
        }
                
@@ -70,44 +79,42 @@ public class TopicResource extends BaseLoggingClass {
        notes = "Returns array of  `Topic` objects.", 
        response = Topic.class)
        @ApiResponses( value = {
-           @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
+           @ApiResponse( code = 200, message = "Success", response = Topic.class),
            @ApiResponse( code = 400, message = "Error", response = ApiError.class )
        })
        public Response getTopics() {
-
-               ApiService check = new ApiService();
-
                List<Topic> allTopics = mr_topicService.getAllTopics();
                
                GenericEntity<List<Topic>> list = new GenericEntity<List<Topic>>(allTopics) {
                        };
-               return check.success(list);
+               return responseBuilder.success(list);
                
        }
                
        @POST
-       @ApiOperation( value = "return Topic details", 
-       notes = "Create  `Topic` object.", 
+       @ApiOperation( value = "Create a Topic object", 
+       notes = "Create  `Topic` object."
+                       + "For convenience, the message body may populate the `clients` array, in which case each entry will be added as an `MR_Client`."
+                       + "  Beginning in ONAP Dublin Release, dbcapi will create two AAF Roles by default, one each for the publisher and subscriber per topic."
+                       + "  MR_Clients can then specify an AAF Identity to be added to the appropriate default Role, avoiding the need to create Role(s) in advance.", 
        response = Topic.class)
        @ApiResponses( value = {
-           @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
+           @ApiResponse( code = 200, message = "Success", response = Topic.class),
            @ApiResponse( code = 400, message = "Error", response = ApiError.class )
        })
-       public Response  addTopic( 
-                       Topic topic
-                       ) {
-               logger.info( "addTopic request: " + String.valueOf(topic) );
-               ApiService check = new ApiService();
+       public Response  addTopic(Topic topic, @QueryParam("useExisting") String useExisting) {
+               logger.info( "addTopic request: " + topic  + " useExisting=" + useExisting );
+               ApiError apiError = new ApiError();
 
                try {
-                       check.required( "topicName", topic.getTopicName(), "^\\S+$" );  //no white space allowed in topicName
-                       check.required( "topicDescription", topic.getTopicDescription(), "" );
-                       check.required( "owner", topic.getOwner(), "" );
+                       checker.required( "topicName", topic.getTopicName(), "^\\S+$" );  //no white space allowed in topicName
+                       checker.required( "topicDescription", topic.getTopicDescription());
+                       checker.required( "owner", topic.getOwner());
                } catch( RequiredFieldException rfe ) {
-                       return check.error();
+                       logger.error("Error", rfe.getApiError());
+                       return responseBuilder.error(rfe.getApiError());
                }
                
-               //String repReq = topic.getReplicationRequest();
                ReplicationType t = topic.getReplicationCase();
                if ( t == null || t == ReplicationType.REPLICATION_NOT_SPECIFIED ) {
                        topic.setReplicationCase( mr_topicService.reviewTopic(topic));
@@ -117,13 +124,25 @@ public class TopicResource extends BaseLoggingClass {
                        logger.info( "setting defaultTopicStyle=" + defaultTopicStyle + " for topic " + topic.getTopicName() );
                        topic.setFqtnStyle( defaultTopicStyle );
                }
+               String pc = topic.getPartitionCount();
+               if ( pc == null ) {
+                       topic.setPartitionCount(defaultPartitionCount);
+               }
+               String rc = topic.getReplicationCount();
+               if ( rc == null ) {
+                       topic.setReplicationCount(defaultReplicationCount);
+               }
                topic.setLastMod();
+               Boolean flag = false;
+               if (useExisting != null) {
+                       flag = "true".compareToIgnoreCase( useExisting ) == 0;
+               }
                
-               Topic mrc =  mr_topicService.addTopic(topic, check.getErr());
-               if ( mrc != null && check.getErr().is2xx() ) {
-                       return check.success(Status.CREATED.getStatusCode(), mrc);
+               Topic mrc =  mr_topicService.addTopic(topic, apiError, flag);
+               if ( mrc != null && apiError.is2xx() ) {
+                       return responseBuilder.success(CREATED.getStatusCode(), mrc);
                }
-               return check.error();
+               return responseBuilder.error(apiError);
        }
        
        @PUT
@@ -131,19 +150,17 @@ public class TopicResource extends BaseLoggingClass {
        notes = "Update a  `Topic` object, identified by topicId", 
        response = Topic.class)
        @ApiResponses( value = {
-           @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
+           @ApiResponse( code = 200, message = "Success", response = Topic.class),
            @ApiResponse( code = 400, message = "Error", response = ApiError.class )
        })
        @Path("/{topicId}")
-       public Response updateTopic( 
-                       @PathParam("topicId") String topicId
-                       ) {
-               ApiService check = new ApiService();
+       public Response updateTopic(@PathParam("topicId") String topicId) {
+               ApiError apiError = new ApiError();
 
-               check.setCode(Status.BAD_REQUEST.getStatusCode());
-               check.setMessage( "Method /PUT not supported for /topics");
+               apiError.setCode(Status.BAD_REQUEST.getStatusCode());
+               apiError.setMessage(UNSUPPORTED_PUT_MSG);
                
-               return check.error();
+               return responseBuilder.error(apiError);
        }
                
        @DELETE
@@ -151,26 +168,25 @@ public class TopicResource extends BaseLoggingClass {
        notes = "Delete a  `Topic` object, identified by topicId", 
        response = Topic.class)
        @ApiResponses( value = {
-           @ApiResponse( code = 204, message = "Success", response = DR_Pub.class),
+           @ApiResponse( code = 204, message = "Success", response = Topic.class),
            @ApiResponse( code = 400, message = "Error", response = ApiError.class )
        })
        @Path("/{topicId}")
-       public Response deleteTopic( 
-                       @PathParam("topicId") String id
-                       ){
-               ApiService check = new ApiService();
+       public Response deleteTopic(@PathParam("topicId") String id){
+               ApiError apiError = new ApiError();
 
                try {
-                       check.required( "fqtn", id, "" );
+                       checker.required( "fqtn", id);
                } catch( RequiredFieldException rfe ) {
-                       return check.error();
+                       logger.error("Error", rfe.getApiError());
+                       return responseBuilder.error(rfe.getApiError());
                }
                
-               mr_topicService.removeTopic(id, check.getErr());
-               if ( check.getErr().is2xx()) {
-                       return check.success(Status.NO_CONTENT.getStatusCode(), null);
+               mr_topicService.removeTopic(id, apiError);
+               if (apiError.is2xx()) {
+                       return responseBuilder.success(Status.NO_CONTENT.getStatusCode(), null);
                } 
-               return check.error();
+               return responseBuilder.error(apiError);
        }
        
 
@@ -179,25 +195,24 @@ public class TopicResource extends BaseLoggingClass {
        notes = "Retrieve a  `Topic` object, identified by topicId", 
        response = Topic.class)
        @ApiResponses( value = {
-           @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
+           @ApiResponse( code = 200, message = "Success", response = Topic.class),
            @ApiResponse( code = 400, message = "Error", response = ApiError.class )
        })
        @Path("/{topicId}")
-       public Response getTopic( 
-                       @PathParam("topicId") String id
-                       ) {
+       public Response getTopic(@PathParam("topicId") String id) {
                logger.info("Entry: /GET " + id);
-               ApiService check = new ApiService();
+               ApiError apiError = new ApiError();
 
                try {
-                       check.required( "topicName", id, "^\\S+$" );  //no white space allowed in topicName
+                       checker.required( "topicName", id, "^\\S+$" );  //no white space allowed in topicName
                } catch( RequiredFieldException rfe ) {
-                       return check.error();
+                       logger.error("Error", rfe.getApiError());
+                       return responseBuilder.error(rfe.getApiError());
                }
-               Topic mrc =  mr_topicService.getTopic( id, check.getErr() );
+               Topic mrc =  mr_topicService.getTopic(id, apiError);
                if ( mrc == null ) {
-                       return check.error();
+                       return responseBuilder.error(apiError);
                }
-               return check.success(mrc);
+               return responseBuilder.success(mrc);
                }
 }