Ejemplo n.º 1
0
        public async Task GetMetadata_ValidResponse_ValidMetadata()
        {
            /*** Arrange ***/
            var responseString = "{\"$id\":\"c79896a0-a33f-11e3-a5e2-0800200c9a66\",\"$type\": \"properties\",\"$parent\": \"file_552345101\",\"client_number\": \"820183\",\"client_name\": \"Biomedical Corp\",\"case_reference\": \"A83JAA\",\"case_type\": \"Employment Litigation\",\"assigned_attorney\": \"Francis Burke\",\"case_status\": \"in-progress\"}";

            _handler.Setup(h => h.ExecuteAsync <BoxMetadata>(It.IsAny <IBoxRequest>()))
            .Returns(Task.FromResult <IBoxResponse <BoxMetadata> >(new BoxResponse <BoxMetadata>()
            {
                Status        = ResponseStatus.Success,
                ContentString = responseString
            }));

            /*** Act ***/
            BoxMetadata md = await _metadataManager.GetMetadata("fakeId");

            /*** Assert ***/
            Assert.AreEqual("c79896a0-a33f-11e3-a5e2-0800200c9a66", md.Id);
            Assert.AreEqual("properties", md.Type);
            Assert.AreEqual("file_552345101", md.Parent);
            Assert.AreEqual("820183", md["client_number"]);
            Assert.AreEqual("Biomedical Corp", md["client_name"]);
            Assert.AreEqual("A83JAA", md["case_reference"]);
            Assert.AreEqual("Employment Litigation", md["case_type"]);
            Assert.AreEqual("Francis Burke", md["assigned_attorney"]);
            Assert.AreEqual("in-progress", md["case_status"]);
        }
Ejemplo n.º 2
0
        public async Task UpdateMetadata_ValidResponse_ValidMetadata()
        {
            /*** Arrange ***/
            var responseString = "{\"$id\":\"c79896a0-a33f-11e3-a5e2-0800200c9a66\",\"$type\": \"properties\",\"$parent\": \"file_552345101\",\"client_number\": \"820183\",\"client_name\": \"Biomedical Corp\",\"case_reference\": \"A83JAA\",\"case_type\": \"Employment Litigation\",\"assigned_attorney\": \"Eugene Huang\",\"retention_length\": \"7_years\"}";

            _handler.Setup(h => h.ExecuteAsync <BoxMetadata>(It.IsAny <IBoxRequest>()))
            .Returns(Task.FromResult <IBoxResponse <BoxMetadata> >(new BoxResponse <BoxMetadata>()
            {
                Status        = ResponseStatus.Success,
                ContentString = responseString
            }));

            var mdReq = new BoxMetadataRequest[]
            {
                new BoxMetadataRequest()
                {
                    Op = BoxMetadataOperations.Test, Path = "/assigned_attorney", Value = "Francis Burke"
                },
                new BoxMetadataRequest()
                {
                    Op = BoxMetadataOperations.Replace, Path = "/assigned_attorney", Value = "Eugene Huang"
                },
                new BoxMetadataRequest()
                {
                    Op = BoxMetadataOperations.Test, Path = "/case_status", Value = "in-progress"
                },
                new BoxMetadataRequest()
                {
                    Op = BoxMetadataOperations.Remove, Path = "/case_status", Value = "Francis Burke"
                },
                new BoxMetadataRequest()
                {
                    Op = BoxMetadataOperations.Add, Path = "/retention_length", Value = "7_years"
                }
            };

            /*** Act ***/
            BoxMetadata md = await _metadataManager.UpdateMetadata("fakeId", mdReq);

            /*** Assert ***/

            Assert.AreEqual("c79896a0-a33f-11e3-a5e2-0800200c9a66", md.Id);
            Assert.AreEqual("properties", md.Type);
            Assert.AreEqual("file_552345101", md.Parent);
            Assert.AreEqual("820183", md["client_number"]);
            Assert.AreEqual("Biomedical Corp", md["client_name"]);
            Assert.AreEqual("A83JAA", md["case_reference"]);
            Assert.AreEqual("Employment Litigation", md["case_type"]);
            Assert.AreEqual("Eugene Huang", md["assigned_attorney"]);
            Assert.AreEqual("7_years", md["retention_length"]);
        }
        public async Task GetMetadata_LiveSession_ValidResponse()
        {
            /*** Act ***/
            BoxMetadata md = await _client.ResourcePlugins.Get <BoxMetadataManager>().GetMetadata(TestFileId);

            /*** Assert ***/
            Assert.IsNotNull(md.Id);
            Assert.AreEqual("properties", md.Type);
            Assert.AreEqual(string.Format("file_{0}", TestFileId), md.Parent);
            Assert.AreEqual("820183", md["client_number"]);
            Assert.AreEqual("Biomedical Corp", md["client_name"]);
            Assert.AreEqual("A83JAA", md["case_reference"]);
            Assert.AreEqual("Employment Litigation", md["case_type"]);
            Assert.AreEqual("Francis Burke", md["assigned_attorney"]);
            Assert.AreEqual("in-progress", md["case_status"]);
        }
Ejemplo n.º 4
0
        public async Task GetMetadata_EmptyResponse_EmptyMetadata()
        {
            /*** Arrange ***/
            var responseString = "{}";

            _handler.Setup(h => h.ExecuteAsync <BoxMetadata>(It.IsAny <IBoxRequest>()))
            .Returns(Task.FromResult <IBoxResponse <BoxMetadata> >(new BoxResponse <BoxMetadata>()
            {
                Status        = ResponseStatus.Success,
                ContentString = responseString
            }));

            /*** Act ***/
            BoxMetadata md = await _metadataManager.GetMetadata("fakeId");

            /*** Assert ***/
            //Assert.AreEqual(0, md.Count);
        }
        public async Task UpdateMetadata_LiveSession_ValidResponse()
        {
            /*** Arrange ***/
            var mdReq = new BoxMetadataRequest[]
            {
                new BoxMetadataRequest()
                {
                    Op = BoxMetadataOperations.Test, Path = "assigned_attorney", Value = "Francis Burke"
                },
                new BoxMetadataRequest()
                {
                    Op = BoxMetadataOperations.Replace, Path = "assigned_attorney", Value = "Eugene Huang"
                },
                new BoxMetadataRequest()
                {
                    Op = BoxMetadataOperations.Test, Path = "case_status", Value = "in-progress"
                },
                new BoxMetadataRequest()
                {
                    Op = BoxMetadataOperations.Remove, Path = "case_status", Value = "Francis Burke"
                },
                new BoxMetadataRequest()
                {
                    Op = BoxMetadataOperations.Add, Path = "retention_length", Value = "7_years"
                }
            };

            /*** Act ***/
            BoxMetadata md = await _client.ResourcePlugins.Get <BoxMetadataManager>().UpdateMetadata(TestFileId, mdReq);

            /*** Assert ***/

            Assert.IsNotNull(md.Id);
            Assert.AreEqual("properties", md.Type);
            Assert.AreEqual(string.Format("file_{0}", TestFileId), md.Parent);
            Assert.AreEqual("820183", md["client_number"]);
            Assert.AreEqual("Biomedical Corp", md["client_name"]);
            Assert.AreEqual("A83JAA", md["case_reference"]);
            Assert.AreEqual("Employment Litigation", md["case_type"]);
            Assert.AreEqual("Eugene Huang", md["assigned_attorney"]);
            Assert.AreEqual("7_years", md["retention_length"]);
        }
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            // Null check so that the JObject.Load doesn't throw an exception
            if (reader.TokenType == JsonToken.Null)
            {
                return(null);
            }

            // Load JObject from stream
            JObject jObject = JObject.Load(reader);

            // Create BoxMetadata target object
            var target = new BoxMetadata();

            // *** Custom Parsing for Box Metadata ***
            // Because the API returns system properties in $key format, we must use completely customized parsing.
            // Reflection is used to set the properties since the objects are immutable. This was chosen over an overloaded constructor
            // since additional system properties can be added at any time
            JToken token;

            if (jObject.TryGetValue(BoxMetadata.FieldId, out token))
            {
                token.Parent.Remove();
                typeof(BoxMetadata).GetProperty("Id").SetValue(target, token.ToObject <string>(), null);
            }
            if (jObject.TryGetValue(BoxMetadata.FieldType, out token))
            {
                token.Parent.Remove();
                typeof(BoxMetadata).GetProperty("Type").SetValue(target, token.ToObject <string>(), null);
            }
            if (jObject.TryGetValue(BoxMetadata.FieldParent, out token))
            {
                token.Parent.Remove();
                typeof(BoxMetadata).GetProperty("Parent").SetValue(target, token.ToObject <string>(), null);
            }

            // Populate the remaining properties as key value pairs in the dictionary
            serializer.Populate(jObject.CreateReader(), target);

            return(target);
        }
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            // Null check so that the JObject.Load doesn't throw an exception
            if (reader.TokenType == JsonToken.Null)
                return null;

            // Load JObject from stream
            JObject jObject = JObject.Load(reader);

            // Create BoxMetadata target object
            var target = new BoxMetadata();

            // *** Custom Parsing for Box Metadata ***
            // Because the API returns system properties in $key format, we must use completely customized parsing.
            // Reflection is used to set the properties since the objects are immutable. This was chosen over an overloaded constructor
            // since additional system properties can be added at any time
            JToken token;
            if (jObject.TryGetValue(BoxMetadata.FieldId, out token))
            { 
                token.Parent.Remove();
                typeof(BoxMetadata).GetProperty("Id").SetValue(target, token.ToObject<string>(), null);
            }
            if (jObject.TryGetValue(BoxMetadata.FieldType, out token))
            {
                token.Parent.Remove();
                typeof(BoxMetadata).GetProperty("Type").SetValue(target, token.ToObject<string>(), null);
            }
            if (jObject.TryGetValue(BoxMetadata.FieldParent, out token))
            {
                token.Parent.Remove();
                typeof(BoxMetadata).GetProperty("Parent").SetValue(target, token.ToObject<string>(), null);
            }

            // Populate the remaining properties as key value pairs in the dictionary
            serializer.Populate(jObject.CreateReader(), target);

            return target;
        }