public MockedDocument( string name, string content = null, string id = null, IFolder parent = null, MockBehavior behavior = MockBehavior.Strict) : base(name, id, behavior) { this.ObjectType = this.docType; this.Renditions = new List <IRendition>(); this.parent = parent ?? Mock.Of <IFolder>(); this.Setup(m => m.BaseType).Returns(this.docType); this.Setup(m => m.BaseTypeId).Returns(BaseTypeId.CmisDocument); this.SetupParent(this.parent); this.Setup(m => m.ContentStreamId).Returns(defaultStreamId); this.Setup(m => m.ContentStreamLength).Returns(() => this.Stream == null ? (long?)null : this.Stream.Length); this.Setup(m => m.ContentStreamFileName).Returns(() => this.Stream != null ? this.Stream.FileName : name); this.Setup(m => m.SetContentStream(It.Is <IContentStream>(s => s != null), It.IsAny <bool>())).Callback <IContentStream, bool>((stream, overwrite) => this.SetContent(stream, overwrite)).Returns(() => this.Object); this.Setup(m => m.SetContentStream(It.Is <IContentStream>(s => s != null), It.IsAny <bool>(), It.IsAny <bool>())).Callback <IContentStream, bool, bool>((stream, o, r) => this.SetContent(stream, o)).Returns(() => Mock.Of <IObjectId>(oid => oid.Id == this.Object.Id)); this.Setup(m => m.GetContentStream()).Returns(() => this.Stream); this.Setup(m => m.GetContentStream(It.Is <string>(sid => sid == null || sid.Equals(this.defaultStreamId)), It.IsAny <long>(), It.IsAny <long>())).Returns <string, long?, long?>((sid, offset, length) => { var subStream = new MockedContentStream(this.Stream.FileName, behavior: behavior); using (var orig = this.Stream.Stream) using (var part = new MemoryStream()) { orig.Seek(offset.GetValueOrDefault(), SeekOrigin.Begin); orig.CopyTo(part, 8096, (int)length.GetValueOrDefault()); subStream.Content = part.ToArray(); } return(subStream.Object); }); if (content != null) { this.Stream = new MockedContentStream(name, content, behavior: behavior).Object; } this.Setup(m => m.Renditions).Returns(() => new List <IRendition>(this.Renditions)); this.Setup(m => m.IsPrivateWorkingCopy).Returns(() => this.IsPrivateWorkingCopy); this.Setup(m => m.DeleteContentStream()).Callback(() => { if (this.Stream != null) { this.Stream = null; this.UpdateChangeToken(); this.NotifyChanges(); } }).Returns(() => this.Object); this.Setup(m => m.DeleteContentStream(It.IsAny <bool>())).Callback(() => { if (this.Stream != null) { this.Stream = null; this.UpdateChangeToken(); this.NotifyChanges(); } }).Returns(() => Mock.Of <IObjectId>(oid => oid.Id == this.Object.Id)); this.Setup(m => m.Delete(It.IsAny <bool>())).Callback <bool>((allVersions) => this.Delete(allVersions)); this.Setup(m => m.DeleteAllVersions()).Callback(() => this.Delete(true)); this.Setup(m => m.GetAllVersions()).Returns(() => new List <IDocument>(this.AllVersions)); this.AllVersions = new List <IDocument>(new IDocument[] { this.Object }); }
private void SetContent(IContentStream inputstream, bool overwrite) { if (this.Stream != null && this.Stream.Length != null && this.Stream.Length > 0 && !overwrite) { throw new CmisContentAlreadyExistsException(); } var newContent = new MockedContentStream(inputstream.FileName, null, inputstream.MimeType); using (var memStream = new MemoryStream()) using (var input = inputstream.Stream){ input.CopyTo(memStream); newContent.Content = memStream.ToArray(); } this.Stream = newContent.Object; this.UpdateChangeToken(); this.NotifyChanges(); }