ConfiguratorOnCreation implements MachineConfigurator { void updateAgentConfigurationFile( TargetHandlerParameters parameters, SSHClient ssh, File tmpDir, Map<String,String> keyToNewValue ) throws IOException { this.logger.fine( "Updating agent parameters on remote host..." ); String agentConfigDir = Utils.getValue( parameters.getTargetProperties(), SCP_AGENT_CONFIG_DIR, DEFAULT_SCP_AGENT_CONFIG_DIR ); File localAgentConfig = new File( tmpDir, Constants.KARAF_CFG_FILE_AGENT ); File remoteAgentConfig = new File( agentConfigDir, Constants.KARAF_CFG_FILE_AGENT ); ssh.newSCPFileTransfer().download(remoteAgentConfig.getCanonicalPath(), new FileSystemFile(tmpDir)); String config = Utils.readFileContent(localAgentConfig); config = Utils.updateProperties( config, keyToNewValue ); Utils.writeStringInto(config, localAgentConfig); ssh.newSCPFileTransfer().upload(new FileSystemFile(localAgentConfig), agentConfigDir); } ConfiguratorOnCreation(
			TargetHandlerParameters parameters,
			String ip,
			String machineId,
			EmbeddedHandler embedded ); @Override void close(); @Override boolean configure(); @Override Instance getScopedInstance();  }
@Test public void testUpdateAgentConfigurationFile() throws Exception { File tmpDir = this.folder.newFolder(); File agentConfigurationFile = new File( tmpDir, Constants.KARAF_CFG_FILE_AGENT ); Properties props = new Properties(); props.setProperty( "a0", "c0" ); props.setProperty( "a1", "c213" ); props.setProperty( "a2", "c2" ); props.setProperty( "a3", "c3" ); props.setProperty( "a4", "c4" ); props.setProperty( "a5", "c5" ); Utils.writePropertiesFile( props, agentConfigurationFile ); TargetHandlerParameters parameters = new TargetHandlerParameters() .targetProperties( new HashMap<String,String>( 0 )); final Map<String,String> keyToNewValue = new HashMap<> (); keyToNewValue.put( "a1", "b1" ); keyToNewValue.put( "a2", "b2" ); keyToNewValue.put( "a3", "b3" ); SSHClient ssh = Mockito.mock( SSHClient.class ); SCPFileTransfer scp = Mockito.mock( SCPFileTransfer.class ); Mockito.when( ssh.newSCPFileTransfer()).thenReturn( scp ); EmbeddedHandler embedded = new EmbeddedHandler(); embedded.karafData = this.folder.newFolder().getAbsolutePath(); ConfiguratorOnCreation configurator = new ConfiguratorOnCreation( parameters, "ip", "machineId", embedded ); configurator.updateAgentConfigurationFile( parameters, ssh, tmpDir, keyToNewValue ); ArgumentCaptor<String> remotePathCaptor = ArgumentCaptor.forClass( String.class ); ArgumentCaptor<FileSystemFile> fileCaptor = ArgumentCaptor.forClass( FileSystemFile.class ); Mockito.verify( scp ).download( remotePathCaptor.capture(), fileCaptor.capture()); Assert.assertEquals( tmpDir, fileCaptor.getValue().getFile()); Assert.assertEquals( new File( DEFAULT_SCP_AGENT_CONFIG_DIR, Constants.KARAF_CFG_FILE_AGENT ).getAbsolutePath(), remotePathCaptor.getValue()); ArgumentCaptor<String> remotePathCaptor2 = ArgumentCaptor.forClass( String.class ); ArgumentCaptor<FileSystemFile> fileCaptor2 = ArgumentCaptor.forClass( FileSystemFile.class ); Mockito.verify( scp ).upload( fileCaptor2.capture(), remotePathCaptor2.capture()); Assert.assertEquals( agentConfigurationFile.getAbsolutePath(), fileCaptor2.getValue().getFile().getAbsolutePath()); Assert.assertEquals( DEFAULT_SCP_AGENT_CONFIG_DIR, remotePathCaptor2.getValue()); Mockito.verifyNoMoreInteractions( scp ); Mockito.verify( ssh, Mockito.times( 2 )).newSCPFileTransfer(); Mockito.verifyNoMoreInteractions( ssh ); Properties readProps = Utils.readPropertiesFile( agentConfigurationFile ); Assert.assertEquals( "c0", readProps.get( "a0" )); Assert.assertEquals( "b1", readProps.get( "a1" )); Assert.assertEquals( "b2", readProps.get( "a2" )); Assert.assertEquals( "b3", readProps.get( "a3" )); Assert.assertEquals( "c4", readProps.get( "a4" )); Assert.assertEquals( "c5", readProps.get( "a5" )); Assert.assertEquals( props.size(), readProps.size()); configurator.close(); }