From 1e774d460b5c9d24fa5ce668a55d3a8f395b9eb7 Mon Sep 17 00:00:00 2001 From: Michael Reiche <48999328+mikereiche@users.noreply.github.com> Date: Tue, 14 Sep 2021 10:09:51 -0700 Subject: [PATCH] Test case for exceptions thrown from events - validation. (#1225) Closes #1224. --- pom.xml | 14 ++++++++++ .../data/couchbase/domain/Airport.java | 12 ++++++++ ...chbaseRepositoryQueryIntegrationTests.java | 28 +++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/pom.xml b/pom.xml index 29b59cbb..6b51b5d2 100644 --- a/pom.xml +++ b/pom.xml @@ -118,6 +118,20 @@ true + + javax.el + javax.el-api + 3.0.0 + test + + + + org.glassfish + javax.el + 3.0.0 + test + + diff --git a/src/test/java/org/springframework/data/couchbase/domain/Airport.java b/src/test/java/org/springframework/data/couchbase/domain/Airport.java index 78e12dfa..04143626 100644 --- a/src/test/java/org/springframework/data/couchbase/domain/Airport.java +++ b/src/test/java/org/springframework/data/couchbase/domain/Airport.java @@ -24,6 +24,8 @@ import org.springframework.data.annotation.Version; import org.springframework.data.couchbase.core.mapping.Document; import org.springframework.data.couchbase.core.mapping.Expiration; +import javax.validation.constraints.Max; + /** * Airport entity * @@ -43,6 +45,8 @@ public class Airport extends ComparableEntity { @CreatedBy private String createdBy; @Expiration private long expiration; + @Max(2) + long size; @PersistenceConstructor public Airport(String id, String iata, String icao) { @@ -87,4 +91,12 @@ public class Airport extends ComparableEntity { public String getCreatedBy() { return createdBy; } + + public long getSize(){ + return size; + } + + public void setSize(long size){ + this.size = size; + } } diff --git a/src/test/java/org/springframework/data/couchbase/repository/CouchbaseRepositoryQueryIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/repository/CouchbaseRepositoryQueryIntegrationTests.java index 197c0b60..58b7169e 100644 --- a/src/test/java/org/springframework/data/couchbase/repository/CouchbaseRepositoryQueryIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/repository/CouchbaseRepositoryQueryIntegrationTests.java @@ -26,6 +26,8 @@ import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import junit.framework.AssertionFailedError; + import java.lang.reflect.Method; import java.time.Duration; import java.util.ArrayList; @@ -40,6 +42,8 @@ import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.stream.Collectors; +import javax.validation.ConstraintViolationException; + import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; @@ -52,6 +56,7 @@ import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration; import org.springframework.data.couchbase.core.CouchbaseQueryExecutionException; import org.springframework.data.couchbase.core.CouchbaseTemplate; import org.springframework.data.couchbase.core.RemoveResult; +import org.springframework.data.couchbase.core.mapping.event.ValidatingCouchbaseEventListener; import org.springframework.data.couchbase.core.query.N1QLExpression; import org.springframework.data.couchbase.core.query.QueryCriteria; import org.springframework.data.couchbase.domain.Address; @@ -82,6 +87,7 @@ import org.springframework.data.domain.Sort; import org.springframework.data.projection.SpelAwareProxyProjectionFactory; import org.springframework.data.repository.core.support.DefaultRepositoryMetadata; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; +import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; import com.couchbase.client.core.error.AmbiguousTimeoutException; import com.couchbase.client.core.error.CouchbaseException; @@ -123,6 +129,7 @@ public class CouchbaseRepositoryQueryIntegrationTests extends ClusterAwareIntegr Airport vie = null; try { vie = new Airport("airports::vie", "vie", "low4"); + vie.setSize(2); airportRepository.save(vie); List all = new ArrayList<>(); airportRepository.findAll().forEach(all::add); @@ -133,6 +140,18 @@ public class CouchbaseRepositoryQueryIntegrationTests extends ClusterAwareIntegr } } + @Test + void shouldNotSave() { + Airport vie = new Airport("airports::vie", "vie", "low4"); + vie.setSize(3); + try { + assertThrows(ConstraintViolationException.class, () -> airportRepository.save(vie)); + } catch (AssertionFailedError e) { + airportRepository.delete(vie); + throw e; + } + } + @Autowired PersonRepository personRepository; @Test @@ -780,5 +799,14 @@ public class CouchbaseRepositoryQueryIntegrationTests extends ClusterAwareIntegr return new AuditingDateTimeProvider(); } + @Bean + public LocalValidatorFactoryBean validator() { + return new LocalValidatorFactoryBean(); + } + + @Bean + public ValidatingCouchbaseEventListener validationEventListener() { + return new ValidatingCouchbaseEventListener(validator()); + } } }