public void ReturnTrue_WhenQueriedFieldsAreNullOrWhiteSpace(string fields) { // Arrange var propertyMappingService = new PropertyMappingService(); List <string> errorMessages = new List <string>(); var validationResult = propertyMappingService.ValidMappingExistsFor <CategoryOfmForGet, Category>(null, ref errorMessages); var actualErrorMessages = JsonConvert.SerializeObject(errorMessages, new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore, Formatting = Formatting.Indented }); var expectedErrorMessages = JsonConvert.SerializeObject( new List <string>(), new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore, Formatting = Formatting.Indented }); Assert.AreEqual(expectedErrorMessages, actualErrorMessages); Assert.IsTrue(validationResult); }
public void ReturnTrueAndNoErrorMessages_WhenPropertyNameDoesExistOnEntity() { // Arrange var propertyMappingService = new PropertyMappingService(); List <string> errorMessages = new List <string>(); var validationResult = propertyMappingService.ValidMappingExistsFor <CategoryOfmForGet, Category>("Name", ref errorMessages); var actualErrorMessages = JsonConvert.SerializeObject(errorMessages, new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore, Formatting = Formatting.Indented }); var expectedErrorMessages = JsonConvert.SerializeObject( new List <string>(), new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore, Formatting = Formatting.Indented }); Assert.AreEqual(expectedErrorMessages, actualErrorMessages); Assert.IsTrue(validationResult); }
public void Given_A_Non_Valid_Mapping_When_ValidMappingExistsFor_Is_Called_Then_Return_False() { // Arrange // Test setup var pmd = new Dictionary <string, PropertyMappingValue> { { "Id", new PropertyMappingValue(new List <string>() { "Id" }) }, { "Name", new PropertyMappingValue(new List <string>() { "FirstName", "LastName" }) } }; var pm = new PropertyMapping <Cat, Dog>(pmd); var pms = new PropertyMappingService(); pms.AddPropertyMapping <Cat, Dog>(pm); // Act // Do the thing you want to test var result = pms.ValidMappingExistsFor <Cat, Dog>("non valid mapping"); // Assert //Did the thing you did actually work Assert.False(result); }
public void Given_NullOrWhiteSpace_Mapping_When_ValidMappingExistsFor_Is_Called_Then_Return_False() { var pmd = new Dictionary <string, PropertyMappingValue> { { "Id", new PropertyMappingValue(new List <string>() { "Id" }) }, { "Name", new PropertyMappingValue(new List <string>() { "FirstName", "LastName" }) } }; var pm = new PropertyMapping <Cat, Dog>(pmd); var pms = new PropertyMappingService(); pms.AddPropertyMapping <Cat, Dog>(pm); var result = pms.ValidMappingExistsFor <Cat, Dog>(""); Assert.True(result); }
public async Task <IActionResult> GetBanknotes(CurrenciesResourceParameters resourceParameters, [FromHeader(Name = "Accept")] string mediaType) { if (!PropertyMappingService.ValidMappingExistsFor <Banknote>(resourceParameters.OrderBy)) { return(BadRequest()); } if (!TypeHelper.TypeHasProperties <BanknoteDto>(resourceParameters.Fields)) { return(BadRequest()); } var retrievedBanknotes = await _banknoteService.FindBanknotes(resourceParameters); var banknotes = _mapper.Map <IEnumerable <BanknoteDto> >(retrievedBanknotes); var shapedBanknotes = banknotes.ShapeData(resourceParameters.Fields); if (mediaType == "application/json+hateoas") { if (!string.IsNullOrEmpty(resourceParameters.Fields) && !resourceParameters.Fields.ToLowerInvariant().Contains("id")) { return(BadRequest("Field parameter 'id' is required")); } var paginationMetadata = new { totalCount = retrievedBanknotes.TotalCount, pageSize = retrievedBanknotes.PageSize, currentPage = retrievedBanknotes.CurrentPage, totalPages = retrievedBanknotes.TotalPages }; Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(paginationMetadata)); var links = CreateBanknotesLinks(resourceParameters, retrievedBanknotes.HasNext, retrievedBanknotes.HasPrevious); var linkedBanknotes = shapedBanknotes.Select(banknote => { var banknoteAsDictionary = banknote as IDictionary <string, object>; var banknoteLinks = CreateBanknoteLinks((Guid)banknoteAsDictionary["Id"], resourceParameters.Fields); banknoteAsDictionary.Add("links", banknoteLinks); return(banknoteAsDictionary); }); var linkedCollectionResource = new LinkedCollectionResource { Value = linkedBanknotes, Links = links }; return(Ok(linkedCollectionResource)); } else { var previousPageLink = retrievedBanknotes.HasPrevious ? CreateBanknotesResourceUri(resourceParameters, ResourceUriType.PreviousPage) : null; var nextPageLink = retrievedBanknotes.HasNext ? CreateBanknotesResourceUri(resourceParameters, ResourceUriType.NextPage) : null; var paginationMetadata = new { totalCount = retrievedBanknotes.TotalCount, pageSize = retrievedBanknotes.PageSize, currentPage = retrievedBanknotes.CurrentPage, totalPages = retrievedBanknotes.TotalPages, previousPageLink, nextPageLink, }; Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(paginationMetadata)); return(Ok(shapedBanknotes)); } }
public IActionResult GetAuthors(AuthorsResourceParameters authorsResourceParameters, [FromHeader(Name = "Accept")] string mediaType) { if (!PropertyMappingService.ValidMappingExistsFor <AuthorDto, Author>(authorsResourceParameters.OrderBy)) { return(BadRequest()); } if (!TypeHelperService.TypeHasProperties <AuthorDto>(authorsResourceParameters.Fields)) { return(BadRequest()); } var authorsFromRepo = LibraryRepository.GetAuthors(authorsResourceParameters); if (authorsFromRepo == null) { return(NotFound()); } var authorsModel = Mapper.Map <IEnumerable <AuthorDto> >(authorsFromRepo); if (mediaType == "application/vnd.marvin.hateoas+json") { var paginationMetadata = new { totalCount = authorsFromRepo.TotalCount, pageSize = authorsFromRepo.PageSize, currentPage = authorsFromRepo.CurrentPage, totalPages = authorsFromRepo.TotalPages, }; Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(paginationMetadata)); var links = CreateLinksForAuthors(authorsResourceParameters, authorsFromRepo.HasNext, authorsFromRepo.HasPrevious); var shapedAuthors = authorsModel.ShapeData(authorsResourceParameters.Fields); var shapedAuthorsWithLinks = shapedAuthors.Select(author => { var authorAsDictionary = author as IDictionary <string, object>; var authorLinks = CreateLinksForAuthor((Guid)authorAsDictionary["Id"], authorsResourceParameters.Fields); authorAsDictionary.Add("links", authorLinks); return(authorAsDictionary); }); var linkedCollectionResource = new { values = shapedAuthorsWithLinks, links = links }; return(Ok(linkedCollectionResource)); } else { var previousPageLink = authorsFromRepo.HasPrevious ? CreateAuthorsResourceUri(authorsResourceParameters, ResourceUriType.PreviousPage) : null; var nextPageLink = authorsFromRepo.HasNext ? CreateAuthorsResourceUri(authorsResourceParameters, ResourceUriType.NextPage) : null; var paginationMetadata = new { totalCount = authorsFromRepo.TotalCount, pageSize = authorsFromRepo.PageSize, currentPage = authorsFromRepo.CurrentPage, totalPages = authorsFromRepo.TotalPages, previousPageLink = previousPageLink, nextPageLink = nextPageLink }; Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(paginationMetadata)); return(Ok(authorsModel.ShapeData(authorsResourceParameters.Fields))); } }
public void ReturnsTrueValidFields(string field, PropertyMappingService property) => Assert.True(property.ValidMappingExistsFor <DummyEntityDto, DummyEntity>(field));