This repository has been archived by the owner on Jun 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix : issue about shop_database datasource
add configuration files to write in the correct databse (shop_db) add unit tests linked #15
- Loading branch information
1 parent
8cae88d
commit b493ce1
Showing
20 changed files
with
218 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
Server/shop/product/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
Server/shop/product/src/test/java/com/cashmanager/server/product/ProductControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
5
Server/shop/product/src/test/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...tabase/src/main/java/com/cashmanager/server/shop_database/config/FlywayConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...base/src/main/java/com/cashmanager/server/shop_database/config/FlywayStartUpStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...main/java/com/cashmanager/server/shop_database/config/ProductDatasourceConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 7 additions & 3 deletions
10
Server/shop/shop_database/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
33 changes: 33 additions & 0 deletions
33
...op_database/src/test/java/com/cashmanager/server/shop_database/ProductRepositoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
Server/shop/shop_database/src/test/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |