Skip to content

Commit

Permalink
Migrated orders to postgres and added some basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
niallthomson committed Jan 3, 2024
1 parent e25d310 commit 9c6a185
Show file tree
Hide file tree
Showing 12 changed files with 183 additions and 44 deletions.
22 changes: 14 additions & 8 deletions deploy/docker-compose/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ services:
environment:
- JAVA_OPTS=-XX:MaxRAMPercentage=75.0 -Djava.security.egd=file:/dev/urandom
- SERVER_TOMCAT_ACCESSLOG_ENABLED=true
- SPRING_PROFILES_ACTIVE=mysql,rabbitmq
- SPRING_DATASOURCE_URL=jdbc:mariadb://orders-db:3306/orders
- SPRING_PROFILES_ACTIVE=rabbitmq
- SPRING_DATASOURCE_URL=jdbc:postgresql://orders-db:5432/orders
- SPRING_DATASOURCE_USERNAME=orders_user
- SPRING_DATASOURCE_PASSWORD=${MYSQL_PASSWORD}
- SPRING_RABBITMQ_HOST=rabbitmq
Expand All @@ -97,15 +97,21 @@ services:
- ALL

orders-db:
image: mariadb:10.9
image: postgres:16.1
hostname: orders-db
restart: always
security_opt:
- no-new-privileges:true
environment:
- MYSQL_ROOT_PASSWORD=${MYSQL_PASSWORD}
- MYSQL_ALLOW_EMPTY_PASSWORD=true
- MYSQL_DATABASE=orders
- MYSQL_USER=orders_user
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
- reschedule=on-node-failure
- POSTGRES_PASSWORD=${MYSQL_PASSWORD}
- POSTGRES_DB=orders
- POSTGRES_USER=orders_user
healthcheck:
test: [ "CMD-SHELL", "pg_isready -d orders -U orders_user" ]
interval: 10s
timeout: 5s
retries: 30
mem_limit: 128m

checkout:
Expand Down
2 changes: 1 addition & 1 deletion src/catalog/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ services:
image: microservices-demo/catalog
hostname: catalog
depends_on:
- catalog-db
- catalog-db
restart: always
cap_drop:
- all
Expand Down
26 changes: 10 additions & 16 deletions src/orders/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ services:
environment:
- reschedule=on-node-failure
- SERVER_TOMCAT_ACCESSLOG_ENABLED=true
- SPRING_PROFILES_ACTIVE=mysql,rabbitmq
- SPRING_DATASOURCE_URL=jdbc:mariadb://orders-db:3306/orders
- SPRING_PROFILES_ACTIVE=rabbitmq
- SPRING_DATASOURCE_URL=jdbc:postgresql://orders-db:5432/orders
- SPRING_DATASOURCE_USERNAME=orders_user
- SPRING_DATASOURCE_PASSWORD=${MYSQL_PASSWORD}
- SPRING_RABBITMQ_HOST=rabbitmq
Expand All @@ -42,28 +42,22 @@ services:

# nosemgrep: yaml.docker-compose.security.writable-filesystem-service.writable-filesystem-service
orders-db:
image: mysql:5.7
image: postgres:16.1
hostname: orders-db
restart: always
security_opt:
- no-new-privileges:true
environment:
- reschedule=on-node-failure
- MYSQL_ROOT_PASSWORD=${MYSQL_PASSWORD}
- MYSQL_ALLOW_EMPTY_PASSWORD=true
- MYSQL_DATABASE=orders
- MYSQL_USER=orders_user
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
- POSTGRES_PASSWORD=${MYSQL_PASSWORD}
- POSTGRES_DB=orders
- POSTGRES_USER=orders_user
ports:
- "3306:3306"
- "5432:5432"
healthcheck:
test:
[
"CMD-SHELL",
"mysql -u root -p${MYSQL_PASSWORD} -e 'SELECT 1 cache'"
]
interval: 1s
timeout: 3s
test: [ "CMD-SHELL", "pg_isready -d orders -U orders_user" ]
interval: 10s
timeout: 5s
retries: 30

# nosemgrep: yaml.docker-compose.security.writable-filesystem-service.writable-filesystem-service
Expand Down
29 changes: 21 additions & 8 deletions src/orders/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@
<artifactId>spring-boot-properties-migrator</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
Expand All @@ -49,9 +44,27 @@
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>3.3.2</version>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package com.amazon.sample.orders.config;


import com.amazon.sample.orders.entities.OrderEntity;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jdbc.core.convert.JdbcCustomConversions;
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
import org.springframework.data.jdbc.repository.config.AbstractJdbcConfiguration;
import org.springframework.data.relational.RelationalManagedTypes;
import org.springframework.data.relational.core.mapping.NamingStrategy;
import org.springframework.data.relational.core.mapping.event.BeforeConvertCallback;

import java.util.Optional;
import java.util.UUID;

@Configuration
public class PersistenceConfig {
public class PersistenceConfig extends AbstractJdbcConfiguration {
@Bean
BeforeConvertCallback<OrderEntity> beforeSaveCallback() {
return (entity) -> {
Expand All @@ -19,4 +24,12 @@ BeforeConvertCallback<OrderEntity> beforeSaveCallback() {
return entity;
};
}

@Override
public JdbcMappingContext jdbcMappingContext(Optional<NamingStrategy> namingStrategy,
JdbcCustomConversions customConversions, RelationalManagedTypes jdbcManagedTypes) {
JdbcMappingContext context = super.jdbcMappingContext(namingStrategy, customConversions, jdbcManagedTypes);
context.setForceQuote(false);
return context;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,19 @@ public class OrderEntity {
private String lastName;
private String email;

@MappedCollection(keyColumn = "PRODUCT_ID")
@MappedCollection(keyColumn = "product_id")
private List<OrderItemEntity> items = new ArrayList<>();

public OrderEntity() {

}

public OrderEntity(String firstName, String lastName, String email) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}

public String getId() {
return id;
}
Expand Down Expand Up @@ -76,8 +86,10 @@ public void setItems(List<OrderItemEntity> items) {
this.items = items;
}

public void addItem(OrderItemEntity item) {
public OrderEntity addItem(OrderItemEntity item) {
this.items.add(item);

return this;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

package com.amazon.sample.orders.entities;

import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Table;

@Table
Expand Down
4 changes: 0 additions & 4 deletions src/orders/src/main/resources/application-mysql.yml

This file was deleted.

4 changes: 3 additions & 1 deletion src/orders/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ management:
include: '*'

server:
port: ${port:8080}
port: ${port:8080}

spring.flyway.baseline-on-migrate: true
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

import static org.assertj.core.api.BDDAssertions.then;

public class OrdersMetricsTest {
public class OrdersMetricsTests {

private MeterRegistry meterRegistry;
private final String PRODUCT_1 = "Product1";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: MIT-0
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
* software and associated documentation files (the "Software"), to deal in the Software
* without restriction, including without limitation the rights to use, copy, modify,
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.amazon.sample.orders.services;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.hasSize;

import java.util.List;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.PostgreSQLContainer;

import com.amazon.sample.orders.entities.OrderEntity;
import com.amazon.sample.orders.repositories.OrderRepository;

import io.restassured.RestAssured;
import io.restassured.http.ContentType;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class OrderServicePostgresTests {

@LocalServerPort
private Integer port;

static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>(
"postgres:16.1");

@BeforeAll
static void beforeAll() {
postgres.start();
}

@AfterAll
static void afterAll() {
postgres.stop();
}

@DynamicPropertySource
static void configureProperties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", postgres::getJdbcUrl);
registry.add("spring.datasource.username", postgres::getUsername);
registry.add("spring.datasource.password", postgres::getPassword);
}

@Autowired
OrderRepository orderRepository;

@BeforeEach
void setUp() {
RestAssured.baseURI = "http://localhost:" + port;
orderRepository.deleteAll();
}

@Test
void shouldGetEmptyOrders() {
given()
.contentType(ContentType.JSON)
.when()
.get("/orders")
.then()
.statusCode(200)
.body(".", hasSize(0));
}

@Test
void shouldGetAllOrders() {
List<OrderEntity> orders = List.of(
new OrderEntity("first", "last", "[email protected]"),
new OrderEntity("first", "last", "[email protected]"));
orderRepository.saveAll(orders);

given()
.contentType(ContentType.JSON)
.when()
.get("/orders")
.then()
.statusCode(200)
.body(".", hasSize(2));
}
}

0 comments on commit 9c6a185

Please sign in to comment.