FetchTaupageYamlImpl implements FetchTaupageYaml { @Override public Optional<TaupageYaml> getTaupageYaml(final String instanceId, final String account, final String region) { final AmazonEC2Client client = clientProvider.getClient(AmazonEC2Client.class, account, Region.getRegion(Regions.fromName(region))); try { final DescribeInstanceAttributeResult response = client.describeInstanceAttribute( new DescribeInstanceAttributeRequest() .withInstanceId(instanceId) .withAttribute(USER_DATA)); return ofNullable(response) .map(DescribeInstanceAttributeResult::getInstanceAttribute) .map(InstanceAttribute::getUserData) .map(Base64::decode) .map(String::new) .map(TaupageYamlUtil::parseTaupageYaml); } catch (final AmazonClientException e) { log.warn("Could not get Taupage YAML for instance: " + instanceId, e); return empty(); } catch (YAMLException | IllegalArgumentException s) { log.warn("Taupage YAML is not valid for instance: " + instanceId, s); return empty(); } } @Autowired  FetchTaupageYamlImpl(final ClientProvider clientProvider); @Override Optional<TaupageYaml> getTaupageYaml(final String instanceId, final String account, final String region);  }
@Test public void testGetTaupageYaml() throws Exception { when(amazonEC2ClientMock.describeInstanceAttribute(any())).thenReturn(new DescribeInstanceAttributeResult(). withInstanceAttribute(new InstanceAttribute() .withUserData(Base64.encodeAsString("blub: fdsa".getBytes())))); final FetchTaupageYaml fetchTaupageYaml = new FetchTaupageYamlImpl(clientProviderMock); final Optional<TaupageYaml> result = fetchTaupageYaml.getTaupageYaml(INSTANCE_ID, ACCOUNT, REGION); assertThat(result).isPresent(); verify(amazonEC2ClientMock).describeInstanceAttribute(any()); }
@Test public void testBrokenYaml() throws Exception{ final String yamlData = "- a\n- b\n- c\n"; when(amazonEC2ClientMock.describeInstanceAttribute(any())).thenReturn(new DescribeInstanceAttributeResult(). withInstanceAttribute(new InstanceAttribute() .withUserData(Base64.encodeAsString(yamlData.getBytes())))); final FetchTaupageYaml fetchTaupageYaml = new FetchTaupageYamlImpl(clientProviderMock); final Optional<TaupageYaml> result = fetchTaupageYaml.getTaupageYaml(INSTANCE_ID, ACCOUNT, REGION); assertThat(result).isEmpty(); verify(amazonEC2ClientMock).describeInstanceAttribute(any()); }