ApplicationResource implements IApplicationResource { @Override public Response replaceApplicationBindings( String applicationName, String externalExportPrefix, List<String> boundApps ) { if( this.logger.isLoggable( Level.FINE )) { StringBuilder sb = new StringBuilder(); sb.append( "Replacing the bindings for the " ); sb.append( externalExportPrefix ); sb.append( " prefix with " ); sb.append( Arrays.toString( boundApps.toArray())); sb.append( " in application " ); sb.append( applicationName ); sb.append( "." ); this.logger.fine( sb.toString()); } String lang = lang( this.manager ); Response response; try { ManagedApplication ma = this.manager.applicationMngr().findManagedApplicationByName( applicationName ); if( ma == null ) { response = RestServicesUtils.handleError( Status.NOT_FOUND, new RestError( ErrorCode.REST_INEXISTING, application( applicationName )), lang ).build(); } else { Set<String> apps = new TreeSet<> (); if( boundApps != null ) apps.addAll( boundApps ); this.manager.applicationMngr().replaceApplicationBindings( ma, externalExportPrefix, apps ); response = Response.ok().build(); } } catch( UnauthorizedActionException | IOException e ) { response = RestServicesUtils.handleError( Status.FORBIDDEN, 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 testReplaceApplicationBindings_success() throws Exception { TestApplication app2 = new TestApplication(); app2.setDirectory( this.folder.newFolder()); app2.getTemplate().setName( "tpl-other" ); app2.getTemplate().setExternalExportsPrefix( "eep" ); app2.setName( "app-other" ); this.managerWrapper.addManagedApplication( new ManagedApplication( app2 )); Assert.assertEquals( 0, this.msgClient.allSentMessages.size()); Assert.assertEquals( 0, this.ma.removeAwaitingMessages( this.app.getTomcatVm()).size()); Assert.assertEquals( 0, this.ma.removeAwaitingMessages( this.app.getMySqlVm()).size()); Response resp = this.resource.replaceApplicationBindings( this.ma.getName(), app2.getTemplate().getExternalExportsPrefix(), Arrays.asList( app2.getName())); Assert.assertEquals( Status.OK.getStatusCode(), resp.getStatus()); Assert.assertEquals( 0, this.msgClient.allSentMessages.size()); List<Message> messages = this.ma.removeAwaitingMessages( this.app.getTomcatVm()); Assert.assertEquals( 1, messages.size()); messages.addAll( this.ma.removeAwaitingMessages( this.app.getMySqlVm())); Assert.assertEquals( 2, messages.size()); for( Message m : this.msgClient.allSentMessages ) { Assert.assertEquals( MsgCmdChangeBinding.class, m.getClass()); MsgCmdChangeBinding msg = (MsgCmdChangeBinding) m; Assert.assertEquals( app2.getTemplate().getExternalExportsPrefix(), msg.getExternalExportsPrefix()); Assert.assertNotNull( msg.getAppNames()); Assert.assertEquals( 1, msg.getAppNames().size()); Assert.assertTrue( msg.getAppNames().contains( app2.getName())); } resp = this.resource.replaceApplicationBindings( this.ma.getName(), app2.getTemplate().getExternalExportsPrefix(), new ArrayList<String>( 0 )); Assert.assertEquals( Status.OK.getStatusCode(), resp.getStatus()); Assert.assertEquals( 0, this.msgClient.allSentMessages.size()); messages = this.ma.removeAwaitingMessages( this.app.getTomcatVm()); Assert.assertEquals( 1, messages.size()); messages.addAll( this.ma.removeAwaitingMessages( this.app.getMySqlVm())); Assert.assertEquals( 2, messages.size()); for( Message m : this.msgClient.allSentMessages ) { Assert.assertEquals( MsgCmdChangeBinding.class, m.getClass()); MsgCmdChangeBinding msg = (MsgCmdChangeBinding) m; Assert.assertEquals( app2.getTemplate().getExternalExportsPrefix(), msg.getExternalExportsPrefix()); Assert.assertNull( msg.getAppNames()); } resp = this.resource.replaceApplicationBindings( this.ma.getName(), app2.getTemplate().getExternalExportsPrefix(), null ); Assert.assertEquals( Status.OK.getStatusCode(), resp.getStatus()); Assert.assertEquals( 0, this.msgClient.allSentMessages.size()); messages = this.ma.removeAwaitingMessages( this.app.getTomcatVm()); Assert.assertEquals( 0, messages.size()); messages.addAll( this.ma.removeAwaitingMessages( this.app.getMySqlVm())); Assert.assertEquals( 0, messages.size()); }
@Test public void testReplaceApplicationBindings_inexistingApplication() throws Exception { Response resp = this.resource.replaceApplicationBindings( "invalid", this.ma.getApplication().getTemplate().getName(), Arrays.asList( this.ma.getName())); Assert.assertEquals( Status.NOT_FOUND.getStatusCode(), resp.getStatus()); }
@Test public void testReplaceApplicationBindings_invalidBoundTemplate() throws Exception { TestApplication app2 = new TestApplication(); app2.setDirectory( this.folder.newFolder()); app2.getTemplate().setName( "tpl-other" ); app2.setName( "app-other" ); this.managerWrapper.addManagedApplication( new ManagedApplication( app2 )); Response resp = this.resource.replaceApplicationBindings( this.ma.getName(), this.ma.getApplication().getTemplate().getName(), Arrays.asList( app2.getName())); Assert.assertEquals( Status.FORBIDDEN.getStatusCode(), resp.getStatus()); Assert.assertEquals( 0, this.msgClient.allSentMessages.size()); }