diff --git a/Server/bank/database/src/test/java/com/cashmanager/server/database/DatabaseApplicationTests.java b/Server/bank/database/src/test/java/com/cashmanager/server/database/DatabaseApplicationTests.java index 9d72773..e718bcf 100644 --- a/Server/bank/database/src/test/java/com/cashmanager/server/database/DatabaseApplicationTests.java +++ b/Server/bank/database/src/test/java/com/cashmanager/server/database/DatabaseApplicationTests.java @@ -3,7 +3,7 @@ import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; -@SpringBootTest() +@SpringBootTest class DatabaseApplicationTests { @Test diff --git a/Server/init-shop-db.sh b/Server/init-shop-db.sh index ba1e30b..818f768 100644 --- a/Server/init-shop-db.sh +++ b/Server/init-shop-db.sh @@ -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 diff --git a/Server/shop/product/pom.xml b/Server/shop/product/pom.xml index 3078a74..19e1a3f 100644 --- a/Server/shop/product/pom.xml +++ b/Server/shop/product/pom.xml @@ -53,7 +53,16 @@ mapstruct 1.5.5.Final - + + com.h2database + h2 + test + + + junit + junit + test + diff --git a/Server/shop/product/src/main/java/com/cashmanager/server/product/controllers/ProductController.java b/Server/shop/product/src/main/java/com/cashmanager/server/product/controllers/ProductController.java index fdafdda..d4e33e6 100644 --- a/Server/shop/product/src/main/java/com/cashmanager/server/product/controllers/ProductController.java +++ b/Server/shop/product/src/main/java/com/cashmanager/server/product/controllers/ProductController.java @@ -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"), diff --git a/Server/shop/product/src/main/java/com/cashmanager/server/product/services/ProductService.java b/Server/shop/product/src/main/java/com/cashmanager/server/product/services/ProductService.java index 49554cf..b9878b3 100644 --- a/Server/shop/product/src/main/java/com/cashmanager/server/product/services/ProductService.java +++ b/Server/shop/product/src/main/java/com/cashmanager/server/product/services/ProductService.java @@ -32,7 +32,6 @@ public List 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); }); diff --git a/Server/shop/product/src/main/resources/application.properties b/Server/shop/product/src/main/resources/application.properties index 547ab32..36c1390 100644 --- a/Server/shop/product/src/main/resources/application.properties +++ b/Server/shop/product/src/main/resources/application.properties @@ -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 diff --git a/Server/shop/product/src/test/java/com/cashmanager/server/product/ProductApplicationTests.java b/Server/shop/product/src/test/java/com/cashmanager/server/product/ProductApplicationTests.java index 78d7afc..4e3a411 100644 --- a/Server/shop/product/src/test/java/com/cashmanager/server/product/ProductApplicationTests.java +++ b/Server/shop/product/src/test/java/com/cashmanager/server/product/ProductApplicationTests.java @@ -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() { + } } diff --git a/Server/shop/product/src/test/java/com/cashmanager/server/product/ProductControllerTest.java b/Server/shop/product/src/test/java/com/cashmanager/server/product/ProductControllerTest.java new file mode 100644 index 0000000..e47bd1f --- /dev/null +++ b/Server/shop/product/src/test/java/com/cashmanager/server/product/ProductControllerTest.java @@ -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 products = productService.getAllProducts(); + //assert that the list is not null + assertNotNull("This list should not be null",products); + } +} diff --git a/Server/shop/product/src/test/resources/application.properties b/Server/shop/product/src/test/resources/application.properties new file mode 100644 index 0000000..651e64d --- /dev/null +++ b/Server/shop/product/src/test/resources/application.properties @@ -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 \ No newline at end of file diff --git a/Server/shop/shop_database/pom.xml b/Server/shop/shop_database/pom.xml index 26b8784..236a384 100644 --- a/Server/shop/shop_database/pom.xml +++ b/Server/shop/shop_database/pom.xml @@ -57,6 +57,16 @@ spring-boot-starter-test test + + com.h2database + h2 + test + + + junit + junit + test + diff --git a/Server/shop/shop_database/src/main/java/com/cashmanager/server/shop_database/ShopDatabaseApplication.java b/Server/shop/shop_database/src/main/java/com/cashmanager/server/shop_database/ShopDatabaseApplication.java index 47a44fe..9578d7d 100644 --- a/Server/shop/shop_database/src/main/java/com/cashmanager/server/shop_database/ShopDatabaseApplication.java +++ b/Server/shop/shop_database/src/main/java/com/cashmanager/server/shop_database/ShopDatabaseApplication.java @@ -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 { diff --git a/Server/shop/shop_database/src/main/java/com/cashmanager/server/shop_database/config/FlywayConfiguration.java b/Server/shop/shop_database/src/main/java/com/cashmanager/server/shop_database/config/FlywayConfiguration.java new file mode 100644 index 0000000..f22bd7d --- /dev/null +++ b/Server/shop/shop_database/src/main/java/com/cashmanager/server/shop_database/config/FlywayConfiguration.java @@ -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(); + + } +} diff --git a/Server/shop/shop_database/src/main/java/com/cashmanager/server/shop_database/config/FlywayStartUpStrategy.java b/Server/shop/shop_database/src/main/java/com/cashmanager/server/shop_database/config/FlywayStartUpStrategy.java new file mode 100644 index 0000000..5ce346f --- /dev/null +++ b/Server/shop/shop_database/src/main/java/com/cashmanager/server/shop_database/config/FlywayStartUpStrategy.java @@ -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 + }; + } +} diff --git a/Server/shop/shop_database/src/main/java/com/cashmanager/server/shop_database/config/ProductDatasourceConfiguration.java b/Server/shop/shop_database/src/main/java/com/cashmanager/server/shop_database/config/ProductDatasourceConfiguration.java new file mode 100644 index 0000000..07c0b47 --- /dev/null +++ b/Server/shop/shop_database/src/main/java/com/cashmanager/server/shop_database/config/ProductDatasourceConfiguration.java @@ -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); + } +} diff --git a/Server/shop/shop_database/src/main/java/com/cashmanager/server/shop_database/entities/Product.java b/Server/shop/shop_database/src/main/java/com/cashmanager/server/shop_database/entities/Product.java index 70058b4..2199386 100644 --- a/Server/shop/shop_database/src/main/java/com/cashmanager/server/shop_database/entities/Product.java +++ b/Server/shop/shop_database/src/main/java/com/cashmanager/server/shop_database/entities/Product.java @@ -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; diff --git a/Server/shop/shop_database/src/main/resources/application.properties b/Server/shop/shop_database/src/main/resources/application.properties index fc097a2..3089f59 100644 --- a/Server/shop/shop_database/src/main/resources/application.properties +++ b/Server/shop/shop_database/src/main/resources/application.properties @@ -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 diff --git a/Server/shop/shop_database/src/test/java/com/cashmanager/server/shop_database/ProductRepositoryTest.java b/Server/shop/shop_database/src/test/java/com/cashmanager/server/shop_database/ProductRepositoryTest.java new file mode 100644 index 0000000..e37a337 --- /dev/null +++ b/Server/shop/shop_database/src/test/java/com/cashmanager/server/shop_database/ProductRepositoryTest.java @@ -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 products = productRepository.findAll(); + //assert that the data is recuperated + assertFalse("This list should not be empty", products.isEmpty()); + + + } +} diff --git a/Server/shop/shop_database/src/test/java/com/cashmanager/server/shop_database/ShopDatabaseApplicationTests.java b/Server/shop/shop_database/src/test/java/com/cashmanager/server/shop_database/ShopDatabaseApplicationTests.java index 8a64ea3..27f53ae 100644 --- a/Server/shop/shop_database/src/test/java/com/cashmanager/server/shop_database/ShopDatabaseApplicationTests.java +++ b/Server/shop/shop_database/src/test/java/com/cashmanager/server/shop_database/ShopDatabaseApplicationTests.java @@ -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() { + } } diff --git a/Server/shop/shop_database/src/test/resources/application.properties b/Server/shop/shop_database/src/test/resources/application.properties new file mode 100644 index 0000000..f5533d6 --- /dev/null +++ b/Server/shop/shop_database/src/test/resources/application.properties @@ -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 diff --git a/Server/shop/shop_database/src/test/resources/db/test.sql b/Server/shop/shop_database/src/test/resources/db/test.sql new file mode 100644 index 0000000..69f331f --- /dev/null +++ b/Server/shop/shop_database/src/test/resources/db/test.sql @@ -0,0 +1 @@ +INSERT INTO products (id,name,price,product_url,stock) VALUES ('ff13f62b-87bc-4a9e-825e-f5bbc99f5f95','product01',3.0,'test.com',100); \ No newline at end of file