Skip to content

Commit

Permalink
Merge pull request #4 from grookage/v1
Browse files Browse the repository at this point in the history
ValidationUtils Bug Fix + Tests
  • Loading branch information
koushikr authored Sep 16, 2024
2 parents 895a97a + 1d8118c commit a2d04b7
Show file tree
Hide file tree
Showing 43 changed files with 1,279 additions and 61 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

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

## [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.
- Some minor code formatting and linting fixes

## [0.0.1-RC1]

- A versioned schema registry, to register various schemas with all primitive and custom data-types, bound by a
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-RC1</version>
<versio>0.0.1-RC2</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-RC1</version>
<version>0.0.1-RC2</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-RC1</version>
<version>0.0.1-RC2</version>
<relativePath>../leia-parent</relativePath>
</parent>

Expand Down
33 changes: 32 additions & 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-RC1</version>
<version>0.0.1-RC2</version>
<relativePath>../leia-parent</relativePath>
</parent>

Expand Down Expand Up @@ -77,6 +77,37 @@
<groupId>com.grookage.leia</groupId>
<artifactId>leia-schema-validator</artifactId>
</dependency>


<dependency>
<artifactId>mockito-core</artifactId>
<groupId>org.mockito</groupId>
<scope>test</scope>
</dependency>

<dependency>
<artifactId>mockito-junit-jupiter</artifactId>
<groupId>org.mockito</groupId>
<scope>test</scope>
</dependency>

<dependency>
<artifactId>mockito-inline</artifactId>
<groupId>org.mockito</groupId>
<scope>test</scope>
</dependency>

<dependency>
<artifactId>leia-models</artifactId>
<type>test-jar</type>
<exclusions>
<exclusion>
<artifactId>*</artifactId>
<groupId>*</groupId>
</exclusion>
</exclusions>
<groupId>com.grookage.leia</groupId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public List<SchemaDetails> getSchemaDetails() {
}

public List<SchemaDetails> getSchemaDetails(final Set<String> namespaces) {
if (null == refresher.getConfiguration()) {
throw new IllegalStateException("The configuration object has returned null data. Something gone wrong with refresher");
}
return refresher.getConfiguration().stream()
.filter(each -> namespaces.contains(each.getSchemaKey().getNamespace())).toList();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (c) 2024. Koushik R <[email protected]>.
*
* 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
*
* 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.
*/

package com.grookage.leia.client;

import com.grookage.leia.client.refresher.LeiaClientRefresher;
import com.grookage.leia.models.ResourceHelper;
import com.grookage.leia.models.schema.SchemaDetails;
import com.grookage.leia.models.schema.SchemaKey;
import com.grookage.leia.validator.LeiaSchemaValidator;
import lombok.Builder;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.util.List;
import java.util.Set;

class SchemaClientTest {

static class TestableSchemaClient extends AbstractSchemaClient {

@Builder
public TestableSchemaClient(LeiaClientRefresher refresher, LeiaSchemaValidator schemaValidator) {
super(refresher, schemaValidator);
}
}

@Test
@SneakyThrows
void testSchemaClient() {
final var clientRefresher = Mockito.mock(LeiaClientRefresher.class);
final var schemaValidator = Mockito.mock(LeiaSchemaValidator.class);
final var schemaClient = TestableSchemaClient.builder()
.refresher(clientRefresher)
.schemaValidator(schemaValidator)
.build();
Assertions.assertNull(schemaClient.getSchemaDetails());
Assertions.assertThrows(IllegalStateException.class, () -> schemaClient.getSchemaDetails(Set.of("testNamespace")));
Assertions.assertFalse(schemaClient.valid(SchemaKey.builder().build()));
final var schemaDetails = ResourceHelper
.getResource("schema/schemaDetails.json", SchemaDetails.class);
Assertions.assertNotNull(schemaDetails);
final var schemaKey = schemaDetails.getSchemaKey();
Mockito.when(clientRefresher.getConfiguration()).thenReturn(List.of(schemaDetails));
final var details = schemaClient.getSchemaDetails();
Assertions.assertNotNull(details);
Assertions.assertTrue(schemaClient.getSchemaDetails(Set.of()).isEmpty());
Assertions.assertTrue(schemaClient.getSchemaDetails(Set.of("random")).isEmpty());
Assertions.assertFalse(schemaClient.getSchemaDetails(Set.of("testNamespace")).isEmpty());
Assertions.assertFalse(schemaClient.valid(schemaKey));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2024. Koushik R <[email protected]>.
*
* 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
*
* 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.
*/

package com.grookage.leia.client.datasource;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;

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();
}

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

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


}
}
35 changes: 20 additions & 15 deletions leia-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@
<parent>
<groupId>com.grookage.leia</groupId>
<artifactId>leia-parent</artifactId>
<version>0.0.1-RC1</version>
<version>0.0.1-RC2</version>
<relativePath>../leia-parent</relativePath>
</parent>

<artifactId>leia-core</artifactId>


<dependencies>

<dependency>
<groupId>com.grookage.leia</groupId>
<artifactId>leia-models</artifactId>
Expand All @@ -45,18 +43,6 @@
<artifactId>leia-refresher</artifactId>
</dependency>

<dependency>
<artifactId>leia-models</artifactId>
<type>test-jar</type>
<exclusions>
<exclusion>
<artifactId>*</artifactId>
<groupId>*</groupId>
</exclusion>
</exclusions>
<groupId>com.grookage.leia</groupId>
</dependency>

<dependency>
<artifactId>aspectjrt</artifactId>
<groupId>org.aspectj</groupId>
Expand All @@ -79,6 +65,25 @@
<groupId>org.mockito</groupId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<artifactId>leia-models</artifactId>
<type>test-jar</type>
<exclusions>
<exclusion>
<artifactId>*</artifactId>
<groupId>*</groupId>
</exclusion>
</exclusions>
<groupId>com.grookage.leia</groupId>
</dependency>

</dependencies>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,18 @@ public static SchemaProcessorHub of() {
}

public SchemaProcessorHub withSchemaRepository(SchemaRepository schemaRepository) {
Preconditions.checkNotNull(schemaRepository, "Schema Repository can't be null");
this.schemaRepository = schemaRepository;
return this;
}

public SchemaProcessorHub withVersionIDGenerator(VersionIDGenerator versionIDGenerator) {
Preconditions.checkNotNull(versionIDGenerator, "Version ID Generator can't be null");
this.versionIDGenerator = versionIDGenerator;
return this;
}

public SchemaProcessorHub build() {
Preconditions.checkNotNull(schemaRepository, "Schema Repository can't be null");
Preconditions.checkNotNull(versionIDGenerator, "Version ID Generator can't be null");
Arrays.stream(SchemaEvent.values()).forEach(this::buildProcessor);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.grookage.leia.repository.SchemaRepository;
import com.grookage.leia.repository.config.CacheConfig;
import lombok.Builder;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
Expand All @@ -32,6 +33,7 @@
import java.util.stream.Collectors;

@Slf4j
@Getter
public class SchemaRetriever {

private final SchemaRepository schemaRepository;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2024. Koushik R <[email protected]>.
*
* 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
*
* 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.
*/

package com.grookage.leia.core.ingestion.hub;

import com.grookage.leia.core.ingestion.VersionIDGenerator;
import com.grookage.leia.models.schema.engine.SchemaEvent;
import com.grookage.leia.repository.SchemaRepository;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

class SchemaProcessorHubTest {

@Test
void testSchemaProcessorHub() {
final var schemaRepository = Mockito.mock(SchemaRepository.class);
final var generator = new VersionIDGenerator() {
@Override
public String generateVersionId(String prefix) {
return "V1234";
}
};
final var hub = SchemaProcessorHub.of()
.withSchemaRepository(schemaRepository)
.withVersionIDGenerator(generator)
.build();
Assertions.assertNotNull(hub.getProcessor(SchemaEvent.CREATE_SCHEMA).orElse(null));
Assertions.assertThrows(NullPointerException.class, () -> SchemaProcessorHub.of()
.withVersionIDGenerator(generator)
.build());
Assertions.assertThrows(NullPointerException.class, () -> SchemaProcessorHub.of()
.withSchemaRepository(schemaRepository)
.build());
}
}
Loading

0 comments on commit a2d04b7

Please sign in to comment.