コード例 #1
0
        public void File_add_throws_when_owner_now_found()
        {
            // Arrange
            FakeFileStorage         fileStorage         = new FakeFileStorage();
            FakeFileRepository      fileRepository      = new FakeFileRepository();
            FakeEventPublisher      eventPublisher      = new FakeEventPublisher();
            FakeCurrentUserSource   currentUserSource   = new FakeCurrentUserSource();
            FakePostCommitRegistrar postCommitRegistrar = new FakePostCommitRegistrar();

            currentUserSource.CurrentUser = null;

            var commandHandler = new AddFileCommandHandler(
                fileRepository, fileStorage, eventPublisher, postCommitRegistrar, currentUserSource);

            var contentBuffer = Encoding.UTF8.GetBytes("testContent");

            using (Stream fileContent = new MemoryStream(contentBuffer))
            {
                var command = new AddFileCommand("filename", "desc", "text/plain", fileContent);

                // Assert & Act
                Assert.Throws <NotFoundException>(() =>
                {
                    commandHandler.Handle(command);
                    postCommitRegistrar.ExecuteActions();
                });
            }
        }
コード例 #2
0
        public void File_add_adds_file_and_publishes_event()
        {
            // Arrange
            FakeFileStorage         fileStorage         = new FakeFileStorage();
            FakeFileRepository      fileRepository      = new FakeFileRepository();
            FakeEventPublisher      eventPublisher      = new FakeEventPublisher();
            FakeCurrentUserSource   currentUserSource   = new FakeCurrentUserSource();
            FakePostCommitRegistrar postCommitRegistrar = new FakePostCommitRegistrar();

            currentUserSource.CurrentUser = new User("userId", "username");

            var commandHandler = new AddFileCommandHandler(
                fileRepository, fileStorage, eventPublisher, postCommitRegistrar, currentUserSource);

            var contentBuffer = Encoding.UTF8.GetBytes("testContent");

            using (Stream fileContent = new MemoryStream(contentBuffer))
            {
                var command = new AddFileCommand("filename", "desc", "text/plain", fileContent);

                // Act
                commandHandler.Handle(command);
                postCommitRegistrar.ExecuteActions();

                // Assert
                File addedFile = fileRepository.GetByName("filename");

                Assert.IsNotNull(addedFile);
                Assert.AreEqual("filename", addedFile.FileName);
                Assert.AreEqual("desc", addedFile.Description);
                Assert.AreEqual("userId", addedFile.Owner.Id);

                Stream fileStream = fileStorage.ReadFile(addedFile).Result;
                Assert.IsNotNull(fileStream);

                using (var reader = new StreamReader(fileStream))
                    Assert.AreEqual("testContent", reader.ReadToEnd());

                File publishedEvent = eventPublisher.VerifyPublishedOnce <File>();
                Assert.AreEqual(addedFile, publishedEvent);
            }
        }