DirectoryResource extends AbstractJaxRSResource { @PUT @Path( "{pathId : .+}" ) @Consumes( { WILDCARD } ) @StatusCodes( { @ResponseCode( code = 200, condition = "Successfully created folder." ), @ResponseCode( code = 403, condition = "Forbidden." ), @ResponseCode( code = 409, condition = "Path already exists." ), @ResponseCode( code = 500, condition = "Server Error." ) } ) public Response createDirs( @PathParam ( "pathId" ) String pathId ) { try { if ( FileUtils.isRootLevelPath( FileUtils.idToPath( pathId ) ) ) { return Response.status( Response.Status.FORBIDDEN ).entity( "couldNotCreateRootLevelFolder" ).build(); } if ( fileService.doCreateDirSafe( pathId ) ) { return Response.ok().build(); } else { return Response.status( Response.Status.CONFLICT ).entity( "couldNotCreateFolderDuplicate" ).build(); } } catch ( FileService.InvalidNameException e ) { return Response.status( Response.Status.FORBIDDEN ).entity( "containsIllegalCharacters" ).build(); } catch ( Throwable t ) { return Response.serverError().entity( "" ).build(); } } DirectoryResource();  DirectoryResource( HttpServletResponse httpServletResponse ); @PUT @Path( "{pathId : .+}" ) @Consumes( { WILDCARD } ) @StatusCodes( { @ResponseCode( code = 200, condition = "Successfully created folder." ), @ResponseCode( code = 403, condition = "Forbidden." ), @ResponseCode( code = 409, condition = "Path already exists." ), @ResponseCode( code = 500, condition = "Server Error." ) } ) Response createDirs( @PathParam ( "pathId" ) String pathId ); @GET @Path ( "{pathId : .+}/isVisible" ) @Consumes( { WILDCARD } ) @Produces( MediaType.TEXT_PLAIN ) @StatusCodes ( { @ResponseCode ( code = 200, condition = "Successfully returns a boolean value, either true or false" ) } ) Response isDirVisible( @PathParam ( "pathId" ) String pathId ); @GET @Path ( "{pathId : .+}/defaultLocation" ) @Produces( MediaType.TEXT_PLAIN ) @StatusCodes ( { @ResponseCode ( code = 200, condition = "Successfully returns a default open/save location" ) } ) Response getDefaultLocation( @PathParam ( "pathId" ) String pathId );  }
@Test public void testCreateDirs_Ok() throws Exception { doReturn( true ).when( directoryResource.fileService ).doCreateDirSafe( PATH ); Response testResponse = directoryResource.createDirs( PATH ); assertEquals( Response.Status.OK.getStatusCode(), testResponse.getStatus() ); verify( directoryResource.fileService, times( 1 ) ).doCreateDirSafe( PATH ); }
@Test public void testCreateDirs_Conflict() throws Exception { doReturn( false ).when( directoryResource.fileService ).doCreateDirSafe( PATH ); Response testResponse = directoryResource.createDirs( PATH ); assertEquals( Response.Status.CONFLICT.getStatusCode(), testResponse.getStatus() ); verify( directoryResource.fileService, times( 1 ) ).doCreateDirSafe( PATH ); }
@Test public void testCreateDirs_ServerError_0() throws Exception { Response testResponse = directoryResource.createDirs( null ); assertEquals( Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), testResponse.getStatus() ); verify( directoryResource.fileService, times( 0 ) ).doCreateDirSafe( anyString() ); }
@Test public void testCreateDirs_ServerError_1() throws Exception { doThrow( new RuntimeException() ).when( directoryResource.fileService ).doCreateDirSafe( PATH ); Response testResponse = directoryResource.createDirs( PATH ); assertEquals( Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), testResponse.getStatus() ); verify( directoryResource.fileService, times( 1 ) ).doCreateDirSafe( PATH ); }
@Test public void testCreateDirs_Forbidden() throws Exception { doReturn( false ).when( directoryResource.fileService ).doCreateDirSafe( anyString() ); Response testResponse = directoryResource.createDirs( ROOTLEVEL_PATH ); assertEquals( Response.Status.FORBIDDEN.getStatusCode(), testResponse.getStatus() ); verify( directoryResource.fileService, times( 0 ) ).doCreateDirSafe( anyString() ); }
@Test public void testCreateDirs_Forbidden_ControlCharactersFound() { doReturn( new StringBuffer() ).when( directoryResource.fileService ).doGetReservedChars(); Response testResponse = directoryResource.createDirs( PATH_CONTROL_CHARACTER ); assertEquals( Response.Status.FORBIDDEN.getStatusCode(), testResponse.getStatus() ); assertEquals( testResponse.getEntity(), "containsIllegalCharacters" ); }
@Test public void testCreateDirs_Special_Characters() throws Exception { doReturn( true ).when( directoryResource.fileService ).doCreateDirSafe( PATH_SPECIAL_CHARACTERS ); Response testResponse = directoryResource.createDirs( PATH_SPECIAL_CHARACTERS ); assertEquals( Response.Status.OK.getStatusCode(), testResponse.getStatus() ); verify( directoryResource.fileService, times( 1 ) ).doCreateDirSafe( PATH_SPECIAL_CHARACTERS ); }
@Test public void testCreateDirs_Forbidden_Japanese_Characters() throws Exception { doReturn( true ).when( directoryResource.fileService ).doCreateDirSafe( PATH_JAPANESE_CHARACTERS ); Response testResponse = directoryResource.createDirs( PATH_JAPANESE_CHARACTERS ); assertEquals( Response.Status.OK.getStatusCode(), testResponse.getStatus() ); verify( directoryResource.fileService, times( 1 ) ).doCreateDirSafe( PATH_JAPANESE_CHARACTERS ); }