tests added

This commit is contained in:
iliax 2023-04-27 22:45:20 +04:00
parent f25242db57
commit 2bff27244f
3 changed files with 80 additions and 31 deletions

View file

@ -1,15 +1,10 @@
package com.provectus.kafka.ui.service.integration.odd; package com.provectus.kafka.ui.service.integration.odd;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.provectus.kafka.ui.sr.api.KafkaSrClientApi; import com.provectus.kafka.ui.sr.api.KafkaSrClientApi;
import com.provectus.kafka.ui.sr.model.SchemaReference; import com.provectus.kafka.ui.sr.model.SchemaReference;
import java.util.List; import java.util.List;
import java.util.Optional;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
// logic copied from AbstractSchemaProvider:resolveReferences // logic copied from AbstractSchemaProvider:resolveReferences
@ -22,41 +17,41 @@ class SchemaReferencesResolver {
this.client = client; this.client = client;
} }
Mono<ImmutableMap<String, String>> resolve(List<SchemaReference> references) { Mono<ImmutableMap<String, String>> resolve(List<SchemaReference> refs) {
return resolveReferences(references, new State(ImmutableMap.of(), ImmutableSet.of())) return resolveReferences(
.map(State::resolved); refs == null ? List.of() : refs,
new Resolving(ImmutableMap.of(), ImmutableSet.of())).map(
Resolving::resolved
);
} }
private record State(ImmutableMap<String, String> resolved, ImmutableSet<String> visited) { private record Resolving(ImmutableMap<String, String> resolved, ImmutableSet<String> visited) {
State visit(String name) { Resolving visit(String name) {
return new State(resolved, ImmutableSet.<String>builder().addAll(visited).add(name).build()); return new Resolving(resolved, ImmutableSet.<String>builder().addAll(visited).add(name).build());
} }
State resolve(String ref, String schema) { Resolving resolve(String ref, String schema) {
return new State(ImmutableMap.<String, String>builder().putAll(resolved).put(ref, schema).build(), visited); return new Resolving(ImmutableMap.<String, String>builder().putAll(resolved).put(ref, schema).build(), visited);
} }
} }
private Mono<State> resolveReferences(List<SchemaReference> references, private Mono<Resolving> resolveReferences(List<SchemaReference> references, Resolving initState) {
State initState) { Mono<Resolving> result = Mono.just(initState);
Mono<State> result = Mono.just(initState); for (SchemaReference reference : references) {
for (var reference : Optional.ofNullable(references).orElse(List.of())) {
result = result.flatMap(state -> { result = result.flatMap(state -> {
if (state.visited().contains(reference.getName())) { if (state.visited().contains(reference.getName())) {
return Mono.just(state); return Mono.just(state);
} else { } else {
final var newState = state.visit(reference.getName()); final var newState = state.visit(reference.getName());
return client.getSubjectVersion(reference.getSubject(), String.valueOf(reference.getVersion()), true) return client.getSubjectVersion(reference.getSubject(), String.valueOf(reference.getVersion()), true)
.flatMap(subj -> .flatMap(subj ->
resolveReferences(subj.getReferences(), newState) resolveReferences(subj.getReferences(), newState)
.map(withNewRefs -> withNewRefs.resolve(reference.getName(), subj.getSchema())) .map(withNewRefs -> withNewRefs.resolve(reference.getName(), subj.getSchema()))
); );
} }
} });
);
} }
return result; return result;
} }
} }

View file

@ -2,6 +2,7 @@ package com.provectus.kafka.ui;
import com.provectus.kafka.ui.model.CompatibilityLevelDTO; import com.provectus.kafka.ui.model.CompatibilityLevelDTO;
import com.provectus.kafka.ui.model.NewSchemaSubjectDTO; import com.provectus.kafka.ui.model.NewSchemaSubjectDTO;
import com.provectus.kafka.ui.model.SchemaReferenceDTO;
import com.provectus.kafka.ui.model.SchemaSubjectDTO; import com.provectus.kafka.ui.model.SchemaSubjectDTO;
import com.provectus.kafka.ui.model.SchemaSubjectsResponseDTO; import com.provectus.kafka.ui.model.SchemaSubjectsResponseDTO;
import com.provectus.kafka.ui.model.SchemaTypeDTO; import com.provectus.kafka.ui.model.SchemaTypeDTO;
@ -190,6 +191,58 @@ class SchemaRegistryServiceTests extends AbstractIntegrationTest {
Assertions.assertEquals(schema, actual.getSchema()); Assertions.assertEquals(schema, actual.getSchema());
} }
@Test
void shouldCreateNewProtobufSchemaWithRefs() {
NewSchemaSubjectDTO requestBody = new NewSchemaSubjectDTO()
.schemaType(SchemaTypeDTO.PROTOBUF)
.subject(subject + "-ref")
.schema("""
syntax = "proto3";
message MyRecord {
int32 id = 1;
string name = 2;
}
""");
webTestClient
.post()
.uri("/api/clusters/{clusterName}/schemas", LOCAL)
.contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromPublisher(Mono.just(requestBody), NewSchemaSubjectDTO.class))
.exchange()
.expectStatus()
.isOk();
requestBody = new NewSchemaSubjectDTO()
.schemaType(SchemaTypeDTO.PROTOBUF)
.subject(subject)
.schema("""
syntax = "proto3";
import "MyRecord.proto";
message MyRecordWithRef {
int32 id = 1;
MyRecord my_ref = 2;
}
""")
.references(List.of(new SchemaReferenceDTO().name("MyRecord.proto").subject(subject + "-ref").version(1)));
SchemaSubjectDTO actual = webTestClient
.post()
.uri("/api/clusters/{clusterName}/schemas", LOCAL)
.contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromPublisher(Mono.just(requestBody), NewSchemaSubjectDTO.class))
.exchange()
.expectStatus()
.isOk()
.expectBody(SchemaSubjectDTO.class)
.returnResult()
.getResponseBody();
Assertions.assertNotNull(actual);
Assertions.assertEquals(requestBody.getReferences(), actual.getReferences());
}
@Test @Test
public void shouldReturnBackwardAsGlobalCompatibilityLevelByDefault() { public void shouldReturnBackwardAsGlobalCompatibilityLevelByDefault() {
webTestClient webTestClient
@ -278,7 +331,7 @@ class SchemaRegistryServiceTests extends AbstractIntegrationTest {
void shouldCreateNewSchemaWhenSubjectIncludesNonAsciiCharacters() { void shouldCreateNewSchemaWhenSubjectIncludesNonAsciiCharacters() {
String schema = String schema =
"{\"subject\":\"test/test\",\"schemaType\":\"JSON\",\"schema\":" "{\"subject\":\"test/test\",\"schemaType\":\"JSON\",\"schema\":"
+ "\"{\\\"type\\\": \\\"string\\\"}\"}"; + "\"{\\\"type\\\": \\\"string\\\"}\"}";
webTestClient webTestClient
.post() .post()

View file

@ -55,6 +55,7 @@ class SchemaReferencesResolverTest {
assertThat(result.block()) assertThat(result.block())
.containsExactlyEntriesOf( .containsExactlyEntriesOf(
// checking map should be ordered
ImmutableMap.<String, String>builder() ImmutableMap.<String, String>builder()
.put("ref1", "schema1") .put("ref1", "schema1")
.put("ref2_1_1", "schema2_1_1") .put("ref2_1_1", "schema2_1_1")