forked from hibernate/hibernate-reactive
-
Notifications
You must be signed in to change notification settings - Fork 2
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
1 changed file
with
104 additions
and
0 deletions.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
...-reactive-core/src/test/java/org/hibernate/reactive/types/JsonWithBigDecimalTypeTest.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,104 @@ | ||
/* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright: Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.reactive.types; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.concurrent.CompletionStage; | ||
|
||
import org.hibernate.reactive.BaseReactiveTest; | ||
import org.hibernate.reactive.util.impl.CompletionStages; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import io.vertx.core.json.JsonObject; | ||
import io.vertx.junit5.Timeout; | ||
import io.vertx.junit5.VertxTestContext; | ||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Table; | ||
import org.assertj.core.api.Assertions; | ||
|
||
import static java.util.concurrent.TimeUnit.MINUTES; | ||
|
||
/** | ||
* Test types that we expect to work only on selected DBs. | ||
*/ | ||
@Timeout(value = 10, timeUnit = MINUTES) | ||
|
||
public class JsonWithBigDecimalTypeTest extends BaseReactiveTest { | ||
|
||
private final static BigDecimal TAO = BigDecimal.valueOf( 6.2832 ); | ||
|
||
@Override | ||
protected Collection<Class<?>> annotatedEntities() { | ||
return List.of( Book.class ); | ||
} | ||
|
||
|
||
@Override | ||
protected CompletionStage<Void> cleanDb() { | ||
return CompletionStages.voidFuture(); | ||
} | ||
|
||
@Test | ||
public void testJsonType(VertxTestContext context) { | ||
JsonObject jsonPrice = new JsonObject().put( "amount", TAO ); | ||
final Book theBookOfM = new Book( 5, "The Book of M", jsonPrice ); | ||
test( context, getMutinySessionFactory() | ||
.withTransaction( session -> session.persist( theBookOfM ) ) | ||
.chain( () -> getMutinySessionFactory().withTransaction( s -> { | ||
return s.createNativeQuery( "select id,title,price from BookWithJson b where (b.price ->> 'amount')::decimal between ?1 and ?2" ) | ||
.setParameter( 1, BigDecimal.valueOf( 4.0 ) ) | ||
.setParameter( 2, BigDecimal.valueOf( 100.0 ) ) | ||
.getSingleResult(); | ||
} ) ) | ||
.invoke( result -> Assertions.assertThat( result ).isEqualTo( theBookOfM ) ) | ||
); | ||
} | ||
|
||
@Entity(name = "Book") | ||
@Table(name = "BookWithJson") | ||
public static class Book { | ||
|
||
@Id | ||
Integer id; | ||
|
||
String title; | ||
|
||
@Column(name = "price") | ||
JsonObject price; | ||
|
||
public Book() { | ||
} | ||
|
||
public Book(Integer id, String title, JsonObject price) { | ||
this.id = id; | ||
this.title = title; | ||
this.price = price; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if ( this == o ) { | ||
return true; | ||
} | ||
if ( o == null || getClass() != o.getClass() ) { | ||
return false; | ||
} | ||
Book book = (Book) o; | ||
return Objects.equals( title, book.title ); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode( title ); | ||
} | ||
} | ||
} |