Skip to content

It fix 2 #340

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

Merged
merged 3 commits into from
Mar 17, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,22 @@ protected void beforeEach() {
protected void createArtifact(String artifactId, ArtifactType artifactType, String artifactContent) {
given()
.when()
.contentType(CT_JSON)
.header("X-Registry-ArtifactId", artifactId)
.header("X-Registry-ArtifactType", artifactType.name())
.body(artifactContent)
.post("/artifacts")
.contentType(CT_JSON)
.header("X-Registry-ArtifactId", artifactId)
.header("X-Registry-ArtifactType", artifactType.name())
.body(artifactContent)
.post("/artifacts")
.then()
.statusCode(200)
.body("id", equalTo(artifactId))
.body("type", equalTo(artifactType.name()));
.statusCode(200)
.body("id", equalTo(artifactId))
.body("type", equalTo(artifactType.name()));
}

protected static void retry(Runnable runnable) throws Exception {
retry(() -> {
runnable.run();
return null;
});
}

protected static <T> T retry(Callable<T> callable) throws Exception {
Expand Down
45 changes: 44 additions & 1 deletion app/src/test/java/io/apicurio/registry/RegistryClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.concurrent.CompletionStage;
import java.util.stream.Collectors;
import java.util.stream.LongStream;

/**
* @author Ales Justin
Expand Down Expand Up @@ -59,7 +62,6 @@ public void testAsyncCRUD() throws Exception {
ArtifactMetaData artifactMetaData = service.getArtifactMetaData(artifactId);
Assertions.assertNotNull(artifactMetaData);
Assertions.assertEquals("myname", artifactMetaData.getName());
return null;
});

stream = new ByteArrayInputStream("{\"name\":\"ibm\"}".getBytes(StandardCharsets.UTF_8));
Expand All @@ -70,4 +72,45 @@ public void testAsyncCRUD() throws Exception {
}
}
}

@Test
void deleteArtifactSpecificVersion() throws Exception {
try (RegistryService service = RegistryClient.cached("http://localhost:8081")) {
ByteArrayInputStream artifactData = new ByteArrayInputStream("{\"type\":\"record\",\"name\":\"myrecordx\",\"fields\":[{\"name\":\"foo\",\"type\":\"string\"}]}".getBytes(StandardCharsets.UTF_8));
String artifactId = generateArtifactId();
ConcurrentUtil.result(service.createArtifact(ArtifactType.AVRO, artifactId, artifactData));

for (int x = 0; x < 9; x++) {
String artifactDefinition = "{\"type\":\"record\",\"name\":\"myrecordx\",\"fields\":[{\"name\":\"foo" + x + "\",\"type\":\"string\"}]}";
artifactData = new ByteArrayInputStream(artifactDefinition.getBytes(StandardCharsets.UTF_8));
ConcurrentUtil.result(service.updateArtifact(artifactId, ArtifactType.AVRO, artifactData));
}

retry(() -> {
List<Long> artifactVersions = service.listArtifactVersions(artifactId);
List<Long> expectedVersions = LongStream.range(1, 11).boxed().collect(Collectors.toList());
Assertions.assertEquals(artifactVersions, expectedVersions);
});

service.deleteArtifactVersion(4, artifactId);

retry(() -> {
List<Long> artifactVersions = service.listArtifactVersions(artifactId);
List<Long> expectedVersions = LongStream.range(1, 11).boxed().filter(l -> l != 4).collect(Collectors.toList());
Assertions.assertEquals(artifactVersions, expectedVersions);
});

assertWebError(404, () -> service.getArtifactVersion(4, artifactId));

artifactData = new ByteArrayInputStream("{\"type\":\"record\",\"name\":\"myrecordx\",\"fields\":[{\"name\":\"foo11\",\"type\":\"string\"}]}".getBytes(StandardCharsets.UTF_8));
service.updateArtifact(artifactId, ArtifactType.AVRO, artifactData);

retry(() -> {
List<Long> artifactVersions = service.listArtifactVersions(artifactId);
List<Long> expectedVersions = LongStream.range(1, 12).boxed().filter(l -> l != 4).collect(Collectors.toList());
Assertions.assertEquals(artifactVersions, expectedVersions);
});
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,6 @@

package io.apicurio.registry.client;

import java.io.InputStream;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.NavigableMap;
import java.util.TreeMap;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

import javax.enterprise.inject.Vetoed;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;

import io.apicurio.registry.rest.beans.ArtifactMetaData;
import io.apicurio.registry.rest.beans.EditableMetaData;
import io.apicurio.registry.rest.beans.Rule;
Expand All @@ -39,6 +25,15 @@
import io.apicurio.registry.types.RuleType;
import io.apicurio.registry.utils.IoUtil;

import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ConcurrentHashMap;
import javax.enterprise.inject.Vetoed;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;

/**
* @author Ales Justin
*/
Expand All @@ -48,7 +43,6 @@ class CachedRegistryService implements RegistryService {

private final RegistryService delegate;

private final Map<String, NavigableMap<Integer, ArtifactMetaData>> amds = new ConcurrentHashMap<>();
private final Map<String, Map<String, ArtifactMetaData>> cmds = new ConcurrentHashMap<>();
private final Map<String, Map<Integer, VersionMetaData>> vmds = new ConcurrentHashMap<>();
private final Map<Long, ArtifactMetaData> globalAMD = new ConcurrentHashMap<>();
Expand All @@ -73,20 +67,16 @@ private RegistryService getDelegate() {

@Override
public void reset() {
amds.clear();
cmds.clear();
vmds.clear();
globalAMD.clear();
}

@Override
public ArtifactMetaData getArtifactMetaData(String artifactId) {
NavigableMap<Integer, ArtifactMetaData> map = amds.computeIfAbsent(artifactId, id -> new TreeMap<>());
if (map.isEmpty()) {
ArtifactMetaData amd = getDelegate().getArtifactMetaData(artifactId);
map.put(amd.getVersion(), amd);
globalAMD.put(amd.getGlobalId(), amd);
}
return map.lastEntry().getValue();
ArtifactMetaData amd = getDelegate().getArtifactMetaData(artifactId);
globalAMD.put(amd.getGlobalId(), amd);
return amd;
}

/**
Expand All @@ -95,7 +85,7 @@ public ArtifactMetaData getArtifactMetaData(String artifactId) {
@Override
public ArtifactMetaData getArtifactMetaDataByContent(String artifactId, InputStream data) {
String content = IoUtil.toString(data);
Map<String, ArtifactMetaData> map = cmds.computeIfAbsent(artifactId, id -> new TreeMap<>());
Map<String, ArtifactMetaData> map = cmds.computeIfAbsent(artifactId, id -> new ConcurrentHashMap<>());
return map.computeIfAbsent(content, c -> {
InputStream copy = IoUtil.toStream(content);
ArtifactMetaData amd = getDelegate().getArtifactMetaDataByContent(artifactId, copy);
Expand All @@ -118,22 +108,10 @@ public VersionMetaData getArtifactVersionMetaData(Integer version, String artifa
return map.computeIfAbsent(version, v -> getDelegate().getArtifactVersionMetaData(version, artifactId));
}

@Override
public List<Long> listArtifactVersions(String artifactId) {
NavigableMap<Integer, ArtifactMetaData> map = amds.get(artifactId);
if (map != null) {
return map.keySet().stream().map(Long::new).collect(Collectors.toList());
} else {
return Collections.emptyList();
}
}

@Override
public CompletionStage<ArtifactMetaData> createArtifact(ArtifactType xRegistryArtifactType, String xRegistryArtifactId, InputStream data) {
CompletionStage<ArtifactMetaData> cs = getDelegate().createArtifact(xRegistryArtifactType, xRegistryArtifactId, data);
return cs.thenApply(amd -> {
NavigableMap<Integer, ArtifactMetaData> map = amds.computeIfAbsent(xRegistryArtifactId, id -> new TreeMap<>());
map.put(amd.getVersion(), amd);
globalAMD.put(amd.getGlobalId(), amd);
return amd;
});
Expand All @@ -143,8 +121,6 @@ public CompletionStage<ArtifactMetaData> createArtifact(ArtifactType xRegistryAr
public CompletionStage<ArtifactMetaData> updateArtifact(String artifactId, ArtifactType xRegistryArtifactType, InputStream data) {
CompletionStage<ArtifactMetaData> cs = getDelegate().updateArtifact(artifactId, xRegistryArtifactType, data);
return cs.thenApply(amd -> {
NavigableMap<Integer, ArtifactMetaData> map = amds.computeIfAbsent(artifactId, id -> new TreeMap<>());
map.put(amd.getVersion(), amd);
globalAMD.put(amd.getGlobalId(), amd);
return amd;
});
Expand Down Expand Up @@ -188,6 +164,11 @@ public void testUpdateArtifact(String artifactId, ArtifactType xRegistryArtifact
getDelegate().testUpdateArtifact(artifactId, xRegistryArtifactType, content);
}

@Override
public List<Long> listArtifactVersions(String artifactId) {
return getDelegate().listArtifactVersions(artifactId);
}

// ---- Auto reset

@Override
Expand Down
61 changes: 27 additions & 34 deletions tests/src/test/java/io/apicurio/tests/BaseIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,27 @@

package io.apicurio.tests;

import io.apicurio.registry.client.RegistryClient;
import io.apicurio.registry.client.RegistryService;
import io.apicurio.registry.rest.beans.ArtifactMetaData;
import io.apicurio.registry.rest.beans.EditableMetaData;
import io.apicurio.registry.types.ArtifactType;
import io.apicurio.registry.utils.ConcurrentUtil;
import io.apicurio.tests.interfaces.TestSeparator;
import io.apicurio.tests.utils.subUtils.TestUtils;
import io.confluent.kafka.schemaregistry.client.CachedSchemaRegistryClient;
import io.confluent.kafka.schemaregistry.client.SchemaRegistryClient;
import io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException;
import io.restassured.RestAssured;
import io.restassured.parsing.Parser;
import org.apache.avro.Schema;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.TestInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

Expand All @@ -36,28 +57,6 @@
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;

import org.apache.avro.Schema;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.TestInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.apicurio.registry.client.RegistryClient;
import io.apicurio.registry.client.RegistryService;
import io.apicurio.registry.rest.beans.ArtifactMetaData;
import io.apicurio.registry.rest.beans.EditableMetaData;
import io.apicurio.registry.types.ArtifactType;
import io.apicurio.registry.utils.ConcurrentUtil;
import io.apicurio.tests.interfaces.TestSeparator;
import io.apicurio.tests.utils.subUtils.TestUtils;
import io.confluent.kafka.schemaregistry.client.CachedSchemaRegistryClient;
import io.confluent.kafka.schemaregistry.client.SchemaRegistryClient;
import io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException;
import io.restassured.RestAssured;
import io.restassured.parsing.Parser;

public abstract class BaseIT implements TestSeparator, Constants {

protected static final Logger LOGGER = LoggerFactory.getLogger(BaseIT.class);
Expand All @@ -79,7 +78,7 @@ protected final String resourceToString(String resourceName) {

@BeforeAll
static void beforeAll() throws Exception {
if (!RegistryFacade.REGISTRY_URL.equals(RegistryFacade.DEFAULT_REGISTRY_URL) || RegistryFacade.EXTERNAL_REGISTRY.equals("")) {
if (!Boolean.parseBoolean(RegistryFacade.EXTERNAL_REGISTRY)) {
registry.start();
} else {
LOGGER.info("Going to use already running registries on {}:{}", RegistryFacade.REGISTRY_URL, RegistryFacade.REGISTRY_PORT);
Expand All @@ -97,9 +96,10 @@ static void beforeAll() throws Exception {

@AfterAll
static void afterAll(TestInfo info) throws Exception {
if (!RegistryFacade.EXTERNAL_REGISTRY.equals(Boolean.TRUE.toString())) {
if (!Boolean.parseBoolean(RegistryFacade.EXTERNAL_REGISTRY)) {
registry.stop();
Thread.sleep(3000);
//noinspection OptionalGetWithoutIsPresent
storeRegistryLog(info.getTestClass().get().getCanonicalName());
}
apicurioService.close();
Expand All @@ -112,6 +112,7 @@ private static void storeRegistryLog(String className) {
File logDir = new File("target/logs/" + className + "-" + currentDate);

if (!logDir.exists()) {
//noinspection ResultOfMethodCallIgnored
logDir.mkdirs();
}

Expand Down Expand Up @@ -141,8 +142,8 @@ protected Map<String, String> createMultipleArtifacts(int count) {
}

protected void deleteMultipleArtifacts(Map<String, String> idMap) {
for (Map.Entry entry : idMap.entrySet()) {
apicurioService.deleteArtifact(entry.getValue().toString());
for (Map.Entry<String, String> entry : idMap.entrySet()) {
apicurioService.deleteArtifact(entry.getValue());
LOGGER.info("Deleted artifact {} with ID: {}", entry.getKey(), entry.getValue());
}
}
Expand Down Expand Up @@ -205,14 +206,6 @@ public void createArtifactViaConfluentClient(Schema schema, String artifactName)
});
}

public void updateArtifactViaConfluentClient(Schema schema, String artifactName) throws IOException, RestClientException {
int idOfSchema = confluentService.register(artifactName, schema);
Schema newSchema = confluentService.getBySubjectAndId(artifactName, idOfSchema);
LOGGER.info("Checking that created schema is equal to the get schema");
assertThat(schema.toString(), is(newSchema.toString()));
assertThat(confluentService.getVersion(artifactName, schema), is(confluentService.getVersion(artifactName, newSchema)));
}

protected static void clearAllConfluentSubjects() throws IOException, RestClientException {
List<String> confluentSubjects = (List<String>) confluentService.getAllSubjects();
for (String confluentSubject : confluentSubjects) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,30 @@

package io.apicurio.tests.serdes.apicurio;

import static io.apicurio.tests.Constants.CLUSTER;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.apache.avro.Schema;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;

import com.google.protobuf.Descriptors;

import io.apicurio.registry.common.proto.Serde;
import io.apicurio.registry.rest.beans.ArtifactMetaData;
import io.apicurio.registry.types.ArtifactType;
import io.apicurio.registry.utils.IoUtil;
import io.apicurio.tests.BaseIT;
import io.apicurio.tests.Constants;
import io.apicurio.tests.serdes.KafkaClients;
import io.apicurio.tests.serdes.proto.MsgTypes;
import io.apicurio.tests.utils.subUtils.ArtifactUtils;
import io.apicurio.tests.utils.subUtils.TestUtils;
import org.apache.avro.Schema;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;

import static io.apicurio.tests.Constants.CLUSTER;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

@Tag(CLUSTER)
public class BasicApicurioSerDesIT extends BaseIT {
Expand Down Expand Up @@ -183,6 +183,13 @@ void testJsonSchemaApicurioSerDes(TestInfo testInfo) throws InterruptedException
ArtifactMetaData artifact = ArtifactUtils.createArtifact(apicurioService, ArtifactType.JSON, artifactId, IoUtil.toStream(jsonSchema));
LOGGER.debug("++++++++++++++++++ Artifact created: {}", artifact.getGlobalId());

TestUtils.waitFor(
"Artifact not registered",
Constants.POLL_INTERVAL,
Constants.TIMEOUT_GLOBAL,
() -> apicurioService.getArtifactMetaDataByGlobalId(artifact.getGlobalId()) != null
);

KafkaClients.produceJsonSchemaApicurioMessages(topicName, subjectName, 10).get(5, TimeUnit.SECONDS);
KafkaClients.consumeJsonSchemaApicurioMessages(topicName, 10).get(5, TimeUnit.SECONDS);
}
Expand Down
Loading