public async Task <HttpResponseMessage> PostAsync([FromBody] HumanV1Dto humanDto) { HttpResponseMessage response = new HttpResponseMessage(); try { Human human = _autoMapper.Map <Human>(humanDto); bool isMutant = await _mutantService.IsMutantAsync(human); if (isMutant) { response.StatusCode = HttpStatusCode.OK; } else { response.StatusCode = HttpStatusCode.Forbidden; } } catch (ArgumentException) { response.StatusCode = HttpStatusCode.BadRequest; //TODO: add some message with the wrong validation. //TODO: log the exception. } catch (Exception) { response.StatusCode = HttpStatusCode.InternalServerError; //TODO: log the exception. } return(response); }
public async Task MutantV1Controller_PostAsync_WrongDto_Succeeds() { //Arrange MutantV1Controller controller = UnityConfig.Resolve <MutantV1Controller>(); HumanV1Dto dto = new HumanV1Dto() { Dna = new string[] { "AAA", "CCC", "TTTXX" } }; //Action HttpResponseMessage result = await controller.PostAsync(dto); //Asserts Assert.IsNotNull(result); Assert.AreEqual(HttpStatusCode.BadRequest, result.StatusCode); }
public async Task MutantV1Controller_PostAsync_NonHandledException_Succeeds() { //Arrange MutantV1Controller controller = GetMutantController(); HumanV1Dto dto = new HumanV1Dto(); _mutantServiceMock.Setup(x => x.IsMutantAsync(It.IsAny <Human>())).ThrowsAsync(new Exception()).Verifiable(); //Action HttpResponseMessage result = await controller.PostAsync(dto); //Asserts Assert.IsNotNull(result); Assert.AreEqual(HttpStatusCode.InternalServerError, result.StatusCode); _mutantServiceMock.Verify(x => x.IsMutantAsync(It.IsAny <Human>()), Times.Once); }
public async Task MutantV1Controller_PostAsync_ValidDtoFullyMapped_Succeeds() { //Arrange MutantV1Controller controller = GetMutantController(); HumanV1Dto dto = new HumanV1Dto() { Dna = new string[] { "AAA", "CCC", "TTT" } }; _mutantServiceMock.Setup(x => x.IsMutantAsync(It.IsAny <Human>())).ReturnsAsync(true).Verifiable(); //Action HttpResponseMessage result = await controller.PostAsync(dto); //Asserts Assert.IsNotNull(result); Assert.AreEqual(HttpStatusCode.OK, result.StatusCode); _mutantServiceMock.Verify(x => x.IsMutantAsync(It.IsAny <Human>()), Times.Once); }