|
| 1 | +/* Hibernate, Relational Persistence for Idiomatic Java |
| 2 | + * |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * Copyright: Red Hat Inc. and Hibernate Authors |
| 5 | + */ |
| 6 | +package org.hibernate.reactive; |
| 7 | + |
| 8 | +import java.util.Collection; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Objects; |
| 11 | + |
| 12 | +import org.hibernate.reactive.testing.DBSelectionExtension; |
| 13 | + |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 16 | + |
| 17 | +import io.vertx.junit5.Timeout; |
| 18 | +import io.vertx.junit5.VertxTestContext; |
| 19 | +import jakarta.persistence.Entity; |
| 20 | +import jakarta.persistence.Id; |
| 21 | +import jakarta.persistence.Table; |
| 22 | + |
| 23 | +import static java.util.concurrent.TimeUnit.MINUTES; |
| 24 | +import static org.assertj.core.api.Assertions.assertThat; |
| 25 | +import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.COCKROACHDB; |
| 26 | +import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.DB2; |
| 27 | +import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.MARIA; |
| 28 | +import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.MYSQL; |
| 29 | +import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.ORACLE; |
| 30 | +import static org.hibernate.reactive.testing.DBSelectionExtension.skipTestsFor; |
| 31 | + |
| 32 | +/** |
| 33 | + * Same as Hibernate ORM org.hibernate.orm.test.stateless.UpsertTest |
| 34 | + * <p> |
| 35 | + * These tests are in a separate class because we need to skip the execution on some databases, |
| 36 | + * but once this has been resolved, they could be in {@link ReactiveStatelessSessionTest}. |
| 37 | + * </p> |
| 38 | + */ |
| 39 | +@Timeout(value = 10, timeUnit = MINUTES) |
| 40 | +public class UpsertTest extends BaseReactiveTest { |
| 41 | + |
| 42 | + /** |
| 43 | + * Something is missing in HR to make it work for these databases. |
| 44 | + */ |
| 45 | + @RegisterExtension |
| 46 | + public DBSelectionExtension dbSelection = skipTestsFor( COCKROACHDB, DB2, MARIA, MYSQL, ORACLE ); |
| 47 | + |
| 48 | + @Override |
| 49 | + protected Collection<Class<?>> annotatedEntities() { |
| 50 | + return List.of( Record.class ); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testMutinyUpsert(VertxTestContext context) { |
| 55 | + test( context, getMutinySessionFactory().withStatelessTransaction( ss -> ss |
| 56 | + .upsert( new Record( 123L, "hello earth" ) ) |
| 57 | + .call( () -> ss.upsert( new Record( 456L, "hello mars" ) ) ) |
| 58 | + ) |
| 59 | + .call( v -> getMutinySessionFactory().withStatelessTransaction( ss -> ss |
| 60 | + .createQuery( "from Record order by id", Record.class ).getResultList() ) |
| 61 | + .invoke( results -> assertThat( results ).containsExactly( |
| 62 | + new Record( 123L, "hello earth" ), |
| 63 | + new Record( 456L, "hello mars" ) |
| 64 | + ) ) |
| 65 | + ) |
| 66 | + .call( () -> getMutinySessionFactory().withStatelessTransaction( ss -> ss |
| 67 | + .upsert( new Record( 123L, "goodbye earth" ) ) |
| 68 | + ) ) |
| 69 | + .call( v -> getMutinySessionFactory().withStatelessTransaction( ss -> ss |
| 70 | + .createQuery( "from Record order by id", Record.class ).getResultList() ) |
| 71 | + .invoke( results -> assertThat( results ).containsExactly( |
| 72 | + new Record( 123L, "goodbye earth" ), |
| 73 | + new Record( 456L, "hello mars" ) |
| 74 | + ) ) |
| 75 | + ) |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void testMutinyUpsertWithEntityName(VertxTestContext context) { |
| 81 | + test( context, getMutinySessionFactory().withStatelessTransaction( ss -> ss |
| 82 | + .upsert( Record.class.getName(), new Record( 123L, "hello earth" ) ) |
| 83 | + .call( () -> ss.upsert( Record.class.getName(), new Record( 456L, "hello mars" ) ) ) |
| 84 | + ) |
| 85 | + .call( v -> getMutinySessionFactory().withStatelessTransaction( ss -> ss |
| 86 | + .createQuery( "from Record order by id", Record.class ).getResultList() ) |
| 87 | + .invoke( results -> assertThat( results ).containsExactly( |
| 88 | + new Record( 123L, "hello earth" ), |
| 89 | + new Record( 456L, "hello mars" ) |
| 90 | + ) ) |
| 91 | + ) |
| 92 | + .call( () -> getMutinySessionFactory().withStatelessTransaction( ss -> ss |
| 93 | + .upsert( Record.class.getName(), new Record( 123L, "goodbye earth" ) ) |
| 94 | + ) ) |
| 95 | + .call( v -> getMutinySessionFactory().withStatelessTransaction( ss -> ss |
| 96 | + .createQuery( "from Record order by id", Record.class ).getResultList() ) |
| 97 | + .invoke( results -> assertThat( results ).containsExactly( |
| 98 | + new Record( 123L, "goodbye earth" ), |
| 99 | + new Record( 456L, "hello mars" ) |
| 100 | + ) ) |
| 101 | + ) |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void testStageUpsert(VertxTestContext context) { |
| 107 | + test( context, getSessionFactory().withStatelessTransaction( ss -> ss |
| 108 | + .upsert( new Record( 123L, "hello earth" ) ) |
| 109 | + .thenCompose( v -> ss.upsert( new Record( 456L, "hello mars" ) ) ) |
| 110 | + ) |
| 111 | + .thenCompose( v -> getSessionFactory().withStatelessTransaction( ss -> ss |
| 112 | + .createQuery( "from Record order by id", Record.class ).getResultList() ) |
| 113 | + .thenAccept( results -> assertThat( results ).containsExactly( |
| 114 | + new Record( 123L, "hello earth" ), |
| 115 | + new Record( 456L, "hello mars" ) |
| 116 | + ) ) |
| 117 | + ) |
| 118 | + .thenCompose( v -> getSessionFactory().withStatelessTransaction( ss -> ss |
| 119 | + .upsert( new Record( 123L, "goodbye earth" ) ) |
| 120 | + ) ) |
| 121 | + .thenCompose( v -> getSessionFactory().withStatelessTransaction( ss -> ss |
| 122 | + .createQuery( "from Record order by id", Record.class ).getResultList() ) |
| 123 | + .thenAccept( results -> assertThat( results ).containsExactly( |
| 124 | + new Record( 123L, "goodbye earth" ), |
| 125 | + new Record( 456L, "hello mars" ) |
| 126 | + ) ) |
| 127 | + ) |
| 128 | + ); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void testStageUpsertWithEntityName(VertxTestContext context) { |
| 133 | + test( context, getSessionFactory().withStatelessTransaction( ss -> ss |
| 134 | + .upsert( Record.class.getName(), new Record( 123L, "hello earth" ) ) |
| 135 | + .thenCompose( v -> ss.upsert( Record.class.getName(), new Record( 456L, "hello mars" ) ) ) |
| 136 | + ) |
| 137 | + .thenCompose( v -> getSessionFactory().withStatelessTransaction( ss -> ss |
| 138 | + .createQuery( "from Record order by id", Record.class ).getResultList() ) |
| 139 | + .thenAccept( results -> assertThat( results ).containsExactly( |
| 140 | + new Record( 123L, "hello earth" ), |
| 141 | + new Record( 456L, "hello mars" ) |
| 142 | + ) ) |
| 143 | + ) |
| 144 | + .thenCompose( v -> getSessionFactory().withStatelessTransaction( ss -> ss |
| 145 | + .upsert( Record.class.getName(), new Record( 123L, "goodbye earth" ) ) |
| 146 | + ) ) |
| 147 | + .thenCompose( v -> getSessionFactory().withStatelessTransaction( ss -> ss |
| 148 | + .createQuery( "from Record order by id", Record.class ).getResultList() ) |
| 149 | + .thenAccept( results -> assertThat( results ).containsExactly( |
| 150 | + new Record( 123L, "goodbye earth" ), |
| 151 | + new Record( 456L, "hello mars" ) |
| 152 | + ) ) |
| 153 | + ) |
| 154 | + ); |
| 155 | + } |
| 156 | + |
| 157 | + @Entity(name = "Record") |
| 158 | + @Table(name = "Record") |
| 159 | + public static class Record { |
| 160 | + @Id |
| 161 | + public Long id; |
| 162 | + public String message; |
| 163 | + |
| 164 | + Record(Long id, String message) { |
| 165 | + this.id = id; |
| 166 | + this.message = message; |
| 167 | + } |
| 168 | + |
| 169 | + Record() { |
| 170 | + } |
| 171 | + |
| 172 | + public Long getId() { |
| 173 | + return id; |
| 174 | + } |
| 175 | + |
| 176 | + public String getMessage() { |
| 177 | + return message; |
| 178 | + } |
| 179 | + |
| 180 | + public void setMessage(String msg) { |
| 181 | + message = msg; |
| 182 | + } |
| 183 | + |
| 184 | + // Equals and HashCode for simplifying the test assertions, |
| 185 | + // not to be taken as an example or for production. |
| 186 | + @Override |
| 187 | + public boolean equals(Object o) { |
| 188 | + if ( this == o ) { |
| 189 | + return true; |
| 190 | + } |
| 191 | + if ( o == null || getClass() != o.getClass() ) { |
| 192 | + return false; |
| 193 | + } |
| 194 | + Record record = (Record) o; |
| 195 | + return Objects.equals( id, record.id ) && Objects.equals( message, record.message ); |
| 196 | + } |
| 197 | + |
| 198 | + @Override |
| 199 | + public int hashCode() { |
| 200 | + return Objects.hash( id, message ); |
| 201 | + } |
| 202 | + } |
| 203 | +} |
0 commit comments