|
36 | 36 | import org.junit.Rule;
|
37 | 37 | import org.junit.runner.RunWith;
|
38 | 38 |
|
| 39 | +import java.util.Arrays; |
39 | 40 | import java.util.concurrent.CompletionStage;
|
40 | 41 | import java.util.concurrent.TimeUnit;
|
| 42 | +import java.util.stream.Collectors; |
41 | 43 |
|
42 | 44 | import static org.hibernate.reactive.containers.DatabaseConfiguration.dbType;
|
| 45 | +import static org.hibernate.reactive.util.impl.CompletionStages.loop; |
43 | 46 |
|
44 | 47 | /**
|
45 | 48 | * Base class for unit tests that need a connection to the selected db and
|
@@ -127,6 +130,26 @@ protected Configuration constructConfiguration() {
|
127 | 130 | return configuration;
|
128 | 131 | }
|
129 | 132 |
|
| 133 | + public CompletionStage<Void> deleteEntities(Class<?>... entities) { |
| 134 | + return deleteEntities( Arrays.stream( entities ) |
| 135 | + .map( BaseReactiveTest::defaultEntityName ) |
| 136 | + .collect( Collectors.toList() ) |
| 137 | + .toArray( new String[entities.length] ) ); |
| 138 | + } |
| 139 | + |
| 140 | + private static String defaultEntityName(Class<?> aClass) { |
| 141 | + int index = aClass.getName().lastIndexOf( '.' ); |
| 142 | + index = index > -1 ? index + 1 : 0; |
| 143 | + return aClass.getName().substring( index ); |
| 144 | + } |
| 145 | + |
| 146 | + public CompletionStage<Void> deleteEntities(String... entities) { |
| 147 | + return getSessionFactory() |
| 148 | + .withTransaction( (s, tx) -> loop( entities, name -> s |
| 149 | + .createQuery( "from " + name ).getResultList() |
| 150 | + .thenCompose( list -> s.remove( list.toArray( new Object[list.size()] ) ) ) ) ); |
| 151 | + } |
| 152 | + |
130 | 153 | @Before
|
131 | 154 | public void before(TestContext context) {
|
132 | 155 | Configuration configuration = constructConfiguration();
|
|
0 commit comments