Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Commit

Permalink
fix : issue about shop_database datasource
Browse files Browse the repository at this point in the history
add configuration files to write in the correct databse (shop_db)
add unit tests

linked #15
  • Loading branch information
MeloLawson committed Jan 2, 2024
1 parent 8cae88d commit b493ce1
Show file tree
Hide file tree
Showing 20 changed files with 218 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest()
@SpringBootTest
class DatabaseApplicationTests {

@Test
Expand Down
1 change: 1 addition & 0 deletions Server/init-shop-db.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-E
CREATE USER "$SHOP_USER" WITH PASSWORD '$SHOP_PASSWORD';
CREATE DATABASE "$SHOP_DB";
GRANT ALL PRIVILEGES ON DATABASE "$SHOP_DB" TO "$SHOP_USER";
ALTER DATABASE "$SHOP_DB" OWNER TO "$SHOP_USER";
EOSQL
11 changes: 10 additions & 1 deletion Server/shop/product/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,16 @@
<artifactId>mapstruct</artifactId>
<version>1.5.5.Final</version>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public ProductController(IProductService productService) {
}


@GetMapping("/")
@GetMapping("")
@Operation(summary = "Get all the products available in the database", description = "Returns a list of products")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Successfully retrieved"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public List<ProductDto> getAllProducts() throws ProductException {
}
else{
products.forEach(product -> {
// ProductDto productDto = new ProductDto(product.getId(),product.getName(),product.getPrice(), product.getStock());
ProductDto productDto = ProductMapper.INSTANCE.productToProductDto(product);
productDtos.add(productDto);
});
Expand Down
8 changes: 4 additions & 4 deletions Server/shop/product/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
spring.application.name=PRODUCT-SERVICE
server.port=8081
spring.datasource.url=jdbc:postgresql://localhost:5432/bank_db
spring.datasource.username=bank_user
spring.datasource.password=bank_password
server.port=8082
spring.datasource.url=jdbc:postgresql://localhost:5432/shop_db
spring.datasource.username=shop_user
spring.datasource.password=shop_password



Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Profile;

@SpringBootTest
@Profile("test")
class ProductApplicationTests {
//
// @Test
// void contextLoads() {
// }

@Test
void contextLoads() {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.cashmanager.server.product;

import com.cashmanager.server.common.dto.ProductDto;
import com.cashmanager.server.product.services.interfaces.IProductService;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Profile;


import java.util.List;
import static org.springframework.test.util.AssertionErrors.assertNotNull;

@SpringBootTest
@Profile("test")
public class ProductControllerTest {

@MockBean
private IProductService productService;

@Test
public void testGetProducts() throws Exception {
//get the products
List<ProductDto> products = productService.getAllProducts();
//assert that the list is not null
assertNotNull("This list should not be null",products);
}
}
5 changes: 5 additions & 0 deletions Server/shop/product/src/test/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
spring.datasource.username=shop_user
spring.datasource.password=shop_password
spring.profiles.active=test
10 changes: 10 additions & 0 deletions Server/shop/shop_database/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Profile;

@SpringBootApplication
public class ShopDatabaseApplication {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.cashmanager.server.shop_database.config;


import jakarta.annotation.PostConstruct;
import org.flywaydb.core.Flyway;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Profile("dev")
@Configuration
public class FlywayConfiguration {


@PostConstruct
public void migrateFlyway(){
Flyway flyway = Flyway.configure()
.dataSource("jdbc:postgresql://localhost:5432/shop_db","shop_user","shop_password")
.load();

flyway.migrate();

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.cashmanager.server.shop_database.config;

import org.springframework.boot.autoconfigure.flyway.FlywayMigrationStrategy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Profile("dev")

@Configuration
public class FlywayStartUpStrategy {
@Bean
public FlywayMigrationStrategy flywayMigrationStrategy() {
return flyway -> {
// do nothing
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.cashmanager.server.shop_database.config;

import jakarta.persistence.EntityManagerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@Profile("dev")

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "productEntityManagerFactory",
transactionManagerRef = "productTransactionManager",
basePackages = {"com.cashmanager.server.shop_database.repositories"})
public class ProductDatasourceConfiguration {

@Primary
@Bean(name="productProperties")
@ConfigurationProperties("spring.datasource.product")
public DataSourceProperties dataSourceProperties(){
return new DataSourceProperties();
}

@Primary
@Bean(name="productDataSource")
public DataSource dataSource(@Qualifier("productProperties") DataSourceProperties properties){
return properties.initializeDataSourceBuilder().build();
}

@Primary
@Bean(name = "productEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean (EntityManagerFactoryBuilder builder, @Qualifier("productDataSource") DataSource dataSource){
return builder.dataSource(dataSource)
.packages("com.cashmanager.server.shop_database.entities")
.persistenceUnit("products").build();
}

@Primary
@Bean(name="productTransactionManager")
@ConfigurationProperties("spring.jpa")
public PlatformTransactionManager transactionManager(@Qualifier("productEntityManagerFactory")EntityManagerFactory entityManagerFactory){
return new JpaTransactionManager(entityManagerFactory);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class Product {
private String name;
@Column( nullable = false)
private BigDecimal price;
@Column( nullable = false)
@Column(name = "product_url", nullable = false)
private String productUrl;
@Column( nullable = false)
private Integer stock;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# WARNING!!! Currently this launching shop will create tables in bank database
spring.application.name=SHOP-DATABASE-SERVICE
spring.profiles.active=dev
server.port=8081
# postgresql database url for unit tests
spring.datasource.url=jdbc:postgresql://localhost:5432/shop_db
spring.datasource.product.url=jdbc:postgresql://localhost:5432/shop_db
# postgresql database username
spring.datasource.username=shop_user
spring.datasource.product.username=shop_user
# postgresql database password
spring.datasource.password=shop_password
spring.datasource.product.password=shop_password
spring.jpa.properties.hibernate.show_sql=true

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.cashmanager.server.shop_database;

import com.cashmanager.server.shop_database.entities.Product;
import com.cashmanager.server.shop_database.repositories.ProductRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Profile;
import org.springframework.test.context.jdbc.Sql;

import java.util.List;

import static org.springframework.test.util.AssertionErrors.assertFalse;

@SpringBootTest
@Profile("test")
public class ProductRepositoryTest {

@Autowired
private ProductRepository productRepository;

@Test
@Sql({"/db/test.sql"})
public void getProducts(){
//get the products in the database
List<Product> products = productRepository.findAll();
//assert that the data is recuperated
assertFalse("This list should not be empty", products.isEmpty());


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Profile;

@SpringBootTest
@Profile("test")
class ShopDatabaseApplicationTests {

// @Test
// void contextLoads() {
// }
@Test
void contextLoads() {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
spring.datasource.username=shop_user
spring.datasource.password=shop_password
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.profiles.active=test
1 change: 1 addition & 0 deletions Server/shop/shop_database/src/test/resources/db/test.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INSERT INTO products (id,name,price,product_url,stock) VALUES ('ff13f62b-87bc-4a9e-825e-f5bbc99f5f95','product01',3.0,'test.com',100);

0 comments on commit b493ce1

Please sign in to comment.