/// <summary>
 /// Gets collection of gallery items.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the Microsoft.Azure.Gallery.IItemOperations.
 /// </param>
 /// <param name='parameters'>
 /// Optional. Query parameters. If null is passed returns all gallery
 /// items.
 /// </param>
 /// <returns>
 /// List of gallery items.
 /// </returns>
 public static ItemListResult List(this IItemOperations operations, ItemListParameters parameters)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((IItemOperations)s).ListAsync(parameters);
     }
     , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }
 /// <summary>
 /// Gets collection of gallery items.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the Microsoft.Azure.Gallery.IItemOperations.
 /// </param>
 /// <param name='parameters'>
 /// Optional. Query parameters. If null is passed returns all gallery
 /// items.
 /// </param>
 /// <returns>
 /// List of gallery items.
 /// </returns>
 public static Task<ItemListResult> ListAsync(this IItemOperations operations, ItemListParameters parameters)
 {
     return operations.ListAsync(parameters, CancellationToken.None);
 }
        public void FiltersGalleryTemplatesLatestVersion()
        {
            string filterString = FilterString.Generate<ItemListFilter>(f => f.Publisher == "Microsoft");
            ItemListParameters actual = new ItemListParameters();
            galleryClientMock.Setup(f => f.Items.ListAsync(It.IsAny<ItemListParameters>(), new CancellationToken()))
                .Returns(Task.Factory.StartNew(() => new ItemListResult
                {
                    Items = new List<GalleryItem>()
                    {
                        new GalleryItem()
                        {
                            Name = "Template0",
                            Publisher = "Microsoft",
                            Version = "0.0.0.0"
                        },
                        new GalleryItem()
                        {
                            Name = "Template1",
                            Publisher = "Microsoft",
                            Version = "0.0.0.1"
                        },
                        new GalleryItem()
                        {
                            Name = "Template2",
                            Publisher = "Microsoft",
                            Version = "0.0.0.2"
                        }
                    }
                }))
                .Callback((ItemListParameters p, CancellationToken c) => actual = p);

            FilterGalleryTemplatesOptions options = new FilterGalleryTemplatesOptions()
            {
                Publisher = "Microsoft"
            };

            List<PSGalleryItem> result = galleryTemplatesClient.FilterGalleryTemplates(options);

            Assert.Equal(1, result.Count);
            Assert.Equal("Template2", result[0].Name);
        }
        public void FiltersGalleryTemplatesUsingComplexQuery()
        {
            string filterString = "Publisher eq 'Microsoft' and CategoryIds/any(c: c eq 'awesome')";
            ItemListParameters actual = new ItemListParameters();
            galleryClientMock.Setup(f => f.Items.ListAsync(It.IsAny<ItemListParameters>(), new CancellationToken()))
                .Returns(Task.Factory.StartNew(() => new ItemListResult
                {
                    Items = new List<GalleryItem>()
                    {
                        new GalleryItem()
                        {
                            Name = "Template1",
                            Publisher = "Microsoft"
                        },
                        new GalleryItem()
                        {
                            Name = "Template2",
                            Publisher = "Microsoft"
                        }
                    }
                }))
                .Callback((ItemListParameters p, CancellationToken c) => actual = p);

            FilterGalleryTemplatesOptions options = new FilterGalleryTemplatesOptions()
            {
                Publisher = "Microsoft",
                Category = "awesome",
                AllVersions = true
            };

            List<PSGalleryItem> result = galleryTemplatesClient.FilterGalleryTemplates(options);

            Assert.Equal(2, result.Count);
            Assert.Equal(filterString, actual.Filter);
        }
        private List<GalleryItem> QueryGalleryTemplates(FilterGalleryTemplatesOptions options, List<string> filterStrings, ItemListParameters parameters)
        {
            if (!string.IsNullOrEmpty(options.Publisher))
            {
                filterStrings.Add(FilterString.Generate<ItemListFilter>(f => f.Publisher == options.Publisher));
            }

            if (!string.IsNullOrEmpty(options.Category))
            {
                filterStrings.Add(FilterString.Generate<ItemListFilter>(f => f.CategoryIds.Contains(options.Category)));
            }

            if (filterStrings.Count > 0)
            {
                parameters = new ItemListParameters() { Filter = string.Join(" and ", filterStrings) };
            }

            List<GalleryItem> galleryItems = GalleryClient.Items.List(parameters).Items.ToList();
            if (!string.IsNullOrEmpty(options.ApplicationName))
            {
                List<GalleryItem> result = new List<GalleryItem>();
                string wildcardApplicationName = Regex.Escape(options.ApplicationName).Replace(@"\*", ".*").Replace(@"\?", ".");
                Regex regex = new Regex(wildcardApplicationName, RegexOptions.IgnoreCase);
                foreach (var galleryItem in galleryItems)
                {
                    if (regex.IsMatch(galleryItem.Name))
                    {
                        result.Add(galleryItem);
                    }
                }

                return result;
            }

            return galleryItems;
        }
        private List<GalleryItem> QueryGalleryTemplates(FilterGalleryTemplatesOptions options, List<string> filterStrings, ItemListParameters parameters)
        {
            if (!string.IsNullOrEmpty(options.Publisher))
            {
                filterStrings.Add(FilterString.Generate<ItemListFilter>(f => f.Publisher == options.Publisher));
            }

            if (!string.IsNullOrEmpty(options.Category))
            {
                filterStrings.Add(FilterString.Generate<ItemListFilter>(f => f.CategoryIds.Contains(options.Category)));
            }

            if (filterStrings.Count > 0)
            {
                parameters = new ItemListParameters() { Filter = string.Join(" and ", filterStrings) };
            }

            return GalleryClient.Items.List(parameters).Items.ToList();
        }