[JAVA-21545] Fix test (#14258)

This commit is contained in:
panos-kakos 2023-06-18 11:16:45 +03:00 committed by GitHub
parent 25c2652260
commit 191245c2da
1 changed files with 57 additions and 7 deletions

View File

@ -1,6 +1,8 @@
package com.baeldung.spring.data.dynamodb.repository;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import com.amazonaws.services.dynamodbv2.model.CreateTableRequest;
import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
@ -10,17 +12,24 @@ import com.baeldung.spring.data.dynamodb.model.ProductInfo;
import com.baeldung.spring.data.dynamodb.repositories.ProductInfoRepository;
import com.baeldung.spring.data.dynamodb.repository.rule.LocalDbCreationRule;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Optional;
import java.util.Properties;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.core.Is.is;
@ -31,23 +40,40 @@ import static org.junit.Assert.assertThat;
@SpringBootTest(classes = Application.class)
@WebAppConfiguration
@ActiveProfiles("local")
@TestPropertySource(properties = { "amazon.dynamodb.endpoint=http://localhost:8000/", "amazon.aws.accesskey=test1", "amazon.aws.secretkey=test231" })
public class ProductInfoRepositoryIntegrationTest {
@ClassRule
public static LocalDbCreationRule dynamoDB = new LocalDbCreationRule();
private DynamoDBMapper dynamoDBMapper;
private static DynamoDBMapper dynamoDBMapper;
private static AmazonDynamoDB amazonDynamoDB;
@Autowired
private AmazonDynamoDB amazonDynamoDB;
private ProductInfoRepository repository;
@Autowired
ProductInfoRepository repository;
private static final String DYNAMODB_ENDPOINT = "amazon.dynamodb.endpoint";
private static final String AWS_ACCESSKEY = "amazon.aws.accesskey";
private static final String AWS_SECRETKEY = "amazon.aws.secretkey";
private static final String EXPECTED_COST = "20";
private static final String EXPECTED_PRICE = "50";
@BeforeClass
public static void setupClass() {
Properties testProperties = loadFromFileInClasspath("application.properties")
.filter(properties -> !isEmpty(properties.getProperty(AWS_ACCESSKEY)))
.filter(properties -> !isEmpty(properties.getProperty(AWS_SECRETKEY)))
.filter(properties -> !isEmpty(properties.getProperty(DYNAMODB_ENDPOINT)))
.orElseThrow(() -> new RuntimeException("Unable to get all of the required test property values"));
String amazonAWSAccessKey = testProperties.getProperty(AWS_ACCESSKEY);
String amazonAWSSecretKey = testProperties.getProperty(AWS_SECRETKEY);
String amazonDynamoDBEndpoint = testProperties.getProperty(DYNAMODB_ENDPOINT);
amazonDynamoDB = new AmazonDynamoDBClient(new BasicAWSCredentials(amazonAWSAccessKey, amazonAWSSecretKey));
amazonDynamoDB.setEndpoint(amazonDynamoDBEndpoint);
dynamoDBMapper = new DynamoDBMapper(amazonDynamoDB);
}
@Before
public void setup() throws Exception {
@ -77,4 +103,28 @@ public class ProductInfoRepositoryIntegrationTest {
assertThat(result.size(), is(greaterThan(0)));
assertThat(result.get(0).getCost(), is(equalTo(EXPECTED_COST)));
}
private static boolean isEmpty(String inputString) {
return inputString == null || "".equals(inputString);
}
private static Optional<Properties> loadFromFileInClasspath(String fileName) {
InputStream stream = null;
try {
Properties config = new Properties();
Path configLocation = Paths.get(ClassLoader.getSystemResource(fileName).toURI());
stream = Files.newInputStream(configLocation);
config.load(stream);
return Optional.of(config);
} catch (Exception e) {
return Optional.empty();
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
}
}
}
}
}