From 1e7033532f6b5e54994421857b10f95f1810d585 Mon Sep 17 00:00:00 2001 From: FrancescoDiTonno-BVT Date: Tue, 29 Oct 2024 17:01:17 +0100 Subject: [PATCH] [PPABV-34] feat: pm database connection configured (#139) * feat: pm database connection configured * feat: new connection to the second db has been estabilished * fix: connection update to multiple databases with a single client --- docker-compose.yml | 25 ------- docker/mongoDB/conf/mongo-express-history.env | 3 - docker/mongoDB/conf/mongodb-history.env | 3 - docker/mongoDB/mongo-history-init.js | 75 ------------------- docker/mongoDB/mongo-init.js | 75 +++++++++++++++++++ ...gopaEcommerceHelpdeskServiceApplication.kt | 5 +- .../EcommerceHistoryMongoRepositoryConfig.kt | 11 +++ .../configurations/MongoEcommerceConfig.kt | 12 ++- src/main/resources/application.properties | 8 +- .../MongoEcommerceConfigTest.kt | 20 ++++- src/test/resources/application.properties | 7 +- 11 files changed, 126 insertions(+), 118 deletions(-) delete mode 100644 docker/mongoDB/conf/mongo-express-history.env delete mode 100644 docker/mongoDB/conf/mongodb-history.env delete mode 100644 docker/mongoDB/mongo-history-init.js create mode 100644 src/main/kotlin/it/pagopa/ecommerce/helpdesk/configurations/EcommerceHistoryMongoRepositoryConfig.kt diff --git a/docker-compose.yml b/docker-compose.yml index 654780a8..7ab98573 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -56,31 +56,6 @@ services: depends_on: - mongo - mongo-history: - container_name: pagopa-history-helpdesk-mongo - image: mongo - env_file: docker/mongoDB/conf/mongodb-history.env - ports: - - "27018:27017" - networks: - - pagopa-ecommerce-net - healthcheck: - test: echo "db.stats().ok" | mongo --quiet - interval: 10s - volumes: - - ./docker/mongoDB/mongo-history-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro - - mongo-express-history: - container_name: pagopa-history-helpdesk-express - image: mongo-express - env_file: docker/mongoDB/conf/mongo-express-history.env - ports: - - "8082:8081" - networks: - - pagopa-ecommerce-net - depends_on: - - mongo-history - pagopa-pdv-mock: container_name: pagopa-pdv-mock-helpdesk build: diff --git a/docker/mongoDB/conf/mongo-express-history.env b/docker/mongoDB/conf/mongo-express-history.env deleted file mode 100644 index 36299692..00000000 --- a/docker/mongoDB/conf/mongo-express-history.env +++ /dev/null @@ -1,3 +0,0 @@ -MONGO_INITDB_ROOT_USERNAME=admin -MONGO_INITDB_ROOT_PASSWORD=password -ME_CONFIG_MONGODB_URL=mongodb://$MONGO_INITDB_ROOT_USERNAME:$MONGO_INITDB_ROOT_PASSWORD@pagopa-history-helpdesk-mongo:27017/ \ No newline at end of file diff --git a/docker/mongoDB/conf/mongodb-history.env b/docker/mongoDB/conf/mongodb-history.env deleted file mode 100644 index affa2a10..00000000 --- a/docker/mongoDB/conf/mongodb-history.env +++ /dev/null @@ -1,3 +0,0 @@ -MONGO_INITDB_ROOT_USERNAME=admin -MONGO_INITDB_ROOT_PASSWORD=password -MONGO_INITDB_DATABASE=history \ No newline at end of file diff --git a/docker/mongoDB/mongo-history-init.js b/docker/mongoDB/mongo-history-init.js deleted file mode 100644 index 33eaaca4..00000000 --- a/docker/mongoDB/mongo-history-init.js +++ /dev/null @@ -1,75 +0,0 @@ -conn = new Mongo(); -db = conn.getDB("history"); - -db.getCollection('pm-transactions-history').insertMany([{ - "userInfo": { - "userFiscalCode": "RSSMRA80A01H501U", - "notificationEmail": "mario.rossi@example.com", - "authenticationType": 1 - }, - "transactionInfo": { - "creationDate": "2024-05-10T11:14:32.556+02:00", - "status": 1, - "statusDetails": 1, - "amount": 30000, - "fee": 100, - "grandTotal": 30100, - "rrn": "241310000102", - "authorizationCode": "00", - "paymentMethodName": "Pagamento con carte" - }, - "paymentInfo": { - "origin": "CITTADINANZA_DIGITALE", - "details": [ - { - "subject": "TARI 2021", - "iuv": "02055446688775544", - "idTransaction": "feae3da1480841a6ba11520cbd36090b", - "creditorInstitution": "EC_TE", - "paFiscalCode": "77777777777" - } - ] - }, - "pspInfo": { - "pspId": "UNCRITMM", - "businessName": "UniCredit S.p.A", - "idChannel": "00348170101_01_ONUS" - }, - "product": "PM" -}, -{ - "userInfo": { - "userFiscalCode": "NVEGVN80A01H501U", - "notificationEmail": "giovanni.neve@test.com", - "authenticationType": 1 - }, - "transactionInfo": { - "creationDate": "2024-06-10T11:14:32.556+02:00", - "status": 1, - "statusDetails": 1, - "amount": 45000, - "fee": 150, - "grandTotal": 45150, - "rrn": "241310000103", - "authorizationCode": "01", - "paymentMethodName": "Pagamento con carte" - }, - "paymentInfo": { - "origin": "CITTADINANZA_DIGITALE", - "details": [ - { - "subject": "TARI 2022", - "iuv": "02055446688775545", - "idTransaction": "feae3da1480841a6ba11520cbd36091c", - "creditorInstitution": "EC_TE", - "paFiscalCode": "77777777777" - } - ] - }, - "pspInfo": { - "pspId": "UNCRITMM", - "businessName": "UniCredit S.p.A", - "idChannel": "00348170101_01_ONUS" - }, - "product": "PM" -}]); \ No newline at end of file diff --git a/docker/mongoDB/mongo-init.js b/docker/mongoDB/mongo-init.js index 5bbff449..09499cec 100644 --- a/docker/mongoDB/mongo-init.js +++ b/docker/mongoDB/mongo-init.js @@ -326,3 +326,78 @@ db.getCollection('dead-letter-events').insertMany([{ "insertionDate": "2023-11-30T14:50:27.001Z", "data": "NOTIFICATIONS_SERVICE" }]); + +db2 = conn.getDB("ecommerce-history"); + +db2.getCollection('pm-transactions-history').insertMany([{ + "userInfo": { + "userFiscalCode": "RSSMRA80A01H501U", + "notificationEmail": "mario.rossi@example.com", + "authenticationType": 1 + }, + "transactionInfo": { + "creationDate": "2024-05-10T11:14:32.556+02:00", + "status": 1, + "statusDetails": 1, + "amount": 30000, + "fee": 100, + "grandTotal": 30100, + "rrn": "241310000102", + "authorizationCode": "00", + "paymentMethodName": "Pagamento con carte" + }, + "paymentInfo": { + "origin": "CITTADINANZA_DIGITALE", + "details": [ + { + "subject": "TARI 2021", + "iuv": "02055446688775544", + "idTransaction": "feae3da1480841a6ba11520cbd36090b", + "creditorInstitution": "EC_TE", + "paFiscalCode": "77777777777" + } + ] + }, + "pspInfo": { + "pspId": "UNCRITMM", + "businessName": "UniCredit S.p.A", + "idChannel": "00348170101_01_ONUS" + }, + "product": "PM" +}, +{ + "userInfo": { + "userFiscalCode": "NVEGVN80A01H501U", + "notificationEmail": "giovanni.neve@test.com", + "authenticationType": 1 + }, + "transactionInfo": { + "creationDate": "2024-06-10T11:14:32.556+02:00", + "status": 1, + "statusDetails": 1, + "amount": 45000, + "fee": 150, + "grandTotal": 45150, + "rrn": "241310000103", + "authorizationCode": "01", + "paymentMethodName": "Pagamento con carte" + }, + "paymentInfo": { + "origin": "CITTADINANZA_DIGITALE", + "details": [ + { + "subject": "TARI 2022", + "iuv": "02055446688775545", + "idTransaction": "feae3da1480841a6ba11520cbd36091c", + "creditorInstitution": "EC_TE", + "paFiscalCode": "77777777777" + } + ] + }, + "pspInfo": { + "pspId": "UNCRITMM", + "businessName": "UniCredit S.p.A", + "idChannel": "00348170101_01_ONUS" + }, + "product": "PM" +}]); \ No newline at end of file diff --git a/src/main/kotlin/it/pagopa/ecommerce/helpdesk/PagopaEcommerceHelpdeskServiceApplication.kt b/src/main/kotlin/it/pagopa/ecommerce/helpdesk/PagopaEcommerceHelpdeskServiceApplication.kt index 97a4fdaf..b1c5baae 100644 --- a/src/main/kotlin/it/pagopa/ecommerce/helpdesk/PagopaEcommerceHelpdeskServiceApplication.kt +++ b/src/main/kotlin/it/pagopa/ecommerce/helpdesk/PagopaEcommerceHelpdeskServiceApplication.kt @@ -1,10 +1,13 @@ package it.pagopa.ecommerce.helpdesk import org.springframework.boot.autoconfigure.SpringBootApplication +import org.springframework.boot.autoconfigure.data.mongo.MongoReactiveDataAutoConfiguration import org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration import org.springframework.boot.runApplication -@SpringBootApplication(exclude = [MongoReactiveAutoConfiguration::class]) +@SpringBootApplication( + exclude = [MongoReactiveAutoConfiguration::class, MongoReactiveDataAutoConfiguration::class] +) class PagopaEcommerceHelpdeskServiceApplication fun main(args: Array) { diff --git a/src/main/kotlin/it/pagopa/ecommerce/helpdesk/configurations/EcommerceHistoryMongoRepositoryConfig.kt b/src/main/kotlin/it/pagopa/ecommerce/helpdesk/configurations/EcommerceHistoryMongoRepositoryConfig.kt new file mode 100644 index 00000000..00b5bfd9 --- /dev/null +++ b/src/main/kotlin/it/pagopa/ecommerce/helpdesk/configurations/EcommerceHistoryMongoRepositoryConfig.kt @@ -0,0 +1,11 @@ +package it.pagopa.ecommerce.helpdesk.configurations + +import org.springframework.context.annotation.Configuration +import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories + +@Configuration +@EnableReactiveMongoRepositories( + basePackages = ["it.pagopa.ecommerce.helpdesk.dataproviders.repositories.history"], + reactiveMongoTemplateRef = "ecommerceHistoryReactiveMongoTemplate" +) +class EcommerceHistoryMongoRepositoryConfig {} diff --git a/src/main/kotlin/it/pagopa/ecommerce/helpdesk/configurations/MongoEcommerceConfig.kt b/src/main/kotlin/it/pagopa/ecommerce/helpdesk/configurations/MongoEcommerceConfig.kt index 81a11482..5106f05b 100644 --- a/src/main/kotlin/it/pagopa/ecommerce/helpdesk/configurations/MongoEcommerceConfig.kt +++ b/src/main/kotlin/it/pagopa/ecommerce/helpdesk/configurations/MongoEcommerceConfig.kt @@ -14,7 +14,7 @@ import org.springframework.data.mongodb.core.ReactiveMongoTemplate class MongoEcommerceConfig() { @Bean(name = ["ecommerceReactiveMongoClient"]) fun ecommerceReactiveMongoClient( - @Value("\${ecommerce.mongodb.uri}") uri: String, + @Value("\${mongodb.uri}") uri: String, ): MongoClient { val connectionString = ConnectionString(uri) val settings = MongoClientSettings.builder().applyConnectionString(connectionString).build() @@ -24,7 +24,15 @@ class MongoEcommerceConfig() { @Bean(name = ["ecommerceReactiveMongoTemplate"]) fun ecommerceReactiveMongoTemplate( @Qualifier("ecommerceReactiveMongoClient") mongoClient: MongoClient, - @Value("\${ecommerce.mongodb.database}") database: String + @Value("\${mongodb.ecommerce.database}") database: String + ): ReactiveMongoTemplate { + return ReactiveMongoTemplate(mongoClient, database) + } + + @Bean(name = ["ecommerceHistoryReactiveMongoTemplate"]) + fun ecommerceHistoryReactiveMongoTemplate( + @Qualifier("ecommerceReactiveMongoClient") mongoClient: MongoClient, + @Value("\${mongodb.ecommerce_history.database}") database: String ): ReactiveMongoTemplate { return ReactiveMongoTemplate(mongoClient, database) } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 5dcde9b3..35ed8fae 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -27,9 +27,11 @@ pm.oracle.password=\${PM_ORACLE_PASSWORD} #spring.data.mongodb.uri=mongodb://\${MONGO_USERNAME}:\${MONGO_PASSWORD}@\${MONGO_HOST}:\${MONGO_PORT}/?ssl=\${MONGO_SSL_ENABLED}&readPreference=secondary&minPoolSize=\${MONGO_MIN_POOL_SIZE}&maxPoolSize=\${MONGO_MAX_POOL_SIZE}&maxIdleTimeMS=\${MONGO_MAX_IDLE_TIMEOUT_MS}&connectTimeoutMS=\${MONGO_CONNECTION_TIMEOUT_MS}&socketTimeoutMS=\${MONGO_SOCKET_TIMEOUT_MS}&serverSelectionTimeoutMS=\${MONGO_SERVER_SELECTION_TIMEOUT_MS}&waitQueueTimeoutMS=\${MONGO_WAITING_QUEUE_MS}&heartbeatFrequencyMS=\${MONGO_HEARTBEAT_FREQUENCY_MS}\${MONGO_REPLICA_SET_OPTION:} #spring.data.mongodb.database=ecommerce -#Ecommerce mongo database configuration -ecommerce.mongodb.uri=mongodb://\${MONGO_USERNAME}:\${MONGO_PASSWORD}@\${MONGO_HOST}:\${MONGO_PORT}/?ssl=\${MONGO_SSL_ENABLED}&readPreference=secondary&minPoolSize=\${MONGO_MIN_POOL_SIZE}&maxPoolSize=\${MONGO_MAX_POOL_SIZE}&maxIdleTimeMS=\${MONGO_MAX_IDLE_TIMEOUT_MS}&connectTimeoutMS=\${MONGO_CONNECTION_TIMEOUT_MS}&socketTimeoutMS=\${MONGO_SOCKET_TIMEOUT_MS}&serverSelectionTimeoutMS=\${MONGO_SERVER_SELECTION_TIMEOUT_MS}&waitQueueTimeoutMS=\${MONGO_WAITING_QUEUE_MS}&heartbeatFrequencyMS=\${MONGO_HEARTBEAT_FREQUENCY_MS}\${MONGO_REPLICA_SET_OPTION:} -ecommerce.mongodb.database=ecommerce +#Ecommerce mongo database configurations +mongodb.uri=mongodb://\${MONGO_USERNAME}:\${MONGO_PASSWORD}@\${MONGO_HOST}:\${MONGO_PORT}/?ssl=\${MONGO_SSL_ENABLED}&readPreference=secondary&minPoolSize=\${MONGO_MIN_POOL_SIZE}&maxPoolSize=\${MONGO_MAX_POOL_SIZE}&maxIdleTimeMS=\${MONGO_MAX_IDLE_TIMEOUT_MS}&connectTimeoutMS=\${MONGO_CONNECTION_TIMEOUT_MS}&socketTimeoutMS=\${MONGO_SOCKET_TIMEOUT_MS}&serverSelectionTimeoutMS=\${MONGO_SERVER_SELECTION_TIMEOUT_MS}&waitQueueTimeoutMS=\${MONGO_WAITING_QUEUE_MS}&heartbeatFrequencyMS=\${MONGO_HEARTBEAT_FREQUENCY_MS}\${MONGO_REPLICA_SET_OPTION:} +mongodb.ecommerce.database=ecommerce +mongodb.ecommerce_history.database=ecommerce-history + deadLetter.queueMapping=\${SEARCH_DEAD_LETTER_QUEUE_MAPPING} #PDV configuration diff --git a/src/test/kotlin/it/pagopa/ecommerce/helpdesk/configurations/MongoEcommerceConfigTest.kt b/src/test/kotlin/it/pagopa/ecommerce/helpdesk/configurations/MongoEcommerceConfigTest.kt index 8a621d23..eee82018 100644 --- a/src/test/kotlin/it/pagopa/ecommerce/helpdesk/configurations/MongoEcommerceConfigTest.kt +++ b/src/test/kotlin/it/pagopa/ecommerce/helpdesk/configurations/MongoEcommerceConfigTest.kt @@ -9,7 +9,8 @@ class MongoEcommerceConfigTest { private val mongoEcommerceConfig = MongoEcommerceConfig() private val uri = "mongodb://localhost:27017/test" private val mongoClient: MongoClient = mongoEcommerceConfig.ecommerceReactiveMongoClient(uri) - val database = "ecommerce" + val database_ecommerce = "ecommerce" + val database_ecommerce_history = "ecommerce-history" @Test fun `ecommerceReactiveMongoClient should create MongoClient with correct settings`() { @@ -19,10 +20,23 @@ class MongoEcommerceConfigTest { @Test fun `ecommerceReactiveMongoTemplate should create ReactiveMongoTemplate with correct client and database`() { val reactiveMongoTemplate = - mongoEcommerceConfig.ecommerceReactiveMongoTemplate(mongoClient, database) + mongoEcommerceConfig.ecommerceReactiveMongoTemplate(mongoClient, database_ecommerce) assertThat(reactiveMongoTemplate).isNotNull assertThat(reactiveMongoTemplate.mongoDatabaseFactory.mongoDatabase.block()?.name) - .isEqualTo(database) + .isEqualTo(database_ecommerce) + } + + @Test + fun `ecommerceHistoryReactiveMongoTemplate should create ReactiveMongoTemplate with correct client and database`() { + val reactiveMongoTemplate = + mongoEcommerceConfig.ecommerceHistoryReactiveMongoTemplate( + mongoClient, + database_ecommerce_history + ) + + assertThat(reactiveMongoTemplate).isNotNull + assertThat(reactiveMongoTemplate.mongoDatabaseFactory.mongoDatabase.block()?.name) + .isEqualTo(database_ecommerce_history) } } diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties index 33e73dd9..bdffcf70 100644 --- a/src/test/resources/application.properties +++ b/src/test/resources/application.properties @@ -26,9 +26,10 @@ pm.oracle.password=test #spring.data.mongodb.uri=mongodb://mongo:mongo@$mongo:6080/?ssl=true&replicaSet=globaldb #spring.data.mongodb.database=ecommerce -#Ecommerce mongo database configuration -ecommerce.mongodb.uri=mongodb://mongo:mongo@$mongo:6080/?ssl=true&replicaSet=globaldb -ecommerce.mongodb.database=ecommerce +#Ecommerce mongo database configurations +mongodb.uri=mongodb://mongo:mongo@$mongo:6081/?ssl=true&replicaSet=globaldb +mongodb.ecommerce.database=ecommerce +mongodb.ecommerce_history.database=ecommerce-history deadLetter.queueMapping={ ALL: '*', ECOMMERCE: 'pagopa-ecommerce-transactions-dead-letter-queue', NOTIFICATIONS_SERVICE: 'pagopa-ecommerce-notifications-service-errors-queue' }