UpgradeRestService { @POST public Response upgrade( OobUpgradeSpec upgradeSpec ) throws HMSRestException { String message = null; Response validationResponse = UpgradeUtil.validateUpgradeRequest( upgradeSpec ); if ( validationResponse != null ) { if ( upgradeSpec != null ) { UpgradeUtil.deleteUpgradeFiles( upgradeSpec.getId() ); } return validationResponse; } final String upgradeId = upgradeSpec.getId(); final String upgradeDir = UpgradeUtil.getUpgradeDir( upgradeId ); UpgradeStatus status = new UpgradeStatus(); status.setId( upgradeId ); boolean scriptsExecutable = FileUtil.setFilesExecutable( upgradeDir, "sh" ); if ( !scriptsExecutable ) { UpgradeUtil.deleteUpgradeFiles( upgradeId ); message = String.format( "Failed to grant execute rights to upgrade scripts at '%s'.", upgradeDir ); logger.error( message ); status.setStatusCode( UpgradeStatusCode.HMS_OOB_UPGRADE_INTERNAL_ERROR ); status.setStatusMessage( UpgradeStatusCode.HMS_OOB_UPGRADE_INTERNAL_ERROR.getStatusMessage() ); status.setMoreInfo( message ); return Response.status( Status.INTERNAL_SERVER_ERROR ).entity( status ).build(); } boolean serviceInMaintenance = ServiceManager.putServiceInMaintenance(); if ( serviceInMaintenance ) { boolean upgradeInitiated = UpgradeUtil.initiateUpgrade( upgradeId, upgradeSpec.getFileName() ); if ( upgradeInitiated ) { if ( ( ServiceManager.getServiceState().equals( ServiceState.NORMAL_MAINTENANCE ) ) && ( ServiceManager.getActiveRequests() == 1 ) ) { message = "Upgrade initiated after Out-of-band agent in " + ServiceState.NORMAL_MAINTENANCE.toString(); } else if ( ( ServiceManager.getServiceState().equals( ServiceState.FORCE_MAINTENANCE ) ) && ( ServiceManager.getActiveRequests() > 1 ) ) { message = String.format( "Upgrade initiated after Out-of-band agent " + "in %s with %s active requests.", ServiceState.FORCE_MAINTENANCE.toString(), ServiceManager.getActiveRequests() ); } logger.info( message ); status.setStatusCode( UpgradeStatusCode.HMS_OOB_UPGRADE_INITIATED ); final String hmsUpgradeDir = UpgradeUtil.getHmsUpgradeDir(); String upgradeStatusFileAbsPath = String.format( "%1$s/%2$s.json", hmsUpgradeDir, upgradeId ); boolean saved = HmsUpgradeUtil.saveUpgradeStatus( upgradeStatusFileAbsPath, status ); if ( saved ) { logger.info( "Saved upgrade status to '{}'", upgradeStatusFileAbsPath ); } else { logger.warn( "Unable to save upgrade status to '{}'.", upgradeStatusFileAbsPath ); } status.setStatusMessage( UpgradeStatusCode.HMS_OOB_UPGRADE_INITIATED.getStatusMessage() ); status.setMoreInfo( message ); return Response.status( Status.ACCEPTED ).entity( status ).build(); } else { UpgradeUtil.deleteUpgradeFiles( upgradeId ); message = "Executing Out-of-band Agent upgrade script failed."; logger.error( message ); ServiceManager.putServiceInRunning(); status.setStatusCode( UpgradeStatusCode.HMS_OOB_UPGRADE_INTERNAL_ERROR ); status.setStatusMessage( UpgradeStatusCode.HMS_OOB_UPGRADE_INTERNAL_ERROR.getStatusMessage() ); status.setMoreInfo( message ); return Response.status( Status.INTERNAL_SERVER_ERROR ).entity( status ).build(); } } else { UpgradeUtil.deleteUpgradeFiles( upgradeId ); message = "Setting Out-of-band Agent Service in MAINTENANCE failed."; logger.error( message ); status.setStatusCode( UpgradeStatusCode.HMS_OOB_UPGRADE_INTERNAL_ERROR ); status.setStatusMessage( UpgradeStatusCode.HMS_OOB_UPGRADE_INTERNAL_ERROR.getStatusMessage() ); status.setMoreInfo( message ); return Response.status( Status.INTERNAL_SERVER_ERROR ).entity( status ).build(); } }  @POST Response upgrade( OobUpgradeSpec upgradeSpec ); @POST @Path( "/rollback" ) Response rollback( RollbackSpec rollbackSpec ); @POST @Path( "/upload" ) @Consumes( MediaType.MULTIPART_FORM_DATA ) Response uploadFile( MultipartFormDataInput multipartFormDataInput ); @GET @Path( "/monitor/{upgradeId}" ) Response getUpgradeStatus( @PathParam( "upgradeId" ) String upgradeId ); @DELETE @Path( "/backup/{upgradeId}" ) Response deleteBackup( @PathParam( "upgradeId" ) String upgradeId ); @POST @Path( "/proxy/restart/{upgradeId}" ) Response restartProxy( @PathParam( "upgradeId" ) String upgradeId ); @GET @Path( "/download/inventory/{upgradeId}" ) File downloadInventoryFile( @PathParam( "upgradeId" ) String upgradeId );  }
@Test public void testUpgradeWithNullSpec() { Response response = upgrade( null ); assertNotNull( response ); assertTrue( response.getStatus() == Status.BAD_REQUEST.getStatusCode() ); UpgradeStatus status = (UpgradeStatus) response.getEntity(); assertNotNull( status ); message = "HMS upgrade request should have the mandatory parameters - [ id, fileName, checksum ]."; assertEquals( message, status.getMoreInfo() ); }
@Test public void testUpgradeWithNullChecksum() { upgradeSpec.setChecksum( null ); Response response = upgrade( upgradeSpec ); assertNotNull( response ); assertTrue( response.getStatus() == Status.BAD_REQUEST.getStatusCode() ); UpgradeStatus status = (UpgradeStatus) response.getEntity(); assertNotNull( status ); message = "'checksum' is a mandatory parameter for HMS Upgrade."; assertEquals( message, status.getMoreInfo() ); }
@Test public void testUpgradeWithBlankChecksum() { upgradeSpec.setChecksum( " " ); Response response = upgrade( upgradeSpec ); assertNotNull( response ); assertTrue( response.getStatus() == Status.BAD_REQUEST.getStatusCode() ); UpgradeStatus status = (UpgradeStatus) response.getEntity(); assertNotNull( status ); message = "'checksum' is a mandatory parameter for HMS Upgrade."; assertEquals( message, status.getMoreInfo() ); }
@Test public void testUpgradeWithNullFileName() { upgradeSpec.setFileName( null ); Response response = upgrade( upgradeSpec ); assertNotNull( response ); assertTrue( response.getStatus() == Status.BAD_REQUEST.getStatusCode() ); UpgradeStatus status = (UpgradeStatus) response.getEntity(); assertNotNull( status ); message = "'fileName' is a mandatory parameter for HMS Upgrade."; assertEquals( message, status.getMoreInfo() ); }
@Test public void testUpgradeWithBlankFileName() { upgradeSpec.setFileName( " " ); Response response = upgrade( upgradeSpec ); assertNotNull( response ); assertTrue( response.getStatus() == Status.BAD_REQUEST.getStatusCode() ); UpgradeStatus status = (UpgradeStatus) response.getEntity(); assertNotNull( status ); message = "'fileName' is a mandatory parameter for HMS Upgrade."; assertEquals( message, status.getMoreInfo() ); }
@Test public void testUpgradeWithNullHmsToken() { upgradeSpec.setId( null ); Response response = upgrade( upgradeSpec ); assertNotNull( response ); assertTrue( response.getStatus() == Status.BAD_REQUEST.getStatusCode() ); UpgradeStatus status = (UpgradeStatus) response.getEntity(); assertNotNull( status ); message = "'id' is a mandatory parameter for HMS Upgrade."; assertEquals( message, status.getMoreInfo() ); }
@Test public void testUpgradeWithBlankHmsToken() { upgradeSpec.setId( " " ); Response response = upgrade( upgradeSpec ); assertNotNull( response ); assertTrue( response.getStatus() == Status.BAD_REQUEST.getStatusCode() ); UpgradeStatus status = (UpgradeStatus) response.getEntity(); assertNotNull( status ); message = "'id' is a mandatory parameter for HMS Upgrade."; assertEquals( message, status.getMoreInfo() ); }
@Test public void testUpgradeWithInvalidFile() { Calendar cal = Calendar.getInstance(); String timeInMillis = Long.toString( cal.getTimeInMillis() ); upgradeSpec.setId( "hmsToken" ); upgradeSpec.setFileName( timeInMillis ); Response response = upgrade( upgradeSpec ); assertNotNull( response ); assertTrue( response.getStatus() == Status.FORBIDDEN.getStatusCode() ); UpgradeStatus status = (UpgradeStatus) response.getEntity(); assertNotNull( status ); message = String.format( "Upgrade binary '%s' not found at '%s'.", upgradeSpec.getFileName(), UpgradeUtil.getUpgradeDir( "hmsToken" ) ); assertEquals( message, status.getMoreInfo() ); }