Skip to content

Commit

Permalink
Add small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
subham-soni committed Sep 13, 2024
1 parent 895a97a commit 4d21134
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
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 @@ -50,12 +50,10 @@ public abstract class LeiaClientBundle<T extends Configuration> implements Confi
protected abstract Injector getInjector(Environment environment);

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

Expand All @@ -76,7 +74,7 @@ public void run(T configuration, Environment environment) {
.build())
.configRefreshTimeSeconds(configRefreshSeconds)
.build();
final var validator = getSchemaValidator(configuration, environment, clientRefresher);
final var validator = getSchemaValidator(configuration, clientRefresher);
validator.start();
if (withProducerClient) {
producerClient = LeiaMessageProduceClient.builder()
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 @@ -69,14 +69,14 @@ protected List<Lifecycle> withLifecycleManagers(T configuration) {
public void run(T configuration, Environment environment) {
final var userResolver = userResolver(configuration);
Preconditions.checkNotNull(userResolver, "User Resolver can't be null");
this.schemaRepository = getSchemaRepository(configuration);
final var schemaProcessorHub = SchemaProcessorHub.of()
.withSchemaRepository(getSchemaRepository(configuration))
.withSchemaRepository(schemaRepository)
.withVersionIDGenerator(getVersionIDGenerator())
.build();
this.schemaIngestor = new SchemaIngestor<U>()
.withProcessorHub(schemaProcessorHub)
.build();
this.schemaRepository = getSchemaRepository(configuration);
final var cacheConfig = getCacheConfig(configuration);
this.schemaRetriever = new SchemaRetriever(schemaRepository, cacheConfig);
withLifecycleManagers(configuration)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.util.Set;

@EqualsAndHashCode(callSuper = true)
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public TimeBasedDataProvider(Supplier<T> configSupplier, T initialDefaultValue,

public void start() {
this.executorService.scheduleWithFixedDelay(this.updater, 0L, this.delay, this.timeUnit);
this.update();
}

public void stop() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,17 @@
import java.util.stream.Collectors;

@Slf4j
public class InjectableSchemaValidator implements LeiaSchemaValidator {
public class StaticSchemaValidator implements LeiaSchemaValidator {

private final ConcurrentHashMap<SchemaKey, Boolean> validationRegistry = new ConcurrentHashMap<>();
private final Supplier<List<SchemaDetails>> supplier;
private final Set<String> packageRoots;
private final Injector injector;

@Builder
public InjectableSchemaValidator(Supplier<List<SchemaDetails>> supplier,
Set<String> packageRoots,
Injector injector) {
public StaticSchemaValidator(Supplier<List<SchemaDetails>> supplier,
Set<String> packageRoots) {
this.supplier = supplier;
this.packageRoots = packageRoots;
this.injector = injector;
}

public Optional<SchemaDetails> getSchemaDetails(final SchemaKey schemaKey) {
Expand All @@ -58,12 +55,12 @@ public Optional<SchemaDetails> getSchemaDetails(final SchemaKey schemaKey) {
}

@SneakyThrows
private <T> boolean validate(final SchemaKey schemaKey, T data) {
private <T> boolean validate(final SchemaKey schemaKey, Class<?> clazz) {
final var details = getSchemaDetails(schemaKey).orElse(null);
if (null == details) {
throw SchemaValidationException.error(ValidationErrorCode.NO_SCHEMA_FOUND);
}
return SchemaValidationUtils.valid(details, data.getClass());
return SchemaValidationUtils.valid(details, clazz);
}

@Override
Expand All @@ -73,14 +70,13 @@ public void start() {
final var reflections = new Reflections(handlerPackage);
final var annotatedClasses = reflections.getTypesAnnotatedWith(SchemaValidator.class);
annotatedClasses.forEach(annotatedClass -> {
final var instance = injector.getInstance(annotatedClass);
final var annotation = instance.getClass().getAnnotation(SchemaValidator.class);
final var annotation = annotatedClass.getAnnotation(SchemaValidator.class);
final var schemaKey = SchemaKey.builder()
.schemaName(annotation.schemaName())
.version(annotation.versionId())
.namespace(annotation.namespace())
.build();
validationRegistry.putIfAbsent(schemaKey, validate(schemaKey, instance));
validationRegistry.putIfAbsent(schemaKey, validate(schemaKey, annotatedClass));
});
});
final var invalidSchemas = validationRegistry.keySet().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static boolean valid(final SchemaDetails schemaDetails,
final var validationType = schemaDetails.getValidationType();
final var allAttributesListed = fields.stream().filter(each -> schemaDetails.hasAttribute(each.getName()))
.collect(Collectors.toSet());
if (!allAttributesListed.isEmpty() &&
if (allAttributesListed.size() != fields.size() &&
validationType == SchemaValidationType.STRICT) {
log.error("There seems to be attributes present in the class definition that are not in the schema. " +
"[Validation Failed]. The extra attributes are {}",
Expand Down

0 comments on commit 4d21134

Please sign in to comment.