@Override public DeltaCalculationAlgorithm createDeltaCalculator( ProtocolSchema protocolSchemaBody, BaseSchema baseDataSchema) { Schema protocolSchema = new Schema.Parser().parse(protocolSchemaBody.getRawSchema()); Schema baseSchema = new Schema.Parser().parse(baseDataSchema.getRawSchema()); return new DefaultDeltaCalculationAlgorithm(protocolSchema, baseSchema); }
@Test public void testMergeOf3Configurations() throws Exception { // Create Configuration Schema Path schemaUrl = Paths.get(Thread.currentThread().getContextClassLoader().getResource("override/schema.json").toURI()); DataSchema configuraionSchema = new DataSchema(new String(Files.readAllBytes(schemaUrl))); SchemaGenerationAlgorithmFactory factory = new SchemaGenerationAlgorithmFactoryImpl(); SchemaGenerationAlgorithm generator = factory.createSchemaGenerator(configuraionSchema); BaseSchema baseSchemaString = generator.getBaseSchema(); OverrideSchema overrideSchemaString = generator.getOverrideSchema(); // Create Configuration weight 0 Path configuraionWeight0Path = Paths.get(Thread.currentThread().getContextClassLoader().getResource("override/config_weight_0.json").toURI()); BaseData baseData = new BaseData(baseSchemaString, new String(Files.readAllBytes(configuraionWeight0Path))); // Create Configuration weight 1 Path configuraionWeight1Path = Paths.get(Thread.currentThread().getContextClassLoader().getResource("override/config_weight_1.json").toURI()); OverrideData data1 = new OverrideData(overrideSchemaString, new String(Files.readAllBytes(configuraionWeight1Path))); // Create Configuration weight 2 Path configuraionWeight2Path = Paths.get(Thread.currentThread().getContextClassLoader().getResource("override/config_weight_2.json").toURI()); OverrideData data2 = new OverrideData(overrideSchemaString, new String(Files.readAllBytes(configuraionWeight2Path))); List<OverrideData> configurations = new ArrayList<>(); configurations.add(data1); configurations.add(data2); Path mergedConfiguraionPath = Paths.get(Thread.currentThread().getContextClassLoader().getResource("override/merged_config.json").toURI()); String mergedConfiguraionBody = new String(Files.readAllBytes(mergedConfiguraionPath)); OverrideAlgorithm merger = new DefaultOverrideAlgorithm(); BaseData mergeResult = merger.override(baseData, configurations); GenericAvroConverter<GenericRecord> converter = new GenericAvroConverter<>(baseSchemaString.getRawSchema()); GenericRecord mergeResultGenericRecord = converter.decodeJson(mergeResult.getRawData()); GenericRecord expectedMergedConfiguraionGenericRecord = converter.decodeJson(mergedConfiguraionBody); Assert.assertTrue(mergeResultGenericRecord.equals(expectedMergedConfiguraionGenericRecord)); }
@Test public void testGeneration() throws Exception { // Read Configuration Schema Path schemaPath = Paths.get(Thread.currentThread().getContextClassLoader().getResource("generation/simpleSchema.json").toURI()); BaseSchema configuraionSchema = new BaseSchema(new String(Files.readAllBytes(schemaPath))); Path configurationPath = Paths.get(Thread.currentThread().getContextClassLoader().getResource("generation/simpleConfiguration.json").toURI()); String configuraion = new String(Files.readAllBytes(configurationPath)); GenericAvroConverter<GenericRecord> converter = new GenericAvroConverter<>(configuraionSchema.getRawSchema()); // generated default configuration DefaultUuidValidator uuidGenerator = new DefaultUuidValidator(configuraionSchema, new BaseDataFactory()); KaaData processedConfigurationBody = uuidGenerator.validateUuidFields(new BaseData(configuraionSchema, configuraion), null); GenericRecord processedConfiguration = converter.decodeJson(processedConfigurationBody.getRawData()); Object uuid = processedConfiguration.get(CommonConstants.UUID_FIELD); Assert.assertNotNull(uuid); Assert.assertTrue(uuid instanceof GenericFixed); Assert.assertEquals(CommonConstants.KAA_NAMESPACE + "." + CommonConstants.UUID_TYPE, ((GenericFixed) uuid).getSchema().getFullName()); }
Schema baseAvroSchema = baseSchemaParser.parse(baseConfiguration.getSchema().getRawSchema()); Schema.Parser overrideSchemaParser = new Schema.Parser(); Schema overrideAvroSchema = overrideSchemaParser.parse(
/** * Checks generation of a complex default configuration. */ @Test public void testGenerateComplexDefaultConfigurationSuccess() throws Exception { // Read Configuration Schema Path schemaPath = Paths.get(Thread.currentThread().getContextClassLoader().getResource("generation/complexSchema.json").toURI()); BaseSchema configuraionSchema = new BaseSchema(new String(Files.readAllBytes(schemaPath))); // generated default configuration DefaultRecordGenerationAlgorithm configurationProcessor = new DefaultRecordGenerationAlgorithmImpl(configuraionSchema, new BaseDataFactory()); KaaData generatedConfiguration = configurationProcessor.getRootData(); // Read expected generated Configuration Path expectedGeneratedConfigurationPath = Paths.get(Thread.currentThread().getContextClassLoader().getResource("generation/expectedDefaultConfigurationForComplexSchema.json").toURI()); String expectedGeneratedConfiguration = new String(Files.readAllBytes(expectedGeneratedConfigurationPath)); GenericAvroConverter<GenericRecord> converter = new GenericAvroConverter<>(configuraionSchema.getRawSchema()); GenericRecord generatedConfigurationGenericRecord = converter.decodeJson(generatedConfiguration.getRawData()); GenericRecord expectedGeneratedConfigurationGenericRecord = converter.decodeJson(expectedGeneratedConfiguration); AvroDataCanonizationUtils.removeUuid(generatedConfigurationGenericRecord); AvroDataCanonizationUtils.removeUuid(expectedGeneratedConfigurationGenericRecord); Assert.assertTrue(generatedConfigurationGenericRecord.equals(expectedGeneratedConfigurationGenericRecord)); }
/** * Checks generation of a default configuration for a schema with reused * type. */ @Test public void testGenerateDefaultConfigurationForSchemaWithReusedType() throws Exception { // Read Configuration Schema Path schemaPath = Paths.get(Thread.currentThread().getContextClassLoader().getResource("generation/schemaWithReusedType.json").toURI()); BaseSchema configuraionSchema = new BaseSchema(new String(Files.readAllBytes(schemaPath))); // generated default configuration DefaultRecordGenerationAlgorithm configurationProcessor = new DefaultRecordGenerationAlgorithmImpl(configuraionSchema, new BaseDataFactory()); KaaData generatedConfiguration = configurationProcessor.getRootData(); // Read expected generated Configuration Path expectedGeneratedConfigurationPath = Paths.get(Thread.currentThread().getContextClassLoader().getResource("generation/expectedDefaultConfigurationForSchemaWithReusedType.json").toURI()); String expectedGeneratedConfiguration = new String(Files.readAllBytes(expectedGeneratedConfigurationPath)); GenericAvroConverter<GenericRecord> converter = new GenericAvroConverter<>(configuraionSchema.getRawSchema()); GenericRecord generatedConfigurationGenericRecord = converter.decodeJson(generatedConfiguration.getRawData()); GenericRecord expectedGeneratedConfigurationGenericRecord = converter.decodeJson(expectedGeneratedConfiguration); AvroDataCanonizationUtils.removeUuid(generatedConfigurationGenericRecord); AvroDataCanonizationUtils.removeUuid(expectedGeneratedConfigurationGenericRecord); Assert.assertTrue(generatedConfigurationGenericRecord.equals(expectedGeneratedConfigurationGenericRecord)); }
/** * Checks generation of a default configuration for a schema with fixed * type. */ @Test public void testGenerateDefaultConfigurationForSchemaWithFixedType() throws Exception { // Read Configuration Schema Path schemaPath = Paths.get(Thread.currentThread().getContextClassLoader().getResource("generation/schemaWithFixedType.json").toURI()); BaseSchema configuraionSchema = new BaseSchema(new String(Files.readAllBytes(schemaPath))); // generated default configuration DefaultRecordGenerationAlgorithm configurationProcessor = new DefaultRecordGenerationAlgorithmImpl(configuraionSchema, new BaseDataFactory()); KaaData generatedConfiguration = configurationProcessor.getRootData(); // Read expected generated Configuration Path expectedGeneratedConfigurationPath = Paths.get(Thread.currentThread().getContextClassLoader().getResource("generation/expectedDefaultConfigurationWithFixedType.json").toURI()); String expectedGeneratedConfiguration = new String(Files.readAllBytes(expectedGeneratedConfigurationPath)); GenericAvroConverter<GenericRecord> converter = new GenericAvroConverter<>(configuraionSchema.getRawSchema()); GenericRecord generatedConfigurationGenericRecord = converter.decodeJson(generatedConfiguration.getRawData()); GenericRecord expectedGeneratedConfigurationGenericRecord = converter.decodeJson(expectedGeneratedConfiguration); AvroDataCanonizationUtils.removeUuid(generatedConfigurationGenericRecord); AvroDataCanonizationUtils.removeUuid(expectedGeneratedConfigurationGenericRecord); Assert.assertTrue(generatedConfigurationGenericRecord.equals(expectedGeneratedConfigurationGenericRecord)); }
@Test public void testValidationWithoutOldConfiguration() throws Exception { // Read Configuration Schema Path schemaPath = Paths.get(Thread.currentThread().getContextClassLoader().getResource("generation/simpleSchema.json").toURI()); BaseSchema configuraionSchema = new BaseSchema(new String(Files.readAllBytes(schemaPath))); Schema avroSchema = new Schema.Parser().parse(configuraionSchema.getRawSchema()); GenericRecord record = new GenericData.Record(avroSchema); record.put("intField", 5); GenericFixed uuid = AvroUtils.generateUuidObject(); record.put(CommonConstants.UUID_FIELD, uuid); GenericAvroConverter<GenericRecord> converter = new GenericAvroConverter<>(avroSchema); String configurationBody = converter.encodeToJson(record); DefaultUuidValidator uuidGenerator = new DefaultUuidValidator(configuraionSchema, new BaseDataFactory()); KaaData processedConfigurationBody = uuidGenerator.validateUuidFields(new BaseData(configuraionSchema, configurationBody), null); GenericRecord processedConfiguration = converter.decodeJson(processedConfigurationBody.getRawData()); Assert.assertNotEquals(processedConfiguration.get(CommonConstants.UUID_FIELD), uuid); }
BaseSchema baseSchema = new BaseSchema(configurationSchema.getBaseSchema()); uuidValidator = new DefaultUuidValidator(baseSchema, new BaseDataFactory()); avroConverter = new GenericAvroConverter<GenericRecord>(baseSchema.getRawSchema()); avroSchema = new Schema.Parser().parse(baseSchema.getRawSchema()); } else { LOG.debug("Create default UUID validator with override schema: {}",
@Test public void testValidationOfComplexWithoutOldConfiguration() throws Exception { // Read Configuration Schema Path schemaPath = Paths.get(Thread.currentThread().getContextClassLoader().getResource("generation/simpleComplexSchema.json").toURI()); BaseSchema configuraionSchema = new BaseSchema(new String(Files.readAllBytes(schemaPath))); Schema.Parser schemaParser = new Schema.Parser(); Schema avroSchema = schemaParser.parse(configuraionSchema.getRawSchema()); GenericRecord recordNew1 = new GenericData.Record(schemaParser.getTypes().get("org.kaaproject.recordT")); recordNew1.put("intField", 4); GenericFixed uuidNew1 = AvroUtils.generateUuidObject(); recordNew1.put(CommonConstants.UUID_FIELD, uuidNew1); GenericRecord rootNew = new GenericData.Record(avroSchema); rootNew.put("recordField", recordNew1); GenericAvroConverter<GenericRecord> converter = new GenericAvroConverter<>(avroSchema); String configurationBodyNew = converter.encodeToJson(rootNew); DefaultUuidValidator uuidGenerator = new DefaultUuidValidator(configuraionSchema, new BaseDataFactory()); KaaData processedConfigurationBody = uuidGenerator.validateUuidFields(rootNew, null); GenericRecord processedConfiguration = converter.decodeJson(processedConfigurationBody.getRawData()); GenericRecord processedRecord = (GenericRecord) processedConfiguration.get("recordField"); Assert.assertNotEquals(uuidNew1, processedRecord.get(CommonConstants.UUID_FIELD)); Assert.assertNotNull(processedConfiguration.get(CommonConstants.UUID_FIELD)); }
@Test public void testValidationWithOldConfiguration() throws Exception { // Read Configuration Schema Path schemaPath = Paths.get(Thread.currentThread().getContextClassLoader().getResource("generation/simpleSchema.json").toURI()); BaseSchema configuraionSchema = new BaseSchema(new String(Files.readAllBytes(schemaPath))); Schema avroSchema = new Schema.Parser().parse(configuraionSchema.getRawSchema()); GenericRecord recordNew = new GenericData.Record(avroSchema); recordNew.put("intField", 4); GenericFixed uuidNew = AvroUtils.generateUuidObject(); recordNew.put(CommonConstants.UUID_FIELD, uuidNew); GenericRecord recordOld = new GenericData.Record(avroSchema); recordOld.put("intField", 5); GenericFixed uuidOld = AvroUtils.generateUuidObject(); recordOld.put(CommonConstants.UUID_FIELD, uuidOld); GenericAvroConverter<GenericRecord> converter = new GenericAvroConverter<>(avroSchema); String configurationBodyNew = converter.encodeToJson(recordNew); String configurationBodyOld = converter.encodeToJson(recordOld); DefaultUuidValidator uuidGenerator = new DefaultUuidValidator(configuraionSchema, new BaseDataFactory()); KaaData processedConfigurationBody = uuidGenerator.validateUuidFields(recordNew, recordOld); GenericRecord processedConfiguration = converter.decodeJson(processedConfigurationBody.getRawData()); Assert.assertEquals(processedConfiguration.get(CommonConstants.UUID_FIELD), uuidOld); }
@Test public void testValidationOfComplexTypes() throws Exception { // Read Configuration Schema Path schemaPath = Paths.get(Thread.currentThread().getContextClassLoader().getResource("generation/simpleComplexSchema.json").toURI()); BaseSchema configuraionSchema = new BaseSchema(new String(Files.readAllBytes(schemaPath))); Schema.Parser schemaParser = new Schema.Parser(); Schema avroSchema = schemaParser.parse(configuraionSchema.getRawSchema()); GenericRecord recordNew1 = new GenericData.Record(schemaParser.getTypes().get("org.kaaproject.recordT")); recordNew1.put("intField", 4); GenericFixed uuidNew1 = AvroUtils.generateUuidObject(); recordNew1.put(CommonConstants.UUID_FIELD, uuidNew1); GenericRecord rootNew = new GenericData.Record(avroSchema); rootNew.put("recordField", recordNew1); GenericRecord recordOld1 = new GenericData.Record(schemaParser.getTypes().get("org.kaaproject.recordT")); recordOld1.put("intField", 6); recordOld1.put(CommonConstants.UUID_FIELD, uuidNew1); GenericRecord rootOld = new GenericData.Record(avroSchema); rootOld.put("recordField", recordOld1); rootOld.put(CommonConstants.UUID_FIELD, AvroUtils.generateUuidObject()); GenericAvroConverter<GenericRecord> converter = new GenericAvroConverter<>(avroSchema); String configurationBodyNew = converter.encodeToJson(rootNew); String configurationBodyOld = converter.encodeToJson(rootOld); DefaultUuidValidator uuidGenerator = new DefaultUuidValidator(configuraionSchema, new BaseDataFactory()); KaaData processedConfigurationBody = uuidGenerator.validateUuidFields(rootNew, rootOld); GenericRecord processedConfiguration = converter.decodeJson(processedConfigurationBody.getRawData()); GenericRecord processedRecord = (GenericRecord) processedConfiguration.get("recordField"); Assert.assertEquals(uuidNew1, processedRecord.get(CommonConstants.UUID_FIELD)); Assert.assertNotNull(processedConfiguration.get(CommonConstants.UUID_FIELD)); }
private void generateSchemas(ConfigurationSchemaDto schema) throws SchemaCreationException { CTLSchemaDto ctlSchema = ctlService.findCtlSchemaById(schema.getCtlSchemaId()); String sch = ctlService.flatExportAsString(ctlSchema); DataSchema dataSchema = new DataSchema(sch); if (!dataSchema.isEmpty()) { SchemaGenerationAlgorithm schemaGenerator = schemaGeneratorFactory.createSchemaGenerator(dataSchema); ProtocolSchema protocol = schemaGenerator.getProtocolSchema(); BaseSchema base = schemaGenerator.getBaseSchema(); OverrideSchema override = schemaGenerator.getOverrideSchema(); if (!protocol.isEmpty() && !base.isEmpty() && !override.isEmpty()) { schema.setBaseSchema(base.getRawSchema()); schema.setProtocolSchema(protocol.getRawSchema()); schema.setOverrideSchema(override.getRawSchema()); } else { LOG.trace("One or more generated schemas are empty. base: {} protocol {} override {}", base, protocol, override); throw new IncorrectParameterException("Can't generate schemas. Check your data schema"); } } else { LOG.warn("Can't generate schemas because data schema is empty."); } }
BaseSchema configuraionSchema = new BaseSchema(new String(Files.readAllBytes(schemaPath))); Schema.Parser schemaParser = new Schema.Parser(); Schema avroSchema = schemaParser.parse(configuraionSchema.getRawSchema());
BaseSchema configuraionSchema = new BaseSchema(new String(Files.readAllBytes(schemaPath))); Schema.Parser schemaParser = new Schema.Parser(); Schema avroSchema = schemaParser.parse(configuraionSchema.getRawSchema());
BaseSchema configuraionSchema = new BaseSchema(new String(Files.readAllBytes(schemaPath))); Schema.Parser schemaParser = new Schema.Parser(); Schema avroSchema = schemaParser.parse(configuraionSchema.getRawSchema());