@Override public BaseSchema createBaseSchema(String schema) { return new BaseSchema(schema); }
@Test public void testGetConfigurationByNameForNullNameParameter() 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()); GenericRecord generatedConfiguration = configurationProcessor.getConfigurationByName(null, "org.kaa.config"); Assert.assertNull(generatedConfiguration); }
/** * Verifies that configuration generation fails when schema contains field * of type map. */ @Test(expected = ConfigurationGenerationException.class) public void testConfigurationGenerationFailedForSchemaWithMapField() throws Exception { // Read Configuration Schema Path schemaPath = Paths.get(Thread.currentThread().getContextClassLoader().getResource("generation/schemaWithMapType.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(); }
/** * Verifies that configuration generation fails when schema contains "by_default" field * doesn't match to the actual field type. */ @Test(expected = ConfigurationGenerationException.class) public void testConfigurationGenerationFailedForSchemaWithUnsuitableDefault() throws Exception { // Read Configuration Schema Path schemaPath = Paths.get(Thread.currentThread().getContextClassLoader().getResource("generation/schemaWithUnsuitableDefault.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(); }
@Test public void testGetRootBinaryConfigurationFailsWhenByDefauleIsMissed() throws Exception { // Read Configuration Schema Path schemaPath = Paths.get(Thread.currentThread().getContextClassLoader().getResource("generation/schemaWithMissedByDefault.json").toURI()); BaseSchema configuraionSchema = new BaseSchema(new String(Files.readAllBytes(schemaPath))); // generated default configuration DefaultRecordGenerationAlgorithm configurationProcessor = new DefaultRecordGenerationAlgorithmImpl(configuraionSchema, new BaseDataFactory()); GenericRecord generatedConfiguration = configurationProcessor.getRootConfiguration(); Assert.assertEquals(0, generatedConfiguration.get("testField5")); }
@Test public void testGetConfigurationByNameForNullNamespaceParameter() 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()); GenericRecord generatedConfiguration = configurationProcessor.getConfigurationByName("testT", null); Assert.assertNull(generatedConfiguration); }
@Test public void testGetConfigurationByNameForMissedSchema() 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()); GenericRecord generatedConfiguration = configurationProcessor.getConfigurationByName("missed", "org.kaa.config"); Assert.assertNull(generatedConfiguration); }
@Test public void testIdenticalConfigurations() throws IOException, URISyntaxException, DeltaCalculatorException { URL protocolSchemaUrl = Thread.currentThread().getContextClassLoader().getResource(COMPLEX_PROTOCOL_SCHEMA); URL schemaUrl = Thread.currentThread().getContextClassLoader().getResource(COMPLEX_SCHEMA); Schema protocolSchema = new Schema.Parser().parse(new File(protocolSchemaUrl.getPath())); Schema schema = new Schema.Parser().parse(new File(schemaUrl.getPath())); DeltaCalculationAlgorithm calculator = new DefaultDeltaCalculationAlgorithm(protocolSchema, schema); ByteArrayOutputStream baosOld = new ByteArrayOutputStream(); URL oldConfigUrl = Thread.currentThread().getContextClassLoader().getResource(OLD_COMPLEX_CONFIG); IOUtils.copy(new FileInputStream(oldConfigUrl.getPath()), baosOld, 1024); String oldStr = new String(baosOld.toByteArray(), "UTF-8"); ByteArrayOutputStream baosNew = new ByteArrayOutputStream(); URL newConfigUrl = Thread.currentThread().getContextClassLoader().getResource(OLD_COMPLEX_CONFIG); IOUtils.copy(new FileInputStream(newConfigUrl.getPath()), baosNew, 1024); String newStr = new String(baosNew.toByteArray(), "UTF-8"); BaseData oldData = new BaseData(new BaseSchema(schema.toString()), oldStr); BaseData newData = new BaseData(new BaseSchema(schema.toString()), newStr); RawBinaryDelta deltaResult = calculator.calculate(oldData, newData); assertFalse(deltaResult.hasChanges()); assertNull(deltaResult.getData()); assertFalse(deltaResult.hasChanges()); } }
@Test public void testGetConfigurationByNameForNestedType() 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()); configurationProcessor.getConfigurationByName("testRecordItemT", "org.kaa.config"); GenericRecord generatedConfiguration = configurationProcessor.getConfigurationByName("testRecordItemT", "org.kaa.config"); Assert.assertNotNull(generatedConfiguration); Assert.assertEquals(4, generatedConfiguration.get("testField4")); }
@Test public void testComplexFieldsDelta() throws IOException, URISyntaxException, DeltaCalculatorException { URL protocolSchemaUrl = Thread.currentThread().getContextClassLoader().getResource(COMPLEX_PROTOCOL_SCHEMA); URL schemaUrl = Thread.currentThread().getContextClassLoader().getResource(COMPLEX_SCHEMA); Schema protocolSchema = new Schema.Parser().parse(new File(protocolSchemaUrl.getPath())); Schema schema = new Schema.Parser().parse(new File(schemaUrl.getPath())); DeltaCalculationAlgorithm calculator = new DefaultDeltaCalculationAlgorithm(protocolSchema, schema); ByteArrayOutputStream baosOld = new ByteArrayOutputStream(); URL oldConfigUrl = Thread.currentThread().getContextClassLoader().getResource(OLD_COMPLEX_CONFIG); IOUtils.copy(new FileInputStream(oldConfigUrl.getPath()), baosOld, 1024); String oldStr = new String(baosOld.toByteArray(), "UTF-8"); ByteArrayOutputStream baosNew = new ByteArrayOutputStream(); URL newConfigUrl = Thread.currentThread().getContextClassLoader().getResource(NEW_COMPLEX_CONFIG); IOUtils.copy(new FileInputStream(newConfigUrl.getPath()), baosNew, 1024); String newStr = new String(baosNew.toByteArray(), "UTF-8"); BaseData oldData = new BaseData(new BaseSchema(schema.toString()), oldStr); BaseData newData = new BaseData(new BaseSchema(schema.toString()), newStr); RawBinaryDelta deltaResult = calculator.calculate(oldData, newData); AvroBinaryDelta deltaExpected = getComplexFieldDelta(protocolSchema); assertTrue(deltaResult.hasChanges()); assertArrayEquals(deltaExpected.getData(), deltaResult.getData()); assertTrue(deltaResult.hasChanges()); }
@Test public void testPrimitiveFieldsDelta() throws IOException, DeltaCalculatorException { URL protocolSchemaUrl = Thread.currentThread().getContextClassLoader().getResource("delta/primitiveFieldsDeltaProtocolSchema.json"); Schema protocolSchema = new Schema.Parser().parse(new File(protocolSchemaUrl.getPath())); URL schemaUrl = Thread.currentThread().getContextClassLoader().getResource("delta/primitiveFieldsDeltaSchema.json"); Schema schema = new Schema.Parser().parse(new File(schemaUrl.getPath())); DeltaCalculationAlgorithm calculator = new DefaultDeltaCalculationAlgorithm(protocolSchema, schema); ByteArrayOutputStream baosOld = new ByteArrayOutputStream(); URL oldConfigUrl = Thread.currentThread().getContextClassLoader().getResource("delta/primitiveFieldsDeltaCurrent.json"); IOUtils.copy(new FileInputStream(oldConfigUrl.getPath()), baosOld, 1024); String oldStr = new String(baosOld.toByteArray(), "UTF-8"); ByteArrayOutputStream baosNew = new ByteArrayOutputStream(); URL newConfigUrl = Thread.currentThread().getContextClassLoader().getResource("delta/primitiveFieldsDeltaNew.json"); IOUtils.copy(new FileInputStream(newConfigUrl.getPath()), baosNew, 1024); String newStr = new String(baosNew.toByteArray(), "UTF-8"); BaseData oldData = new BaseData(new BaseSchema(schema.toString()), oldStr); BaseData newData = new BaseData(new BaseSchema(schema.toString()), newStr); RawBinaryDelta deltaResult = calculator.calculate(oldData, newData); GenericRecord delta = new GenericData.Record(getDeltaSchemaByFullName(protocolSchema, "org.kaa.config.testT")); GenericEnumSymbol unchanged = new GenericData.EnumSymbol(getSchemaByFullName(delta.getSchema().getField("testField1").schema().getTypes(), "org.kaaproject.configuration.unchangedT"), "unchanged"); byte[] rawUuid = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; GenericFixed uuid = new GenericData.Fixed(delta.getSchema().getField("__uuid").schema(), rawUuid); delta.put("testField1", unchanged); delta.put("testField2", 456); delta.put("__uuid", uuid); AvroBinaryDelta deltaExpected = new AvroBinaryDelta(protocolSchema); deltaExpected.addDelta(delta); assertArrayEquals(deltaExpected.getData(), deltaResult.getData()); }
@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()); }
/** * 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 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)); }
/** * 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)); }
@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); }
@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)); }
savedSchema.getId()); try { BaseSchema baseSchema = new BaseSchema(savedSchema.getBaseSchema()); DefaultRecordGenerationAlgorithm<BaseData> configurationProcessor = new DefaultRecordGenerationAlgorithmImpl<BaseSchema, BaseData>(