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;

            IEventTeacherService service = testServer.Host.Services.GetService(typeof(IEventTeacherService)) as IEventTeacherService;
            var model = new ApiEventTeacherServerRequestModel();

            model.SetProperties(1, 1);
            CreateResponse <ApiEventTeacherServerResponseModel> createdResponse = await service.Create(model);

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

            ActionResponse deleteResult = await client.EventTeacherDeleteAsync(2);

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

            verifyResponse.Should().BeNull();
        }
Example #2
0
        public void MapServerResponseToRequest()
        {
            var mapper = new ApiEventTeacherServerModelMapper();
            var model  = new ApiEventTeacherServerResponseModel();

            model.SetProperties(1, 1, 1);
            ApiEventTeacherServerRequestModel response = mapper.MapServerResponseToRequest(model);

            response.Should().NotBeNull();
            response.EventId.Should().Be(1);
            response.TeacherId.Should().Be(1);
        }
Example #3
0
        public virtual async Task <IActionResult> Create([FromBody] ApiEventTeacherServerRequestModel model)
        {
            CreateResponse <ApiEventTeacherServerResponseModel> result = await this.EventTeacherService.Create(model);

            if (result.Success)
            {
                return(this.Created($"{this.Settings.ExternalBaseUrl}/api/EventTeachers/{result.Record.Id}", result));
            }
            else
            {
                return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result));
            }
        }
Example #4
0
        public void CreatePatch()
        {
            var mapper = new ApiEventTeacherServerModelMapper();
            var model  = new ApiEventTeacherServerRequestModel();

            model.SetProperties(1, 1);

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

            patch.ApplyTo(response);
            response.EventId.Should().Be(1);
            response.TeacherId.Should().Be(1);
        }
Example #5
0
        private async Task <ApiEventTeacherServerRequestModel> PatchModel(int id, JsonPatchDocument <ApiEventTeacherServerRequestModel> patch)
        {
            var record = await this.EventTeacherService.Get(id);

            if (record == null)
            {
                return(null);
            }
            else
            {
                ApiEventTeacherServerRequestModel request = this.EventTeacherModelMapper.MapServerResponseToRequest(record);
                patch.ApplyTo(request);
                return(request);
            }
        }
Example #6
0
        public async void Delete_NoErrorsOccurred_ShouldReturnResponse()
        {
            var mock  = new ServiceMockFacade <IEventTeacherService, IEventTeacherRepository>();
            var model = new ApiEventTeacherServerRequestModel();

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

            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.EventTeacherModelValidatorMock.Verify(x => x.ValidateDeleteAsync(It.IsAny <int>()));
            mock.MediatorMock.Verify(x => x.Publish(It.IsAny <EventTeacherDeletedNotification>(), It.IsAny <CancellationToken>()));
        }
Example #7
0
        public virtual async Task <IActionResult> Update(int id, [FromBody] ApiEventTeacherServerRequestModel model)
        {
            ApiEventTeacherServerRequestModel request = await this.PatchModel(id, this.EventTeacherModelMapper.CreatePatch(model)) as ApiEventTeacherServerRequestModel;

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

                if (result.Success)
                {
                    return(this.Ok(result));
                }
                else
                {
                    return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result));
                }
            }
        }
Example #8
0
        public async void Delete_ErrorsOccurred_ShouldReturnErrorResponse()
        {
            var mock          = new ServiceMockFacade <IEventTeacherService, IEventTeacherRepository>();
            var model         = new ApiEventTeacherServerRequestModel();
            var validatorMock = new Mock <IApiEventTeacherServerRequestModelValidator>();

            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 EventTeacherService(mock.LoggerMock.Object,
                                                  mock.MediatorMock.Object,
                                                  mock.RepositoryMock.Object,
                                                  validatorMock.Object,
                                                  mock.DALMapperMockFactory.DALEventTeacherMapperMock);

            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 <EventTeacherDeletedNotification>(), It.IsAny <CancellationToken>()), Times.Never());
        }
Example #9
0
        public virtual async Task <IActionResult> Patch(int id, [FromBody] JsonPatchDocument <ApiEventTeacherServerRequestModel> patch)
        {
            ApiEventTeacherServerResponseModel record = await this.EventTeacherService.Get(id);

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

                UpdateResponse <ApiEventTeacherServerResponseModel> result = await this.EventTeacherService.Update(id, model);

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