public void Test_Add_Attachments_With_FileController()
        {
            var dataService = DataServiceFactory();

            dataService.Setup(
                ds =>
                ds.GetContentItem(It.IsAny <int>())).Returns(MockHelper.CreateValidContentItemReader);

            // Use a closure to store the metadata locally in this method.
            var data = new Dictionary <string, string>();

            dataService.Setup(
                ds =>
                ds.GetMetaData(It.IsAny <int>())).Returns(
                () => MockHelper.CreateMetaDataReaderFromDictionary(data));

            dataService.Setup(
                ds =>
                ds.AddMetaData(
                    It.IsAny <ContentItem>(),
                    It.IsAny <string>(),
                    It.IsAny <string>()))
            .Callback <ContentItem, string, string>((ci, name, value) => data[name] = value);

            var contentController = Util.GetContentController();

            var contentItem = contentController.GetContentItem(Constants.CONTENT_ValidContentItemId);

            Assert.IsNotNull(contentItem);

            var serialized = contentItem.Metadata[FileController.FilesKey];

            Assert.IsNull(serialized);
            Assert.IsEmpty(contentItem.Files);

            var fileManager = ComponentFactory.GetComponent <IFileManager>();

            // Add some files.
            var fileController = ComponentFactory.GetComponent <IAttachmentController>();

            fileController.AddFileToContent(contentItem.ContentItemId, fileManager.GetFile(0));
            fileController.AddFileToContent(contentItem.ContentItemId, fileManager.GetFile(1));

            contentItem = contentController.GetContentItem(Constants.CONTENT_ValidContentItemId);

            Assert.AreEqual(contentItem.Files.Count, 2);
            Assert.AreEqual(contentItem.Files[0].FileId, 0);
            Assert.AreEqual(contentItem.Files[1].FileId, 1);

            dataService.Verify(
                ds => ds.DeleteMetaData(It.IsAny <ContentItem>(), FileController.FilesKey, "[0]"), Times.Once());

            dataService.Verify(
                ds => ds.AddMetaData(It.IsAny <ContentItem>(), FileController.FilesKey, "[0]"), Times.Once());

            dataService.Verify(
                ds => ds.AddMetaData(It.IsAny <ContentItem>(), FileController.FilesKey, "[0,1]"), Times.Once());
        }
        private static Mock <IDataService> DataServiceFactoryWithLocalMetaData(ref Dictionary <string, string> metadata)
        {
            var dataService = DataServiceFactory();

            var closure = metadata;

            dataService.Setup(ds => ds.GetMetaData(It.IsAny <int>())).Returns(() => MockHelper.CreateMetaDataReaderFromDictionary(closure));
            dataService.Setup(ds => ds.AddMetaData(It.IsAny <ContentItem>(), It.IsAny <string>(), It.IsAny <string>())).
            Callback <ContentItem, string, string>((ci, name, value) => closure[name] = value);
            dataService.Setup(ds => ds.DeleteMetaData(It.IsAny <ContentItem>(), It.IsAny <string>(), It.IsAny <string>()))
            .Callback <ContentItem, string, string>((ci, key, val) => closure.Remove(key));

            return(dataService);
        }