Skip to content

Commit

Permalink
feat(get-domain-from-backend) Use new Helium API to fetch backend domain
Browse files Browse the repository at this point in the history
* Update Helium to 1.4.1
* Add assertions to major tests
* Remove Consts.java from tests
* Add call to get api version and default domain
* Persist fallback domain to database
* Add FallbackDomainFetcher to handle fetch  from cache, database and API
* Properly receive thrown exception from dropwizard task execution
* Add Mockito for testing mocks
* Add JavaDoc for FallbackDomainFetcher
  • Loading branch information
alexandreferris committed Oct 7, 2024
1 parent 84673c0 commit b2137d6
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 12 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<artifactId>mockito-core</artifactId>
<version>5.14.1</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/wire/bots/hold/FallbackDomainFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ public class FallbackDomainFetcher implements Runnable {
* Fetches from API and compares against database value (if any), then inserts into database and updates cache value.
* If value received from the API is different from what is saved in the database, a [RuntimeException] is thrown.
* </p>
* <p>
* This fallback domain is necessary for LegalHold to work with Federation (as it needs id@domain) and not just the ID anymore.
* In case there is a mismatch we are throwing a RuntimeException so it stops the execution of this app, so in an event
* of already having a defined default domain saved in the database and this app restarts with a different domain
* we don't get mismatching domains.
* </p>
* @param loginClient [{@link LoginClient}] as API to get backend configuration containing default domain.
* @param metadataDAO [{@link MetadataDAO}] as DAO to get/insert default domain to database.
*
Expand All @@ -41,9 +47,12 @@ public void run() {
metadataDAO.insert(MetadataDAO.FALLBACK_DOMAIN_KEY, apiVersionResponse.domain);
Cache.setFallbackDomain(apiVersionResponse.domain);
} else {
System.out.println("EEEEEEEE -> " + apiVersionResponse.domain);
if (metadata.value.equals(apiVersionResponse.domain)) {
System.out.println("EEEEEEEE -> p1");
Cache.setFallbackDomain(apiVersionResponse.domain);
} else {
System.out.println("EEEEEEEE -> p2");
String formattedExceptionMessage = String.format(
"Database already has a default domain as %s and instead we got %s from the Backend API.",
metadata.value,
Expand Down
36 changes: 29 additions & 7 deletions src/main/java/com/wire/bots/hold/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
import org.jdbi.v3.sqlobject.SqlObjectPlugin;

import javax.ws.rs.client.Client;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public class Service extends Application<Config> {
Expand Down Expand Up @@ -92,7 +94,7 @@ protected SwaggerBundleConfiguration getSwaggerBundleConfiguration(Config config
}

@Override
public void run(Config config, Environment environment) {
public void run(Config config, Environment environment) throws ExecutionException, InterruptedException {
this.config = config;
this.environment = environment;
Service.metrics = environment.metrics();
Expand Down Expand Up @@ -129,17 +131,37 @@ public void run(Config config, Environment environment) {

addResource(ServiceAuthenticationFilter.ServiceAuthenticationFeature.class);

environment
.lifecycle()
.executorService("fallback_domain_fetcher")
.build()
.execute(
// environment
// .lifecycle()
// .executorService("fallback_domain_fetcher")
// .build()
// .submit(
// new FallbackDomainFetcher(
// new LoginClient(httpClient),
// metadataDAO
// )
// );

Runnable run = new Runnable() {
@Override
public void run(){
new FallbackDomainFetcher(
new LoginClient(httpClient),
metadataDAO
)
).run();
}
};

final Future<?> fallbackDomainFetcher = environment
.lifecycle()
.executorService("fallback_domain_fetcher")
.build()
.submit(
run
);

fallbackDomainFetcher.get();

environment.healthChecks().register(
"SanityCheck",
new SanityCheck(accessDAO, httpClient)
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/db/migration/V107__add_metadata_table.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE TABLE Metadata (
key VARCHAR NOT NULL UNIQUE,
value VARCHAR NOT NULL
key VARCHAR(255) PRIMARY KEY,
value VARCHAR(255) NOT NULL
);
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.junit.Before;
import org.junit.Test;

import static org.mockito.Matchers.any;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verify;
Expand Down

0 comments on commit b2137d6

Please sign in to comment.