Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.springframework.data.couchbase.core.mapping.Document;
import org.springframework.data.couchbase.core.mapping.Expiration;

import javax.validation.constraints.Max;

/**
* Airport entity
*
Expand All @@ -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) {
Expand Down Expand Up @@ -87,4 +91,12 @@ public Airport clearVersion() {
public String getCreatedBy() {
return createdBy;
}

public long getSize(){
return size;
}

public void setSize(long size){
this.size = size;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
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;
Expand All @@ -40,6 +42,8 @@
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;
Expand All @@ -52,6 +56,7 @@
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;
Expand Down Expand Up @@ -82,6 +87,7 @@
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;
Expand Down Expand Up @@ -123,6 +129,7 @@ void shouldSaveAndFindAll() {
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);
Expand All @@ -133,6 +140,18 @@ void shouldSaveAndFindAll() {
}
}

@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
Expand Down Expand Up @@ -780,5 +799,14 @@ public DateTimeProvider testDateTimeProvider() {
return new AuditingDateTimeProvider();
}

@Bean
public LocalValidatorFactoryBean validator() {
return new LocalValidatorFactoryBean();
}

@Bean
public ValidatingCouchbaseEventListener validationEventListener() {
return new ValidatingCouchbaseEventListener(validator());
}
}
}