Skip to content

Commit

Permalink
try to fix OffsetDateTime issue
Browse files Browse the repository at this point in the history
  • Loading branch information
saragluna committed Oct 12, 2022
1 parent 953dfaa commit 8045613
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -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();
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
public class StubDateTimeProvider implements DateTimeProvider {

private OffsetDateTime now = OffsetDateTime.now();
private volatile int modifiedTimes = 0;

@Override
public Optional<TemporalAccessor> getNow() {
Expand All @@ -19,5 +20,10 @@ public Optional<TemporalAccessor> getNow() {

public void setNow(OffsetDateTime now) {
this.now = now;
modifiedTimes++;
}

public int getModifiedTimes() {
return modifiedTimes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,7 +22,6 @@
@Configuration
@PropertySource(value = { "classpath:application.properties" })
@EnableCosmosRepositories
@EnableCosmosAuditing(dateTimeProviderRef = "auditingDateTimeProvider")
@EnableReactiveCosmosRepositories
public class TestRepositoryConfig extends AbstractCosmosConfiguration {
@Value("${cosmos.uri:}")
Expand Down Expand Up @@ -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<String> getMappingBasePackages() {
final Package mappingBasePackage = getClass().getPackage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@
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;
import com.azure.spring.data.cosmos.repository.repository.AuditableIdGeneratedRepository;
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;
Expand All @@ -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();

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@
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;
import com.azure.spring.data.cosmos.repository.repository.ReactiveAuditableIdGeneratedRepository;
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;
Expand All @@ -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();

Expand Down Expand Up @@ -67,6 +77,7 @@ public void testInsertShouldSetAuditableEntries() {
"created-by",
now))
.verifyComplete();
LOGGER.info("the modified times of stubDateTimeProvider is {}", stubDateTimeProvider.getModifiedTimes());
}

@Test
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 8045613

Please sign in to comment.