public async Task Serialize_ResourceCollectionDocument_for_primary_data_only()
        {
            var primaryData1 = new Mock <IResourceObject>(MockBehavior.Strict);
            var primaryData2 = new Mock <IResourceObject>(MockBehavior.Strict);
            var mockResourceObjectFormatter = new Mock <IResourceObjectFormatter>(MockBehavior.Strict);

            mockResourceObjectFormatter.Setup(m => m.Serialize(primaryData1.Object, It.IsAny <JsonWriter>()))
            .Returns((IResourceObject resourceObject, JsonWriter writer) =>
            {
                writer.WriteValue("Primary data 1");
                return(Task.FromResult(0));
            });
            mockResourceObjectFormatter.Setup(m => m.Serialize(primaryData2.Object, It.IsAny <JsonWriter>()))
            .Returns((IResourceObject resourceObject, JsonWriter writer) =>
            {
                writer.WriteValue("Primary data 2");
                return(Task.FromResult(0));
            });

            var primaryData = new[] { primaryData1.Object, primaryData2.Object };
            IResourceCollectionDocument document = new ResourceCollectionDocument(primaryData, null, null);

            var formatter = new ResourceCollectionDocumentFormatter(mockResourceObjectFormatter.Object, null);

            await AssertSerializeOutput(formatter, document, "Json/Fixtures/ResourceCollectionDocumentFormatter/Serialize_ResourceCollectionDocument_for_primary_data_only.json");
        }
        public async Task Serialize_ResourceCollectionDocument_for_all_possible_members()
        {
            var primaryData1     = new Mock <IResourceObject>(MockBehavior.Strict);
            var primaryData2     = new Mock <IResourceObject>(MockBehavior.Strict);
            var relatedResource1 = new Mock <IResourceObject>(MockBehavior.Strict);
            var relatedResource2 = new Mock <IResourceObject>(MockBehavior.Strict);
            var relatedResource3 = new Mock <IResourceObject>(MockBehavior.Strict);

            var mockResourceObjectFormatter = new Mock <IResourceObjectFormatter>(MockBehavior.Strict);

            mockResourceObjectFormatter.Setup(m => m.Serialize(primaryData1.Object, It.IsAny <JsonWriter>()))
            .Returns((IResourceObject resourceObject, JsonWriter writer) =>
            {
                writer.WriteValue("Primary data 1");
                return(Task.FromResult(0));
            });
            mockResourceObjectFormatter.Setup(m => m.Serialize(primaryData2.Object, It.IsAny <JsonWriter>()))
            .Returns((IResourceObject resourceObject, JsonWriter writer) =>
            {
                writer.WriteValue("Primary data 2");
                return(Task.FromResult(0));
            });
            mockResourceObjectFormatter.Setup(m => m.Serialize(relatedResource1.Object, It.IsAny <JsonWriter>()))
            .Returns((IResourceObject resourceObject, JsonWriter writer) =>
            {
                writer.WriteValue("Related data object 1");
                return(Task.FromResult(0));
            });
            mockResourceObjectFormatter.Setup(m => m.Serialize(relatedResource2.Object, It.IsAny <JsonWriter>()))
            .Returns((IResourceObject resourceObject, JsonWriter writer) =>
            {
                writer.WriteValue("Related data object 2");
                return(Task.FromResult(0));
            });
            mockResourceObjectFormatter.Setup(m => m.Serialize(relatedResource3.Object, It.IsAny <JsonWriter>()))
            .Returns((IResourceObject resourceObject, JsonWriter writer) =>
            {
                writer.WriteValue("Related data object 3");
                return(Task.FromResult(0));
            });

            var mockMetadata          = new Mock <IMetadata>(MockBehavior.Strict);
            var mockMetadataFormatter = new Mock <IMetadataFormatter>(MockBehavior.Strict);

            mockMetadataFormatter.Setup(m => m.Serialize(mockMetadata.Object, It.IsAny <JsonWriter>()))
            .Returns((IMetadata resourceObject, JsonWriter writer) =>
            {
                writer.WriteValue("Placeholder metadata object");
                return(Task.FromResult(0));
            });

            var primaryData      = new[] { primaryData1.Object, primaryData2.Object };
            var relatedResources = new[] { relatedResource1.Object, relatedResource2.Object, relatedResource3.Object };
            IResourceCollectionDocument document = new ResourceCollectionDocument(primaryData, relatedResources, mockMetadata.Object);

            var formatter = new ResourceCollectionDocumentFormatter(mockResourceObjectFormatter.Object, mockMetadataFormatter.Object);

            await AssertSerializeOutput(formatter, document, "Json/Fixtures/ResourceCollectionDocumentFormatter/Serialize_ResourceCollectionDocument_for_all_possible_members.json");
        }
        public IResourceCollectionDocument BuildDocument <TModel>(IEnumerable <TModel> primaryData, string linkBaseUrl, string[] includePathExpressions, IMetadata metadata,
                                                                  IDictionary <object, IMetadata> resourceMetadata = null)
        {
            var idDictionariesByType = new Dictionary <string, IDictionary <string, ResourceObject> >();
            var primaryDataResources =
                primaryData.Select(d => (IResourceObject)CreateResourceObject(d, idDictionariesByType, null, includePathExpressions, linkBaseUrl, resourceMetadata))
                .ToArray();

            var relatedData = idDictionariesByType.Values.SelectMany(d => d.Values).Cast <IResourceObject>().ToArray();
            var document    = new ResourceCollectionDocument(primaryDataResources, relatedData, metadata);

            return(document);
        }