changes in DR_PubResourceTest
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / resources / DR_PubResource.java
index bcdb4b3..6e652a8 100644 (file)
@@ -46,7 +46,6 @@ 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.Feed;
-import org.onap.dmaap.dbcapi.service.ApiService;
 import org.onap.dmaap.dbcapi.service.DR_PubService;
 import org.onap.dmaap.dbcapi.service.FeedService;
 
@@ -58,7 +57,9 @@ import org.onap.dmaap.dbcapi.service.FeedService;
 @Authorization
 public class DR_PubResource extends BaseLoggingClass {
 
-       DR_PubService dr_pubService = new DR_PubService();
+       private DR_PubService dr_pubService = new DR_PubService();
+       private ResponseBuilder responseBuilder = new ResponseBuilder();
+       private RequiredChecker checker = new RequiredChecker();
        
        @GET
        @ApiOperation( value = "return DR_Pub details", 
@@ -69,14 +70,12 @@ public class DR_PubResource extends BaseLoggingClass {
            @ApiResponse( code = 400, message = "Error", response = ApiError.class )
        })
        public  Response getDr_Pubs() {
-               ApiService resp = new ApiService();
-
                logger.info( "Entry: GET /dr_pubs");
                List<DR_Pub> pubs = dr_pubService.getAllDr_Pubs();
 
                GenericEntity<List<DR_Pub>> list = new GenericEntity<List<DR_Pub>>(pubs) {
         };
-        return resp.success(list);
+        return responseBuilder.success(list);
        }
        
        @POST
@@ -87,26 +86,46 @@ public class DR_PubResource extends BaseLoggingClass {
            @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
            @ApiResponse( code = 400, message = "Error", response = ApiError.class )
        })
-       public Response addDr_Pub( 
-                       DR_Pub pub
-                       ) {
-               ApiService resp = new ApiService();
+       public Response addDr_Pub(DR_Pub pub) {
+               ApiError apiError = new ApiError();
+               FeedService feeds = new FeedService();
+               Feed fnew = null;
 
                logger.info( "Entry: POST /dr_pubs");
 
                try {
-                       resp.required( "feedId", pub.getFeedId(), "");
-                       resp.required( "dcaeLocationName", pub.getDcaeLocationName(), "");
+                       checker.required( "feedId", pub.getFeedId());
+               } catch ( RequiredFieldException rfe ) {
+                       try {
+                               checker.required( "feedName", pub.getFeedName());
+                       }catch ( RequiredFieldException rfe2 ) {
+                               logger.debug( rfe2.getApiError().toString() );
+                               return responseBuilder.error(rfe2.getApiError());
+                       }
+                       // if we found a FeedName instead of a FeedId then try to look it up.
+                       List<Feed> nfeeds =  feeds.getAllFeeds( pub.getFeedName(), pub.getFeedVersion(), "equals");
+                       if ( nfeeds.isEmpty() ) {
+                               apiError.setCode(Status.NOT_FOUND.getStatusCode());
+                               apiError.setFields("feedName");
+                               return responseBuilder.error(apiError);
+                       }
+                       fnew = nfeeds.get(0);
+               }
+               try {
+                       checker.required( "dcaeLocationName", pub.getDcaeLocationName());
                } catch ( RequiredFieldException rfe ) {
-                       logger.debug( resp.getErr().toString() );
-                       return resp.error();    
+                       logger.debug( rfe.getApiError().toString() );
+                       return responseBuilder.error(rfe.getApiError());
                }
 
-               FeedService feeds = new FeedService();
-               Feed fnew = feeds.getFeed( pub.getFeedId(), resp.getErr() );
+
+               // we may have fnew already if located by FeedName
                if ( fnew == null ) {
-                       logger.info( "Specified feed " + pub.getFeedId() + " not known to Bus Controller");     
-                       return resp.error();    
+                       fnew = feeds.getFeed(pub.getFeedId(), apiError);
+               }
+               if ( fnew == null ) {
+                       logger.info( "Specified feed " + pub.getFeedId() + " or " + pub.getFeedName() + " not known to Bus Controller");        
+                       return responseBuilder.error(apiError);
                }
 
                ArrayList<DR_Pub> pubs = fnew.getPubs();
@@ -122,16 +141,16 @@ public class DR_PubResource extends BaseLoggingClass {
                }
                pubs.add( pub );
                fnew.setPubs(pubs);
-               fnew = feeds.updateFeed( fnew, resp.getErr() ); 
+               fnew = feeds.updateFeed(fnew, apiError);
                
-               if ( ! resp.getErr().is2xx()) { 
-                       return resp.error();                    
+               if (!apiError.is2xx()) {
+                       return responseBuilder.error(apiError);
                }
                pubs = fnew.getPubs();
                logger.info( "num existing pubs after = " + pubs.size() );
                
-               DR_Pub pnew = dr_pubService.getDr_Pub(pub.getPubId(), resp.getErr());
-               return resp.success(Status.CREATED.getStatusCode(), pnew);
+               DR_Pub pnew = dr_pubService.getDr_Pub(pub.getPubId(), apiError);
+               return responseBuilder.success(Status.CREATED.getStatusCode(), pnew);
        }
        
        @PUT
@@ -143,16 +162,11 @@ public class DR_PubResource extends BaseLoggingClass {
            @ApiResponse( code = 400, message = "Error", response = ApiError.class )
        })
        @Path("/{pubId}")
-       public Response updateDr_Pub( 
-                       @PathParam("pubId") String name, 
-                       DR_Pub pub
-                       ) {
-               ApiService resp = new ApiService();
-
+       public Response updateDr_Pub(@PathParam("pubId") String name, DR_Pub pub) {
                logger.info( "Entry: PUT /dr_pubs");
                pub.setPubId(name);
                DR_Pub res = dr_pubService.updateDr_Pub(pub);
-               return resp.success(res);
+               return responseBuilder.success(res);
        }
        
        @DELETE
@@ -164,33 +178,31 @@ public class DR_PubResource extends BaseLoggingClass {
            @ApiResponse( code = 400, message = "Error", response = ApiError.class )
        })
        @Path("/{pubId}")
-       public Response deleteDr_Pub( 
-                       @PathParam("pubId") String id
-                       ){
+       public Response deleteDr_Pub(@PathParam("pubId") String id){
 
-               ApiService resp = new ApiService();
+               ApiError apiError = new ApiError();
 
                try {
-                       resp.required( "pubId", id, "");
+                       checker.required( "pubId", id);
                } catch ( RequiredFieldException rfe ) {
-                       return resp.error();
+                       return responseBuilder.error(rfe.getApiError());
                }
 
-               DR_Pub pub =  dr_pubService.getDr_Pub( id, resp.getErr() );
-               if ( ! resp.getErr().is2xx()) { 
-                       return resp.error();                                    
+               DR_Pub pub =  dr_pubService.getDr_Pub(id, apiError);
+               if ( !apiError.is2xx()) {
+                       return responseBuilder.error(apiError);
                }
                FeedService feeds = new FeedService();
-               Feed fnew = feeds.getFeed( pub.getFeedId(), resp.getErr() );
+               Feed fnew = feeds.getFeed(pub.getFeedId(), apiError);
                if ( fnew == null ) {
                        logger.info( "Specified feed " + pub.getFeedId() + " not known to Bus Controller");     
-                       return resp.error();
+                       return responseBuilder.error(apiError);
                }
                ArrayList<DR_Pub> pubs = fnew.getPubs();
                if ( pubs.size() == 1 ) {
-                       resp.setCode(Status.BAD_REQUEST.getStatusCode());
-                       resp.setMessage( "Can't delete the last publisher of a feed");
-                       return resp.error();    
+                       apiError.setCode(Status.BAD_REQUEST.getStatusCode());
+                       apiError.setMessage( "Can't delete the last publisher of a feed");
+                       return responseBuilder.error(apiError);
                }
                
                for( Iterator<DR_Pub> i = pubs.iterator(); i.hasNext(); ) {
@@ -200,16 +212,16 @@ public class DR_PubResource extends BaseLoggingClass {
                        }
                }
                fnew.setPubs(pubs);
-               fnew = feeds.updateFeed( fnew, resp.getErr() );
-               if ( ! resp.getErr().is2xx()) { 
-                       return resp.error();                    
+               fnew = feeds.updateFeed(fnew,apiError);
+               if (!apiError.is2xx()) {
+                       return responseBuilder.error(apiError);
                }
                
-               dr_pubService.removeDr_Pub(id, resp.getErr() );
-               if ( ! resp.getErr().is2xx()) { 
-                       return resp.error();            
+               dr_pubService.removeDr_Pub(id, apiError);
+               if (!apiError.is2xx()) {
+                       return responseBuilder.error(apiError);
                }
-               return resp.success(Status.NO_CONTENT.getStatusCode(), null);
+               return responseBuilder.success(Status.NO_CONTENT.getStatusCode(), null);
        }
 
        @GET
@@ -221,21 +233,19 @@ public class DR_PubResource extends BaseLoggingClass {
            @ApiResponse( code = 400, message = "Error", response = ApiError.class )
        })
        @Path("/{pubId}")
-       public Response get( 
-                       @PathParam("pubId") String id
-                       ) {
-               ApiService resp = new ApiService();
+       public Response get(@PathParam("pubId") String id) {
+               ApiError apiError = new ApiError();
 
                try {
-                       resp.required( "feedId", id, "");
+                       checker.required( "feedId", id);
                } catch ( RequiredFieldException rfe ) {
-                       return resp.error();    
+                       return responseBuilder.error(rfe.getApiError());
                }
 
-               DR_Pub pub =  dr_pubService.getDr_Pub( id, resp.getErr() );
-               if ( ! resp.getErr().is2xx()) { 
-                       resp.getErr();                  
+               DR_Pub pub =  dr_pubService.getDr_Pub(id, apiError);
+               if (!apiError.is2xx()) {
+                       return responseBuilder.error(apiError);
                }
-               return resp.success(Status.OK.getStatusCode(), pub);
+               return responseBuilder.success(Status.OK.getStatusCode(), pub);
        }
 }