public void delete_returns_409_NoContent_whend_document_deleted(
            Guid documentId,
            DocumentModel document,
            DocumentLockedException exception,
            DocumentsController sut)
        {
            var result = sut.Delete(documentId);

            result.Should().BeOfType<ResponseMessageResult>()
                  .Which.Response.StatusCode.Should().Be(HttpStatusCode.NoContent);
        }
        public void delete_returns_409_Conflict_on_lock_exception(
            Guid documentId,
            DocumentModel document,
            DocumentLockedException exception,
            [Frozen]Mock<ICommand<DocumentReference>> deleteCmd,
            DocumentsController sut)
        {
            deleteCmd.Setup(c => c.Execute(It.IsAny<Envelope<DocumentReference>>()))
                     .Throws(exception);

            var result = sut.Delete(documentId);

            result.Should().BeOfType<ResponseMessageResult>()
                  .Which.Response.StatusCode.Should().Be(HttpStatusCode.Conflict, "because document lock exception was thrown");

            result.Should().BeOfType<ResponseMessageResult>()
                  .Which.Response.Content.ReadAsStringAsync().Result.Should().NotBeNullOrEmpty();
        }
        public void post_returns_correct_result_when_unauthorized(
            DocumentModel document,
            [Frozen]Mock<ICommand<Document>> cmd,
            DocumentsController sut)
        {
            cmd.Setup(c => c.Execute(It.IsAny<Envelope<Document>>())).Throws<UnauthorizedAccessException>();

            var response = sut.Post(document);

            response.Should().BeOfType<UnauthorizedResult>("because auth header was not specified");
        }
        public void post_returns_400_BadRequest_on_validation_exception(
            DocumentModel document,
            ValidationException exception,
            [Frozen]Mock<ICommand<Document>> submitNewCmd,
            DocumentsController sut)
        {
            submitNewCmd.Setup(c => c.Execute(It.IsAny<Envelope<Document>>()))
                        .Throws(exception);

            var result = sut.Post(document);

            result.Should().BeOfType<BadRequestErrorMessageResult>()
                .Which.Message.Should().Be(exception.Message);
        }
        public void post_returns_201_Created_when_command_succeeds(
            DocumentModel document,
            [Frozen]Mock<UrlHelper> url,
            DocumentsController sut)
        {
            var createdUri = "http://localhost:8051/api/documents/123";
            url.Setup(u => u.Link(It.IsAny<string>(), It.IsAny<object>()))
                            .Returns(createdUri);

            var result = sut.Post(document);

            result.Should().BeOfType<CreatedNegotiatedContentResult<DocumentResponseModel>>()
                .Which.Content.ShouldBeEquivalentTo(document, options => options.ExcludingMissingMembers());
        }
        public IHttpActionResult Put(DocumentModel model, Guid documentId)
        {
            try
            {
                _updateDocumentCmd.Execute(Envelop(new Document(documentId, model.Title, model.Content)));
            }
            catch (UnauthorizedAccessException)
            {
                return this.Unauthorized();
            }
            catch (DocumentLockedException e)
            {
                return this.ResponseMessage(new HttpResponseMessage(HttpStatusCode.Conflict)
                {
                    Content = new StringContent(e.Message)
                });
            }

            var doc = _getDocument.Execute(Envelop(documentId));
            return this.Ok<DocumentResponseModel>(MapDocument(doc));
        }
        public IHttpActionResult Post(DocumentModel model)
        {
            var id = Guid.NewGuid();
            try
            {
                _submitDocumentCmd.Execute(Envelop(new Document(id, model.Title, model.Content)));
            }
            catch (UnauthorizedAccessException)
            {
                return this.Unauthorized();
            }
            catch (ValidationException e)
            {
                return this.BadRequest(e.Message);
            }

            var link = this.Url.Link("GetDocumentById", new { documentId = id });
            return this.Created<DocumentResponseModel>("", new DocumentResponseModel()
            {
                Id = id.ToString(),
                Title = model.Title,
                Content = model.Content
            });
        }