TaupageExpirationTimeProviderImpl implements TaupageExpirationTimeProvider { @Override @Cacheable(cacheNames = "ami-expiration-time", cacheManager = "oneMinuteTTLCacheManager") public ZonedDateTime getExpirationTime(String regionName, String imageOwner, String imageId) { final AmazonEC2Client ec2 = clientProvider.getClient(AmazonEC2Client.class, imageOwner, Region.getRegion(Regions.fromName(regionName))); final DescribeTagsRequest tagsRequest = new DescribeTagsRequest().withFilters( new Filter("resource-id").withValues(imageId), new Filter("resource-type").withValues("image"), new Filter("key").withValues(TAG_KEY)); return ec2.describeTags(tagsRequest).getTags().stream() .findFirst() .map(TagDescription::getValue) .map(value -> ZonedDateTime.parse(value, ISO_DATE_TIME)) .orElse(null); } TaupageExpirationTimeProviderImpl(ClientProvider clientProvider); @Override @Cacheable(cacheNames = "ami-expiration-time", cacheManager = "oneMinuteTTLCacheManager") ZonedDateTime getExpirationTime(String regionName, String imageOwner, String imageId);  }
@Test public void getExpirationTime() { final DescribeTagsResult response = new DescribeTagsResult() .withTags(new TagDescription() .withResourceType("image") .withResourceId(IMAGE_ID) .withKey(TaupageExpirationTimeProviderImpl.TAG_KEY) .withValue("2018-06-20T03:00:00+02:00")); when(mockEC2Client.describeTags(any(DescribeTagsRequest.class))).thenReturn(response); final ZonedDateTime result = expirationTimeProvider.getExpirationTime(REGION_NAME, IMAGE_OWNER, IMAGE_ID); assertThat(result).isEqualTo(ZonedDateTime.of(2018, 6, 20, 3, 0, 0, 0, ZoneOffset.ofHours(2))); verify(mockClientProvider).getClient(eq(AmazonEC2Client.class), eq(IMAGE_OWNER), eq(getRegion(fromName(REGION_NAME)))); verify(mockEC2Client).describeTags( eq(new DescribeTagsRequest().withFilters( new Filter("resource-id").withValues(IMAGE_ID), new Filter("resource-type").withValues("image"), new Filter("key").withValues(TaupageExpirationTimeProviderImpl.TAG_KEY)))); }
@Test public void getAbsentExpirationTime() { when(mockEC2Client.describeTags(any(DescribeTagsRequest.class))).thenReturn(new DescribeTagsResult()); final ZonedDateTime result = expirationTimeProvider.getExpirationTime(REGION_NAME, IMAGE_OWNER, IMAGE_ID); assertThat(result).isNull(); verify(mockClientProvider).getClient(eq(AmazonEC2Client.class), eq(IMAGE_OWNER), eq(getRegion(fromName(REGION_NAME)))); verify(mockEC2Client).describeTags( eq(new DescribeTagsRequest().withFilters( new Filter("resource-id").withValues(IMAGE_ID), new Filter("resource-type").withValues("image"), new Filter("key").withValues(TaupageExpirationTimeProviderImpl.TAG_KEY)))); }