public async void TestDelete()
        {
            var builder = new WebHostBuilder()
                          .UseEnvironment("Production")
                          .UseStartup <TestStartup>();
            TestServer testServer = new TestServer(builder);

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

            var createModel = new ApiDeviceRequestModel();

            createModel.SetProperties("B", Guid.Parse("3842cac4-b9a0-8223-0dcc-509a6f75849b"));
            CreateResponse <ApiDeviceResponseModel> createResult = await client.DeviceCreateAsync(createModel);

            createResult.Success.Should().BeTrue();

            ApiDeviceResponseModel getResponse = await client.DeviceGetAsync(2);

            getResponse.Should().NotBeNull();

            ActionResponse deleteResult = await client.DeviceDeleteAsync(2);

            deleteResult.Success.Should().BeTrue();

            ApiDeviceResponseModel verifyResponse = await client.DeviceGetAsync(2);

            verifyResponse.Should().BeNull();
        }
        private async Task <ApiDeviceResponseModel> CreateRecord(ApiClient client)
        {
            var model = new ApiDeviceRequestModel();

            model.SetProperties("B", Guid.Parse("3842cac4-b9a0-8223-0dcc-509a6f75849b"));
            CreateResponse <ApiDeviceResponseModel> result = await client.DeviceCreateAsync(model);

            result.Success.Should().BeTrue();
            return(result.Record);
        }
Beispiel #3
0
        public void MapResponseToRequest()
        {
            var mapper = new ApiDeviceModelMapper();
            var model  = new ApiDeviceResponseModel();

            model.SetProperties(1, "A", Guid.Parse("8420cdcf-d595-ef65-66e7-dff9f98764da"));
            ApiDeviceRequestModel response = mapper.MapResponseToRequest(model);

            response.Name.Should().Be("A");
            response.PublicId.Should().Be(Guid.Parse("8420cdcf-d595-ef65-66e7-dff9f98764da"));
        }
Beispiel #4
0
        public void MapModelToBO()
        {
            var mapper = new BOLDeviceMapper();
            ApiDeviceRequestModel model = new ApiDeviceRequestModel();

            model.SetProperties("A", Guid.Parse("8420cdcf-d595-ef65-66e7-dff9f98764da"));
            BODevice response = mapper.MapModelToBO(1, model);

            response.Name.Should().Be("A");
            response.PublicId.Should().Be(Guid.Parse("8420cdcf-d595-ef65-66e7-dff9f98764da"));
        }
Beispiel #5
0
        public virtual BODevice MapModelToBO(
            int id,
            ApiDeviceRequestModel model
            )
        {
            BODevice boDevice = new BODevice();

            boDevice.SetProperties(
                id,
                model.Name,
                model.PublicId);
            return(boDevice);
        }
Beispiel #6
0
        public virtual async Task <IActionResult> Create([FromBody] ApiDeviceRequestModel model)
        {
            CreateResponse <ApiDeviceResponseModel> result = await this.DeviceService.Create(model);

            if (result.Success)
            {
                return(this.Created($"{this.Settings.ExternalBaseUrl}/api/Devices/{result.Record.Id}", result));
            }
            else
            {
                return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result));
            }
        }
Beispiel #7
0
        public void CreatePatch()
        {
            var mapper = new ApiDeviceModelMapper();
            var model  = new ApiDeviceRequestModel();

            model.SetProperties("A", Guid.Parse("8420cdcf-d595-ef65-66e7-dff9f98764da"));

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

            patch.ApplyTo(response);
            response.Name.Should().Be("A");
            response.PublicId.Should().Be(Guid.Parse("8420cdcf-d595-ef65-66e7-dff9f98764da"));
        }
Beispiel #8
0
        public virtual async Task <CreateResponse <ApiDeviceResponseModel> > Create(
            ApiDeviceRequestModel model)
        {
            CreateResponse <ApiDeviceResponseModel> response = new CreateResponse <ApiDeviceResponseModel>(await this.deviceModelValidator.ValidateCreateAsync(model));

            if (response.Success)
            {
                var bo     = this.bolDeviceMapper.MapModelToBO(default(int), model);
                var record = await this.deviceRepository.Create(this.dalDeviceMapper.MapBOToEF(bo));

                response.SetRecord(this.bolDeviceMapper.MapBOToModel(this.dalDeviceMapper.MapEFToBO(record)));
            }

            return(response);
        }
Beispiel #9
0
        private async Task <ApiDeviceRequestModel> PatchModel(int id, JsonPatchDocument <ApiDeviceRequestModel> patch)
        {
            var record = await this.DeviceService.Get(id);

            if (record == null)
            {
                return(null);
            }
            else
            {
                ApiDeviceRequestModel request = this.DeviceModelMapper.MapResponseToRequest(record);
                patch.ApplyTo(request);
                return(request);
            }
        }
Beispiel #10
0
        public virtual async Task <UpdateResponse <ApiDeviceResponseModel> > Update(
            int id,
            ApiDeviceRequestModel model)
        {
            var validationResult = await this.deviceModelValidator.ValidateUpdateAsync(id, model);

            if (validationResult.IsValid)
            {
                var bo = this.bolDeviceMapper.MapModelToBO(id, model);
                await this.deviceRepository.Update(this.dalDeviceMapper.MapBOToEF(bo));

                var record = await this.deviceRepository.Get(id);

                return(new UpdateResponse <ApiDeviceResponseModel>(this.bolDeviceMapper.MapBOToModel(this.dalDeviceMapper.MapEFToBO(record))));
            }
            else
            {
                return(new UpdateResponse <ApiDeviceResponseModel>(validationResult));
            }
        }
Beispiel #11
0
        public async void Create()
        {
            var mock  = new ServiceMockFacade <IDeviceRepository>();
            var model = new ApiDeviceRequestModel();

            mock.RepositoryMock.Setup(x => x.Create(It.IsAny <Device>())).Returns(Task.FromResult(new Device()));
            var service = new DeviceService(mock.LoggerMock.Object,
                                            mock.RepositoryMock.Object,
                                            mock.ModelValidatorMockFactory.DeviceModelValidatorMock.Object,
                                            mock.BOLMapperMockFactory.BOLDeviceMapperMock,
                                            mock.DALMapperMockFactory.DALDeviceMapperMock,
                                            mock.BOLMapperMockFactory.BOLDeviceActionMapperMock,
                                            mock.DALMapperMockFactory.DALDeviceActionMapperMock);

            CreateResponse <ApiDeviceResponseModel> response = await service.Create(model);

            response.Should().NotBeNull();
            mock.ModelValidatorMockFactory.DeviceModelValidatorMock.Verify(x => x.ValidateCreateAsync(It.IsAny <ApiDeviceRequestModel>()));
            mock.RepositoryMock.Verify(x => x.Create(It.IsAny <Device>()));
        }
Beispiel #12
0
        public async void Delete()
        {
            var mock  = new ServiceMockFacade <IDeviceRepository>();
            var model = new ApiDeviceRequestModel();

            mock.RepositoryMock.Setup(x => x.Delete(It.IsAny <int>())).Returns(Task.CompletedTask);
            var service = new DeviceService(mock.LoggerMock.Object,
                                            mock.RepositoryMock.Object,
                                            mock.ModelValidatorMockFactory.DeviceModelValidatorMock.Object,
                                            mock.BOLMapperMockFactory.BOLDeviceMapperMock,
                                            mock.DALMapperMockFactory.DALDeviceMapperMock,
                                            mock.BOLMapperMockFactory.BOLDeviceActionMapperMock,
                                            mock.DALMapperMockFactory.DALDeviceActionMapperMock);

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

            response.Should().NotBeNull();
            mock.RepositoryMock.Verify(x => x.Delete(It.IsAny <int>()));
            mock.ModelValidatorMockFactory.DeviceModelValidatorMock.Verify(x => x.ValidateDeleteAsync(It.IsAny <int>()));
        }
Beispiel #13
0
        public virtual async Task <IActionResult> Update(int id, [FromBody] ApiDeviceRequestModel model)
        {
            ApiDeviceRequestModel request = await this.PatchModel(id, this.DeviceModelMapper.CreatePatch(model));

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

                if (result.Success)
                {
                    return(this.Ok(result));
                }
                else
                {
                    return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result));
                }
            }
        }
Beispiel #14
0
        public virtual async Task <IActionResult> Patch(int id, [FromBody] JsonPatchDocument <ApiDeviceRequestModel> patch)
        {
            ApiDeviceResponseModel record = await this.DeviceService.Get(id);

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

                UpdateResponse <ApiDeviceResponseModel> result = await this.DeviceService.Update(id, model);

                if (result.Success)
                {
                    return(this.Ok(result));
                }
                else
                {
                    return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result));
                }
            }
        }
Beispiel #15
0
        public virtual async Task <UpdateResponse <ApiDeviceResponseModel> > DeviceUpdateAsync(int id, ApiDeviceRequestModel item)
        {
            HttpResponseMessage httpResponse = await this.client.PutAsJsonAsync($"api/Devices/{id}", item).ConfigureAwait(false);

            return(JsonConvert.DeserializeObject <UpdateResponse <ApiDeviceResponseModel> >(httpResponse.Content.ContentToString()));
        }
 public async Task <ValidationResult> ValidateUpdateAsync(int id, ApiDeviceRequestModel model)
 {
     this.NameRules();
     this.PublicIdRules();
     return(await this.ValidateAsync(model, id));
 }