forked from hibernate/hibernate-reactive
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[hibernate#1905] Add test for Reactive find with lock in Quarkus with…
… reactive hibernate
- Loading branch information
Showing
1 changed file
with
133 additions
and
0 deletions.
There are no files selected for viewing
133 changes: 133 additions & 0 deletions
133
hibernate-reactive-core/src/test/java/org/hibernate/reactive/FindByIdWithLockTest.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,133 @@ | ||
/* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright: Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.reactive; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import io.vertx.junit5.Timeout; | ||
import io.vertx.junit5.VertxTestContext; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.FetchType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.LockModeType; | ||
import jakarta.persistence.ManyToOne; | ||
import jakarta.persistence.OneToMany; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@Timeout(value = 10, timeUnit = TimeUnit.MINUTES) | ||
public class FindByIdWithLockTest extends BaseReactiveTest { | ||
private static final Long CHILD_ID = 1L; | ||
|
||
@Override | ||
protected Collection<Class<?>> annotatedEntities() { | ||
return List.of( Parent.class, Child.class ); | ||
} | ||
|
||
@Test | ||
public void testFindChild(VertxTestContext context) { | ||
Parent parent = new Parent( 1L, "Lio" ); | ||
Child child = new Child( CHILD_ID, "And" ); | ||
test( | ||
context, getMutinySessionFactory() | ||
.withTransaction( session -> session.persistAll( parent, child ) ) | ||
.chain( () -> getMutinySessionFactory() | ||
.withTransaction( session -> session | ||
.find( Child.class, CHILD_ID, LockModeType.PESSIMISTIC_WRITE ) | ||
.invoke( c -> { | ||
assertThat( c ).isNotNull(); | ||
assertThat( c.getId() ).isEqualTo( CHILD_ID ); | ||
} | ||
) ) ) | ||
); | ||
} | ||
|
||
@Entity(name = "Parent") | ||
public static class Parent { | ||
|
||
@Id | ||
private Long id; | ||
|
||
private String name; | ||
|
||
@OneToMany(fetch = FetchType.EAGER) | ||
public List<Child> children; | ||
|
||
public Parent() { | ||
} | ||
|
||
public Parent(Long id, String name) { | ||
this.id = id; | ||
this.name = name; | ||
} | ||
|
||
public void add(Child child) { | ||
if ( children == null ) { | ||
children = new ArrayList<>(); | ||
} | ||
children.add( child ); | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public List<Child> getChildren() { | ||
return children; | ||
} | ||
} | ||
|
||
|
||
@Entity(name = "Child") | ||
public static class Child { | ||
|
||
@Id | ||
private Long id; | ||
|
||
public String name; | ||
|
||
@ManyToOne | ||
public Parent parent; | ||
|
||
public Child() { | ||
} | ||
|
||
public Child(Long id, String name) { | ||
this.id = id; | ||
this.name = name; | ||
} | ||
|
||
public Child(Long id, String name, Parent parent) { | ||
this.id = id; | ||
this.name = name; | ||
this.parent = parent; | ||
parent.add( this ); | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public Parent getParent() { | ||
return parent; | ||
} | ||
} | ||
|
||
|
||
} |