ApplicationResource implements IApplicationResource { @Override public Response addInstance( String applicationName, String parentInstancePath, Instance instance ) { if( parentInstancePath == null ) this.logger.fine( "Request: add root instance " + instance.getName() + " in " + applicationName + "." ); else this.logger.fine( "Request: add instance " + instance.getName() + " under " + parentInstancePath + " in " + applicationName + "." ); String lang = lang( this.manager ); Response response; 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 { Graphs graphs = ma.getApplication().getTemplate().getGraphs(); String componentName = null; if( instance.getComponent() != null ) componentName = instance.getComponent().getName(); Component realComponent; if( componentName == null ) { response = handleError( Status.NOT_FOUND, new RestError( REST_MISSING_PROPERTY, value( "component" )), lang ).build(); } else if((realComponent = ComponentHelpers.findComponent( graphs, componentName )) == null ) { response = handleError( Status.NOT_FOUND, new RestError( REST_INEXISTING, component( componentName )), lang ).build(); } else { instance.setComponent( realComponent ); InstanceHelpers.fixOverriddenExports( instance ); Instance parentInstance = InstanceHelpers.findInstanceByPath( ma.getApplication(), parentInstancePath ); this.manager.instancesMngr().addInstance( ma, parentInstance, instance ); response = Response.ok().build(); } } } catch( ImpossibleInsertionException | 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 testAddInstance_root_success() throws Exception { Assert.assertEquals( 2, this.app.getRootInstances().size()); Instance newInstance = new Instance( "vm-mail" ).component( this.app.getMySqlVm().getComponent()); Response resp = this.resource.addInstance( this.app.getName(), null, newInstance ); Assert.assertEquals( Status.OK.getStatusCode(), resp.getStatus()); Assert.assertEquals( 3, this.app.getRootInstances().size()); }
@Test public void testAddInstance_root_byComponentName_success() throws Exception { Assert.assertEquals( 2, this.app.getRootInstances().size()); String componentName = this.app.getMySqlVm().getComponent().getName(); Instance newInstance = new Instance( "vm-mail" ).component( new Component( componentName )); Response resp = this.resource.addInstance( this.app.getName(), null, newInstance ); Assert.assertEquals( Status.OK.getStatusCode(), resp.getStatus()); Collection<Instance> rootInstances = this.app.getRootInstances(); Assert.assertEquals( 3, rootInstances.size()); for( Instance rootInstance : rootInstances ) { Assert.assertEquals( rootInstance.getName(), Constants.TARGET_INSTALLER, rootInstance.getComponent().getInstallerName()); } }
@Test public void testAddInstance_IOException() throws Exception { this.msgClient.connected.set( false ); Assert.assertEquals( 2, this.app.getRootInstances().size()); Instance newInstance = new Instance( "vm-mail" ).component( this.app.getMySqlVm().getComponent()); Response resp = this.resource.addInstance( this.app.getName(), null, newInstance ); Assert.assertEquals( Status.FORBIDDEN.getStatusCode(), resp.getStatus()); Assert.assertEquals( 2, this.app.getRootInstances().size()); }
@Test public void testAddInstance_root_nullComponent() throws Exception { Assert.assertEquals( 2, this.app.getRootInstances().size()); Instance existingInstance = new Instance( this.app.getMySqlVm().getName()); Response resp = this.resource.addInstance( this.app.getName(), null, existingInstance ); Assert.assertEquals( Status.NOT_FOUND.getStatusCode(), resp.getStatus()); }
@Test public void testAddInstance_root_invalidComponent() throws Exception { Assert.assertEquals( 2, this.app.getRootInstances().size()); Instance existingInstance = new Instance( this.app.getMySqlVm().getName()).component( new Component( "invalid" )); Response resp = this.resource.addInstance( this.app.getName(), null, existingInstance ); Assert.assertEquals( Status.NOT_FOUND.getStatusCode(), resp.getStatus()); }
@Test public void testAddInstance_root_duplicate() throws Exception { Assert.assertEquals( 2, this.app.getRootInstances().size()); Instance existingInstance = new Instance( this.app.getMySqlVm().getName()).component( this.app.getMySqlVm().getComponent()); Response resp = this.resource.addInstance( this.app.getName(), null, existingInstance ); Assert.assertEquals( Status.FORBIDDEN.getStatusCode(), resp.getStatus()); }
@Test public void testAddInstance_child_success() throws Exception { Instance newMysql = new Instance( "mysql-2" ).component( this.app.getMySql().getComponent()); Assert.assertEquals( 1, this.app.getTomcatVm().getChildren().size()); Assert.assertFalse( this.app.getTomcatVm().getChildren().contains( newMysql )); Response resp = this.resource.addInstance( this.app.getName(), InstanceHelpers.computeInstancePath( this.app.getTomcatVm()), newMysql ); Assert.assertEquals( Status.OK.getStatusCode(), resp.getStatus()); Assert.assertEquals( 2, this.app.getTomcatVm().getChildren().size()); List<String> paths = new ArrayList<> (); for( Instance inst : this.app.getTomcatVm().getChildren()) paths.add( InstanceHelpers.computeInstancePath( inst )); String rootPath = InstanceHelpers.computeInstancePath( this.app.getTomcatVm()); Assert.assertTrue( paths.contains( rootPath + "/" + newMysql.getName())); Assert.assertTrue( paths.contains( rootPath + "/" + this.app.getTomcat().getName())); }
@Test public void testAddInstance_child_byComponentName_success() throws Exception { String componentName = this.app.getMySql().getComponent().getName(); Instance newMysql = new Instance( "mysql-2" ).component( new Component( componentName )); Assert.assertEquals( 1, this.app.getTomcatVm().getChildren().size()); Assert.assertFalse( this.app.getTomcatVm().getChildren().contains( newMysql )); Response resp = this.resource.addInstance( this.app.getName(), InstanceHelpers.computeInstancePath( this.app.getTomcatVm()), newMysql ); Assert.assertEquals( Status.OK.getStatusCode(), resp.getStatus()); Assert.assertEquals( 2, this.app.getTomcatVm().getChildren().size()); List<String> paths = new ArrayList<> (); for( Instance inst : this.app.getTomcatVm().getChildren()) { Assert.assertEquals( inst.getName(), "puppet", inst.getComponent().getInstallerName()); paths.add( InstanceHelpers.computeInstancePath( inst )); } String rootPath = InstanceHelpers.computeInstancePath( this.app.getTomcatVm()); Assert.assertTrue( paths.contains( rootPath + "/" + newMysql.getName())); Assert.assertTrue( paths.contains( rootPath + "/" + this.app.getTomcat().getName())); }
@Test public void testAddInstance_child_incompleteComponent() throws Exception { String mySqlComponentName = this.app.getMySql().getComponent().getName(); Instance newMysql = new Instance( "mysql-2" ).component( new Component( mySqlComponentName )); Assert.assertEquals( 1, this.app.getTomcatVm().getChildren().size()); Assert.assertFalse( this.app.getTomcatVm().getChildren().contains( newMysql )); Response resp = this.resource.addInstance( this.app.getName(), InstanceHelpers.computeInstancePath( this.app.getTomcatVm()), newMysql ); Assert.assertEquals( Status.OK.getStatusCode(), resp.getStatus()); Assert.assertEquals( 2, this.app.getTomcatVm().getChildren().size()); List<String> paths = new ArrayList<> (); for( Instance inst : this.app.getTomcatVm().getChildren()) paths.add( InstanceHelpers.computeInstancePath( inst )); String rootPath = InstanceHelpers.computeInstancePath( this.app.getTomcatVm()); Assert.assertTrue( paths.contains( rootPath + "/" + newMysql.getName())); Assert.assertTrue( paths.contains( rootPath + "/" + this.app.getTomcat().getName())); }
@Test public void testAddInstance_child_failure() throws Exception { Instance newWar = new Instance( "war-2" ).component( this.app.getWar().getComponent()); Assert.assertEquals( 1, this.app.getTomcatVm().getChildren().size()); Assert.assertFalse( this.app.getTomcatVm().getChildren().contains( newWar )); Response resp = this.resource.addInstance( this.app.getName(), InstanceHelpers.computeInstancePath( this.app.getTomcatVm()), newWar ); Assert.assertEquals( Status.FORBIDDEN.getStatusCode(), resp.getStatus()); }
@Test public void testAddInstance_inexstingApplication() throws Exception { Instance newMysql = new Instance( "mysql-2" ).component( this.app.getMySql().getComponent()); Response resp = this.resource.addInstance( "inexisting", InstanceHelpers.computeInstancePath( this.app.getTomcatVm()), newMysql ); Assert.assertEquals( Status.NOT_FOUND.getStatusCode(), resp.getStatus()); }
@Test public void testAddInstance_inexstingParentInstance() throws Exception { Instance newMysql = new Instance( "mysql-2" ).component( this.app.getMySql().getComponent()); Response resp = this.resource.addInstance( "inexisting", "/bip/bip", newMysql ); Assert.assertEquals( Status.NOT_FOUND.getStatusCode(), resp.getStatus()); }