Skip to content

Commit

Permalink
ccompat api will now check for existing version with same content bef…
Browse files Browse the repository at this point in the history
…ore registering new schema. Fixes #551 (#596)
  • Loading branch information
EricWittmann authored Jun 3, 2020
1 parent 72fea9e commit f17c329
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.SortedSet;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.function.Function;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -106,6 +107,16 @@ public Schema getSchema(String subject, SchemaContent schema) throws ArtifactNot

@Override
public CompletionStage<Long> createSchema(String subject, String schema, String schemaType) throws ArtifactAlreadyExistsException, ArtifactNotFoundException, RegistryStorageException {
// Check to see if this content is already registered - return the global ID of that content
// if it exists. If not, then register the new content.
try {
ContentHandle content = ContentHandle.create(schema);
ArtifactMetaDataDto dto = storage.getArtifactMetaData(subject, content);
return CompletableFuture.completedFuture(dto.getGlobalId());
} catch (ArtifactNotFoundException nfe) {
// This is OK - when it happens just move on and create
}

// TODO Should this creation and updating of an artifact be a different operation?
// TODO method that returns a completion stage should not throw an exception
CompletionStage<ArtifactMetaDataDto> artifactMeta = createOrUpdateArtifact(subject, schema, ArtifactType.fromValue(schemaType));
Expand Down
56 changes: 51 additions & 5 deletions app/src/test/java/io/apicurio/registry/ConfluentCompatApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,53 @@ public void testCreateSubject() throws Exception {
.body("", equalTo(new JsonPath(SCHEMA_SIMPLE).getMap("")));
}

/**
* Endpoint: /subjects/(string: subject)/versions
*/
@Test
public void testCreateDuplicateContent() throws Exception {
final String SUBJECT = "testCreateDuplicateContent";
// POST content1
ValidatableResponse res = given()
.when()
.contentType(ContentTypes.COMPAT_SCHEMA_REGISTRY_STABLE_LATEST)
.body(SCHEMA_1_WRAPPED)
.post("/ccompat/subjects/{subject}/versions", SUBJECT)
.then()
.statusCode(200)
.body("id", Matchers.allOf(Matchers.isA(Integer.class), Matchers.greaterThanOrEqualTo(0)));
int id1 = res.extract().jsonPath().getInt("id");

this.waitForGlobalId(id1);

// POST content2
res = given()
.when()
.contentType(ContentTypes.COMPAT_SCHEMA_REGISTRY_STABLE_LATEST)
.body(SCHEMA_2_WRAPPED)
.post("/ccompat/subjects/{subject}/versions", SUBJECT)
.then()
.statusCode(200)
.body("id", Matchers.allOf(Matchers.isA(Integer.class), Matchers.greaterThanOrEqualTo(0)));
int id2 = res.extract().jsonPath().getInt("id");

this.waitForGlobalId(id2);

// POST content3 (duplicate of content1)
res = given()
.when()
.contentType(ContentTypes.COMPAT_SCHEMA_REGISTRY_STABLE_LATEST)
.body(SCHEMA_1_WRAPPED)
.post("/ccompat/subjects/{subject}/versions", SUBJECT)
.then()
.statusCode(200)
.body("id", Matchers.allOf(Matchers.isA(Integer.class), Matchers.greaterThanOrEqualTo(0)));
int id3 = res.extract().jsonPath().getInt("id");

// ID1 and ID3 should be the same because they are the same content within the same subject.
Assertions.assertEquals(id1, id3);
}

/**
* Endpoint: /compatibility/subjects/{subject}/versions/{version}
*/
Expand Down Expand Up @@ -297,7 +344,7 @@ public void testGetSchemaVersions() throws Exception {
final Integer globalId1 = given()
.when()
.contentType(ContentTypes.COMPAT_SCHEMA_REGISTRY_STABLE_LATEST)
.body(SCHEMA_SIMPLE_WRAPPED)
.body(SCHEMA_1_WRAPPED)
.post("/ccompat/subjects/{subject}/versions", SUBJECT)
.then()
.statusCode(200)
Expand All @@ -311,15 +358,14 @@ public void testGetSchemaVersions() throws Exception {
final Integer globalId2 = given()
.when()
.contentType(ContentTypes.COMPAT_SCHEMA_REGISTRY_STABLE_LATEST)
.body(SCHEMA_SIMPLE_WRAPPED)
.body(SCHEMA_2_WRAPPED)
.post("/ccompat/subjects/{subject}/versions", SUBJECT)
.then()
.statusCode(200)
.body("id", Matchers.allOf(Matchers.isA(Integer.class), Matchers.greaterThanOrEqualTo(1)))
.extract().body().jsonPath().get("id");
Assertions.assertNotNull(globalId2);


this.waitForGlobalId(globalId2);

//Verify
Expand All @@ -341,7 +387,7 @@ public void testGetSchemaReferencedVersions() throws Exception {
//Create two versions of the same artifact
// POST
final Integer globalId1 = given().when().contentType(ContentTypes.COMPAT_SCHEMA_REGISTRY_STABLE_LATEST)
.body(SCHEMA_SIMPLE_WRAPPED).post("/ccompat/subjects/{subject}/versions", SUBJECT).then()
.body(SCHEMA_1_WRAPPED).post("/ccompat/subjects/{subject}/versions", SUBJECT).then()
.statusCode(200)
.body("id", Matchers.allOf(Matchers.isA(Integer.class), Matchers.greaterThanOrEqualTo(0)))
.extract().body().jsonPath().get("id");
Expand All @@ -351,7 +397,7 @@ public void testGetSchemaReferencedVersions() throws Exception {

// POST
final Integer globalId2 = given().when().contentType(ContentTypes.COMPAT_SCHEMA_REGISTRY_STABLE_LATEST)
.body(SCHEMA_SIMPLE_WRAPPED).post("/ccompat/subjects/{subject}/versions", SUBJECT).then()
.body(SCHEMA_2_WRAPPED).post("/ccompat/subjects/{subject}/versions", SUBJECT).then()
.statusCode(200)
.body("id", Matchers.allOf(Matchers.isA(Integer.class), Matchers.greaterThanOrEqualTo(1)))
.extract().body().jsonPath().get("id");
Expand Down

0 comments on commit f17c329

Please sign in to comment.