Beispiel #1
0
        public async Task CreateConnectionTest()
        {
            var obj1 = await ObjectHelper.CreateNewAsync();

            var obj2 = await ObjectHelper.CreateNewAsync();

            dynamic conn = new APConnection("sibling", "object", obj1.Id, "object", obj2.Id);

            conn.field1 = Unique.String;
            conn.field2 = 123;

            var request = new CreateConnectionRequest()
            {
                Connection = conn
            };
            var response = await request.ExecuteAsync();

            ApiHelper.EnsureValidResponse(response);
            Assert.IsNotNull(response.Connection, "Connection in create connection response is null.");
            Assert.IsFalse(string.IsNullOrWhiteSpace(response.Connection.Id), "Connection id in response.connection is invalid.");
            var endpoints = response.Connection.Endpoints.ToArray();

            Assert.IsNotNull(endpoints[0], "Endpoint A is null.");
            Assert.IsNotNull(endpoints[1], "Endpoint B is null.");
            Assert.IsTrue(endpoints.Select(x => x.ObjectId).Intersect(new[] { obj1.Id, obj2.Id }).Count() == 2);
        }
Beispiel #2
0
        public async Task ConnectionsTest()
        {
            var parent = ObjectHelper.NewInstance();

            parent["stringfield"] = "parent";
            var child = ObjectHelper.NewInstance();

            child["stringfield"] = "child";
            var conn = APConnection.New("link").FromNewObject("parent", parent).ToNewObject("child", child);
            await conn.SaveAsync();

            var parent2 = await conn.Endpoints["parent"].GetObjectAsync();
            var child2  = await conn.Endpoints["child"].GetObjectAsync();

            Assert.IsTrue(parent2 != null && child2 != null);
            Assert.IsTrue(parent2.Get <string>("stringfield") == "parent");
            Assert.IsTrue(child2.Get <string>("stringfield") == "child");

            // Swap and test
            parent = ObjectHelper.NewInstance();
            parent["stringfield"] = "parent";
            child = ObjectHelper.NewInstance();
            child["stringfield"] = "child";
            conn = APConnection.New("link").FromNewObject("child", child).ToNewObject("parent", parent);
            await conn.SaveAsync();

            parent2 = await conn.Endpoints["parent"].GetObjectAsync();
            child2  = await conn.Endpoints["child"].GetObjectAsync();
            Assert.IsTrue(parent2 != null && child2 != null);
            Assert.IsTrue(parent2.Get <string>("stringfield") == "parent");
            Assert.IsTrue(child2.Get <string>("stringfield") == "child");
        }
Beispiel #3
0
        public async Task UpdateConnectionAsyncTest()
        {
            dynamic conn = APConnection
                           .New("sibling")
                           .FromNewObject("object", ObjectHelper.NewInstance())
                           .ToNewObject("object", ObjectHelper.NewInstance());

            conn.field1 = "test";
            conn.field2 = 15L;
            await conn.SaveAsync();

            Console.WriteLine("Created connection with id: {0}", conn.Id);

            // Update the connection
            conn.field1 = "updated";
            conn.field2 = 11L;
            await conn.SaveAsync();

            // Get the connection
            APConnection read = await APConnections.GetAsync("sibling", conn.Id);

            // Asserts
            Assert.IsTrue(read.Get <string>("field1") == "updated");
            Assert.IsTrue(read.Get <int>("field2") == 11L);
        }
Beispiel #4
0
        public async Task GetConnectedObjectsOnConnectionWithSameTypeAndDifferentLabels()
        {
            var parent = await ObjectHelper.CreateNewAsync();

            var child1 = await ObjectHelper.CreateNewAsync();

            var child2 = await ObjectHelper.CreateNewAsync();

            // Create connections
            await APConnection.New("link")
            .FromExistingObject("parent", parent.Id)
            .ToExistingObject("child", child1.Id)
            .SaveAsync();

            await APConnection.New("link")
            .FromExistingObject("parent", parent.Id)
            .ToExistingObject("child", child2.Id)
            .SaveAsync();

            // Get connected objects
            var objects = await parent.GetConnectedObjectsAsync("link", label : "child");

            Assert.IsTrue(objects.Count == 2);
            Assert.IsTrue(objects.Select(a => a.Id).Intersect(new[] { child1.Id, child2.Id }).Count() == 2);
        }
        public async Task GetConnectedObjectsAsyncTest()
        {
            // Create objects
            var obj1 = await ObjectHelper.CreateNewAsync();

            var obj2 = await ObjectHelper.CreateNewAsync();

            var obj3 = await ObjectHelper.CreateNewAsync();

            var obj4 = await ObjectHelper.CreateNewAsync();

            var obj5 = await ObjectHelper.CreateNewAsync();

            // Create connections
            await APConnection.New("sibling").FromExistingObject("object", obj1.Id).ToExistingObject("object", obj2.Id).SaveAsync();

            await APConnection.New("sibling").FromExistingObject("object", obj1.Id).ToExistingObject("object", obj3.Id).SaveAsync();

            await APConnection.New("sibling").FromExistingObject("object", obj1.Id).ToExistingObject("object", obj4.Id).SaveAsync();

            await APConnection.New("sibling").FromExistingObject("object", obj1.Id).ToExistingObject("object", obj5.Id).SaveAsync();

            // Get connected
            var connectedObjects = await obj1.GetConnectedObjectsAsync("sibling");

            Assert.IsTrue(connectedObjects != null);
            Assert.IsTrue(connectedObjects.TotalRecords == 4);
            Assert.IsTrue(connectedObjects.Select(x => x.Id).Intersect(new[] { obj2.Id, obj3.Id, obj4.Id, obj5.Id }).Count() == 4);
        }
Beispiel #6
0
        public async Task GetConnectedObjectsForSameTypeAndDifferentLabelsWithoutLabelTest()
        {
            var parent = await ObjectHelper.CreateNewAsync();

            var child1 = await ObjectHelper.CreateNewAsync();

            var child2 = await ObjectHelper.CreateNewAsync();

            // Create connections
            await APConnection.New("link")
            .FromExistingObject("parent", parent.Id)
            .ToExistingObject("child", child1.Id)
            .SaveAsync();

            await APConnection.New("link")
            .FromExistingObject("parent", parent.Id)
            .ToExistingObject("child", child2.Id)
            .SaveAsync();

            // Get connected objects
            try
            {
                var objects = await parent.GetConnectedObjectsAsync("link");

                Assert.Fail("This call should have failed since we did not specify the label to retreive.");
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex.Message == "Label is required for edge 'link'.");
            }
        }
        public async Task GetConnectedObjectsWithSortingSupportTest()
        {
            // Create 5 connected objects and request page 2 with page size of 2.
            // With sorting, it should return specific objects.
            var root = await ObjectHelper.CreateNewAsync();

            List <APObject> children = new List <APObject>();

            children.Add(ObjectHelper.NewInstance());
            children.Add(ObjectHelper.NewInstance());
            children.Add(ObjectHelper.NewInstance());
            children.Add(ObjectHelper.NewInstance());
            children.Add(ObjectHelper.NewInstance());

            var tasks = children.ConvertAll(x =>
                                            APConnection.New("sibling").FromExistingObject("object", root.Id).ToNewObject("object", x).SaveAsync());

            #if NET40
            await TaskEx.WhenAll(tasks);
            #else
            await Task.WhenAll(tasks);
            #endif

            children = children.OrderBy(x => x.Id).ToList();
            var results = await root.GetConnectedObjectsAsync("sibling", orderBy : "__id", sortOrder : SortOrder.Ascending,
                                                              pageSize : 2, pageNumber : 2);

            Assert.IsTrue(results.Count == 2);
            Assert.IsTrue(results[0].Id == children[2].Id);
            Assert.IsTrue(results[1].Id == children[3].Id);
        }
Beispiel #8
0
        public async Task CreateConnectionWithNewUserAndNewDevice()
        {
            var device = DeviceHelper.NewDevice();
            var user   = UserHelper.NewUser();
            var conn   = APConnection.New("my_device")
                         .FromNewObject("device", device)
                         .ToNewObject("user", user);
            await conn.SaveAsync();

            Assert.IsTrue(string.IsNullOrWhiteSpace(conn.Id) == false);
            Console.WriteLine("Created connection with id: {0}", conn.Id);
        }
 private void ParseEndpoints(APConnection conn, JObject json, JsonSerializer serializer)
 {
     // Parse the endpoints
     JToken value;
     Endpoint ep1 = null, ep2 = null;
     if (json.TryGetValue("__endpointa", out value) == true && value.Type == JTokenType.Object)
         ep1 = ParseEndpoint(value as JObject, serializer);
     else throw new Exception(string.Format("Endpoint A for connection with id {0} is invalid.", conn.Id));
     if (json.TryGetValue("__endpointb", out value) == true && value.Type == JTokenType.Object)
         ep2 = ParseEndpoint(value as JObject, serializer);
     else throw new Exception(string.Format("Endpoint B for connection with id {0} is invalid.", conn.Id));
     conn.Endpoints = new EndpointPair(ep1, ep2);
 }
        public async Task ProjectWithArgsTest()
        {
            // Create sample data
            string val1 = Unique.String, val2 = Unique.String;
            var    root = await ObjectHelper.CreateNewAsync();

            var level1Child = ObjectHelper.NewInstance();

            level1Child.Set <string>("stringfield", val1);
            var level1Edge = APConnection.New("link").FromExistingObject("parent", root.Id).ToNewObject("child", level1Child);
            await level1Edge.SaveAsync();

            var level2Child = ObjectHelper.NewInstance();

            level2Child.Set <string>("stringfield", val2);
            var level2Edge = APConnection.New("link").FromExistingObject("parent", level1Child.Id).ToNewObject("child", level2Child);
            await level2Edge.SaveAsync();

            // Run filter
            var results = await Graph.Select("sample_project",
                                             new [] { root.Id },
                                             new Dictionary <string, string> {
                { "level1_filter", val1 }, { "level2_filter", val2 }
            });

            Assert.IsTrue(results.Count == 1);
            Assert.IsTrue(results[0].Object != null);
            Assert.IsTrue(results[0].Object.Id == root.Id);

            var level1Children = results[0].GetChildren("level1_children");

            Assert.IsTrue(level1Children.Count == 1);
            Assert.IsTrue(level1Children[0].Object != null);
            Assert.IsTrue(level1Children[0].Object.Id == level1Child.Id);
            Assert.IsTrue(level1Children[0].Connection != null);
            Assert.IsTrue(level1Children[0].Connection.Id == level1Edge.Id);
            Assert.IsTrue(level1Children[0].Connection.Endpoints["parent"].ObjectId == root.Id);
            Assert.IsTrue(level1Children[0].Connection.Endpoints["child"].ObjectId == level1Child.Id);

            var level2Children = level1Children[0].GetChildren("level2_children");

            Assert.IsTrue(level2Children.Count == 1);
            Assert.IsTrue(level2Children[0].Object != null);
            Assert.IsTrue(level2Children[0].Object.Id == level2Child.Id);
            Assert.IsTrue(level2Children[0].Connection != null);
            Assert.IsTrue(level2Children[0].Connection.Id == level2Edge.Id);
            Assert.IsTrue(level2Children[0].Connection.Endpoints["parent"].ObjectId == level1Child.Id);
            Assert.IsTrue(level2Children[0].Connection.Endpoints["child"].ObjectId == level2Child.Id);
        }
        public async static Task <APConnection> CreateNew(APConnection conn = null)
        {
            if (conn == null)
            {
                conn = APConnection
                       .New("sibling")
                       .FromNewObject("object", ObjectHelper.NewInstance())
                       .ToNewObject("object", ObjectHelper.NewInstance());
            }
            await conn.SaveAsync();

            Assert.IsTrue(string.IsNullOrWhiteSpace(conn.Id) == false);
            Console.WriteLine("Created connection with id: {0}", conn.Id);
            return(conn);
        }
        public async Task CreateConnectionWithNewObjectsAsyncTest()
        {

            var obj1 = ObjectHelper.NewInstance();
            var obj2 = ObjectHelper.NewInstance();
            dynamic conn = new APConnection("sibling", "object", obj1, "object", obj2);
            conn.field1 = Unique.String;
            conn.field2 = 123;

            var request = new CreateConnectionRequest() { Connection = conn };
            var response = await request.ExecuteAsync();
            ApiHelper.EnsureValidResponse(response);
            Assert.IsNotNull(response.Connection, "Connection in create connection response is null.");
            Assert.IsFalse(string.IsNullOrWhiteSpace(response.Connection.Id), "Connection id in response.connection is invalid.");
        }
 public async static Task<APConnection> CreateNew(APConnection conn = null)
 {
     if (conn == null)
     {
         conn = APConnection
             .New("sibling")
             .FromNewObject("object", ObjectHelper.NewInstance())
             .ToNewObject("object", ObjectHelper.NewInstance());
     }
     await conn.SaveAsync();
     Assert.IsTrue(string.IsNullOrWhiteSpace(conn.Id) == false);
     Console.WriteLine("Created connection with id: {0}", conn.Id);
     return conn;
     
 }
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            // Manage the open and closing of the object.
            // Write APConnection specific content (e.g., endpoints)
            // Call appropriate helpers to write common stuff.
            APConnection conn = value as APConnection;

            if (conn == null)
            {
                writer.WriteNull();
                return;
            }
            writer.StartObject();
            EntityParser.WriteJson(writer, conn, serializer);
            WriteEndpoints(writer, conn, serializer);
            writer.WriteEndObject();
        }
Beispiel #15
0
        public async Task CreateConnectionBetweenNewObjectsAsyncTest()
        {
            var obj1 = new APObject("object");
            var obj2 = new APObject("object");
            var conn = APConnection
                       .New("sibling")
                       .FromNewObject("object", obj1)
                       .ToNewObject("object", obj2);

            await conn.SaveAsync();

            Assert.IsTrue(string.IsNullOrWhiteSpace(conn.Id) == false);
            Console.WriteLine("Created connection with id: {0}", conn.Id);
            Assert.IsTrue(string.IsNullOrWhiteSpace(obj1.Id) == false);
            Console.WriteLine("Created new apObject with id: {0}", obj1.Id);
            Assert.IsTrue(string.IsNullOrWhiteSpace(obj2.Id) == false);
            Console.WriteLine("Created new apObject with id: {0}", obj2.Id);
        }
Beispiel #16
0
        public async Task CreateConnection2BetweenNewAndExistingObjectsAsyncTest()
        {
            var obj1 = await ObjectHelper.CreateNewAsync();

            var obj2 = new APObject("object");
            var conn = APConnection
                       .New("sibling")
                       .FromExistingObject("object", obj1.Id)
                       .ToNewObject("object", obj2);
            await conn.SaveAsync();

            Assert.IsTrue(string.IsNullOrWhiteSpace(conn.Id) == false);
            Console.WriteLine("Created connection with id: {0}", conn.Id);
            Assert.IsTrue(string.IsNullOrWhiteSpace(obj2.Id) == false);
            Console.WriteLine("Created new apObject with id: {0}", obj1.Id);
            // Ensure that the endpoint ids match
            Assert.IsTrue(conn.Endpoints.ToArray().Select(x => x.ObjectId).Intersect(new[] { obj1.Id, obj2.Id }).Count() == 2);
        }
        public async Task CreateConnectionTest()
        {
            var obj1 = await ObjectHelper.CreateNewAsync();
            var obj2 = await ObjectHelper.CreateNewAsync();
            dynamic conn = new APConnection("sibling", "object", obj1.Id, "object", obj2.Id);
            conn.field1 = Unique.String;
            conn.field2 = 123;

            var request = new CreateConnectionRequest() { Connection = conn };
            var response = await request.ExecuteAsync();
            ApiHelper.EnsureValidResponse(response);
            Assert.IsNotNull(response.Connection, "Connection in create connection response is null.");
            Assert.IsFalse(string.IsNullOrWhiteSpace(response.Connection.Id), "Connection id in response.connection is invalid.");
            var endpoints = response.Connection.Endpoints.ToArray();
            Assert.IsNotNull(endpoints[0], "Endpoint A is null.");
            Assert.IsNotNull(endpoints[1], "Endpoint B is null.");
            Assert.IsTrue(endpoints.Select(x => x.ObjectId).Intersect(new[] { obj1.Id, obj2.Id }).Count() == 2);
        }
Beispiel #18
0
        public async Task CreateDuplicateConnectionAsyncTest()
        {
            var o1   = new APObject("object");
            var o2   = new APObject("object");
            var conn = await APConnection
                       .New("sibling")
                       .FromNewObject("object", o1)
                       .ToNewObject("object", o2)
                       .SaveAsync();

            var dupe = await APConnection
                       .New("sibling")
                       .FromExistingObject("object", o1.Id)
                       .ToExistingObject("object", o2.Id)
                       .SaveAsync();

            Assert.AreEqual(conn.Id, dupe.Id);
        }
        private void WriteEndpoints(JsonWriter writer, APConnection conn, JsonSerializer serializer)
        {
            // Write endpoint A
            if (conn.Endpoints.EndpointA.CreateEndpoint == false)
            {
                writer
                .WriteProperty("__endpointa")
                .StartObject()
                .WriteProperty("label", conn.Endpoints.EndpointA.Label)
                .WriteProperty("objectid", conn.Endpoints.EndpointA.ObjectId)
                .EndObject();
            }
            else
            {
                writer
                .WriteProperty("__endpointa")
                .StartObject()
                .WriteProperty("label", conn.Endpoints.EndpointA.Label)
                .WriteProperty("object")
                .WithWriter(w => WriteObject(w, conn.Endpoints.EndpointA.Content))
                .EndObject();
            }

            // Write endpoint B
            if (conn.Endpoints.EndpointB.CreateEndpoint == false)
            {
                writer
                .WriteProperty("__endpointb")
                .StartObject()
                .WriteProperty("label", conn.Endpoints.EndpointB.Label)
                .WriteProperty("objectid", conn.Endpoints.EndpointB.ObjectId)
                .EndObject();
            }
            else
            {
                writer
                .WriteProperty("__endpointb")
                .StartObject()
                .WriteProperty("label", conn.Endpoints.EndpointB.Label)
                .WriteProperty("object")
                .WithWriter(w => WriteObject(w, conn.Endpoints.EndpointB.Content))
                .EndObject();
            }
        }
 public void HasBatchReferencesShouldBeTrueForConnectionsWithBatchReferences()
 {
     APBatch batch = new APBatch();
     var obj = ObjectHelper.NewInstance();
     var objReference = batch.SaveObject(obj);
     var connA = new APConnection("type", "labelA", "IdA", "labelB", "idB");
     var connB = new APConnection("type", "labelA", obj, "labelB", obj);
     var connC = new APConnection("type", "labelA", obj, "labelB", "IdB");
     var connD = new APConnection("type", "labelA", "IdA", "labelB", objReference);
     var connE = new APConnection("type", "labelA", objReference, "labelB", objReference);
     var connF = new APConnection("type", "labelA", obj, "labelB", objReference);
     
     Assert.IsFalse(connA.Endpoints.HasBatchReference);
     Assert.IsFalse(connB.Endpoints.HasBatchReference);
     Assert.IsFalse(connC.Endpoints.HasBatchReference);
     Assert.IsTrue(connD.Endpoints.HasBatchReference);
     Assert.IsTrue(connE.Endpoints.HasBatchReference);
     Assert.IsTrue(connF.Endpoints.HasBatchReference);
 }
        public async Task FilterWithArgsTest()
        {
            var parent = await ObjectHelper.CreateNewAsync();

            var unique = Unique.String;
            var child  = ObjectHelper.NewInstance();

            child.Set <string>("stringfield", unique);
            var conn = APConnection.New("link").FromExistingObject("parent", parent.Id).ToNewObject("child", child);
            await conn.SaveAsync();

            // Run filter
            var results = await Graph.Query("sample_filter", new Dictionary <string, string> {
                { "search_value", unique }
            });

            Assert.IsTrue(results.Count == 1);
            Assert.IsTrue(results[0] == parent.Id);
        }
Beispiel #22
0
        public async Task CreateConnectionWithNewObjectsAsyncTest()
        {
            var     obj1 = ObjectHelper.NewInstance();
            var     obj2 = ObjectHelper.NewInstance();
            dynamic conn = new APConnection("sibling", "object", obj1, "object", obj2);

            conn.field1 = Unique.String;
            conn.field2 = 123;

            var request = new CreateConnectionRequest()
            {
                Connection = conn
            };
            var response = await request.ExecuteAsync();

            ApiHelper.EnsureValidResponse(response);
            Assert.IsNotNull(response.Connection, "Connection in create connection response is null.");
            Assert.IsFalse(string.IsNullOrWhiteSpace(response.Connection.Id), "Connection id in response.connection is invalid.");
        }
Beispiel #23
0
        public async Task UpdatePartialConnectionAsyncTest()
        {
            // Create new connection
            var obj1 = await ObjectHelper.CreateNewAsync();

            var obj2 = new APObject("object");
            var conn = APConnection
                       .New("sibling")
                       .FromExistingObject("object", obj1.Id)
                       .ToNewObject("object", obj2);
            await conn.SaveAsync();

            // Update
            var conn2 = new APConnection("sibling", conn.Id);
            var value = Guid.NewGuid().ToString();

            conn["field1"] = value;
            await conn.SaveAsync();

            var updated = await APConnections.GetAsync("sibling", conn.Id);

            Assert.IsTrue(updated.Get <string>("field1") == value);
        }
Beispiel #24
0
        public async Task GetEndpointContentTest()
        {
            var existing = await ObjectHelper.CreateNewAsync();

            var newObj = ObjectHelper.NewInstance();
            var conn   = APConnection
                         .New("link")
                         .FromExistingObject("parent", existing.Id)
                         .ToNewObject("child", newObj);
            await conn.SaveAsync();

            Assert.IsTrue(conn.GetEndpointId("parent") == existing.Id);
            Assert.IsTrue(string.IsNullOrWhiteSpace(conn.GetEndpointId("child")) == false);

            // Get endpoints
            var child = await conn.GetEndpointObjectAsync("child");

            var parent = await conn.GetEndpointObjectAsync("parent");

            Assert.IsNotNull(child);
            Assert.IsNotNull(parent);
            Assert.IsTrue(parent.Id == existing.Id);
        }
Beispiel #25
0
        public async Task CreateDuplicateConnectionWithFaultAsyncTest()
        {
            var o1   = new APObject("object");
            var o2   = new APObject("object");
            var conn = await APConnection
                       .New("sibling")
                       .FromNewObject("object", o1)
                       .ToNewObject("object", o2)
                       .SaveAsync();

            try
            {
                var dupe = await APConnection
                           .New("sibling")
                           .FromExistingObject("object", o1.Id)
                           .ToExistingObject("object", o2.Id)
                           .SaveAsync(throwIfAlreadyExists: true);

                Assert.Fail("Duplicate connection creation did not fault.");
            }
            catch (DuplicateObjectException)
            {
            }
        }
        private void ParseEndpoints(APConnection conn, JObject json, JsonSerializer serializer)
        {
            // Parse the endpoints
            JToken   value;
            Endpoint ep1 = null, ep2 = null;

            if (json.TryGetValue("__endpointa", out value) == true && value.Type == JTokenType.Object)
            {
                ep1 = ParseEndpoint(value as JObject, serializer);
            }
            else
            {
                throw new Exception(string.Format("Endpoint A for connection with id {0} is invalid.", conn.Id));
            }
            if (json.TryGetValue("__endpointb", out value) == true && value.Type == JTokenType.Object)
            {
                ep2 = ParseEndpoint(value as JObject, serializer);
            }
            else
            {
                throw new Exception(string.Format("Endpoint B for connection with id {0} is invalid.", conn.Id));
            }
            conn.Endpoints = new EndpointPair(ep1, ep2);
        }
        private APConnection ParseConnection(string parentLabel, APObject parentObj, APObject currentObj, JObject json)
        {
            string label = string.Empty;
            if( json.Property("__label") != null ) 
                label = GetValue(json, "__label", JTokenType.String, true).ToString();
            else 
                label = GetValue(json, "label", JTokenType.String, true).ToString();
            var relation = GetValue(json, "__relationtype", JTokenType.String, true).ToString();
            var id = GetValue(json, "__id", JTokenType.String, true).ToString();
            var conn = new APConnection(relation, id);
            conn.Endpoints = new EndpointPair(
                new Endpoint(parentLabel, parentObj),
                new Endpoint(label, currentObj));
            // Parse system properties
            JToken value = null;
            // Id
            if (json.TryGetValue("__id", out value) == true && value.Type != JTokenType.Null)
            {
                conn.Id = value.ToString();
                json.Remove("__id");
            }
            // Revision
            if (json.TryGetValue("__revision", out value) == true && value.Type != JTokenType.Null)
            {
                conn.Revision = int.Parse(value.ToString());
                json.Remove("__revision");
            }
            // Created by
            if (json.TryGetValue("__createdby", out value) == true && value.Type != JTokenType.Null)
            {
                conn.CreatedBy = value.ToString();
                json.Remove("__createdby");
            }
            // Create date
            if (json.TryGetValue("__utcdatecreated", out value) == true && value.Type != JTokenType.Null)
            {
                conn.CreatedAt = (DateTime)value;
                json.Remove("__utcdatecreated");
            }
            // Last updated by
            if (json.TryGetValue("__lastmodifiedby", out value) == true && value.Type != JTokenType.Null)
            {
                conn.LastUpdatedBy = value.ToString();
                json.Remove("__lastmodifiedby");
            }
            // Last update date
            if (json.TryGetValue("__utclastupdateddate", out value) == true && value.Type != JTokenType.Null)
            {
                conn.LastUpdatedAt = (DateTime)value;
                json.Remove("__utclastupdateddate");
            }

            // Parse connection tags
            if (json.TryGetValue("__tags", out value) == true && value.Type == JTokenType.Array)
            {
                ((JArray)value).Values<string>().For(t => conn.AddTag(t, true));
            }
            json.Remove("__tags");

            // Parse connection attributes
            if (json.TryGetValue("__attributes", out value) == true && value.Type == JTokenType.Object)
            {
                foreach (var property in ((JObject)value).Properties())
                {
                    if( property.Type != JTokenType.Null )
                        conn.SetAttribute(property.Name, property.Value.ToString(), true);
                }
            }
            json.Remove("__attributes");

            // properties
            foreach (var property in json.Properties())
            {
                // Ignore objects
                if (property.Value.Type == JTokenType.Object) continue;
                // Check for arrays
                else if (property.Value.Type == JTokenType.Array)
                {
                    conn.SetList<string>(property.Name, property.Value.Values<string>(), true);
                }
                // Set value of the property
                else if (property.Value.Type == JTokenType.Date)
                    conn.SetField(property.Name, ((DateTime)property.Value).ToString("o"), true);
                else
                    conn.SetField(property.Name, property.Value.Type == JTokenType.Null ? null : property.Value.ToString(), true);
            }

            return conn;
        }
 private void WriteEndpoints(JsonWriter writer, APConnection conn, JsonSerializer serializer)
 {
     WriteEndpoint("__endpointa", writer, conn.Endpoints.EndpointA);
     WriteEndpoint("__endpointb", writer, conn.Endpoints.EndpointB);
 }
        private void WriteEndpoints(JsonWriter writer, APConnection conn, JsonSerializer serializer)
        {
            // Write endpoint A
            if (conn.Endpoints.EndpointA.CreateEndpoint == false)
            {
                writer
                    .WriteProperty("__endpointa")
                    .StartObject()
                    .WriteProperty("label", conn.Endpoints.EndpointA.Label)
                    .WriteProperty("objectid", conn.Endpoints.EndpointA.ObjectId)
                    .EndObject();
            }
            else
            {
                writer
                    .WriteProperty("__endpointa")
                    .StartObject()
                    .WriteProperty("label", conn.Endpoints.EndpointA.Label)
                    .WriteProperty("object")
                    .WithWriter(w => WriteObject(w, conn.Endpoints.EndpointA.Content))
                    .EndObject();
            }

            // Write endpoint B
            if (conn.Endpoints.EndpointB.CreateEndpoint == false)
            {
                writer
                    .WriteProperty("__endpointb")
                    .StartObject()
                    .WriteProperty("label", conn.Endpoints.EndpointB.Label)
                    .WriteProperty("objectid", conn.Endpoints.EndpointB.ObjectId)
                    .EndObject();
            }
            else
            {
                writer
                    .WriteProperty("__endpointb")
                    .StartObject()
                    .WriteProperty("label", conn.Endpoints.EndpointB.Label)
                    .WriteProperty("object")
                    .WithWriter(w => WriteObject(w, conn.Endpoints.EndpointB.Content))
                    .EndObject();
            }
        }
Beispiel #30
0
        private APConnection ParseConnection(string parentLabel, APObject parentObj, APObject currentObj, JObject json)
        {
            string label = string.Empty;

            if (json.Property("__label") != null)
            {
                label = GetValue(json, "__label", JTokenType.String, true).ToString();
            }
            else
            {
                label = GetValue(json, "label", JTokenType.String, true).ToString();
            }
            var relation = GetValue(json, "__relationtype", JTokenType.String, true).ToString();
            var id       = GetValue(json, "__id", JTokenType.String, true).ToString();
            var conn     = new APConnection(relation, id);

            conn.Endpoints = new EndpointPair(
                new Endpoint(parentLabel, parentObj)
            {
                ObjectId = parentObj.Id
            },
                new Endpoint(label, currentObj)
            {
                ObjectId = currentObj.Id
            });
            // Parse system properties
            JToken value = null;

            // Id
            if (json.TryGetValue("__id", out value) == true && value.Type != JTokenType.Null)
            {
                conn.Id = value.ToString();
                json.Remove("__id");
            }
            // Revision
            if (json.TryGetValue("__revision", out value) == true && value.Type != JTokenType.Null)
            {
                conn.Revision = int.Parse(value.ToString());
                json.Remove("__revision");
            }
            // Created by
            if (json.TryGetValue("__createdby", out value) == true && value.Type != JTokenType.Null)
            {
                conn.CreatedBy = value.ToString();
                json.Remove("__createdby");
            }
            // Create date
            if (json.TryGetValue("__utcdatecreated", out value) == true && value.Type != JTokenType.Null)
            {
                conn.CreatedAt = (DateTime)value;
                json.Remove("__utcdatecreated");
            }
            // Last updated by
            if (json.TryGetValue("__lastmodifiedby", out value) == true && value.Type != JTokenType.Null)
            {
                conn.LastUpdatedBy = value.ToString();
                json.Remove("__lastmodifiedby");
            }
            // Last update date
            if (json.TryGetValue("__utclastupdateddate", out value) == true && value.Type != JTokenType.Null)
            {
                conn.LastUpdatedAt = (DateTime)value;
                json.Remove("__utclastupdateddate");
            }

            // Parse connection tags
            if (json.TryGetValue("__tags", out value) == true && value.Type == JTokenType.Array)
            {
                ((JArray)value).Values <string>().For(t => conn.AddTag(t, true));
            }
            json.Remove("__tags");

            // Parse connection attributes
            if (json.TryGetValue("__attributes", out value) == true && value.Type == JTokenType.Object)
            {
                foreach (var property in ((JObject)value).Properties())
                {
                    if (property.Type != JTokenType.Null)
                    {
                        conn.SetAttribute(property.Name, property.Value.ToString(), true);
                    }
                }
            }
            json.Remove("__attributes");

            // properties
            foreach (var property in json.Properties())
            {
                // Ignore objects
                if (property.Value.Type == JTokenType.Object)
                {
                    continue;
                }
                // Check for arrays
                else if (property.Value.Type == JTokenType.Array)
                {
                    conn.SetList <string>(property.Name, property.Value.Values <string>(), true);
                }
                // Set value of the property
                else if (property.Value.Type == JTokenType.Date)
                {
                    conn.SetField(property.Name, ((DateTime)property.Value).ToString("o"), true);
                }
                else
                {
                    conn.SetField(property.Name, property.Value.Type == JTokenType.Null ? null : property.Value.ToString(), true);
                }
            }

            return(conn);
        }
        public async Task UpdatePartialConnectionAsyncTest()
        {
            // Create new connection
            var obj1 = await ObjectHelper.CreateNewAsync();
            var obj2 = new APObject("object");
            var conn = APConnection
                            .New("sibling")
                            .FromExistingObject("object", obj1.Id)
                            .ToNewObject("object", obj2);
            await conn.SaveAsync();

            // Update
            var conn2 = new APConnection("sibling", conn.Id);
            var value = Guid.NewGuid().ToString();
            conn["field1"] = value;
            await conn.SaveAsync();

            var updated = await APConnections.GetAsync("sibling", conn.Id);
            Assert.IsTrue(updated.Get<string>("field1") == value);
        }