Skip to content

Commit

Permalink
revert to previous version
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaconsalvi committed Nov 27, 2024
1 parent 1fc83a2 commit 291e9c0
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

}
Original file line number Diff line number Diff line change
@@ -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;
}
}
7 changes: 3 additions & 4 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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());

}
}

0 comments on commit 291e9c0

Please sign in to comment.