Test case for exceptions thrown from events - validation. (#1225)

Closes #1224.
This commit is contained in:
Michael Reiche
2021-09-14 10:09:51 -07:00
committed by GitHub
parent a879d0b4e3
commit 1e774d460b
3 changed files with 54 additions and 0 deletions

14
pom.xml
View File

@@ -118,6 +118,20 @@
<optional>true</optional>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>3.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.el</artifactId>
<version>3.0.0</version>
<scope>test</scope>
</dependency>
<!-- CDI -->
<!-- Dependency order required to build against CDI 1.0 and test with CDI 2.0 -->
<dependency>

View File

@@ -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;
}
}

View File

@@ -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<Airport> 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());
}
}
}