示例#1
0
        public virtual async void TestUpdate()
        {
            var builder = new WebHostBuilder()
                          .UseEnvironment("Production")
                          .UseStartup <TestStartup>();
            TestServer testServer = new TestServer(builder);

            var client = new ApiClient(testServer.CreateClient());

            client.SetBearerToken(JWTTestHelper.GenerateBearerToken());
            var mapper = new ApiBreedServerModelMapper();
            ApplicationDbContext        context = testServer.Host.Services.GetService(typeof(ApplicationDbContext)) as ApplicationDbContext;
            IBreedService               service = testServer.Host.Services.GetService(typeof(IBreedService)) as IBreedService;
            ApiBreedServerResponseModel model   = await service.Get(1);

            ApiBreedClientRequestModel request = mapper.MapServerResponseToClientRequest(model);

            request.SetProperties("B", 1);

            UpdateResponse <ApiBreedClientResponseModel> updateResponse = await client.BreedUpdateAsync(model.Id, request);

            context.Entry(context.Set <Breed>().ToList()[0]).Reload();
            updateResponse.Record.Should().NotBeNull();
            updateResponse.Success.Should().BeTrue();
            updateResponse.Record.Id.Should().Be(1);
            context.Set <Breed>().ToList()[0].Name.Should().Be("B");
            context.Set <Breed>().ToList()[0].SpeciesId.Should().Be(1);

            updateResponse.Record.Id.Should().Be(1);
            updateResponse.Record.Name.Should().Be("B");
            updateResponse.Record.SpeciesId.Should().Be(1);
        }
示例#2
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;

            IBreedService service = testServer.Host.Services.GetService(typeof(IBreedService)) as IBreedService;
            var           model   = new ApiBreedServerRequestModel();

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

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

            ActionResponse deleteResult = await client.BreedDeleteAsync(2);

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

            verifyResponse.Should().BeNull();
        }
示例#3
0
        public void MapServerResponseToRequest()
        {
            var mapper = new ApiBreedServerModelMapper();
            var model  = new ApiBreedServerResponseModel();

            model.SetProperties(1, "A", 1);
            ApiBreedServerRequestModel response = mapper.MapServerResponseToRequest(model);

            response.Should().NotBeNull();
            response.Name.Should().Be("A");
            response.SpeciesId.Should().Be(1);
        }
示例#4
0
        public async virtual Task <IActionResult> Get(int id)
        {
            ApiBreedServerResponseModel response = await this.BreedService.Get(id);

            if (response == null)
            {
                return(this.StatusCode(StatusCodes.Status404NotFound));
            }
            else
            {
                return(this.Ok(response));
            }
        }
示例#5
0
        public async void Get_ShouldReturnNullBecauseRecordNotFound()
        {
            var mock = new ServiceMockFacade <IBreedService, IBreedRepository>();

            mock.RepositoryMock.Setup(x => x.Get(It.IsAny <int>())).Returns(Task.FromResult <Breed>(null));
            var service = new BreedService(mock.LoggerMock.Object,
                                           mock.MediatorMock.Object,
                                           mock.RepositoryMock.Object,
                                           mock.ModelValidatorMockFactory.BreedModelValidatorMock.Object,
                                           mock.DALMapperMockFactory.DALBreedMapperMock,
                                           mock.DALMapperMockFactory.DALPetMapperMock);

            ApiBreedServerResponseModel response = await service.Get(default(int));

            response.Should().BeNull();
            mock.RepositoryMock.Verify(x => x.Get(It.IsAny <int>()));
        }
示例#6
0
        public async void Create_Errors()
        {
            BreedControllerMockFacade mock = new BreedControllerMockFacade();

            var mockResponse = new Mock <CreateResponse <ApiBreedServerResponseModel> >(null as ApiBreedServerResponseModel);
            var mockRecord   = new ApiBreedServerResponseModel();

            mockResponse.SetupGet(x => x.Success).Returns(false);

            mock.ServiceMock.Setup(x => x.Create(It.IsAny <ApiBreedServerRequestModel>())).Returns(Task.FromResult <CreateResponse <ApiBreedServerResponseModel> >(mockResponse.Object));
            BreedController controller = new BreedController(mock.ApiSettingsMoc.Object, mock.LoggerMock.Object, mock.TransactionCoordinatorMock.Object, mock.ServiceMock.Object, mock.ModelMapperMock.Object);

            controller.ControllerContext             = new ControllerContext();
            controller.ControllerContext.HttpContext = new DefaultHttpContext();

            IActionResult response = await controller.Create(new ApiBreedServerRequestModel());

            response.Should().BeOfType <ObjectResult>();
            (response as ObjectResult).StatusCode.Should().Be((int)HttpStatusCode.UnprocessableEntity);
            mock.ServiceMock.Verify(x => x.Create(It.IsAny <ApiBreedServerRequestModel>()));
        }
示例#7
0
        public async void All_Exists()
        {
            BreedControllerMockFacade mock = new BreedControllerMockFacade();
            var record  = new ApiBreedServerResponseModel();
            var records = new List <ApiBreedServerResponseModel>();

            records.Add(record);
            mock.ServiceMock.Setup(x => x.All(It.IsAny <int>(), It.IsAny <int>(), It.IsAny <string>())).Returns(Task.FromResult(records));
            BreedController controller = new BreedController(mock.ApiSettingsMoc.Object, mock.LoggerMock.Object, mock.TransactionCoordinatorMock.Object, mock.ServiceMock.Object, mock.ModelMapperMock.Object);

            controller.ControllerContext             = new ControllerContext();
            controller.ControllerContext.HttpContext = new DefaultHttpContext();

            IActionResult response = await controller.All(1000, 0, string.Empty);

            response.Should().BeOfType <OkObjectResult>();
            (response as OkObjectResult).StatusCode.Should().Be((int)HttpStatusCode.OK);
            var items = (response as OkObjectResult).Value as List <ApiBreedServerResponseModel>;

            items.Count.Should().Be(1);
            mock.ServiceMock.Verify(x => x.All(It.IsAny <int>(), It.IsAny <int>(), It.IsAny <string>()));
        }
示例#8
0
        public virtual async Task <IActionResult> Patch(int id, [FromBody] JsonPatchDocument <ApiBreedServerRequestModel> patch)
        {
            ApiBreedServerResponseModel record = await this.BreedService.Get(id);

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

                UpdateResponse <ApiBreedServerResponseModel> result = await this.BreedService.Update(id, model);

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