ApplicationResource implements IApplicationResource { @Override public Response removeInstance( String applicationName, String instancePath ) { this.logger.fine( "Request: remove " + instancePath + " in " + applicationName + "." ); String lang = lang( this.manager ); Response response = Response.ok().build(); Instance instance; try { ManagedApplication ma = this.manager.applicationMngr().findManagedApplicationByName( applicationName ); if( ma == null ) { response = handleError( Status.NOT_FOUND, new RestError( REST_INEXISTING, application( applicationName )), lang ).build(); } else if(( instance = InstanceHelpers.findInstanceByPath( ma.getApplication(), instancePath )) == null ) { response = handleError( Status.NOT_FOUND, new RestError( REST_INEXISTING, instance( instancePath ), application( applicationName )), lang ).build(); } else { this.manager.instancesMngr().removeInstance( ma, instance ); } } catch( UnauthorizedActionException e ) { response = RestServicesUtils.handleError( Status.FORBIDDEN, new RestError( ErrorCode.REST_UNDETAILED_ERROR, e ), lang ).build(); } catch( IOException e ) { response = RestServicesUtils.handleError( Status.NOT_ACCEPTABLE, new RestError( ErrorCode.REST_UNDETAILED_ERROR, e ), lang ).build(); } return response; } ApplicationResource( Manager manager ); @Override Response changeInstanceState( String applicationName, String newState, String instancePath ); @Override Response setDescription( String applicationName, String desc ); @Override Response deployAndStartAll( String applicationName, String instancePath ); @Override Response stopAll( String applicationName, String instancePath ); @Override Response undeployAll( String applicationName, String instancePath ); @Override List<Instance> listChildrenInstances( String applicationName, String instancePath, boolean allChildren ); @Override Response bindApplication( String applicationName, String externalExportPrefix, String boundApp ); @Override Response unbindApplication( String applicationName, String externalExportPrefix, String boundApp ); @Override Response replaceApplicationBindings( String applicationName, String externalExportPrefix, List<String> boundApps ); @Override Response getApplicationBindings( String applicationName ); @Override Response addInstance( String applicationName, String parentInstancePath, Instance instance ); @Override Response removeInstance( String applicationName, String instancePath ); @Override List<TargetAssociation> findTargetAssociations( String applicationName ); @Override Response resynchronize( String applicationName ); @Override List<Component> listComponents( String applicationName ); @Override List<Component> findComponentChildren( String applicationName, String componentName ); @Override List<Component> findComponentAncestors( String applicationName, String componentName ); @Override List<String> listCommands( String app ); @Override Response executeCommand( String app, String commandName ); @Override Response getCommandInstructions( String appName, String commandName ); @Override Response replaceTags( String name, String version, List<String> tags );  }
@Test public void testRemoveInstance_success() { final String tomcatPath = InstanceHelpers.computeInstancePath( this.app.getTomcat()); Assert.assertNotNull( InstanceHelpers.findInstanceByPath( this.app, tomcatPath )); Response resp = this.resource.removeInstance( this.app.getName(), tomcatPath ); Assert.assertEquals( Status.OK.getStatusCode(), resp.getStatus()); Assert.assertNull( InstanceHelpers.findInstanceByPath( this.app, tomcatPath )); }
@Test public void testRemoveInstance_stillRunning() { this.app.getTomcat().setStatus( InstanceStatus.DEPLOYED_STARTED ); final String tomcatPath = InstanceHelpers.computeInstancePath( this.app.getTomcat()); Assert.assertNotNull( InstanceHelpers.findInstanceByPath( this.app, tomcatPath )); Response resp = this.resource.removeInstance( this.app.getName(), tomcatPath ); Assert.assertEquals( Status.FORBIDDEN.getStatusCode(), resp.getStatus()); Assert.assertNotNull( InstanceHelpers.findInstanceByPath( this.app, tomcatPath )); }
@Test public void testRemoveInstance_IOException() { this.msgClient.connected.set( false ); final String tomcatPath = InstanceHelpers.computeInstancePath( this.app.getTomcat()); Assert.assertNotNull( InstanceHelpers.findInstanceByPath( this.app, tomcatPath )); Response resp = this.resource.removeInstance( this.app.getName(), tomcatPath ); Assert.assertEquals( Status.NOT_ACCEPTABLE.getStatusCode(), resp.getStatus()); Assert.assertNotNull( InstanceHelpers.findInstanceByPath( this.app, tomcatPath )); }
@Test public void testRemoveInstance_nonExistingInstance() { Response resp = this.resource.removeInstance( this.app.getName(), "/I-do-not-exist" ); Assert.assertEquals( Status.NOT_FOUND.getStatusCode(), resp.getStatus()); }
@Test public void testRemoveInstance_nonExistingApplication() { Response resp = this.resource.removeInstance( "I-am-not-an-app", InstanceHelpers.computeInstancePath( this.app.getTomcat())); Assert.assertEquals( Status.NOT_FOUND.getStatusCode(), resp.getStatus()); }