示例#1
0
文件: FormatTest.cs 项目: wpq0/FormNG
 public void CanParseAndFormatList(object input, DataType itemType, string itemFormat, string expected)
 {
     var schema = new ListSchema { DataType = DataType.List, ItemType = itemType, ItemFormat = itemFormat};
     var field = new Field(schema, input);
     var actual = field.Format();
     StringAssert.AreEqualIgnoringCase(actual, expected);
 }
示例#2
0
文件: FormatTest.cs 项目: wpq0/FormNG
 public void CanParseAndFormatList(DataType type, object input, string format, string expected)
 {
     var schema = new ListSchema {DataFormat = format};
     var list = new Field(schema, input);
     var actual = list.Format();
     Assert.AreEqual(expected, actual);
 }
        public async Task <SiteList> GetSiteListAsync(string identifier, ListSchema listSchema)
        {
            try
            {
                if (String.IsNullOrEmpty(identifier))
                {
                    throw new ArgumentNullException(nameof(identifier));
                }

                var spSiteList = new SiteList
                {
                    SiteId = _timeTrackerOptions.SharePointSiteId
                };

                switch (listSchema)
                {
                case ListSchema.NotificationsListSchema:
                    spSiteList.ListId = _timeTrackerOptions.NotificationsListPrefix + identifier;
                    break;

                case ListSchema.ReportHoursListSchema:
                    spSiteList.ListId = _timeTrackerOptions.ReportHoursListPrefix + identifier;
                    break;

                case ListSchema.TeamHoursListSchema:
                    spSiteList.ListId = _timeTrackerOptions.TeamHoursListPrefix + identifier;
                    break;

                case ListSchema.WorkHoursListSchema:
                    spSiteList.ListId = _timeTrackerOptions.WorkHoursListPrefix + identifier;
                    break;

                case ListSchema.UsersListSchema:
                    spSiteList.ListId = _timeTrackerOptions.SharePointUsersList;
                    break;

                case ListSchema.TotalHrsListSchema:
                    spSiteList.ListId = _timeTrackerOptions.SharePointTotalHoursList;
                    break;
                }

                // Call to Graph API to check if SharePoint List exists.
                var listExists = await TryGetSiteListAsync(spSiteList);

                // Create List if it does not exists.
                if (!listExists)
                {
                    switch (listSchema)
                    {
                    case ListSchema.NotificationsListSchema:
                        await CreateSiteListAsync(spSiteList, ListSchema.NotificationsListSchema);

                        break;

                    case ListSchema.ReportHoursListSchema:
                        await CreateSiteListAsync(spSiteList, ListSchema.ReportHoursListSchema);

                        break;

                    case ListSchema.TeamHoursListSchema:
                        await CreateSiteListAsync(spSiteList, ListSchema.TeamHoursListSchema);

                        break;

                    case ListSchema.WorkHoursListSchema:
                        await CreateSiteListAsync(spSiteList, ListSchema.WorkHoursListSchema);

                        break;

                    case ListSchema.UsersListSchema:
                        await CreateSiteListAsync(spSiteList, ListSchema.UsersListSchema);

                        break;

                    case ListSchema.TotalHrsListSchema:
                        await CreateSiteListAsync(spSiteList, ListSchema.TotalHrsListSchema);

                        break;
                    }
                }

                return(spSiteList);
            }
            catch (ServiceException ex)
            {
                switch (ex.Error.Code)
                {
                case "Request_ResourceNotFound":
                case "ResourceNotFound":
                case "ErrorItemNotFound":
                case "itemNotFound":
                    throw;

                case "TokenNotFound":
                    //await HttpContext.ChallengeAsync();
                    throw;

                default:
                    throw;
                }
            }
        }
        public async Task CreateSiteListAsync(SiteList siteList, ListSchema listSchema)
        {
            try
            {
                if (String.IsNullOrEmpty(siteList.ListId))
                {
                    throw new ArgumentNullException(nameof(siteList.ListId));
                }
                if (String.IsNullOrEmpty(siteList.SiteId))
                {
                    throw new ArgumentNullException(nameof(siteList.SiteId));
                }

                var htmlBody = "";

                switch (listSchema)
                {
                case ListSchema.NotificationsListSchema:
                    htmlBody = SharePointListsSchemaHelper.GetNotificationsJsonSchema(siteList.ListId);
                    break;

                case ListSchema.ReportHoursListSchema:
                    htmlBody = SharePointListsSchemaHelper.GetReportHoursJsonSchema(siteList.ListId);
                    break;

                case ListSchema.TeamHoursListSchema:
                    htmlBody = SharePointListsSchemaHelper.GetTeamHoursJsonSchema(siteList.ListId);
                    break;

                case ListSchema.WorkHoursListSchema:
                    htmlBody = SharePointListsSchemaHelper.GetWorkHoursJsonSchema(siteList.ListId);
                    break;

                case ListSchema.UsersListSchema:
                    htmlBody = SharePointListsSchemaHelper.GetUsersMembershipJsonSchema(siteList.ListId);
                    break;

                case ListSchema.TotalHrsListSchema:
                    htmlBody = SharePointListsSchemaHelper.GetTotalHoursJsonSchema(siteList.ListId);
                    break;
                }

                var requestUrl = _timeTrackerOptions.GraphRequestUrl + "/sites/" + siteList.SiteId + "/lists";

                // Create the request message and add the content.
                HttpRequestMessage hrm = new HttpRequestMessage(HttpMethod.Post, requestUrl);
                hrm.Content = new StringContent(htmlBody, System.Text.Encoding.UTF8, "application/json");

                var response = new HttpResponseMessage();

                // Authenticate (add access token) our HttpRequestMessage
                await GraphAppClient.AuthenticationProvider.AuthenticateRequestAsync(hrm);

                // Send the request and get the response.
                response = await GraphAppClient.HttpProvider.SendAsync(hrm);

                // Get the content from the response.
                if (response.StatusCode != System.Net.HttpStatusCode.Created)
                {
                    // TODO: Depending on code, rise proper exception for now invalid request is
                    throw new ServiceException(new Error {
                        Code = ErrorConstants.Codes.InvalidRequest, Message = response.StatusCode.ToString()
                    });
                }
            }
            catch (ServiceException ex)
            {
                switch (ex.Error.Code)
                {
                case "Request_ResourceNotFound":
                case "ResourceNotFound":
                case "ErrorItemNotFound":
                case "itemNotFound":
                    throw;

                case "TokenNotFound":
                    //await HttpContext.ChallengeAsync();
                    throw;

                default:
                    throw;
                }
            }
        }
示例#5
0
文件: SchemaTest.cs 项目: wpq0/FormNG
 public void ListSchemaCanHaveItemsOfCertainTypes(DataType input, DataType expected)
 {
     var list = new ListSchema(null, input);
     Assert.AreEqual(expected, list.GetItemSchema().DataType);
     Assert.AreEqual(expected, list.ItemType);
     list.ItemType = input;
     Assert.AreEqual(expected, list.GetItemSchema().DataType);
     Assert.AreEqual(expected, list.ItemType);
 }
示例#6
0
文件: ParseTest.cs 项目: wpq0/FormNG
        public void ParseListReturnsEmptyArrayWhenInputIsEmpty()
        {
            var schema = new ListSchema();
            var field = new Field(schema);

            var parsed = field.Value as object[];
            Assert.IsNotNull(parsed);
            Assert.AreEqual(parsed.Length, 0);
        }
示例#7
0
文件: ParseTest.cs 项目: wpq0/FormNG
        public void CanParseList(string input, DataType itemType, string itemFormat, string setFormat, string cfgFormat, string lstExpectedStr, bool expectSuccess)
        {
            var schema = new ListSchema
            {
                DataFormat = setFormat,
                ItemType = itemType,
                ItemFormat = itemFormat
            };
            var field = new Field(schema, input);
            field.Context.Config.ListFormat = cfgFormat;

            var parsed = (IEnumerable<object>)field.Value;
            var actual = parsed.Select(x => x.ToString()).ToArray();

            if (expectSuccess)
            {
                Assert.AreEqual(NodeState.Resolved, field.State);
                Assert.IsNull(field.Errors);
                var expected = lstExpectedStr.Split(new[] { ',' }, itemType == DataType.String ? StringSplitOptions.None : StringSplitOptions.RemoveEmptyEntries);
                CollectionAssert.AreEqual(expected, actual);
            }
            else
            {
                Assert.AreEqual(NodeState.Fault, field.State);
                Assert.IsNotNull(field.Errors);
                Assert.IsTrue(field.Errors.Any());
                Assert.AreEqual(ValidationType.Format, field.Errors.First().Type);
                CollectionAssert.IsEmpty(actual);
            }
        }