DefineVariableCommandInstruction extends AbstractCommandInstruction { @Override public void updateContext() { String newValue = replaceVariables( this.value, this.context, this.instancePath ); if( newValue != null ) { this.context.variables.put( this.key, newValue ); } else { setDisabled( true ); this.context.disabledVariables.add( this.key ); this.context.variables.put( this.key, "fake" ); String path = "/fake"; if( this.instancePath != null ) path = this.instancePath + path; this.context.instancePathToComponentName.put( path, FAKE_COMPONENT_NAME ); } } DefineVariableCommandInstruction( Context context, String instruction, int line ); @Override List<ParsingError> doValidate(); @Override void updateContext(); static final String MILLI_TIME; static final String NANO_TIME; static final String FORMATTED_TIME_PREFIX; static final String EXISTING_INDEX_PREFIX; static final String RANDOM_UUID; static final String SMART_INDEX; }
@Test public void testUpdateContext() { String instr1 = "define key = toto"; String instr2 = "define key2 = $(key)2"; String instr3 = "define key2 = $(key) 2"; String instr4 = "define nano = " + NANO_TIME; String instr5 = "define milli = " + MILLI_TIME; String instr6 = "define uuid = " + RANDOM_UUID; String instr7 = "define indexWithoutInstance = oops " + SMART_INDEX; String instr8 = "define index = oops " + SMART_INDEX + " under /tomcat-vm"; String instr9 = "define tt = t " + SMART_INDEX + " t under /tomcat-vm"; String instr10 = "define ftime1 = " + FORMATTED_TIME_PREFIX + "HH:mm:ss )"; String instr11 = "define ftime2 = " + FORMATTED_TIME_PREFIX + "MMMM)"; String instr12 = "define ftime3 = something happenned at $(ftime1)"; String instr13 = "define ftime4 = " + FORMATTED_TIME_PREFIX + "'today at' HH:mm:ss )"; DefineVariableCommandInstruction instr = new DefineVariableCommandInstruction( this.context, instr1, 1 ); Assert.assertFalse( instr.isDisabled()); Assert.assertEquals( 0, instr.validate().size()); instr.updateContext(); Assert.assertEquals( "toto", this.context.variables.get( "key" )); instr = new DefineVariableCommandInstruction( this.context, instr2, 1 ); Assert.assertFalse( instr.isDisabled()); Assert.assertEquals( 0, instr.validate().size()); instr.updateContext(); Assert.assertEquals( "toto2", this.context.variables.get( "key2" )); instr = new DefineVariableCommandInstruction( this.context, instr3, 1 ); Assert.assertFalse( instr.isDisabled()); Assert.assertEquals( 0, instr.validate().size()); instr.updateContext(); Assert.assertEquals( "toto 2", this.context.variables.get( "key2" )); instr = new DefineVariableCommandInstruction( this.context, instr4, 1 ); Assert.assertFalse( instr.isDisabled()); Assert.assertEquals( 0, instr.validate().size()); instr.updateContext(); Assert.assertNotNull( this.context.variables.get( "nano" )); Assert.assertFalse( NANO_TIME.equals( this.context.variables.get( "nano" ))); instr = new DefineVariableCommandInstruction( this.context, instr5, 1 ); Assert.assertFalse( instr.isDisabled()); Assert.assertEquals( 0, instr.validate().size()); instr.updateContext(); Assert.assertNotNull( this.context.variables.get( "milli" )); Assert.assertFalse( MILLI_TIME.equals( this.context.variables.get( "milli" ))); instr = new DefineVariableCommandInstruction( this.context, instr6, 1 ); Assert.assertFalse( instr.isDisabled()); Assert.assertEquals( 0, instr.validate().size()); instr.updateContext(); Assert.assertNotNull( this.context.variables.get( "uuid" )); Assert.assertFalse( RANDOM_UUID.equals( this.context.variables.get( "uuid" ))); instr = new DefineVariableCommandInstruction( this.context, instr7, 1 ); Assert.assertFalse( instr.isDisabled()); Assert.assertEquals( 0, instr.validate().size()); instr.updateContext(); Assert.assertNotNull( this.context.variables.get( "indexWithoutInstance" )); Assert.assertEquals( "oops 1", this.context.variables.get( "indexWithoutInstance" )); instr = new DefineVariableCommandInstruction( this.context, instr9, 1 ); Assert.assertFalse( instr.isDisabled()); Assert.assertEquals( 0, instr.validate().size()); instr.updateContext(); Assert.assertNotNull( this.context.variables.get( "tt" )); Assert.assertEquals( "t 1 t", this.context.variables.get( "tt" )); instr = new DefineVariableCommandInstruction( this.context, instr10, 1 ); Assert.assertFalse( instr.isDisabled()); Assert.assertEquals( 0, instr.validate().size()); instr.updateContext(); Assert.assertNotNull( this.context.variables.get( "ftime1" )); Assert.assertNotEquals( "HH:mm:ss", this.context.variables.get( "ftime1" )); Assert.assertTrue( this.context.variables.get( "ftime1" ).matches( "\\d\\d:\\d\\d:\\d\\d" )); instr = new DefineVariableCommandInstruction( this.context, instr11, 1 ); Assert.assertFalse( instr.isDisabled()); Assert.assertEquals( 0, instr.validate().size()); instr.updateContext(); Assert.assertNotNull( this.context.variables.get( "ftime2" )); Assert.assertNotEquals( "MMM", this.context.variables.get( "ftime2" )); Assert.assertTrue( this.context.variables.get( "ftime2" ).matches( "[a-zA-Z]+.*" )); instr = new DefineVariableCommandInstruction( this.context, instr12, 1 ); Assert.assertFalse( instr.isDisabled()); Assert.assertEquals( 0, instr.validate().size()); instr.updateContext(); Assert.assertEquals( "something happenned at " + this.context.variables.get( "ftime1" ), this.context.variables.get( "ftime3" )); instr = new DefineVariableCommandInstruction( this.context, instr13, 1 ); Assert.assertFalse( instr.isDisabled()); Assert.assertEquals( 0, instr.validate().size()); instr.updateContext(); Assert.assertNotNull( this.context.variables.get( "ftime4" )); Assert.assertTrue( this.context.variables.get( "ftime4" ).matches( "today at \\d\\d:\\d\\d:\\d\\d" )); instr = new DefineVariableCommandInstruction( this.context, instr8, 1 ); Assert.assertFalse( instr.isDisabled()); Assert.assertEquals( 0, instr.validate().size()); instr.updateContext(); Assert.assertNotNull( this.context.variables.get( "index" )); Assert.assertEquals( "oops 1", this.context.variables.get( "index" )); instr.updateContext(); Assert.assertNotNull( this.context.variables.get( "index" )); Assert.assertEquals( "oops 1", this.context.variables.get( "index" )); this.context.instancePathToComponentName.put( "/tomcat-vm/oops 1", this.app.getTomcatVm().getComponent().getName()); instr.updateContext(); Assert.assertNotNull( this.context.variables.get( "index" )); Assert.assertEquals( "oops 2", this.context.variables.get( "index" )); this.context.instancePathToComponentName.put( "/tomcat-vm/oops 2", this.app.getTomcatVm().getComponent().getName()); instr.updateContext(); Assert.assertNotNull( this.context.variables.get( "index" )); Assert.assertEquals( "oops 3", this.context.variables.get( "index" )); instr.updateContext(); Assert.assertNotNull( this.context.variables.get( "index" )); Assert.assertEquals( "oops 3", this.context.variables.get( "index" )); }
@Test public void testUpdateContext_queryIndexes() { this.context.instancePathToComponentName.put( "/tomcat-vm/oops 1", this.app.getTomcatVm().getComponent().getName()); this.context.instancePathToComponentName.put( "/tomcat-vm/oops 2", this.app.getTomcatVm().getComponent().getName()); this.context.instancePathToComponentName.put( "/tomcat-vm/oops 3", this.app.getTomcatVm().getComponent().getName()); String instr14 = "define exi = oops " + EXISTING_INDEX_PREFIX + " MIN ) under /tomcat-vm"; AbstractCommandInstruction instr = new DefineVariableCommandInstruction( this.context, instr14, 1 ); Assert.assertEquals( 0, instr.validate().size()); instr.updateContext(); Assert.assertFalse( instr.isDisabled()); Assert.assertNotNull( this.context.variables.get( "exi" )); Assert.assertEquals( "oops 1", this.context.variables.get( "exi" )); String instr15 = "define exi = oops " + EXISTING_INDEX_PREFIX + " MIN > 1 ) under /tomcat-vm"; instr = new DefineVariableCommandInstruction( this.context, instr15, 1 ); Assert.assertEquals( 0, instr.validate().size()); instr.updateContext(); Assert.assertFalse( instr.isDisabled()); Assert.assertNotNull( this.context.variables.get( "exi" )); Assert.assertEquals( "oops 2", this.context.variables.get( "exi" )); String instr16 = "define exi = oops " + EXISTING_INDEX_PREFIX + " MIN > 2 ) under /tomcat-vm"; instr = new DefineVariableCommandInstruction( this.context, instr16, 1 ); Assert.assertEquals( 0, instr.validate().size()); instr.updateContext(); Assert.assertFalse( instr.isDisabled()); Assert.assertNotNull( this.context.variables.get( "exi" )); Assert.assertEquals( "oops 3", this.context.variables.get( "exi" )); String instr17 = "define exi = oops " + EXISTING_INDEX_PREFIX + " MIN > 3 ) under /tomcat-vm"; instr = new DefineVariableCommandInstruction( this.context, instr17, 1 ); Assert.assertEquals( 0, instr.validate().size()); instr.updateContext(); Assert.assertTrue( instr.isDisabled()); Assert.assertNotNull( this.context.variables.get( "exi" )); Assert.assertEquals( "fake", this.context.variables.get( "exi" )); String instr18 = "define exi = oops " + EXISTING_INDEX_PREFIX + " MAX ) under /tomcat-vm"; instr = new DefineVariableCommandInstruction( this.context, instr18, 1 ); Assert.assertEquals( 0, instr.validate().size()); instr.updateContext(); Assert.assertFalse( instr.isDisabled()); Assert.assertNotNull( this.context.variables.get( "exi" )); Assert.assertEquals( "oops 3", this.context.variables.get( "exi" )); String instr19 = "define exi = oops " + EXISTING_INDEX_PREFIX + " MAX > 1 ) under /tomcat-vm"; instr = new DefineVariableCommandInstruction( this.context, instr19, 1 ); Assert.assertEquals( 0, instr.validate().size()); instr.updateContext(); Assert.assertFalse( instr.isDisabled()); Assert.assertNotNull( this.context.variables.get( "exi" )); Assert.assertEquals( "oops 3", this.context.variables.get( "exi" )); String instr20 = "define exi = oops " + EXISTING_INDEX_PREFIX + " MAX < 3 ) under /tomcat-vm"; instr = new DefineVariableCommandInstruction( this.context, instr20, 1 ); Assert.assertEquals( 0, instr.validate().size()); instr.updateContext(); Assert.assertFalse( instr.isDisabled()); Assert.assertNotNull( this.context.variables.get( "exi" )); Assert.assertEquals( "oops 2", this.context.variables.get( "exi" )); String instr21 = "define exi = oops " + EXISTING_INDEX_PREFIX + " MAX < 2 ) under /tomcat-vm"; instr = new DefineVariableCommandInstruction( this.context, instr21, 1 ); Assert.assertEquals( 0, instr.validate().size()); instr.updateContext(); Assert.assertFalse( instr.isDisabled()); Assert.assertNotNull( this.context.variables.get( "exi" )); Assert.assertEquals( "oops 1", this.context.variables.get( "exi" )); String instr22 = "define exi = oops " + EXISTING_INDEX_PREFIX + " MAX > 3 ) under /tomcat-vm"; instr = new DefineVariableCommandInstruction( this.context, instr22, 1 ); Assert.assertEquals( 0, instr.validate().size()); instr.updateContext(); Assert.assertTrue( instr.isDisabled()); Assert.assertNotNull( this.context.variables.get( "exi" )); Assert.assertEquals( "fake", this.context.variables.get( "exi" )); Assert.assertEquals( 1, this.context.disabledVariables.size()); Assert.assertEquals( "exi", this.context.disabledVariables.iterator().next()); }
@Test public void testUpdateContext_forSmartIndex_1() { String line = "define vm id = " + SMART_INDEX; DefineVariableCommandInstruction instr = new DefineVariableCommandInstruction( this.context, line, 1 ); Assert.assertEquals( 0, instr.validate().size()); instr.updateContext(); Assert.assertEquals( "1", this.context.variables.get( "vm id" )); }
@Test public void testUpdateContext_forSmartIndex_2() { String line = "define vm id = " + SMART_INDEX + " under /tomcat-vm"; DefineVariableCommandInstruction instr = new DefineVariableCommandInstruction( this.context, line, 1 ); Assert.assertEquals( 0, instr.validate().size()); instr.updateContext(); Assert.assertEquals( "1", this.context.variables.get( "vm id" )); }
@Test public void testUpdateContext_forSmartIndex_3() { String line = "define war id = t" + SMART_INDEX + " under /tomcat-vm"; DefineVariableCommandInstruction instr = new DefineVariableCommandInstruction( this.context, line, 1 ); Assert.assertEquals( 0, instr.validate().size()); instr.updateContext(); Assert.assertEquals( "t1", this.context.variables.get( "war id" )); line = "Create tomcat as t1 under /tomcat-vm"; CreateInstanceCommandInstruction ci = new CreateInstanceCommandInstruction( this.context, line, 1 ); Assert.assertEquals( 0, ci.validate().size()); ci.updateContext(); line = "define war id = t" + SMART_INDEX + " under /tomcat-vm"; instr = new DefineVariableCommandInstruction( this.context, line, 1 ); Assert.assertEquals( 0, instr.validate().size()); instr.updateContext(); Assert.assertEquals( "t2", this.context.variables.get( "war id" )); }