private bool CreateKeyedResourceInstance(ResourceContainer container, ResourceType resourceType, out KeyExpression key, out KeyedResourceInstance newResource) { int retryCount = 5; if (!_existingKeyMap.Keys.Contains(container)) { _existingKeyMap.Add(container, new KeyExpressions()); } newResource = resourceType.CreateRandomResource(container, new KeyExpressions(), false); // should not make request key = newResource.ResourceInstanceKey.CreateKeyExpression(container, resourceType); while (_existingKeyMap[container].Contains(key) && retryCount > 0) { newResource.ResourceInstanceKey = ResourceInstanceUtil.CreateUniqueKey(container, resourceType, new KeyExpressions(), new KeyExpressions()); key = newResource.ResourceInstanceKey.CreateKeyExpression(container, resourceType); retryCount--; } if (retryCount == 0) { newResource = null; key = null; return(false); } _existingKeyMap[container].Add(key); _existingEntityMap[key] = newResource; return(true); }
public KeyedResourceInstance CreateRandomResource(ResourceContainer container, KeyExpression specifiedKey) { List <ResourceInstanceProperty> properties = new List <ResourceInstanceProperty>(); foreach (ResourceProperty p in this.Properties.OfType <ResourceProperty>().Where(rp => rp.IsNavigation == false && rp.PrimaryKey == null)) { ResourceInstanceProperty property = p.CreateRandomResourceInstanceProperty(); properties.Add(property); } return(ResourceInstanceUtil.CreateKeyedResourceInstance(specifiedKey, container, properties.ToArray())); }
public static AstoriaRequest BuildInsert(Workspace workspace, ResourceContainer container, ResourceType type, HttpStatusCode expectedStatusCode, SerializationFormatKind format, out KeyExpression createdKey) { KeyedResourceInstance newResource = type.CreateRandomResource(container); if (newResource == null) { newResource = ResourceInstanceUtil.CreateKeyedResourceInstanceByClone(container, type); if (newResource == null) { createdKey = null; return(null); } } QueryNode query; if (!type.Key.Properties.Any(p => p.Facets.ServerGenerated) && newResource.ResourceInstanceKey != null) { createdKey = newResource.ResourceInstanceKey.CreateKeyExpression(container, type); query = ContainmentUtil.BuildCanonicalQuery(createdKey, true); } else { createdKey = null; // the key is unknown, must be server generated // in this case, lets hope that containment is a non-issue query = Query.From(Exp.Variable(container)) .Select(); if (!container.Facets.TopLevelAccess && expectedStatusCode == HttpStatusCode.Created) { expectedStatusCode = HttpStatusCode.BadRequest; } } AstoriaRequest request = workspace.CreateRequest(); request.Verb = RequestVerb.Post; request.Query = query; request.UpdateTree = newResource; request.ExpectedStatusCode = expectedStatusCode; request.Format = format; return(request); }
public ResourceInstanceProperty CreateRandomResourceInstanceProperty() { ResourceInstanceProperty instanceProperty; if (this.IsComplexType) { ComplexType ct = (ComplexType)this.Type; if (ct.Facets.AbstractType) { ct = ct.DerivedTypes.Where(t => !t.Facets.AbstractType).Choose(); } ComplexResourceInstance cri = ResourceInstanceUtil.CreateComplexResourceInstance(ct); return(instanceProperty = new ResourceInstanceComplexProperty(ct.Name, this.Name, cri)); } else { return(this.CreateRandomResourceSimpleInstanceProperty()); } }
protected string CreateCanonicalUri(ResourceInstanceKey key) { UriQueryBuilder builder = new UriQueryBuilder(Workspace, Workspace.ServiceUri); builder.UseBinaryFormatForDates = false; builder.CleanUpSpecialCharacters = false; KeyExpression keyExpression = ResourceInstanceUtil.ConvertToKeyExpression(key, Workspace); if (keyExpression != null) { QueryNode query = ContainmentUtil.BuildCanonicalQuery(keyExpression); return(builder.Build(query)); } IEnumerable <ResourceInstanceSimpleProperty> properties = key.KeyProperties.OfType <ResourceInstanceSimpleProperty>(); string keyString = builder.CreateKeyString(properties.Select(p => p.Name).ToArray(), properties.Select(p => p.PropertyValue).ToArray()); string uri = Workspace.ServiceUri + "/" + key.ResourceSetName + "(" + keyString + ")"; return(uri); }
internal KeyedResourceInstance CreateRandomResource(ResourceContainer container, KeyExpressions existingKeys, bool populateNavProps) { KeyExpressions relatedForeignKeys = new KeyExpressions(); ResourceInstanceKey key = ResourceInstanceUtil.CreateUniqueKey(container, this, relatedForeignKeys, existingKeys); List <ResourceInstanceProperty> properties = new List <ResourceInstanceProperty>(); // populate the non-key, non-navigation properties // foreach (ResourceProperty p in this.Properties.OfType <ResourceProperty>().Where(rp => rp.IsNavigation == false && rp.PrimaryKey == null)) { if (p.Facets.IsIdentity) { continue; } ResourceInstanceProperty property = p.CreateRandomResourceInstanceProperty(); properties.Add(property); } if (populateNavProps) { // populate the navigation properties, but don't go by key, as some foreign keys MAY NOT HAVE ASSOCIATED NAVIGATION PROPERTIES // foreach (ResourceProperty p in this.Properties.OfType <ResourceProperty>().Where(rp => rp.IsNavigation)) { // find a key for this navigation property KeyExpression navPropKey = null; foreach (KeyExpression keyExp in relatedForeignKeys) { //if (p.Type.Equals(keyExp.ResourceType) // || p.Type is ResourceType && (p.Type as ResourceType).BaseTypes.Contains(keyExp.ResourceType)) if (p.OtherAssociationEnd.ResourceType.Equals(keyExp.ResourceType)) { navPropKey = keyExp; break; } } ResourceContainer associatedContainer = container.FindDefaultRelatedContainer(p); if (navPropKey == null) { if (p.OtherAssociationEnd.ResourceType.Key.Properties.OfType <ResourceProperty>() .Where(rp => rp.ForeignKeys.Any()) .Any(rp => rp.ForeignKeys.First().PrimaryKey.Properties.OfType <ResourceProperty>().First().ResourceType.Equals(this))) { // this association has a fk back to the current type, so it cannot be based on any existing entity AstoriaTestLog.WriteLineIgnore("Skipping nav prop '{0}.{1}' due to foreign key constraint on entity being created", this.Name, p.Name); continue; } else { navPropKey = associatedContainer.Workspace.GetRandomExistingKey(associatedContainer, p.OtherAssociationEnd.ResourceType); } } if (navPropKey != null) { if (p.OtherAssociationEnd.Multiplicity == Multiplicity.Many) { properties.Add(ResourceInstanceUtil.CreateCollectionInstanceProperty(new KeyExpressions(navPropKey), navPropKey.ResourceContainer, p)); } else { properties.Add(ResourceInstanceUtil.CreateRefInstanceProperty(navPropKey, navPropKey.ResourceContainer, p)); } } } } return(ResourceInstanceUtil.CreateKeyedResourceInstance(key.CreateKeyExpression(container, this), container, properties.ToArray())); }