public static void File_exists__Gets_deserialized() { // Arrange var expected = new Contains { Property = new DerivedProperty { PropertyOnBase = "propBase", PropertyOnDerived = "propDerived", }, }; var configurationSerializerRepresentation = new SerializerRepresentation(SerializationKind.Json, typeof(PropertyJsonConfig).ToRepresentation()); var serializer = new ObcJsonSerializer(typeof(PropertyJsonConfig).ToJsonSerializationConfigurationType()); var expectedJson = serializer.SerializeToString(expected); // Act - Method specified serialization. Config.Reset(); var actualViaParameterMethodSpecific = Config.Get(expected.GetType(), configurationSerializerRepresentation); var actualJsonViaParameterMethodSpecific = serializer.SerializeToString(actualViaParameterMethodSpecific); var actualViaGenericMethodSpecific = Config.Get(expected.GetType(), configurationSerializerRepresentation); var actualJsonViaGenericMethodSpecific = serializer.SerializeToString(actualViaGenericMethodSpecific); // Act - Default via type parameter. var exceptionGet = Record.Exception(() => { Config.Reset(); return(Config.Get(expected.GetType())); }); // Act - Default via generic call. var exceptionGetGeneric = Record.Exception(() => { Config.Reset(); return(Config.Get <Contains>()); }); // Act - Global specified Config.Reset(); Config.SetSerialization(configurationSerializerRepresentation); var actualViaParameterGlobal = Config.Get(expected.GetType()); var actualJsonViaParameterGlobal = serializer.SerializeToString(actualViaParameterGlobal); var actualViaGenericGlobal = Config.Get <Contains>(); var actualJsonViaGenericGlobal = serializer.SerializeToString(actualViaGenericGlobal); // Assert exceptionGet.Should().NotBeNull(); exceptionGet.InnerException.Should().NotBeNull(); exceptionGet.InnerException.Message.Should().StartWith("Could not create an instance of type Naos.Configuration.Test.IBase. Type is an interface or abstract class and cannot be instantiated."); exceptionGetGeneric.Should().NotBeNull(); exceptionGetGeneric.Message.Should().StartWith("Could not create an instance of type Naos.Configuration.Test.IBase. Type is an interface or abstract class and cannot be instantiated."); actualJsonViaParameterMethodSpecific.Should().Be(expectedJson); actualJsonViaGenericMethodSpecific.Should().Be(expectedJson); actualJsonViaParameterGlobal.Should().Be(expectedJson); actualJsonViaGenericGlobal.Should().Be(expectedJson); }
public static void Presence_of_concreteType_token_does_not_interfere_with_normal_deserialization() { // Arrange var expectedId = "year-field"; var expectedDecimalPlaces = 5; var expectedTitle = "my-title"; var year = new YearField(expectedId) { NumberOfDecimalPlaces = expectedDecimalPlaces, Title = expectedTitle, }; var serializer = new ObcJsonSerializer(typeof(TypesToRegisterJsonSerializationConfiguration <Field>).ToJsonSerializationConfigurationType()); var jsonWithConcreteType = serializer.SerializeToString(year); var settings = new NullJsonSerializationConfiguration().BuildJsonSerializerSettings(SerializationDirection.Deserialize, (JsonSerializationConfigurationBase)serializer.SerializationConfiguration); settings.Converters = new JsonConverter[0]; // Act var actual = JsonConvert.DeserializeObject <YearField>(jsonWithConcreteType, settings); // Assert typeof(YearField).IsAssignableTo(typeof(Field)).Should().BeTrue(); jsonWithConcreteType.Should().Contain("$concreteType"); actual.Id.Should().Be(expectedId); actual.NumberOfDecimalPlaces.Should().Be(expectedDecimalPlaces); actual.Title.Should().Be(expectedTitle); }
public static void ObcJsonSerializer___With_type_Default___Uses_default() { // Arrange var property1 = A.Dummy <string>(); var property2 = A.Dummy <string>(); var property3 = A.Dummy <string>(); var expected = "{" + Environment.NewLine + Invariant($" \"property1\": \"{property1}\",") + Environment.NewLine + Invariant($" \"property2\": \"{property2}\",") + Environment.NewLine + Invariant($" \"property3\": \"{property3}\",") + Environment.NewLine + " \"property4\": null" + Environment.NewLine + "}"; var test = new TestObject { Property1 = property1, Property2 = property2, Property3 = property3, }; var serializer = new ObcJsonSerializer(); // Act var actual = serializer.SerializeToString(test); // Assert actual.Should().Be(expected); }
public static void ObcJsonSerializer___With_type_Compact___Uses_compact() { // Arrange var property1 = A.Dummy <string>(); var property2 = A.Dummy <string>(); var property3 = A.Dummy <string>(); var expected = "{" + Invariant($"\"property1\":\"{property1}\",") + Invariant($"\"property2\":\"{property2}\",") + Invariant($"\"property3\":\"{property3}\",") + "\"property4\":null" + "}"; var test = new TestObject { Property1 = property1, Property2 = property2, Property3 = property3, }; var serializer = new ObcJsonSerializer <CompactFormatJsonSerializationConfiguration <NullJsonSerializationConfiguration> >(); // Act var actual = serializer.SerializeToString(test); // Assert actual.Should().Be(expected); }
public void TestConfigStreamBuilding() { var localStreamName = "MyStream"; var x = new SqlServerStreamConfig( localStreamName, StreamAccessKinds.Read, TimeSpan.FromSeconds(30), TimeSpan.FromSeconds(30), new SerializerRepresentation( SerializationKind.Bson, typeof(SqlServerBsonSerializationConfiguration).ToRepresentation().RemoveAssemblyVersions()), SerializationFormat.String, new[] { new SqlServerLocator("localhost", "MyDatabase", "user", "password"), }); var serializer = new ObcJsonSerializer(typeof(SqlServerJsonSerializationConfiguration).ToJsonSerializationConfigurationType()); var output = serializer.SerializeToString(x); var stream = Config.GetByName <SqlServerStreamConfig>( localStreamName, new SerializerRepresentation( SerializationKind.Json, typeof(SqlServerJsonSerializationConfiguration).ToRepresentation()), SerializerFactory.Instance) .ToStream(SerializerFactory.Instance); stream.MustForTest().NotBeNull().And().BeOfType <SqlStream>(); }
public static void Serialize___Should_serialize_anonymous_object_using_same_settings_as_non_anonymous_object___When_called() { // Arrange var serializer = new ObcJsonSerializer(typeof(AttemptOnUnregisteredTypeJsonSerializationConfiguration <TypesToRegisterJsonSerializationConfiguration <TestBaseModel> >).ToJsonSerializationConfigurationType()); var model = new { AnonymousProperty = new TestBaseModel[] { new TestDerivedModel { DateTimeProperty = new DateTime(2020, 4, 8, 13, 43, 52, 101, DateTimeKind.Utc), TestEnum = TestEnum.Second, }, }, }; var expected = "{\r\n \"anonymousProperty\": [\r\n {\r\n \"dateTimeProperty\": \"2020-04-08T13:43:52.1010000Z\",\r\n \"testEnum\": \"second\",\r\n \"$concreteType\": \"OBeautifulCode.Serialization.Json.Test.AnonymousObjectsTest+TestDerivedModel, OBeautifulCode.Serialization.Json.Test\"\r\n }\r\n ]\r\n}"; // Act var actual = serializer.SerializeToString(model); // Assert actual.AsTest().Must().BeEqualTo(expected); }
public static void RawSubject_Roundtrips() { // Arrange var expected = A.Dummy <RawSubject>(); // Act var actualBsonString = BsonSerializer.SerializeToString(expected); var actualBson = BsonSerializer.Deserialize <RawSubject>(actualBsonString); var actualJsonString = JsonSerializer.SerializeToString(expected); var actualJson = JsonSerializer.Deserialize <RawSubject>(actualJsonString); // Assert actualBson.Should().Be(expected); actualJson.Should().Be(expected); }
public static void LambdaSerializer__Passes_through() { // Arrange var property1 = A.Dummy<string>(); var property2 = A.Dummy<string>(); var property3 = A.Dummy<string>(); var expected = "{" + Environment.NewLine + Invariant($" \"property1\": \"{property1}\",") + Environment.NewLine + Invariant($" \"property2\": \"{property2}\",") + Environment.NewLine + Invariant($" \"property3\": \"{property3}\",") + Environment.NewLine + " \"property4\": null" + Environment.NewLine + "}"; var test = new TestObjectForLambda { Property1 = property1, Property2 = property2, Property3 = property3, }; var backingSerializer = new ObcJsonSerializer(); var serializer = new ObcLambdaBackedSerializer( backingSerializer.SerializeToString, backingSerializer.Deserialize, backingSerializer.SerializeToBytes, backingSerializer.Deserialize); // Act var stringOut = serializer.SerializeToString(test); var stringRoundTrip = serializer.Deserialize(stringOut, test.GetType()); var bytesOut = serializer.SerializeToBytes(stringRoundTrip); var bytesRoundTrip = serializer.Deserialize(bytesOut, stringRoundTrip.GetType()); var actual = backingSerializer.SerializeToString(bytesRoundTrip); // Assert actual.Should().Be(expected); stringOut.Should().Be(expected); }
public static void TestBase() { var serializer = new ObcJsonSerializer(typeof(TypesToRegisterJsonSerializationConfiguration <TestBase>).ToJsonSerializationConfigurationType()); var tasks = Enumerable.Range(1, 100).Select(_ => A.Dummy <TestBase>()) .Select(_ => new Task(() => serializer.SerializeToString(_))).ToArray(); Parallel.ForEach(tasks, _ => _.Start()); Task.WaitAll(tasks); }
public static void RootFirstWorks() { var middleSerializer = new ObcJsonSerializer(typeof(ConfigMiddle).ToJsonSerializationConfigurationType()); middleSerializer.SerializeToString(null); var topSerializer = new ObcJsonSerializer(typeof(ConfigTop).ToJsonSerializationConfigurationType()); topSerializer.SerializeToString(null); }
public static void Roundtrip_DefaultPackageDescription() { // Arrange var expected = default(PackageDescription); var jsonSerializer = new ObcJsonSerializer <PackagingJsonSerializationConfiguration>(); // Act var actualJsonString = jsonSerializer.SerializeToString(expected); var actualFromJsonString = jsonSerializer.Deserialize <PackageDescription>(actualJsonString); // Assert actualFromJsonString.Should().Be(expected); }
public void Retrieve___Should_do_something___When_called() { var sqlServerLocator = new SqlServerLocator("localhost", "Streams", "sa", "password", "SQLDEV2017"); var connectionString = sqlServerLocator.BuildConnectionString(TimeSpan.FromSeconds(100)); var output = SqlServerDatabaseManager.Retrieve(connectionString); output.MustForTest().NotBeNull(); var jsonSerializer = new ObcJsonSerializer(); var json = jsonSerializer.SerializeToString(output); this.testOutputHelper.WriteLine(json); }
public static void ProcessDetails_Roundtrips() { // Arrange var expected = A.Dummy <ProcessDetails>(); var bsonSerializer = new ObcBsonSerializer(typeof(DiagnosticsBsonSerializationConfiguration).ToBsonSerializationConfigurationType()); var jsonSerializer = new ObcJsonSerializer(typeof(DiagnosticsJsonSerializationConfiguration).ToJsonSerializationConfigurationType()); // Act var actualBsonString = bsonSerializer.SerializeToString(expected); var actualBson = bsonSerializer.Deserialize <ProcessDetails>(actualBsonString); var actualJsonString = jsonSerializer.SerializeToString(expected); var actualJson = jsonSerializer.Deserialize <ProcessDetails>(actualJsonString); // Assert actualBson.Should().Be(expected); actualJson.Should().Be(expected); }
public static void Roundtrip_DummyPackage() { // Arrange var expected = A.Dummy <Package>(); var bsonSerializer = new ObcBsonSerializer <PackagingBsonSerializationConfiguration>(); var jsonSerializer = new ObcJsonSerializer <PackagingJsonSerializationConfiguration>(); // Act var actualBsonString = bsonSerializer.SerializeToString(expected); var actualFromBsonString = bsonSerializer.Deserialize <Package>(actualBsonString); var actualJsonString = jsonSerializer.SerializeToString(expected); var actualFromJsonString = jsonSerializer.Deserialize <Package>(actualJsonString); // Assert actualFromBsonString.Should().Be(expected); actualFromJsonString.Should().Be(expected); }
public static void Roundtrip_DefaultPropertiesPackageRepositoryConfiguration() { // Arrange var expected = new PackageRepositoryConfiguration(); var bsonSerializer = new ObcBsonSerializer <PackagingBsonSerializationConfiguration>(); var jsonSerializer = new ObcJsonSerializer <PackagingJsonSerializationConfiguration>(); // Act var actualBsonString = bsonSerializer.SerializeToString(expected); var actualFromBsonString = bsonSerializer.Deserialize <PackageRepositoryConfiguration>(actualBsonString); var actualJsonString = jsonSerializer.SerializeToString(expected); var actualFromJsonString = jsonSerializer.Deserialize <PackageRepositoryConfiguration>(actualJsonString); // Assert actualFromBsonString.Should().Be(expected); actualFromJsonString.Should().Be(expected); }
public static void Roundtrip_DefaultScheduleBase() { // Arrange var expected = default(ScheduleBase); var bsonSerializer = new ObcBsonSerializer <CronBsonSerializationConfiguration>(); var jsonSerializer = new ObcJsonSerializer <CronJsonSerializationConfiguration>(); // Act var actualBsonString = bsonSerializer.SerializeToString(expected); var actualFromBsonString = bsonSerializer.Deserialize <ScheduleBase>(actualBsonString); var actualJsonString = jsonSerializer.SerializeToString(expected); var actualFromJsonString = jsonSerializer.Deserialize <ScheduleBase>(actualJsonString); // Assert actualFromBsonString.Should().Be(expected); actualFromJsonString.Should().Be(expected); }