Skip to content

Commit

Permalink
Postgres Database integration tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
Syed Abdullah committed Oct 22, 2024
1 parent 4b17d03 commit 1346aaa
Show file tree
Hide file tree
Showing 18 changed files with 510 additions and 44 deletions.
18 changes: 18 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand Down Expand Up @@ -159,6 +164,11 @@
<artifactId>mysql</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand All @@ -184,6 +194,14 @@
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
</plugin>
</plugins>
</build>

Expand Down
142 changes: 142 additions & 0 deletions postgressampledatabase.sql

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/main/java/syed/abdullah/demo/entity/Customer.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
@Getter
@Setter
@Entity(name = Customer.ENTITY_NAME)
@Table(name = Customer.TABLE_NAME, schema = "ecommerce", indexes = {
@Table(name = Customer.TABLE_NAME, indexes = {
@Index(name = "salesRepEmployeeNumber", columnList = "salesRepEmployeeNumber")
})
public class Customer implements Serializable {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/syed/abdullah/demo/entity/Employee.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
@Getter
@Setter
@Entity(name = Employee.ENTITY_NAME)
@Table(name = Employee.TABLE_NAME, schema = "ecommerce", indexes = {
@Table(name = Employee.TABLE_NAME, indexes = {
@Index(name = "officeCode", columnList = "officeCode"),
@Index(name = "reportsTo", columnList = "reportsTo")
})
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/syed/abdullah/demo/entity/Office.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
@Getter
@Setter
@Entity(name = Office.ENTITY_NAME)
@Table(name = Office.TABLE_NAME, schema = "ecommerce")
@Table(name = Office.TABLE_NAME)
public class Office implements Serializable {
public static final String ENTITY_NAME = "Office";
public static final String TABLE_NAME = "offices";
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/syed/abdullah/demo/entity/Order.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import lombok.*;
import org.hibernate.annotations.Type;

import java.io.Serializable;
import java.time.LocalDate;
Expand All @@ -15,7 +16,7 @@
@Getter
@Setter
@Entity(name = Order.ENTITY_NAME)
@Table(name = Order.TABLE_NAME, schema = "ecommerce", indexes = {
@Table(name = Order.TABLE_NAME, indexes = {
@Index(name = "customerNumber", columnList = "customerNumber")
})
public class Order implements Serializable {
Expand Down Expand Up @@ -50,8 +51,7 @@ public class Order implements Serializable {
@Enumerated(EnumType.STRING)
private OrderStatus status;

@Lob
@Column(name = COLUMN_COMMENTS_NAME)
@Column(name = COLUMN_COMMENTS_NAME, columnDefinition = "text")
private String comments;

@NotNull
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/syed/abdullah/demo/entity/Orderdetail.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@Getter
@Setter
@Entity(name = Orderdetail.ENTITY_NAME)
@Table(name = Orderdetail.TABLE_NAME, schema = "ecommerce", indexes = {
@Table(name = Orderdetail.TABLE_NAME, indexes = {
@Index(name = "productCode", columnList = "productCode")
})
public class Orderdetail implements Serializable {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/syed/abdullah/demo/entity/Payment.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@Getter
@Setter
@Entity(name = Payment.ENTITY_NAME)
@Table(name = Payment.TABLE_NAME, schema = "ecommerce")
@Table(name = Payment.TABLE_NAME)
public class Payment implements Serializable {
public static final String ENTITY_NAME = "Payment";
public static final String TABLE_NAME = "payments";
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/syed/abdullah/demo/entity/Product.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@Getter
@Setter
@Entity(name = Product.ENTITY_NAME)
@Table(name = Product.TABLE_NAME, schema = "ecommerce", indexes = {
@Table(name = Product.TABLE_NAME, indexes = {
@Index(name = "productLine", columnList = "productLine")
})
public class Product implements Serializable {
Expand Down Expand Up @@ -57,8 +57,7 @@ public class Product implements Serializable {
private String productVendor;

@NotNull
@Lob
@Column(name = COLUMN_PRODUCTDESCRIPTION_NAME, nullable = false)
@Column(name = COLUMN_PRODUCTDESCRIPTION_NAME, nullable = false, columnDefinition = "text")
private String productDescription;

@NotNull
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/syed/abdullah/demo/entity/Productline.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
@Getter
@Setter
@Entity(name = Productline.ENTITY_NAME)
@Table(name = Productline.TABLE_NAME, schema = "ecommerce")
@Table(name = Productline.TABLE_NAME)
public class Productline implements Serializable {
public static final String ENTITY_NAME = "Productline";
public static final String TABLE_NAME = "productlines";
Expand All @@ -35,8 +35,7 @@ public class Productline implements Serializable {
@Column(name = COLUMN_TEXTDESCRIPTION_NAME, length = 4000)
private String textDescription;

@Lob
@Column(name = COLUMN_HTMLDESCRIPTION_NAME)
@Column(name = COLUMN_HTMLDESCRIPTION_NAME, columnDefinition = "text")
private String htmlDescription;

@Column(name = COLUMN_IMAGE_NAME)
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/syed/abdullah/demo/entity/Wishlist.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@Getter
@Setter
@Entity(name = Wishlist.ENTITY_NAME)
@Table(name = Wishlist.TABLE_NAME, schema = "ecommerce", indexes = {
@Table(name = Wishlist.TABLE_NAME, indexes = {
@Index(name = "productCode", columnList = "productCode")
})
public class Wishlist implements Serializable {
Expand Down
50 changes: 26 additions & 24 deletions src/main/java/syed/abdullah/demo/repository/ProductRepository.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package syed.abdullah.demo.repository;

import io.swagger.v3.oas.annotations.Hidden;
import org.springframework.data.domain.Limit;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
Expand All @@ -14,30 +15,31 @@
@Hidden
public interface ProductRepository extends JpaRepository<Product, String>, JpaSpecificationExecutor<Product> {
@Query(value = """
SELECT p.* from products p
WHERE p.productCode in (select * from (
select p.productCode
from products p
inner join orderdetails o on p.productCode = o.productCode
inner join orders o2 on o.orderNumber = o2.orderNumber
WHERE DATE(o2.orderDate) BETWEEN :startDate AND :endDate and o2.status = 'SHIPPED'
Group by p.productCode
ORDER BY Sum(o.quantityOrdered) desc
LIMIT :topN) as tmp )
""", nativeQuery = true)
List<Product> getTopNProductsBetweenDatesBasedOnNumberOfSales(Integer topN, LocalDate startDate, LocalDate endDate);
SELECT p FROM Product p
WHERE p.productCode IN (
SELECT p.productCode FROM Order o
JOIN o.orderdetails od
JOIN od.productCode p
WHERE o.orderDate BETWEEN :startDate AND :endDate
AND o.status = 'SHIPPED'
GROUP BY p.productCode
ORDER BY SUM(od.quantityOrdered),p.productCode DESC
)
""")
List<Product> getTopNProductsBetweenDatesBasedOnNumberOfSales(LocalDate startDate, LocalDate endDate, Limit topN);

@Query(value = """
SELECT p.* from products p
WHERE p.productCode in (select * from (
select p.productCode
from products p
inner join orderdetails o on p.productCode = o.productCode
inner join orders o2 on o.orderNumber = o2.orderNumber
WHERE DATE(o2.orderDate) BETWEEN :startDate AND :endDate and o2.status = 'SHIPPED'
Group by p.productCode
ORDER BY Sum(o.quantityOrdered * o.priceEach) desc
LIMIT :topN) as tmp )
""", nativeQuery = true)
List<Product> getTopNProductsBetweenDatesBasedOnTotalSaleAmount(Integer topN, LocalDate startDate, LocalDate endDate);
SELECT p FROM Product p
WHERE p.productCode IN (
SELECT p.productCode FROM Order o
JOIN o.orderdetails od
JOIN od.productCode p
WHERE o.orderDate BETWEEN :startDate AND :endDate
AND o.status = 'SHIPPED'
GROUP BY p.productCode
ORDER BY SUM(od.quantityOrdered * od.priceEach),p.productCode DESC
)
""")
List<Product> getTopNProductsBetweenDatesBasedOnTotalSaleAmount(LocalDate startDate, LocalDate endDate,Limit topN);

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import jakarta.persistence.EntityNotFoundException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Limit;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import syed.abdullah.demo.entity.Customer;
Expand Down Expand Up @@ -77,7 +78,7 @@ public LocalDate getMaxSaleDay(LocalDate startDate, LocalDate endDate) {
*/
@Observed(name = "getTopNSellingItemsAllTime",contextualName = "getTopNSellingItemsAllTime")
public List<Product> getTopNSellingItemsAllTime(Integer number) {
return getTopNSellingItemsBetweenDatesBasedOnTotalSaleAmount(number, LocalDate.MIN, LocalDate.now());
return getTopNSellingItemsBetweenDatesBasedOnTotalSaleAmount(number, LocalDate.EPOCH, LocalDate.now());
}

/**
Expand All @@ -104,13 +105,13 @@ private LastMonth getLastMonth() {

@Observed(name = "getTopNSellingItemsBetweenDatesBasedOnNumberOfSales",contextualName = "getTopNSellingItemsBetweenDatesBasedOnNumberOfSales")
public List<Product> getTopNSellingItemsBetweenDatesBasedOnNumberOfSales(Integer number, LocalDate startDate, LocalDate endDate) {
List<Product> basedOnNumberOfSales = productRepository.getTopNProductsBetweenDatesBasedOnNumberOfSales(number, startDate, endDate);
List<Product> basedOnNumberOfSales = productRepository.getTopNProductsBetweenDatesBasedOnNumberOfSales(startDate, endDate, Limit.of(number));
return basedOnNumberOfSales;
}

@Observed(name = "getTopNSellingItemsBetweenDatesBasedOnTotalSaleAmount",contextualName = "getTopNSellingItemsBetweenDatesBasedOnTotalSaleAmount")
public List<Product> getTopNSellingItemsBetweenDatesBasedOnTotalSaleAmount(Integer number, LocalDate startDate, LocalDate endDate) {
List<Product> basedOnTotalSaleAmount = productRepository.getTopNProductsBetweenDatesBasedOnTotalSaleAmount(number, startDate, endDate);
List<Product> basedOnTotalSaleAmount = productRepository.getTopNProductsBetweenDatesBasedOnTotalSaleAmount(startDate, endDate, Limit.of(number));
return basedOnTotalSaleAmount;
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/main/resources/application-postgresdb.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
spring.application.name=demo

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl

spring.sql.init.mode=always
spring.test.database.replace=none
spring.datasource.url=jdbc:tc:pgvector/pgvector:pg16:///ecommerce
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=none
spring.sql.init.data-locations=classpath:postgresdb-schema-with-data.sql
spring.jpa.properties.hibernate.show_sql=true
logging.level.org.hibernate.orm.jdbc.bind=trace

## Disable liquibase as it contains MySQL schema files
spring.liquibase.enabled=false
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package syed.abdullah.demo.postgres.integration;

import org.junit.jupiter.api.BeforeAll;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.test.context.ActiveProfiles;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Testcontainers;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Testcontainers
@ActiveProfiles("postgresdb")
public abstract class AbstractIntegrationTestPostgresDB {

//@Container
@ServiceConnection
static PostgreSQLContainer<?> pgvectorDb = new PostgreSQLContainer<>("pgvector/pgvector:pg16");

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

0 comments on commit 1346aaa

Please sign in to comment.