-
Notifications
You must be signed in to change notification settings - Fork 34
/
UserRepositoryIT.java
136 lines (116 loc) · 4.81 KB
/
UserRepositoryIT.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
* Copyright © 2018 spring-data-dynamodb-example (https://github.com/derjust/spring-data-dynamodb-examples)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.derjust.spring_data_dynamodb_examples.simple;
import java.util.List;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.socialsignin.spring.data.dynamodb.repository.config.EnableDynamoDBRepositories;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit4.SpringRunner;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig;
import com.amazonaws.services.dynamodbv2.model.CreateTableRequest;
import com.amazonaws.services.dynamodbv2.model.DeleteTableRequest;
import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
import com.amazonaws.services.dynamodbv2.util.TableUtils;
import static org.hamcrest.Matchers.*;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {PropertyPlaceholderAutoConfiguration.class, UserRepositoryIT.DynamoDBConfig.class})
public class UserRepositoryIT {
private static final Logger log = LoggerFactory.getLogger(UserRepositoryIT.class);
@Configuration
@EnableDynamoDBRepositories(basePackageClasses = UserRepository.class)
public static class DynamoDBConfig {
@Value("${amazon.aws.accesskey}")
private String amazonAWSAccessKey;
@Value("${amazon.aws.secretkey}")
private String amazonAWSSecretKey;
public AWSCredentialsProvider amazonAWSCredentialsProvider() {
return new AWSStaticCredentialsProvider(amazonAWSCredentials());
}
@Bean
public AWSCredentials amazonAWSCredentials() {
return new BasicAWSCredentials(amazonAWSAccessKey, amazonAWSSecretKey);
}
@Bean
public DynamoDBMapperConfig dynamoDBMapperConfig() {
return DynamoDBMapperConfig.DEFAULT;
}
@Bean
public DynamoDBMapper dynamoDBMapper(AmazonDynamoDB amazonDynamoDB, DynamoDBMapperConfig config) {
return new DynamoDBMapper(amazonDynamoDB, config);
}
@Bean
public AmazonDynamoDB amazonDynamoDB() {
return AmazonDynamoDBClientBuilder.standard().withCredentials(amazonAWSCredentialsProvider())
.withRegion(Regions.US_EAST_1).build();
}
}
@Autowired
private UserRepository repository;
@Test
public void sampleTestCase() {
User gosling = new User("James", "Gosling");
repository.save(gosling);
User hoeller = new User("Juergen", "Hoeller");
repository.save(hoeller);
List<User> result = repository.findByLastName("Gosling");
Assert.assertThat(result.size(), is(1));
Assert.assertThat(result, hasItem(gosling));
log.info("Found in table: {}", result.get(0));
}
@Autowired
private AmazonDynamoDB amazonDynamoDB;
@Autowired
private DynamoDBMapper mapper;
private boolean tableWasCreatedForTest;
@Before
public void init() throws Exception {
CreateTableRequest ctr = mapper.generateCreateTableRequest(User.class)
.withProvisionedThroughput(new ProvisionedThroughput(1L, 1L));
tableWasCreatedForTest = TableUtils.createTableIfNotExists(amazonDynamoDB, ctr);
if (tableWasCreatedForTest) {
log.info("Created table {}", ctr.getTableName());
}
TableUtils.waitUntilActive(amazonDynamoDB, ctr.getTableName());
log.info("Table {} is active", ctr.getTableName());
}
@After
public void destroy() throws Exception {
if (tableWasCreatedForTest) {
DeleteTableRequest dtr = mapper.generateDeleteTableRequest(User.class);
TableUtils.deleteTableIfExists(amazonDynamoDB, dtr);
log.info("Deleted table {}", dtr.getTableName());
}
}
}