[JAVA-21545] Fix test (#14258)
This commit is contained in:
parent
25c2652260
commit
191245c2da
|
@ -1,6 +1,8 @@
|
||||||
package com.baeldung.spring.data.dynamodb.repository;
|
package com.baeldung.spring.data.dynamodb.repository;
|
||||||
|
|
||||||
|
import com.amazonaws.auth.BasicAWSCredentials;
|
||||||
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
|
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
|
||||||
|
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
|
||||||
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
|
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
|
||||||
import com.amazonaws.services.dynamodbv2.model.CreateTableRequest;
|
import com.amazonaws.services.dynamodbv2.model.CreateTableRequest;
|
||||||
import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
|
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.repositories.ProductInfoRepository;
|
||||||
import com.baeldung.spring.data.dynamodb.repository.rule.LocalDbCreationRule;
|
import com.baeldung.spring.data.dynamodb.repository.rule.LocalDbCreationRule;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
import org.junit.ClassRule;
|
import org.junit.ClassRule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.test.context.ActiveProfiles;
|
import org.springframework.test.context.ActiveProfiles;
|
||||||
import org.springframework.test.context.TestPropertySource;
|
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
import org.springframework.test.context.web.WebAppConfiguration;
|
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.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.greaterThan;
|
import static org.hamcrest.Matchers.greaterThan;
|
||||||
import static org.hamcrest.core.Is.is;
|
import static org.hamcrest.core.Is.is;
|
||||||
|
@ -31,23 +40,40 @@ import static org.junit.Assert.assertThat;
|
||||||
@SpringBootTest(classes = Application.class)
|
@SpringBootTest(classes = Application.class)
|
||||||
@WebAppConfiguration
|
@WebAppConfiguration
|
||||||
@ActiveProfiles("local")
|
@ActiveProfiles("local")
|
||||||
@TestPropertySource(properties = { "amazon.dynamodb.endpoint=http://localhost:8000/", "amazon.aws.accesskey=test1", "amazon.aws.secretkey=test231" })
|
|
||||||
public class ProductInfoRepositoryIntegrationTest {
|
public class ProductInfoRepositoryIntegrationTest {
|
||||||
|
|
||||||
@ClassRule
|
@ClassRule
|
||||||
public static LocalDbCreationRule dynamoDB = new LocalDbCreationRule();
|
public static LocalDbCreationRule dynamoDB = new LocalDbCreationRule();
|
||||||
|
private static DynamoDBMapper dynamoDBMapper;
|
||||||
private DynamoDBMapper dynamoDBMapper;
|
private static AmazonDynamoDB amazonDynamoDB;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private AmazonDynamoDB amazonDynamoDB;
|
private ProductInfoRepository repository;
|
||||||
|
|
||||||
@Autowired
|
private static final String DYNAMODB_ENDPOINT = "amazon.dynamodb.endpoint";
|
||||||
ProductInfoRepository repository;
|
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_COST = "20";
|
||||||
private static final String EXPECTED_PRICE = "50";
|
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
|
@Before
|
||||||
public void setup() throws Exception {
|
public void setup() throws Exception {
|
||||||
|
|
||||||
|
@ -77,4 +103,28 @@ public class ProductInfoRepositoryIntegrationTest {
|
||||||
assertThat(result.size(), is(greaterThan(0)));
|
assertThat(result.size(), is(greaterThan(0)));
|
||||||
assertThat(result.get(0).getCost(), is(equalTo(EXPECTED_COST)));
|
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) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue