private IEntity LocateResource(ConnectorRequest request, ApiResourceMapping mapping, out ConnectorResponse errorResponse) { // Check path if (request.ApiPath == null || request.ApiPath.Length < 3) { errorResponse = new ConnectorResponse(HttpStatusCode.MethodNotAllowed); return(null); } if (request.ApiPath.Length > 3) { errorResponse = new ConnectorResponse(HttpStatusCode.NotFound); return(null); } // Get resource ID field value (i.e. the string being used to locate the resource, e.g. name) string resourceIdFieldValue = request.ApiPath [2]; if (string.IsNullOrEmpty(resourceIdFieldValue)) { throw new Exception("Empty resource identity was received."); // assert false .. this should be blocked earlier. } // Resolve resource from string IResourceResolver resolver = _resourceResolverProvider.GetResolverForResourceMapping(mapping); ResourceResolverEntry resolveResult = resolver.ResolveResource(resourceIdFieldValue); if (resolveResult.Entity == null) { throw new WebArgumentNotFoundException( ); // Resource could not be located, so ensure a 404 is thrown } errorResponse = null; return(resolveResult.Entity); }
public void Test_Resolve_WithMapping_NotFound(string identity, string type, ResourceResolverError expectedError) { IResourceResolverProvider provider = GetResolverProvider( ); ApiResourceMapping mapping = new ApiResourceMapping( ); mapping.MappedType = Entity.Get <EntityType>("test:allFields"); mapping.Save( ); IResourceResolver resolver = provider.GetResolverForResourceMapping(mapping); ResourceResolverEntry entry = resolver.ResolveResource(identity); Assert.That(entry.Entity, Is.Null); Assert.That(entry.Error, Is.EqualTo(expectedError)); }
public void Test_ResolveResource_WithMapping_NoIdentityField_ByName(string identity, string type) { IResourceResolverProvider provider = GetResolverProvider( ); ApiResourceMapping mapping = new ApiResourceMapping( ); mapping.MappedType = Entity.Get <EntityType>(type); mapping.Save( ); IResourceResolver resolver = provider.GetResolverForResourceMapping(mapping); ResourceResolverEntry entry = resolver.ResolveResource(identity); IEntity entity = entry.Entity; Assert.That(entity, Is.Not.Null); Assert.That(entity.As <Resource>( ).Name, Is.EqualTo(identity)); }
public void Test_ResolveResource_WithMapping_NoIdentityField_ByGuid(string alias, string type, string name) { IEntity entity1 = Entity.Get(new EntityRef(alias)); string identity = entity1.UpgradeId.ToString(); IResourceResolverProvider provider = GetResolverProvider( ); ApiResourceMapping mapping = new ApiResourceMapping( ); mapping.MappedType = Entity.Get <EntityType>(type); mapping.Save( ); IResourceResolver resolver = provider.GetResolverForResourceMapping(mapping); ResourceResolverEntry entry = resolver.ResolveResource(identity); IEntity entity = entry.Entity; Assert.That(entity, Is.Not.Null); Assert.That(entity.As <Resource>( ).Name, Is.EqualTo(name)); }
public void Test_NullMapper( ) { IResourceResolverProvider provider = GetResolverProvider( ); Assert.Throws <ArgumentNullException>(() => provider.GetResolverForResourceMapping((ApiResourceMapping)null)); }