Merge remote-tracking branch 'upstream/master'
This commit is contained in:
@@ -23,6 +23,10 @@
|
||||
<spring-data-dynamodb.version>4.4.1</spring-data-dynamodb.version>
|
||||
<aws-java-sdk-dynamodb.version>1.11.64</aws-java-sdk-dynamodb.version>
|
||||
<bootstrap.version>3.3.7-1</bootstrap.version>
|
||||
<sqlite4java.version>1.0.392</sqlite4java.version>
|
||||
<dynamodb.version>1.11.106</dynamodb.version>
|
||||
<dynamodblocal.version>1.11.86</dynamodblocal.version>
|
||||
<dynamodblocal.repository.url>https://s3-us-west-2.amazonaws.com/dynamodb-local/release</dynamodblocal.repository.url>
|
||||
</properties>
|
||||
|
||||
<dependencyManagement>
|
||||
@@ -103,6 +107,57 @@
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>${httpclient.version}</version>
|
||||
</dependency>
|
||||
<!-- ================================================== -->
|
||||
<!-- DynamoDBLocal dependencies -->
|
||||
<!-- ================================================== -->
|
||||
<dependency>
|
||||
<groupId>com.amazonaws</groupId>
|
||||
<artifactId>DynamoDBLocal</artifactId>
|
||||
<version>${dynamodblocal.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.almworks.sqlite4java</groupId>
|
||||
<artifactId>sqlite4java</artifactId>
|
||||
<version>${sqlite4java.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.almworks.sqlite4java</groupId>
|
||||
<artifactId>sqlite4java-win32-x86</artifactId>
|
||||
<version>${sqlite4java.version}</version>
|
||||
<type>dll</type>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.almworks.sqlite4java</groupId>
|
||||
<artifactId>sqlite4java-win32-x64</artifactId>
|
||||
<version>${sqlite4java.version}</version>
|
||||
<type>dll</type>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.almworks.sqlite4java</groupId>
|
||||
<artifactId>libsqlite4java-osx</artifactId>
|
||||
<version>${sqlite4java.version}</version>
|
||||
<type>dylib</type>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.almworks.sqlite4java</groupId>
|
||||
<artifactId>libsqlite4java-linux-i386</artifactId>
|
||||
<version>${sqlite4java.version}</version>
|
||||
<type>so</type>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.almworks.sqlite4java</groupId>
|
||||
<artifactId>libsqlite4java-linux-amd64</artifactId>
|
||||
<version>${sqlite4java.version}</version>
|
||||
<type>so</type>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@@ -120,6 +175,25 @@
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<version>2.10</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>copy-dependencies</id>
|
||||
<phase>test-compile</phase>
|
||||
<goals>
|
||||
<goal>copy-dependencies</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<includeScope>test</includeScope>
|
||||
<includeTypes>so,dll,dylib</includeTypes>
|
||||
<outputDirectory>${project.basedir}/native-libs</outputDirectory>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
|
||||
@@ -142,6 +216,11 @@
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>dynamodb-local</id>
|
||||
<name>DynamoDB Local Release Repository</name>
|
||||
<url>${dynamodblocal.repository.url}</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
<pluginRepositories>
|
||||
<pluginRepository>
|
||||
|
||||
+12
-7
@@ -8,8 +8,9 @@ import com.amazonaws.services.dynamodbv2.model.ResourceInUseException;
|
||||
import com.baeldung.Application;
|
||||
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.Ignore;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -21,7 +22,10 @@ import org.springframework.test.context.web.WebAppConfiguration;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.hamcrest.Matchers.greaterThan;
|
||||
import static org.hamcrest.core.Is.is;
|
||||
import static org.hamcrest.core.IsEqual.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest(classes = Application.class)
|
||||
@@ -30,6 +34,9 @@ import static org.junit.Assert.assertTrue;
|
||||
@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;
|
||||
|
||||
@Autowired
|
||||
@@ -42,7 +49,6 @@ public class ProductInfoRepositoryIntegrationTest {
|
||||
private static final String EXPECTED_PRICE = "50";
|
||||
|
||||
@Before
|
||||
@Ignore // TODO Remove Ignore annotations when running locally with Local DynamoDB instance
|
||||
public void setup() throws Exception {
|
||||
|
||||
try {
|
||||
@@ -57,19 +63,18 @@ public class ProductInfoRepositoryIntegrationTest {
|
||||
// Do nothing, table already created
|
||||
}
|
||||
|
||||
// TODO How to handle different environments. i.e. AVOID deleting all entries in ProductInfoion table
|
||||
// TODO How to handle different environments. i.e. AVOID deleting all entries in ProductInfo on table
|
||||
dynamoDBMapper.batchDelete((List<ProductInfo>) repository.findAll());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore // TODO Remove Ignore annotations when running locally with Local DynamoDB instance
|
||||
public void givenItemWithExpectedCost_whenRunFindAll_thenItemIsFound() {
|
||||
|
||||
ProductInfo productInfo = new ProductInfo(EXPECTED_COST, EXPECTED_PRICE);
|
||||
repository.save(productInfo);
|
||||
|
||||
List<ProductInfo> result = (List<ProductInfo>) repository.findAll();
|
||||
assertTrue("Not empty", result.size() > 0);
|
||||
assertTrue("Contains item with expected cost", result.get(0).getCost().equals(EXPECTED_COST));
|
||||
assertThat(result.size(), is(greaterThan(0)));
|
||||
assertThat(result.get(0).getCost(), is(equalTo(EXPECTED_COST)));
|
||||
}
|
||||
}
|
||||
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.spring.data.dynamodb.repository.rule;
|
||||
|
||||
import com.amazonaws.services.dynamodbv2.local.main.ServerRunner;
|
||||
import com.amazonaws.services.dynamodbv2.local.server.DynamoDBProxyServer;
|
||||
import org.junit.rules.ExternalResource;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class LocalDbCreationRule extends ExternalResource {
|
||||
|
||||
protected DynamoDBProxyServer server;
|
||||
|
||||
public LocalDbCreationRule() {
|
||||
System.setProperty("sqlite4java.library.path", "native-libs");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void before() throws Exception {
|
||||
String port = "8000";
|
||||
this.server = ServerRunner.createServerFromCommandLineArgs(new String[]{"-inMemory", "-port", port});
|
||||
server.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void after() {
|
||||
this.stopUnchecked(server);
|
||||
}
|
||||
|
||||
protected void stopUnchecked(DynamoDBProxyServer dynamoDbServer) {
|
||||
try {
|
||||
dynamoDbServer.stop();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,11 +16,13 @@
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<spring.version>4.3.7.RELEASE</spring.version>
|
||||
<spring-data-redis>1.8.1.RELEASE</spring-data-redis>
|
||||
<spring.version>5.0.3.RELEASE</spring.version>
|
||||
<spring-data-redis>2.0.3.RELEASE</spring-data-redis>
|
||||
<cglib.version>3.2.4</cglib.version>
|
||||
<jedis.version>2.9.0</jedis.version>
|
||||
<nosqlunit.version>0.10.0</nosqlunit.version>
|
||||
<spring-data-commons.version>2.0.3.RELEASE</spring-data-commons.version>
|
||||
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
@@ -73,6 +75,12 @@
|
||||
<artifactId>nosqlunit-redis</artifactId>
|
||||
<version>${nosqlunit.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-commons</artifactId>
|
||||
<version>${spring-data-commons.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
||||
+6
-3
@@ -1,8 +1,5 @@
|
||||
package com.baeldung.spring.data.redis.config;
|
||||
|
||||
import com.baeldung.spring.data.redis.queue.MessagePublisher;
|
||||
import com.baeldung.spring.data.redis.queue.RedisMessagePublisher;
|
||||
import com.baeldung.spring.data.redis.queue.RedisMessageSubscriber;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -11,10 +8,16 @@ import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.listener.ChannelTopic;
|
||||
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
|
||||
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
|
||||
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
|
||||
import org.springframework.data.redis.serializer.GenericToStringSerializer;
|
||||
|
||||
import com.baeldung.spring.data.redis.queue.MessagePublisher;
|
||||
import com.baeldung.spring.data.redis.queue.RedisMessagePublisher;
|
||||
import com.baeldung.spring.data.redis.queue.RedisMessageSubscriber;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan("com.baeldung.spring.data.redis")
|
||||
@EnableRedisRepositories(basePackages = "com.baeldung.spring.data.redis.repo")
|
||||
public class RedisConfig {
|
||||
|
||||
@Bean
|
||||
|
||||
+3
@@ -2,6 +2,9 @@ package com.baeldung.spring.data.redis.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.springframework.data.redis.core.RedisHash;
|
||||
|
||||
@RedisHash("Student")
|
||||
public class Student implements Serializable {
|
||||
|
||||
public enum Gender {
|
||||
|
||||
+5
-14
@@ -1,18 +1,9 @@
|
||||
package com.baeldung.spring.data.redis.repo;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.baeldung.spring.data.redis.model.Student;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public interface StudentRepository {
|
||||
|
||||
void saveStudent(Student person);
|
||||
|
||||
void updateStudent(Student student);
|
||||
|
||||
Student findStudent(String id);
|
||||
|
||||
Map<Object, Object> findAllStudents();
|
||||
|
||||
void deleteStudent(String id);
|
||||
}
|
||||
@Repository
|
||||
public interface StudentRepository extends CrudRepository<Student, String> {}
|
||||
|
||||
-49
@@ -1,49 +0,0 @@
|
||||
package com.baeldung.spring.data.redis.repo;
|
||||
|
||||
import com.baeldung.spring.data.redis.model.Student;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.HashOperations;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.Map;
|
||||
|
||||
@Repository
|
||||
public class StudentRepositoryImpl implements StudentRepository {
|
||||
|
||||
private static final String KEY = "Student";
|
||||
|
||||
private RedisTemplate<String, Object> redisTemplate;
|
||||
private HashOperations hashOperations;
|
||||
|
||||
@Autowired
|
||||
public StudentRepositoryImpl(RedisTemplate<String, Object> redisTemplate) {
|
||||
this.redisTemplate = redisTemplate;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
private void init() {
|
||||
hashOperations = redisTemplate.opsForHash();
|
||||
}
|
||||
|
||||
public void saveStudent(final Student student) {
|
||||
hashOperations.put(KEY, student.getId(), student);
|
||||
}
|
||||
|
||||
public void updateStudent(final Student student) {
|
||||
hashOperations.put(KEY, student.getId(), student);
|
||||
}
|
||||
|
||||
public Student findStudent(final String id) {
|
||||
return (Student) hashOperations.get(KEY, id);
|
||||
}
|
||||
|
||||
public Map<Object, Object> findAllStudents() {
|
||||
return hashOperations.entries(KEY);
|
||||
}
|
||||
|
||||
public void deleteStudent(final String id) {
|
||||
hashOperations.delete(KEY, id);
|
||||
}
|
||||
}
|
||||
+22
-18
@@ -1,17 +1,20 @@
|
||||
package com.baeldung.spring.data.redis.repo;
|
||||
|
||||
import com.baeldung.spring.data.redis.config.RedisConfig;
|
||||
import com.baeldung.spring.data.redis.model.Student;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import com.baeldung.spring.data.redis.config.RedisConfig;
|
||||
import com.baeldung.spring.data.redis.model.Student;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = RedisConfig.class)
|
||||
@@ -23,18 +26,18 @@ public class StudentRepositoryIntegrationTest {
|
||||
@Test
|
||||
public void whenSavingStudent_thenAvailableOnRetrieval() throws Exception {
|
||||
final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1);
|
||||
studentRepository.saveStudent(student);
|
||||
final Student retrievedStudent = studentRepository.findStudent(student.getId());
|
||||
studentRepository.save(student);
|
||||
final Student retrievedStudent = studentRepository.findById(student.getId()).get();
|
||||
assertEquals(student.getId(), retrievedStudent.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUpdatingStudent_thenAvailableOnRetrieval() throws Exception {
|
||||
final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1);
|
||||
studentRepository.saveStudent(student);
|
||||
studentRepository.save(student);
|
||||
student.setName("Richard Watson");
|
||||
studentRepository.saveStudent(student);
|
||||
final Student retrievedStudent = studentRepository.findStudent(student.getId());
|
||||
studentRepository.save(student);
|
||||
final Student retrievedStudent = studentRepository.findById(student.getId()).get();
|
||||
assertEquals(student.getName(), retrievedStudent.getName());
|
||||
}
|
||||
|
||||
@@ -42,18 +45,19 @@ public class StudentRepositoryIntegrationTest {
|
||||
public void whenSavingStudents_thenAllShouldAvailableOnRetrieval() throws Exception {
|
||||
final Student engStudent = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1);
|
||||
final Student medStudent = new Student("Med2015001", "Gareth Houston", Student.Gender.MALE, 2);
|
||||
studentRepository.saveStudent(engStudent);
|
||||
studentRepository.saveStudent(medStudent);
|
||||
final Map<Object, Object> retrievedStudent = studentRepository.findAllStudents();
|
||||
assertEquals(retrievedStudent.size(), 2);
|
||||
studentRepository.save(engStudent);
|
||||
studentRepository.save(medStudent);
|
||||
List<Student> students = new ArrayList<>();
|
||||
studentRepository.findAll().forEach(students::add);
|
||||
assertEquals(students.size(), 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDeletingStudent_thenNotAvailableOnRetrieval() throws Exception {
|
||||
final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1);
|
||||
studentRepository.saveStudent(student);
|
||||
studentRepository.deleteStudent(student.getId());
|
||||
final Student retrievedStudent = studentRepository.findStudent(student.getId());
|
||||
studentRepository.save(student);
|
||||
studentRepository.deleteById(student.getId());
|
||||
final Student retrievedStudent = studentRepository.findById(student.getId()).orElse(null);
|
||||
assertNull(retrievedStudent);
|
||||
}
|
||||
}
|
||||
+3
-3
@@ -1,10 +1,10 @@
|
||||
package org.baeldung.inmemory.persistence.model;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import javax.persistence.ElementCollection;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Student {
|
||||
|
||||
Reference in New Issue
Block a user