ApplicationResource implements IApplicationResource { @Override public Response getApplicationBindings( String applicationName ) { Response response; ManagedApplication ma = this.manager.applicationMngr().findManagedApplicationByName( applicationName ); if( ma == null ) { response = handleError( Status.NOT_FOUND, new RestError( REST_INEXISTING, application( applicationName )), lang( this.manager )).build(); } else { ApplicationBindings bindings = new ApplicationBindings(); for( Component c : ComponentHelpers.findAllComponents( ma.getApplication())) { for( ImportedVariable var : ComponentHelpers.findAllImportedVariables( c ).values()) { if( ! var.isExternal()) continue; String prefix = VariableHelpers.parseVariableName( var.getName()).getKey(); bindings.prefixToItems.put( prefix, new ArrayList<ApplicationBindingItem> ()); } } for( ManagedApplication managedApp : this.manager.applicationMngr().getManagedApplications()) { String prefix = managedApp.getApplication().getTemplate().getExternalExportsPrefix(); if( prefix == null || ! bindings.prefixToItems.containsKey( prefix )) continue; Set<String> boundApps = ma.getApplication().getApplicationBindings().get( prefix ); boolean bound = boundApps != null && boundApps.contains( managedApp.getName()); ApplicationBindingItem item = new ApplicationBindingItem( managedApp.getName(), bound ); bindings.prefixToItems.get( prefix ).add( item ); } response = Response.ok().entity( bindings ).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 testGetApplicationBindings_inexistingApplication() throws Exception { Response resp = this.resource.getApplicationBindings( "inexisting" ); Assert.assertEquals( Status.NOT_FOUND.getStatusCode(), resp.getStatus()); }
@Test public void testGetApplicationBindings_success_noExternalImports() throws Exception { this.app.bindWithApplication( "some", "value" ); this.app.bindWithApplication( "another", "value" ); Response resp = this.resource.getApplicationBindings( this.app.getName()); Assert.assertEquals( Status.OK.getStatusCode(), resp.getStatus()); ApplicationBindings bindings = (ApplicationBindings) resp.getEntity(); Assert.assertEquals( 0, bindings.prefixToItems.size()); }
@Test public void testGetApplicationBindings_success_withExternalImports() throws Exception { ImportedVariable newVar = new ImportedVariable( "prefix1.ip", false, true ); this.app.getWar().getComponent().importedVariables.put( newVar.getName(), newVar ); newVar = new ImportedVariable( "prefix2.something", false, true ); this.app.getWar().getComponent().importedVariables.put( newVar.getName(), newVar ); newVar = new ImportedVariable( "prefix3.else", false, true ); this.app.getWar().getComponent().importedVariables.put( newVar.getName(), newVar ); Map<String,String> nameToExternalExportPrefix = new LinkedHashMap<>( 4 ); nameToExternalExportPrefix.put( "app1", "prefix1" ); nameToExternalExportPrefix.put( "this_app", "prefix2" ); nameToExternalExportPrefix.put( "app2", "prefix1" ); nameToExternalExportPrefix.put( "app", null ); for( Map.Entry<String,String> entry : nameToExternalExportPrefix.entrySet()) { TestApplication ta = new TestApplication(); ta.setDirectory( this.folder.newFolder()); ta.setName( entry.getKey()); ta.getTemplate().setExternalExportsPrefix( entry.getValue()); this.managerWrapper.addManagedApplication( new ManagedApplication( ta )); if( entry.getValue() != null ) this.app.bindWithApplication( entry.getValue(), ta.getName()); } this.app.bindWithApplication( "invalid", "app" ); Response resp = this.resource.getApplicationBindings( this.app.getName()); Assert.assertEquals( Status.OK.getStatusCode(), resp.getStatus()); ApplicationBindings bindings = (ApplicationBindings) resp.getEntity(); Assert.assertEquals( 3, bindings.prefixToItems.size()); List<ApplicationBindingItem> items = bindings.prefixToItems.get( "prefix1" ); Assert.assertEquals( 2, items.size()); Assert.assertTrue( items.get( 0 ).isBound()); Assert.assertTrue( items.get( 1 ).isBound()); List<String> boundAppNames = new ArrayList<> (); boundAppNames.add( items.get( 0 ).getApplicationName()); boundAppNames.add( items.get( 1 ).getApplicationName()); Collections.sort( boundAppNames ); Assert.assertEquals( Arrays.asList( "app1", "app2" ), boundAppNames ); items = bindings.prefixToItems.get( "prefix2" ); Assert.assertEquals( 1, items.size()); Assert.assertTrue( items.get( 0 ).isBound()); Assert.assertEquals( "this_app", items.get( 0 ).getApplicationName()); items = bindings.prefixToItems.get( "prefix3" ); Assert.assertEquals( 0, items.size()); Assert.assertTrue( this.app.unbindFromApplication( "prefix1", "app2" )); resp = this.resource.getApplicationBindings( this.app.getName()); Assert.assertEquals( Status.OK.getStatusCode(), resp.getStatus()); bindings = (ApplicationBindings) resp.getEntity(); Assert.assertEquals( 3, bindings.prefixToItems.size()); items = bindings.prefixToItems.get( "prefix1" ); Assert.assertEquals( 2, items.size()); if( "app1".equals( items.get( 1 ).getApplicationName())) { ApplicationBindingItem itemToMove = items.remove( 1 ); items.add( 0, itemToMove ); } Assert.assertEquals( "app1", items.get( 0 ).getApplicationName()); Assert.assertTrue( items.get( 0 ).isBound()); Assert.assertEquals( "app2", items.get( 1 ).getApplicationName()); Assert.assertFalse( items.get( 1 ).isBound()); items = bindings.prefixToItems.get( "prefix2" ); Assert.assertEquals( 1, items.size()); Assert.assertTrue( items.get( 0 ).isBound()); Assert.assertEquals( "this_app", items.get( 0 ).getApplicationName()); items = bindings.prefixToItems.get( "prefix3" ); Assert.assertEquals( 0, items.size()); }