public async Task GetTranslationsByLangauge_ShouldReturnNotFoundResult_WhenLanguageDoesNotExist() { // Arrange const int languageId = 8941; GetTranslationsByLanguageQueryParams model = new GetTranslationsByLanguageQueryParams(); Mock <IMediator> mediatorMock = new Mock <IMediator>(); mediatorMock .Setup(m => m.Send(It.IsAny <LanguageExistsQuery>(), It.IsAny <CancellationToken>())) .ReturnsAsync(false); LanguageController controller = new LanguageController(mediatorMock.Object); // Act ActionResult <IDictionary <string, string> > response = await controller.GetTranslationsByLanguage(languageId, model); // Assert NotFoundObjectResult result = Assert.IsType <NotFoundObjectResult>(response.Result); ErrorResource error = Assert.IsType <ErrorResource>(result.Value); Assert.Equal(StatusCodes.Status404NotFound, error.StatusCode); }
public async Task GetTranslationsByLanguage_ShouldReturnTranslations_WhenLanguageExists() { // Arrange const int languageId = 1; GetTranslationsByLanguageQueryParams model = new GetTranslationsByLanguageQueryParams { Pattern = "*" }; IDictionary <string, string> expectedTranslations = new Dictionary <string, string> { { "key", "value" } }; Mock <IMediator> mediatorMock = new Mock <IMediator>(); mediatorMock .Setup(m => m.Send(It.IsAny <LanguageExistsQuery>(), It.IsAny <CancellationToken>())) .ReturnsAsync(true); mediatorMock .Setup(m => m.Send(It.IsAny <GetTranslationsByLanguageQuery>(), It.IsAny <CancellationToken>())) .ReturnsAsync(expectedTranslations); LanguageController controller = new LanguageController(mediatorMock.Object); // Act ActionResult <IDictionary <string, string> > response = await controller.GetTranslationsByLanguage(languageId, model); // Assert OkObjectResult okResult = Assert.IsType <OkObjectResult>(response.Result); IDictionary <string, string> translations = (IDictionary <string, string>)okResult.Value; Assert.NotNull(translations); Assert.NotEmpty(translations); Assert.Single(translations); }
public async Task <ActionResult <IDictionary <string, string> > > GetTranslationsByLanguage([FromRoute] int languageId, [FromQuery] GetTranslationsByLanguageQueryParams model, CancellationToken cancellationToken = default) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } LanguageExistsQuery existsQuery = new LanguageExistsQuery { LanguageId = languageId }; bool languageExists = await _mediator.Send(existsQuery, cancellationToken); if (!languageExists) { return(NotFound(new ErrorResource { StatusCode = StatusCodes.Status404NotFound, Message = $"Language with ID '{languageId}' does not exist" })); } GetTranslationsByLanguageQuery translationsQuery = new GetTranslationsByLanguageQuery { LanguageId = languageId, Pattern = model.Pattern }; IDictionary <string, string> translations = await _mediator.Send(translationsQuery, cancellationToken); return(Ok(translations)); }