|
5 | 5 | */
|
6 | 6 | package org.hibernate.reactive.configuration;
|
7 | 7 |
|
| 8 | +import java.net.URI; |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.Map; |
| 11 | + |
8 | 12 | import org.hibernate.HibernateError;
|
9 | 13 | import org.hibernate.reactive.pool.impl.DefaultSqlClientPool;
|
10 | 14 | import org.hibernate.reactive.pool.impl.DefaultSqlClientPoolConfiguration;
|
11 | 15 |
|
12 | 16 | import org.junit.Test;
|
13 | 17 |
|
14 |
| -import java.net.URI; |
15 |
| - |
16 | 18 | import io.vertx.sqlclient.SqlConnectOptions;
|
17 | 19 |
|
18 | 20 | import static org.assertj.core.api.Assertions.assertThat;
|
| 21 | +import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.SQLSERVER; |
| 22 | +import static org.hibernate.reactive.containers.DatabaseConfiguration.createJdbcUrl; |
| 23 | +import static org.hibernate.reactive.containers.DatabaseConfiguration.dbType; |
19 | 24 | import static org.junit.Assert.assertThrows;
|
20 | 25 |
|
| 26 | +/** |
| 27 | + * Test the correct creation of the {@link SqlConnectOptions} |
| 28 | + * given a JDBC connection URL. |
| 29 | + * <p> |
| 30 | + * This test doesn't require docker. |
| 31 | + */ |
21 | 32 | public class JdbcUrlParserTest {
|
22 | 33 |
|
23 | 34 | @Test
|
24 | 35 | public void returnsNullForNull() {
|
25 |
| - assertThrows( HibernateError.class, () -> { |
| 36 | + final HibernateError error = assertThrows( HibernateError.class, () -> { |
26 | 37 | URI uri = DefaultSqlClientPool.parse( null );
|
27 | 38 | assertThat( uri ).isNull();
|
28 | 39 | } );
|
| 40 | + assertThat( error.getMessage() ).contains( "was not provided" ); |
29 | 41 | }
|
30 | 42 |
|
31 | 43 | @Test
|
32 |
| - public void invalidParameter() { |
33 |
| - assertThrows( HibernateError.class, () -> { |
34 |
| - URI uri = DefaultSqlClientPool.parse( "jdbc:postgresql://localhost:5432/hreact" ); |
35 |
| - DefaultSqlClientPoolConfiguration cfg = new DefaultSqlClientPoolConfiguration(); |
36 |
| - final SqlConnectOptions connectOptions = cfg.connectOptions( uri ); |
| 44 | + public void missingUser() { |
| 45 | + final HibernateError error = assertThrows( HibernateError.class, () -> { |
| 46 | + URI uri = DefaultSqlClientPool.parse( defaultUrl() ); |
| 47 | + new DefaultSqlClientPoolConfiguration().connectOptions( uri ); |
37 | 48 | } );
|
| 49 | + assertThat( error.getMessage() ).contains( "database username not specified" ); |
38 | 50 | }
|
39 | 51 |
|
40 | 52 | @Test
|
41 |
| - public void parameters() { |
42 |
| - URI uri = DefaultSqlClientPool.parse( "jdbc:postgresql://localhost:5432/hreact?user=hello"); |
43 |
| - DefaultSqlClientPoolConfiguration cfg = new DefaultSqlClientPoolConfiguration(); |
44 |
| - final SqlConnectOptions connectOptions = cfg.connectOptions( uri ); |
45 |
| - assertThat(connectOptions).isNotNull(); |
| 53 | + public void testOptionsWithExtraProperties() { |
| 54 | + Map<String, String> params = new HashMap<>(); |
| 55 | + params.put( "user", "hello" ); |
| 56 | + params.put( "param1", "value1" ); |
| 57 | + params.put( "param2", "value2" ); |
| 58 | + params.put( "param3", "value3" ); |
| 59 | + |
| 60 | + assertOptions( createUrl( params ), defaultDb(), params ); |
46 | 61 | }
|
47 | 62 |
|
48 | 63 | @Test
|
49 |
| - public void uriCreation() { |
50 |
| - URI uri = DefaultSqlClientPool.parse( "jdbc:postgresql://localhost:5432/hreact"); |
51 |
| - assertThat(uri).isNotNull(); |
| 64 | + public void testOptionsWithoutExtraProperties() { |
| 65 | + // Without a user we would have an exception |
| 66 | + Map<String, String> params = new HashMap<>(); |
| 67 | + params.put( "user", "PerryThePlatypus" ); |
| 68 | + |
| 69 | + assertOptions( createUrl( params ), defaultDb(), params ); |
52 | 70 | }
|
53 | 71 |
|
54 | 72 | @Test
|
55 |
| - public void parsePort() { |
56 |
| - URI uri = DefaultSqlClientPool.parse( "jdbc:postgresql://localhost:5432/hreact"); |
57 |
| - assertThat(uri).hasPort(5432); |
| 73 | + public void testOptionsWithPasswordAndProperties() { |
| 74 | + Map<String, String> params = new HashMap<>(); |
| 75 | + params.put( "password", "helloPwd" ); |
| 76 | + params.put( "user", "username" ); |
| 77 | + params.put( "param2", "Value2" ); |
| 78 | + |
| 79 | + assertOptions( createUrl( params ), defaultDb(), params ); |
58 | 80 | }
|
59 | 81 |
|
60 | 82 | @Test
|
61 |
| - public void parseHost() { |
62 |
| - URI uri = DefaultSqlClientPool.parse( "jdbc:postgresql://localhost:5432/hreact"); |
63 |
| - assertThat(uri).hasHost("localhost"); |
| 83 | + public void testDatabaseAsProperty() { |
| 84 | + Map<String, String> params = new HashMap<>(); |
| 85 | + params.put( "database", "helloDatabase" ); |
| 86 | + params.put( "user", "PerryThePlatypus" ); |
| 87 | + params.put( "password", "XxXxX" ); |
| 88 | + params.put( "param2", "Value2" ); |
| 89 | + |
| 90 | + assertOptions( createUrl( params ), "helloDatabase", params ); |
64 | 91 | }
|
65 | 92 |
|
66 |
| - @Test |
67 |
| - public void parseScheme() { |
68 |
| - URI uri = DefaultSqlClientPool.parse( "jdbc:postgresql://localhost:5432/hreact"); |
69 |
| - assertThat(uri).hasScheme("postgresql"); |
| 93 | + /** |
| 94 | + * Create the default {@link SqlConnectOptions} with the given extra properties |
| 95 | + * and assert that's correct. |
| 96 | + * |
| 97 | + * @return the created options in case a test needs custom extra assertions |
| 98 | + */ |
| 99 | + private SqlConnectOptions assertOptions(String url, String expectedDbName, Map<String, String> parameters) { |
| 100 | + URI uri = DefaultSqlClientPool.parse( url ); |
| 101 | + SqlConnectOptions options = new DefaultSqlClientPoolConfiguration().connectOptions( uri ); |
| 102 | + |
| 103 | + // These keys won't be mapped as properties |
| 104 | + String username = parameters.remove( "user" ); |
| 105 | + String password = parameters.remove( "password" ); |
| 106 | + parameters.remove( "database" ); |
| 107 | + |
| 108 | + assertThat( options ).as( "URL: " + url ).isNotNull(); |
| 109 | + assertThat( options.getUser() ).as( "URL: " + url ).isEqualTo( username ); |
| 110 | + assertThat( options.getPassword() ).as( "URL: " + url ).isEqualTo( password ); |
| 111 | + assertThat( options.getDatabase() ).as( "URL: " + url ).isEqualTo( expectedDbName ); |
| 112 | + assertThat( options.getHost() ).as( "URL: " + url ).isEqualTo( "localhost" ); |
| 113 | + assertThat( options.getPort() ).as( "URL: " + url ).isEqualTo( dbType().getDefaultPort() ); |
| 114 | + |
| 115 | + // Check extra properties |
| 116 | + assertThat( options.getProperties() ).as( "URL: " + url ).containsExactlyInAnyOrderEntriesOf( parameters ); |
| 117 | + return options; |
| 118 | + } |
| 119 | + |
| 120 | + private String defaultUrl() { |
| 121 | + return createUrl( new HashMap<>() ); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Create the JDBC Url with the additional extra properties (if any) |
| 126 | + */ |
| 127 | + private String createUrl(Map<String, String> properties) { |
| 128 | + return createJdbcUrl( "localhost", dbType().getDefaultPort(), defaultDb(), properties ); |
| 129 | + } |
| 130 | + |
| 131 | + private String defaultDb() { |
| 132 | + return dbType() == SQLSERVER ? "" : "hreactDB"; |
70 | 133 | }
|
71 | 134 | }
|
0 commit comments