2021-08-05 18:33:03 -07:00
|
|
|
/*
|
2021-12-16 07:13:50 -08:00
|
|
|
* Copyright 2021 Collate
|
2021-12-01 12:46:28 +05:30
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2021-08-05 18:33:03 -07:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-09-14 23:14:02 -07:00
|
|
|
package org.openmetadata.service;
|
2021-08-01 14:27:44 -07:00
|
|
|
|
2022-04-27 18:58:44 +01:00
|
|
|
import static java.lang.String.format;
|
|
|
|
|
|
|
|
import io.dropwizard.testing.ConfigOverride;
|
2021-08-01 14:27:44 -07:00
|
|
|
import io.dropwizard.testing.ResourceHelpers;
|
|
|
|
import io.dropwizard.testing.junit5.DropwizardAppExtension;
|
2021-12-16 07:13:50 -08:00
|
|
|
import javax.ws.rs.client.Client;
|
|
|
|
import javax.ws.rs.client.WebTarget;
|
2022-01-20 02:06:48 +05:30
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2022-04-27 18:58:44 +01:00
|
|
|
import org.flywaydb.core.Flyway;
|
2021-11-12 19:28:26 +01:00
|
|
|
import org.glassfish.jersey.client.ClientProperties;
|
|
|
|
import org.glassfish.jersey.client.HttpUrlConnectorProvider;
|
2022-04-27 18:58:44 +01:00
|
|
|
import org.junit.jupiter.api.AfterAll;
|
|
|
|
import org.junit.jupiter.api.BeforeAll;
|
2022-09-14 23:14:02 -07:00
|
|
|
import org.openmetadata.service.fernet.Fernet;
|
|
|
|
import org.openmetadata.service.resources.CollectionRegistry;
|
|
|
|
import org.openmetadata.service.resources.events.WebhookCallbackResource;
|
|
|
|
import org.openmetadata.service.security.policyevaluator.PolicyCache;
|
|
|
|
import org.openmetadata.service.security.policyevaluator.RoleCache;
|
|
|
|
import org.openmetadata.service.security.policyevaluator.SubjectCache;
|
2022-04-27 18:58:44 +01:00
|
|
|
import org.testcontainers.containers.JdbcDatabaseContainer;
|
2021-08-01 14:27:44 -07:00
|
|
|
|
2022-01-20 02:06:48 +05:30
|
|
|
@Slf4j
|
2022-09-17 11:35:45 -07:00
|
|
|
public abstract class OpenMetadataApplicationTest {
|
2022-04-27 18:58:44 +01:00
|
|
|
protected static final String CONFIG_PATH = ResourceHelpers.resourceFilePath("openmetadata-secure-test.yaml");
|
|
|
|
private static JdbcDatabaseContainer<?> SQL_CONTAINER;
|
2022-09-17 11:35:45 -07:00
|
|
|
public static DropwizardAppExtension<OpenMetadataApplicationConfig> APP;
|
2021-12-23 11:26:00 -08:00
|
|
|
protected static final WebhookCallbackResource webhookCallbackResource = new WebhookCallbackResource();
|
2022-02-08 09:50:39 +01:00
|
|
|
public static final String FERNET_KEY_1 = "ihZpp5gmmDvVsgoOG6OVivKWwC9vd5JQ";
|
2021-08-01 14:27:44 -07:00
|
|
|
|
|
|
|
static {
|
2021-12-23 11:26:00 -08:00
|
|
|
CollectionRegistry.addTestResource(webhookCallbackResource);
|
2022-02-08 09:50:39 +01:00
|
|
|
Fernet.getInstance().setFernetKey(FERNET_KEY_1);
|
2021-08-01 14:27:44 -07:00
|
|
|
}
|
|
|
|
|
2022-04-27 18:58:44 +01:00
|
|
|
@BeforeAll
|
|
|
|
public static void createApplication() throws Exception {
|
|
|
|
// The system properties are provided by maven-surefire for testing with mysql and postgres
|
|
|
|
final String jdbcContainerClassName = System.getProperty("jdbcContainerClassName");
|
|
|
|
final String jdbcContainerImage = System.getProperty("jdbcContainerImage");
|
2022-05-16 15:26:20 -07:00
|
|
|
LOG.info("Using test container class {} and image {}", jdbcContainerClassName, jdbcContainerImage);
|
2022-04-27 18:58:44 +01:00
|
|
|
|
|
|
|
SQL_CONTAINER =
|
|
|
|
(JdbcDatabaseContainer<?>)
|
|
|
|
Class.forName(jdbcContainerClassName).getConstructor(String.class).newInstance(jdbcContainerImage);
|
2022-04-28 07:30:56 -07:00
|
|
|
SQL_CONTAINER.withReuse(true);
|
|
|
|
SQL_CONTAINER.withStartupTimeoutSeconds(240);
|
|
|
|
SQL_CONTAINER.withConnectTimeoutSeconds(240);
|
2022-04-27 18:58:44 +01:00
|
|
|
SQL_CONTAINER.start();
|
|
|
|
|
|
|
|
final String migrationScripsLocation =
|
|
|
|
ResourceHelpers.resourceFilePath("db/sql/" + SQL_CONTAINER.getDriverClassName());
|
|
|
|
Flyway flyway =
|
|
|
|
Flyway.configure()
|
|
|
|
.dataSource(SQL_CONTAINER.getJdbcUrl(), SQL_CONTAINER.getUsername(), SQL_CONTAINER.getPassword())
|
|
|
|
.table("DATABASE_CHANGE_LOG")
|
|
|
|
.locations("filesystem:" + migrationScripsLocation)
|
|
|
|
.sqlMigrationPrefix("v")
|
|
|
|
.load();
|
|
|
|
flyway.clean();
|
|
|
|
flyway.migrate();
|
|
|
|
|
|
|
|
APP =
|
|
|
|
new DropwizardAppExtension<>(
|
2022-09-17 11:35:45 -07:00
|
|
|
OpenMetadataApplication.class,
|
2022-04-27 18:58:44 +01:00
|
|
|
CONFIG_PATH,
|
|
|
|
// Database overrides
|
|
|
|
ConfigOverride.config("database.driverClass", SQL_CONTAINER.getDriverClassName()),
|
|
|
|
ConfigOverride.config("database.url", SQL_CONTAINER.getJdbcUrl()),
|
|
|
|
ConfigOverride.config("database.user", SQL_CONTAINER.getUsername()),
|
|
|
|
ConfigOverride.config("database.password", SQL_CONTAINER.getPassword()),
|
|
|
|
// Migration overrides
|
|
|
|
ConfigOverride.config("migrationConfiguration.path", migrationScripsLocation));
|
2022-10-03 21:51:57 -07:00
|
|
|
|
2022-04-27 18:58:44 +01:00
|
|
|
APP.before();
|
2021-08-01 14:27:44 -07:00
|
|
|
}
|
2021-11-09 20:56:45 -08:00
|
|
|
|
2022-04-27 18:58:44 +01:00
|
|
|
@AfterAll
|
2022-08-03 12:08:19 -07:00
|
|
|
public static void stopApplication() throws Exception {
|
2022-04-28 07:30:56 -07:00
|
|
|
// If BeforeAll causes and exception AfterAll still gets called before that exception is thrown.
|
2022-04-27 18:58:44 +01:00
|
|
|
// If a NullPointerException is thrown during the cleanup of above it will eat the initial error
|
|
|
|
if (APP != null) {
|
|
|
|
APP.after();
|
2022-08-03 12:08:19 -07:00
|
|
|
APP.getEnvironment().getApplicationContext().getServer().stop();
|
2022-04-27 18:58:44 +01:00
|
|
|
}
|
2022-08-06 12:50:23 -07:00
|
|
|
SubjectCache.cleanUp();
|
|
|
|
PolicyCache.cleanUp();
|
2022-08-03 12:08:19 -07:00
|
|
|
RoleCache.cleanUp();
|
2022-01-30 12:53:17 -08:00
|
|
|
}
|
|
|
|
|
2022-04-27 18:58:44 +01:00
|
|
|
public static Client getClient() {
|
|
|
|
return APP.client()
|
|
|
|
.property(ClientProperties.CONNECT_TIMEOUT, 0)
|
|
|
|
.property(ClientProperties.READ_TIMEOUT, 0)
|
|
|
|
.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static WebTarget getResource(String collection) {
|
|
|
|
return getClient().target(format("http://localhost:%s/api/v1/%s", APP.getLocalPort(), collection));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static WebTarget getConfigResource(String resource) {
|
|
|
|
return getClient().target(format("http://localhost:%s/api/v1/config/%s", APP.getLocalPort(), resource));
|
2021-11-09 20:56:45 -08:00
|
|
|
}
|
2021-12-16 07:13:50 -08:00
|
|
|
}
|