Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
carlesarnal committed Nov 29, 2024
1 parent 26c14a3 commit 187ca1a
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.apicurio.registry.support.HealthUtils;
import io.apicurio.registry.support.TestCmmn;
import io.apicurio.registry.types.RuleType;
import io.apicurio.registry.utils.tests.BaseHttpUtils;
import io.apicurio.registry.utils.tests.TestUtils;
import io.confluent.connect.avro.AvroConverter;
import io.confluent.kafka.schemaregistry.CompatibilityLevel;
Expand Down Expand Up @@ -52,6 +53,7 @@
import io.confluent.kafka.serializers.protobuf.KafkaProtobufSerializer;
import io.confluent.kafka.serializers.protobuf.KafkaProtobufSerializerConfig;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.response.Response;
import jakarta.enterprise.inject.Typed;
import org.apache.avro.AvroTypeException;
import org.apache.avro.Schema;
Expand All @@ -62,6 +64,9 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.io.UncheckedIOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -1530,6 +1535,41 @@ public void testSubjectCompatibilityAfterDeletingSubject() throws Exception {
assertEquals(ErrorCode.SUBJECT_COMPATIBILITY_NOT_CONFIGURED.value(), rce.getErrorCode(), "Compatibility Level doesn't exist");
}
assertEquals(FULL.name, confluentClient.getConfig(null).getCompatibilityLevel(), "Top Compatibility Level Exists");
}

@Test
void compatibilityGlobalRules() throws Exception {
var first = "{\"type\":\"record\",\"name\":\"myrecord1\",\"fields\":[{\"name\":\"foo\",\"type\":\"string\"}]}";
// Adding a default value to the new field to keep full compatibility
var second = "{\"type\":\"record\",\"name\":\"myrecord1\",\"fields\":[{\"name\":\"foo\",\"type\":\"string\"}, {\"name\":\"bar\",\"type\":\"string\", \"default\": \"42\"}]}";
var invalid = "{\"type\": \"bloop\"}";

confluentClient.updateCompatibility("FULL", null);

String schemeSubject = TestUtils.generateArtifactId();
confluentClient.registerSchema(first, schemeSubject);

confluentClient.registerSchema(second, schemeSubject);

testCompatibility(wrap(invalid), schemeSubject, 422);

confluentClient.deleteSubject(Collections.emptyMap(), schemeSubject);
confluentClient.deleteSubject(Collections.emptyMap(), schemeSubject, true);
}

private static String wrap(String schema) {
return "{\"schema\": \"" + schema.replace("\"", "\\\"") + "\"}";
}

public Response testCompatibility(String body, String schemaName, int returnCode) {
try {
URL url = new URL("http://localhost:" + testPort + "/apis/ccompat/v7/compatibility/subjects/" + schemaName + "/versions/latest");
return BaseHttpUtils.rulesPostRequest(SR, body, url, returnCode);
} catch (MalformedURLException e) {
throw new UncheckedIOException(e);
}
}

public static final String SR = "application/vnd.schemaregistry.v1+json";

}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void cleanArtifacts() throws Exception {

@Test
public void testConfiguration() throws Exception {
String groupId = TestUtils.generateGroupId();
String groupId = TestUtils.generateSubject();
String topic = TestUtils.generateArtifactId();
String recordName = "myrecord4";
AvroGenericRecordSchemaFactory schemaFactory = new AvroGenericRecordSchemaFactory(groupId, recordName, List.of("bar"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ void evolveSchemaTest(boolean reuseClients) throws Exception {
String topicName = TestUtils.generateTopic();
kafkaCluster.createTopic(topicName, 1, 1);

String recordNamespace = TestUtils.generateGroupId();
String recordNamespace = TestUtils.generateSubject();
String recordName = TestUtils.generateSubject();
String schemaKey = "key1";
AvroGenericRecordSchemaFactory avroSchema = new AvroGenericRecordSchemaFactory(recordNamespace, recordName, List.of(schemaKey));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ void createAndUpdateArtifact() throws Exception {
@Test
void createAndDeleteMultipleArtifacts() throws Exception {
LOGGER.info("Creating some artifacts...");
String groupId = TestUtils.generateGroupId();
String groupId = TestUtils.generateSubject();

List<ArtifactMetaData> artifacts = IntStream.range(0, 10)
.mapToObj(i -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ protected Set<Incompatibility> isBackwardsCompatibleWith(String existing, String
}
} catch (AvroRuntimeException ex) {
throw new UnprocessableSchemaException("Could not execute compatibility rule on invalid Avro schema", ex);
} catch (NullPointerException ex) {
//I hate this, but Avro is throwing this exception for invalid schemas...
throw new UnprocessableSchemaException("Could not execute compatibility rule on invalid Avro schema", ex);
}
}

Expand Down
4 changes: 4 additions & 0 deletions utils/tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@
<groupId>io.apicurio</groupId>
<artifactId>apicurio-common-rest-client-common</artifactId>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
</dependency>

</dependencies>
<dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
/*
* Copyright 2020 Red Hat
*
* 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 io.apicurio.registry.utils.tests;

import io.restassured.response.Response;

import java.net.URL;

import static io.restassured.RestAssured.given;

public class BaseHttpUtils {

public static Response getRequest(String contentType, String endpoint, int returnCode) {
return given()
.when()
.contentType(contentType)
.get(endpoint)
.then()
.statusCode(returnCode)
.extract()
.response();
}

public static Response getRequest(String contentType, URL endpoint, int returnCode) {
return given()
.when()
.contentType(contentType)
.get(endpoint)
.then()
.statusCode(returnCode)
.extract()
.response();
}

public static Response postRequest(String contentType, String body, String endpoint, int returnCode) {
return given()
.when()
.contentType(contentType)
.body(body)
.post(endpoint)
.then()
.statusCode(returnCode)
.extract()
.response();
}

public static Response postRequest(String contentType, String body, URL endpoint, int returnCode) {
return given()
.when()
.contentType(contentType)
.body(body)
.post(endpoint)
.then()
.statusCode(returnCode)
.extract()
.response();
}

public static Response putRequest(String contentType, String body, String endpoint, int returnCode) {
return given()
.when()
.contentType(contentType)
.body(body)
.put(endpoint)
.then()
.statusCode(returnCode)
.extract()
.response();
}

public static Response putRequest(String contentType, String body, URL endpoint, int returnCode) {
return given()
.when()
.contentType(contentType)
.body(body)
.put(endpoint)
.then()
.statusCode(returnCode)
.extract()
.response();
}

public static Response deleteRequest(String contentType, String endpoint, int returnCode) {
return given()
.when()
.contentType(contentType)
.delete(endpoint)
.then()
.statusCode(returnCode)
.extract()
.response();
}

public static Response rulesPostRequest(String contentType, String rule, String endpoint, int returnCode) {
return given()
.when()
.contentType(contentType)
.body(rule)
.post(endpoint)
.then()
.statusCode(returnCode)
.extract()
.response();
}

public static Response rulesPostRequest(String contentType, String rule, URL endpoint, int returnCode) {
return given()
.when()
.contentType(contentType)
.body(rule)
.post(endpoint)
.then()
.statusCode(returnCode)
.extract()
.response();
}

public static Response rulesGetRequest(String contentType, String endpoint, int returnCode) {
return given()
.when()
.contentType(contentType)
.get(endpoint)
.then()
.statusCode(returnCode)
.extract()
.response();
}

public static Response rulesPutRequest(String contentType, String rule, String endpoint, int returnCode) {
return given()
.when()
.contentType(contentType)
.body(rule)
.put(endpoint)
.then()
.statusCode(returnCode)
.extract()
.response();
}

public static Response rulesDeleteRequest(String contentType, String endpoint, int returnCode) {
return given()
.when()
.contentType(contentType)
.delete(endpoint)
.then()
.statusCode(returnCode)
.extract()
.response();
}

public static Response artifactPostRequest(String artifactId, String contentType, String body, String endpoint, int returnCode) {
return given()
.when()
.header("X-Registry-Artifactid", artifactId)
.contentType(contentType)
.body(body)
.post(endpoint)
.then()
.statusCode(returnCode)
.extract()
.response();
}

}

0 comments on commit 187ca1a

Please sign in to comment.