final Caller caller = this.populator().getCaller();
// 1. first start a basic single VM with a clientToken
final CreateRequest request1 = this.populator().getIdempotentCreateRequest("suite:basic:idempotency", token);
final CreateResult result1 = rm.create(request1, caller);
logger.info("Leased vm '" + result1.getVMs()[0].getID() + '\'');
assertEquals(token, result1.getVMs()[0].getClientToken());
// 2. now re-launch with the same clientToken, should get back the same instance
// (and no new instance should have been started)
final CreateRequest request2 = this.populator().getIdempotentCreateRequest("suite:basic:idempotency", token);
final CreateResult result2 = rm.create(request2, caller);
assertEquals(result1.getVMs()[0].getID(), result2.getVMs()[0].getID());
assertEquals(token, result2.getVMs()[0].getClientToken());
logger.info("Leased same vm '" + result2.getVMs()[0].getID() + '\'');
assertEquals(1, rm.getAllByCaller(caller).length);
// 3. Now launch a VM with the same parameters but different clientToken --
// should get a new VM
String anotherToken = UUID.randomUUID().toString();
final CreateRequest request3 = this.populator().getIdempotentCreateRequest("suite:basic:idempotency", anotherToken);
final CreateResult result3 = rm.create(request3, caller);
logger.info("Leased vm '" + result3.getVMs()[0].getID() + '\'');
assertNotSame(result1.getVMs()[0].getID(), result3.getVMs()[0].getID());
assertEquals(2, rm.getAllByCaller(caller).length);
// 4. Kill the first VM and then attempt to re-launch with its same clientToken.
// We should get back a terminated state response with the original ID
rm.trash(result1.getVMs()[0].getID(), Manager.INSTANCE, caller);
assertEquals(1, rm.getAllByCaller(caller).length);
final CreateRequest request4 = this.populator().getIdempotentCreateRequest("suite:basic:idempotency", token);
final CreateResult result4 = rm.create(request4, caller);
logger.info("Leased same vm (terminated) '" + result4.getVMs()[0].getID() + '\'');
assertEquals(result1.getVMs()[0].getID(), result2.getVMs()[0].getID());
assertEquals(State.STATE_Cancelled, result4.getVMs()[0].getState().getState());
assertEquals(token, result4.getVMs()[0].getClientToken());