@Test
public void testUpdateAgentConfigurationFile() throws Exception {

	// Prepare the mocks
	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 );
	// Invoke the method
	EmbeddedHandler embedded = new EmbeddedHandler();
	embedded.karafData = this.folder.newFolder().getAbsolutePath();
	ConfiguratorOnCreation configurator = new ConfiguratorOnCreation( parameters, "ip", "machineId", embedded );
	configurator.updateAgentConfigurationFile( parameters, ssh, tmpDir, keyToNewValue );
	// Verify
	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());
	// 1st: we upload the user data
	// 2nd: we reupload the same file than the one we downloaded
	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());
	// And no additional call
	Mockito.verifyNoMoreInteractions( scp );
	Mockito.verify( ssh, Mockito.times( 2 )).newSCPFileTransfer();
	Mockito.verifyNoMoreInteractions( ssh );
	// Verify the properties were correctly updated in the file
	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());
	// Prevent a compilation warning about leaks
	configurator.close();
}