Skip to content

Commit 1e774d4

Browse files
authored
Test case for exceptions thrown from events - validation. (#1225)
Closes #1224.
1 parent a879d0b commit 1e774d4

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,20 @@
118118
<optional>true</optional>
119119
</dependency>
120120

121+
<dependency>
122+
<groupId>javax.el</groupId>
123+
<artifactId>javax.el-api</artifactId>
124+
<version>3.0.0</version>
125+
<scope>test</scope>
126+
</dependency>
127+
128+
<dependency>
129+
<groupId>org.glassfish</groupId>
130+
<artifactId>javax.el</artifactId>
131+
<version>3.0.0</version>
132+
<scope>test</scope>
133+
</dependency>
134+
121135
<!-- CDI -->
122136
<!-- Dependency order required to build against CDI 1.0 and test with CDI 2.0 -->
123137
<dependency>

src/test/java/org/springframework/data/couchbase/domain/Airport.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import org.springframework.data.couchbase.core.mapping.Document;
2525
import org.springframework.data.couchbase.core.mapping.Expiration;
2626

27+
import javax.validation.constraints.Max;
28+
2729
/**
2830
* Airport entity
2931
*
@@ -43,6 +45,8 @@ public class Airport extends ComparableEntity {
4345

4446
@CreatedBy private String createdBy;
4547
@Expiration private long expiration;
48+
@Max(2)
49+
long size;
4650

4751
@PersistenceConstructor
4852
public Airport(String id, String iata, String icao) {
@@ -87,4 +91,12 @@ public Airport clearVersion() {
8791
public String getCreatedBy() {
8892
return createdBy;
8993
}
94+
95+
public long getSize(){
96+
return size;
97+
}
98+
99+
public void setSize(long size){
100+
this.size = size;
101+
}
90102
}

src/test/java/org/springframework/data/couchbase/repository/CouchbaseRepositoryQueryIntegrationTests.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import static org.junit.jupiter.api.Assertions.assertThrows;
2727
import static org.junit.jupiter.api.Assertions.assertTrue;
2828

29+
import junit.framework.AssertionFailedError;
30+
2931
import java.lang.reflect.Method;
3032
import java.time.Duration;
3133
import java.util.ArrayList;
@@ -40,6 +42,8 @@
4042
import java.util.concurrent.Future;
4143
import java.util.stream.Collectors;
4244

45+
import javax.validation.ConstraintViolationException;
46+
4347
import org.junit.jupiter.api.Test;
4448
import org.springframework.beans.factory.annotation.Autowired;
4549
import org.springframework.context.annotation.Bean;
@@ -52,6 +56,7 @@
5256
import org.springframework.data.couchbase.core.CouchbaseQueryExecutionException;
5357
import org.springframework.data.couchbase.core.CouchbaseTemplate;
5458
import org.springframework.data.couchbase.core.RemoveResult;
59+
import org.springframework.data.couchbase.core.mapping.event.ValidatingCouchbaseEventListener;
5560
import org.springframework.data.couchbase.core.query.N1QLExpression;
5661
import org.springframework.data.couchbase.core.query.QueryCriteria;
5762
import org.springframework.data.couchbase.domain.Address;
@@ -82,6 +87,7 @@
8287
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
8388
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
8489
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
90+
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
8591

8692
import com.couchbase.client.core.error.AmbiguousTimeoutException;
8793
import com.couchbase.client.core.error.CouchbaseException;
@@ -123,6 +129,7 @@ void shouldSaveAndFindAll() {
123129
Airport vie = null;
124130
try {
125131
vie = new Airport("airports::vie", "vie", "low4");
132+
vie.setSize(2);
126133
airportRepository.save(vie);
127134
List<Airport> all = new ArrayList<>();
128135
airportRepository.findAll().forEach(all::add);
@@ -133,6 +140,18 @@ void shouldSaveAndFindAll() {
133140
}
134141
}
135142

143+
@Test
144+
void shouldNotSave() {
145+
Airport vie = new Airport("airports::vie", "vie", "low4");
146+
vie.setSize(3);
147+
try {
148+
assertThrows(ConstraintViolationException.class, () -> airportRepository.save(vie));
149+
} catch (AssertionFailedError e) {
150+
airportRepository.delete(vie);
151+
throw e;
152+
}
153+
}
154+
136155
@Autowired PersonRepository personRepository;
137156

138157
@Test
@@ -780,5 +799,14 @@ public DateTimeProvider testDateTimeProvider() {
780799
return new AuditingDateTimeProvider();
781800
}
782801

802+
@Bean
803+
public LocalValidatorFactoryBean validator() {
804+
return new LocalValidatorFactoryBean();
805+
}
806+
807+
@Bean
808+
public ValidatingCouchbaseEventListener validationEventListener() {
809+
return new ValidatingCouchbaseEventListener(validator());
810+
}
783811
}
784812
}

0 commit comments

Comments
 (0)