public async void RegisterUser_ReturnsRegisteredUser()
        {
            _catapultEngineService
            .Setup(s => s.CreateCatapultEngine(It.IsAny <string>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync((string name, CancellationToken cancellationToken) =>
                          new CatapultEngine
            {
                Id   = 1,
                Name = name
            });
            _catapultEngineService.Setup(s => s.GenerateConfirmationToken(It.IsAny <int>(), It.IsAny <CancellationToken>())).ReturnsAsync("xxx");
            _catapultEngineService.Setup(s => s.ConfirmRegistration(It.IsAny <int>(), It.IsAny <string>(), It.IsAny <CancellationToken>())).Returns(Task.CompletedTask);

            var controller = new CatapultEngineController(_catapultEngineService.Object, _mapper, _logger.Object);

            var dto = new RegisterCatapultEngineDto
            {
                Name = "Engine01"
            };
            var result = await controller.RegisterEngine(dto);

            var okActionResult = Assert.IsType <OkObjectResult>(result);
            var returnValue    = Assert.IsType <CatapultEngineDto>(okActionResult.Value);

            Assert.Equal(1, returnValue.Id);
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> RegisterEngine(RegisterCatapultEngineDto dto)
        {
            _logger.LogRequest("Registering engine. Request body: {@dto}", dto);

            int catapultEngineId = 0;

            try
            {
                var createdCatapultEngine = await _catapultEngineService.CreateCatapultEngine(dto.Name);

                if (createdCatapultEngine != null)
                {
                    catapultEngineId = createdCatapultEngine.Id;

                    var token = await _catapultEngineService.GenerateConfirmationToken(createdCatapultEngine.Id);

                    await _catapultEngineService.ConfirmRegistration(catapultEngineId, token);
                }

                var result = new CatapultEngineDto
                {
                    Id   = catapultEngineId,
                    Name = dto.Name
                };

                _logger.LogResponse("Engine registered. Response body: {@result}", result);

                return(Ok(result));
            }
            catch (DuplicateCatapultEngineException nex)
            {
                _logger.LogWarning(nex, "Duplicate catapult engine name");
                return(BadRequest(nex.Message));
            }
            catch (CatapultEngineCreationFailedException uex)
            {
                _logger.LogWarning(uex, "Catapult engine creation failed");
                return(BadRequest(uex.GetExceptionMessageList()));
            }
        }
        public async Task <CatapultEngineDto> RegisterEngine(RegisterCatapultEngineDto dto)
        {
            var path = $"engine/register";

            return(await Api.Post <RegisterCatapultEngineDto, CatapultEngineDto>(path, dto));
        }