Beispiel #1
0
        public void Should_serialize_playlist_request_to_json()
        {
            var playlistRequest = new PlaylistRequest
            {
                Description = "A New Playlist Description",
                ImageUrl    = "an-image-url",
                Name        = "New Playlist",
                Tracks      = new List <Product> {
                    new Product {
                        TrackId = "12345"
                    },
                    new Product {
                        TrackId = "98765"
                    }
                },
                Status = "Published",
                Tags   = new List <Tag>
                {
                    new Tag {
                        Name = "tag1", PlaylistPosition = 1
                    },
                    new Tag {
                        Name = "tag2"
                    }
                },
                Visibility  = PlaylistVisibilityType.Private,
                Annotations = new List <Annotation>
                {
                    new Annotation("key", "value"),
                    new Annotation("another key", "another value")
                }
            };

            var json = new JsonPayloadSerializer().Serialize(playlistRequest);

            Debug.WriteLine(json);

            var expectedJson = string.Join("",
                                           "{",
                                           "\"tracks\":[{\"trackId\":\"12345\"},{\"trackId\":\"98765\"}],",
                                           "\"name\":\"New Playlist\",",
                                           "\"visibility\":\"Private\",",
                                           "\"status\":\"Published\",",
                                           "\"description\":\"A New Playlist Description\",",
                                           "\"imageUrl\":\"an-image-url\",",
                                           "\"tags\":[{\"name\":\"tag1\",\"playlistPosition\":1},{\"name\":\"tag2\",\"playlistPosition\":null}],",
                                           "\"annotations\":{",
                                           "\"key\":\"value\",",
                                           "\"another key\":\"another value\"",
                                           "}",
                                           "}"
                                           );

            Assert.That(json, Is.EqualTo(expectedJson));
        }
        public void should_serialize_and_deserialize_event_message()
        {
            var changedNameEvent = new ChangedNameEvent("John Snow");
            var serializer = new JsonPayloadSerializer();

            var serializedPayload = serializer.Serialize(changedNameEvent);

            var deserializedPayload = serializer.Deserialize<ChangedNameEvent>(serializedPayload);

            deserializedPayload.Name.Should().Be(changedNameEvent.Name);
        }
        public static IList <JObject> GetThings()
        {
            // Dedup thing list
            var uniqueThings = new HashSet <Thing>();

            foreach (var thing in OcclusionManager.AllThings)
            {
                uniqueThings.Add(thing);
            }

            //return uniqueThings.Select(thing => ThingPayload.FromThing(thing)).ToList();
            return(uniqueThings.Select(thing => JsonPayloadSerializer.ObjectToPayload(thing)).ToList());
        }
        public void should_serialize_deserialize_aggregate_root_from_stream()
        {
            var id = A.RandomGuid();
            var aggregate = Person.CreateNew(id);
            aggregate.ChangeAge(29);
            aggregate.ChangeName("John Snow");
            var serializer = new JsonPayloadSerializer();

            var bytes = serializer.Serialize(aggregate);

            var deserializedPayload = serializer.Deserialize<Person>(bytes);

            deserializedPayload.Age.Should().Be(aggregate.Age);
            deserializedPayload.Name.Should().Be(aggregate.Name);
        }
Beispiel #5
0
        private static void SaveTest()
        {
            var note = Note.CreateNew(Guid.NewGuid(), "Be Da Best", "Do Or Die");

            note.ChangeTitle("Terefere");
            note.ChangeTitle("JABADABADU");

            var jsonSerializer = new JsonPayloadSerializer();
            var simpleDataPersistence = new SimpleDataPersistenceEngine("SD_ES", jsonSerializer);
            var store = new EventStore(simpleDataPersistence);
            using (var stream = store.CreateStream("ST1"))
            {
                stream.AttachAggregate(note);

                stream.CommitChanges(Guid.NewGuid());
            }
        }
 public void SetUp()
 {
     _payloadSerializer = new JsonPayloadSerializer();
 }
Beispiel #7
0
        /// <summary>
        /// Returns the payload to be used for this test case and the specified test configuration.
        /// </summary>
        /// <param name="testConfiguration">The test configuration to use.</param>
        /// <returns>The payload to use for testing.</returns>
        public static byte[] GetPayload(
            ReaderTestConfiguration testConfiguration,
            List <Func <ReaderTestConfiguration, Func <ODataPayloadElement, ODataPayloadElement> > > payloadNormalizers,
            PayloadReaderTestDescriptor.Settings settings,
            ODataPayloadElement payloadElement)
        {
            IPayloadSerializer payloadSerializer = null;

            // Apply all payload element transforms before serialization.
            IPayloadTransform <ODataPayloadElement> payloadElementTransform = settings.PayloadTransformFactory.GetTransform <ODataPayloadElement>();
            ODataPayloadElement transformedODataElement = null;

            if (payloadElementTransform.TryTransform(payloadElement, out transformedODataElement))
            {
                payloadElement = transformedODataElement;
            }

            ODataPayloadElement payloadElementToSerialize = payloadElement;

            // Apply all normalizers/fixups before serialization
            if (payloadNormalizers != null)
            {
                ODataPayloadElement payloadElementCopy = null;
                foreach (var getPayloadNormalizerFunc in payloadNormalizers)
                {
                    var normalizer = getPayloadNormalizerFunc(testConfiguration);
                    if (normalizer != null)
                    {
                        if (payloadElementCopy == null)
                        {
                            payloadElementCopy = payloadElementToSerialize.DeepCopy();
                        }

                        payloadElementCopy = normalizer(payloadElementCopy);
                    }
                }

                payloadElementToSerialize = payloadElementCopy ?? payloadElementToSerialize;
            }

            if (testConfiguration.Format == ODataFormat.Atom)
            {
                payloadSerializer = new XmlPayloadSerializer(settings.PayloadElementToXmlConverter);
            }
            else if (testConfiguration.Format == ODataFormat.Json)
            {
                // Create a copy of the payload element so that we can add annotations to it.
                payloadElementToSerialize = payloadElementToSerialize.DeepCopy();

                // Annotate elements with version and response/request as appropriate
                PayloadFormatVersionAnnotatingVisitor.AnnotateJsonLight(
                    payloadElementToSerialize,
                    testConfiguration.Version.ToDataServiceProtocolVersion(),
                    testConfiguration.IsRequest);

                payloadSerializer = new JsonPayloadSerializer(settings.PayloadElementToJsonLightConverter.ConvertToJsonLight);
            }
            else if (testConfiguration.Format == null)
            {
                if (payloadElementToSerialize.ElementType == ODataPayloadElementType.PrimitiveValue)
                {
                    PrimitiveValue primitiveValue = (PrimitiveValue)payloadElementToSerialize;
                    if (primitiveValue.ClrValue == null)
                    {
                        throw new NotSupportedException("Reading null values is not supported (since we don't support writing null values).");
                    }
                    else if (primitiveValue.ClrValue.GetType() == typeof(byte[]))
                    {
                        payloadSerializer = settings.BinaryValuePayloadElementConverter;
                    }
                    else
                    {
                        payloadSerializer = settings.TextValuePayloadElementConverter;
                    }
                }
                else if (payloadElementToSerialize.ElementType == ODataPayloadElementType.BatchRequestPayload || payloadElementToSerialize.ElementType == ODataPayloadElementType.BatchResponsePayload)
                {
                    return(SerializeBatchPayload(payloadElementToSerialize, settings));
                }
                else
                {
                    throw new NotImplementedException("Default format not yet implemented for payload test descriptor and payload element type '" + payloadElementToSerialize.ElementType + "'.");
                }
            }
            else
            {
                throw new NotSupportedException("Unexpected format.");
            }

            // Default encoding is UTF8
            return(payloadSerializer.SerializeToBinary(payloadElementToSerialize, null));
        }
		public void SetUp()
		{
			_payloadSerializer = new JsonPayloadSerializer();
		}