public void when_descriptor_given_it_should_serialize_and_parse_message() { var ingressRegistry = new CaseOfficeIngressApiMessageTypesRegistry(); ingressRegistry.Initialize(); var ingressDescriptor = ingressRegistry.GetDescriptorByMessageTypeName <CreateJusticeCase>(typeof(CreateJusticeCase).FullName !); var egressRegistry = new CaseOfficeEgressApiMessageTypesRegistry(); egressRegistry.Initialize(); var egressDescriptor = egressRegistry.GetDescriptorByMessageType <CreateJusticeCase>(); var message = new CreateJusticeCase { SubjectId = Guid.NewGuid(), Reason = "some reason", ResponsibleId = Guid.NewGuid() }; using var bufferOwner = MemoryPool <byte> .Shared.Rent(); var buffer = bufferOwner.Memory; egressDescriptor.Serialize(message, buffer); var parsedMessage = ingressDescriptor.Parse(buffer); parsedMessage.Should().NotBeSameAs(message, "parsed message should not be the same object as the original"); parsedMessage.Should().BeEquivalentTo(message, "parsed message should have same property values as the original"); }
public void when_executed_with_context_with_wrong_message_type_within_broker_message_metadata_it_should_fail() { var registry = new CaseOfficeIngressApiMessageTypesRegistry(); registry.Initialize(); var context = new MessageHandlingContext { Api = new FakeIngressApi { MessageTypesRegistry = registry } }; var step = new ExtractMessageTypeStep(); // ReSharper disable once JoinDeclarationAndInitializer - it's a way to test. Func <Task> sut; context.Metadata = new Dictionary <string, string> { { VentureApi.Headers.MessageTypeName, null } }; sut = () => step.Execute(context); sut.Should().Throw <PoezdOperationException>("null is a wrong message type value"); context.Metadata = new Dictionary <string, string> { { VentureApi.Headers.MessageTypeName, string.Empty } }; sut = () => step.Execute(context); sut.Should().Throw <PoezdOperationException>("an empty string is a wrong message type value"); context.Metadata = new Dictionary <string, string> { { VentureApi.Headers.MessageTypeName, WhitespaceString } }; sut = () => step.Execute(context); sut.Should().Throw <PoezdOperationException>("a whitespace string is a wrong message type value"); }
public void when_registry_initialized_and_clr_type_for_known_message_type_requested_it_should_return_clr_type() { var sut = new CaseOfficeIngressApiMessageTypesRegistry(); sut.Initialize(); sut.GetMessageTypeByItsMessageTypeName(typeof(CreateJusticeCase).FullName !).Should().NotBeNull("type is known"); }
public async Task when_create_case_command_received_it_should_create_case_in_store() { var container = RoutingTests.SetupContainer <EmptyPipeFitter, FinishTestPipeFitter, EmptyPipeFitter, FinishTestPipeFitter>( api => api .WithId("ingress-case-office") .WithQueueNamePatternsProvider <VentureQueueNamePatternsProvider>() .WithPipeFitter <VentureIngressPipeFitter>() .WithMessageKey <Ignore>() .WithMessagePayload <byte[]>() .WithMessageTypesRegistry <CaseOfficeIngressApiMessageTypesRegistry>() .WithHandlerRegistry <VentureServiceHandlersRegistry>(), api => api .WithId("egress-case-office") .WithMessageKey <int>() .WithMessagePayload <byte[]>() .WithMessageTypesRegistry <EmptyEgressApiMessageTypesRegistry>() .WithPipeFitter <EmptyPipeFitter>(), _testOutput); container.RegisterSingleton <VentureQueueNamePatternsProvider>(); AddIngressPipeline(container); var registry = new CaseOfficeIngressApiMessageTypesRegistry(); registry.Initialize(); container.RegisterInstance(registry); const string topic = "venture.commands.case-office.v1"; var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(value: 5)).Token; await using var kafkaTestContext = _kafkaTestContextFactory.Create <int, byte[]>(timeout); await kafkaTestContext.CreateTopics(topic); var testIsFinished = RoutingTests.AddTestFinishSemaphore(container); var router = container.GetMessageRouter(); await router.Start(timeout); var stringCreator = new StringCreator(); var expectedReason = stringCreator.Get(length: 10); var(command, serialized) = CreateSerializedMessage(expectedReason); var headers = CreateHeaders(command.GetType()); await kafkaTestContext.Produce( topic, int.MaxValue, serialized, headers); await testIsFinished.WaitAsync(timeout); var justiceCaseStorage = (IAggregateStorageTestBackdoor <JusticeCase>)container.GetInstance <IAggregateStorage <JusticeCase> >(); justiceCaseStorage.GetAll().Should().HaveCount(expected: 1); justiceCaseStorage.GetAll().Single().Reason.Should().Be(expectedReason); // TODO: assert }
public void when_registry_initialized_and_descriptor_for_known_message_type_requested_it_should_return_descriptor() { var sut = new CaseOfficeIngressApiMessageTypesRegistry(); sut.Initialize(); sut.GetDescriptorByMessageTypeName <CreateJusticeCase>(typeof(CreateJusticeCase).FullName !).Should() .NotBeNull("descriptor is present").And .BeAssignableTo <IIngressMessageTypeDescriptor <CreateJusticeCase> >("descriptor should support typed contract"); }
public void when_executed_without_context_it_should_fail() { var registry = new CaseOfficeIngressApiMessageTypesRegistry(); registry.Initialize(); // ReSharper disable once AssignNullToNotNullAttribute - it's a test against null. Func <Task> sut = async() => await new ExtractMessageTypeStep().Execute(context: null); sut.Should().Throw <ArgumentNullException>().Where(exception => exception.ParamName.Equals("context"), "context must be specified"); }
public void when_executed_with_message_type_and_message_payload_but_without_message_type_descriptor_within_context_it_should_fail() { var(_, messageBytes) = CreateSerializedMessage(); var registry = new CaseOfficeIngressApiMessageTypesRegistry(); registry.Initialize(); var messageType = typeof(CreateJusticeCase); var context = new MessageHandlingContext { MessageType = messageType, Payload = messageBytes }; var step = new ParseBrokerMessageStep(); Func <Task> sut = () => step.Execute(context); sut.Should().Throw <KeyNotFoundException>("impossible to parse message without its message type descriptor"); }
public void when_executed_with_context_without_message_type_registry_it_should_fail() { var registry = new CaseOfficeIngressApiMessageTypesRegistry(); registry.Initialize(); const string expectedTypeName = "Venture.CaseOffice.Messages.V1.Commands.CreateCase"; var context = new MessageHandlingContext { Metadata = new Dictionary <string, string> { { VentureApi.Headers.MessageTypeName, expectedTypeName } } }; var step = new ExtractMessageTypeStep(); Func <Task> sut = () => step.Execute(context); sut.Should().Throw <KeyNotFoundException>("it can not work without message types registry"); }
public void when_executed_with_message_type_but_without_broker_message_within_context_it_should_fail() { var registry = new CaseOfficeIngressApiMessageTypesRegistry(); registry.Initialize(); var messageType = typeof(CreateJusticeCase); var context = new MessageHandlingContext { MessageType = messageType, Descriptor = registry.GetDescriptorByMessageTypeName <CreateJusticeCase>(messageType.FullName !) }; var step = new ParseBrokerMessageStep(); Func <Task> sut = () => step.Execute(context); sut.Should().Throw <KeyNotFoundException>("impossible to parse message without its payload"); }
public void when_executed_with_context_with_missing_message_type_within_broker_message_metadata_it_should_fail() { var registry = new CaseOfficeIngressApiMessageTypesRegistry(); registry.Initialize(); var context = new MessageHandlingContext { Api = new FakeIngressApi { MessageTypesRegistry = registry }, Metadata = new Dictionary <string, string>() }; var step = new ExtractMessageTypeStep(); Func <Task> sut = () => step.Execute(context); sut.Should().Throw <PoezdOperationException>("it's an error to publish message without its type within broker message metadata"); }
public void when_executed_with_context_with_unknown_message_type_within_broker_message_metadata_it_should_fail_skip_message_exception() { var registry = new CaseOfficeIngressApiMessageTypesRegistry(); registry.Initialize(); var step = new ExtractMessageTypeStep(); var context = new MessageHandlingContext { Api = new FakeIngressApi { MessageTypesRegistry = registry }, Metadata = new Dictionary <string, string> { { VentureApi.Headers.MessageTypeName, "unknown" } } }; Func <Task> sut = () => step.Execute(context); sut.Should().Throw <PoezdOperationException>("unknown type name used as a broker message type name"); }
public async Task when_executed_with_broker_message_and_message_type_name_withing_context_it_should_store_deserialized_message() { var(message, messageBytes) = CreateSerializedMessage(); var registry = new CaseOfficeIngressApiMessageTypesRegistry(); registry.Initialize(); var messageType = typeof(CreateJusticeCase); var context = new MessageHandlingContext { Payload = messageBytes, MessageType = messageType, Descriptor = registry.GetDescriptorByMessageTypeName <CreateJusticeCase>(messageType.FullName !) }; var sut = new ParseBrokerMessageStep(); await sut.Execute(context); var parsedMessage = context.Message; parsedMessage.Should().NotBeSameAs(message, "parsed message should not be the same object as the original"); parsedMessage.Should().BeEquivalentTo(message, "parsed message should have same property values as the original"); }
public async Task when_executed_with_context_containing_message_type_within_broker_message_metadata_it_should_store_it_in_context() { var registry = new CaseOfficeIngressApiMessageTypesRegistry(); registry.Initialize(); const string expectedTypeName = "Venture.CaseOffice.Messages.V1.Commands.CreateJusticeCase"; var context = new MessageHandlingContext { Api = new FakeIngressApi { MessageTypesRegistry = registry }, Metadata = new Dictionary <string, string> { { VentureApi.Headers.MessageTypeName, expectedTypeName } } }; var sut = new ExtractMessageTypeStep(); await sut.Execute(context); context.TypeName.Should().Be(expectedTypeName, "this header should be copied"); context.MessageType.Should().Be(typeof(CreateJusticeCase), "message type should be recognized by its name"); }