From d309692588968db097e26713fed9a23f448d25d9 Mon Sep 17 00:00:00 2001 From: Michael Reiche <48999328+mikereiche@users.noreply.github.com> Date: Thu, 22 Feb 2024 19:49:27 -0500 Subject: [PATCH] Change AuditingEventListener Constructor Signature. (#1908) Closes #1884. --- .../mapping/event/AuditingEventListener.java | 21 ++++++++++++++----- ...mplateQueryCollectionIntegrationTests.java | 7 ++++--- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/springframework/data/couchbase/core/mapping/event/AuditingEventListener.java b/src/main/java/org/springframework/data/couchbase/core/mapping/event/AuditingEventListener.java index 6717faf87..75b9daef0 100644 --- a/src/main/java/org/springframework/data/couchbase/core/mapping/event/AuditingEventListener.java +++ b/src/main/java/org/springframework/data/couchbase/core/mapping/event/AuditingEventListener.java @@ -37,7 +37,7 @@ */ public class AuditingEventListener implements ApplicationListener> { - private final ObjectFactory auditingHandlerFactory; + private final ObjectFactory auditingHandlerFactory; public AuditingEventListener() { this.auditingHandlerFactory = null; @@ -51,8 +51,8 @@ public AuditingEventListener() { * * @param auditingHandlerFactory must not be {@literal null}. */ - public AuditingEventListener(ObjectFactory auditingHandlerFactory) { - Assert.notNull(auditingHandlerFactory, "IsNewAwareAuditingHandler must not be null!"); + public AuditingEventListener(ObjectFactory auditingHandlerFactory) { + Assert.notNull(auditingHandlerFactory, "auditingHandlerFactory must not be null!"); this.auditingHandlerFactory = auditingHandlerFactory; } @@ -63,8 +63,19 @@ public AuditingEventListener(ObjectFactory auditingHa @Override public void onApplicationEvent(CouchbaseMappingEvent event) { if (event instanceof BeforeConvertEvent) { - Optional.ofNullable(event.getSource())// - .ifPresent(it -> auditingHandlerFactory.getObject().markAudited(it)); + IsNewAwareAuditingHandler h = auditingHandlerFactory != null + && auditingHandlerFactory.getObject() instanceof IsNewAwareAuditingHandler + ? (IsNewAwareAuditingHandler) (auditingHandlerFactory.getObject()) + : null; + if (auditingHandlerFactory != null && h == null) { + if (LOG.isWarnEnabled()) { + LOG.warn("event:{} source:{} auditingHandler is not an IsNewAwareAuditingHandler: {}", + event.getClass().getSimpleName(), event.getSource(), auditingHandlerFactory.getObject()); + } + } + if (h != null) { + Optional.ofNullable(event.getSource()).ifPresent(it -> h.markAudited(it)); + } } if (event instanceof BeforeSaveEvent) {} if (event instanceof AfterSaveEvent) {} diff --git a/src/test/java/org/springframework/data/couchbase/core/query/ReactiveCouchbaseTemplateQueryCollectionIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/core/query/ReactiveCouchbaseTemplateQueryCollectionIntegrationTests.java index 00c973c55..61ee6e567 100644 --- a/src/test/java/org/springframework/data/couchbase/core/query/ReactiveCouchbaseTemplateQueryCollectionIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/core/query/ReactiveCouchbaseTemplateQueryCollectionIntegrationTests.java @@ -115,10 +115,12 @@ public void beforeEach() { // then do processing for this class couchbaseTemplate.removeByQuery(User.class).withConsistency(REQUEST_PLUS).inCollection(collectionName).all(); couchbaseTemplate.findByQuery(User.class).withConsistency(REQUEST_PLUS).inCollection(collectionName).all(); - couchbaseTemplate.removeByQuery(Airport.class).inScope(scopeName).inCollection(collectionName).all(); + couchbaseTemplate.removeByQuery(Airport.class).withConsistency(REQUEST_PLUS).inScope(scopeName) + .inCollection(collectionName).all(); couchbaseTemplate.findByQuery(Airport.class).withConsistency(REQUEST_PLUS).inScope(scopeName) .inCollection(collectionName).all(); - couchbaseTemplate.removeByQuery(Airport.class).inScope(otherScope).inCollection(otherCollection).all(); + couchbaseTemplate.removeByQuery(Airport.class).withConsistency(REQUEST_PLUS).inScope(otherScope) + .inCollection(otherCollection).all(); couchbaseTemplate.findByQuery(Airport.class).withConsistency(REQUEST_PLUS).inScope(otherScope) .inCollection(otherCollection).all(); @@ -528,7 +530,6 @@ public void upsertById() { // 10 @Test public void existsByIdOther() { // 1 - GetOptions options = GetOptions.getOptions().timeout(Duration.ofSeconds(10)); ExistsOptions existsOptions = ExistsOptions.existsOptions().timeout(Duration.ofSeconds(10)); Airport saved = template.insertById(Airport.class).inScope(otherScope).inCollection(otherCollection) .one(vie.withIcao("lowg")).block();