ApplicationMngrImpl implements IApplicationMngr { @Override public void bindOrUnbindApplication( ManagedApplication ma, String externalExportPrefix, String applicationName, boolean bind ) throws UnauthorizedActionException, IOException { Application app = findApplicationByName( applicationName ); if( app == null ) throw new UnauthorizedActionException( "Application " + applicationName + " does not exist." ); if( ! externalExportPrefix.equals( app.getTemplate().getExternalExportsPrefix())) throw new UnauthorizedActionException( "Application " + applicationName + "'s template does not have " + externalExportPrefix + " as external exports prefix." ); boolean notify = true; if( bind ) ma.getApplication().bindWithApplication( externalExportPrefix, applicationName ); else notify = ma.getApplication().unbindFromApplication( externalExportPrefix, applicationName ); if( notify ) { ConfigurationUtils.saveApplicationBindings( ma.getApplication()); this.logger.fine( "External prefix " + externalExportPrefix + " is now bound to application " + applicationName + " in " + ma.getName() + "." ); for( Instance inst : InstanceHelpers.findAllScopedInstances( ma.getApplication())) { MsgCmdChangeBinding msg = new MsgCmdChangeBinding( externalExportPrefix, ma.getApplication().getApplicationBindings().get( externalExportPrefix )); this.messagingMngr.sendMessageSafely( ma, inst, msg ); } } } ApplicationMngrImpl(
			INotificationMngr notificationMngr,
			IConfigurationMngr configurationMngr,
			ITargetsMngr targetsMngr,
			IMessagingMngr messagingMngr,
			IRandomMngr randomMngr,
			IAutonomicMngr autonomicMngr ); void setApplicationTemplateMngr( IApplicationTemplateMngr applicationTemplateMngr ); @Override Application findApplicationByName( String applicationName ); @Override ManagedApplication findManagedApplicationByName( String applicationName ); @Override Collection<ManagedApplication> getManagedApplications(); @Override ManagedApplication createApplication( String name, String description, String tplName, String tplQualifier ); @Override ManagedApplication createApplication( String name, String description, ApplicationTemplate tpl ); @Override void updateApplication( ManagedApplication ma, String newDesc ); @Override void deleteApplication( ManagedApplication ma ); @Override void restoreApplications(); @Override boolean isTemplateUsed( ApplicationTemplate tpl ); @Override void bindOrUnbindApplication( ManagedApplication ma, String externalExportPrefix, String applicationName, boolean bind ); @Override void replaceApplicationBindings( ManagedApplication ma, String externalExportPrefix, Set<String> applicationNames );  }
@Test( expected = UnauthorizedActionException.class ) public void testBindApplication_invalidApplication() throws Exception { TestApplication app = new TestApplication(); app.setDirectory( this.folder.newFolder()); ManagedApplication ma = new ManagedApplication( app ); TestManagerWrapper.addManagedApplication( this.mngr, ma ); this.mngr.bindOrUnbindApplication( ma, ma.getApplication().getTemplate().getName(), "invalid", true ); }
@Test( expected = UnauthorizedActionException.class ) public void testBindApplication_invalidTemplate() throws Exception { TestApplication app = new TestApplication(); app.setDirectory( this.folder.newFolder()); ManagedApplication ma1 = new ManagedApplication( app ); TestManagerWrapper.addManagedApplication( this.mngr, ma1 ); app = new TestApplication(); app.getTemplate().setName( "tpl-other" ); app.setName( "app-other" ); app.setDirectory( this.folder.newFolder()); ManagedApplication ma2 = new ManagedApplication( app ); TestManagerWrapper.addManagedApplication( this.mngr, ma2 ); this.mngr.bindOrUnbindApplication( ma1, ma1.getApplication().getTemplate().getName(), ma2.getName(), true ); }
@Test( expected = UnauthorizedActionException.class ) public void testBindApplication_invalidTemplatePrefix() throws Exception { TestApplication app = new TestApplication(); app.setDirectory( this.folder.newFolder()); app.getTemplate().setExternalExportsPrefix( "prefix" ); ManagedApplication ma1 = new ManagedApplication( app ); TestManagerWrapper.addManagedApplication( this.mngr, ma1 ); app = new TestApplication(); app.setDirectory( this.folder.newFolder()); ManagedApplication ma2 = new ManagedApplication( app ); TestManagerWrapper.addManagedApplication( this.mngr, ma2 ); this.mngr.bindOrUnbindApplication( ma1, ma1.getApplication().getTemplate().getName(), ma2.getName(), true ); }
@Test public void testBindApplication_success() throws Exception { TestApplication app1 = new TestApplication(); app1.setDirectory( this.folder.newFolder()); app1.getTemplate().setExternalExportsPrefix( "prefix1" ); ManagedApplication ma1 = new ManagedApplication( app1 ); TestManagerWrapper.addManagedApplication( this.mngr, ma1 ); TestApplication app2 = new TestApplication(); app2.getTemplate().setName( "tpl-other" ); app2.getTemplate().setExternalExportsPrefix( "tpl-other-prefix" ); app2.getTemplate().setExternalExportsPrefix( "prefix2" ); app2.setName( "app-other" ); app2.getMySqlVm().setName( "other-mysql" ); app2.getTomcatVm().setName( "other-tomcat" ); app2.setDirectory( this.folder.newFolder()); ManagedApplication ma2 = new ManagedApplication( app2 ); TestManagerWrapper.addManagedApplication( this.mngr, ma2 ); Assert.assertEquals( 0, ma1.getApplication().getApplicationBindings().size()); String eep = ma2.getApplication().getTemplate().getExternalExportsPrefix(); Mockito.verifyZeroInteractions( this.messagingMngr ); this.mngr.bindOrUnbindApplication( ma1, eep, ma2.getName(), true ); Assert.assertEquals( 1, ma1.getApplication().getApplicationBindings().size()); Assert.assertTrue( ma1.getApplication().getApplicationBindings().get( eep ).contains( ma2.getName())); ArgumentCaptor<ManagedApplication> arg0 = ArgumentCaptor.forClass( ManagedApplication.class ); ArgumentCaptor<Instance> arg1 = ArgumentCaptor.forClass( Instance.class ); ArgumentCaptor<Message> arg2 = ArgumentCaptor.forClass( Message.class ); Mockito.verify( this.messagingMngr, Mockito.times( 2 )).sendMessageSafely( arg0.capture(), arg1.capture(), arg2.capture()); for( ManagedApplication s : arg0.getAllValues()) { Assert.assertEquals( ma1, s ); } for( Message m : arg2.getAllValues()) { Assert.assertEquals( MsgCmdChangeBinding.class, m.getClass()); MsgCmdChangeBinding msg = (MsgCmdChangeBinding) m; Assert.assertEquals( ma2.getApplication().getTemplate().getExternalExportsPrefix(), msg.getExternalExportsPrefix()); Assert.assertNotNull( msg.getAppNames()); Assert.assertEquals( 1, msg.getAppNames().size()); Assert.assertTrue( msg.getAppNames().contains( ma2.getName())); } List<Instance> instances = arg1.getAllValues(); Assert.assertTrue( instances.contains( app1.getMySqlVm())); Assert.assertTrue( instances.contains( app1.getTomcatVm())); }
@Test public void testUnbindApplication_success_withNotification() throws Exception { TestApplication app1 = new TestApplication(); app1.setDirectory( this.folder.newFolder()); app1.getTemplate().setExternalExportsPrefix( "prefix1" ); ManagedApplication ma1 = new ManagedApplication( app1 ); TestManagerWrapper.addManagedApplication( this.mngr, ma1 ); TestApplication app2 = new TestApplication(); app2.getTemplate().setName( "tpl-other" ); app2.getTemplate().setExternalExportsPrefix( "tpl-other-prefix" ); app2.getTemplate().setExternalExportsPrefix( "prefix2" ); app2.setName( "app-other" ); app2.getMySqlVm().setName( "other-mysql" ); app2.getTomcatVm().setName( "other-tomcat" ); app2.setDirectory( this.folder.newFolder()); ManagedApplication ma2 = new ManagedApplication( app2 ); TestManagerWrapper.addManagedApplication( this.mngr, ma2 ); Assert.assertEquals( 0, ma1.getApplication().getApplicationBindings().size()); String eep = ma2.getApplication().getTemplate().getExternalExportsPrefix(); Mockito.verifyZeroInteractions( this.messagingMngr ); this.mngr.bindOrUnbindApplication( ma1, eep, ma2.getName(), true ); Assert.assertEquals( 1, ma1.getApplication().getApplicationBindings().size()); Assert.assertTrue( ma1.getApplication().getApplicationBindings().get( eep ).contains( ma2.getName())); Mockito.reset( this.messagingMngr ); this.mngr.bindOrUnbindApplication( ma1, eep, ma2.getName(), false ); Assert.assertEquals( 0, ma1.getApplication().getApplicationBindings().size()); ArgumentCaptor<ManagedApplication> arg0 = ArgumentCaptor.forClass( ManagedApplication.class ); ArgumentCaptor<Instance> arg1 = ArgumentCaptor.forClass( Instance.class ); ArgumentCaptor<Message> arg2 = ArgumentCaptor.forClass( Message.class ); Mockito.verify( this.messagingMngr, Mockito.times( 2 )).sendMessageSafely( arg0.capture(), arg1.capture(), arg2.capture()); for( ManagedApplication s : arg0.getAllValues()) { Assert.assertEquals( ma1, s ); } for( Message m : arg2.getAllValues()) { Assert.assertEquals( MsgCmdChangeBinding.class, m.getClass()); MsgCmdChangeBinding msg = (MsgCmdChangeBinding) m; Assert.assertEquals( ma2.getApplication().getTemplate().getExternalExportsPrefix(), msg.getExternalExportsPrefix()); Assert.assertNull( msg.getAppNames()); } List<Instance> instances = arg1.getAllValues(); Assert.assertTrue( instances.contains( app1.getMySqlVm())); Assert.assertTrue( instances.contains( app1.getTomcatVm())); }
@Test public void testUnbindApplication_success_withoutNotification() throws Exception { TestApplication app1 = new TestApplication(); app1.setDirectory( this.folder.newFolder()); app1.getTemplate().setExternalExportsPrefix( "prefix1" ); ManagedApplication ma1 = new ManagedApplication( app1 ); TestManagerWrapper.addManagedApplication( this.mngr, ma1 ); TestApplication app2 = new TestApplication(); app2.getTemplate().setName( "tpl-other" ); app2.getTemplate().setExternalExportsPrefix( "tpl-other-prefix" ); app2.getTemplate().setExternalExportsPrefix( "prefix2" ); app2.setName( "app-other" ); app2.getMySqlVm().setName( "other-mysql" ); app2.getTomcatVm().setName( "other-tomcat" ); app2.setDirectory( this.folder.newFolder()); ManagedApplication ma2 = new ManagedApplication( app2 ); TestManagerWrapper.addManagedApplication( this.mngr, ma2 ); Assert.assertEquals( 0, ma1.getApplication().getApplicationBindings().size()); String eep = ma2.getApplication().getTemplate().getExternalExportsPrefix(); this.mngr.bindOrUnbindApplication( ma1, eep, ma2.getName(), false ); Assert.assertEquals( 0, ma1.getApplication().getApplicationBindings().size()); Mockito.verifyZeroInteractions( this.messagingMngr ); }