AmiIdProviderImpl implements AmiIdProvider { @Override public Optional<String> apply(final EC2InstanceContext context) { return cache.getUnchecked(context); }  @Override Optional<String> apply(final EC2InstanceContext context);  }
@Test public void testAmiIdFound() throws Exception { when(ec2InstanceContextMock.getInstanceJson()).thenReturn("{\"imageId\":\"123\"}"); final Optional<String> result = amiIdProvider.apply(ec2InstanceContextMock); assertThat(result).isPresent(); verify(ec2InstanceContextMock).getInstanceJson(); }
@Test public void testAmiIdNotFound() throws Exception { when(ec2InstanceContextMock.getInstanceJson()).thenReturn("{json here"); when(ec2InstanceContextMock.getInstanceId()).thenReturn(INSTANCE_ID); when(ec2InstanceContextMock.getClient(eq(AmazonEC2Client.class))).thenReturn(amazonEC2ClientMock); final DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest().withInstanceIds(INSTANCE_ID); when(amazonEC2ClientMock.describeInstances(eq(describeInstancesRequest))) .thenReturn(new DescribeInstancesResult() .withReservations(newArrayList( new Reservation().withInstances(newArrayList( new Instance() .withInstanceId(INSTANCE_ID) .withImageId(IMAGE_ID) )) ))); final Optional<String> result = amiIdProvider.apply(ec2InstanceContextMock); assertThat(result).isPresent(); verify(ec2InstanceContextMock).getInstanceJson(); verify(ec2InstanceContextMock).getInstanceId(); verify(ec2InstanceContextMock).getClient(eq(AmazonEC2Client.class)); verify(amazonEC2ClientMock).describeInstances(eq(describeInstancesRequest)); }
@Test public void testAmiIdNotFoundInAWS() throws Exception { when(ec2InstanceContextMock.getInstanceJson()).thenReturn("{json here"); when(ec2InstanceContextMock.getInstanceId()).thenReturn(INSTANCE_ID); when(ec2InstanceContextMock.getClient(eq(AmazonEC2Client.class))).thenReturn(amazonEC2ClientMock); final DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest().withInstanceIds(INSTANCE_ID); when(amazonEC2ClientMock.describeInstances(eq(describeInstancesRequest))) .thenReturn(new DescribeInstancesResult() .withReservations(newArrayList( new Reservation().withInstances(newArrayList( new Instance() .withInstanceId("another id") .withImageId(IMAGE_ID) )) ))); final Optional<String> result = amiIdProvider.apply(ec2InstanceContextMock); assertThat(result).isEmpty(); verify(ec2InstanceContextMock).getInstanceJson(); verify(ec2InstanceContextMock).getInstanceId(); verify(ec2InstanceContextMock).getClient(eq(AmazonEC2Client.class)); verify(amazonEC2ClientMock).describeInstances(eq(describeInstancesRequest)); }
@Test public void testAmiIdAWSException() throws Exception { when(ec2InstanceContextMock.getInstanceJson()).thenReturn("{json here"); when(ec2InstanceContextMock.getInstanceId()).thenReturn(INSTANCE_ID); when(ec2InstanceContextMock.getClient(eq(AmazonEC2Client.class))) .thenThrow(new AmazonClientException("oops, I did it again... Britney")); final Optional<String> result = amiIdProvider.apply(ec2InstanceContextMock); assertThat(result).isEmpty(); verify(ec2InstanceContextMock).getInstanceJson(); verify(ec2InstanceContextMock).getInstanceId(); verify(ec2InstanceContextMock).getClient(eq(AmazonEC2Client.class)); }