public async Task <ActionResult> PostAsync([FromBody] CreateDroid createDroid) { try { var droid = await _droidHandler.HandleAsync(createDroid); return(CreatedAtAction( nameof(GetAsync), new { serialNumber = (Guid)droid.SerialNumber }, droid.ToViewModel())); } catch (BusinessException ex) { return(ex.GetStatusCode()); } }
public async Task HandleCreateDroid_WithValidDroid_DroidCreatedSuccessfully() { // Arrange var droidHandler = new DroidHandler(_repoMock.Object, _repoArmMock.Object, _quoteMock.Object, _encryptionMock.Object); var createDroid = new CreateDroid("Toto", "To"); // Act var droidCreated = await droidHandler.HandleAsync(createDroid).ConfigureAwait(false); // Assert _repoMock.Verify((r) => r.DoesNameAlreadyExistsAsync(createDroid.Name), Times.Once); _repoMock.Verify((r) => r.InsertAsync(droidCreated), Times.Once); (droidCreated.Name).Should().Be(createDroid.Name); (droidCreated.Nickname).Should().Be(createDroid.Nickname); }
public async Task InsertDroid_WithInvalidDroidName_ReturnsConflictStatusCodeAsync() { // Arrange var createDroid = new CreateDroid("Toto", "To"); _handler .Setup(x => x.HandleAsync(createDroid)) .Throws <DroidConflictNameException>(); // Act var httpResponse = await _controller.PostAsync(createDroid); var httpCode = ((StatusCodeResult)httpResponse).StatusCode; // Assert httpCode.Should().Be((int)HttpStatusCode.Conflict); }
internal static async Task <Droid> CreateNewAsync( Func <DroidName, Task <bool> > nameAlreadyExists, Func <Task <string> > getRandomQuoteAsync, Func <string, string> encrypt, CreateDroid createDroid) // INFO: Usage of dependency injection { var droid = new Droid(Guid.NewGuid(), DateTimeOffset.Now) { _arms = new List <Arm>() }; droid.SetNickname(createDroid.Nickname); await droid.SetQuoteAsync(getRandomQuoteAsync); droid.WithEncryptedQuote(encrypt, droid.Quote); return(await droid.SetNameAsync(nameAlreadyExists, createDroid.Name)); }
public async Task InsertDroid_WithValidDroid_ReturnsEqualDroidAsync() { // Arrange var createDroid = new CreateDroid("Toto", "To"); var droidDto = new DroidDto { DroidId = Guid.Parse("341d9e5b-ae06-4c06-ba4f-7256317792cd"), CreationDate = new DateTime(2019, 02, 06), Name = "Toto", Nickname = "To" }; var armDtoList = new List <IArmDto> { new ArmDto { DroidId = droidDto.DroidId, ArmId = Guid.NewGuid() } }; var droid = Droid.FromDto(droidDto).WithArms(armDtoList); _handler .Setup(x => x.HandleAsync(createDroid)) .Returns(Task.FromResult(droid)); // Act var httpResponse = await _controller.PostAsync(createDroid); var result = (ObjectResult)httpResponse; var element = (DroidViewModel)result.Value; // Assert result.StatusCode.Should().Be((int)HttpStatusCode.Created); element.SerialNumber.Should().Be(droidDto.DroidId); element.Name.Should().Be(droidDto.Name); element.Nickname.Should().Be(droidDto.Nickname); }