RenameCommandInstruction extends AbstractCommandInstruction { @Override public void updateContext() { String newInstancePath = "/" + this.instancePath.replaceFirst( "/[^/]+$", this.newInstanceName ); Map<String,String> copy = new HashMap<>( this.context.instancePathToComponentName ); for( Map.Entry<String,String> entry : copy.entrySet()) { if( ! entry.getKey().startsWith( this.instancePath + "/" ) && ! entry.getKey().equals( this.instancePath )) continue; String updatedPath = entry.getKey().replace( this.instancePath, newInstancePath ); this.context.instancePathToComponentName.remove( entry.getKey()); this.context.instancePathToComponentName.put( updatedPath, entry.getValue()); } } RenameCommandInstruction( Context context, String instruction, int line ); @Override List<ParsingError> doValidate(); @Override void updateContext(); String getNewInstanceName(); String getInstancePath();  }
@Test public void testUpdateContext() { String line = "rename /tomcat-vm as vm-for-tomcat"; RenameCommandInstruction instr = new RenameCommandInstruction( this.context, line, 1 ); Assert.assertEquals( 0, instr.validate().size()); Assert.assertTrue( this.context.instancePathToComponentName.containsKey( "/tomcat-vm" )); Assert.assertTrue( this.context.instancePathToComponentName.containsKey( "/tomcat-vm/tomcat-server" )); Assert.assertTrue( this.context.instancePathToComponentName.containsKey( "/tomcat-vm/tomcat-server/hello-world" )); int instancesCount = this.context.instancePathToComponentName.size(); instr.updateContext(); Assert.assertEquals( instancesCount, this.context.instancePathToComponentName.size()); Assert.assertFalse( this.context.instancePathToComponentName.containsKey( "/tomcat-vm" )); Assert.assertFalse( this.context.instancePathToComponentName.containsKey( "/tomcat-vm/tomcat-server" )); Assert.assertFalse( this.context.instancePathToComponentName.containsKey( "/tomcat-vm/tomcat-server/hello-world" )); Assert.assertTrue( this.context.instancePathToComponentName.containsKey( "/vm-for-tomcat" )); Assert.assertTrue( this.context.instancePathToComponentName.containsKey( "/vm-for-tomcat/tomcat-server" )); Assert.assertTrue( this.context.instancePathToComponentName.containsKey( "/vm-for-tomcat/tomcat-server/hello-world" )); }