Skip to content

Commit

Permalink
[hibernate#1627] @SQLSelect test
Browse files Browse the repository at this point in the history
  • Loading branch information
blafond committed Jul 12, 2023
1 parent 29c518e commit e88effd
Showing 1 changed file with 118 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/* 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.Arrays;
import java.util.Collection;
import java.util.List;


import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.SQLDeleteAll;
import org.hibernate.annotations.SQLInsert;
import org.hibernate.annotations.SQLSelect;
import org.hibernate.annotations.SQLUpdate;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import io.vertx.junit5.VertxTestContext;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class SQLSelectTest extends BaseReactiveTest {

private Person thePerson;

@Override
protected Collection<Class<?>> annotatedEntities() {
return List.of( Person.class );
}

@BeforeEach
public void populateDb(VertxTestContext context) {
List<String> phones = Arrays.asList( "999-999-9999", "111-111-1111", "123-456-7890" );
thePerson = new Person();
thePerson.id = 724200;
thePerson.name = "Claude";
thePerson.phones = phones;

test( context, getMutinySessionFactory().withTransaction( (s, t) -> s.persist( thePerson ) ));
}

@Test
public void findEntity(VertxTestContext context) {
test( context, openSession()
.thenCompose( session -> session.find( Person.class, thePerson.getId() ) )
.thenAccept( found -> assertPhones( found, "999-999-9999", "111-111-1111", "123-456-7890" ) )
);
}

@Test
public void findEntityWithNativeQuery(VertxTestContext context) {
test( context, openSession()
.thenCompose( s -> s.createNativeQuery(
"SELECT id, name FROM person WHERE id = $1",
Person.class
)
.setParameter( 1, thePerson.getId() )
.getSingleResult() )
.thenAccept( found -> assertPhones( found, "999-999-9999", "111-111-1111", "123-456-7890" ) )
);
}

private static void assertPhones(Person person, String... expectedPhones) {
assertNotNull( person );
assertThat( person.getPhones() ).containsExactlyInAnyOrder( expectedPhones );
}


@Entity(name = "Person")
@SQLInsert(sql = "INSERT INTO person (name, id) VALUES ($1, $2) ")
@SQLUpdate(sql = "UPDATE person SET name = $1 where id = $2 ")
@SQLDelete(sql = "UPDATE person SET WHERE id = $1 ")
@SQLSelect(sql = "SELECT id, name FROM person WHERE id = $1")
static class Person {

@Id
private int id;

private String name;

@ElementCollection(fetch = FetchType.EAGER)

@SQLInsert(sql = "INSERT INTO person_phones (person_id, phones) VALUES ($1, $2) ")
@SQLDeleteAll(sql = "UPDATE person_phones SET WHERE person_id = $1")
@SQLSelect(sql = "SELECT phones FROM Person_phones WHERE person_id = $1")
private List<String> phones = new ArrayList<>();

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public List<String> getPhones() {
return phones;
}
}
}

0 comments on commit e88effd

Please sign in to comment.