|
| 1 | +package com.baeldung.spring.cloud.aws.ec2; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import org.junit.Assume; |
| 6 | +import org.junit.Before; |
| 7 | +import org.junit.Test; |
| 8 | +import org.junit.runner.RunWith; |
| 9 | +import org.slf4j.Logger; |
| 10 | +import org.slf4j.LoggerFactory; |
| 11 | +import org.springframework.beans.factory.annotation.Autowired; |
| 12 | +import org.springframework.boot.test.context.SpringBootTest; |
| 13 | +import org.springframework.test.context.TestPropertySource; |
| 14 | +import org.springframework.test.context.junit4.SpringRunner; |
| 15 | + |
| 16 | +import com.amazonaws.regions.Regions; |
| 17 | +import com.amazonaws.services.ec2.AmazonEC2; |
| 18 | + |
| 19 | +@SpringBootTest |
| 20 | +@RunWith(SpringRunner.class) |
| 21 | +@TestPropertySource("classpath:application-test.properties") |
| 22 | +public class EC2MetadataIntegrationTest { |
| 23 | + |
| 24 | + private static final Logger logger = LoggerFactory.getLogger(EC2MetadataIntegrationTest.class); |
| 25 | + |
| 26 | + private boolean serverEc2; |
| 27 | + |
| 28 | + @Before |
| 29 | + public void setUp() { |
| 30 | + serverEc2 = Regions.getCurrentRegion() != null; |
| 31 | + } |
| 32 | + |
| 33 | + @Autowired |
| 34 | + private EC2Metadata eC2Metadata; |
| 35 | + |
| 36 | + @Autowired |
| 37 | + private AmazonEC2 amazonEC2; |
| 38 | + |
| 39 | + @Test |
| 40 | + public void whenEC2ClinentNotNull_thenSuccess() { |
| 41 | + assertThat(amazonEC2).isNotNull(); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void whenEC2MetadataNotNull_thenSuccess() { |
| 46 | + assertThat(eC2Metadata).isNotNull(); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void whenMetdataValuesNotNull_thenSuccess() { |
| 51 | + Assume.assumeTrue(serverEc2); |
| 52 | + assertThat(eC2Metadata.getAmiId()).isNotEqualTo("N/A"); |
| 53 | + assertThat(eC2Metadata.getInstanceType()).isNotEqualTo("N/A"); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void whenMetadataLogged_thenSuccess() { |
| 58 | + logger.info("Environment is EC2: {}", serverEc2); |
| 59 | + logger.info(eC2Metadata.toString()); |
| 60 | + } |
| 61 | +} |
0 commit comments