HMSManagementRestService { @GET @Path( "/hmslogs" ) @Produces( MediaType.APPLICATION_OCTET_STREAM ) public Response getHmsLogs() throws HMSRestException { String tempDir = HmsConfigHolder.getHMSConfigProperty( HmsConfigHolder.HMS_TEMPORARY_LOG_DIR ); File tempDirHandle = new File( tempDir ); if ( !tempDirHandle.exists() && !tempDirHandle.isDirectory() ) { if ( !FileUtil.createDirectory( tempDir ) ) { logger.error( "Unable to create directory for creating temporary OOB logs" ); ResponseBuilder response = Response.status( Status.INTERNAL_SERVER_ERROR ); return response.build(); } } String tempZipFileName = System.currentTimeMillis() + "-hmsLog.zip"; String outputZipFile = tempDir + tempZipFileName; String hmslogPath = HmsConfigHolder.getHMSConfigProperty( HmsConfigHolder.HMS_LOG_FILE_PATH ); String hmslog1Path = HmsConfigHolder.getHMSConfigProperty( HmsConfigHolder.HMS_LOG_1_FILE_PATH ); if ( hmslogPath == null || hmslogPath.isEmpty() ) { ResponseBuilder response = Response.status( Status.NOT_FOUND ); return response.build(); } logger.debug( "Hms Log file path:" + hmslogPath ); logger.debug( "Hms Log 1 file path:" + hmslog1Path ); boolean zipForLogCreated = ZipUtil.zipFiles( outputZipFile, hmslogPath, hmslog1Path ); if ( zipForLogCreated ) { File hmsLog = new File( outputZipFile ); ResponseBuilder response = Response.ok( hmsLog ); response.header( "Content-Disposition", "attachment; filename=\"" + "hmslog.zip" + "\"" ); return response.build(); } else { ResponseBuilder response = Response.status( Status.INTERNAL_SERVER_ERROR ); return response.build(); } }  @GET @Path( "/nodes" ) @Produces( "application/json" ) Map<String, Object[]> getHMSNodes(); @GET @Path( "/discover" ) @Produces( "application/json" ) NodeDiscoveryResponse discoverNodes(); @POST @Path( "/handshake" ) @Consumes( MediaType.APPLICATION_JSON ) @Produces( "application/json" ) BaseResponse handshake(); @POST @Path( "/handshake/{aggregator_ip}/{source}" ) @Consumes( MediaType.APPLICATION_JSON ) @Produces( "application/json" ) BaseResponse handshake( @PathParam( "aggregator_ip" ) String aggregatorIp,
                                   @PathParam( "source" ) String source ); @GET @Path( "/service/operations" ) @Produces( "application/json" ) Map<String, List<HmsApi>> getSupportedOOBOperations(); @GET @Path( "/debug/threads" ) @Produces( "application/json" ) Map<String, String> executeThreadStackLogger(); @PUT @Path( "/refreshinventory" ) @Produces( MediaType.APPLICATION_JSON ) @Deprecated Response refreshInventory( final String inventory ); @GET @Path( "/hmslogs" ) @Produces( MediaType.APPLICATION_OCTET_STREAM ) Response getHmsLogs(); @DELETE @Path( "/hmslogs" ) @Produces( "application/json" ) BaseResponse deleteTemporaryHmsLogFile(); boolean isFileDeleteable( File logArchiveName ); @GET @Path( "/newhosts" ) @Produces( MediaType.APPLICATION_JSON ) Response getNewHosts(); static final String GRANT_EXECUTE_RIGHTS; final String TIMESTAMP_FORMAT; }
@Test public void getHMSLogsTest_fileNotExists() throws Exception { File fileMock = mock( File.class ); whenNew( File.class ).withArguments( anyString() ).thenReturn( fileMock ); when( fileMock.canWrite() ).thenReturn( true ); when( fileMock.getParentFile() ).thenReturn( fileMock ); when( fileMock.getParentFile().exists() ).thenReturn( true ); when( fileMock.getParentFile().mkdirs() ).thenReturn( true ); when( fileMock.createNewFile() ).thenReturn( true ); PowerMockito.mockStatic( ZipUtil.class ); when( ZipUtil.zipFiles( any( String.class ), any( String.class ) ) ).thenReturn( true ); HMSManagementRestService service = new HMSManagementRestService(); Response response = service.getHmsLogs(); assertNotNull( response ); assertEquals( response.getStatus(), 404 ); }
@Test @Ignore public void getHMSLogsTest() throws Exception { HMSManagementRestService service = new HMSManagementRestService(); PowerMockito.mockStatic( ZipUtil.class ); when( ZipUtil.zipFiles( any( String.class ), any( String.class ) ) ).thenReturn( true ); Response response = service.getHmsLogs(); assertNotNull( response ); assertEquals( response.getStatus(), 200 ); }