@@ -142,7 +142,33 @@ public class User {
142142 return lastName;
143143 }
144144
145- // Also add setters
145+ public void setId (String id ) {
146+ this . id = id;
147+ }
148+
149+ public void setFirstName (String firstName ) {
150+ this . firstName = firstName;
151+ }
152+
153+ public void setLastName (String lastName ) {
154+ this . lastName = lastName;
155+ }
156+
157+ @Override
158+ public boolean equals (Object o ) {
159+ if (this == o) return true ;
160+ if (o == null || getClass() != o. getClass()) return false ;
161+
162+ User user = (User ) o;
163+
164+ return id. equals(user. id);
165+
166+ }
167+
168+ @Override
169+ public int hashCode () {
170+ return id. hashCode();
171+ }
146172}
147173```
148174
@@ -177,22 +203,57 @@ And finally write a test client
177203@RunWith (SpringJUnit4ClassRunner . class)
178204@SpringApplicationConfiguration (classes = {
179205 PropertyPlaceholderAutoConfiguration . class, DynamoDBConfig . class})
180- public class UserRepositoryIntegrationTest {
181-
182- @Autowired UserRepository repository;
206+ public class UserRepositoryIntegrationTest {
183207
184- @Test
185- public void sampleTestCase () {
186- User dave = new User (" Dave" , " Matthews" );
187- repository. save(user);
188-
189- User carter = new User (" Carter" , " Beauford" );
190- repository. save(carter);
191-
192- List<User > result = repository. findByLastName(" Matthews" );
193- assertThat(result. size(), is(1 ));
194- assertThat(result, hasItem(dave));
195- }
208+ private static final String KEY_NAME = " id" ;
209+ private static final Long READ_CAPACITY_UNITS = 5L ;
210+ private static final Long WRITE_CAPACITY_UNITS = 5L ;
211+
212+ @Autowired
213+ UserRepository repository;
214+
215+ @Autowired
216+ private AmazonDynamoDB amazonDynamoDB;
217+
218+ @Before
219+ public void init () throws Exception {
220+
221+ ListTablesResult listTablesResult = amazonDynamoDB. listTables();
222+
223+ listTablesResult. getTableNames(). stream().
224+ filter(tableName - > tableName. equals(User . TABLE_NAME )). forEach(tableName - > {
225+ amazonDynamoDB. deleteTable(tableName);
226+ });
227+
228+ List<AttributeDefinition > attributeDefinitions = new ArrayList<AttributeDefinition > ();
229+ attributeDefinitions. add(new AttributeDefinition (). withAttributeName(KEY_NAME ). withAttributeType(" S" ));
230+
231+ List<KeySchemaElement > keySchemaElements = new ArrayList<KeySchemaElement > ();
232+ keySchemaElements. add(new KeySchemaElement (). withAttributeName(KEY_NAME ). withKeyType(KeyType . HASH ));
233+
234+ CreateTableRequest request = new CreateTableRequest ()
235+ .withTableName(TABLE_NAME )
236+ .withKeySchema(keySchemaElements)
237+ .withAttributeDefinitions(attributeDefinitions)
238+ .withProvisionedThroughput(new ProvisionedThroughput (). withReadCapacityUnits(READ_CAPACITY_UNITS )
239+ .withWriteCapacityUnits(WRITE_CAPACITY_UNITS ));
240+
241+ amazonDynamoDB. createTable(request);
242+
243+ }
244+
245+ @Test
246+ public void sampleTestCase () {
247+ User dave = new User (" Dave" , " Matthews" );
248+ repository. save(dave);
249+
250+ User carter = new User (" Carter" , " Beauford" );
251+ repository. save(carter);
252+
253+ List<User > result = repository. findByLastName(" Matthews" );
254+ Assert . assertThat(result. size(), is(1 ));
255+ Assert . assertThat(result, hasItem(dave));
256+ }
196257}
197258```
198259
0 commit comments