Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for nested attributes, bug fixes and refactoring #3

Merged
merged 16 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

All notable changes to this project will be documented in this file.

## [0.0.1-RC3]
- SchemaValidationUtils - Add support for nested attributes. Refactor to validate schema and individual attributes separately.
- Simplified NamespaceDataSource to use a supplier instead of hierarchy for static and dynamic source
- LeiaBundle: Fixed duplicate instantiation of schema repository
- TimeBasedDataProvider: Fixed the initial delay of executor to prevent multiple invocation of update on startup
- Schema Validator: Removed instance creation of schema class. It's not required for static validations.

## [0.0.1-RC2]
- Added tests to the core functionality to schemaValidator, refresher, client, core and models
- Fixed the validation bug in SchemaValidationUtils. field.getType().isInstanceOf is a miss, Class.isAssignable is the correct way to check for class assignments.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ And
<dependency>
<groupId>com.grookage.leia</groupId>
<artifactId>leia-bom</artifactId>
<versio>0.0.1-RC2</version>
<versio>0.0.1-RC3</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion leia-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>com.grookage.leia</groupId>
<artifactId>leia</artifactId>
<version>0.0.1-RC2</version>
<version>0.0.1-RC3</version>
</parent>

<artifactId>leia-bom</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion leia-client-dropwizard/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>com.grookage.leia</groupId>
<artifactId>leia-parent</artifactId>
<version>0.0.1-RC2</version>
<version>0.0.1-RC3</version>
<relativePath>../leia-parent</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@
package com.grookage.leia.dw.client;

import com.google.common.base.Preconditions;
import com.google.inject.Injector;
import com.grookage.leia.client.LeiaMessageProduceClient;
import com.grookage.leia.client.datasource.NamespaceDataSource;
import com.grookage.leia.client.refresher.LeiaClientRefresher;
import com.grookage.leia.client.refresher.LeiaClientSupplier;
import com.grookage.leia.provider.config.LeiaHttpConfiguration;
import com.grookage.leia.validator.InjectableSchemaValidator;
import com.grookage.leia.validator.StaticSchemaValidator;
import com.grookage.leia.validator.LeiaSchemaValidator;
import io.dropwizard.Configuration;
import io.dropwizard.ConfiguredBundle;
Expand All @@ -47,15 +46,11 @@ public abstract class LeiaClientBundle<T extends Configuration> implements Confi

protected abstract Set<String> getPackageRoots(T configuration);

protected abstract Injector getInjector(Environment environment);

protected LeiaSchemaValidator getSchemaValidator(T configuration,
Environment environment,
LeiaClientRefresher clientRefresher) {
return InjectableSchemaValidator.builder()
.supplier(clientRefresher::getConfiguration)
return StaticSchemaValidator.builder()
.supplier(clientRefresher::getData)
.packageRoots(getPackageRoots(configuration))
.injector(getInjector(environment))
.build();
}

Expand All @@ -68,15 +63,15 @@ public void run(T configuration, Environment environment) {
final var packageRoots = getPackageRoots(configuration);
Preconditions.checkArgument(null != packageRoots && !packageRoots.isEmpty(), "Package Roots can't be null or empty");
final var withProducerClient = withProducerClient(configuration);
final var configRefreshSeconds = getRefreshIntervalSeconds(configuration);
final var dataRefreshSeconds = getRefreshIntervalSeconds(configuration);
final var clientRefresher = LeiaClientRefresher.builder()
.supplier(LeiaClientSupplier.builder()
.httpConfiguration(httpConfiguration)
.namespaceDataSource(namespaceDataSource)
.build())
.configRefreshTimeSeconds(configRefreshSeconds)
.refreshTimeInSeconds(dataRefreshSeconds)
.build();
final var validator = getSchemaValidator(configuration, environment, clientRefresher);
final var validator = getSchemaValidator(configuration, clientRefresher);
validator.start();
if (withProducerClient) {
producerClient = LeiaMessageProduceClient.builder()
Expand Down
2 changes: 1 addition & 1 deletion leia-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>com.grookage.leia</groupId>
<artifactId>leia-parent</artifactId>
<version>0.0.1-RC2</version>
<version>0.0.1-RC3</version>
<relativePath>../leia-parent</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ public abstract class AbstractSchemaClient implements LeiaSchemaClient {
private final LeiaSchemaValidator schemaValidator;

public List<SchemaDetails> getSchemaDetails() {
return refresher.getConfiguration();
return refresher.getData();
}

public List<SchemaDetails> getSchemaDetails(final Set<String> namespaces) {
if (null == refresher.getConfiguration()) {
if (null == refresher.getData()) {
throw new IllegalStateException("The configuration object has returned null data. Something gone wrong with refresher");
}
return refresher.getConfiguration().stream()
return refresher.getData().stream()
.filter(each -> namespaces.contains(each.getSchemaKey().getNamespace())).toList();
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@

package com.grookage.leia.client.datasource;

import lombok.AllArgsConstructor;

import java.util.Set;
import java.util.function.Supplier;

public interface NamespaceDataSource {
@AllArgsConstructor
public class NamespaceDataSource {

Set<String> getNamespaces();
private final Supplier<Set<String>> namespaceSupplier;

public Set<String> getNamespaces() {
return namespaceSupplier.get();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
public class LeiaClientRefresher extends HttpLeiaRefresher<List<SchemaDetails>> {

@Builder
public LeiaClientRefresher(LeiaClientSupplier supplier, int configRefreshTimeSeconds) {
super(supplier, configRefreshTimeSeconds);
public LeiaClientRefresher(LeiaClientSupplier supplier, int refreshTimeInSeconds) {
super(supplier, refreshTimeInSeconds);
}

}
subham-soni marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void testSchemaClient() {
.getResource("schema/schemaDetails.json", SchemaDetails.class);
Assertions.assertNotNull(schemaDetails);
final var schemaKey = schemaDetails.getSchemaKey();
Mockito.when(clientRefresher.getConfiguration()).thenReturn(List.of(schemaDetails));
Mockito.when(clientRefresher.getData()).thenReturn(List.of(schemaDetails));
final var details = schemaClient.getSchemaDetails();
Assertions.assertNotNull(details);
Assertions.assertTrue(schemaClient.getSchemaDetails(Set.of()).isEmpty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,26 @@ class NamespaceDataSourceTest {

@Test
void testNamespaceDataSource() {
final var staticSource = new StaticNamespaceDataSource(Set.of("testNamespace"));
Assertions.assertNotNull(staticSource);
Assertions.assertNotNull(staticSource.getNamespaces());
Assertions.assertFalse(staticSource.getNamespaces().isEmpty());
final var supplier = new Supplier<Set<String>>() {

private static final AtomicReference<Boolean> supplierMarker = new AtomicReference<>(false);

@Override
public Set<String> get() {
return supplierMarker.get() ?
Set.of("testNamespace") : Set.of();
Set.of("testNamespace") : Set.of();
}

public void mark() {
supplierMarker.set(true);
}
};
final var suppliedSource = new DynamicNamespaceDataSource(supplier);
final var suppliedSource = new NamespaceDataSource(supplier);
Assertions.assertNotNull(suppliedSource.getNamespaces());
Assertions.assertTrue(suppliedSource.getNamespaces().isEmpty());

supplier.mark();
Assertions.assertNotNull(suppliedSource.getNamespaces());
Assertions.assertFalse(suppliedSource.getNamespaces().isEmpty());


}
}
2 changes: 1 addition & 1 deletion leia-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>com.grookage.leia</groupId>
<artifactId>leia-parent</artifactId>
<version>0.0.1-RC2</version>
<version>0.0.1-RC3</version>
<relativePath>../leia-parent</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public class RepositoryRefresher extends AbstractLeiaRefresher<SchemaRegistry> {

@Builder
protected RepositoryRefresher(final RepositorySupplier supplier,
final int configRefreshTimeSeconds) {
super(supplier, configRefreshTimeSeconds);
final int refreshTimeInSeconds) {
super(supplier, refreshTimeInSeconds);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,30 +52,30 @@ public SchemaRetriever(final SchemaRepository schemaRepository,
supplier.start();
this.refresher = RepositoryRefresher.builder()
.supplier(supplier)
.configRefreshTimeSeconds(cacheConfig.getRefreshCacheSeconds())
.refreshTimeInSeconds(cacheConfig.getRefreshCacheSeconds())
.build();
}
}

public Optional<SchemaDetails> getSchemaDetails(final SchemaKey schemaKey) {
if (null != cacheConfig && cacheConfig.isEnabled()) {
return this.refresher.getConfiguration().getSchemaDetails(schemaKey);
return this.refresher.getData().getSchemaDetails(schemaKey);
} else {
return schemaRepository.get(schemaKey);
}
}

public List<SchemaDetails> getCurrentSchemaDetails(final Set<String> namespaces) {
if (null != cacheConfig && cacheConfig.isEnabled()) {
return this.refresher.getConfiguration().getSchemaDetails(namespaces);
return this.refresher.getData().getSchemaDetails(namespaces);
} else {
return schemaRepository.getSchemas(namespaces, Set.of(SchemaState.APPROVED));
}
}

public List<SchemaDetails> getAllSchemaDetails(final Set<String> namespaces) {
if (null != cacheConfig && cacheConfig.isEnabled()) {
return this.refresher.getConfiguration().getAllSchemaDetails(namespaces);
return this.refresher.getData().getAllSchemaDetails(namespaces);
} else {
return schemaRepository.getSchemas(namespaces, Arrays.stream(SchemaState.values()).collect(Collectors.toSet()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.grookage.leia.models.schema.SchemaKey;
import com.grookage.leia.models.schema.SchemaRegistry;
import com.grookage.leia.models.utils.LeiaUtils;
import com.grookage.leia.provider.exceptions.RefresherErrorCode;
import com.grookage.leia.provider.exceptions.RefresherException;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand All @@ -39,16 +41,35 @@ void testRepositoryRefresher() {
.build();
Mockito.when(supplier.get()).thenReturn(registry);
final var refresher = new RepositoryRefresher(supplier, 5);
Assertions.assertTrue(refresher.getConfiguration().getSchemas().isEmpty());
Assertions.assertTrue(refresher.getData().getSchemas().isEmpty());
registry.add(schemaDetails);
LeiaUtils.sleepFor(6);
final var schemas = refresher.getConfiguration();
final var schemas = refresher.getData();
final var schema = schemas.getSchemaDetails(SchemaKey.builder()
.namespace("testNamespace")
.schemaName("testSchema")
.version("V1234")
.build()).orElse(null);
Assertions.assertNotNull(schema);
}

@Test
void testRepositoryRefresher_whenSupplierReturnNullAtStart() {
final var supplier = Mockito.mock(RepositorySupplier.class);
Mockito.doReturn(null).when(supplier).get();
final var exception = Assertions.assertThrows(RefresherException.class,
() -> new RepositoryRefresher(supplier, 5));
Assertions.assertNotNull(exception);
Assertions.assertEquals(RefresherErrorCode.REFRESH_FAILED.getStatus(), exception.getStatus());
}

@Test
void testRepositoryRefresher_whenSupplierThrowExceptionAtStart() {
final var supplier = Mockito.mock(RepositorySupplier.class);
Mockito.doThrow(RefresherException.error(RefresherErrorCode.REFRESH_FAILED)).when(supplier).get();
final var exception = Assertions.assertThrows(RefresherException.class,
() -> new RepositoryRefresher(supplier, 5));
Assertions.assertNotNull(exception);
Assertions.assertEquals(RefresherErrorCode.REFRESH_FAILED.getStatus(), exception.getStatus());
}
}
2 changes: 1 addition & 1 deletion leia-dropwizard-es/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>com.grookage.leia</groupId>
<artifactId>leia-parent</artifactId>
<version>0.0.1-RC2</version>
<version>0.0.1-RC3</version>
<relativePath>../leia-parent</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion leia-dropwizard/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>com.grookage.leia</groupId>
<artifactId>leia-parent</artifactId>
<version>0.0.1-RC2</version>
<version>0.0.1-RC3</version>
<relativePath>../leia-parent</relativePath>
</parent>

Expand Down
Loading
Loading