示例#1
0
        public async Task <ObjectResponse <Printer> > Create(
            [FromBody] Dto.Request.Printer printer,
            CancellationToken cancellationToken)
        {
            if (!ModelState.IsValid)
            {
                return(new ObjectResponse <Printer>(
                           HttpStatusCode.BadRequest,
                           "Model state is invalid."));
            }

            var request = new CreatePrinterCommand
            {
                Printer = printer
            };
            var response = await _mediator.Send(
                request,
                cancellationToken);

            return(new ObjectResponse <Printer>
            {
                Success = true,
                Value = response.Printer
            });
        }
        public void GivenPrinterDto_WhenMappedToPrinterDomain_ThenMappingCorrect()
        {
            // Arrange
            var config = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile <DtoToDomainMapping>();
            });
            var sut = new Mapper(config);

            var dtoPrinter = new Dto.Request.Printer
            {
                MarlinId = Guid.NewGuid().ToString(),
                Name     = "Hello World",
                BedSize  = new Dto.Request.Dimensions
                {
                    X = 100,
                    Y = 200,
                    Z = 300
                }
            };

            // Act
            var domainPrinter = sut.Map <Domain.Models.Printer>(dtoPrinter);

            // Assert
            Assert.Equal(dtoPrinter.MarlinId, domainPrinter.MarlinId);
            Assert.Equal(dtoPrinter.Name, domainPrinter.Name);
            Assert.Equal(dtoPrinter.BedSize.X, domainPrinter.BedSizeX);
            Assert.Equal(dtoPrinter.BedSize.Y, domainPrinter.BedSizeY);
            Assert.Equal(dtoPrinter.BedSize.Z, domainPrinter.BedSizeZ);
        }
        public async Task GivenId_AndPrinter_WhenUpdate_ThenQuerySent_AndResponseReturned()
        {
            // Arrange
            var mockMediator = new Mock <IMediator>();
            var sut          = new PrintersController(
                new PrinterIdValidator(),
                mockMediator.Object);
            var id       = ObjectId.GenerateNewId().ToString();
            var printer  = new Dto.Request.Printer();
            var response = new UpdatePrinterCommandResponse();

            mockMediator.Setup(x => x.Send(
                                   It.IsAny <UpdatePrinterCommand>(),
                                   It.IsAny <CancellationToken>()))
            .ReturnsAsync(response);

            var cancellationTokenSource = new CancellationTokenSource();

            // Act
            var result = await sut.Update(
                id,
                printer,
                cancellationTokenSource.Token);

            // Assert
            mockMediator.Verify(x => x.Send(
                                    It.Is <UpdatePrinterCommand>(y => y.Id == id && y.Printer == printer),
                                    It.Is <CancellationToken>(y => y == cancellationTokenSource.Token)), Times.Once);
            Assert.Null(result.Error);
        }
        public async Task GivenPrinter_WhenCreate_ThenQuerySent_AndResponseReturned()
        {
            // Arrange
            var mockMediator = new Mock <IMediator>();
            var sut          = new PrintersController(
                new PrinterIdValidator(),
                mockMediator.Object);
            var printer  = new Dto.Request.Printer();
            var response = new CreatePrinterCommandResponse
            {
                Printer = new Dto.Response.Printer
                {
                    Id = "HelloWorld"
                }
            };

            mockMediator.Setup(x => x.Send(
                                   It.IsAny <CreatePrinterCommand>(),
                                   It.IsAny <CancellationToken>()))
            .ReturnsAsync(response);

            var cancellationTokenSource = new CancellationTokenSource();

            // Act
            var result = await sut.Create(
                printer,
                cancellationTokenSource.Token);

            // Assert
            mockMediator.Verify(x => x.Send(
                                    It.Is <CreatePrinterCommand>(y => y.Printer == printer),
                                    It.Is <CancellationToken>(y => y == cancellationTokenSource.Token)), Times.Once);
            Assert.Equal(response.Printer.Id, result.Value.Id);
        }
示例#5
0
        public async Task <ObjectResponse <Printer> > CreateAsync(
            Dto.Request.Printer printer,
            CancellationToken cancellationToken)
        {
            var response = await _httpAdapter.PostAsync(
                new Uri($"/api/Printers", UriKind.Relative),
                new StringContent(JsonConvert.SerializeObject(printer), Encoding.UTF8, "application/json"),
                cancellationToken);

            return(JsonConvert.DeserializeObject <ObjectResponse <Printer> >(await response.Content.ReadAsStringAsync(cancellationToken)));
        }
示例#6
0
        public async Task GivenPrinter_AndCancellationToken_WhenCreateAsync_ThenHttpAdapterCalled_AndResponseDeserialised()
        {
            // Arrange
            var mockHttpAdapter = new Mock <IHttpAdapter <PrintersClient> >();
            var sut             = new PrintersClient(mockHttpAdapter.Object);

            var request = new Dto.Request.Printer
            {
                Name = "Bob Hoskins"
            };
            var requestContent = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json");

            var response = new ObjectResponse <Printer>
            {
                Success = true,
                Value   = new Printer
                {
                    Id = "Hello World"
                },
                Error = new Error
                {
                    HttpStatusCode = System.Net.HttpStatusCode.OK,
                    Message        = "Deserialisation test"
                }
            };

            mockHttpAdapter.Setup(x => x.PostAsync(
                                      It.IsAny <Uri>(),
                                      It.IsAny <StringContent>(),
                                      It.IsAny <CancellationToken>()))
            .ReturnsAsync(new HttpResponseMessage
            {
                StatusCode = System.Net.HttpStatusCode.OK,
                Content    = new StringContent(JsonConvert.SerializeObject(response), Encoding.UTF8, "application/json")
            });

            var cancellationTokenSource = new CancellationTokenSource();

            // Act
            var result = await sut.CreateAsync(
                request,
                cancellationTokenSource.Token);

            // Assert
            Assert.True(result.Success);
            Assert.Equal(response.Value.Id, result.Value.Id);
            Assert.Equal(response.Error.Message, result.Error.Message);
            mockHttpAdapter.Verify(x => x.PostAsync(
                                       It.Is <Uri>(y => y.ToString() == new Uri($"/api/Printers", UriKind.Relative).ToString()),
                                       It.Is <StringContent>(y => y.ReadAsStringAsync().GetAwaiter().GetResult() == requestContent.ReadAsStringAsync().GetAwaiter().GetResult()),
                                       It.Is <CancellationToken>(y => y == cancellationTokenSource.Token)), Times.Once);
        }
示例#7
0
        public async Task <Response> Update(
            string id,
            [FromBody] Dto.Request.Printer printer,
            CancellationToken cancellationToken)
        {
            var idValidationResultError = _idValidator.Validate(id);

            if (idValidationResultError != null)
            {
                return(new Response
                {
                    Error = idValidationResultError
                });
            }

            if (!ModelState.IsValid)
            {
                return(new Response(
                           HttpStatusCode.BadRequest,
                           "Model state is invalid."));
            }

            var request = new UpdatePrinterCommand
            {
                Id      = id,
                Printer = printer
            };
            var response = await _mediator.Send(
                request,
                cancellationToken);

            return(new Response
            {
                Success = response.Error == null,
                Error = response.Error
            });
        }