9
9
10
10
package io .github .mfvanek .pg .spring ;
11
11
12
+ import io .github .mfvanek .pg .connection .PgConnection ;
13
+ import io .github .mfvanek .pg .connection .PgHostImpl ;
12
14
import org .junit .jupiter .api .Test ;
15
+ import org .mockito .Mockito ;
16
+ import org .springframework .boot .test .context .runner .ApplicationContextRunner ;
17
+ import org .springframework .context .ConfigurableApplicationContext ;
18
+
19
+ import java .sql .Connection ;
20
+ import java .sql .DatabaseMetaData ;
21
+ import java .sql .SQLException ;
22
+ import javax .annotation .Nonnull ;
13
23
14
24
import static org .assertj .core .api .Assertions .assertThat ;
25
+ import static org .assertj .core .api .Assertions .assertThatThrownBy ;
15
26
16
27
class DatabaseStructureHealthAutoConfigurationTest extends AutoConfigurationTestBase {
17
28
@@ -27,16 +38,45 @@ void withoutDataSource() {
27
38
@ Test
28
39
void withDataSource () {
29
40
assertWithTestConfig ()
41
+ .withPropertyValues ("spring.datasource.url=jdbc:postgresql://localhost:5432" )
30
42
.withInitializer (AutoConfigurationTestBase ::initialize )
31
43
.run (context -> {
32
- assertThat (context .getBeanDefinitionNames ())
33
- .filteredOn (beanNamesFilter )
34
- .hasSameSizeAs (EXPECTED_BEANS )
35
- .containsAll (EXPECTED_BEANS );
44
+ assertThatBeansPresent (context );
36
45
assertThatBeansAreNotNullBean (context );
37
46
});
38
47
}
39
48
49
+ @ Test
50
+ void withDataSourceButWithoutConnectionString () throws SQLException {
51
+ try (Connection connectionMock = Mockito .mock (Connection .class )) {
52
+ setMocks (connectionMock );
53
+
54
+ assertWithTestConfig ()
55
+ .withInitializer (AutoConfigurationTestBase ::initialize )
56
+ .run (context -> {
57
+ assertThatBeansPresent (context );
58
+ assertThatBeansAreNotNullBean (context );
59
+ assertThatPgConnectionIsValid (context );
60
+ });
61
+ }
62
+ }
63
+
64
+ @ Test
65
+ void withDataSourceAndEmptyConnectionString () throws SQLException {
66
+ try (Connection connectionMock = Mockito .mock (Connection .class )) {
67
+ setMocks (connectionMock );
68
+
69
+ assertWithTestConfig ()
70
+ .withPropertyValues ("spring.datasource.url=" )
71
+ .withInitializer (AutoConfigurationTestBase ::initialize )
72
+ .run (context -> {
73
+ assertThatBeansPresent (context );
74
+ assertThatBeansAreNotNullBean (context );
75
+ assertThatPgConnectionIsValid (context );
76
+ });
77
+ }
78
+ }
79
+
40
80
@ Test
41
81
void shouldNotCreateAutoConfigurationWithDisabledProperty () {
42
82
assertWithTestConfig ()
@@ -51,15 +91,55 @@ void shouldNotCreateAutoConfigurationWithDisabledProperty() {
51
91
@ Test
52
92
void shouldCreateAutoConfigurationWhenPropertyExplicitlySet () {
53
93
assertWithTestConfig ()
54
- .withPropertyValues ("pg.index.health.test.enabled=true" )
94
+ .withPropertyValues ("pg.index.health.test.enabled=true" ,
95
+ "spring.datasource.url=jdbc:postgresql://localhost:5432" )
55
96
.withInitializer (AutoConfigurationTestBase ::initialize )
56
97
.run (context -> {
57
- assertThat (context .getBeanDefinitionNames ())
58
- .isNotEmpty ()
59
- .filteredOn (beanNamesFilter )
60
- .hasSameSizeAs (EXPECTED_BEANS )
61
- .containsAll (EXPECTED_BEANS );
98
+ assertThatBeansPresent (context );
62
99
assertThatBeansAreNotNullBean (context );
63
100
});
64
101
}
102
+
103
+ @ Test
104
+ void withDataSourceAndExceptionWhileObtainingUrlFromMetadata () throws SQLException {
105
+ try (Connection connectionMock = Mockito .mock (Connection .class )) {
106
+ Mockito .when (DATA_SOURCE_MOCK .getConnection ())
107
+ .thenReturn (connectionMock );
108
+ Mockito .when (connectionMock .getMetaData ())
109
+ .thenThrow (SQLException .class );
110
+
111
+ final ApplicationContextRunner contextRunner = assertWithTestConfig ()
112
+ .withInitializer (AutoConfigurationTestBase ::initialize );
113
+ assertThatThrownBy (() -> contextRunner .run (this ::assertThatPgConnectionIsValid ))
114
+ .isInstanceOf (IllegalStateException .class )
115
+ .hasMessage ("Unstarted application context org.springframework.boot.test.context.assertj.AssertableApplicationContext[" +
116
+ "startupFailure=org.springframework.beans.factory.BeanCreationException] failed to start" )
117
+ .hasStackTraceContaining ("Factory method 'pgConnection' threw exception; nested exception is io.github.mfvanek.pg.connection.PgSqlException" );
118
+ }
119
+ }
120
+
121
+ private void assertThatBeansPresent (@ Nonnull final ConfigurableApplicationContext context ) {
122
+ assertThat (context .getBeanDefinitionNames ())
123
+ .isNotEmpty ()
124
+ .filteredOn (beanNamesFilter )
125
+ .hasSameSizeAs (EXPECTED_BEANS )
126
+ .containsAll (EXPECTED_BEANS );
127
+ }
128
+
129
+ private void assertThatPgConnectionIsValid (@ Nonnull final ConfigurableApplicationContext context ) {
130
+ assertThat (context .getBean ("pgConnection" , PgConnection .class ))
131
+ .isNotNull ()
132
+ .satisfies (c -> assertThat (c .getHost ())
133
+ .isEqualTo (PgHostImpl .ofUrl ("jdbc:postgresql://192.168.1.1:6432" )));
134
+ }
135
+
136
+ private void setMocks (@ Nonnull final Connection connectionMock ) throws SQLException {
137
+ Mockito .when (DATA_SOURCE_MOCK .getConnection ())
138
+ .thenReturn (connectionMock );
139
+ final DatabaseMetaData metaDataMock = Mockito .mock (DatabaseMetaData .class );
140
+ Mockito .when (connectionMock .getMetaData ())
141
+ .thenReturn (metaDataMock );
142
+ Mockito .when (metaDataMock .getURL ())
143
+ .thenReturn ("jdbc:postgresql://192.168.1.1:6432" );
144
+ }
65
145
}
0 commit comments