diff --git a/src/main/java/it/gov/pagopa/rtp/activator/configuration/CosmosDBConfig.java b/src/main/java/it/gov/pagopa/rtp/activator/configuration/CosmosDBConfig.java index 89fd772..fd3d34f 100644 --- a/src/main/java/it/gov/pagopa/rtp/activator/configuration/CosmosDBConfig.java +++ b/src/main/java/it/gov/pagopa/rtp/activator/configuration/CosmosDBConfig.java @@ -3,16 +3,31 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import com.azure.spring.cloud.autoconfigure.implementation.context.properties.AzureGlobalProperties; +import com.azure.cosmos.CosmosClientBuilder; +import com.azure.spring.data.cosmos.config.AbstractCosmosConfiguration; import com.azure.spring.data.cosmos.repository.config.EnableReactiveCosmosRepositories; @Configuration @EnableReactiveCosmosRepositories("it.gov.pagopa.rtp.activator.repository") -public class CosmosDBConfig { +public class CosmosDBConfig extends AbstractCosmosConfiguration { + + private CosmosPropertiesConfig cosmosPropertiesConfig; + + public CosmosDBConfig(CosmosPropertiesConfig cosmosPropertiesConfig) { + this.cosmosPropertiesConfig = cosmosPropertiesConfig; + } + + @Override + protected String getDatabaseName() { + return cosmosPropertiesConfig.getDatabase(); + } @Bean - public AzureGlobalProperties azureGlobalProperties() { - return new AzureGlobalProperties(); + public CosmosClientBuilder getCosmosClientBuilder() { + + return new CosmosClientBuilder() + .endpoint(cosmosPropertiesConfig.getUri()) + .key(cosmosPropertiesConfig.getKey()); } } diff --git a/src/main/java/it/gov/pagopa/rtp/activator/configuration/CosmosPropertiesConfig.java b/src/main/java/it/gov/pagopa/rtp/activator/configuration/CosmosPropertiesConfig.java new file mode 100644 index 0000000..0401e26 --- /dev/null +++ b/src/main/java/it/gov/pagopa/rtp/activator/configuration/CosmosPropertiesConfig.java @@ -0,0 +1,21 @@ +package it.gov.pagopa.rtp.activator.configuration; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +import lombok.Getter; +import lombok.Setter; + +@ConfigurationProperties(prefix = "azure.cosmos") +@Getter +@Setter +public class CosmosPropertiesConfig { + private String database; + private String uri; + private String key; + + public CosmosPropertiesConfig(String database, String uri, String key) { + this.database = database; + this.uri = uri; + this.key = key; + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 60aced9..01c90d2 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -10,8 +10,7 @@ management.endpoint.health.probes.enabled=true #repository -spring.cloud.azure.cosmos.endpoint=${COSMOS_ACCOUNT_RTP_ENDPOINT:} -spring.cloud.azure.cosmos.key=${AZURE_COSMOS_KEY:} -spring.cloud.azure.cosmos.database=${DB_NAME:rtp} -spring.cloud.azure.cosmos.populate-query-metrics=true +azure.cosmos.uri=${COSMOS_ACCOUNT_RTP_ENDPOINT:} +azure.cosmos.key=${AZURE_COSMOS_KEY:} +azure.cosmos.database=${DB_NAME:rtp} activation.base-url=${BASE_URL:http://localhost} diff --git a/src/test/java/it/gov/pagopa/rtp/activator/configuration/CosmosDBConfigTest.java b/src/test/java/it/gov/pagopa/rtp/activator/configuration/CosmosDBConfigTest.java new file mode 100644 index 0000000..6ec6f87 --- /dev/null +++ b/src/test/java/it/gov/pagopa/rtp/activator/configuration/CosmosDBConfigTest.java @@ -0,0 +1,44 @@ +package it.gov.pagopa.rtp.activator.configuration; + +import com.azure.cosmos.CosmosClientBuilder; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; + +import org.mockito.junit.jupiter.MockitoExtension; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.verify; + +@ExtendWith(MockitoExtension.class) +class CosmosDBConfigTest { + + @Mock + private CosmosPropertiesConfig cosmosPropertiesConfig; + + @InjectMocks + private CosmosDBConfig cosmosDBConfig; + + @BeforeEach + void setUp() { + lenient().when(cosmosPropertiesConfig.getDatabase()).thenReturn("test-db"); + lenient().when(cosmosPropertiesConfig.getUri()).thenReturn("https://test-endpoint:443/"); + } + + @Test + void testGetDatabaseName() { + String database = cosmosDBConfig.getDatabaseName(); + assertEquals("test-db", database); + } + + @Test + void testGetCosmosClientBuilder() { + CosmosClientBuilder builder = cosmosDBConfig.getCosmosClientBuilder(); + verify(cosmosPropertiesConfig).getUri(); + assertNotNull(builder); + } +} diff --git a/src/test/java/it/gov/pagopa/rtp/activator/configuration/CosmosPropertiesConfigTest.java b/src/test/java/it/gov/pagopa/rtp/activator/configuration/CosmosPropertiesConfigTest.java new file mode 100644 index 0000000..06d7b46 --- /dev/null +++ b/src/test/java/it/gov/pagopa/rtp/activator/configuration/CosmosPropertiesConfigTest.java @@ -0,0 +1,28 @@ +package it.gov.pagopa.rtp.activator.configuration; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@ExtendWith(SpringExtension.class) +@EnableConfigurationProperties(value = CosmosPropertiesConfig.class) +@TestPropertySource("classpath:application.properties") +class CosmosPropertiesConfigTest { + + @Autowired + private CosmosPropertiesConfig cosmosPropertiesConfig; + + @Test + void testPropertiesLoaded() { + assertNotNull(cosmosPropertiesConfig); + assertEquals("https://example.com/db/endpoint", cosmosPropertiesConfig.getUri()); + assertEquals("rtp", cosmosPropertiesConfig.getDatabase()); + + } +} \ No newline at end of file