Esempio n. 1
0
        public static void FromDescribedSerializationGeneric___Valid___Just_passes_to_specific_factory_version_with_default_factory()
        {
            // Arrange
            var expected = A.Dummy <string>();
            var serializerRepresentation = new SerializerRepresentation(SerializationKind.Json, typeof(NullJsonSerializationConfiguration).ToRepresentation(), CompressionKind.None);
            var payload = "\"" + expected + "\"";
            var describedSerialization = new StringDescribedSerialization(
                expected.GetType().ToRepresentation(),
                serializerRepresentation,
                payload);

            // Act
            var actual = describedSerialization.DeserializePayload <string>();

            // Assert
            actual.Should().NotBeNull();
            actual.Should().Be(expected);
        }
Esempio n. 2
0
        public static void FromDescribedSerializationWithSpecificFactory___Null_object___Works()
        {
            // Arrange
            string expected = null;
            var    serializerRepresentation = new SerializerRepresentation(SerializationKind.Json, typeof(NullJsonSerializationConfiguration).ToRepresentation(), CompressionKind.None);
            var    payload = "null";
            var    describedSerialization = new StringDescribedSerialization(
                typeof(string).ToRepresentation(),
                serializerRepresentation,
                payload);

            // Act
            var actual = DomainExtensions.DeserializePayloadUsingSpecificFactory(
                describedSerialization,
                SerializerFactory.Instance);

            // Assert
            actual.Should().Be(expected);
        }
Esempio n. 3
0
        public static void FromDescribedSerializationWithSpecificFactory___All_valid___Works()
        {
            // Arrange
            var expected = A.Dummy <string>();
            var serializerRepresentation = new SerializerRepresentation(SerializationKind.Json, typeof(NullJsonSerializationConfiguration).ToRepresentation(), CompressionKind.None);
            var payload = "\"" + expected + "\"";
            var describedSerialization = new StringDescribedSerialization(
                expected.GetType().ToRepresentation(),
                serializerRepresentation,
                payload);

            // Act
            var actual = DomainExtensions.DeserializePayloadUsingSpecificFactory(
                describedSerialization,
                SerializerFactory.Instance);

            // Assert
            actual.Should().NotBeNull();
            actual.Should().Be(expected);
        }
Esempio n. 4
0
        static StringDescribedSerializationTest()
        {
            ConstructorArgumentValidationTestScenarios
            .RemoveAllScenarios()
            .AddScenario(() =>
                         new ConstructorArgumentValidationTestScenario <StringDescribedSerialization>
            {
                Name             = "constructor should throw ArgumentNullException when parameter 'payloadTypeRepresentation' is null scenario",
                ConstructionFunc = () =>
                {
                    var referenceObject = A.Dummy <StringDescribedSerialization>();

                    var result = new StringDescribedSerialization(
                        null,
                        referenceObject.SerializerRepresentation,
                        referenceObject.SerializedPayload);

                    return(result);
                },
                ExpectedExceptionType            = typeof(ArgumentNullException),
                ExpectedExceptionMessageContains = new[] { "payloadTypeRepresentation" },
            })
            .AddScenario(() =>
                         new ConstructorArgumentValidationTestScenario <StringDescribedSerialization>
            {
                Name             = "constructor should throw ArgumentNullException when parameter 'serializerRepresentation' is null scenario",
                ConstructionFunc = () =>
                {
                    var referenceObject = A.Dummy <StringDescribedSerialization>();

                    var result = new StringDescribedSerialization(
                        referenceObject.PayloadTypeRepresentation,
                        null,
                        referenceObject.SerializedPayload);

                    return(result);
                },
                ExpectedExceptionType            = typeof(ArgumentNullException),
                ExpectedExceptionMessageContains = new[] { "serializerRepresentation" },
            });
        }
        public override TryHandleRecordResult Execute(
            StandardTryHandleRecordOp operation)
        {
            operation.MustForArg(nameof(operation)).NotBeNull();

            var sqlServerLocator      = this.TryGetLocator(operation);
            var convertedRecordFilter = this.ConvertRecordFilter(operation.RecordFilter, sqlServerLocator);
            var tagIdsForEntryCsv     = this.GetIdsAddIfNecessaryTag(
                sqlServerLocator,
                operation.Tags ?? new List <NamedValue <string> >())
                                        .Select(_ => _.ToStringInvariantPreferred())
                                        .ToCsv();
            var storedProcOp = StreamSchema.Sprocs.TryHandleRecord.BuildExecuteStoredProcedureOp(
                this.Name,
                operation.Concern,
                operation.Details ?? Invariant($"Created by {nameof(StandardTryHandleRecordOp)}."),
                convertedRecordFilter,
                tagIdsForEntryCsv,
                operation.OrderRecordsBy,
                operation.MinimumInternalRecordId,
                operation.InheritRecordTags,
                operation.StreamRecordItemsToInclude);

            var sqlProtocol = this.BuildSqlOperationsProtocol(sqlServerLocator);
            var sprocResult = sqlProtocol.Execute(storedProcOp);

            int shouldHandle = sprocResult.OutputParameters[nameof(StreamSchema.Sprocs.TryHandleRecord.OutputParamName.ShouldHandle)].GetValueOfType <int>();
            int isBlocked    = sprocResult.OutputParameters[nameof(StreamSchema.Sprocs.TryHandleRecord.OutputParamName.IsBlocked)].GetValueOfType <int>();

            if (shouldHandle != 1)
            {
                return(new TryHandleRecordResult(null, isBlocked == 1));
            }

            long   internalRecordId            = sprocResult.OutputParameters[nameof(StreamSchema.Sprocs.TryHandleRecord.OutputParamName.InternalRecordId)].GetValueOfType <long>();
            int    serializerRepresentationId  = sprocResult.OutputParameters[nameof(StreamSchema.Sprocs.TryHandleRecord.OutputParamName.SerializerRepresentationId)].GetValueOfType <int>();
            int    identifierTypeWithVersionId = sprocResult.OutputParameters[nameof(StreamSchema.Sprocs.TryHandleRecord.OutputParamName.IdentifierTypeWithVersionId)].GetValueOfType <int>();
            int    objectTypeWithVersionId     = sprocResult.OutputParameters[nameof(StreamSchema.Sprocs.TryHandleRecord.OutputParamName.ObjectTypeWithVersionId)].GetValueOfType <int>();
            string stringSerializedId          = sprocResult.OutputParameters[nameof(StreamSchema.Sprocs.TryHandleRecord.OutputParamName.StringSerializedId)].GetValueOfType <string>();
            string stringSerializedObject      = sprocResult.OutputParameters[nameof(StreamSchema.Sprocs.TryHandleRecord.OutputParamName.StringSerializedObject)].GetValueOfType <string>();

            byte[]   binarySerializedObject = sprocResult.OutputParameters[nameof(StreamSchema.Sprocs.TryHandleRecord.OutputParamName.BinarySerializedObject)].GetValueOfType <byte[]>();
            DateTime recordTimestampRaw     = sprocResult.OutputParameters[nameof(StreamSchema.Sprocs.TryHandleRecord.OutputParamName.RecordDateTime)].GetValueOfType <DateTime>();
            DateTime?objectTimestampRaw     = sprocResult.OutputParameters[nameof(StreamSchema.Sprocs.TryHandleRecord.OutputParamName.ObjectDateTime)].GetValueOfType <DateTime?>();
            string   recordTagIdsCsv        = sprocResult.OutputParameters[nameof(StreamSchema.Sprocs.TryHandleRecord.OutputParamName.TagIdsCsv)].GetValueOfType <string>();

            var identifiedSerializerRepresentation = this.GetSerializerRepresentationFromId(sqlServerLocator, serializerRepresentationId);
            var identifierType = this.GetTypeById(sqlServerLocator, identifierTypeWithVersionId, true);
            var objectType     = this.GetTypeById(sqlServerLocator, objectTypeWithVersionId, true);
            var tagIds         = recordTagIdsCsv?.FromCsv().Select(_ => long.Parse(_, CultureInfo.InvariantCulture)).ToList();
            var tags           = tagIds == null ? null : this.GetTagsByIds(sqlServerLocator, tagIds);

            var recordTimestamp = recordTimestampRaw.ToUtc();

            var objectTimestamp = objectTimestampRaw == null ? (DateTime?)null : objectTimestampRaw.ToUtc();

            var metadata = new StreamRecordMetadata(
                stringSerializedId,
                identifiedSerializerRepresentation.SerializerRepresentation,
                identifierType,
                objectType,
                tags,
                recordTimestamp,
                objectTimestamp);

            DescribedSerializationBase payload;

            if (operation.StreamRecordItemsToInclude == StreamRecordItemsToInclude.MetadataAndPayload)
            {
                switch (identifiedSerializerRepresentation.SerializationFormat)
                {
                case SerializationFormat.Binary:
                    payload = new BinaryDescribedSerialization(
                        objectType.WithVersion,
                        identifiedSerializerRepresentation.SerializerRepresentation,
                        binarySerializedObject);
                    break;

                case SerializationFormat.String:
                    payload = new StringDescribedSerialization(
                        objectType.WithVersion,
                        identifiedSerializerRepresentation.SerializerRepresentation,
                        stringSerializedObject);
                    break;

                default:
                    throw new NotSupportedException(
                              Invariant($"{nameof(SerializationFormat)} {identifiedSerializerRepresentation.SerializationFormat} is not supported."));
                }
            }
            else
            {
                payload = new NullDescribedSerialization(metadata.TypeRepresentationOfObject.WithVersion, metadata.SerializerRepresentation);
            }

            var record = new StreamRecord(internalRecordId, metadata, payload);
            var result = new TryHandleRecordResult(record);

            return(result);
        }
        public override StreamRecord Execute(
            StandardGetLatestRecordOp operation)
        {
            operation.MustForArg(nameof(operation)).NotBeNull();

            var sqlServerLocator      = this.TryGetLocator(operation);
            var convertedRecordFilter = this.ConvertRecordFilter(operation.RecordFilter, sqlServerLocator);

            var storedProcOp = StreamSchema.Sprocs.GetLatestRecord.BuildExecuteStoredProcedureOp(
                this.Name,
                convertedRecordFilter,
                operation.StreamRecordItemsToInclude);

            var sqlProtocol = this.BuildSqlOperationsProtocol(sqlServerLocator);
            var sprocResult = sqlProtocol.Execute(storedProcOp);

            long internalRecordId = sprocResult.OutputParameters[nameof(StreamSchema.Sprocs.GetLatestRecord.OutputParamName.InternalRecordId)].GetValueOfType <long>();

            if (internalRecordId == StreamSchema.Tables.Record.NullId)
            {
                switch (operation.RecordNotFoundStrategy)
                {
                case RecordNotFoundStrategy.ReturnDefault:
                    return(null);

                case RecordNotFoundStrategy.Throw:
                    throw new InvalidOperationException(
                              Invariant(
                                  $"Expected stream {this.StreamRepresentation} to contain a matching record for {operation}, none was found and {nameof(operation.RecordNotFoundStrategy)} is '{operation.RecordNotFoundStrategy}'."));

                default:
                    throw new NotSupportedException(
                              Invariant($"{nameof(RecordNotFoundStrategy)} {operation.RecordNotFoundStrategy} is not supported."));
                }
            }

            int    serializerRepresentationId  = sprocResult.OutputParameters[nameof(StreamSchema.Sprocs.GetLatestRecord.OutputParamName.SerializerRepresentationId)].GetValueOfType <int>();
            int    identifierTypeWithVersionId = sprocResult.OutputParameters[nameof(StreamSchema.Sprocs.GetLatestRecord.OutputParamName.IdentifierTypeWithVersionId)].GetValueOfType <int>();
            int    objectTypeWithVersionId     = sprocResult.OutputParameters[nameof(StreamSchema.Sprocs.GetLatestRecord.OutputParamName.ObjectTypeWithVersionId)].GetValueOfType <int>();
            string stringSerializedId          = sprocResult.OutputParameters[nameof(StreamSchema.Sprocs.GetLatestRecord.OutputParamName.StringSerializedId)].GetValueOfType <string>();
            string stringSerializedObject      = sprocResult.OutputParameters[nameof(StreamSchema.Sprocs.GetLatestRecord.OutputParamName.StringSerializedObject)].GetValueOfType <string>();

            byte[]   binarySerializedObject = sprocResult.OutputParameters[nameof(StreamSchema.Sprocs.GetLatestRecord.OutputParamName.BinarySerializedObject)].GetValueOfType <byte[]>();
            DateTime recordTimestampRaw     = sprocResult.OutputParameters[nameof(StreamSchema.Sprocs.GetLatestRecord.OutputParamName.RecordDateTime)].GetValueOfType <DateTime>();
            DateTime?objectTimestampRaw     = sprocResult.OutputParameters[nameof(StreamSchema.Sprocs.GetLatestRecord.OutputParamName.ObjectDateTime)].GetValueOfType <DateTime?>();
            string   tagIdsCsv = sprocResult.OutputParameters[nameof(StreamSchema.Sprocs.GetLatestRecord.OutputParamName.TagIdsCsv)].GetValueOfType <string>();

            var identifiedSerializerRepresentation = this.GetSerializerRepresentationFromId(sqlServerLocator, serializerRepresentationId);
            var identifierType = this.GetTypeById(sqlServerLocator, identifierTypeWithVersionId, true);
            var objectType     = this.GetTypeById(sqlServerLocator, objectTypeWithVersionId, true);
            var tagIds         = tagIdsCsv?.FromCsv().Select(_ => long.Parse(_, CultureInfo.InvariantCulture)).ToList();
            var tags           = tagIds == null ? null : this.GetTagsByIds(sqlServerLocator, tagIds);

            var recordTimestamp = recordTimestampRaw.ToUtc();

            var objectTimestamp = objectTimestampRaw == null ? (DateTime?)null : objectTimestampRaw.ToUtc();

            var metadata = new StreamRecordMetadata(
                stringSerializedId,
                identifiedSerializerRepresentation.SerializerRepresentation,
                identifierType,
                objectType,
                tags,
                recordTimestamp,
                objectTimestamp);

            DescribedSerializationBase payload;

            if (operation.StreamRecordItemsToInclude == StreamRecordItemsToInclude.MetadataAndPayload)
            {
                switch (identifiedSerializerRepresentation.SerializationFormat)
                {
                case SerializationFormat.Binary:
                    payload = new BinaryDescribedSerialization(
                        objectType.WithVersion,
                        identifiedSerializerRepresentation.SerializerRepresentation,
                        binarySerializedObject);
                    break;

                case SerializationFormat.String:
                    payload = new StringDescribedSerialization(
                        objectType.WithVersion,
                        identifiedSerializerRepresentation.SerializerRepresentation,
                        stringSerializedObject);
                    break;

                default:
                    throw new NotSupportedException(
                              Invariant($"{nameof(SerializationFormat)} {identifiedSerializerRepresentation.SerializationFormat} is not supported."));
                }
            }
            else
            {
                payload = new NullDescribedSerialization(metadata.TypeRepresentationOfObject.WithVersion, metadata.SerializerRepresentation);
            }

            var result = new StreamRecord(internalRecordId, metadata, payload);

            return(result);
        }