use of com.apple.foundationdb.record.metadata.MetaDataException in project fdb-record-layer by FoundationDB.
the class FDBMetaDataStoreTest method recordTypesWithOneOfUnion.
@Test
public void recordTypesWithOneOfUnion() {
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
RecordMetaDataBuilder metaData = RecordMetaData.newBuilder().setRecords(TestRecordsOneOfProto.getDescriptor());
final KeyExpression pkey = Key.Expressions.field("rec_no");
metaData.getRecordType("MySimpleRecord").setPrimaryKey(pkey);
metaData.getRecordType("MyOtherRecord").setPrimaryKey(pkey);
metaDataStore.saveRecordMetaData(metaData);
context.commit();
}
// Add a record type to oneOf. It should fail.
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
DescriptorProtos.DescriptorProto newRecordType = DescriptorProtos.DescriptorProto.newBuilder().setName("MyNewRecord").addField(DescriptorProtos.FieldDescriptorProto.newBuilder().setLabel(DescriptorProtos.FieldDescriptorProto.Label.LABEL_OPTIONAL).setType(DescriptorProtos.FieldDescriptorProto.Type.TYPE_INT32).setName("rec_no").setNumber(1)).build();
MetaDataException e = assertThrows(MetaDataException.class, () -> addRecordType(newRecordType, Key.Expressions.field("rec_no")));
assertEquals(e.getMessage(), "Adding record type to oneof is not allowed");
context.commit();
}
}
use of com.apple.foundationdb.record.metadata.MetaDataException in project fdb-record-layer by FoundationDB.
the class FDBMetaDataStoreTest method dontRenameNonUnionToUnion.
/**
* Verify that renaming a regular record type to the default union name throws an error.
*/
@Test
public void dontRenameNonUnionToUnion() {
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
RecordMetaData metaData = RecordMetaData.build(TestRecords1Proto.getDescriptor());
metaDataStore.saveRecordMetaData(metaData);
context.commit();
}
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
// to avoid conflicts
renameRecordType(RecordMetaDataBuilder.DEFAULT_UNION_NAME, "RecordOneUnion");
MetaDataException e = assertThrows(MetaDataException.class, () -> renameRecordType("MySimpleRecord", RecordMetaDataBuilder.DEFAULT_UNION_NAME));
assertEquals("Cannot rename record type to the default union name", e.getMessage());
}
}
use of com.apple.foundationdb.record.metadata.MetaDataException in project fdb-record-layer by FoundationDB.
the class FDBMetaDataStoreTest method renameRecordTypeWithClashingNested.
/**
* Verify that if there is a nested type defined in a message, then changing a top-level record type
* to that name won't cause any fields in that type to start pointing to the new type.
*/
@Test
public void renameRecordTypeWithClashingNested() {
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
RecordMetaData metaData = RecordMetaData.build(TestRecordsDoubleNestedProto.getDescriptor());
metaDataStore.saveRecordMetaData(metaData);
context.commit();
}
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
renameRecordType("OtherRecord", "InnerRecord");
validateInnerRecordsInRightPlaces(metaDataStore.getRecordMetaData());
// Unqualify the inner record's field type name
metaDataStore.mutateMetaData(metaDataProtoBuilder -> {
DescriptorProtos.FileDescriptorProto.Builder fileBuilder = metaDataProtoBuilder.getRecordsBuilder();
DescriptorProtos.DescriptorProto.Builder outerBuilder = fileBuilder.getMessageTypeBuilderList().stream().filter(messageBuilder -> messageBuilder.getName().equals("OuterRecord")).findFirst().get();
outerBuilder.getFieldBuilderList().stream().filter(fieldBuilder -> fieldBuilder.getName().equals("inner")).forEach(fieldBuilder -> fieldBuilder.setTypeName("MiddleRecord.InnerRecord"));
outerBuilder.getNestedTypeBuilderList().stream().filter(messageBuilder -> messageBuilder.getName().equals("MiddleRecord")).flatMap(messageBuilder -> messageBuilder.getFieldBuilderList().stream()).filter(fieldBuilder -> fieldBuilder.getName().equals("inner")).forEach(fieldBuilder -> fieldBuilder.setTypeName("InnerRecord"));
});
validateInnerRecordsInRightPlaces(metaDataStore.getRecordMetaData());
// do not commit
}
// isn't fully qualified.
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
metaDataStore.mutateMetaData(metaDataProtoBuilder -> {
DescriptorProtos.FileDescriptorProto.Builder fileBuilder = metaDataProtoBuilder.getRecordsBuilder();
DescriptorProtos.DescriptorProto.Builder outerBuilder = fileBuilder.getMessageTypeBuilderList().stream().filter(messageBuilder -> messageBuilder.getName().equals("OuterRecord")).findFirst().get();
outerBuilder.getFieldBuilderList().stream().filter(fieldBuilder -> fieldBuilder.getName().equals("inner")).forEach(fieldBuilder -> fieldBuilder.setTypeName("MiddleRecord.InnerRecord"));
outerBuilder.getNestedTypeBuilderList().stream().filter(messageBuilder -> messageBuilder.getName().equals("MiddleRecord")).flatMap(messageBuilder -> messageBuilder.getFieldBuilderList().stream()).filter(fieldBuilder -> fieldBuilder.getName().equals("inner")).forEach(fieldBuilder -> fieldBuilder.setTypeName("InnerRecord"));
});
renameRecordType("OtherRecord", "InnerRecord");
validateInnerRecordsInRightPlaces(metaDataStore.getRecordMetaData());
// do not commit
}
// would change the type that the Protobuf would not have built.
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
MetaDataException e = assertThrows(MetaDataException.class, () -> metaDataStore.mutateMetaData(metaDataProtoBuilder -> {
DescriptorProtos.FileDescriptorProto.Builder fileBuilder = metaDataProtoBuilder.getRecordsBuilder();
fileBuilder.getMessageTypeBuilderList().stream().filter(messageBuilder -> messageBuilder.getName().equals("OuterRecord")).flatMap(messageBuilder -> messageBuilder.getFieldBuilderList().stream()).filter(fieldBuilder -> fieldBuilder.getName().equals("inner")).forEach(fieldBuilder -> fieldBuilder.setTypeName("InnerRecord"));
}));
assertEquals("Error converting from protobuf", e.getMessage());
assertNotNull(e.getCause());
assertThat(e.getCause(), instanceOf(Descriptors.DescriptorValidationException.class));
assertThat(e.getCause().getMessage(), containsString("\"InnerRecord\" is not defined."));
// do not commit
}
}
use of com.apple.foundationdb.record.metadata.MetaDataException in project fdb-record-layer by FoundationDB.
the class FDBMetaDataStoreTest method tryRenameNonExistentRecord.
/**
* Verify that renaming a non-existent record doesn't work.
*/
@Test
public void tryRenameNonExistentRecord() {
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
RecordMetaData metaData = RecordMetaData.build(TestRecords1Proto.getDescriptor());
metaDataStore.saveRecordMetaData(metaData);
context.commit();
}
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
MetaDataException e = assertThrows(MetaDataException.class, () -> renameRecordType("MyNonExistentRecord", "SomethingElse"));
assertEquals("No record type found with name MyNonExistentRecord", e.getMessage());
}
}
use of com.apple.foundationdb.record.metadata.MetaDataException in project fdb-record-layer by FoundationDB.
the class FDBMetaDataStoreTest method unionRecordTypes.
@Test
public void unionRecordTypes() {
int version;
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
RecordMetaData metaData = RecordMetaData.build(TestRecords1Proto.getDescriptor());
version = metaData.getVersion();
metaDataStore.saveRecordMetaData(metaData);
context.commit();
assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
}
// Add a record type with default union name.
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
assertEquals(version, metaDataStore.getRecordMetaData().getVersion());
DescriptorProtos.DescriptorProto unionRecordType = DescriptorProtos.DescriptorProto.newBuilder().setName(RecordMetaDataBuilder.DEFAULT_UNION_NAME).addField(DescriptorProtos.FieldDescriptorProto.newBuilder().setLabel(DescriptorProtos.FieldDescriptorProto.Label.LABEL_OPTIONAL).setType(DescriptorProtos.FieldDescriptorProto.Type.TYPE_INT32).setName("rec_no").setNumber(1)).build();
MetaDataException e = assertThrows(MetaDataException.class, () -> addRecordType(unionRecordType, Key.Expressions.field("rec_no")));
assertEquals(e.getMessage(), "Adding UNION record type not allowed");
// version must remain unchanged
assertEquals(version, metaDataStore.getRecordMetaData().getVersion());
context.commit();
}
// Add a record type with non-default union name but union usage.
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
assertEquals(version, metaDataStore.getRecordMetaData().getVersion());
DescriptorProtos.MessageOptions.Builder unionMessageOptions = DescriptorProtos.MessageOptions.newBuilder().setExtension(RecordMetaDataOptionsProto.record, RecordMetaDataOptionsProto.RecordTypeOptions.newBuilder().setUsage(RecordMetaDataOptionsProto.RecordTypeOptions.Usage.UNION).build());
DescriptorProtos.DescriptorProto unionRecordType = DescriptorProtos.DescriptorProto.newBuilder().setName("NonDefaultUnionRecord").addField(DescriptorProtos.FieldDescriptorProto.newBuilder().setLabel(DescriptorProtos.FieldDescriptorProto.Label.LABEL_OPTIONAL).setType(DescriptorProtos.FieldDescriptorProto.Type.TYPE_INT32).setName("rec_no").setNumber(1)).setOptions(unionMessageOptions).build();
MetaDataException e = assertThrows(MetaDataException.class, () -> addRecordType(unionRecordType, Key.Expressions.field("rec_no")));
assertEquals(e.getMessage(), "Adding UNION record type not allowed");
// version must remain unchanged
assertEquals(version, metaDataStore.getRecordMetaData().getVersion());
context.commit();
}
// Deprecate the record type union.
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
assertEquals(version, metaDataStore.getRecordMetaData().getVersion());
MetaDataException e = assertThrows(MetaDataException.class, () -> deprecateRecordType(RecordMetaDataBuilder.DEFAULT_UNION_NAME));
assertEquals(e.getMessage(), "Cannot deprecate the union");
// version must remain unchanged
assertEquals(version, metaDataStore.getRecordMetaData().getVersion());
context.commit();
}
}
Aggregations