Пример #1
0
        private static DSPContext GetDefaultData(DSPMetadata metadata)
        {
            var peopleType = metadata.GetResourceType("PeopleType");
            var officeType = metadata.GetResourceType("OfficeType");

            #region Default Data for the Model
            var context = new DSPContext();

            DSPResource people1 = new DSPResource(peopleType);
            people1.SetValue("ID", 1);
            people1.SetValue("Name", "Foo");
            people1.SetValue("Body", Encoding.UTF8.GetBytes("a byte array"));
            people1.SetValue("Age", 23);

            var office = new DSPResource(officeType);
            office.SetValue("Building", "Building 18");
            office.SetValue("OfficeNumber", 100);
            people1.SetValue("Office", office);

            var people = context.GetResourceSetEntities("People");
            people.Add(people1);

            #endregion Default Data for the Model

            return(context);
        }
 public ResourceChange(string collectionName, DSPResource resource, Action <DocumentDbContext, ResourceChange> action)
 {
     this.CollectionName     = collectionName;
     this.Resource           = resource;
     this.Action             = action;
     this.ModifiedProperties = new Dictionary <string, object>();
 }
Пример #3
0
        private DSPServiceDefinition CreateCollectionReadService(Dictionary <Type, object> data)
        {
            DSPMetadata metadata   = new DSPMetadata("SpatialServerIntegration", "AstoriaUnitTests.Tests");
            var         entityType = metadata.AddEntityType("EntityType", null, null, false);

            metadata.AddResourceSet("Entities", entityType);
            metadata.AddKeyProperty(entityType, "ID", typeof(int));
            foreach (Type t in data.Keys)
            {
                metadata.AddCollectionProperty(entityType, "Collection" + t.Name, t);
            }

            DSPContext dataSource = new DSPContext();
            var        entities   = dataSource.GetResourceSetEntities("Entities");
            var        resource   = new DSPResource(entityType);

            resource.SetValue("ID", 0);

            foreach (KeyValuePair <Type, object> d in data)
            {
                resource.SetValue("Collection" + d.Key.Name, d.Value);
            }

            entities.Add(resource);

            return(new DSPServiceDefinition()
            {
                Metadata = metadata,
                DataSource = dataSource
            });
        }
Пример #4
0
        public static DSPResource CreateDSPResource(JToken document, DocumentDbMetadata dbMetadata, string resourceName, string ownerPrefix = null)
        {
            var resourceType = dbMetadata.ResolveResourceType(resourceName, ownerPrefix);

            if (resourceType == null)
            {
                throw new ArgumentException(string.Format("Unable to resolve resource type {0}", resourceName), "resourceName");
            }
            var resource = new DSPResource(resourceType);

            foreach (var element in document)
            {
                var resourceProperty = dbMetadata.ResolveResourceProperty(resourceType, element as JProperty);
                if (resourceProperty == null)
                {
                    continue;
                }

                object propertyValue = ConvertJsonValue(element as JProperty, resourceType, resourceProperty, resourceProperty.Name, dbMetadata);
                resource.SetValue(resourceProperty.Name, propertyValue);
            }
            AssignNullCollections(resource, resourceType);

            return(resource);
        }
Пример #5
0
        private static DSPResource CreateResource(DSPMetadata metadata, string entityTypeName, int idValue, KeyValuePair <string, object>[] propertyValues, bool useComplexType)
        {
            var entityType = metadata.GetResourceType(entityTypeName);

            DSPResource entity;

            if (useComplexType)
            {
                entity = new DSPResource(entityType);
            }
            else
            {
                entity = new DSPResource(entityType, propertyValues);
            }

            entity.SetValue("ID", idValue);

            DSPResource resourceForProperties;

            if (useComplexType)
            {
                string complexTypeName = GetComplexTypeName(entityTypeName);
                var    complexType     = metadata.GetResourceType(complexTypeName);
                resourceForProperties = new DSPResource(complexType, propertyValues);
                entity.SetValue("ComplexProperty", resourceForProperties);
            }
            else
            {
                resourceForProperties = entity;
            }
            return(entity);
        }
Пример #6
0
        private static void PopulateResourceSet(DSPContext context, DSPResource resource)
        {
            string resourceSetName = GetResourceSetName(resource.ResourceType.Name);
            var    resourceSet     = context.GetResourceSetEntities(resourceSetName);

            resourceSet.AddRange(new object[] { resource });
        }
Пример #7
0
        /// <summary>
        /// Create a service for a specific spatial property type.
        /// </summary>
        /// <remarks>
        /// DEVNOTE(pqian):
        /// The service will populate with properties named after the property type, with a sequence number
        /// indicating which test sample it holds. For example, if the method is called with 3 sample data,
        /// and the target type is GeographyPoint, then the service will look like
        /// EntityType:
        /// ID
        /// GeographyPoint1
        /// GeographyPoint2
        /// GeographyPoint3
        /// </remarks>
        /// <typeparam name="T">The target type</typeparam>
        /// <param name="data">Sample Data</param>
        /// <param name="overrideType">Use this type instead of typeof(T)</param>
        /// <param name="openProperty">Use Open Type</param>
        /// <param name="writable">Writable service</param>
        /// <returns>The constructed service definition</returns>
        private DSPServiceDefinition CreateSpatialPropertyService <T>(T[] data, Type overrideType = null, bool openProperty = false, bool writable = false)
        {
            Type propertyType = overrideType ?? typeof(T);

            DSPMetadata metadata   = new DSPMetadata("SpatialServerIntegration", "AstoriaUnitTests.Tests");
            var         entityType = metadata.AddEntityType("EntityType", null, null, false);

            metadata.AddResourceSet("Entities", entityType);
            metadata.AddKeyProperty(entityType, "ID", typeof(int));

            if (!openProperty)
            {
                for (int i = 0; i < data.Length; ++i)
                {
                    metadata.AddPrimitiveProperty(entityType, propertyType.Name + i, propertyType);
                }
            }

            entityType.IsOpenType = openProperty;

            var definition = new DSPServiceDefinition()
            {
                Metadata = metadata
            };

            if (writable)
            {
                definition.CreateDataSource = (m) => new DSPContext();
                definition.Writable         = true;
            }
            else
            {
                DSPContext dataSource = new DSPContext();
                var        entities   = dataSource.GetResourceSetEntities("Entities");
                var        resource   = new DSPResource(entityType);
                resource.SetValue("ID", 0);

                for (int i = 0; i < data.Length; ++i)
                {
                    if (!openProperty)
                    {
                        metadata.AddPrimitiveProperty(entityType, propertyType.Name + i, propertyType);
                    }

                    resource.SetValue(propertyType.Name + i, data[i]);
                }

                entities.Add(resource);
                definition.DataSource = dataSource;
            }

            return(definition);
        }
Пример #8
0
        public void AllowRedefiningConcurrencyTokenOnDerivedType()
        {
            var metadataProvider = new DSPMetadata("DefaultContainer", "Default");

            var entityType = metadataProvider.AddEntityType("EntityType", null, null, false /*isAbstract*/);

            metadataProvider.AddKeyProperty(entityType, "ID", typeof(int));
            metadataProvider.AddPrimitiveProperty(entityType, "LastUpdatedAuthor", typeof(string), true /*eTag*/);

            var derivedType = metadataProvider.AddEntityType("DerivedType", null, entityType, false /*isAbstract*/);

            metadataProvider.AddPrimitiveProperty(derivedType, "LastModifiedAuthor", typeof(string), true /*eTag*/);

            metadataProvider.AddResourceSet("Customers", entityType);

            var wrapper = new DataServiceMetadataProviderWrapper(metadataProvider);

            DSPResource baseTypeInstance = new DSPResource(entityType);

            baseTypeInstance.SetValue("ID", 1);
            baseTypeInstance.SetValue("LastUpdatedAuthor", "Phani");

            DSPResource derivedTypeInstance = new DSPResource(derivedType);

            derivedTypeInstance.SetValue("ID", 1);
            derivedTypeInstance.SetValue("LastModifiedAuthor", "Raj");

            DSPContext dataContext = new DSPContext();
            var        entities    = dataContext.GetResourceSetEntities("Customers");

            entities.AddRange(new object[] { baseTypeInstance, derivedTypeInstance });

            var service = new DSPUnitTestServiceDefinition(metadataProvider, DSPDataProviderKind.Reflection, dataContext);

            using (TestWebRequest request = service.CreateForInProcess())
            {
                try
                {
                    request.StartService();
                    request.RequestUriString = "/$metadata";
                    request.SendRequestAndCheckResponse();
                }
                finally
                {
                    request.StopService();
                }
            }
        }
Пример #9
0
        internal static DSPResource CreateTripLegResource(DSPMetadata roadTripMetadata, int id, ITestGeography geography1, ITestGeography geography2, bool useComplexType, Action <string, List <KeyValuePair <string, object> > > modifyPropertyValues)
        {
            List <KeyValuePair <string, object> > tripLegPropertyValues = new List <KeyValuePair <string, object> >()
            {
                new KeyValuePair <string, object>("GeographyProperty1", geography1.AsGeography()),
                new KeyValuePair <string, object>("GeographyProperty2", geography2.AsGeography()),
            };

            if (modifyPropertyValues != null)
            {
                modifyPropertyValues("TripLeg", tripLegPropertyValues);
            }
            DSPResource tripLegResource = CreateResource(roadTripMetadata, "TripLeg", id, tripLegPropertyValues.ToArray(), useComplexType);

            return(tripLegResource);
        }
Пример #10
0
        internal static DSPResource CreateRestStopResource(DSPMetadata roadTripMetadata, int id, ITestGeography location, bool useComplexType, Action <string, List <KeyValuePair <string, object> > > modifyPropertyValues)
        {
            List <KeyValuePair <string, object> > restStopPropertyValues = new List <KeyValuePair <string, object> >()
            {
                new KeyValuePair <string, object>("GeographyProperty", location.AsGeography()),
            };

            if (modifyPropertyValues != null)
            {
                modifyPropertyValues("RestStop", restStopPropertyValues);
            }

            DSPResource restStopResource = CreateResource(roadTripMetadata, "RestStop", id, restStopPropertyValues.ToArray(), useComplexType);

            return(restStopResource);
        }
Пример #11
0
        internal static DSPResource CreateAmusementParkResource(DSPMetadata roadTripMetadata, int id, ITestGeography location, string name, bool useComplexType, Action <string, List <KeyValuePair <string, object> > > modifyPropertyValues)
        {
            List <KeyValuePair <string, object> > amusementParkPropertyValues = new List <KeyValuePair <string, object> >()
            {
                new KeyValuePair <string, object>("GeographyProperty", location.AsGeography()),
                new KeyValuePair <string, object>("Name", name),
            };

            if (modifyPropertyValues != null)
            {
                modifyPropertyValues("AmusementPark", amusementParkPropertyValues);
            }

            DSPResource amusementParkResource = CreateResource(roadTripMetadata, "AmusementPark", id, amusementParkPropertyValues.ToArray(), useComplexType);

            return(amusementParkResource);
        }
        /// <summary>
        /// Creates a datasource from the given metadata.
        /// </summary>
        /// <param name="metadata">The metadata against which the datasource is created.</param>
        /// <returns>DSPContext representing the data source.</returns>
        private static DSPContext CreateDataSource(DSPMetadata metadata)
        {
            DSPContext   context      = new DSPContext();
            ResourceType customerType = metadata.GetResourceType("CustomerEntity");
            DSPResource  entity1      = new DSPResource(customerType);

            entity1.SetValue("ID", 1);
            entity1.SetValue("Name", "Vineet");

            DSPResource entity2 = new DSPResource(customerType);

            entity2.SetValue("ID", 2);
            entity2.SetValue("Name", "Jimmy");

            context.GetResourceSetEntities("CustomerEntities").Add(entity1);
            context.GetResourceSetEntities("CustomerEntities").Add(entity2);
            return(context);
        }
Пример #13
0
 private static void AssignNullCollections(DSPResource resource, ResourceType resourceType)
 {
     foreach (var resourceProperty in resourceType.Properties)
     {
         var propertyValue = resource.GetValue(resourceProperty.Name);
         if (resourceProperty.Kind == ResourcePropertyKind.Collection)
         {
             if (propertyValue == null)
             {
                 resource.SetValue(resourceProperty.Name, new object[0]);
             }
         }
         else if (propertyValue is DSPResource)
         {
             AssignNullCollections(propertyValue as DSPResource, resourceProperty.ResourceType);
         }
     }
 }
Пример #14
0
        internal static DSPContext PopulateRoadTripData(DSPMetadata roadTripMetadata, GeographyPropertyValues defaultValues, bool useComplexType, Action <string, List <KeyValuePair <string, object> > > modifyPropertyValues = null)
        {
            var context = new DSPContext();

            DSPResource tripLegResource = CreateTripLegResource(roadTripMetadata, DefaultId, defaultValues.TripLegGeography1, defaultValues.TripLegGeography2, useComplexType, modifyPropertyValues);

            PopulateResourceSet(context, tripLegResource);

            DSPResource amusementParkResource = CreateAmusementParkResource(roadTripMetadata, DefaultId, defaultValues.AmusementParkGeography, "Disneyland", useComplexType, modifyPropertyValues);

            PopulateResourceSet(context, amusementParkResource);

            DSPResource restStopResource = CreateRestStopResource(roadTripMetadata, DefaultId, defaultValues.RestStopGeography, useComplexType, modifyPropertyValues);

            PopulateResourceSet(context, restStopResource);

            return(context);
        }
Пример #15
0
        public static BsonDocument CreateBSonDocument(DSPResource resource, MongoMetadata mongoMetadata, string resourceName)
        {
            var document    = new BsonDocument();
            var resourceSet = mongoMetadata.ResolveResourceSet(resourceName);

            if (resourceSet != null)
            {
                foreach (var property in resourceSet.ResourceType.Properties)
                {
                    var propertyValue = resource.GetValue(property.Name);
                    if (propertyValue != null)
                    {
                        document.Set(property.Name, BsonValue.Create(propertyValue));
                    }
                }
            }
            return(document);
        }
Пример #16
0
        private static void ExecuteUpdate(DSPResource resource, int id, TestWebRequest baseRequest, string httpMethod, string preferHeader, bool useBatch, DSPResourceSerializerFormat payloadFormat)
        {
            string payload = DSPResourceSerializer.WriteEntity(resource, payloadFormat);

            bool isPost = httpMethod == "POST";
            bool expectedReturnContent = preferHeader == "return=representation" || isPost && preferHeader == null;

            string uriSuffix = isPost ? String.Empty : String.Format("({0})", id);

            TestWebRequest request = useBatch ? new InMemoryWebRequest() : baseRequest;

            request.RequestUriString         = String.Format("/{0}s{1}", resource.ResourceType.Name, uriSuffix);
            request.HttpMethod               = httpMethod;
            request.RequestVersion           = "4.0;";
            request.RequestMaxVersion        = "4.0;";
            request.RequestHeaders["Prefer"] = preferHeader;
            request.Accept             = payloadFormat == DSPResourceSerializerFormat.Atom ? "application/atom+xml,application/xml" : UnitTestsUtil.JsonLightMimeType;
            request.RequestContentType = "application/atom+xml";
            request.SetRequestStreamAsText(payload);

            if (useBatch)
            {
                var batchRequest = new BatchWebRequest();
                var changeset    = new BatchWebRequest.Changeset();
                changeset.Parts.Add((InMemoryWebRequest)request);
                batchRequest.Changesets.Add(changeset);
                batchRequest.SendRequest(baseRequest);

                Assert.IsTrue(UnitTestsUtil.IsSuccessStatusCode(baseRequest.ResponseStatusCode), "Unexpected error occurred on batch.");
            }
            else
            {
                request.SendRequest();
            }

            Assert.IsTrue(UnitTestsUtil.IsSuccessStatusCode(request.ResponseStatusCode), "Unexpected error occurred when sending the request.");
            if (expectedReturnContent)
            {
                // If the request is expected to return content, verify there were no instream errors
                Exception e            = request.ParseResponseInStreamError();
                string    errorMessage = e != null ? e.Message : string.Empty;
                Assert.IsNull(e, "Expected no exception, but got the following error", errorMessage);
            }
        }
Пример #17
0
        public static JObject CreateJsonDocument(DSPResource resource, DocumentDbMetadata dbMetadata, string resourceName)
        {
            var document    = new JObject();
            var resourceSet = dbMetadata.ResolveResourceSet(resourceName);

            if (resourceSet != null)
            {
                foreach (var property in resourceSet.ResourceType.Properties)
                {
                    var propertyValue = resource.GetValue(property.Name);
                    if (propertyValue != null)
                    {
                        var text = JsonConvert.SerializeObject(propertyValue);
                        document.Add(property.Name, JObject.Parse(text));
                    }
                }
            }
            return(document);
        }
Пример #18
0
        private static DSPServiceDefinition SetupService()
        {
            DSPMetadata metadata = new DSPMetadata("ActionsWithLargePayload", "AstoriaUnitTests.ActionTestsWithLargePayload");
            var         customer = metadata.AddEntityType("Customer", null, null, false);

            metadata.AddKeyProperty(customer, "ID", typeof(int));
            var customers = metadata.AddResourceSet("Customers", customer);

            ComplexType = metadata.AddComplexType("AddressComplexType", null, null, false);
            metadata.AddPrimitiveProperty(ComplexType, "ZipCode", typeof(int));
            metadata.AddPrimitiveProperty(ComplexType, "City", typeof(string));

            DSPContext context = new DSPContext();

            metadata.SetReadOnly();

            DSPResource customer1 = new DSPResource(customer);

            customer1.SetValue("ID", 1);
            context.GetResourceSetEntities("Customers").Add(customer1);

            MyDSPActionProvider actionProvider = new MyDSPActionProvider();

            SetUpActionWithLargeParameterPayload(actionProvider, metadata, customer);
            SetUpActionWithLargeCollectionParameterPayload(actionProvider, metadata, customer);
            SetupLargeNumberOfActions(actionProvider, metadata, customer);

            DSPServiceDefinition service = new DSPServiceDefinition()
            {
                Metadata           = metadata,
                CreateDataSource   = (m) => context,
                ForceVerboseErrors = true,
                Writable           = true,
                ActionProvider     = actionProvider,
            };

            return(service);
        }
Пример #19
0
        private DSPResource CreateDSPResource(ResourceType resourceType, BsonDocument bsonDocument)
        {
            var resource = new DSPResource(resourceType);

            foreach (var element in bsonDocument.Elements)
            {
                if (element.Name == "_id")
                {
                    resource.SetValue("ID", element.Value.ToString());
                    this.resourceMap.Add(element.Value.AsObjectId, resource);
                }
                else if (element.Value.GetType() == typeof(BsonDocument))
                {
                    this.resourceReferences.Add(new Tuple <ObjectId, string, ObjectId>(
                                                    bsonDocument["_id"].AsObjectId, element.Name, element.Value.AsBsonDocument["_id"].AsObjectId));
                }
                else if (element.Value.GetType() == typeof(BsonArray))
                {
                    var bsonArray = element.Value.AsBsonArray;
                    if (bsonArray != null && bsonArray.Count > 0)
                    {
                        var tuple = new Tuple <ObjectId, string, List <ObjectId> >(
                            bsonDocument["_id"].AsObjectId, element.Name, new List <ObjectId>());
                        resourceSetReferences.Add(tuple);
                        foreach (var item in bsonArray)
                        {
                            tuple.Item3.Add(item.AsBsonDocument["_id"].AsObjectId);
                        }
                    }
                }
                else if (element.Value.RawValue != null)
                {
                    resource.SetValue(element.Name, element.Value.RawValue);
                }
            }
            return(resource);
        }
Пример #20
0
        internal static DSPServiceDefinition SetUpNamedStreamService()
        {
            DSPMetadata metadata = new DSPMetadata("NamedStreamIDSPContainer", "NamedStreamTest");

            // entity with streams
            ResourceType entityWithNamedStreams = metadata.AddEntityType("EntityWithNamedStreams", null, null, false);

            metadata.AddKeyProperty(entityWithNamedStreams, "ID", typeof(int));
            metadata.AddPrimitiveProperty(entityWithNamedStreams, "Name", typeof(string));
            ResourceProperty streamInfo1 = new ResourceProperty("Stream1", ResourcePropertyKind.Stream, ResourceType.GetPrimitiveResourceType(typeof(Stream)));

            entityWithNamedStreams.AddProperty(streamInfo1);
            ResourceProperty streamInfo2 = new ResourceProperty("Stream2", ResourcePropertyKind.Stream, ResourceType.GetPrimitiveResourceType(typeof(Stream)));

            entityWithNamedStreams.AddProperty(streamInfo2);

            // entity1 with streams
            ResourceType entityWithNamedStreams1 = metadata.AddEntityType("EntityWithNamedStreams1", null, null, false);

            metadata.AddKeyProperty(entityWithNamedStreams1, "ID", typeof(int));
            metadata.AddPrimitiveProperty(entityWithNamedStreams1, "Name", typeof(string));
            ResourceProperty refStreamInfo1 = new ResourceProperty("RefStream1", ResourcePropertyKind.Stream, ResourceType.GetPrimitiveResourceType(typeof(Stream)));

            entityWithNamedStreams1.AddProperty(refStreamInfo1);

            // entity2 with streams
            ResourceType entityWithNamedStreams2 = metadata.AddEntityType("EntityWithNamedStreams2", null, null, false);

            metadata.AddKeyProperty(entityWithNamedStreams2, "ID", typeof(string));
            metadata.AddPrimitiveProperty(entityWithNamedStreams2, "Name", typeof(string));
            ResourceProperty collectionStreamInfo = new ResourceProperty("ColStream", ResourcePropertyKind.Stream, ResourceType.GetPrimitiveResourceType(typeof(Stream)));

            entityWithNamedStreams2.AddProperty(collectionStreamInfo);

            ResourceSet set1 = metadata.AddResourceSet("MySet1", entityWithNamedStreams);
            ResourceSet set2 = metadata.AddResourceSet("MySet2", entityWithNamedStreams1);
            ResourceSet set3 = metadata.AddResourceSet("MySet3", entityWithNamedStreams2);

            // add navigation property to entityWithNamedStreams
            metadata.AddResourceReferenceProperty(entityWithNamedStreams, "Ref", set2, entityWithNamedStreams1);
            metadata.AddResourceSetReferenceProperty(entityWithNamedStreams, "Collection", set3, entityWithNamedStreams2);
            metadata.AddResourceSetReferenceProperty(entityWithNamedStreams2, "Collection1", set2, entityWithNamedStreams1);

            DSPServiceDefinition service = new DSPServiceDefinition();

            service.Metadata             = metadata;
            service.MediaResourceStorage = new DSPMediaResourceStorage();
            service.SupportMediaResource = true;
            service.SupportNamedStream   = true;
            service.ForceVerboseErrors   = true;
            service.PageSizeCustomizer   = (config, type) =>
            {
                config.SetEntitySetPageSize("MySet3", 1);
            };

            // populate data
            DSPContext context = new DSPContext();

            DSPResource entity1 = new DSPResource(entityWithNamedStreams);
            {
                context.GetResourceSetEntities("MySet1").Add(entity1);

                entity1.SetValue("ID", 1);
                entity1.SetValue("Name", "Foo");
                DSPMediaResource namedStream1 = service.MediaResourceStorage.CreateMediaResource(entity1, streamInfo1);
                namedStream1.ContentType = "image/jpeg";
                byte[] data1 = new byte[] { 0, 1, 2, 3, 4 };
                namedStream1.GetWriteStream().Write(data1, 0, data1.Length);

                DSPMediaResource namedStream2 = service.MediaResourceStorage.CreateMediaResource(entity1, streamInfo2);
                namedStream2.ContentType = "image/jpeg";
                byte[] data2 = new byte[] { 0, 1, 2, 3, 4 };
                namedStream2.GetWriteStream().Write(data2, 0, data2.Length);
            }

            DSPResource entity2 = new DSPResource(entityWithNamedStreams1);

            {
                context.GetResourceSetEntities("MySet2").Add(entity2);

                entity2.SetValue("ID", 3);
                entity2.SetValue("Name", "Bar");
                DSPMediaResource refNamedStream1 = service.MediaResourceStorage.CreateMediaResource(entity2, refStreamInfo1);
                refNamedStream1.ContentType = "image/jpeg";
                byte[] data1 = new byte[] { 0, 1, 2, 3, 4 };
                refNamedStream1.GetWriteStream().Write(data1, 0, data1.Length);

                // set the navigation property
                entity1.SetValue("Ref", entity2);
            }

            {
                DSPResource entity3 = new DSPResource(entityWithNamedStreams2);
                context.GetResourceSetEntities("MySet3").Add(entity3);

                entity3.SetValue("ID", "ABCDE");
                entity3.SetValue("Name", "Bar");
                DSPMediaResource stream = service.MediaResourceStorage.CreateMediaResource(entity3, collectionStreamInfo);
                stream.ContentType = "image/jpeg";
                byte[] data1 = new byte[] { 0, 1, 2, 3, 4 };
                stream.GetWriteStream().Write(data1, 0, data1.Length);
                entity3.SetValue("Collection1", new List <DSPResource>()
                {
                    entity2
                });

                DSPResource entity4 = new DSPResource(entityWithNamedStreams2);
                context.GetResourceSetEntities("MySet3").Add(entity4);

                entity4.SetValue("ID", "XYZ");
                entity4.SetValue("Name", "Foo");
                DSPMediaResource stream1 = service.MediaResourceStorage.CreateMediaResource(entity3, collectionStreamInfo);
                stream1.ContentType = "image/jpeg";
                stream1.GetWriteStream().Write(data1, 0, data1.Length);
                entity4.SetValue("Collection1", new List <DSPResource>()
                {
                    entity2
                });

                entity1.SetValue("Collection", new List <DSPResource>()
                {
                    entity3, entity4
                });
            }

            service.DataSource = context;
            return(service);
        }
Пример #21
0
        protected override DSPContext CreateDataSource()
        {
            DSPContext context = new DSPContext();

            ResourceSet productsSet, categoriesSet;

            this.Metadata.TryResolveResourceSet("Products", out productsSet);
            this.Metadata.TryResolveResourceSet("Categories", out categoriesSet);
            IList <DSPResource> products   = context.GetResourceSetEntities(productsSet.Name);
            IList <DSPResource> categories = context.GetResourceSetEntities(categoriesSet.Name);

            var categoryFood = new DSPResource(categoriesSet.ResourceType);

            categoryFood.SetValue("ID", 0);
            categoryFood.SetValue("Name", "Food");
            categoryFood.SetValue("Products", new List <DSPResource>());
            categories.Add(categoryFood);

            var categoryBeverages = new DSPResource(categoriesSet.ResourceType);

            categoryBeverages.SetValue("ID", 1);
            categoryBeverages.SetValue("Name", "Beverages");
            categoryBeverages.SetValue("Products", new List <DSPResource>());
            categories.Add(categoryBeverages);

            var categoryElectronics = new DSPResource(categoriesSet.ResourceType);

            categoryElectronics.SetValue("ID", 2);
            categoryElectronics.SetValue("Name", "Electronics");
            categoryElectronics.SetValue("Products", new List <DSPResource>());
            categories.Add(categoryElectronics);

            var productBread = new DSPResource(productsSet.ResourceType);

            productBread.SetValue("ID", 0);
            productBread.SetValue("Name", "Bread");
            productBread.SetValue("Description", "Whole grain bread");
            productBread.SetValue("ReleaseDate", new DateTime(1992, 1, 1));
            productBread.SetValue("DiscontinueDate", null);
            productBread.SetValue("Rating", 4);
            productBread.SetValue("Category", categoryFood);
            products.Add(productBread);

            var productMilk = new DSPResource(productsSet.ResourceType);

            productMilk.SetValue("ID", 1);
            productMilk.SetValue("Name", "Milk");
            productMilk.SetValue("Description", "Low fat milk");
            productMilk.SetValue("ReleaseDate", new DateTime(1995, 10, 21));
            productMilk.SetValue("DiscontinueDate", null);
            productMilk.SetValue("Rating", 3);
            productMilk.SetValue("Category", categoryBeverages);
            products.Add(productMilk);

            var productWine = new DSPResource(productsSet.ResourceType);

            productWine.SetValue("ID", 2);
            productWine.SetValue("Name", "Wine");
            productWine.SetValue("Description", "Red wine, year 2003");
            productWine.SetValue("ReleaseDate", new DateTime(2003, 11, 24));
            productWine.SetValue("DiscontinueDate", new DateTime(2008, 3, 1));
            productWine.SetValue("Rating", 5);
            productWine.SetValue("Category", categoryBeverages);
            products.Add(productWine);

            ((List <DSPResource>)categoryFood.GetValue("Products")).Add(productBread);
            ((List <DSPResource>)categoryBeverages.GetValue("Products")).Add(productMilk);
            ((List <DSPResource>)categoryBeverages.GetValue("Products")).Add(productWine);

            return(context);
        }
Пример #22
0
        private DSPUnitTestServiceDefinition CreateTestService(bool openType = false)
        {
            DSPMetadata metadata   = new DSPMetadata("SpatialQueryTests", "AstoriaUnitTests.Tests");
            var         entityType = metadata.AddEntityType("SpatialEntity", null, null, false);

            metadata.AddKeyProperty(entityType, "ID", typeof(int));
            entityType.IsOpenType = openType;

            if (!openType)
            {
                metadata.AddPrimitiveProperty(entityType, "Geography", typeof(Geography));
                metadata.AddPrimitiveProperty(entityType, "Point", typeof(GeographyPoint));
                metadata.AddPrimitiveProperty(entityType, "Point2", typeof(GeographyPoint));
                metadata.AddPrimitiveProperty(entityType, "LineString", typeof(GeographyLineString));
                metadata.AddPrimitiveProperty(entityType, "Polygon", typeof(GeographyPolygon));
                metadata.AddPrimitiveProperty(entityType, "GeographyCollection", typeof(GeographyCollection));
                metadata.AddPrimitiveProperty(entityType, "MultiPoint", typeof(GeographyMultiPoint));
                metadata.AddPrimitiveProperty(entityType, "MultiLineString", typeof(GeographyMultiLineString));
                metadata.AddPrimitiveProperty(entityType, "MultiPolygon", typeof(GeographyMultiPolygon));

                metadata.AddPrimitiveProperty(entityType, "Geometry", typeof(Geometry));
                metadata.AddPrimitiveProperty(entityType, "GeometryPoint", typeof(GeometryPoint));
                metadata.AddPrimitiveProperty(entityType, "GeometryPoint2", typeof(GeometryPoint));
                metadata.AddPrimitiveProperty(entityType, "GeometryLineString", typeof(GeometryLineString));
                metadata.AddPrimitiveProperty(entityType, "GeometryPolygon", typeof(GeometryPolygon));
                metadata.AddPrimitiveProperty(entityType, "GeometryCollection", typeof(GeometryCollection));
                metadata.AddPrimitiveProperty(entityType, "GeometryMultiPoint", typeof(GeometryMultiPoint));
                metadata.AddPrimitiveProperty(entityType, "GeometryMultiLineString", typeof(GeometryMultiLineString));
                metadata.AddPrimitiveProperty(entityType, "GeometryMultiPolygon", typeof(GeometryMultiPolygon));
            }

            metadata.AddCollectionProperty(entityType, "CollectionOfPoints", typeof(GeographyPoint));
            metadata.AddCollectionProperty(entityType, "GeometryCollectionOfPoints", typeof(GeometryPoint));
            metadata.AddPrimitiveProperty(entityType, "GeographyNull", typeof(Geography));
            metadata.AddPrimitiveProperty(entityType, "GeometryNull", typeof(Geometry));

            metadata.AddResourceSet("Spatials", entityType);

            metadata.SetReadOnly();

            DSPContext context = new DSPContext();
            var        set     = context.GetResourceSetEntities("Spatials");

            for (int i = 0; i < 3; ++i)
            {
                DSPResource spatialEntity = new DSPResource(entityType);
                spatialEntity.SetValue("ID", i);
                spatialEntity.SetValue("Geography", GeographyFactory.Point(32.0 - i, -100.0).Build());
                spatialEntity.SetValue("Point", GeographyFactory.Point(33.1 - i, -110.0).Build());
                spatialEntity.SetValue("Point2", GeographyFactory.Point(32.1 - i, -110.0).Build());
                spatialEntity.SetValue("LineString", GeographyFactory.LineString(33.1 - i, -110.0).LineTo(35.97 - i, -110).Build());
                spatialEntity.SetValue("Polygon", GeographyFactory.Polygon().Ring(33.1 - i, -110.0).LineTo(35.97 - i, -110.15).LineTo(11.45 - i, 87.75).Ring(35.97 - i, -110).LineTo(36.97 - i, -110.15).LineTo(45.23 - i, 23.18).Build());
                spatialEntity.SetValue("GeographyCollection", GeographyFactory.Collection().Point(-19.99 - i, -12.0).Build());
                spatialEntity.SetValue("MultiPoint", GeographyFactory.MultiPoint().Point(10.2 - i, 11.2).Point(11.9 - i, 11.6).Build());
                spatialEntity.SetValue("MultiLineString", GeographyFactory.MultiLineString().LineString(10.2 - i, 11.2).LineTo(11.9 - i, 11.6).LineString(16.2 - i, 17.2).LineTo(18.9 - i, 19.6).Build());
                spatialEntity.SetValue("MultiPolygon", GeographyFactory.MultiPolygon().Polygon().Ring(10.2 - i, 11.2).LineTo(11.9 - i, 11.6).LineTo(11.45 - i, 87.75).Ring(16.2 - i, 17.2).LineTo(18.9 - i, 19.6).LineTo(11.45 - i, 87.75).Build());
                spatialEntity.SetValue("CollectionOfPoints", new List <GeographyPoint>()
                {
                    GeographyFactory.Point(10.2, 99.5),
                    GeographyFactory.Point(11.2, 100.5)
                });

                spatialEntity.SetValue("Geometry", GeometryFactory.Point(32.0 - i, -10.0).Build());
                spatialEntity.SetValue("GeometryPoint", GeometryFactory.Point(33.1 - i, -11.0).Build());
                spatialEntity.SetValue("GeometryPoint2", GeometryFactory.Point(32.1 - i, -11.0).Build());
                spatialEntity.SetValue("GeometryLineString", GeometryFactory.LineString(33.1 - i, -11.5).LineTo(35.97 - i, -11).Build());
                spatialEntity.SetValue("GeometryPolygon", GeometryFactory.Polygon().Ring(33.1 - i, -13.6).LineTo(35.97 - i, -11.15).LineTo(11.45 - i, 87.75).Ring(35.97 - i, -11).LineTo(36.97 - i, -11.15).LineTo(45.23 - i, 23.18).Build());
                spatialEntity.SetValue("GeometryCollection", GeometryFactory.Collection().Point(-19.99 - i, -12.0).Build());
                spatialEntity.SetValue("GeometryMultiPoint", GeometryFactory.MultiPoint().Point(10.2 - i, 11.2).Point(11.9 - i, 11.6).Build());
                spatialEntity.SetValue("GeometryMultiLineString", GeometryFactory.MultiLineString().LineString(10.2 - i, 11.2).LineTo(11.9 - i, 11.6).LineString(16.2 - i, 17.2).LineTo(18.9 - i, 19.6).Build());
                spatialEntity.SetValue("GeometryMultiPolygon", GeometryFactory.MultiPolygon().Polygon().Ring(10.2 - i, 11.2).LineTo(11.9 - i, 11.6).LineTo(11.45 - i, 87.75).Ring(16.2 - i, 17.2).LineTo(18.9 - i, 19.6).LineTo(11.45 - i, 87.75).Build());
                spatialEntity.SetValue("GeometryCollectionOfPoints", new List <GeometryPoint>()
                {
                    GeometryFactory.Point(10.2, 99.5),
                    GeometryFactory.Point(11.2, 100.5)
                });

                spatialEntity.SetValue("GeographyNull", null);
                spatialEntity.SetValue("GeometryNull", null);

                set.Add(spatialEntity);
            }

            var service = new DSPUnitTestServiceDefinition(metadata, DSPDataProviderKind.CustomProvider, context);

            service.DataServiceBehavior.AcceptSpatialLiteralsInQuery = true;
            service.Writable = true;

            return(service);
        }