Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bcbc37e897 | ||
|
|
c3d6eb6a2f | ||
|
|
776e740c0a | ||
|
|
6e9532c8f5 | ||
|
|
a860d5da00 | ||
|
|
0d18708b13 | ||
|
|
ec5626a2c4 | ||
|
|
be6cc1d523 | ||
|
|
7ef3897067 | ||
|
|
04cd19e697 | ||
|
|
5296c2b042 | ||
|
|
abcf905548 | ||
|
|
fdcd0fb40b | ||
|
|
2282b398f0 | ||
|
|
9bd7376010 |
1
.gitignore
vendored
1
.gitignore
vendored
@ -32,6 +32,7 @@ build/
|
|||||||
### VS Code ###
|
### VS Code ###
|
||||||
.vscode/
|
.vscode/
|
||||||
.DS_Store/
|
.DS_Store/
|
||||||
|
.DS_Store
|
||||||
|
|
||||||
gradle.properties
|
gradle.properties
|
||||||
gradlew
|
gradlew
|
||||||
|
|||||||
@ -3,7 +3,6 @@ include:
|
|||||||
ref: 'main'
|
ref: 'main'
|
||||||
file: 'ci-templates/gradle_java.yml'
|
file: 'ci-templates/gradle_java.yml'
|
||||||
|
|
||||||
|
|
||||||
deploy:
|
deploy:
|
||||||
stage: deploy
|
stage: deploy
|
||||||
tags:
|
tags:
|
||||||
@ -18,4 +17,5 @@ deploy:
|
|||||||
rules:
|
rules:
|
||||||
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
|
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
|
||||||
- if: $CI_COMMIT_BRANCH =~ /^release/
|
- if: $CI_COMMIT_BRANCH =~ /^release/
|
||||||
|
- if: $CI_COMMIT_BRANCH =~ /^feature/
|
||||||
- if: $CI_COMMIT_TAG
|
- if: $CI_COMMIT_TAG
|
||||||
@ -1,7 +1,6 @@
|
|||||||
plugins {
|
plugins {
|
||||||
`java-library`
|
`java-library`
|
||||||
`maven-publish`
|
`maven-publish`
|
||||||
`kotlin-dsl`
|
|
||||||
pmd
|
pmd
|
||||||
checkstyle
|
checkstyle
|
||||||
jacoco
|
jacoco
|
||||||
@ -11,9 +10,6 @@ plugins {
|
|||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenLocal()
|
mavenLocal()
|
||||||
maven {
|
|
||||||
url = uri("https://pdftron.com/maven/release")
|
|
||||||
}
|
|
||||||
maven {
|
maven {
|
||||||
url = uri("https://nexus.knecon.com/repository/gindev/");
|
url = uri("https://nexus.knecon.com/repository/gindev/");
|
||||||
credentials {
|
credentials {
|
||||||
@ -26,13 +22,15 @@ repositories {
|
|||||||
|
|
||||||
val springBootVersion = "3.1.5"
|
val springBootVersion = "3.1.5"
|
||||||
val springCloudVersion = "4.0.4"
|
val springCloudVersion = "4.0.4"
|
||||||
|
val springRabbitTest = "3.0.9"
|
||||||
|
val testContainersVersion = "1.20.1"
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
api("com.fasterxml.jackson.core:jackson-databind:2.15.2:")
|
api("com.fasterxml.jackson.core:jackson-databind:2.15.2:")
|
||||||
api("com.knecon.fforesight:tenant-commons:0.24.0")
|
api("com.knecon.fforesight:tenant-commons:0.31.0-RED10196.0")
|
||||||
api("com.zaxxer:HikariCP:5.0.1")
|
api("com.zaxxer:HikariCP:5.0.1")
|
||||||
api("com.google.guava:guava:32.1.2-jre")
|
api("com.google.guava:guava:32.1.2-jre")
|
||||||
api("org.liquibase:liquibase-core:4.20.0")
|
api("org.liquibase:liquibase-core:4.29.2")
|
||||||
api("org.springframework.boot:spring-boot-starter-data-jpa:${springBootVersion}")
|
api("org.springframework.boot:spring-boot-starter-data-jpa:${springBootVersion}")
|
||||||
api("org.springframework.cloud:spring-cloud-starter-openfeign:${springCloudVersion}")
|
api("org.springframework.cloud:spring-cloud-starter-openfeign:${springCloudVersion}")
|
||||||
api("org.springframework.boot:spring-boot-starter-amqp:${springBootVersion}")
|
api("org.springframework.boot:spring-boot-starter-amqp:${springBootVersion}")
|
||||||
@ -42,7 +40,9 @@ dependencies {
|
|||||||
api("org.projectlombok:lombok:1.18.28")
|
api("org.projectlombok:lombok:1.18.28")
|
||||||
runtimeOnly("org.springframework.boot:spring-boot-devtools:${springBootVersion}")
|
runtimeOnly("org.springframework.boot:spring-boot-devtools:${springBootVersion}")
|
||||||
testImplementation("org.springframework.boot:spring-boot-starter-test:${springBootVersion}")
|
testImplementation("org.springframework.boot:spring-boot-starter-test:${springBootVersion}")
|
||||||
testImplementation("org.springframework.amqp:spring-rabbit-test:3.0.6")
|
testImplementation("org.testcontainers:postgresql:${testContainersVersion}")
|
||||||
|
testImplementation("org.springframework.amqp:spring-rabbit-test:${springRabbitTest}")
|
||||||
|
testImplementation("org.testcontainers:rabbitmq:${testContainersVersion}")
|
||||||
}
|
}
|
||||||
|
|
||||||
group = "com.knecon.fforesight"
|
group = "com.knecon.fforesight"
|
||||||
@ -93,7 +93,7 @@ tasks.named<Test>("test") {
|
|||||||
|
|
||||||
sonarqube {
|
sonarqube {
|
||||||
properties {
|
properties {
|
||||||
property("sonar.login", providers.gradleProperty("sonarToken").getOrNull())
|
providers.gradleProperty("sonarToken").getOrNull()?.let { property("sonar.login", it) }
|
||||||
property("sonar.host.url", "https://sonarqube.knecon.com")
|
property("sonar.host.url", "https://sonarqube.knecon.com")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,84 +0,0 @@
|
|||||||
package com.knecon.fforesight.databasetenantcommons;
|
|
||||||
|
|
||||||
import org.springframework.amqp.core.Binding;
|
|
||||||
import org.springframework.amqp.core.BindingBuilder;
|
|
||||||
import org.springframework.amqp.core.Queue;
|
|
||||||
import org.springframework.amqp.core.QueueBuilder;
|
|
||||||
import org.springframework.amqp.core.TopicExchange;
|
|
||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
|
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
@RequiredArgsConstructor
|
|
||||||
public class TenantMessagingConfiguration {
|
|
||||||
|
|
||||||
@Value("${fforesight.multitenancy.tenant-created-queue:tenant-created}")
|
|
||||||
private String tenantCreatedEventQueue;
|
|
||||||
|
|
||||||
@Value("${fforesight.multitenancy.tenant-created-dlq:tenant-created-dlq}")
|
|
||||||
private String tenantCreatedDLQ;
|
|
||||||
|
|
||||||
@Value("${fforesight.multitenancy.tenant-sync-queue:tenant-sync}")
|
|
||||||
private String tenantSyncEventQueue;
|
|
||||||
|
|
||||||
@Value("${fforesight.multitenancy.tenant-sync-dlq:tenant-sync-dlq}")
|
|
||||||
private String tenantSyncDQL;
|
|
||||||
|
|
||||||
|
|
||||||
@Bean(name = "tenantExchange")
|
|
||||||
TopicExchange tenantExchange(@Value("${fforesight.tenant-exchange.name}") String tenantExchangeName) {
|
|
||||||
|
|
||||||
return new TopicExchange(tenantExchangeName);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Bean("persistenceServiceTenantCreatedQueue")
|
|
||||||
public Queue persistenceServiceTenantCreatedQueue() {
|
|
||||||
|
|
||||||
return QueueBuilder.durable(tenantCreatedEventQueue)
|
|
||||||
.withArgument("x-dead-letter-exchange", "").withArgument("x-dead-letter-routing-key", tenantCreatedDLQ).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public Queue persistenceServiceTenantDLQ() {
|
|
||||||
|
|
||||||
return QueueBuilder.durable(tenantCreatedDLQ).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public Binding tenantExchangeBinding(@Qualifier("persistenceServiceTenantCreatedQueue") Queue persistenceServiceTenantCreatedQueue,
|
|
||||||
@Qualifier("tenantExchange") TopicExchange tenantExchange) {
|
|
||||||
|
|
||||||
return BindingBuilder.bind(persistenceServiceTenantCreatedQueue).to(tenantExchange).with("tenant.created");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Bean("persistenceServiceTenantSyncQueue")
|
|
||||||
public Queue persistenceServiceTenantSyncQueue() {
|
|
||||||
|
|
||||||
return QueueBuilder.durable(tenantSyncEventQueue)
|
|
||||||
.withArgument("x-dead-letter-exchange", "").withArgument("x-dead-letter-routing-key", tenantSyncDQL).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public Queue persistenceServiceTenantSyncDLQ() {
|
|
||||||
|
|
||||||
return QueueBuilder.durable(tenantSyncDQL).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public Binding tenantExchangeSyncBinding(@Qualifier("persistenceServiceTenantSyncQueue") Queue persistenceServiceTenantSyncQueue,
|
|
||||||
@Qualifier("tenantExchange") TopicExchange tenantExchange) {
|
|
||||||
|
|
||||||
return BindingBuilder.bind(persistenceServiceTenantSyncQueue).to(tenantExchange).with("tenant.sync");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -0,0 +1,31 @@
|
|||||||
|
package com.knecon.fforesight.databasetenantcommons.providers;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import com.knecon.fforesight.tenantcommons.listener.ITenantEventHandler;
|
||||||
|
import com.knecon.fforesight.tenantcommons.model.TenantCreatedEvent;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class TenantCreatedDatabaseEventHandler implements ITenantEventHandler<TenantCreatedEvent> {
|
||||||
|
|
||||||
|
private final TenantLiquibaseInitializer tenantLiquibaseInitializer;
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handle(TenantCreatedEvent tenantCreatedEvent) {
|
||||||
|
|
||||||
|
tenantLiquibaseInitializer.initializeTenant(tenantCreatedEvent.getTenantId());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<TenantCreatedEvent> getEventClass() {
|
||||||
|
|
||||||
|
return TenantCreatedEvent.class;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,39 +0,0 @@
|
|||||||
package com.knecon.fforesight.databasetenantcommons.providers;
|
|
||||||
|
|
||||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import com.knecon.fforesight.databasetenantcommons.providers.events.TenantCreatedEvent;
|
|
||||||
|
|
||||||
import jakarta.annotation.PostConstruct;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
import lombok.SneakyThrows;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
|
|
||||||
@Slf4j
|
|
||||||
@Service
|
|
||||||
@RequiredArgsConstructor
|
|
||||||
public class TenantCreatedListener {
|
|
||||||
|
|
||||||
private final TenantLiquibaseInitializer tenantLiquibaseInitializer;
|
|
||||||
|
|
||||||
@Value("${fforesight.multitenancy.tenant-created-queue:tenant-created}")
|
|
||||||
private String tenantCreatedQueue;
|
|
||||||
|
|
||||||
|
|
||||||
@PostConstruct
|
|
||||||
public void postConstruct() {
|
|
||||||
|
|
||||||
log.info("Listener for tenant-created started for queue: {}", tenantCreatedQueue);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@SneakyThrows
|
|
||||||
@RabbitListener(queues = "${fforesight.multitenancy.tenant-created-queue:tenant-created}")
|
|
||||||
public void createTenant(TenantCreatedEvent tenantRequest) {
|
|
||||||
|
|
||||||
tenantLiquibaseInitializer.initializeTenant(tenantRequest.getTenantId());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -0,0 +1,36 @@
|
|||||||
|
package com.knecon.fforesight.databasetenantcommons.providers;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import com.knecon.fforesight.tenantcommons.listener.ITenantEventHandler;
|
||||||
|
import com.knecon.fforesight.tenantcommons.model.TenantSyncEvent;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.SneakyThrows;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class TenantSyncDatabaseEventHandler implements ITenantEventHandler<TenantSyncEvent> {
|
||||||
|
|
||||||
|
private final Optional<TenantSyncService> tenantSyncServices;
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@SneakyThrows
|
||||||
|
|
||||||
|
public void handle(TenantSyncEvent tenantSyncEvent) {
|
||||||
|
|
||||||
|
tenantSyncServices.ifPresent(t -> t.syncTenant(tenantSyncEvent));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<TenantSyncEvent> getEventClass() {
|
||||||
|
|
||||||
|
return TenantSyncEvent.class;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,41 +0,0 @@
|
|||||||
package com.knecon.fforesight.databasetenantcommons.providers;
|
|
||||||
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import com.knecon.fforesight.databasetenantcommons.providers.events.TenantSyncEvent;
|
|
||||||
|
|
||||||
import jakarta.annotation.PostConstruct;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
import lombok.SneakyThrows;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
|
|
||||||
@Slf4j
|
|
||||||
@Service
|
|
||||||
@RequiredArgsConstructor
|
|
||||||
public class TenantSyncListener {
|
|
||||||
|
|
||||||
private final Optional<TenantSyncService> tenantSyncServices;
|
|
||||||
|
|
||||||
@Value("${fforesight.multitenancy.tenant-sync-queue:tenant-sync}")
|
|
||||||
private String tenantSyncQueue;
|
|
||||||
|
|
||||||
|
|
||||||
@PostConstruct
|
|
||||||
public void postConstruct() {
|
|
||||||
|
|
||||||
log.info("Listener for tenant-sync started for queue: {}", tenantSyncQueue);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@SneakyThrows
|
|
||||||
@RabbitListener(queues = "${fforesight.multitenancy.tenant-sync-queue:tenant-sync}")
|
|
||||||
public void createTenant(TenantSyncEvent tenantSyncEvent) {
|
|
||||||
|
|
||||||
tenantSyncServices.ifPresent(t -> t.syncTenant(tenantSyncEvent));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,9 +1,9 @@
|
|||||||
package com.knecon.fforesight.databasetenantcommons.providers;
|
package com.knecon.fforesight.databasetenantcommons.providers;
|
||||||
|
|
||||||
import com.knecon.fforesight.databasetenantcommons.providers.events.TenantSyncEvent;
|
import com.knecon.fforesight.tenantcommons.model.TenantSyncEvent;
|
||||||
|
|
||||||
public interface TenantSyncService {
|
public interface TenantSyncService {
|
||||||
|
|
||||||
void syncTenant(TenantSyncEvent tenantSyncEvent);
|
void syncTenant(TenantSyncEvent tenantSyncEvent);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,14 +0,0 @@
|
|||||||
package com.knecon.fforesight.databasetenantcommons.providers.events;
|
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.Data;
|
|
||||||
import lombok.NoArgsConstructor;
|
|
||||||
|
|
||||||
@Data
|
|
||||||
@AllArgsConstructor
|
|
||||||
@NoArgsConstructor
|
|
||||||
public class TenantCreatedEvent {
|
|
||||||
|
|
||||||
private String tenantId;
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,17 +0,0 @@
|
|||||||
package com.knecon.fforesight.databasetenantcommons.providers.events;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.JsonNode;
|
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.Data;
|
|
||||||
import lombok.NoArgsConstructor;
|
|
||||||
|
|
||||||
@Data
|
|
||||||
@AllArgsConstructor
|
|
||||||
@NoArgsConstructor
|
|
||||||
public class TenantSyncEvent {
|
|
||||||
|
|
||||||
private String tenantId;
|
|
||||||
private JsonNode payload;
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -0,0 +1,56 @@
|
|||||||
|
package com.knecon.fforesight.databasetenantcommons;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration;
|
||||||
|
import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.boot.test.util.TestPropertyValues;
|
||||||
|
import org.springframework.context.ApplicationContextInitializer;
|
||||||
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
|
||||||
|
import com.knecon.fforesight.databasetenantcommons.utils.SpringPostgreSQLTestContainer;
|
||||||
|
import com.knecon.fforesight.tenantcommons.MultiTenancyAutoConfiguration;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.testcontainers.containers.RabbitMQContainer;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@ContextConfiguration(initializers = {BasicIntegrationTest.Initializer.class})
|
||||||
|
@ImportAutoConfiguration({MultiTenancyAutoConfiguration.class, DatabaseTenantCommonsAutoConfiguration.class})
|
||||||
|
@SuppressWarnings("PMD.TestClassWithoutTestCases")
|
||||||
|
@SpringBootTest
|
||||||
|
public abstract class BasicIntegrationTest {
|
||||||
|
|
||||||
|
static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
|
||||||
|
|
||||||
|
@SuppressWarnings("PMD")
|
||||||
|
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
|
||||||
|
|
||||||
|
var postgreSQLContainerMaster = SpringPostgreSQLTestContainer.getInstance().withDatabaseName("integration-tests-db-master").withUsername("sa").withPassword("sa");
|
||||||
|
postgreSQLContainerMaster.start();
|
||||||
|
var port = postgreSQLContainerMaster.getJdbcUrl().substring(0, postgreSQLContainerMaster.getJdbcUrl().lastIndexOf('/')).split(":")[3];
|
||||||
|
|
||||||
|
RabbitMQContainer rabbitContainer = new RabbitMQContainer("rabbitmq:3.12");
|
||||||
|
|
||||||
|
rabbitContainer.start();
|
||||||
|
log.info("Rabbit container started and available at {}", rabbitContainer.getHttpUrl());
|
||||||
|
|
||||||
|
TestPropertyValues.of("RABBITMQ_USERNAME=" + rabbitContainer.getAdminUsername(),
|
||||||
|
"RABBITMQ_PASSWORD=" + rabbitContainer.getAdminPassword(),
|
||||||
|
"RABBITMQ_HOST=" + rabbitContainer.getHost(),
|
||||||
|
"RABBITMQ_PORT=" + rabbitContainer.getAmqpPort(),
|
||||||
|
"DB_PORT=" + port,
|
||||||
|
"DB_HOST=" + postgreSQLContainerMaster.getHost(),
|
||||||
|
"POD_NAME=tenant-database-commons"
|
||||||
|
).applyTo(configurableApplicationContext.getEnvironment());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@SpringBootApplication(exclude = {LiquibaseAutoConfiguration.class, JpaRepositoriesAutoConfiguration.class})
|
||||||
|
static class TestConfiguration {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,94 @@
|
|||||||
|
package com.knecon.fforesight.databasetenantcommons;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
|
||||||
|
|
||||||
|
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
|
||||||
|
import com.knecon.fforesight.databasetenantcommons.providers.TenantLiquibaseInitializer;
|
||||||
|
import com.knecon.fforesight.databasetenantcommons.utils.TestTenantProvider;
|
||||||
|
import com.knecon.fforesight.databasetenantcommons.utils.entity.TestEntity;
|
||||||
|
import com.knecon.fforesight.databasetenantcommons.utils.repo.TestRepository;
|
||||||
|
import com.knecon.fforesight.tenantcommons.EncryptionDecryptionService;
|
||||||
|
import com.knecon.fforesight.tenantcommons.TenantContext;
|
||||||
|
import com.knecon.fforesight.tenantcommons.model.AuthDetails;
|
||||||
|
import com.knecon.fforesight.tenantcommons.model.DatabaseConnection;
|
||||||
|
import com.knecon.fforesight.tenantcommons.model.TenantResponse;
|
||||||
|
import lombok.SneakyThrows;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
public class LiquibaseTenantInitIntegrationTest extends BasicIntegrationTest {
|
||||||
|
|
||||||
|
private final static String TENANT_1 = "tenant1";
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private EncryptionDecryptionService encryptionDecryptionService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RabbitTemplate rabbitTemplate;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private TestTenantProvider tenantProvider;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private TestRepository testRepository;
|
||||||
|
|
||||||
|
@Value("${DB_PORT}")
|
||||||
|
private String port;
|
||||||
|
|
||||||
|
@Value("${DB_HOST}")
|
||||||
|
private String host;
|
||||||
|
|
||||||
|
@Value("${fforesight.tenant-exchange.name:tenants-exchange}")
|
||||||
|
String tenantExchangeName;
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@SneakyThrows
|
||||||
|
public void testTenantInitialized() {
|
||||||
|
|
||||||
|
TenantContext.setTenantId(TENANT_1);
|
||||||
|
log.info("Creating tenant {}", TENANT_1);
|
||||||
|
var testTenant = new TenantResponse();
|
||||||
|
testTenant.setTenantId(TENANT_1);
|
||||||
|
testTenant.setGuid(TENANT_1);
|
||||||
|
testTenant.setDisplayName(TENANT_1);
|
||||||
|
testTenant.setAuthDetails(new AuthDetails());
|
||||||
|
testTenant.setDatabaseConnection(DatabaseConnection.builder()
|
||||||
|
.driver("postgresql")
|
||||||
|
.host(host)
|
||||||
|
.port(port)
|
||||||
|
.database("integration-tests-db-master")
|
||||||
|
.schema(TENANT_1)
|
||||||
|
.username("sa")
|
||||||
|
.password(encryptionDecryptionService.encrypt("sa"))
|
||||||
|
.build());
|
||||||
|
|
||||||
|
tenantProvider.addTenant(testTenant);
|
||||||
|
|
||||||
|
log.info("Sending tenant created event");
|
||||||
|
rabbitTemplate.convertAndSend(tenantExchangeName, "tenant.created", TENANT_1);
|
||||||
|
var iteration = 0;
|
||||||
|
do {
|
||||||
|
Thread.sleep(2000);
|
||||||
|
try {
|
||||||
|
testRepository.findAll();
|
||||||
|
// when find succeeds tenant was created
|
||||||
|
break;
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.debug("Waiting for liquibase to be ready");
|
||||||
|
// continue
|
||||||
|
}
|
||||||
|
} while (iteration < 15);
|
||||||
|
|
||||||
|
testRepository.save(new TestEntity(1, "Hello"));
|
||||||
|
|
||||||
|
var all = testRepository.findAll();
|
||||||
|
assertThat(all).isNotEmpty();
|
||||||
|
assertThat(all.get(0).getLabel()).isEqualTo("Hello");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,25 @@
|
|||||||
|
package com.knecon.fforesight.databasetenantcommons.utils;
|
||||||
|
|
||||||
|
import org.testcontainers.containers.PostgreSQLContainer;
|
||||||
|
|
||||||
|
public final class SpringPostgreSQLTestContainer extends PostgreSQLContainer<SpringPostgreSQLTestContainer> {
|
||||||
|
|
||||||
|
private static final String IMAGE_VERSION = "postgres:15.2";
|
||||||
|
private static SpringPostgreSQLTestContainer container;
|
||||||
|
|
||||||
|
|
||||||
|
private SpringPostgreSQLTestContainer() {
|
||||||
|
|
||||||
|
super(IMAGE_VERSION);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static SpringPostgreSQLTestContainer getInstance() {
|
||||||
|
|
||||||
|
if (container == null) {
|
||||||
|
container = new SpringPostgreSQLTestContainer();
|
||||||
|
}
|
||||||
|
return container;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,61 @@
|
|||||||
|
package com.knecon.fforesight.databasetenantcommons.utils;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import com.knecon.fforesight.tenantcommons.TenantApplicationType;
|
||||||
|
import com.knecon.fforesight.tenantcommons.TenantProvider;
|
||||||
|
import com.knecon.fforesight.tenantcommons.listener.ITenantEventHandler;
|
||||||
|
import com.knecon.fforesight.tenantcommons.model.TenantCreatedEvent;
|
||||||
|
import com.knecon.fforesight.tenantcommons.model.TenantResponse;
|
||||||
|
import com.knecon.fforesight.tenantcommons.model.UpdateDetailsRequest;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class TestTenantProvider implements TenantProvider {
|
||||||
|
|
||||||
|
private List<TenantResponse> tenants = new ArrayList<>();
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateDetails(String s, UpdateDetailsRequest updateDetailsRequest) {
|
||||||
|
//noop
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TenantResponse getTenant(String s) {
|
||||||
|
|
||||||
|
return tenants.stream()
|
||||||
|
.filter(t -> t.getTenantId().equals(s))
|
||||||
|
.findFirst()
|
||||||
|
.orElse(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<TenantResponse> getTenants() {
|
||||||
|
|
||||||
|
return tenants;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TenantApplicationType getTenantApplicationType(String s) {
|
||||||
|
|
||||||
|
return tenants.stream()
|
||||||
|
.filter(t -> t.getTenantId().equals(s))
|
||||||
|
.findFirst()
|
||||||
|
.map(TenantResponse::getApplicationType)
|
||||||
|
.orElse(TenantApplicationType.RedactManager);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void addTenant(TenantResponse tenantResponse) {
|
||||||
|
|
||||||
|
this.tenants.add(tenantResponse);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,24 @@
|
|||||||
|
package com.knecon.fforesight.databasetenantcommons.utils.entity;
|
||||||
|
|
||||||
|
import jakarta.persistence.Column;
|
||||||
|
import jakarta.persistence.Entity;
|
||||||
|
import jakarta.persistence.Id;
|
||||||
|
import jakarta.persistence.Table;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import org.testcontainers.shaded.org.checkerframework.checker.units.qual.A;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Data
|
||||||
|
@Table(name = "test")
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class TestEntity {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
private int id;
|
||||||
|
@Column
|
||||||
|
private String label;
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,9 @@
|
|||||||
|
package com.knecon.fforesight.databasetenantcommons.utils.repo;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
|
import com.knecon.fforesight.databasetenantcommons.utils.entity.TestEntity;
|
||||||
|
|
||||||
|
public interface TestRepository extends JpaRepository<TestEntity, Integer> {
|
||||||
|
|
||||||
|
}
|
||||||
34
src/test/resources/application.yaml
Normal file
34
src/test/resources/application.yaml
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: database-tenant-commons-tester
|
||||||
|
rabbitmq:
|
||||||
|
host: ${RABBITMQ_HOST:localhost}
|
||||||
|
port: ${RABBITMQ_PORT:5672}
|
||||||
|
username: ${RABBITMQ_USERNAME:user}
|
||||||
|
password: ${RABBITMQ_PASSWORD:rabbitmq}
|
||||||
|
listener:
|
||||||
|
simple:
|
||||||
|
acknowledge-mode: AUTO
|
||||||
|
concurrency: 5
|
||||||
|
retry:
|
||||||
|
enabled: true
|
||||||
|
max-attempts: 3
|
||||||
|
max-interval: 15000
|
||||||
|
prefetch: 1
|
||||||
|
|
||||||
|
multitenancy:
|
||||||
|
datasource-cache:
|
||||||
|
maximumSize: 100
|
||||||
|
expireAfterAccess: 1
|
||||||
|
packages:
|
||||||
|
repositories: 'com.knecon.fforesight.databasetenantcommons.utils.repo'
|
||||||
|
entities: 'com.knecon.fforesight.databasetenantcommons.utils.entity'
|
||||||
|
tenant:
|
||||||
|
datasource:
|
||||||
|
driverClassName: org.postgresql.Driver
|
||||||
|
liquibase:
|
||||||
|
changeLog: classpath:changelog/db.changelog-tenant.yaml
|
||||||
|
clear-checksums: true
|
||||||
|
|
||||||
|
|
||||||
|
logging.type: ${LOGGING_TYPE:CONSOLE}
|
||||||
3
src/test/resources/changelog/db.changelog-tenant.yaml
Normal file
3
src/test/resources/changelog/db.changelog-tenant.yaml
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
databaseChangeLog:
|
||||||
|
- include:
|
||||||
|
file: changelog/tenant/test-table.changelog.yaml
|
||||||
@ -0,0 +1,18 @@
|
|||||||
|
databaseChangeLog:
|
||||||
|
- changeSet:
|
||||||
|
id: test-1
|
||||||
|
author: test-1
|
||||||
|
changes:
|
||||||
|
- createTable:
|
||||||
|
columns:
|
||||||
|
- column:
|
||||||
|
constraints:
|
||||||
|
nullable: false
|
||||||
|
primaryKey: true
|
||||||
|
primaryKeyName: test_pkey
|
||||||
|
name: id
|
||||||
|
type: BIGINT
|
||||||
|
- column:
|
||||||
|
name: label
|
||||||
|
type: VARCHAR(255)
|
||||||
|
tableName: test
|
||||||
13
src/test/resources/logback-spring.xml
Normal file
13
src/test/resources/logback-spring.xml
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<configuration>
|
||||||
|
|
||||||
|
<springProperty scope="configuration" name="logType" source="logging.type"/>
|
||||||
|
<springProperty scope="context" name="application.name" source="spring.application.name"/>
|
||||||
|
<springProperty scope="context" name="version" source="project.version"/>
|
||||||
|
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
|
||||||
|
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
|
||||||
|
|
||||||
|
<root level="INFO">
|
||||||
|
<appender-ref ref="${logType}"/>
|
||||||
|
</root>
|
||||||
|
|
||||||
|
</configuration>
|
||||||
Loading…
x
Reference in New Issue
Block a user