use of com.hortonworks.registries.schemaregistry.errors.SchemaBranchAlreadyExistsException in project registry by hortonworks.
the class SchemaRegistryClient method createSchemaBranch.
@Override
public SchemaBranch createSchemaBranch(Long schemaVersionId, SchemaBranch schemaBranch) throws SchemaBranchAlreadyExistsException, SchemaNotFoundException {
WebTarget target = currentSchemaRegistryTargets().schemasTarget.path("versionsById/" + schemaVersionId + "/branch");
Response response = Subject.doAs(subject, new PrivilegedAction<Response>() {
@Override
public Response run() {
return target.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.json(schemaBranch), Response.class);
}
});
int status = response.getStatus();
if (status == Response.Status.OK.getStatusCode()) {
String msg = response.readEntity(String.class);
SchemaBranch returnedSchemaBranch = readEntity(msg, SchemaBranch.class);
return returnedSchemaBranch;
} else if (status == Response.Status.BAD_REQUEST.getStatusCode()) {
throw new SchemaNotFoundException(response.readEntity(String.class));
} else if (status == Response.Status.CONFLICT.getStatusCode()) {
throw new SchemaBranchAlreadyExistsException(response.readEntity(String.class));
} else {
throw new RuntimeException(response.readEntity(String.class));
}
}
use of com.hortonworks.registries.schemaregistry.errors.SchemaBranchAlreadyExistsException in project registry by hortonworks.
the class DefaultSchemaRegistry method createSchemaBranch.
@Override
public SchemaBranch createSchemaBranch(Long schemaVersionId, SchemaBranch schemaBranch) throws SchemaBranchAlreadyExistsException, SchemaNotFoundException {
Preconditions.checkNotNull(schemaBranch.getName(), "Schema branch name can't be null");
SchemaVersionInfo schemaVersionInfo = schemaVersionLifecycleManager.getSchemaVersionInfo(new SchemaIdVersion(schemaVersionId));
SchemaBranchKey schemaBranchKey = new SchemaBranchKey(schemaBranch.getName(), schemaVersionInfo.getName());
SchemaBranch existingSchemaBranch = null;
try {
existingSchemaBranch = schemaBranchCache.get(SchemaBranchCache.Key.of(schemaBranchKey));
} catch (SchemaBranchNotFoundException e) {
// Ignore this error
}
if (existingSchemaBranch != null)
throw new SchemaBranchAlreadyExistsException(String.format("A schema branch with name : '%s' already exists", schemaBranch.getName()));
SchemaBranchStorable schemaBranchStorable = SchemaBranchStorable.from(schemaBranch);
schemaBranchStorable.setSchemaMetadataName(schemaVersionInfo.getName());
schemaBranchStorable.setId(storageManager.nextId(SchemaBranchStorable.NAME_SPACE));
storageManager.add(schemaBranchStorable);
SchemaBranch persistedSchemaBranch;
try {
persistedSchemaBranch = schemaBranchCache.get(SchemaBranchCache.Key.of(schemaBranchKey));
} catch (SchemaBranchNotFoundException e) {
throw new RuntimeException(String.format("Failed to fetch persisted schema branch : '%s' from the database", schemaBranch.getName()));
}
SchemaBranchVersionMapping schemaBranchVersionMapping = new SchemaBranchVersionMapping(persistedSchemaBranch.getId(), schemaVersionInfo.getId());
storageManager.add(schemaBranchVersionMapping);
return persistedSchemaBranch;
}
use of com.hortonworks.registries.schemaregistry.errors.SchemaBranchAlreadyExistsException in project registry by hortonworks.
the class SchemaBranchLifeCycleTest method addSchemaVersionToBranch.
@Test
public void addSchemaVersionToBranch() throws IOException, SchemaBranchNotFoundException, InvalidSchemaException, SchemaNotFoundException, IncompatibleSchemaException, SchemaBranchAlreadyExistsException {
SchemaMetadata schemaMetadata = addSchemaMetadata(testNameRule.getMethodName(), SchemaCompatibility.NONE);
SchemaIdVersion masterSchemaIdVersion1 = addSchemaVersion(SchemaBranch.MASTER_BRANCH, schemaMetadata, "/device.avsc");
SchemaBranch schemaBranch1 = addSchemaBranch("BRANCH1", schemaMetadata, masterSchemaIdVersion1.getSchemaVersionId());
addSchemaVersion(schemaBranch1.getName(), schemaMetadata, "/device-incompat.avsc");
addSchemaVersion(schemaBranch1.getName(), schemaMetadata, "/device-compat.avsc");
Collection<SchemaVersionInfo> schemaBranch1VersionInfos = schemaRegistryClient.getAllVersions(schemaBranch1.getName(), schemaMetadata.getName());
Collection<SchemaVersionInfo> masterSchemaVersionInfos = schemaRegistryClient.getAllVersions(schemaMetadata.getName());
Assert.assertTrue(masterSchemaVersionInfos.size() == 1);
Assert.assertTrue(schemaBranch1VersionInfos.size() == 3);
Long versionsInInitiatedState = schemaBranch1VersionInfos.stream().filter(schemaVersionInfo -> schemaVersionInfo.getStateId().equals(SchemaVersionLifecycleStates.INITIATED.getId())).count();
Long versionsInEnabledState = schemaBranch1VersionInfos.stream().filter(schemaVersionInfo -> schemaVersionInfo.getStateId().equals(SchemaVersionLifecycleStates.ENABLED.getId())).count();
Assert.assertTrue(versionsInInitiatedState == 2);
Assert.assertTrue(versionsInEnabledState == 1);
}
use of com.hortonworks.registries.schemaregistry.errors.SchemaBranchAlreadyExistsException in project registry by hortonworks.
the class SchemaBranchLifeCycleTest method getAllBranches.
@Test
public void getAllBranches() throws IOException, SchemaBranchNotFoundException, InvalidSchemaException, SchemaNotFoundException, IncompatibleSchemaException, SchemaBranchAlreadyExistsException {
SchemaMetadata schemaMetadata = addSchemaMetadata(testNameRule.getMethodName(), SchemaCompatibility.NONE);
SchemaIdVersion masterSchemaIdVersion1 = addSchemaVersion(SchemaBranch.MASTER_BRANCH, schemaMetadata, "/device.avsc");
SchemaBranch schemaBranch1 = addSchemaBranch("BRANCH1", schemaMetadata, masterSchemaIdVersion1.getSchemaVersionId());
SchemaIdVersion masterSchemaIdVersion2 = addSchemaVersion(SchemaBranch.MASTER_BRANCH, schemaMetadata, "/device-incompat.avsc");
SchemaBranch schemaBranch2 = addSchemaBranch("BRANCH2", schemaMetadata, masterSchemaIdVersion2.getSchemaVersionId());
Set<String> actualSchemaBranches = schemaRegistryClient.getSchemaBranches(schemaMetadata.getName()).stream().map(branch -> branch.getName()).collect(Collectors.toSet());
Set<String> expectedSchemaBranches = new HashSet<>(Lists.newArrayList("MASTER", schemaBranch1.getName(), schemaBranch2.getName()));
Assert.assertTrue(SetUtils.isEqualSet(actualSchemaBranches, expectedSchemaBranches));
}
use of com.hortonworks.registries.schemaregistry.errors.SchemaBranchAlreadyExistsException in project registry by hortonworks.
the class SchemaBranchLifeCycleTest method mergeSchemaWithDefaultMergeStrategy.
@Test
public void mergeSchemaWithDefaultMergeStrategy() throws IOException, SchemaBranchNotFoundException, InvalidSchemaException, SchemaNotFoundException, IncompatibleSchemaException, SchemaBranchAlreadyExistsException {
SchemaMetadata schemaMetadata = addSchemaMetadata(testNameRule.toString(), SchemaCompatibility.NONE);
SchemaIdVersion masterSchemaIdVersion1 = addSchemaVersion(SchemaBranch.MASTER_BRANCH, schemaMetadata, "/device.avsc");
SchemaBranch schemaBranch1 = addSchemaBranch("BRANCH1", schemaMetadata, masterSchemaIdVersion1.getSchemaVersionId());
SchemaIdVersion schemaBranch1Version1 = addSchemaVersion(schemaBranch1.getName(), schemaMetadata, "/device-incompat.avsc");
SchemaIdVersion schemaBranch1Version2 = addSchemaVersion(schemaBranch1.getName(), schemaMetadata, "/device-compat.avsc");
schemaRegistryClient.mergeSchemaVersion(schemaBranch1Version2.getSchemaVersionId());
Collection<SchemaVersionInfo> branchSchemaVersionInfos = schemaRegistryClient.getAllVersions(schemaBranch1.getName(), schemaMetadata.getName());
Collection<SchemaVersionInfo> masterSchemaVersionInfos = schemaRegistryClient.getAllVersions(schemaMetadata.getName());
Assert.assertTrue(masterSchemaVersionInfos.size() == 2);
Assert.assertTrue(branchSchemaVersionInfos.size() == 3);
Long branchVersionsInInitiatedState = branchSchemaVersionInfos.stream().filter(schemaVersionInfo -> schemaVersionInfo.getStateId().equals(SchemaVersionLifecycleStates.INITIATED.getId())).count();
Long masterVersionsInEnabledState = masterSchemaVersionInfos.stream().filter(schemaVersionInfo -> schemaVersionInfo.getStateId().equals(SchemaVersionLifecycleStates.ENABLED.getId())).count();
Assert.assertTrue(branchVersionsInInitiatedState == 2);
Assert.assertTrue(masterVersionsInEnabledState == 2);
}
Aggregations