com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException Java Examples

The following examples show how to use com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CustomStateTransitionTest.java    From registry with Apache License 2.0 6 votes vote down vote up
@Test
public void testTransitionToRejectedState() throws IOException, SchemaNotFoundException, InvalidSchemaException, IncompatibleSchemaException, SchemaBranchAlreadyExistsException, SchemaLifecycleException {
    SchemaMetadata schemaMetadata = createSchemaMetadata("test", SchemaCompatibility.NONE);
    SchemaBranch branch1 = new SchemaBranch("BRANCH1", schemaMetadata.getName());

    String schema1 = getSchema("/device.avsc");
    schemaRegistryClient.addSchemaMetadata(schemaMetadata);
    SchemaIdVersion schemaIdVersion1 = schemaRegistryClient.addSchemaVersion(schemaMetadata, new SchemaVersion(schema1, "initial version"));

    schemaRegistryClient.createSchemaBranch(schemaIdVersion1.getSchemaVersionId(), branch1);

    String schema2 = AvroSchemaRegistryClientUtil.getSchema("/device1.avsc");
    SchemaIdVersion schemaIdVersion2 = schemaRegistryClient.addSchemaVersion(branch1.getName(), schemaMetadata, new SchemaVersion(schema2, "modify version"));

    schemaRegistryClient.startSchemaVersionReview(schemaIdVersion2.getSchemaVersionId());
    SchemaVersionInfo schemaVersionInfoAtReviewState = schemaRegistryClient.getSchemaVersionInfo(new SchemaIdVersion(schemaIdVersion2.getSchemaVersionId()));
    Assert.assertTrue(schemaVersionInfoAtReviewState.getStateId().equals(CustomReviewCycleStates.PEER_REVIEW_STATE.getId()));

    schemaRegistryClient.transitionState(schemaIdVersion2.getSchemaVersionId(), CustomReviewCycleStates.REJECTED_REVIEW_STATE.getId(), "Rejected by X".getBytes());
    SchemaVersionInfo schemaVersionInfoAtRejectedState = schemaRegistryClient.getSchemaVersionInfo(new SchemaIdVersion(schemaIdVersion2.getSchemaVersionId()));
    Assert.assertTrue(schemaVersionInfoAtRejectedState.getStateId().equals(CustomReviewCycleStates.REJECTED_REVIEW_STATE.getId()));

}
 
Example #2
Source File: SchemaVersionLifecycleManager.java    From registry with Apache License 2.0 6 votes vote down vote up
public SchemaIdVersion addSchemaVersion(String schemaBranchName,
                                        String schemaName,
                                        SchemaVersion schemaVersion,
                                        boolean disableCanonicalCheck)
        throws SchemaNotFoundException, IncompatibleSchemaException, InvalidSchemaException, SchemaBranchNotFoundException {

    Preconditions.checkNotNull(schemaBranchName, "Schema branch name can't be null");
    Preconditions.checkNotNull(schemaName, "schemaName can't be null");
    Preconditions.checkNotNull(schemaVersion, "schemaVersion can't be null");

    checkSchemaText(schemaVersion.getSchemaText());

    // check whether there exists schema-metadata for schema-metadata-key
    SchemaMetadataInfo schemaMetadataInfo = getSchemaMetadataInfo(schemaName);
    if (schemaMetadataInfo != null) {
        return addSchemaVersion(schemaBranchName, schemaMetadataInfo, schemaVersion, disableCanonicalCheck);
    } else {
        throw new SchemaNotFoundException("SchemaMetadata not found with the schemaName: " + schemaName);
    }
}
 
Example #3
Source File: AvroSchemaResolver.java    From registry with Apache License 2.0 6 votes vote down vote up
private Map<String, Schema> traverseIncludedSchemaTypes(String schemaText,
                                                        Map<String, SchemaParsingState> schemaParsingStates)
        throws InvalidSchemaException, SchemaNotFoundException {
    List<SchemaVersionKey> includedSchemaVersions = getIncludedSchemaVersions(schemaText);

    if (includedSchemaVersions == null || includedSchemaVersions.isEmpty()) {
        return Collections.emptyMap();
    }
    Map<String, Schema> schemaTypes = new HashMap<>();
    for (SchemaVersionKey schemaVersionKey : includedSchemaVersions) {
        Map<String, Schema> collectedSchemas = collectSchemaTypes(schemaVersionKey, schemaParsingStates);
        if (collectedSchemas != null) {
            schemaTypes.putAll(collectedSchemas);
        }
    }

    return schemaTypes;
}
 
Example #4
Source File: AvroSchemaResolver.java    From registry with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #5
Source File: AbstractSnapshotSerializer.java    From registry with Apache License 2.0 6 votes vote down vote up
@Override
public final O serialize(I input, SchemaMetadata schemaMetadata) throws SerDesException {
    ensureInitialized();

    // compute schema based on input object
    String schema = getSchemaText(input);

    // register that schema and get the version
    try {
        SchemaIdVersion schemaIdVersion = schemaRegistryClient.addSchemaVersion(schemaMetadata, new SchemaVersion(schema, "Schema registered by serializer:" + this.getClass()));
        // write the version and given object to the output
        return doSerialize(input, schemaIdVersion);
    } catch (SchemaNotFoundException | IncompatibleSchemaException | InvalidSchemaException | SchemaBranchNotFoundException e) {
        throw new RegistryException(e);
    }
}
 
Example #6
Source File: SchemaBranchLifeCycleTest.java    From registry with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #7
Source File: AbstractSnapshotDeserializer.java    From registry with Apache License 2.0 6 votes vote down vote up
@Override
protected void doInit(Map<String, ?> config) {
    schemaCache = CacheBuilder.newBuilder()
            .maximumSize(getCacheMaxSize(config))
            .expireAfterAccess(getCacheExpiryInSecs(config), TimeUnit.SECONDS)
            .build(new CacheLoader<SchemaVersionKey, S>() {
                @Override
                public S load(SchemaVersionKey schemaVersionKey) {
                    try {
                        return getParsedSchema(schemaVersionKey);
                    } catch (SchemaNotFoundException | InvalidSchemaException e) {
                       throw new RegistryException(e);
                    }
                }
            });
}
 
Example #8
Source File: SchemaVersionLifecycleManager.java    From registry with Apache License 2.0 6 votes vote down vote up
public SchemaIdVersion addSchemaVersion(String schemaBranchName,
                                        SchemaMetadataInfo schemaMetadataInfo,
                                        SchemaVersion schemaVersion,
                                        boolean disableCanonicalCheck)
        throws SchemaNotFoundException, IncompatibleSchemaException, InvalidSchemaException, SchemaBranchNotFoundException {

    Preconditions.checkNotNull(schemaBranchName, "Schema branch name can't be null");
    checkSchemaText(schemaVersion.getSchemaText());

    SchemaVersionInfo schemaVersionInfo;
    // check whether there exists schema-metadata for schema-metadata-key
    SchemaMetadata schemaMetadata = schemaMetadataInfo.getSchemaMetadata();
    // check whether the same schema text exists
    schemaVersionInfo = findSchemaVersion(schemaBranchName, schemaMetadata.getType(), schemaVersion.getSchemaText(), schemaMetadataInfo
            .getSchemaMetadata().getName(), disableCanonicalCheck);
    if (schemaVersionInfo == null) {
        schemaVersionInfo = createSchemaVersion(schemaBranchName,
                                                schemaMetadata,
                                                schemaMetadataInfo.getId(),
                                                schemaVersion);
    }

    return new SchemaIdVersion(schemaMetadataInfo.getId(), schemaVersionInfo.getVersion(), schemaVersionInfo.getId());
}
 
Example #9
Source File: SchemaRegistryClient.java    From registry with Apache License 2.0 6 votes vote down vote up
private SchemaIdVersion handleSchemaIdVersionResponse(SchemaMetadataInfo schemaMetadataInfo,
                                                      Response response) throws IncompatibleSchemaException, InvalidSchemaException {
    int status = response.getStatus();
    String msg = response.readEntity(String.class);
    if (status == Response.Status.BAD_REQUEST.getStatusCode() || status == Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()) {
        CatalogResponse catalogResponse = readCatalogResponse(msg);
        if (CatalogResponse.ResponseMessage.INCOMPATIBLE_SCHEMA.getCode() == catalogResponse.getResponseCode()) {
            throw new IncompatibleSchemaException(catalogResponse.getResponseMessage());
        } else if (CatalogResponse.ResponseMessage.INVALID_SCHEMA.getCode() == catalogResponse.getResponseCode()) {
            throw new InvalidSchemaException(catalogResponse.getResponseMessage());
        } else {
            throw new RuntimeException(catalogResponse.getResponseMessage());
        }

    }

    Integer version = readEntity(msg, Integer.class);

    SchemaVersionInfo schemaVersionInfo = doGetSchemaVersionInfo(new SchemaVersionKey(schemaMetadataInfo.getSchemaMetadata()
                                                                                                        .getName(), version));

    return new SchemaIdVersion(schemaMetadataInfo.getId(), version, schemaVersionInfo.getId());
}
 
Example #10
Source File: SchemaRegistryClient.java    From registry with Apache License 2.0 6 votes vote down vote up
private SchemaIdVersion doAddSchemaVersion(String schemaBranchName, String schemaName,
                                           SchemaVersion schemaVersion, boolean disableCanonicalCheck) throws IncompatibleSchemaException, InvalidSchemaException, SchemaNotFoundException {
    SchemaMetadataInfo schemaMetadataInfo = getSchemaMetadataInfo(schemaName);
    if (schemaMetadataInfo == null) {
        throw new SchemaNotFoundException("Schema with name " + schemaName + " not found");
    }


    Response response = runRetryableBlock((SchemaRegistryTargets targets) -> {
        try {
            WebTarget target = targets.schemasTarget.path(schemaName).path("/versions").queryParam("branch", schemaBranchName)
                    .queryParam("disableCanonicalCheck", disableCanonicalCheck);
            return login.doAction(new PrivilegedAction<Response>() {
                @Override
                public Response run() {
                    return target.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.json(schemaVersion), Response.class);
                }
            });
        } catch (LoginException | ProcessingException e) {
            throw new RegistryRetryableException(e);
        }
    });
    return handleSchemaIdVersionResponse(schemaMetadataInfo, response);
}
 
Example #11
Source File: SchemaRegistryClient.java    From registry with Apache License 2.0 6 votes vote down vote up
@Override
public SchemaIdVersion addSchemaVersion(final String schemaBranchName, final String schemaName, final SchemaVersion schemaVersion, boolean disableCanonicalCheck)
        throws InvalidSchemaException, IncompatibleSchemaException, SchemaNotFoundException, SchemaBranchNotFoundException {

    try {
        return schemaTextCache.get(buildSchemaTextEntry(schemaVersion, schemaName),
                () -> doAddSchemaVersion(schemaBranchName, schemaName, schemaVersion, disableCanonicalCheck));
    } catch (ExecutionException e) {
        Throwable cause = e.getCause();
        LOG.error("Encountered error while adding new version [{}] of schema [{}] and error [{}]", schemaVersion, schemaName, e);
        if (cause != null) {
            if (cause instanceof InvalidSchemaException)
                throw (InvalidSchemaException) cause;
            else if (cause instanceof IncompatibleSchemaException) {
                throw (IncompatibleSchemaException) cause;
            } else if (cause instanceof SchemaNotFoundException) {
                throw (SchemaNotFoundException) cause;
            } else {
                throw new RuntimeException(cause.getMessage(), cause);
            }
        } else {
            throw new RuntimeException(e.getMessage(), e);
        }
    }
}
 
Example #12
Source File: AvroSchemaRegistryClientTest.java    From registry with Apache License 2.0 6 votes vote down vote up
private void _testAvroSerDesGenericObj(Byte protocolId) throws IOException, InvalidSchemaException, IncompatibleSchemaException, SchemaNotFoundException, SchemaBranchNotFoundException {
    Map<String, Object> config = Maps.newHashMap();
    config.putAll(SCHEMA_REGISTRY_CLIENT_CONF);
    config.put(SERDES_PROTOCOL_VERSION, protocolId);

    AvroSnapshotSerializer avroSnapshotSerializer = new AvroSnapshotSerializer();
    avroSnapshotSerializer.init(config);
    AvroSnapshotDeserializer avroSnapshotDeserializer = new AvroSnapshotDeserializer();
    avroSnapshotDeserializer.init(config);

    String deviceSchema = AvroSchemaRegistryClientUtil.getSchema("/device.avsc");
    SchemaMetadata schemaMetadata = createSchemaMetadata(TEST_NAME_RULE.getMethodName(), SchemaCompatibility.BOTH);
    SchemaIdVersion v1 = SCHEMA_REGISTRY_CLIENT.addSchemaVersion(schemaMetadata, new SchemaVersion(deviceSchema, "Initial version of the schema"));
    Assert.assertNotNull(v1);

    Object deviceObject = AvroSchemaRegistryClientUtil.createGenericRecordForDevice();

    byte[] serializedData = avroSnapshotSerializer.serialize(deviceObject, schemaMetadata);
    Object deserializedObj = avroSnapshotDeserializer.deserialize(new ByteArrayInputStream(serializedData), null);
    Assert.assertEquals(deviceObject, deserializedObj);
}
 
Example #13
Source File: SchemaRegistryClient.java    From registry with Apache License 2.0 6 votes vote down vote up
@Override
public SchemaIdVersion addSchemaVersion(String schemaBranchName, SchemaMetadata schemaMetadata, SchemaVersion schemaVersion, boolean disableCanonicalCheck) throws
        InvalidSchemaException, IncompatibleSchemaException, SchemaNotFoundException, SchemaBranchNotFoundException {
    // get it, if it exists in cache
    SchemaDigestEntry schemaDigestEntry = buildSchemaTextEntry(schemaVersion, schemaMetadata.getName());
    SchemaIdVersion schemaIdVersion = schemaTextCache.getIfPresent(schemaDigestEntry);

    if (schemaIdVersion == null) {
        //register schema metadata if it does not exist
        Long metadataId = registerSchemaMetadata(schemaMetadata);
        if (metadataId == null) {
            LOG.error("Schema Metadata [{}] is not registered successfully", schemaMetadata);
            throw new RuntimeException("Given SchemaMetadata could not be registered: " + schemaMetadata);
        }

        // add schemaIdVersion
        schemaIdVersion = addSchemaVersion(schemaBranchName, schemaMetadata.getName(), schemaVersion, disableCanonicalCheck);
    }

    return schemaIdVersion;
}
 
Example #14
Source File: DefaultSchemaRegistry.java    From registry with Apache License 2.0 5 votes vote down vote up
@Override
public SchemaIdVersion addSchemaVersion(SchemaMetadata schemaMetadata,
                                        SchemaVersion schemaVersion,
                                        boolean disableCanonicalCheck)
        throws IncompatibleSchemaException, InvalidSchemaException, SchemaNotFoundException, SchemaBranchNotFoundException {
    lockSchemaMetadata(schemaMetadata.getName());
    return schemaVersionLifecycleManager.addSchemaVersion(SchemaBranch.MASTER_BRANCH, schemaMetadata, schemaVersion, x -> registerSchemaMetadata(x), disableCanonicalCheck);
}
 
Example #15
Source File: StreamsSchemaProvider.java    From registry with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] getFingerprint(String schemaText) throws InvalidSchemaException {
    try {
        return MessageDigest.getInstance(MD5).digest(schemaText.getBytes(UTF_8));
    } catch (Exception e) {
        throw new InvalidSchemaException(e);
    }
}
 
Example #16
Source File: SchemaBranchLifeCycleTest.java    From registry with Apache License 2.0 5 votes vote down vote up
@Test (expected = InvalidSchemaBranchDeletionException.class)
public void deleteBranchWithRootVersionOfAnotherBranch() throws InvalidSchemaException, SchemaNotFoundException, IncompatibleSchemaException, IOException, SchemaBranchAlreadyExistsException, SchemaLifecycleException, InvalidSchemaBranchDeletionException {
    SchemaMetadata schemaMetadata = addSchemaMetadata(testNameRule.getMethodName(), 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");
    addSchemaBranch("CHILD-BRANCH", schemaMetadata,schemaBranch1Version1.getSchemaVersionId());

    schemaRegistryClient.enableSchemaVersion(schemaBranch1Version1.getSchemaVersionId());
    schemaRegistryClient.archiveSchemaVersion(schemaBranch1Version1.getSchemaVersionId());

    schemaRegistryClient.deleteSchemaBranch(schemaBranch1.getId());
}
 
Example #17
Source File: AvroSchemaRegistryTest.java    From registry with Apache License 2.0 5 votes vote down vote up
@Test(expected = InvalidSchemaException.class)
public void testInvalidSchema() throws Exception {
    String schema = "--- random invalid schema ---" + new Date();

    SchemaMetadata schemaMetadataInfo = createSchemaInfo(TEST_NAME_RULE.getMethodName(), SchemaCompatibility.BACKWARD);

    // registering a new schema
    Integer v1 = schemaRegistry.addSchemaVersion(schemaMetadataInfo,
                                                 new SchemaVersion(schema, "Initial version of the schema"))
                               .getVersion();
}
 
Example #18
Source File: SchemaBranchLifeCycleTest.java    From registry with Apache License 2.0 5 votes vote down vote up
@Test (expected = InvalidSchemaBranchDeletionException.class)
public void deleteBranchWithEnabledSchema() throws IOException, SchemaBranchNotFoundException, InvalidSchemaException, SchemaNotFoundException, IncompatibleSchemaException, SchemaBranchAlreadyExistsException, InvalidSchemaBranchDeletionException, SchemaLifecycleException {
    SchemaMetadata schemaMetadata = addSchemaMetadata(testNameRule.getMethodName(), 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");

    schemaRegistryClient.enableSchemaVersion(schemaBranch1Version1.getSchemaVersionId());

    schemaRegistryClient.deleteSchemaBranch(schemaBranch1.getId());
}
 
Example #19
Source File: SchemaBranchLifeCycleTest.java    From registry with Apache License 2.0 5 votes vote down vote up
@Test (expected = SchemaBranchNotFoundException.class)
public void deleteInvalidSchemaBranch() throws IOException, SchemaBranchNotFoundException, InvalidSchemaException, SchemaNotFoundException, IncompatibleSchemaException, SchemaBranchAlreadyExistsException, InvalidSchemaBranchDeletionException {
    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");

    schemaRegistryClient.deleteSchemaBranch(schemaBranch1.getId() + 1);
}
 
Example #20
Source File: SchemaVersionLifecycleManager.java    From registry with Apache License 2.0 5 votes vote down vote up
public SchemaVersionInfo getSchemaVersionInfo(String schemaName,
                                              String schemaText,
                                              boolean disableCanonicalCheck) throws SchemaNotFoundException, InvalidSchemaException, SchemaBranchNotFoundException {
    SchemaMetadataInfo schemaMetadataInfo = getSchemaMetadataInfo(schemaName);
    if (schemaMetadataInfo == null) {
        throw new SchemaNotFoundException("No schema found for schema metadata key: " + schemaName);
    }

    return findSchemaVersion(SchemaBranch.MASTER_BRANCH,
                             schemaMetadataInfo.getSchemaMetadata().getType(),
                             schemaText,
                             schemaName,
                             disableCanonicalCheck);
}
 
Example #21
Source File: DefaultSchemaRegistry.java    From registry with Apache License 2.0 5 votes vote down vote up
@Override
public SchemaIdVersion addSchemaVersion(String schemaBranchName,
                                        SchemaMetadata schemaMetadata,
                                        SchemaVersion schemaVersion,
                                        boolean disableCanonicalCheck)
        throws IncompatibleSchemaException, InvalidSchemaException, SchemaNotFoundException, SchemaBranchNotFoundException {
    lockSchemaMetadata(schemaMetadata.getName());
    return schemaVersionLifecycleManager.addSchemaVersion(schemaBranchName, schemaMetadata, schemaVersion, x -> registerSchemaMetadata(x), disableCanonicalCheck);
}
 
Example #22
Source File: SchemaBranchLifeCycleTest.java    From registry with Apache License 2.0 5 votes vote down vote up
@Test (expected = SchemaNotFoundException.class)
public void mergeSchemaWithInvalidSchemaVersion() 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 schemaBranch1Version1 = addSchemaVersion(schemaBranch1.getName(), schemaMetadata, "/device-incompat.avsc");

    schemaRegistryClient.mergeSchemaVersion(schemaBranch1Version1.getSchemaVersionId()+1);
}
 
Example #23
Source File: SchemaBranchLifeCycleTest.java    From registry with Apache License 2.0 5 votes vote down vote up
@Test
public void mergeSchemaWhenRootVersionIsNotLatest() 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 schemaBranch1Version1 = addSchemaVersion(schemaBranch1.getName(), schemaMetadata, "/device-incompat.avsc");
    addSchemaVersion(SchemaBranch.MASTER_BRANCH, schemaMetadata, "/device-compat.avsc");

    schemaRegistryClient.mergeSchemaVersion(schemaBranch1Version1.getSchemaVersionId());

}
 
Example #24
Source File: AvroSchemaResolver.java    From registry with Apache License 2.0 5 votes vote down vote up
private String getResultantSchema(String schemaText, Map<String, SchemaParsingState> schemaParsingStates)
        throws InvalidSchemaException, SchemaNotFoundException {
    Map<String, Schema> complexTypes = traverseIncludedSchemaTypes(schemaText, schemaParsingStates);

    Schema.Parser parser = new Schema.Parser();
    parser.addTypes(complexTypes);
    Schema schema = parser.parse(schemaText);
    Set<String> visitingTypes = new HashSet<>();
    Schema updatedSchema = handleUnionFieldsWithNull(schema, visitingTypes);

    return (schema == updatedSchema && complexTypes.isEmpty()) ? schemaText : updatedSchema.toString();
}
 
Example #25
Source File: AvroSchemaResolver.java    From registry with Apache License 2.0 5 votes vote down vote up
private Map<String, Schema> collectSchemaTypes(SchemaVersionKey schemaVersionKey,
                                               Map<String, SchemaParsingState> schemaParsingStates)
        throws SchemaNotFoundException, InvalidSchemaException {

    String schemaName = schemaVersionKey.getSchemaName();
    SchemaParsingState schemaParsingState = schemaParsingStates.putIfAbsent(schemaName, SchemaParsingState.PARSING);

    // if it is already parsed then the respective schema types would have been already collected.
    if (SchemaParsingState.PARSED == schemaParsingState) {
        return null;
    }

    // if it is in parsing state earlier and it is visted again then ther eis circular dependency!!
    if (SchemaParsingState.PARSING == schemaParsingState) {
        throw new CyclicSchemaDependencyException("Cyclic dependency of schema imports with schema [" + schemaName + "]");
    }

    // this schema is not yet parsed till now
    if (schemaParsingState == null) {
        Schema.Parser parser = new Schema.Parser();
        Schema schema = parser.parse(getResultantSchema(schemaVersionKey, schemaParsingStates));
        Map<String, Schema> complexTypes = new HashMap<>();
        collectComplexTypes(schema, complexTypes);
        schemaParsingStates.put(schemaName, SchemaParsingState.PARSED);
        return complexTypes;
    }

    throw new IllegalStateException("Schema parsing with schema version " + schemaVersionKey + " is in invalid state!!");
}
 
Example #26
Source File: SchemaBranchLifeCycleTest.java    From registry with Apache License 2.0 5 votes vote down vote up
@Test (expected = InvalidSchemaException.class)
public void addInvalidSchemaToBranch() throws IOException, SchemaBranchNotFoundException, InvalidSchemaException, SchemaNotFoundException, IncompatibleSchemaException, SchemaBranchAlreadyExistsException {
    SchemaMetadata schemaMetadata = addSchemaMetadata(testNameRule.getMethodName(), SchemaCompatibility.BACKWARD);

    SchemaIdVersion masterSchemaIdVersion = addSchemaVersion(SchemaBranch.MASTER_BRANCH, schemaMetadata, "/device.avsc");
    SchemaBranch schemaBranch1 = addSchemaBranch("BRANCH1", schemaMetadata, masterSchemaIdVersion.getSchemaVersionId());
    String schema2 = "--- invalid schema ---";
    schemaRegistryClient.addSchemaVersion(schemaBranch1.getName(), schemaMetadata.getName(), new SchemaVersion(schema2, "second version"));

}
 
Example #27
Source File: SchemaBranchLifeCycleTest.java    From registry with Apache License 2.0 5 votes vote down vote up
@Test (expected = IncompatibleSchemaException.class)
public void addIncompatibleSchemaToBranch() throws IOException, SchemaBranchNotFoundException, InvalidSchemaException, SchemaNotFoundException, IncompatibleSchemaException, SchemaBranchAlreadyExistsException {
    SchemaMetadata schemaMetadata = addSchemaMetadata(testNameRule.getMethodName(), SchemaCompatibility.BACKWARD);

    SchemaIdVersion masterSchemaIdVersion = addSchemaVersion(SchemaBranch.MASTER_BRANCH, schemaMetadata, "/device.avsc");
    SchemaBranch schemaBranch1 = addSchemaBranch("BRANCH1", schemaMetadata, masterSchemaIdVersion.getSchemaVersionId());
    addSchemaVersion(schemaBranch1.getName(), schemaMetadata, "/device-incompat.avsc");
}
 
Example #28
Source File: SchemaBranchLifeCycleTest.java    From registry with Apache License 2.0 5 votes vote down vote up
@Test (expected = SchemaBranchAlreadyExistsException.class)
public void createAlreadyExistingBranch() 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");
    addSchemaBranch(schemaBranch1.getName(), schemaMetadata, masterSchemaIdVersion2.getSchemaVersionId());
}
 
Example #29
Source File: SchemaBranchLifeCycleTest.java    From registry with Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example #30
Source File: AvroSchemaRegistryClientTest.java    From registry with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetSchemaVersionFromStates() throws IOException, SchemaNotFoundException, InvalidSchemaException, IncompatibleSchemaException, SchemaLifecycleException, SchemaBranchAlreadyExistsException {
    SchemaMetadata schemaMetadata = createSchemaMetadata(TEST_NAME_RULE.getMethodName(), SchemaCompatibility.NONE);
    String schemaName = schemaMetadata.getName();

    Long id = SCHEMA_REGISTRY_CLIENT.registerSchemaMetadata(schemaMetadata);
    SchemaIdVersion v1 = SCHEMA_REGISTRY_CLIENT.addSchemaVersion(schemaName, new SchemaVersion(AvroSchemaRegistryClientUtil.getSchema("/schema-1.avsc"), "Initial version of the schema"));
    SchemaIdVersion v2 = SCHEMA_REGISTRY_CLIENT.addSchemaVersion(schemaName, new SchemaVersion(AvroSchemaRegistryClientUtil.getSchema("/schema-2.avsc"), "Second version of the schema"));
    SchemaIdVersion v3 = SCHEMA_REGISTRY_CLIENT.addSchemaVersion(schemaName, new SchemaVersion(AvroSchemaRegistryClientUtil.getSchema("/schema-3.avsc"), "Third version of the schema, removes name field"));

    SchemaBranch schemaBranch = SCHEMA_REGISTRY_CLIENT.createSchemaBranch(v3.getSchemaVersionId(), new SchemaBranch("Branch-1", schemaName));
    SchemaIdVersion v4 = SCHEMA_REGISTRY_CLIENT.addSchemaVersion(schemaBranch.getName(), schemaName, new SchemaVersion(AvroSchemaRegistryClientUtil.getSchema("/schema-4.avsc"), "Forth version of the schema, adds back name field, but different type"));
    SCHEMA_REGISTRY_CLIENT.startSchemaVersionReview(v4.getSchemaVersionId());
    SCHEMA_REGISTRY_CLIENT.transitionState(v4.getSchemaVersionId(), SchemaVersionLifecycleStates.REVIEWED.getId(), null);

    SCHEMA_REGISTRY_CLIENT.archiveSchemaVersion(v2.getSchemaVersionId());
    SCHEMA_REGISTRY_CLIENT.archiveSchemaVersion(v3.getSchemaVersionId());

    Assert.assertEquals(
            transformToSchemaIdVersions(SCHEMA_REGISTRY_CLIENT.getAllVersions(SchemaBranch.MASTER_BRANCH, schemaName, Collections.singletonList(SchemaVersionLifecycleStates.ENABLED.getId()))),
            new HashSet<>(Arrays.asList(v1)));
    Assert.assertEquals(
            transformToSchemaIdVersions(SCHEMA_REGISTRY_CLIENT.getAllVersions(SchemaBranch.MASTER_BRANCH, schemaName, Collections.singletonList(SchemaVersionLifecycleStates.ARCHIVED.getId()))),
            new HashSet<>(Arrays.asList(v2,v3)));
    Assert.assertEquals(
            transformToSchemaIdVersions(SCHEMA_REGISTRY_CLIENT.getAllVersions(schemaBranch.getName(), schemaName, Collections.singletonList(SchemaVersionLifecycleStates.REVIEWED.getId()))),
            new HashSet<>(Arrays.asList(v4)));

    SCHEMA_REGISTRY_CLIENT.disableSchemaVersion(v1.getSchemaVersionId());
    Assert.assertEquals(
            transformToSchemaIdVersions(SCHEMA_REGISTRY_CLIENT.getAllVersions(SchemaBranch.MASTER_BRANCH, schemaName,
                    Arrays.asList(SchemaVersionLifecycleStates.ARCHIVED.getId(), SchemaVersionLifecycleStates.DISABLED.getId()))),
            new HashSet<>(Arrays.asList(v1,v2,v3)));
}