|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Threading; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using FluentAssertions; |
| 7 | +using FluentValidation; |
| 8 | +using FluentValidation.Results; |
| 9 | +using Microsoft.Extensions.Caching.Memory; |
| 10 | +using Microsoft.Extensions.Logging; |
| 11 | +using Microsoft.Extensions.Options; |
| 12 | +using MockQueryable.Moq; |
| 13 | +using Moq; |
| 14 | +using VirtoCommerce.AssetsModule.Core.Assets; |
| 15 | +using VirtoCommerce.CatalogModule.Core.Model; |
| 16 | +using VirtoCommerce.CatalogModule.Core.Services; |
| 17 | +using VirtoCommerce.CatalogModule.Data.Model; |
| 18 | +using VirtoCommerce.CatalogModule.Data.Repositories; |
| 19 | +using VirtoCommerce.CatalogModule.Data.Services; |
| 20 | +using VirtoCommerce.Platform.Caching; |
| 21 | +using VirtoCommerce.Platform.Core.Caching; |
| 22 | +using VirtoCommerce.Platform.Core.Domain; |
| 23 | +using VirtoCommerce.Platform.Core.Events; |
| 24 | +using Xunit; |
| 25 | + |
| 26 | +namespace VirtoCommerce.CatalogModule.Tests; |
| 27 | + |
| 28 | +public class CategoryServiceGetIdsByCodesCacheTests |
| 29 | +{ |
| 30 | + [Fact] |
| 31 | + public async Task GetIdsByCodes_CalledTwiceWithSameCode_LoadsFromRepositoryOnce() |
| 32 | + { |
| 33 | + // Arrange |
| 34 | + var service = GetCategoryService("category-id", "CODE", "catalog-id"); |
| 35 | + |
| 36 | + // Act |
| 37 | + var result1 = await service.GetIdsByCodes("catalog-id", ["CODE"]); |
| 38 | + var result2 = await service.GetIdsByCodes("catalog-id", ["CODE"]); |
| 39 | + |
| 40 | + // Assert |
| 41 | + result1["CODE"].Should().Be("category-id"); |
| 42 | + result2.Should().BeEquivalentTo(result1); |
| 43 | + service.GetIdsByCodesNoCacheCallsCount.Should().Be(1); |
| 44 | + } |
| 45 | + |
| 46 | + [Fact] |
| 47 | + public async Task GetIdsByCodes_AfterCodeChange_DoesNotReturnCachedValue() |
| 48 | + { |
| 49 | + // Arrange |
| 50 | + var service = GetCategoryService("category-id", "CODE", "catalog-id"); |
| 51 | + |
| 52 | + // Act |
| 53 | + var result1 = await service.GetIdsByCodes("catalog-id", ["CODE"]); |
| 54 | + await service.SaveChangesAsync([CreateCategory("category-id", "CODE-NEW", "catalog-id")]); |
| 55 | + var result2 = await service.GetIdsByCodes("catalog-id", ["CODE"]); |
| 56 | + |
| 57 | + // Assert |
| 58 | + result1["CODE"].Should().Be("category-id"); |
| 59 | + result2.Should().BeEmpty(); |
| 60 | + service.GetIdsByCodesNoCacheCallsCount.Should().Be(2); |
| 61 | + } |
| 62 | + |
| 63 | + [Fact] |
| 64 | + public async Task GetIdsByCodes_AfterDelete_DoesNotReturnCachedValue() |
| 65 | + { |
| 66 | + // Arrange |
| 67 | + var service = GetCategoryService("category-id", "CODE", "catalog-id"); |
| 68 | + |
| 69 | + // Act |
| 70 | + var result1 = await service.GetIdsByCodes("catalog-id", ["CODE"]); |
| 71 | + await service.DeleteAsync(["category-id"]); |
| 72 | + var result2 = await service.GetIdsByCodes("catalog-id", ["CODE"]); |
| 73 | + |
| 74 | + // Assert |
| 75 | + result1["CODE"].Should().Be("category-id"); |
| 76 | + result2.Should().BeEmpty(); |
| 77 | + service.GetIdsByCodesNoCacheCallsCount.Should().Be(2); |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | + private static TestableCategoryService GetCategoryService(string categoryId, string categoryCode, string catalogId) |
| 82 | + { |
| 83 | + var categoryEntity = CreateCategoryEntity(categoryId, categoryCode, catalogId); |
| 84 | + var repository = GetCatalogRepository([categoryEntity]); |
| 85 | + |
| 86 | + return new TestableCategoryService( |
| 87 | + () => repository, |
| 88 | + GetPlatformMemoryCache(), |
| 89 | + Mock.Of<IEventPublisher>(), |
| 90 | + GetPropertiesValidator(), |
| 91 | + GetCatalogService(catalogId), |
| 92 | + Mock.Of<IOutlineService>(), |
| 93 | + Mock.Of<IBlobUrlResolver>(), |
| 94 | + Mock.Of<IPropertyValueSanitizer>()); |
| 95 | + } |
| 96 | + |
| 97 | + private static CategoryEntity CreateCategoryEntity(string id, string code, string catalogId) |
| 98 | + { |
| 99 | + return new CategoryEntity |
| 100 | + { |
| 101 | + Id = id, |
| 102 | + Code = code, |
| 103 | + CatalogId = catalogId, |
| 104 | + Name = Guid.NewGuid().ToString(), |
| 105 | + }; |
| 106 | + } |
| 107 | + |
| 108 | + private static Category CreateCategory(string id, string code, string catalogId) |
| 109 | + { |
| 110 | + return new Category |
| 111 | + { |
| 112 | + Id = id, |
| 113 | + Code = code, |
| 114 | + CatalogId = catalogId, |
| 115 | + Name = Guid.NewGuid().ToString(), |
| 116 | + }; |
| 117 | + } |
| 118 | + |
| 119 | + private static ICatalogRepository GetCatalogRepository(List<CategoryEntity> categories) |
| 120 | + { |
| 121 | + var repositoryMock = new Mock<ICatalogRepository>(); |
| 122 | + |
| 123 | + repositoryMock |
| 124 | + .Setup(x => x.Categories) |
| 125 | + .Returns(categories.BuildMockDbSet().Object); |
| 126 | + |
| 127 | + repositoryMock |
| 128 | + .Setup(x => x.GetCategoriesByIdsAsync(It.IsAny<IList<string>>(), It.IsAny<string>())) |
| 129 | + .ReturnsAsync((IList<string> ids, string _) => |
| 130 | + { |
| 131 | + return categories.Where(x => ids.Contains(x.Id)).ToList(); |
| 132 | + }); |
| 133 | + |
| 134 | + repositoryMock |
| 135 | + .Setup(x => x.RemoveCategoriesAsync(It.IsAny<IList<string>>())) |
| 136 | + .Callback((IList<string> ids) => |
| 137 | + { |
| 138 | + categories.RemoveAll(x => ids.Contains(x.Id)); |
| 139 | + }); |
| 140 | + |
| 141 | + repositoryMock |
| 142 | + .Setup(x => x.CategoryLinks) |
| 143 | + .Returns(new List<CategoryRelationEntity>().BuildMockDbSet().Object); |
| 144 | + |
| 145 | + repositoryMock |
| 146 | + .Setup(x => x.Items) |
| 147 | + .Returns(new List<ItemEntity>().BuildMockDbSet().Object); |
| 148 | + |
| 149 | + repositoryMock |
| 150 | + .Setup(x => x.GetAllChildrenCategoriesIdsAsync(It.IsAny<IList<string>>())) |
| 151 | + .ReturnsAsync(new List<string>()); |
| 152 | + |
| 153 | + repositoryMock.Setup(x => x.UnitOfWork).Returns(Mock.Of<IUnitOfWork>()); |
| 154 | + |
| 155 | + return repositoryMock.Object; |
| 156 | + } |
| 157 | + |
| 158 | + private static PlatformMemoryCache GetPlatformMemoryCache() |
| 159 | + { |
| 160 | + var memoryCache = new MemoryCache(Options.Create(new MemoryCacheOptions())); |
| 161 | + |
| 162 | + var platformMemoryCache = new PlatformMemoryCache( |
| 163 | + memoryCache, |
| 164 | + Options.Create(new CachingOptions()), |
| 165 | + new Mock<ILogger<PlatformMemoryCache>>().Object); |
| 166 | + |
| 167 | + return platformMemoryCache; |
| 168 | + } |
| 169 | + |
| 170 | + private static AbstractValidator<IHasProperties> GetPropertiesValidator() |
| 171 | + { |
| 172 | + var hasPropertiesValidatorMock = new Mock<AbstractValidator<IHasProperties>>(); |
| 173 | + |
| 174 | + hasPropertiesValidatorMock |
| 175 | + .Setup(x => x.ValidateAsync(It.IsAny<ValidationContext<IHasProperties>>(), CancellationToken.None)) |
| 176 | + .ReturnsAsync(new ValidationResult()); |
| 177 | + |
| 178 | + return hasPropertiesValidatorMock.Object; |
| 179 | + } |
| 180 | + |
| 181 | + private static ICatalogService GetCatalogService(string catalogId) |
| 182 | + { |
| 183 | + var catalogServiceMock = new Mock<ICatalogService>(); |
| 184 | + |
| 185 | + catalogServiceMock |
| 186 | + .Setup(x => x.GetAsync(It.IsAny<IList<string>>(), It.IsAny<string>(), It.IsAny<bool>())) |
| 187 | + .ReturnsAsync([new Catalog { Id = catalogId }]); |
| 188 | + |
| 189 | + return catalogServiceMock.Object; |
| 190 | + } |
| 191 | + |
| 192 | + private sealed class TestableCategoryService( |
| 193 | + Func<ICatalogRepository> repositoryFactory, |
| 194 | + IPlatformMemoryCache platformMemoryCache, |
| 195 | + IEventPublisher eventPublisher, |
| 196 | + AbstractValidator<IHasProperties> hasPropertyValidator, |
| 197 | + ICatalogService catalogService, |
| 198 | + IOutlineService outlineService, |
| 199 | + IBlobUrlResolver blobUrlResolver, |
| 200 | + IPropertyValueSanitizer propertyValueSanitizer) |
| 201 | + : CategoryService(repositoryFactory, platformMemoryCache, eventPublisher, hasPropertyValidator, catalogService, outlineService, blobUrlResolver, propertyValueSanitizer) |
| 202 | + { |
| 203 | + public int GetIdsByCodesNoCacheCallsCount { get; private set; } |
| 204 | + |
| 205 | + protected override Task<IList<CategoryCodeCacheItem>> GetIdsByCodesNoCache(string catalogId, IList<string> codes) |
| 206 | + { |
| 207 | + GetIdsByCodesNoCacheCallsCount++; |
| 208 | + return base.GetIdsByCodesNoCache(catalogId, codes); |
| 209 | + } |
| 210 | + } |
| 211 | +} |
0 commit comments