use of com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException in project streamline by hortonworks.
the class SchemaResource method postStreamsSchema.
@POST
@Timed
@Produces(MediaType.APPLICATION_JSON)
public Response postStreamsSchema(StreamsSchemaInfo streamsSchemaInfo, @Context SecurityContext securityContext) throws IOException {
Preconditions.checkNotNull(streamsSchemaInfo, "streamsSchemaInfo can not be null");
SchemaIdVersion schemaIdVersion = null;
SchemaMetadata schemaMetadata = streamsSchemaInfo.getSchemaMetadata();
String schemaName = schemaMetadata.getName();
Long schemaMetadataId = schemaRegistryClient.registerSchemaMetadata(schemaMetadata);
LOG.info("Registered schemaMetadataId [{}] for schema with name:[{}]", schemaMetadataId, schemaName);
String streamsSchemaText = streamsSchemaInfo.getSchemaVersion().getSchemaText();
try {
// convert streams schema to avro schema.
String avroSchemaText = AvroStreamlineSchemaConverter.convertStreamlineSchemaToAvroSchema(streamsSchemaText);
SchemaVersion avroSchemaVersion = new SchemaVersion(avroSchemaText, streamsSchemaInfo.getSchemaVersion().getDescription());
schemaIdVersion = schemaRegistryClient.addSchemaVersion(schemaName, avroSchemaVersion);
} catch (InvalidSchemaException e) {
String errMsg = String.format("Invalid schema received for schema with name [%s] : [%s]", schemaName, streamsSchemaText);
LOG.error(errMsg, e);
throw BadRequestException.message(errMsg, e);
} catch (SchemaNotFoundException e) {
String errMsg = String.format("Schema not found for topic: [%s]", schemaName);
LOG.error(errMsg, e);
throw EntityNotFoundException.byId(schemaName);
} catch (IncompatibleSchemaException e) {
String errMsg = String.format("Incompatible schema received for schema with name [%s] : [%s]", schemaName, streamsSchemaText);
LOG.error(errMsg, e);
throw BadRequestException.message(errMsg, e);
} catch (Exception e) {
throw new RuntimeException(e);
}
return WSUtils.respondEntity(schemaIdVersion, OK);
}
use of com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException in project registry by hortonworks.
the class AvroSchemaResolver method getIncludedSchemaVersions.
private List<SchemaVersionKey> getIncludedSchemaVersions(String schemaText) throws InvalidSchemaException {
JsonNode jsonNode = null;
try {
jsonNode = new ObjectMapper().readTree(schemaText);
} catch (IOException e) {
throw new InvalidSchemaException(e);
}
JsonNode includeSchemaNodes = jsonNode.get("includeSchemas");
List<SchemaVersionKey> includedSchemaVersions = new ArrayList<>();
if (includeSchemaNodes != null) {
if (!includeSchemaNodes.isArray()) {
throw new InvalidSchemaException("includeSchemas should be an array of strings");
}
for (JsonNode includeSchema : includeSchemaNodes) {
String name = includeSchema.get("name").asText();
JsonNode versionNode = includeSchema.get("version");
int version = versionNode != null ? versionNode.asInt() : SchemaVersionKey.LATEST_VERSION;
includedSchemaVersions.add(new SchemaVersionKey(name, version));
}
}
return includedSchemaVersions;
}
use of com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException 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.InvalidSchemaException 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