diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/AuditableConfig.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/AuditableConfig.java new file mode 100644 index 0000000000000..02cc6a246e7a6 --- /dev/null +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/AuditableConfig.java @@ -0,0 +1,39 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.spring.data.cosmos.repository; + +import com.azure.spring.data.cosmos.core.mapping.EnableCosmosAuditing; +import org.springframework.context.annotation.Bean; + +public class AuditableConfig { + + @EnableCosmosAuditing(dateTimeProviderRef = "auditingDateTimeProvider") + public static class AuditableConfiguration { + @Bean(name = "auditingDateTimeProvider") + public StubDateTimeProvider stubDateTimeProvider() { + return new StubDateTimeProvider(); + } + + @Bean + public StubAuditorProvider auditorProvider() { + return new StubAuditorProvider(); + } + } + + @EnableCosmosAuditing(dateTimeProviderRef = "auditingDateTimeProvider") + public static class ReactiveAuditableConfiguration { + + @Bean(name = "auditingDateTimeProvider") + public StubDateTimeProvider stubDateTimeProvider() { + return new StubDateTimeProvider(); + } + + @Bean + public StubAuditorProvider auditorProvider() { + return new StubAuditorProvider(); + } + } + + +} diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/StubDateTimeProvider.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/StubDateTimeProvider.java index 2f268fc87aac8..a22f5b1b4b356 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/StubDateTimeProvider.java +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/StubDateTimeProvider.java @@ -11,6 +11,7 @@ public class StubDateTimeProvider implements DateTimeProvider { private OffsetDateTime now = OffsetDateTime.now(); + private volatile int modifiedTimes = 0; @Override public Optional getNow() { @@ -19,5 +20,10 @@ public Optional getNow() { public void setNow(OffsetDateTime now) { this.now = now; + modifiedTimes++; + } + + public int getModifiedTimes() { + return modifiedTimes; } } diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/TestRepositoryConfig.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/TestRepositoryConfig.java index 2668fd2c14990..9a8fd22f31c4b 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/TestRepositoryConfig.java +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/TestRepositoryConfig.java @@ -7,7 +7,6 @@ import com.azure.spring.data.cosmos.common.TestConstants; import com.azure.spring.data.cosmos.config.AbstractCosmosConfiguration; import com.azure.spring.data.cosmos.config.CosmosConfig; -import com.azure.spring.data.cosmos.core.mapping.EnableCosmosAuditing; import com.azure.spring.data.cosmos.core.mapping.event.SimpleCosmosMappingEventListener; import com.azure.spring.data.cosmos.repository.config.EnableCosmosRepositories; import com.azure.spring.data.cosmos.repository.config.EnableReactiveCosmosRepositories; @@ -23,7 +22,6 @@ @Configuration @PropertySource(value = { "classpath:application.properties" }) @EnableCosmosRepositories -@EnableCosmosAuditing(dateTimeProviderRef = "auditingDateTimeProvider") @EnableReactiveCosmosRepositories public class TestRepositoryConfig extends AbstractCosmosConfiguration { @Value("${cosmos.uri:}") @@ -77,16 +75,6 @@ protected String getDatabaseName() { return StringUtils.hasText(this.database) ? this.database : TestConstants.DB_NAME; } - @Bean(name = "auditingDateTimeProvider") - public StubDateTimeProvider stubDateTimeProvider() { - return new StubDateTimeProvider(); - } - - @Bean - public StubAuditorProvider auditorProvider() { - return new StubAuditorProvider(); - } - @Override protected Collection getMappingBasePackages() { final Package mappingBasePackage = getClass().getPackage(); diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/AuditableIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/AuditableIT.java index d674e783f1ebd..2cbf9f8bc1e11 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/AuditableIT.java +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/AuditableIT.java @@ -6,6 +6,7 @@ import com.azure.spring.data.cosmos.core.CosmosTemplate; import com.azure.spring.data.cosmos.domain.AuditableEntity; import com.azure.spring.data.cosmos.domain.AuditableIdGeneratedEntity; +import com.azure.spring.data.cosmos.repository.AuditableConfig; import com.azure.spring.data.cosmos.repository.StubAuditorProvider; import com.azure.spring.data.cosmos.repository.StubDateTimeProvider; import com.azure.spring.data.cosmos.repository.TestRepositoryConfig; @@ -13,8 +14,11 @@ import com.azure.spring.data.cosmos.repository.repository.AuditableRepository; import org.junit.Before; import org.junit.ClassRule; +import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runner.RunWith; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -26,9 +30,15 @@ import static org.assertj.core.api.Assertions.assertThat; @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = TestRepositoryConfig.class) +@ContextConfiguration(classes = { + TestRepositoryConfig.class, + AuditableConfig.AuditableConfiguration.class +}) +@FixMethodOrder public class AuditableIT { + private static final Logger LOGGER = LoggerFactory.getLogger(AuditableIT.class); + @ClassRule public static final IntegrationTestCollectionManager collectionManager = new IntegrationTestCollectionManager(); @@ -62,6 +72,7 @@ public void testInsertShouldSetAuditableEntries() { assertThat(savedEntity.getCreatedDate()).isEqualTo(now); assertThat(savedEntity.getLastModifiedBy()).isEqualTo("created-by"); assertThat(savedEntity.getLastModifiedByDate()).isEqualTo(now); + LOGGER.info("the modified times of stubDateTimeProvider is {}", stubDateTimeProvider.getModifiedTimes()); } @Test @@ -83,6 +94,7 @@ public void testUpdateShouldNotOverwriteCreatedEntries() { assertThat(modifiedEntity.getCreatedDate()).isEqualTo(createdOn); assertThat(modifiedEntity.getLastModifiedBy()).isEqualTo("modified-by"); assertThat(modifiedEntity.getLastModifiedByDate()).isEqualTo(modifiedOn); + LOGGER.info("the modified times of stubDateTimeProvider is {}", stubDateTimeProvider.getModifiedTimes()); } @Test @@ -98,6 +110,7 @@ public void testInsertShouldSetAuditableEntriesIfIdAutoGenerated() { assertThat(savedEntity.getCreatedDate()).isEqualTo(now); assertThat(savedEntity.getLastModifiedBy()).isEqualTo("created-by"); assertThat(savedEntity.getLastModifiedByDate()).isEqualTo(now); + LOGGER.info("the modified times of stubDateTimeProvider is {}", stubDateTimeProvider.getModifiedTimes()); } } diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ReactiveAuditableIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ReactiveAuditableIT.java index af357da4932b6..b045daf372989 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ReactiveAuditableIT.java +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ReactiveAuditableIT.java @@ -6,6 +6,7 @@ import com.azure.spring.data.cosmos.core.ReactiveCosmosTemplate; import com.azure.spring.data.cosmos.domain.AuditableEntity; import com.azure.spring.data.cosmos.domain.AuditableIdGeneratedEntity; +import com.azure.spring.data.cosmos.repository.AuditableConfig; import com.azure.spring.data.cosmos.repository.StubAuditorProvider; import com.azure.spring.data.cosmos.repository.StubDateTimeProvider; import com.azure.spring.data.cosmos.repository.TestRepositoryConfig; @@ -13,8 +14,11 @@ import com.azure.spring.data.cosmos.repository.repository.ReactiveAuditableRepository; import org.junit.Before; import org.junit.ClassRule; +import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runner.RunWith; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -28,9 +32,15 @@ import static org.assertj.core.api.Assertions.assertThat; @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = TestRepositoryConfig.class) +@ContextConfiguration(classes = { + TestRepositoryConfig.class, + AuditableConfig.ReactiveAuditableConfiguration.class +}) +@FixMethodOrder public class ReactiveAuditableIT { + private static final Logger LOGGER = LoggerFactory.getLogger(AuditableIT.class); + @ClassRule public static final ReactiveIntegrationTestCollectionManager collectionManager = new ReactiveIntegrationTestCollectionManager(); @@ -67,6 +77,7 @@ public void testInsertShouldSetAuditableEntries() { "created-by", now)) .verifyComplete(); + LOGGER.info("the modified times of stubDateTimeProvider is {}", stubDateTimeProvider.getModifiedTimes()); } @Test @@ -92,6 +103,7 @@ public void testUpdateShouldNotOverwriteCreatedEntries() { "modified-by", modifiedOn)) .verifyComplete(); + LOGGER.info("the modified times of stubDateTimeProvider is {}", stubDateTimeProvider.getModifiedTimes()); } private boolean validateAuditableFields(AuditableEntity entity, @@ -119,6 +131,7 @@ public void testInsertShouldSetAuditableEntriesIfIdAutoGenerated() { "created-by", now, "created-by", now)) .verifyComplete(); + LOGGER.info("the modified times of stubDateTimeProvider is {}", stubDateTimeProvider.getModifiedTimes()); } private boolean validateAuditableFields(AuditableIdGeneratedEntity entity,