Example #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 ApiBadgeServerModelMapper();
            ApplicationDbContext        context = testServer.Host.Services.GetService(typeof(ApplicationDbContext)) as ApplicationDbContext;
            IBadgeService               service = testServer.Host.Services.GetService(typeof(IBadgeService)) as IBadgeService;
            ApiBadgeServerResponseModel model   = await service.Get(1);

            ApiBadgeClientRequestModel request = mapper.MapServerResponseToClientRequest(model);

            request.SetProperties(DateTime.Parse("1/1/1988 12:00:00 AM"), "B", 1);

            UpdateResponse <ApiBadgeClientResponseModel> updateResponse = await client.BadgeUpdateAsync(model.Id, request);

            context.Entry(context.Set <Badge>().ToList()[0]).Reload();
            updateResponse.Record.Should().NotBeNull();
            updateResponse.Success.Should().BeTrue();
            updateResponse.Record.Id.Should().Be(1);
            context.Set <Badge>().ToList()[0].Date.Should().Be(DateTime.Parse("1/1/1988 12:00:00 AM"));
            context.Set <Badge>().ToList()[0].Name.Should().Be("B");
            context.Set <Badge>().ToList()[0].UserId.Should().Be(1);

            updateResponse.Record.Id.Should().Be(1);
            updateResponse.Record.Date.Should().Be(DateTime.Parse("1/1/1988 12:00:00 AM"));
            updateResponse.Record.Name.Should().Be("B");
            updateResponse.Record.UserId.Should().Be(1);
        }
Example #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;

            IBadgeService service = testServer.Host.Services.GetService(typeof(IBadgeService)) as IBadgeService;
            var           model   = new ApiBadgeServerRequestModel();

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

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

            ActionResponse deleteResult = await client.BadgeDeleteAsync(2);

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

            verifyResponse.Should().BeNull();
        }
Example #3
0
        public async virtual Task <IActionResult> Get(int id)
        {
            ApiBadgeServerResponseModel response = await this.BadgeService.Get(id);

            if (response == null)
            {
                return(this.StatusCode(StatusCodes.Status404NotFound));
            }
            else
            {
                return(this.Ok(response));
            }
        }
        public void MapServerResponseToRequest()
        {
            var mapper = new ApiBadgeServerModelMapper();
            var model  = new ApiBadgeServerResponseModel();

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

            response.Should().NotBeNull();
            response.Date.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM"));
            response.Name.Should().Be("A");
            response.UserId.Should().Be(1);
        }
Example #5
0
        public async void Get_ShouldReturnNullBecauseRecordNotFound()
        {
            var mock = new ServiceMockFacade <IBadgeService, IBadgeRepository>();

            mock.RepositoryMock.Setup(x => x.Get(It.IsAny <int>())).Returns(Task.FromResult <Badge>(null));
            var service = new BadgeService(mock.LoggerMock.Object,
                                           mock.MediatorMock.Object,
                                           mock.RepositoryMock.Object,
                                           mock.ModelValidatorMockFactory.BadgeModelValidatorMock.Object,
                                           mock.DALMapperMockFactory.DALBadgeMapperMock);

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

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

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

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

            mock.ServiceMock.Setup(x => x.Create(It.IsAny <ApiBadgeServerRequestModel>())).Returns(Task.FromResult <CreateResponse <ApiBadgeServerResponseModel> >(mockResponse.Object));
            BadgeController controller = new BadgeController(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 ApiBadgeServerRequestModel());

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

            records.Add(record);
            mock.ServiceMock.Setup(x => x.All(It.IsAny <int>(), It.IsAny <int>(), It.IsAny <string>())).Returns(Task.FromResult(records));
            BadgeController controller = new BadgeController(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 <ApiBadgeServerResponseModel>;

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

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

                UpdateResponse <ApiBadgeServerResponseModel> result = await this.BadgeService.Update(id, model);

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