Ejemplo n.º 1
0
        public void ConvertFromDictionaryParametersSucceeds()
        {
            // Arrange
            var extraConversionKeyName = "biz.dfch.CS.Commons.ConversionKey.ThatIsNotDefinedInTheTargetDataTransferObjectAndWillBeIgnoredOnConversion";

            var parameters = new DictionaryParameters
            {
                { STRING_PROPERTY_WITH_ANNOTATION_ANNOTATION, "arbitrary-string-value" }
                ,
                { LONG_PROPERTY_WITH_ANNOTATION_ANNOTATION, 42L }
                ,
                { extraConversionKeyName, "some-other-arbitrary-string-value" }
            };

            // Act
            var result = ConversionKeyConverter.Convert <ClassWithConversionKeyAttributes>(parameters);

            // Assert
            Assert.IsNotNull(result);
            if (!result.IsValid())
            {
                var validationResults = result.GetValidationResults();
                foreach (var validationResult in validationResults)
                {
                    System.Diagnostics.Debug.WriteLine(validationResult.ErrorMessage);
                }
            }
            Assert.IsTrue(result.IsValid());

            Assert.AreEqual("arbitrary-string-value", result.StringPropertyWithAnnotation);
            Assert.AreEqual(42L, result.LongPropertyWithAnnotation);
            Assert.IsNull(result.StringPropertyWithoutAnnotation);
            Assert.AreEqual(0, result.LongPropertyWithoutAnnotation);
        }
Ejemplo n.º 2
0
        public void ConvertingViaBaseDtoSucceeds()
        {
            // Arrange
            const string name        = "arbitrary-name";
            const string description = "optional-description";
            const string size        = "40";

            var StringPropertyWithAnnotationAnnotationValue      = "arbitrary-string";
            var LongPropertyWithAnnotationAnnotationValue        = 42L;
            var StringPropertyWithAnnotationAnnotationType1Value = "arbitrary-string-1";
            var LongPropertyWithAnnotationAnnotationType1Value   = 1L;
            var StringPropertyWithAnnotationAnnotationType2Value = "arbitrary-string-2";
            var LongPropertyWithAnnotationAnnotationType2Value   = 2L;

            var dictionaryParameters = new DictionaryParameters()
            {
                { STRING_PROPERTY_WITH_ANNOTATION_ANNOTATION, StringPropertyWithAnnotationAnnotationValue }
                ,
                { LONG_PROPERTY_WITH_ANNOTATION_ANNOTATION, LongPropertyWithAnnotationAnnotationValue }
                ,
                { STRING_PROPERTY_WITH_ANNOTATION_ANNOTATION_TYPE1, StringPropertyWithAnnotationAnnotationType1Value }
                ,
                { LONG_PROPERTY_WITH_ANNOTATION_ANNOTATION_TYPE1, LongPropertyWithAnnotationAnnotationType1Value }
                ,
                { STRING_PROPERTY_WITH_ANNOTATION_ANNOTATION_TYPE2, StringPropertyWithAnnotationAnnotationType2Value }
                ,
                { LONG_PROPERTY_WITH_ANNOTATION_ANNOTATION_TYPE2, LongPropertyWithAnnotationAnnotationType2Value }
                ,
                { "arbitrary-other-key", "arbitrary-value" }
            };

            // Act
            var result = ConversionKeyConverter.Convert <ClassWithConversionKeyAttributesAsTBase>(dictionaryParameters, null);

            // Assert

            Assert.IsNotNull(result);
            Assert.AreEqual(2, result.Count);

            foreach (var dto in result)
            {
                Assert.IsTrue(dto.IsValid(), dto.GetType().FullName);
            }

            Assert.IsTrue(result[0] is ClassWithConversionKeyAttributesAsDerivedType1 || result[1] is ClassWithConversionKeyAttributesAsDerivedType1);
            Assert.IsTrue(result[0] is ClassWithConversionKeyAttributesAsDerivedType2 || result[1] is ClassWithConversionKeyAttributesAsDerivedType2);

            var type1 = result[0] is ClassWithConversionKeyAttributesAsDerivedType1 ? result[0] : result[1];

            Assert.IsNotNull(type1);
            var type2 = result[0] is ClassWithConversionKeyAttributesAsDerivedType2 ? result[0] : result[1];

            Assert.IsNotNull(type2);

            Assert.AreEqual(StringPropertyWithAnnotationAnnotationType1Value, type1.StringPropertyWithAnnotation);
            Assert.AreEqual(LongPropertyWithAnnotationAnnotationType1Value, type1.LongPropertyWithAnnotation);

            Assert.AreEqual(StringPropertyWithAnnotationAnnotationType2Value, type2.StringPropertyWithAnnotation);
            Assert.AreEqual(LongPropertyWithAnnotationAnnotationType2Value, type2.LongPropertyWithAnnotation);
        }
Ejemplo n.º 3
0
        public void ConvertToDictionaryParametersSucceeds()
        {
            // Arrange
            var sut = new ClassWithConversionKeyAttributes()
            {
                StringPropertyWithAnnotation    = "arbitrary-StringPropertyWithAnnotation",
                LongPropertyWithAnnotation      = 42L,
                StringPropertyWithoutAnnotation = "arbitrary-StringPropertyWithoutAnnotation",
                LongPropertyWithoutAnnotation   = 8L
            };

            // Act
            var result = ConversionKeyConverter.Convert(sut);

            // Assert
            Assert.IsNotNull(result);
            if (!result.IsValid())
            {
                var validationResults = result.GetValidationResults();
                foreach (var validationResult in validationResults)
                {
                    System.Diagnostics.Debug.WriteLine(validationResult.ErrorMessage);
                }
            }
            Assert.IsTrue(result.IsValid());

            Assert.AreEqual(2, result.Keys.Count);

            Assert.IsTrue(result.ContainsKey(STRING_PROPERTY_WITH_ANNOTATION_ANNOTATION));
            Assert.AreEqual("arbitrary-StringPropertyWithAnnotation", result[STRING_PROPERTY_WITH_ANNOTATION_ANNOTATION]);
            Assert.IsTrue(result.ContainsKey(LONG_PROPERTY_WITH_ANNOTATION_ANNOTATION));
            Assert.AreEqual(42L, result[LONG_PROPERTY_WITH_ANNOTATION_ANNOTATION]);
        }
Ejemplo n.º 4
0
        public void ConvertConversionKeyBaseDtoWithNullConversionKeyBaseDtoThrowsContractFailure2()
        {
            // Arrange
            var dictionaryParameters = default(DictionaryParameters);
            var sut = new ConversionKeyConverter();

            // Act
            // ReSharper disable once ExpressionIsAlwaysNull
            var result = ConversionKeyConverter.Convert <ClassWithConversionKeyAttributes>(dictionaryParameters);

            // Assert
            // N/A
        }
Ejemplo n.º 5
0
        public void ConvertConversionKeyBaseDtoWithNullConversionKeyBaseDtoThrowsContractFailure()
        {
            // Arrange
            var ConversionKeyBaseDto = default(ConversionKeyBaseDto);
            var sut = new ConversionKeyConverter();

            // Act
            // ReSharper disable once ExpressionIsAlwaysNull
            var result = ConversionKeyConverter.Convert(ConversionKeyBaseDto);

            // Assert
            // N/A
        }
Ejemplo n.º 6
0
        public void ConverterTestWithStringTypesCanBeConvertedAndSucceeds()
        {
            var sut = new DictionaryParameters()
            {
                { ConversionKeyClass.BAG_NAME_INT, "42" }
                ,
                { ConversionKeyClass.BAG_NAME_LONG, (int.MaxValue + 1L).ToString() }
                ,
                { ConversionKeyClass.BAG_NAME_DOUBLE, 4.2d.ToString() }
            };

            var result = ConversionKeyConverter.Convert <ConversionKeyClass>(sut);

            Assert.IsTrue(result.IsValid());
        }
Ejemplo n.º 7
0
        public void ConverterTestWithMatchingTypesSucceeds()
        {
            var sut = new DictionaryParameters()
            {
                { ConversionKeyClass.BAG_NAME_INT, 42 }
                ,
                { ConversionKeyClass.BAG_NAME_LONG, int.MaxValue + 1L }
                ,
                { ConversionKeyClass.BAG_NAME_DOUBLE, 4.2d }
            };

            var result = ConversionKeyConverter.Convert <ConversionKeyClass>(sut);

            Assert.IsTrue(result.IsValid());
        }
        public void ConvertingFromConversionKeyUtcSucceeds()
        {
            var start = new DateTimeOffset(2016, 4, 1, 1, 2, 3, 0, TimeSpan.FromHours(2));
            var end   = start.AddYears(1).AddMonths(2).AddDays(3).AddHours(4).AddMinutes(5).AddSeconds(6);
            var iso8601DatePeriodUtc = string.Concat(start.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:sszzz"), "/", end.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:sszzz"));

            var parameters = new DictionaryParameters()
            {
                { CONVERSION_KEY_NAME, iso8601DatePeriodUtc }
            };

            var result = ConversionKeyConverter.Convert <ClassWithDateTimeRange>(parameters);

            Assert.AreEqual(iso8601DatePeriodUtc, result.Property.ToString());
            Assert.AreEqual(start, result.Property.Start);
            Assert.AreEqual(end, result.Property.End);
            Assert.AreEqual(end - start, result.Property.TimeSpan);
        }
Ejemplo n.º 9
0
        public void ConvertingConversionKeyBaseDtoToDictionaryParametersSucceeds()
        {
            var sut = new MyConversionKeyBaseDto()
            {
                StringProperty = MyConversionKeyBaseDto.NAME_PROPERTY_VALUE
                ,
                LongProperty = MyConversionKeyBaseDto.VALUE_PROPERTY_VALUE
            };

            Assert.IsTrue(sut.IsValid());

            var parameters = ConversionKeyConverter.Convert(sut);

            Assert.IsNotNull(parameters);
            Assert.AreEqual(2, parameters.Count);
            Assert.IsTrue(parameters.ContainsKey(MyConversionKeyBaseDto.NAME_PROPERTY_NAME));
            Assert.AreEqual(MyConversionKeyBaseDto.NAME_PROPERTY_VALUE, parameters[MyConversionKeyBaseDto.NAME_PROPERTY_NAME]);
            Assert.IsTrue(parameters.ContainsKey(MyConversionKeyBaseDto.VALUE_PROPERTY_NAME));
            Assert.AreEqual(MyConversionKeyBaseDto.VALUE_PROPERTY_VALUE, parameters[MyConversionKeyBaseDto.VALUE_PROPERTY_NAME]);
        }
Ejemplo n.º 10
0
        public void ConvertFromDictionaryParametersWithTypeConversionSucceeds()
        {
            // Arrange
            var parameters = new DictionaryParameters
            {
                { STRING_PROPERTY_WITH_ANNOTATION_ANNOTATION, "arbitrary-string-value" }
                ,
                { LONG_PROPERTY_WITH_ANNOTATION_ANNOTATION, 42L.ToString() }
            };

            // Act
            var result = ConversionKeyConverter.Convert <ClassWithConversionKeyAttributes>(parameters);

            // Assert
            Assert.IsNotNull(result);
            Assert.IsTrue(result.IsValid());

            Assert.AreEqual("arbitrary-string-value", result.StringPropertyWithAnnotation);
            Assert.AreEqual(42L, result.LongPropertyWithAnnotation);
        }