Skip to content

Commit

Permalink
polish dynamodb implementation (#817)
Browse files Browse the repository at this point in the history
* polish dynamodb implementation

* delombok

* adds to string
  • Loading branch information
rajadilipkolli authored Dec 27, 2024
1 parent 905e61a commit 0083751
Show file tree
Hide file tree
Showing 14 changed files with 128 additions and 62 deletions.
2 changes: 1 addition & 1 deletion aws-dynamodb-project/docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: '3.8'
services:
localstack:
image: localstack/localstack:3.8.1
image: localstack/localstack:4.0.3
ports:
- "4566:4566"
environment:
Expand Down
15 changes: 1 addition & 14 deletions aws-dynamodb-project/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,6 @@
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.awspring.cloud</groupId>
<artifactId>spring-cloud-aws-starter-dynamodb</artifactId>
Expand Down Expand Up @@ -129,14 +124,6 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
<executions>
<execution>
<goals>
Expand Down Expand Up @@ -233,7 +220,7 @@
<configuration>
<java>
<googleJavaFormat>
<version>1.22.0</version>
<version>1.25.2</version>
<style>AOSP</style>
</googleJavaFormat>
<formatAnnotations />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

@SpringBootApplication
@EnableConfigurationProperties({ApplicationProperties.class})
public class Application {
public class DynamoDBApplication {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
SpringApplication.run(DynamoDBApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,67 @@
package com.learning.awsspring.config;

import lombok.Data;
import jakarta.validation.Valid;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;

@Data
@ConfigurationProperties("application")
public class ApplicationProperties {
@NestedConfigurationProperty private Cors cors = new Cors();

@Data
@NestedConfigurationProperty @Valid private Cors cors = new Cors();

public static class Cors {
private String pathPattern = "/api/**";
private String allowedMethods = "*";
private String allowedHeaders = "*";
private String allowedOriginPatterns = "*";
private boolean allowCredentials = true;

public String getPathPattern() {
return pathPattern;
}

public void setPathPattern(String pathPattern) {
this.pathPattern = pathPattern;
}

public String getAllowedMethods() {
return allowedMethods;
}

public void setAllowedMethods(String allowedMethods) {
this.allowedMethods = allowedMethods;
}

public String getAllowedHeaders() {
return allowedHeaders;
}

public void setAllowedHeaders(String allowedHeaders) {
this.allowedHeaders = allowedHeaders;
}

public String getAllowedOriginPatterns() {
return allowedOriginPatterns;
}

public void setAllowedOriginPatterns(String allowedOriginPatterns) {
this.allowedOriginPatterns = allowedOriginPatterns;
}

public boolean isAllowCredentials() {
return allowCredentials;
}

public void setAllowCredentials(boolean allowCredentials) {
this.allowCredentials = allowCredentials;
}
}

public Cors getCors() {
return cors;
}

public void setCors(Cors cors) {
this.cors = cors;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@
import com.learning.awsspring.entities.Customer;
import com.learning.awsspring.repositories.CustomerRepository;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
@Slf4j
public class Initializer implements CommandLineRunner {

private static final Logger log = LoggerFactory.getLogger(Initializer.class);

private final CustomerRepository customerRepository;

public Initializer(CustomerRepository customerRepository) {
this.customerRepository = customerRepository;
}

@Override
public void run(String... args) {
log.info("Running Initializer.....");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
package com.learning.awsspring.config;

import lombok.RequiredArgsConstructor;
import com.learning.awsspring.config.ApplicationProperties.Cors;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.NonNull;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@RequiredArgsConstructor
public class WebMvcConfig implements WebMvcConfigurer {

private final ApplicationProperties properties;

public WebMvcConfig(ApplicationProperties properties) {
this.properties = properties;
}

@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping(properties.getCors().getPathPattern())
.allowedMethods(properties.getCors().getAllowedMethods())
.allowedHeaders(properties.getCors().getAllowedHeaders())
.allowedOriginPatterns(properties.getCors().getAllowedOriginPatterns())
.allowCredentials(properties.getCors().isAllowCredentials());
public void addCorsMappings(@NonNull CorsRegistry registry) {
Cors cors = properties.getCors();
registry.addMapping(cors.getPathPattern())
.allowedMethods(cors.getAllowedMethods())
.allowedHeaders(cors.getAllowedHeaders())
.allowedOriginPatterns(cors.getAllowedOriginPatterns())
.allowCredentials(cors.isAllowCredentials());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,12 @@

import jakarta.validation.constraints.NotEmpty;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.NoArgsConstructor;
import lombok.ToString;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbAttribute;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbBean;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbPartitionKey;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbSortKey;

@NoArgsConstructor
@AllArgsConstructor
@DynamoDbBean
@ToString
@Builder
public class Customer {

private UUID id;
Expand All @@ -26,6 +18,14 @@ public class Customer {
@NotEmpty(message = "Email cannot be empty")
private String email;

public Customer() {}

public Customer(UUID id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
}

// Partition key
@DynamoDbPartitionKey
@DynamoDbAttribute("id")
Expand Down Expand Up @@ -55,4 +55,9 @@ public String getEmail() {
public void setEmail(String email) {
this.email = email;
}

@Override
public String toString() {
return "Customer [id=" + id + ", name=" + name + ", email=" + email + "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable;
Expand All @@ -16,12 +15,17 @@
import software.amazon.awssdk.enhanced.dynamodb.model.WriteBatch;

@Repository
@RequiredArgsConstructor
public class CustomerRepository {

private final DynamoDbTemplate dynamoDbTemplate;
private final DynamoDbEnhancedClient dynamoDbEnhancedClient;

public CustomerRepository(
DynamoDbTemplate dynamoDbTemplate, DynamoDbEnhancedClient dynamoDbEnhancedClient) {
this.dynamoDbTemplate = dynamoDbTemplate;
this.dynamoDbEnhancedClient = dynamoDbEnhancedClient;
}

public List<Customer> findAll() {
return dynamoDbTemplate.scanAll(Customer.class).stream()
.map(Page::items)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class CustomerService {

private final CustomerRepository customerRepository;

public CustomerService(CustomerRepository customerRepository) {
this.customerRepository = customerRepository;
}

public Customer saveCustomer(Customer customer) {
return customerRepository.saveEntity(customer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.learning.awsspring.services.CustomerService;
import java.util.List;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
Expand All @@ -18,11 +17,14 @@

@RestController
@RequestMapping("/api/customers")
@RequiredArgsConstructor
public class CustomerController {

private final CustomerService customerService;

public CustomerController(CustomerService customerService) {
this.customerService = customerService;
}

@GetMapping
public List<Customer> getAllCustomers() {
return customerService.findAllCustomers();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@
import com.learning.awsspring.entities.Customer;
import io.awspring.cloud.dynamodb.DynamoDbOperations;
import java.util.UUID;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import software.amazon.awssdk.enhanced.dynamodb.Key;
import software.amazon.awssdk.enhanced.dynamodb.model.PageIterable;
import software.amazon.awssdk.enhanced.dynamodb.model.QueryConditional;
import software.amazon.awssdk.enhanced.dynamodb.model.QueryEnhancedRequest;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;

@Slf4j
class ApplicationIntegrationTest extends AbstractIntegrationTest {

private static final Logger log = LoggerFactory.getLogger(ApplicationIntegrationTest.class);

@Autowired private DynamoDbOperations dynamoDbOperations;

@Test
Expand All @@ -26,7 +28,7 @@ void contextLoads() {
UUID id = UUID.randomUUID();
String email = "[email protected]";
String name = "junit";
Customer customer = Customer.builder().id(id).name(name).email(email).build();
Customer customer = new Customer(id, name, email);
// Saving Customer
dynamoDbOperations.save(customer);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.learning.awsspring;

import com.learning.awsspring.common.ContainerConfig;
import org.springframework.boot.SpringApplication;

public class TestDynamoDBApplication {

public static void main(String[] args) {
SpringApplication.from(DynamoDBApplication::main).with(ContainerConfig.class).run(args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.learning.awsspring.TestApplication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;

@ActiveProfiles({PROFILE_TEST})
@SpringBootTest(webEnvironment = RANDOM_PORT, classes = TestApplication.class)
@SpringBootTest(webEnvironment = RANDOM_PORT, classes = ContainerConfig.class)
@AutoConfigureMockMvc
public abstract class AbstractIntegrationTest {

Expand Down
Loading

0 comments on commit 0083751

Please sign in to comment.