public void GenerateLocationHeader_UsesEntitySetLinkBuilder_ToGenerateLocationHeader() { // Arrange string idLink = "http://id-link"; Uri editLink = new Uri(idLink); Mock <EntitySetLinkBuilderAnnotation> linkBuilder = new Mock <EntitySetLinkBuilderAnnotation>(); linkBuilder.Setup(b => b.BuildIdLink(It.IsAny <EntityInstanceContext>(), ODataMetadataLevel.Default)) .Returns(idLink); linkBuilder.Setup(b => b.BuildEditLink(It.IsAny <EntityInstanceContext>(), ODataMetadataLevel.Default, idLink)) .Returns(editLink); CustomersModelWithInheritance model = new CustomersModelWithInheritance(); model.Model.SetAnnotationValue(model.Customer, new ClrTypeAnnotation(typeof(TestEntity))); model.Model.SetEntitySetLinkBuilder(model.Customers, linkBuilder.Object); ODataPath path = new ODataPath(new EntitySetPathSegment(model.Customers)); HttpRequestMessage request = new HttpRequestMessage(); request.ODataProperties().Model = model.Model; request.ODataProperties().Path = path; CreatedODataResult <TestEntity> createdODataResult = GetCreatedODataResult(request); // Act var locationHeader = createdODataResult.GenerateLocationHeader(); // Assert Assert.Same(editLink, locationHeader); }
public void GenerateLocationHeader_ThrowsODataPathMissing_IfRequestDoesNotHaveODataPath() { HttpRequestMessage request = new HttpRequestMessage(); request.ODataProperties().Model = EdmCoreModel.Instance; CreatedODataResult <TestEntity> createdODataResult = GetCreatedODataResult(request); // Act & Assert Assert.Throws <InvalidOperationException>(() => createdODataResult.GenerateLocationHeader(), "The operation cannot be completed because no ODataPath is available for the request."); }
public void GenerateLocationHeader_ThrowsRequestMustHaveModel_IfRequestDoesNotHaveModel() { // Arrange HttpRequestMessage request = new HttpRequestMessage(); CreatedODataResult <TestEntity> createdODataResult = GetCreatedODataResult(request); // Act & Assert Assert.Throws <InvalidOperationException>(() => createdODataResult.GenerateLocationHeader(), "The request must have an associated EDM model. Consider using the extension method " + "HttpConfiguration.Routes.MapODataRoute to register a route that parses the OData URI and attaches the model information."); }
public void GenerateLocationHeader_ThrowsEntitySetMissingDuringSerialization_IfODataPathEntitySetIsNull() { ODataPath path = new ODataPath(); HttpRequestMessage request = new HttpRequestMessage(); request.ODataProperties().Model = EdmCoreModel.Instance; request.ODataProperties().Path = path; CreatedODataResult <TestEntity> createdODataResult = GetCreatedODataResult(request); // Act & Assert Assert.Throws <InvalidOperationException>(() => createdODataResult.GenerateLocationHeader(), "The related entity set could not be found from the OData path. The related entity set is required to serialize the payload."); }
public void GenerateLocationHeader_ThrowsEntityTypeNotInModel_IfContentTypeIsNotThereInModel() { CustomersModelWithInheritance model = new CustomersModelWithInheritance(); ODataPath path = new ODataPath(new EntitySetPathSegment(model.Customers)); HttpRequestMessage request = new HttpRequestMessage(); request.ODataProperties().Model = model.Model; request.ODataProperties().Path = path; CreatedODataResult <TestEntity> createdODataResult = GetCreatedODataResult(request); // Act & Assert Assert.Throws <InvalidOperationException>(() => createdODataResult.GenerateLocationHeader(), "Cannot find the entity type 'System.Web.Http.OData.Results.CreatedODataResultTest+TestEntity' in the model."); }
public void GenerateLocationHeader_ThrowsTypeMustBeEntity_IfMappingTypeIsNotEntity() { CustomersModelWithInheritance model = new CustomersModelWithInheritance(); ODataPath path = new ODataPath(new EntitySetPathSegment(model.Customers)); HttpRequestMessage request = new HttpRequestMessage(); request.ODataProperties().Model = model.Model; request.ODataProperties().Path = path; model.Model.SetAnnotationValue(model.Address, new ClrTypeAnnotation(typeof(TestEntity))); CreatedODataResult <TestEntity> createdODataResult = GetCreatedODataResult(request); // Act & Assert Assert.Throws <InvalidOperationException>(() => createdODataResult.GenerateLocationHeader(), "NS.Address is not an entity type. Only entity types are supported."); }
public void GenerateLocationHeader_ThrowsEditLinkNullForLocationHeader_IfEntitySetLinkBuilderReturnsNull() { // Arrange Mock <EntitySetLinkBuilderAnnotation> linkBuilder = new Mock <EntitySetLinkBuilderAnnotation>(); CustomersModelWithInheritance model = new CustomersModelWithInheritance(); model.Model.SetAnnotationValue(model.Customer, new ClrTypeAnnotation(typeof(TestEntity))); model.Model.SetEntitySetLinkBuilder(model.Customers, linkBuilder.Object); ODataPath path = new ODataPath(new EntitySetPathSegment(model.Customers)); HttpRequestMessage request = new HttpRequestMessage(); request.ODataProperties().Model = model.Model; request.ODataProperties().Path = path; CreatedODataResult <TestEntity> createdODataResult = GetCreatedODataResult(request); // Act Assert.Throws <InvalidOperationException>(() => createdODataResult.GenerateLocationHeader(), "The edit link builder for the entity set 'Customers' returned null. An edit link is required for the location header."); }