generated from micronaut-projects/micronaut-project-template
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
195 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...dation/src/test/groovy/io/micronaut/validation/validator/reactive/BookServiceRxJava2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package io.micronaut.validation.validator.reactive; | ||
|
||
import io.micronaut.context.annotation.Executable; | ||
import io.reactivex.Completable; | ||
import io.reactivex.Flowable; | ||
import io.reactivex.Maybe; | ||
import io.reactivex.Observable; | ||
import io.reactivex.Single; | ||
import jakarta.inject.Singleton; | ||
import org.reactivestreams.Publisher; | ||
|
||
import javax.validation.Valid; | ||
import javax.validation.constraints.NotBlank; | ||
import java.util.List; | ||
|
||
@Singleton | ||
class BookServiceRxJava2 { | ||
|
||
@Executable | ||
Publisher<@Valid Book> rxSimple(Publisher<@NotBlank String> title) { | ||
return Single.fromPublisher(title).map(Book::new).toFlowable(); | ||
} | ||
|
||
@Executable | ||
Observable<@Valid Book> rxValid(Observable<@Valid Book> book) { | ||
return book; | ||
} | ||
|
||
@Executable | ||
Completable rxValidWithTypeParameter(Single<List<@Valid Book>> books) { | ||
return books.ignoreElement(); | ||
} | ||
|
||
@Executable | ||
Maybe<@Valid Book> rxValidMaybe(Maybe<@Valid Book> book) { return book; } | ||
|
||
@Executable | ||
Publisher<@Valid Book> rxReturnInvalid(Publisher<@Valid Book> book) { | ||
return Flowable.fromPublisher(book).map(b -> new Book("")); | ||
} | ||
|
||
@Executable | ||
Maybe<Book> rxReturnInvalidWithoutValidation(Flowable<@Valid Book> books) { | ||
return books.firstElement().map(v -> new Book("")); | ||
} | ||
|
||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
...test/groovy/io/micronaut/validation/validator/reactive/RxJava2MethodValidationSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package io.micronaut.validation.validator.reactive | ||
|
||
import io.micronaut.context.ApplicationContext | ||
import io.micronaut.validation.validator.Validator | ||
import io.reactivex.Flowable | ||
import io.reactivex.Maybe | ||
import io.reactivex.Observable | ||
import io.reactivex.Single | ||
import org.reactivestreams.Publisher | ||
import reactor.core.publisher.Flux | ||
import reactor.core.publisher.Mono | ||
import spock.lang.AutoCleanup | ||
import spock.lang.Shared | ||
import spock.lang.Specification | ||
|
||
import javax.validation.ConstraintViolationException | ||
import java.util.concurrent.CompletableFuture | ||
import java.util.concurrent.ExecutionException | ||
import java.util.regex.Pattern | ||
|
||
class RxJava2MethodValidationSpec extends Specification { | ||
|
||
@Shared | ||
@AutoCleanup | ||
ApplicationContext applicationContext = ApplicationContext.run() | ||
|
||
void "test reactive return type validation"() { | ||
given: | ||
BookServiceRxJava2 bookService = applicationContext.getBean(BookServiceRxJava2) | ||
|
||
when: | ||
Single<Book> single = Single.just(new Book("It")) | ||
Single.fromPublisher(bookService.rxReturnInvalid(single.toFlowable())).blockingGet() | ||
|
||
then: | ||
ConstraintViolationException e = thrown() | ||
e.message == 'publisher[]<T Book>.title: must not be blank' | ||
e.getConstraintViolations().first().propertyPath.toString() == 'publisher[]<T Book>.title' | ||
} | ||
|
||
void "test reactive return type no validation"() { | ||
given: | ||
BookServiceRxJava2 bookService = applicationContext.getBean(BookServiceRxJava2) | ||
|
||
when: | ||
Single<Book> single = Single.just(new Book("It")) | ||
bookService.rxReturnInvalidWithoutValidation(single.toFlowable()).blockingGet() | ||
|
||
then: | ||
noExceptionThrown() | ||
} | ||
|
||
void "test reactive validation with invalid simple argument"() { | ||
given: | ||
BookServiceRxJava2 bookService = applicationContext.getBean(BookServiceRxJava2) | ||
|
||
when: | ||
var validator = applicationContext.getBean(Validator) | ||
var violations = validator.forExecutables().validateParameters( | ||
bookService, | ||
BookService.class.getDeclaredMethod("rxSimple", Publisher<String>), | ||
[Flowable.just("")] as Object[] | ||
) | ||
|
||
then: "No errors because publisher is not executed" | ||
violations.size() == 0 | ||
|
||
when: | ||
Single.fromPublisher(bookService.rxSimple(Single.just("").toFlowable())).blockingGet() | ||
|
||
then: | ||
def e = thrown(ConstraintViolationException) | ||
Pattern.matches('rxSimple.title\\[]<T [^>]*String>: must not be blank', e.message) | ||
def path = e.getConstraintViolations().first().propertyPath.iterator() | ||
path.next().getName() == 'rxSimple' | ||
path.next().getName() == 'title' | ||
path.next().isInIterable() | ||
|
||
} | ||
|
||
void "test reactive validation with valid argument"() { | ||
given: | ||
BookServiceRxJava2 bookService = applicationContext.getBean(BookServiceRxJava2) | ||
|
||
when: | ||
def input = Observable.just(new Book("It")) | ||
def book = bookService.rxValid(input).blockingFirst() | ||
|
||
then: | ||
book.title == 'It' | ||
} | ||
|
||
void "test reactive maybe validation with valid argument"() { | ||
given: | ||
BookServiceRxJava2 bookService = applicationContext.getBean(BookServiceRxJava2) | ||
|
||
when: | ||
def input = Maybe.just(new Book("It")) | ||
def book = bookService.rxValidMaybe(input).blockingGet() | ||
|
||
then: | ||
book.title == 'It' | ||
} | ||
|
||
void "test reactive validation with invalid argument"() { | ||
given: | ||
BookServiceRxJava2 bookService = applicationContext.getBean(BookServiceRxJava2) | ||
|
||
when: | ||
def input = Observable.just(new Book("")) | ||
bookService.rxValid(input).blockingFirst() | ||
|
||
then: | ||
def e = thrown(ConstraintViolationException) | ||
Pattern.matches('rxValid.book\\[]<T .*Book>.title: must not be blank', e.message) | ||
e.getConstraintViolations().first().propertyPath.toString().startsWith('rxValid.book') | ||
} | ||
|
||
void "test reactive validation with invalid argument type parameter"() { | ||
given: | ||
BookServiceRxJava2 bookService = applicationContext.getBean(BookServiceRxJava2) | ||
|
||
when: | ||
def input = Single.just([new Book("It"), new Book("")]) | ||
bookService.rxValidWithTypeParameter(input).blockingAwait() | ||
|
||
then: | ||
def e = thrown(ConstraintViolationException) | ||
Pattern.matches('rxValidWithTypeParameter.books\\[]<T List>\\[1]<E Book>.title: must not be blank', e.message) | ||
e.getConstraintViolations().first().propertyPath.toString().startsWith('rxValidWithTypeParameter.books') | ||
} | ||
|
||
} |