com.hortonworks.registries.schemaregistry.avro.AvroCompositeSchemasTest.java Source code

Java tutorial

Introduction

Here is the source code for com.hortonworks.registries.schemaregistry.avro.AvroCompositeSchemasTest.java

Source

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hortonworks.registries.schemaregistry.avro;

import com.hortonworks.registries.schemaregistry.SchemaProvider;
import com.hortonworks.registries.schemaregistry.SchemaVersionInfo;
import com.hortonworks.registries.schemaregistry.SchemaVersionKey;
import com.hortonworks.registries.schemaregistry.SchemaVersionRetriever;
import com.hortonworks.registries.schemaregistry.errors.CyclicSchemaDependencyException;
import com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException;
import com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException;
import org.apache.avro.Schema;
import org.apache.commons.io.IOUtils;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 *
 */
public class AvroCompositeSchemasTest {
    private static final Logger LOG = LoggerFactory.getLogger(AvroCompositeSchemasTest.class);

    private AvroSchemaProvider avroSchemaProvider;
    private Map<String, SchemaVersionInfo> versions;

    @Before
    public void testComposites() throws Exception {
        versions = new HashMap<>();
        versions.put("utils", cretaeSchemaVersionInfo("/avro/composites/util.avsc"));
        versions.put("account", cretaeSchemaVersionInfo("/avro/composites/account.avsc"));
        versions.put("account-cyclic", cretaeSchemaVersionInfo("/avro/composites/account-cyclic.avsc"));
        versions.put("account-dep", cretaeSchemaVersionInfo("/avro/composites/account-dep.avsc"));

        avroSchemaProvider = new AvroSchemaProvider();
        Map<String, Object> config = Collections.singletonMap(SchemaProvider.SCHEMA_VERSION_RETRIEVER_CONFIG,
                (SchemaVersionRetriever) key -> versions.get(key.getSchemaName()));
        avroSchemaProvider.init(config);
    }

    private SchemaVersionInfo cretaeSchemaVersionInfo(String name) throws IOException {
        return new SchemaVersionInfo(1, getResourceText(name), System.currentTimeMillis(), "");
    }

    private String getResourceText(String name) throws IOException {
        return IOUtils.toString(AvroCompositeSchemasTest.class.getResourceAsStream(name), "UTF-8");
    }

    @Test
    public void testResultantSchema() throws Exception {
        String accountSchemaText = avroSchemaProvider
                .getResultantSchema(getResourceText("/avro/composites/account.avsc"));
        LOG.info("accountSchemaText [{}] ", accountSchemaText);
        LOG.info("Validating the resultant schema");
        Schema.Parser parser = new Schema.Parser();
        Schema parsedSchema = parser.parse(accountSchemaText);
        LOG.info("parsedSchema [{}] ", parsedSchema);
    }

    @Test(expected = CyclicSchemaDependencyException.class)
    public void testCyclicSchema() throws Exception {
        String accountSchemaText = avroSchemaProvider
                .getResultantSchema(getResourceText("/avro/composites/account-ref-cyclic.avsc"));
    }
}