Exemple #1
0
        public void TestParseEmptyData()
        {
            JObject       fieldsData = JObject.Parse("{}");
            List <IField> fields     = ScFieldsParser.ParseFieldsData(fieldsData, CancellationToken.None);

            Assert.True(fields.Count == 0);
        }
        public static ScItem ParseSource(JObject item, string db, CancellationToken cancelToken)
        {
            ScItem newItem;

            try {
                var source = ParseItemSource(item, db);

                List <IField> fields       = ScFieldsParser.ParseFieldsData(item, cancelToken);
                var           fieldsByName = new Dictionary <string, IField>(fields.Count);
                foreach (IField singleField in fields)
                {
                    cancelToken.ThrowIfCancellationRequested();

                    string lowercaseName = singleField.Name.ToLowerInvariant();
                    fieldsByName[lowercaseName] = singleField;
                }

                newItem = new ScItem(source, fieldsByName);
            } catch (Exception e) {
                OperationCanceledException cancel = e as OperationCanceledException;
                if (cancel != null)
                {
                    throw cancel;
                }

                throw new ParserException(TaskFlowErrorMessages.PARSER_EXCEPTION_MESSAGE + item.ToString(), e);
            }

            return(newItem);
        }
Exemple #3
0
        public void TestParseNullData()
        {
            JObject fieldsData = null;

            TestDelegate action = () => ScFieldsParser.ParseFieldsData(fieldsData, CancellationToken.None);

            Assert.Throws <ArgumentNullException>(action, "cannot parse null response");
        }
Exemple #4
0
        public static ScItemsResponse Parse(string responseString, CancellationToken cancelToken)
        {
            if (string.IsNullOrEmpty(responseString))
            {
                throw new ArgumentException("response cannot null or empty");
            }

            JObject response = JObject.Parse(responseString);

            int  statusCode       = ScItemsParser.ParseOrFail <int>(response, "$.statusCode");
            bool isSuccessfulCode = (200 <= statusCode) && (statusCode <= 299);

            if (!isSuccessfulCode)
            {
                var error = new WebApiJsonStatusMessage(statusCode, ParseOrFail <string>(response, "$.error.message"));
                throw new WebApiJsonErrorException(error);
            }

            int totalCount  = ParseOrFail <int>(response, "$.result.totalCount");
            int resultCount = ParseOrFail <int>(response, "$.result.resultCount");

            var responseItems = response.SelectToken("$.result.items");
            var items         = new List <ISitecoreItem>();

            foreach (JObject item in responseItems)
            {
                cancelToken.ThrowIfCancellationRequested();

                var source = ParseItemSource(item);

                var displayName = (string)item.GetValue("DisplayName");
                var hasChildren = (bool)item.GetValue("HasChildren");
                var id          = (string)item.GetValue("ID");
                var longId      = (string)item.GetValue("LongID");
                var path        = (string)item.GetValue("Path");
                var template    = (string)item.GetValue("Template");

                JObject       fieldsJSON   = (JObject)item.GetValue("Fields");
                List <IField> fields       = ScFieldsParser.ParseFieldsData(fieldsJSON, cancelToken);
                var           fieldsByName = new Dictionary <string, IField>(fields.Count);
                foreach (IField singleField in fields)
                {
                    cancelToken.ThrowIfCancellationRequested();

                    string lowercaseName = singleField.Name.ToLowerInvariant();
                    fieldsByName[lowercaseName] = singleField;
                }


                ScItem newItem = new ScItem(source, displayName, hasChildren, id, longId, path, template, fieldsByName);
                items.Add(newItem);
            }
            return(new ScItemsResponse(totalCount, resultCount, items));
        }
        public void TestParseBrokenData()
        {
            JObject       fieldsData = JObject.Parse("{}");
            List <IField> fields     = ScFieldsParser.ParseFieldsData(fieldsData, CancellationToken.None);

            ;
            Assert.True(fields.Count == 2);
            ScField field1 = (ScField)fields[0];

            Assert.AreEqual("{75577384-3C97-45DA-A847-81B00500E250}", field1.FieldId);
            ScField field2 = (ScField)fields[1];

            Assert.AreEqual("{A60ACD61-A6DB-4182-8329-C957982CEC74}", field2.FieldId);
        }
        public void TestParseCorrectData()
        {
            JObject fieldsData = JObject.Parse("{\n\"{75577384-3C97-45DA-A847-81B00500E250}\":{\n\"Name\":\"Title\",\n\"Type\":\"text\",\n\"Value\":\"RichText\"\n},\n\"{A60ACD61-A6DB-4182-8329-C957982CEC74}\":{\n\"Name\":\"Text\",\n\"Type\":\"Rich Text\",\n\"Value\":\"\\u003cp\\u003eWelcome to Sitecore\\u003c/p\\u003e\\r\\n\\r\\n\\u003cp\\u003e This is an image with full media path \\\"http://mobiledev1ua1.dk.sitecore.net:89/~/media/4F20B519D5654472B01891CB6103C667.ashx\\\" \\u003cbr\\u003e\\r\\n\\r\\n\\u003c/p\\u003e\\r\\n\\r\\n\\u003cp\\u003eAnd this one has a short media path \\\"~/media/4F20B519D5654472B01891CB6103C667.ashx\\\" \\u003cbr\\u003e\\r\\n\\r\\n\\u003c/p\\u003e\\r\\n\\r\\n\\u003cp\\u003eThey must look the same\\u003c/p\\u003e\"\n}\n}");

            ;
            List <IField> fields = ScFieldsParser.ParseFieldsData(fieldsData, CancellationToken.None);

            ;
            Assert.True(fields.Count == 2);
            ScField field1 = (ScField)fields[0];

            Assert.AreEqual("{75577384-3C97-45DA-A847-81B00500E250}", field1.FieldId);
            ScField field2 = (ScField)fields[1];

            Assert.AreEqual("{A60ACD61-A6DB-4182-8329-C957982CEC74}", field2.FieldId);
        }
Exemple #7
0
        public void TestCancellationCausesOperationCanceledException()
        {
            TestDelegate testAction = async() =>
            {
                var cancel = new CancellationTokenSource();

                Task <List <IField> > action = Task.Factory.StartNew(() =>
                {
                    int millisecondTimeout = 50;
                    Thread.Sleep(millisecondTimeout);
                    JObject fieldsData = JObject.Parse("{\n\"{75577384-3C97-45DA-A847-81B00500E250}\":{\n\"Name\":\"Title\",\n\"Type\":\"text\",\n\"Value\":\"RichText\"\n},\n\"{A60ACD61-A6DB-4182-8329-C957982CEC74}\":{\n\"Name\":\"Text\",\n\"Type\":\"Rich Text\",\n\"Value\":\"\\u003cp\\u003eWelcome to Sitecore\\u003c/p\\u003e\\r\\n\\r\\n\\u003cp\\u003e This is an image with full media path \\\"http://mobiledev1ua1.dk.sitecore.net:89/~/media/4F20B519D5654472B01891CB6103C667.ashx\\\" \\u003cbr\\u003e\\r\\n\\r\\n\\u003c/p\\u003e\\r\\n\\r\\n\\u003cp\\u003eAnd this one has a short media path \\\"~/media/4F20B519D5654472B01891CB6103C667.ashx\\\" \\u003cbr\\u003e\\r\\n\\r\\n\\u003c/p\\u003e\\r\\n\\r\\n\\u003cp\\u003eThey must look the same\\u003c/p\\u003e\"\n}\n}");
                    ;
                    return(ScFieldsParser.ParseFieldsData(fieldsData, cancel.Token));
                });
                cancel.Cancel();
                await action;
            };

            Assert.Catch <OperationCanceledException>(testAction);
        }