diff --git a/spring-boot-data-dynamodb/.gitignore b/spring-boot-data-dynamodb/.gitignore new file mode 100644 index 0000000000..e26d6af438 --- /dev/null +++ b/spring-boot-data-dynamodb/.gitignore @@ -0,0 +1,4 @@ +/target/ +.settings/ +.classpath +.project diff --git a/spring-boot-data-dynamodb/README.MD b/spring-boot-data-dynamodb/README.MD new file mode 100644 index 0000000000..2a87b46021 --- /dev/null +++ b/spring-boot-data-dynamodb/README.MD @@ -0,0 +1,2 @@ +###The Course +The "REST With Spring" Classes: http://bit.ly/restwithspring diff --git a/spring-boot-data-dynamodb/pom.xml b/spring-boot-data-dynamodb/pom.xml new file mode 100644 index 0000000000..023d249c56 --- /dev/null +++ b/spring-boot-data-dynamodb/pom.xml @@ -0,0 +1,189 @@ + + 4.0.0 + com.baeldung + spring-boot + 0.0.1-SNAPSHOT + jar + Spring Boot Actuator + This is simple boot application for Spring boot actuator test + + + spring-boot-starter-parent + org.springframework.boot + 1.2.3.RELEASE + + + + + + org.baeldung.boot.DemoApplication + UTF-8 + 1.8 + 4.3.1.RELEASE + + + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + + org.springframework.boot + spring-boot-starter-actuator + + + + org.springframework.boot + spring-boot-starter-security + + + + io.dropwizard.metrics + metrics-core + + + + com.h2database + h2 + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.springframework.boot + spring-boot-starter + + + com.jayway.jsonpath + json-path + test + + + org.springframework.boot + spring-boot-starter-mail + + + org.subethamail + subethasmtp + 3.1.7 + test + + + + org.webjars + bootstrap + 3.3.4 + + + org.webjars + jquery + 2.1.4 + + + org.springframework.data + spring-data-jpa + 1.10.2.RELEASE + + + com.amazonaws + aws-java-sdk-dynamodb + 1.11.34 + + + org.socialsignin + spring-data-dynamodb + 4.2.1 + + + + + spring-boot + + + src/main/resources + true + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + org.apache.maven.plugins + maven-war-plugin + + + + + + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + true + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + opensourceagility-release + http://repo.opensourceagility.com/release/ + + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + true + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + + diff --git a/spring-boot-data-dynamodb/src/main/java/com/baeldung/spring/data/es/config/DynamoDBConfig.java b/spring-boot-data-dynamodb/src/main/java/com/baeldung/spring/data/es/config/DynamoDBConfig.java new file mode 100644 index 0000000000..554a6c9c12 --- /dev/null +++ b/spring-boot-data-dynamodb/src/main/java/com/baeldung/spring/data/es/config/DynamoDBConfig.java @@ -0,0 +1,41 @@ +package com.baeldung.spring.data.es.config; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +import com.amazonaws.auth.AWSCredentials; +import com.amazonaws.auth.BasicAWSCredentials; +import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; +import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient; + +@Configuration +@ComponentScan(basePackages = { "com.baeldung.spring.data.es.service" }) +public class DynamoDBConfig { + + @Value("${amazon.dynamodb.endpoint}") + private String amazonDynamoDBEndpoint; + + @Value("${amazon.aws.accesskey}") + private String amazonAWSAccessKey; + + @Value("${amazon.aws.secretkey}") + private String amazonAWSSecretKey; + + @Bean + public AmazonDynamoDB amazonDynamoDB() { + AmazonDynamoDB amazonDynamoDB = new AmazonDynamoDBClient(amazonAWSCredentials()); + if (StringUtils.isNotEmpty(amazonDynamoDBEndpoint)) { + amazonDynamoDB.setEndpoint(amazonDynamoDBEndpoint); + } + return amazonDynamoDB; + } + + @Bean + public AWSCredentials amazonAWSCredentials() { + return new BasicAWSCredentials(amazonAWSAccessKey, amazonAWSSecretKey); + } + +} diff --git a/spring-boot-data-dynamodb/src/main/java/com/baeldung/spring/data/es/model/ProductInfo.java b/spring-boot-data-dynamodb/src/main/java/com/baeldung/spring/data/es/model/ProductInfo.java new file mode 100644 index 0000000000..4c97d14045 --- /dev/null +++ b/spring-boot-data-dynamodb/src/main/java/com/baeldung/spring/data/es/model/ProductInfo.java @@ -0,0 +1,47 @@ +package com.baeldung.spring.data.es.model; + +import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute; +import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAutoGeneratedKey; +import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey; + +public class ProductInfo { + private String id; + private String msrp; + private String cost; + + public ProductInfo() { + } + + public ProductInfo(String cost, String msrp) { + this.msrp = msrp; + this.cost = cost; + } + + @DynamoDBHashKey + @DynamoDBAutoGeneratedKey + public String getId() { + return id; + } + + @DynamoDBAttribute + public String getMsrp() { + return msrp; + } + + @DynamoDBAttribute + public String getCost() { + return cost; + } + + public void setId(String id) { + this.id = id; + } + + public void setMsrp(String msrp) { + this.msrp = msrp; + } + + public void setCost(String cost) { + this.cost = cost; + } +} diff --git a/spring-boot-data-dynamodb/src/main/java/com/baeldung/spring/data/es/repository/ProductInfoRepository.java b/spring-boot-data-dynamodb/src/main/java/com/baeldung/spring/data/es/repository/ProductInfoRepository.java new file mode 100644 index 0000000000..a30ff1c4aa --- /dev/null +++ b/spring-boot-data-dynamodb/src/main/java/com/baeldung/spring/data/es/repository/ProductInfoRepository.java @@ -0,0 +1,14 @@ +package com.baeldung.spring.data.es.repository; + +import java.util.List; + +import org.socialsignin.spring.data.dynamodb.repository.EnableScan; +import org.springframework.data.repository.CrudRepository; + +import com.baeldung.spring.data.es.model.ProductInfo; + +@EnableScan +public interface ProductInfoRepository extends CrudRepository { + + List findById(String id); +} diff --git a/spring-boot-data-dynamodb/src/main/java/org/baeldung/Application.java b/spring-boot-data-dynamodb/src/main/java/org/baeldung/Application.java new file mode 100644 index 0000000000..aae0c427a9 --- /dev/null +++ b/spring-boot-data-dynamodb/src/main/java/org/baeldung/Application.java @@ -0,0 +1,13 @@ +package org.baeldung; + +import org.springframework.boot.SpringApplication; +import org.springframework.context.ApplicationContext; + +@org.springframework.boot.autoconfigure.SpringBootApplication +public class Application { + private static ApplicationContext applicationContext; + + public static void main(String[] args) { + applicationContext = SpringApplication.run(Application.class, args); + } +} diff --git a/spring-boot-data-dynamodb/src/main/resources/application.properties b/spring-boot-data-dynamodb/src/main/resources/application.properties new file mode 100644 index 0000000000..d30045d1dc --- /dev/null +++ b/spring-boot-data-dynamodb/src/main/resources/application.properties @@ -0,0 +1,31 @@ +server.port=8080 +server.contextPath=/springbootapp +management.port=8081 +management.address=127.0.0.1 + +endpoints.shutdown.enabled=true + +endpoints.jmx.domain=Spring Sample Application +endpoints.jmx.uniqueNames=true + +##jolokia.config.debug=true +##endpoints.jolokia.enabled=true +##endpoints.jolokia.path=jolokia + +spring.jmx.enabled=true +endpoints.jmx.enabled=true + +## for pretty printing of json when endpoints accessed over HTTP +http.mappers.jsonPrettyPrint=true + +## Configuring info endpoint +info.app.name=Spring Sample Application +info.app.description=This is my first spring boot application G1 +info.app.version=1.0.0 + +## Spring Security Configurations +security.user.name=admin1 +security.user.password=secret1 +management.security.role=SUPERUSER + +logging.level.org.springframework=INFO \ No newline at end of file diff --git a/spring-boot-data-dynamodb/src/main/resources/demo.properties b/spring-boot-data-dynamodb/src/main/resources/demo.properties new file mode 100644 index 0000000000..649b64f59b --- /dev/null +++ b/spring-boot-data-dynamodb/src/main/resources/demo.properties @@ -0,0 +1,6 @@ +spring.output.ansi.enabled=never +server.port=7070 + +# Security +security.user.name=admin +security.user.password=password \ No newline at end of file diff --git a/spring-boot-data-dynamodb/src/main/resources/logback.xml b/spring-boot-data-dynamodb/src/main/resources/logback.xml new file mode 100644 index 0000000000..78913ee76f --- /dev/null +++ b/spring-boot-data-dynamodb/src/main/resources/logback.xml @@ -0,0 +1,14 @@ + + + + + web - %date [%thread] %-5level %logger{36} - %message%n + + + + + + + + + \ No newline at end of file diff --git a/spring-boot-data-dynamodb/src/main/resources/templates/index.html b/spring-boot-data-dynamodb/src/main/resources/templates/index.html new file mode 100644 index 0000000000..046d21600a --- /dev/null +++ b/spring-boot-data-dynamodb/src/main/resources/templates/index.html @@ -0,0 +1,19 @@ + + + WebJars Demo + + + + +

+
+ × + Success! It is working as we expected. +
+
+ + + + + + \ No newline at end of file diff --git a/spring-boot-data-dynamodb/src/test/java/com/baeldung/spring/data/es/repository/ProductInfoRepositoryIntegrationTest.java b/spring-boot-data-dynamodb/src/test/java/com/baeldung/spring/data/es/repository/ProductInfoRepositoryIntegrationTest.java new file mode 100644 index 0000000000..438499bae8 --- /dev/null +++ b/spring-boot-data-dynamodb/src/test/java/com/baeldung/spring/data/es/repository/ProductInfoRepositoryIntegrationTest.java @@ -0,0 +1,85 @@ +package com.baeldung.spring.data.es.repository; + +import static org.junit.Assert.assertTrue; + +import java.util.List; + +import org.baeldung.Application; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.IntegrationTest; +import org.springframework.boot.test.SpringApplicationConfiguration; +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 com.amazonaws.services.dynamodbv2.AmazonDynamoDB; +import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper; +import com.amazonaws.services.dynamodbv2.document.DynamoDB; +import com.amazonaws.services.dynamodbv2.document.Table; +import com.amazonaws.services.dynamodbv2.model.CreateTableRequest; +import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput; +import com.amazonaws.services.dynamodbv2.model.ResourceInUseException; +import com.baeldung.spring.data.es.model.ProductInfo; +import com.baeldung.spring.data.es.repository.ProductInfoRepository; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = Application.class) +@WebAppConfiguration +@IntegrationTest +@ActiveProfiles("local") +@TestPropertySource(properties = { "amazon.dynamodb.endpoint=http://localhost:8000/", "amazon.aws.accesskey=test1", "amazon.aws.secretkey=test231" }) +public class ProductInfoRepositoryIntegrationTest { + + private DynamoDBMapper dynamoDBMapper; + + @Autowired + private DynamoDB dynamoDB; + + @Autowired + private AmazonDynamoDB amazonDynamoDB; + + @Autowired + ProductInfoRepository ProductInfoRepository; + + private static final String EXPECTED_COST = "20"; + private static final String EXPECTED_PRICE = "50"; + + @Before + @Ignore + public void setUp() throws Exception { + + try { + dynamoDBMapper = new DynamoDBMapper(amazonDynamoDB); + + CreateTableRequest tableRequest = dynamoDBMapper.generateCreateTableRequest(ProductInfo.class); // 1 + + tableRequest.setProvisionedThroughput(new ProvisionedThroughput(1L, 1L)); // 2 + + Table table = dynamoDB.createTable(tableRequest); // 3 + + table.waitForActive(); // 4 + } catch (ResourceInUseException e) { + // Do nothing, table already created + } + + // TODO How to handle different environments. i.e. AVOID deleting all entries in ProductInfoion table + dynamoDBMapper.batchDelete((List) ProductInfoRepository.findAll()); + } + + @Ignore + @Test + public void sampleTestCase() { + + ProductInfo dave = new ProductInfo(EXPECTED_COST, EXPECTED_PRICE); + ProductInfoRepository.save(dave); + + List result = (List) ProductInfoRepository.findAll(); + assertTrue("Not empty", result.size() > 0); + assertTrue("Contains item with expected cost", result.get(0).getCost().equals(EXPECTED_COST)); + } +} diff --git a/spring-boot-data-dynamodb/src/test/resources/application.properties b/spring-boot-data-dynamodb/src/test/resources/application.properties new file mode 100644 index 0000000000..01e8a2e52e --- /dev/null +++ b/spring-boot-data-dynamodb/src/test/resources/application.properties @@ -0,0 +1,7 @@ +spring.mail.host=localhost +spring.mail.port=8025 +spring.mail.properties.mail.smtp.auth=false + +amazon.dynamodb.endpoint=http://localhost:8000/ +amazon.aws.accesskey=key +amazon.aws.secretkey=key2 \ No newline at end of file diff --git a/spring-boot-data-dynamodb/src/test/resources/exception-hibernate.properties b/spring-boot-data-dynamodb/src/test/resources/exception-hibernate.properties new file mode 100644 index 0000000000..cde746acb9 --- /dev/null +++ b/spring-boot-data-dynamodb/src/test/resources/exception-hibernate.properties @@ -0,0 +1,2 @@ +spring.profiles.active=exception +spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext diff --git a/spring-boot-data-dynamodb/src/test/resources/exception.properties b/spring-boot-data-dynamodb/src/test/resources/exception.properties new file mode 100644 index 0000000000..c55e415a3a --- /dev/null +++ b/spring-boot-data-dynamodb/src/test/resources/exception.properties @@ -0,0 +1,6 @@ +# Security +security.user.name=admin +security.user.password=password + +spring.dao.exceptiontranslation.enabled=false +spring.profiles.active=exception \ No newline at end of file