コード例 #1
0
        public virtual async void TestDelete()
        {
            var builder = new WebHostBuilder()
                          .UseEnvironment("Production")
                          .UseStartup <TestStartup>();
            TestServer testServer = new TestServer(builder);
            var        client     = new ApiClient(testServer.CreateClient());

            client.SetBearerToken(JWTTestHelper.GenerateBearerToken());
            ApplicationDbContext context = testServer.Host.Services.GetService(typeof(ApplicationDbContext)) as ApplicationDbContext;

            INoteService service = testServer.Host.Services.GetService(typeof(INoteService)) as INoteService;
            var          model   = new ApiNoteServerRequestModel();

            model.SetProperties(1, DateTime.Parse("1/1/1988 12:00:00 AM"), "B", 1);
            CreateResponse <ApiNoteServerResponseModel> createdResponse = await service.Create(model);

            createdResponse.Success.Should().BeTrue();

            ActionResponse deleteResult = await client.NoteDeleteAsync(2);

            deleteResult.Success.Should().BeTrue();
            ApiNoteServerResponseModel verifyResponse = await service.Get(2);

            verifyResponse.Should().BeNull();
        }
コード例 #2
0
ファイル: NoteController.cs プロジェクト: codenesium/samples
        public virtual async Task <IActionResult> Create([FromBody] ApiNoteServerRequestModel model)
        {
            CreateResponse <ApiNoteServerResponseModel> result = await this.NoteService.Create(model);

            if (result.Success)
            {
                return(this.Created($"{this.Settings.ExternalBaseUrl}/api/Notes/{result.Record.Id}", result));
            }
            else
            {
                return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result));
            }
        }
コード例 #3
0
        public void MapServerResponseToRequest()
        {
            var mapper = new ApiNoteServerModelMapper();
            var model  = new ApiNoteServerResponseModel();

            model.SetProperties(1, 1, DateTime.Parse("1/1/1987 12:00:00 AM"), "A", 1);
            ApiNoteServerRequestModel response = mapper.MapServerResponseToRequest(model);

            response.Should().NotBeNull();
            response.CallId.Should().Be(1);
            response.DateCreated.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM"));
            response.NoteText.Should().Be("A");
            response.OfficerId.Should().Be(1);
        }
コード例 #4
0
ファイル: NoteController.cs プロジェクト: codenesium/samples
        private async Task <ApiNoteServerRequestModel> PatchModel(int id, JsonPatchDocument <ApiNoteServerRequestModel> patch)
        {
            var record = await this.NoteService.Get(id);

            if (record == null)
            {
                return(null);
            }
            else
            {
                ApiNoteServerRequestModel request = this.NoteModelMapper.MapServerResponseToRequest(record);
                patch.ApplyTo(request);
                return(request);
            }
        }
コード例 #5
0
        public void CreatePatch()
        {
            var mapper = new ApiNoteServerModelMapper();
            var model  = new ApiNoteServerRequestModel();

            model.SetProperties(1, DateTime.Parse("1/1/1987 12:00:00 AM"), "A", 1);

            JsonPatchDocument <ApiNoteServerRequestModel> patch = mapper.CreatePatch(model);
            var response = new ApiNoteServerRequestModel();

            patch.ApplyTo(response);
            response.CallId.Should().Be(1);
            response.DateCreated.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM"));
            response.NoteText.Should().Be("A");
            response.OfficerId.Should().Be(1);
        }
コード例 #6
0
ファイル: TestNoteService.cs プロジェクト: codenesium/samples
        public async void Delete_NoErrorsOccurred_ShouldReturnResponse()
        {
            var mock  = new ServiceMockFacade <INoteService, INoteRepository>();
            var model = new ApiNoteServerRequestModel();

            mock.RepositoryMock.Setup(x => x.Delete(It.IsAny <int>())).Returns(Task.CompletedTask);
            var service = new NoteService(mock.LoggerMock.Object,
                                          mock.MediatorMock.Object,
                                          mock.RepositoryMock.Object,
                                          mock.ModelValidatorMockFactory.NoteModelValidatorMock.Object,
                                          mock.DALMapperMockFactory.DALNoteMapperMock);

            ActionResponse response = await service.Delete(default(int));

            response.Should().NotBeNull();
            response.Success.Should().BeTrue();
            mock.RepositoryMock.Verify(x => x.Delete(It.IsAny <int>()));
            mock.ModelValidatorMockFactory.NoteModelValidatorMock.Verify(x => x.ValidateDeleteAsync(It.IsAny <int>()));
            mock.MediatorMock.Verify(x => x.Publish(It.IsAny <NoteDeletedNotification>(), It.IsAny <CancellationToken>()));
        }
コード例 #7
0
ファイル: NoteController.cs プロジェクト: codenesium/samples
        public virtual async Task <IActionResult> Update(int id, [FromBody] ApiNoteServerRequestModel model)
        {
            ApiNoteServerRequestModel request = await this.PatchModel(id, this.NoteModelMapper.CreatePatch(model)) as ApiNoteServerRequestModel;

            if (request == null)
            {
                return(this.StatusCode(StatusCodes.Status404NotFound));
            }
            else
            {
                UpdateResponse <ApiNoteServerResponseModel> result = await this.NoteService.Update(id, request);

                if (result.Success)
                {
                    return(this.Ok(result));
                }
                else
                {
                    return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result));
                }
            }
        }
コード例 #8
0
ファイル: TestNoteService.cs プロジェクト: codenesium/samples
        public async void Delete_ErrorsOccurred_ShouldReturnErrorResponse()
        {
            var mock          = new ServiceMockFacade <INoteService, INoteRepository>();
            var model         = new ApiNoteServerRequestModel();
            var validatorMock = new Mock <IApiNoteServerRequestModelValidator>();

            validatorMock.Setup(x => x.ValidateDeleteAsync(It.IsAny <int>())).Returns(Task.FromResult(new FluentValidation.Results.ValidationResult(new List <ValidationFailure>()
            {
                new ValidationFailure("text", "test")
            })));
            var service = new NoteService(mock.LoggerMock.Object,
                                          mock.MediatorMock.Object,
                                          mock.RepositoryMock.Object,
                                          validatorMock.Object,
                                          mock.DALMapperMockFactory.DALNoteMapperMock);

            ActionResponse response = await service.Delete(default(int));

            response.Should().NotBeNull();
            response.Success.Should().BeFalse();
            validatorMock.Verify(x => x.ValidateDeleteAsync(It.IsAny <int>()));
            mock.MediatorMock.Verify(x => x.Publish(It.IsAny <NoteDeletedNotification>(), It.IsAny <CancellationToken>()), Times.Never());
        }
コード例 #9
0
ファイル: NoteController.cs プロジェクト: codenesium/samples
        public virtual async Task <IActionResult> Patch(int id, [FromBody] JsonPatchDocument <ApiNoteServerRequestModel> patch)
        {
            ApiNoteServerResponseModel record = await this.NoteService.Get(id);

            if (record == null)
            {
                return(this.StatusCode(StatusCodes.Status404NotFound));
            }
            else
            {
                ApiNoteServerRequestModel model = await this.PatchModel(id, patch) as ApiNoteServerRequestModel;

                UpdateResponse <ApiNoteServerResponseModel> result = await this.NoteService.Update(id, model);

                if (result.Success)
                {
                    return(this.Ok(result));
                }
                else
                {
                    return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result));
                }
            }
        }