/
DatabaseInfoRetrieverTest.java
172 lines (147 loc) · 7.28 KB
/
DatabaseInfoRetrieverTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
========================================================================
SchemaCrawler
http://www.schemacrawler.com
Copyright (c) 2000-2024, Sualeh Fatehi <sualeh@hotmail.com>.
All rights reserved.
------------------------------------------------------------------------
SchemaCrawler is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
SchemaCrawler and the accompanying materials are made available under
the terms of the Eclipse Public License v1.0, GNU General Public License
v3 or GNU Lesser General Public License v3.
You may elect to redistribute this code under any of these licenses.
The Eclipse Public License is available at:
http://www.eclipse.org/legal/epl-v10.html
The GNU General Public License v3 and the GNU Lesser General Public
License v3 are available at:
http://www.gnu.org/licenses/
========================================================================
*/
package schemacrawler.crawl;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import schemacrawler.schema.DatabaseUser;
import schemacrawler.schema.Property;
import schemacrawler.schemacrawler.InformationSchemaKey;
import schemacrawler.schemacrawler.InformationSchemaViews;
import schemacrawler.schemacrawler.InformationSchemaViewsBuilder;
import schemacrawler.schemacrawler.SchemaCrawlerOptions;
import schemacrawler.schemacrawler.SchemaCrawlerOptionsBuilder;
import schemacrawler.schemacrawler.SchemaRetrievalOptions;
import schemacrawler.schemacrawler.SchemaRetrievalOptionsBuilder;
import schemacrawler.test.utility.ResolveTestContext;
import schemacrawler.test.utility.TestContext;
import schemacrawler.test.utility.WithTestDatabase;
import us.fatehi.utility.datasource.DatabaseConnectionSource;
@WithTestDatabase
@ResolveTestContext
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class DatabaseInfoRetrieverTest {
private MutableCatalog catalog;
@Test
@DisplayName("Database info")
public void databaseInfo(final TestContext testContext, final Connection connection)
throws Exception {
assertThat(
catalog.getDatabaseInfo().toString(),
is("-- database: HSQL Database Engine 2.7.2" + System.lineSeparator()));
}
@Test
@DisplayName("Retrieve database users")
public void databaseUsers(
final TestContext testContext, final DatabaseConnectionSource dataSource) throws Exception {
assertThat(catalog.getDatabaseUsers(), is(empty()));
final InformationSchemaViews informationSchemaViews =
InformationSchemaViewsBuilder.builder()
.withSql(
InformationSchemaKey.DATABASE_USERS,
"SELECT USER_NAME AS USERNAME, "
+ "ADMIN, INITIAL_SCHEMA, AUTHENTICATION, PASSWORD_DIGEST "
+ "FROM INFORMATION_SCHEMA.SYSTEM_USERS")
.toOptions();
final SchemaRetrievalOptionsBuilder schemaRetrievalOptionsBuilder =
SchemaRetrievalOptionsBuilder.builder().withInformationSchemaViews(informationSchemaViews);
final SchemaRetrievalOptions schemaRetrievalOptions = schemaRetrievalOptionsBuilder.toOptions();
final RetrieverConnection retrieverConnection =
new RetrieverConnection(dataSource, schemaRetrievalOptions);
final SchemaCrawlerOptions options = SchemaCrawlerOptionsBuilder.newSchemaCrawlerOptions();
final DatabaseInfoRetriever databaseInfoRetriever =
new DatabaseInfoRetriever(retrieverConnection, catalog, options);
databaseInfoRetriever.retrieveDatabaseUsers();
final List<DatabaseUser> databaseUsers = new ArrayList<>(catalog.getDatabaseUsers());
assertThat(databaseUsers, hasSize(2));
assertThat(
databaseUsers.stream().map(DatabaseUser::getName).collect(Collectors.toList()),
hasItems("OTHERUSER", "SA"));
assertThat(
databaseUsers.stream()
.map(databaseUser -> databaseUser.getAttributes().size())
.collect(Collectors.toList()),
hasItems(3, 3));
assertThat(
databaseUsers.stream()
.map(databaseUser -> databaseUser.getAttributes().keySet())
.flatMap(Collection::stream)
.collect(Collectors.toSet()),
hasItems("AUTHENTICATION", "PASSWORD_DIGEST", "ADMIN"));
}
@BeforeAll
public void loadBaseCatalog(final Connection connection) throws SQLException {
final ConnectionInfoBuilder connectionInfoBuilder = ConnectionInfoBuilder.builder(connection);
final MutableDatabaseInfo databaseInfo =
(MutableDatabaseInfo) connectionInfoBuilder.buildDatabaseInfo();
final MutableJdbcDriverInfo jdbcDriverInfo =
(MutableJdbcDriverInfo) connectionInfoBuilder.buildJdbcDriverInfo();
catalog = new MutableCatalog("database_info_test", databaseInfo, jdbcDriverInfo);
assertThat(catalog.getColumnDataTypes(), is(empty()));
assertThat(catalog.getSchemas(), is(empty()));
assertThat(catalog.getDatabaseInfo().getServerInfo(), is(empty()));
}
@Test
@DisplayName("Retrieve server info")
public void serverInfo(final TestContext testContext, final DatabaseConnectionSource dataSource)
throws Exception {
assertThat(catalog.getDatabaseInfo().getServerInfo(), is(empty()));
final String name = "TEST Server Info Property - Name";
final String description = "TEST Server Info Property - Description";
final String value = "TEST Server Info Property - Value";
final InformationSchemaViews informationSchemaViews =
InformationSchemaViewsBuilder.builder()
.withSql(
InformationSchemaKey.SERVER_INFORMATION,
String.format(
"SELECT '%s' AS NAME, '%s' AS DESCRIPTION, '%s' AS VALUE "
+ "FROM INFORMATION_SCHEMA.SYSTEM_TYPEINFO",
name, description, value))
.toOptions();
final SchemaRetrievalOptionsBuilder schemaRetrievalOptionsBuilder =
SchemaRetrievalOptionsBuilder.builder().withInformationSchemaViews(informationSchemaViews);
final SchemaRetrievalOptions schemaRetrievalOptions = schemaRetrievalOptionsBuilder.toOptions();
final RetrieverConnection retrieverConnection =
new RetrieverConnection(dataSource, schemaRetrievalOptions);
final SchemaCrawlerOptions options = SchemaCrawlerOptionsBuilder.newSchemaCrawlerOptions();
final DatabaseInfoRetriever databaseInfoRetriever =
new DatabaseInfoRetriever(retrieverConnection, catalog, options);
databaseInfoRetriever.retrieveServerInfo();
final List<Property> serverInfo = new ArrayList<>(catalog.getDatabaseInfo().getServerInfo());
assertThat(serverInfo, hasSize(1));
final Property serverInfoProperty = serverInfo.get(0);
assertThat(serverInfoProperty, is(new ImmutableServerInfoProperty(name, value, description)));
assertThat(serverInfoProperty.getDescription(), is(description));
}
}