Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.
C# aspnet-corecsharpdockerdotnetecommerceecommerce-platformmongodbmulti-storemulti-vendornosqlonline-shopshoppingcart
Add tests for ImportDataObject
supportaf5426ba4f42
diff --git a/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/BrandImportDataObjectTests.cs b/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/BrandImportDataObjectTests.csnew file mode 100644index 000000000..ab5825ac3--- /dev/null+++ b/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/BrandImportDataObjectTests.cs@@ -0,0 +1,175 @@+using AutoMapper;+using Grand.Business.Catalog.Services.Brands;+using Grand.Business.Catalog.Services.ExportImport.Dto;+using Grand.Business.Core.Interfaces.Catalog.Brands;+using Grand.Business.Core.Interfaces.Common.Localization;+using Grand.Business.Core.Interfaces.Common.Seo;+using Grand.Business.Core.Interfaces.Storage;+using Grand.Data.Tests.MongoDb;+using Grand.Domain.Catalog;+using Grand.Domain.Customers;+using Grand.Domain.Data;+using Grand.Domain.Localization;+using Grand.Infrastructure;+using Grand.Infrastructure.Caching;+using Grand.Infrastructure.Mapper;+using Grand.Infrastructure.Tests.Caching;+using Grand.Infrastructure.TypeSearchers;+using MediatR;+using Microsoft.VisualStudio.TestTools.UnitTesting;+using Moq;++namespace Grand.Business.Catalog.Services.ExportImport.Tests+{+ [TestClass()]+ public class BrandImportDataObjectTests+ {+ private BrandImportDataObject _brandImportDataObject;+ private IBrandService _brandService;+ private Mock<IWorkContext> _workContextMock;+ private Mock<IPictureService> _pictureServiceMock;+ private Mock<IBrandLayoutService> _brandLayoutServiceMock;+ private Mock<ISlugService> _slugServiceMock;+ private Mock<ILanguageService> _languageServiceMock;+++ private IRepository<Brand> _repository;+ private Mock<IMediator> _mediatorMock;+ private MemoryCacheBase _cacheBase;++ [TestInitialize()]+ public void Init()+ {+ InitAutoMapper();++ _repository = new MongoDBRepositoryTest<Brand>();++ _pictureServiceMock = new Mock<IPictureService>();+ _brandLayoutServiceMock = new Mock<IBrandLayoutService>();+ _slugServiceMock = new Mock<ISlugService>();+ _languageServiceMock = new Mock<ILanguageService>();+ _workContextMock = new Mock<IWorkContext>();+ _workContextMock.Setup(c => c.CurrentStore).Returns(() => new Domain.Stores.Store() { Id = "" });+ _workContextMock.Setup(c => c.CurrentCustomer).Returns(() => new Customer());++ _mediatorMock = new Mock<IMediator>();+ _cacheBase = new MemoryCacheBase(MemoryCacheTest.Get(), _mediatorMock.Object);+ _brandService = new BrandService(_cacheBase, _repository, _workContextMock.Object, _mediatorMock.Object);++ _brandImportDataObject = new BrandImportDataObject(_brandService, _pictureServiceMock.Object, _brandLayoutServiceMock.Object, _slugServiceMock.Object, _languageServiceMock.Object, new Domain.Seo.SeoSettings());+ }++ [TestMethod()]+ public async Task ExecuteTest_Import_Insert()+ {+ //Arrange+ var brands = new List<BrandDto>();+ brands.Add(new BrandDto() { Name = "test1", Published = true });+ brands.Add(new BrandDto() { Name = "test2", Published = true });+ brands.Add(new BrandDto() { Name = "test3", Published = true });+ _brandLayoutServiceMock.Setup(c => c.GetBrandLayoutById(It.IsAny<string>())).Returns(Task.FromResult(new BrandLayout()));+ _brandLayoutServiceMock.Setup(c => c.GetAllBrandLayouts()).Returns(Task.FromResult<IList<BrandLayout>>(new List<BrandLayout>() { new BrandLayout() }));+ _languageServiceMock.Setup(c => c.GetAllLanguages(It.IsAny<bool>(), It.IsAny<string>())).Returns(Task.FromResult<IList<Language>>(new List<Language>()));+ _slugServiceMock.Setup(c => c.GetBySlug(It.IsAny<string>())).Returns(Task.FromResult(new Domain.Seo.EntityUrl() { Slug = "slug" }));+ //Act+ await _brandImportDataObject.Execute(brands);++ //Assert+ Assert.IsTrue(_repository.Table.Any());+ Assert.AreEqual(3, _repository.Table.Count());++ }+ [TestMethod()]+ public async Task ExecuteTest_Import_Update()+ {+ //Arrange+ var brand1 = new Brand() {+ Name = "insert1"+ };+ await _brandService.InsertBrand(brand1);+ var brand2 = new Brand() {+ Name = "insert2"+ };+ await _brandService.InsertBrand(brand2);+ var brand3 = new Brand() {+ Name = "insert3"+ };+ await _brandService.InsertBrand(brand3);+++ var brands = new List<BrandDto>();+ brands.Add(new BrandDto() { Id = brand1.Id, Name = "update1", Published = false, DisplayOrder = 1 });+ brands.Add(new BrandDto() { Id = brand2.Id, Name = "update2", Published = false, DisplayOrder = 2 });+ brands.Add(new BrandDto() { Id = brand3.Id, Name = "update3", Published = false, DisplayOrder = 3 });++ _brandLayoutServiceMock.Setup(c => c.GetBrandLayoutById(It.IsAny<string>())).Returns(Task.FromResult(new BrandLayout()));+ _brandLayoutServiceMock.Setup(c => c.GetAllBrandLayouts()).Returns(Task.FromResult<IList<BrandLayout>>(new List<BrandLayout>() { new BrandLayout() }));+ _languageServiceMock.Setup(c => c.GetAllLanguages(It.IsAny<bool>(), It.IsAny<string>())).Returns(Task.FromResult<IList<Language>>(new List<Language>()));+ _slugServiceMock.Setup(c => c.GetBySlug(It.IsAny<string>())).Returns(Task.FromResult(new Domain.Seo.EntityUrl() { Slug = "slug" }));+ //Act+ await _brandImportDataObject.Execute(brands);++ //Assert+ Assert.IsTrue(_repository.Table.Any());+ Assert.AreEqual(3, _repository.Table.Count());+ Assert.AreEqual("update3", _repository.Table.FirstOrDefault(x => x.Id == brand3.Id).Name);+ Assert.AreEqual(3, _repository.Table.FirstOrDefault(x => x.Id == brand3.Id).DisplayOrder);+ Assert.AreEqual(false, _repository.Table.FirstOrDefault(x => x.Id == brand3.Id).Published);++ }++ [TestMethod()]+ public async Task ExecuteTest_Import_Insert_Update()+ {+ //Arrange+ var brand3 = new Brand() {+ Name = "insert3"+ };+ await _brandService.InsertBrand(brand3);++ var brands = new List<BrandDto>();+ brands.Add(new BrandDto() { Name = "update1", Published = false, DisplayOrder = 1 });+ brands.Add(new BrandDto() { Name = "update2", Published = false, DisplayOrder = 2 });+ brands.Add(new BrandDto() { Id = brand3.Id, Name = "update3", Published = false, DisplayOrder = 3 });++ _brandLayoutServiceMock.Setup(c => c.GetBrandLayoutById(It.IsAny<string>())).Returns(Task.FromResult(new BrandLayout()));+ _brandLayoutServiceMock.Setup(c => c.GetAllBrandLayouts()).Returns(Task.FromResult<IList<BrandLayout>>(new List<BrandLayout>() { new BrandLayout() }));+ _languageServiceMock.Setup(c => c.GetAllLanguages(It.IsAny<bool>(), It.IsAny<string>())).Returns(Task.FromResult<IList<Language>>(new List<Language>()));+ _slugServiceMock.Setup(c => c.GetBySlug(It.IsAny<string>())).Returns(Task.FromResult(new Domain.Seo.EntityUrl() { Slug = "slug" }));+ //Act+ await _brandImportDataObject.Execute(brands);++ //Assert+ Assert.IsTrue(_repository.Table.Any());+ Assert.AreEqual(3, _repository.Table.Count());+ Assert.AreEqual("update3", _repository.Table.FirstOrDefault(x => x.Id == brand3.Id).Name);+ Assert.AreEqual(3, _repository.Table.FirstOrDefault(x => x.Id == brand3.Id).DisplayOrder);+ Assert.AreEqual(false, _repository.Table.FirstOrDefault(x => x.Id == brand3.Id).Published);++ }++ private void InitAutoMapper()+ {+ var typeSearcher = new TypeSearcher();+ //find mapper configurations provided by other assemblies+ var mapperConfigurations = typeSearcher.ClassesOfType<IAutoMapperProfile>();++ //create and sort instances of mapper configurations+ var instances = mapperConfigurations+ .Select(mapperConfiguration => (IAutoMapperProfile)Activator.CreateInstance(mapperConfiguration))+ .OrderBy(mapperConfiguration => mapperConfiguration.Order);++ //create AutoMapper configuration+ var config = new MapperConfiguration(cfg =>+ {+ foreach (var instance in instances)+ {+ cfg.AddProfile(instance.GetType());+ }+ });++ //register automapper+ AutoMapperConfig.Init(config);+ }+ }+}\ No newline at end of filediff --git a/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/CategoryImportDataObjectTests.cs b/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/CategoryImportDataObjectTests.csnew file mode 100644index 000000000..2b2e85518--- /dev/null+++ b/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/CategoryImportDataObjectTests.cs@@ -0,0 +1,176 @@+using AutoMapper;+using Grand.Business.Catalog.Services.Categories;+using Grand.Business.Catalog.Services.ExportImport.Dto;+using Grand.Business.Common.Services.Security;+using Grand.Business.Core.Interfaces.Catalog.Categories;+using Grand.Business.Core.Interfaces.Common.Localization;+using Grand.Business.Core.Interfaces.Common.Seo;+using Grand.Business.Core.Interfaces.Storage;+using Grand.Data.Tests.MongoDb;+using Grand.Domain.Catalog;+using Grand.Domain.Customers;+using Grand.Domain.Data;+using Grand.Domain.Localization;+using Grand.Infrastructure;+using Grand.Infrastructure.Caching;+using Grand.Infrastructure.Mapper;+using Grand.Infrastructure.Tests.Caching;+using Grand.Infrastructure.TypeSearchers;+using MediatR;+using Microsoft.VisualStudio.TestTools.UnitTesting;+using Moq;++namespace Grand.Business.Catalog.Services.ExportImport.Tests+{+ [TestClass()]+ public class CategoryImportDataObjectTests+ {+ private CategoryImportDataObject _categoryImportDataObject;+ private ICategoryService _categoryService;+ private Mock<IWorkContext> _workContextMock;+ private Mock<IPictureService> _pictureServiceMock;+ private Mock<ICategoryLayoutService> _categoryLayoutServiceMock;+ private Mock<ISlugService> _slugServiceMock;+ private Mock<ILanguageService> _languageServiceMock;+++ private IRepository<Category> _repository;+ private Mock<IMediator> _mediatorMock;+ private MemoryCacheBase _cacheBase;++ [TestInitialize()]+ public void Init()+ {+ InitAutoMapper();++ _repository = new MongoDBRepositoryTest<Category>();++ _pictureServiceMock = new Mock<IPictureService>();+ _categoryLayoutServiceMock = new Mock<ICategoryLayoutService>();+ _slugServiceMock = new Mock<ISlugService>();+ _languageServiceMock = new Mock<ILanguageService>();+ _workContextMock = new Mock<IWorkContext>();+ _workContextMock.Setup(c => c.CurrentStore).Returns(() => new Domain.Stores.Store() { Id = "" });+ _workContextMock.Setup(c => c.CurrentCustomer).Returns(() => new Customer());++ _mediatorMock = new Mock<IMediator>();+ _cacheBase = new MemoryCacheBase(MemoryCacheTest.Get(), _mediatorMock.Object);+ _categoryService = new CategoryService(_cacheBase, _repository, _workContextMock.Object, _mediatorMock.Object, new AclService());++ _categoryImportDataObject = new CategoryImportDataObject(_categoryService, _pictureServiceMock.Object, _categoryLayoutServiceMock.Object, _slugServiceMock.Object, _languageServiceMock.Object, new Domain.Seo.SeoSettings());+ }++ [TestMethod()]+ public async Task ExecuteTest_Import_Insert()+ {+ //Arrange+ var categorys = new List<CategoryDto>();+ categorys.Add(new CategoryDto() { Name = "test1", Published = true });+ categorys.Add(new CategoryDto() { Name = "test2", Published = true });+ categorys.Add(new CategoryDto() { Name = "test3", Published = true });+ _categoryLayoutServiceMock.Setup(c => c.GetCategoryLayoutById(It.IsAny<string>())).Returns(Task.FromResult(new CategoryLayout()));+ _categoryLayoutServiceMock.Setup(c => c.GetAllCategoryLayouts()).Returns(Task.FromResult<IList<CategoryLayout>>(new List<CategoryLayout>() { new CategoryLayout() }));+ _languageServiceMock.Setup(c => c.GetAllLanguages(It.IsAny<bool>(), It.IsAny<string>())).Returns(Task.FromResult<IList<Language>>(new List<Language>()));+ _slugServiceMock.Setup(c => c.GetBySlug(It.IsAny<string>())).Returns(Task.FromResult(new Domain.Seo.EntityUrl() { Slug = "slug" }));+ //Act+ await _categoryImportDataObject.Execute(categorys);++ //Assert+ Assert.IsTrue(_repository.Table.Any());+ Assert.AreEqual(3, _repository.Table.Count());++ }+ [TestMethod()]+ public async Task ExecuteTest_Import_Update()+ {+ //Arrange+ var category1 = new Category() {+ Name = "insert1"+ };+ await _categoryService.InsertCategory(category1);+ var category2 = new Category() {+ Name = "insert2"+ };+ await _categoryService.InsertCategory(category2);+ var category3 = new Category() {+ Name = "insert3"+ };+ await _categoryService.InsertCategory(category3);+++ var categorys = new List<CategoryDto>();+ categorys.Add(new CategoryDto() { Id = category1.Id, Name = "update1", Published = false, DisplayOrder = 1 });+ categorys.Add(new CategoryDto() { Id = category2.Id, Name = "update2", Published = false, DisplayOrder = 2 });+ categorys.Add(new CategoryDto() { Id = category3.Id, Name = "update3", Published = false, DisplayOrder = 3 });++ _categoryLayoutServiceMock.Setup(c => c.GetCategoryLayoutById(It.IsAny<string>())).Returns(Task.FromResult(new CategoryLayout()));+ _categoryLayoutServiceMock.Setup(c => c.GetAllCategoryLayouts()).Returns(Task.FromResult<IList<CategoryLayout>>(new List<CategoryLayout>() { new CategoryLayout() }));+ _languageServiceMock.Setup(c => c.GetAllLanguages(It.IsAny<bool>(), It.IsAny<string>())).Returns(Task.FromResult<IList<Language>>(new List<Language>()));+ _slugServiceMock.Setup(c => c.GetBySlug(It.IsAny<string>())).Returns(Task.FromResult(new Domain.Seo.EntityUrl() { Slug = "slug" }));+ //Act+ await _categoryImportDataObject.Execute(categorys);++ //Assert+ Assert.IsTrue(_repository.Table.Any());+ Assert.AreEqual(3, _repository.Table.Count());+ Assert.AreEqual("update3", _repository.Table.FirstOrDefault(x => x.Id == category3.Id).Name);+ Assert.AreEqual(3, _repository.Table.FirstOrDefault(x => x.Id == category3.Id).DisplayOrder);+ Assert.AreEqual(false, _repository.Table.FirstOrDefault(x => x.Id == category3.Id).Published);++ }++ [TestMethod()]+ public async Task ExecuteTest_Import_Insert_Update()+ {+ //Arrange+ var category3 = new Category() {+ Name = "insert3"+ };+ await _categoryService.InsertCategory(category3);++ var categorys = new List<CategoryDto>();+ categorys.Add(new CategoryDto() { Name = "update1", Published = false, DisplayOrder = 1 });+ categorys.Add(new CategoryDto() { Name = "update2", Published = false, DisplayOrder = 2 });+ categorys.Add(new CategoryDto() { Id = category3.Id, Name = "update3", Published = false, DisplayOrder = 3 });++ _categoryLayoutServiceMock.Setup(c => c.GetCategoryLayoutById(It.IsAny<string>())).Returns(Task.FromResult(new CategoryLayout()));+ _categoryLayoutServiceMock.Setup(c => c.GetAllCategoryLayouts()).Returns(Task.FromResult<IList<CategoryLayout>>(new List<CategoryLayout>() { new CategoryLayout() }));+ _languageServiceMock.Setup(c => c.GetAllLanguages(It.IsAny<bool>(), It.IsAny<string>())).Returns(Task.FromResult<IList<Language>>(new List<Language>()));+ _slugServiceMock.Setup(c => c.GetBySlug(It.IsAny<string>())).Returns(Task.FromResult(new Domain.Seo.EntityUrl() { Slug = "slug" }));+ //Act+ await _categoryImportDataObject.Execute(categorys);++ //Assert+ Assert.IsTrue(_repository.Table.Any());+ Assert.AreEqual(3, _repository.Table.Count());+ Assert.AreEqual("update3", _repository.Table.FirstOrDefault(x => x.Id == category3.Id).Name);+ Assert.AreEqual(3, _repository.Table.FirstOrDefault(x => x.Id == category3.Id).DisplayOrder);+ Assert.AreEqual(false, _repository.Table.FirstOrDefault(x => x.Id == category3.Id).Published);++ }++ private void InitAutoMapper()+ {+ var typeSearcher = new TypeSearcher();+ //find mapper configurations provided by other assemblies+ var mapperConfigurations = typeSearcher.ClassesOfType<IAutoMapperProfile>();++ //create and sort instances of mapper configurations+ var instances = mapperConfigurations+ .Select(mapperConfiguration => (IAutoMapperProfile)Activator.CreateInstance(mapperConfiguration))+ .OrderBy(mapperConfiguration => mapperConfiguration.Order);++ //create AutoMapper configuration+ var config = new MapperConfiguration(cfg =>+ {+ foreach (var instance in instances)+ {+ cfg.AddProfile(instance.GetType());+ }+ });++ //register automapper+ AutoMapperConfig.Init(config);+ }+ }+}\ No newline at end of filediff --git a/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/CollectionImportDataObjectTests.cs b/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/CollectionImportDataObjectTests.csnew file mode 100644index 000000000..0bab5063f--- /dev/null+++ b/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/CollectionImportDataObjectTests.cs@@ -0,0 +1,176 @@+using AutoMapper;+using Grand.Business.Catalog.Services.Collections;+using Grand.Business.Catalog.Services.ExportImport.Dto;+using Grand.Business.Common.Services.Security;+using Grand.Business.Core.Interfaces.Catalog.Collections;+using Grand.Business.Core.Interfaces.Common.Localization;+using Grand.Business.Core.Interfaces.Common.Seo;+using Grand.Business.Core.Interfaces.Storage;+using Grand.Data.Tests.MongoDb;+using Grand.Domain.Catalog;+using Grand.Domain.Customers;+using Grand.Domain.Data;+using Grand.Domain.Localization;+using Grand.Infrastructure;+using Grand.Infrastructure.Caching;+using Grand.Infrastructure.Mapper;+using Grand.Infrastructure.Tests.Caching;+using Grand.Infrastructure.TypeSearchers;+using MediatR;+using Microsoft.VisualStudio.TestTools.UnitTesting;+using Moq;++namespace Grand.Business.Catalog.Services.ExportImport.Tests+{+ [TestClass()]+ public class CollectionImportDataObjectTests+ {+ private CollectionImportDataObject _collectionImportDataObject;+ private ICollectionService _collectionService;+ private Mock<IWorkContext> _workContextMock;+ private Mock<IPictureService> _pictureServiceMock;+ private Mock<ICollectionLayoutService> _collectionLayoutServiceMock;+ private Mock<ISlugService> _slugServiceMock;+ private Mock<ILanguageService> _languageServiceMock;+++ private IRepository<Collection> _repository;+ private Mock<IMediator> _mediatorMock;+ private MemoryCacheBase _cacheBase;++ [TestInitialize()]+ public void Init()+ {+ InitAutoMapper();++ _repository = new MongoDBRepositoryTest<Collection>();++ _pictureServiceMock = new Mock<IPictureService>();+ _collectionLayoutServiceMock = new Mock<ICollectionLayoutService>();+ _slugServiceMock = new Mock<ISlugService>();+ _languageServiceMock = new Mock<ILanguageService>();+ _workContextMock = new Mock<IWorkContext>();+ _workContextMock.Setup(c => c.CurrentStore).Returns(() => new Domain.Stores.Store() { Id = "" });+ _workContextMock.Setup(c => c.CurrentCustomer).Returns(() => new Customer());++ _mediatorMock = new Mock<IMediator>();+ _cacheBase = new MemoryCacheBase(MemoryCacheTest.Get(), _mediatorMock.Object);+ _collectionService = new CollectionService(_cacheBase, _repository, _workContextMock.Object, _mediatorMock.Object, new AclService());++ _collectionImportDataObject = new CollectionImportDataObject(_collectionService, _pictureServiceMock.Object, _collectionLayoutServiceMock.Object, _slugServiceMock.Object, _languageServiceMock.Object, new Domain.Seo.SeoSettings());+ }++ [TestMethod()]+ public async Task ExecuteTest_Import_Insert()+ {+ //Arrange+ var collections = new List<CollectionDto>();+ collections.Add(new CollectionDto() { Name = "test1", Published = true });+ collections.Add(new CollectionDto() { Name = "test2", Published = true });+ collections.Add(new CollectionDto() { Name = "test3", Published = true });+ _collectionLayoutServiceMock.Setup(c => c.GetCollectionLayoutById(It.IsAny<string>())).Returns(Task.FromResult(new CollectionLayout()));+ _collectionLayoutServiceMock.Setup(c => c.GetAllCollectionLayouts()).Returns(Task.FromResult<IList<CollectionLayout>>(new List<CollectionLayout>() { new CollectionLayout() }));+ _languageServiceMock.Setup(c => c.GetAllLanguages(It.IsAny<bool>(), It.IsAny<string>())).Returns(Task.FromResult<IList<Language>>(new List<Language>()));+ _slugServiceMock.Setup(c => c.GetBySlug(It.IsAny<string>())).Returns(Task.FromResult(new Domain.Seo.EntityUrl() { Slug = "slug" }));+ //Act+ await _collectionImportDataObject.Execute(collections);++ //Assert+ Assert.IsTrue(_repository.Table.Any());+ Assert.AreEqual(3, _repository.Table.Count());++ }+ [TestMethod()]+ public async Task ExecuteTest_Import_Update()+ {+ //Arrange+ var collection1 = new Collection() {+ Name = "insert1"+ };+ await _collectionService.InsertCollection(collection1);+ var collection2 = new Collection() {+ Name = "insert2"+ };+ await _collectionService.InsertCollection(collection2);+ var collection3 = new Collection() {+ Name = "insert3"+ };+ await _collectionService.InsertCollection(collection3);+++ var collections = new List<CollectionDto>();+ collections.Add(new CollectionDto() { Id = collection1.Id, Name = "update1", Published = false, DisplayOrder = 1 });+ collections.Add(new CollectionDto() { Id = collection2.Id, Name = "update2", Published = false, DisplayOrder = 2 });+ collections.Add(new CollectionDto() { Id = collection3.Id, Name = "update3", Published = false, DisplayOrder = 3 });++ _collectionLayoutServiceMock.Setup(c => c.GetCollectionLayoutById(It.IsAny<string>())).Returns(Task.FromResult(new CollectionLayout()));+ _collectionLayoutServiceMock.Setup(c => c.GetAllCollectionLayouts()).Returns(Task.FromResult<IList<CollectionLayout>>(new List<CollectionLayout>() { new CollectionLayout() }));+ _languageServiceMock.Setup(c => c.GetAllLanguages(It.IsAny<bool>(), It.IsAny<string>())).Returns(Task.FromResult<IList<Language>>(new List<Language>()));+ _slugServiceMock.Setup(c => c.GetBySlug(It.IsAny<string>())).Returns(Task.FromResult(new Domain.Seo.EntityUrl() { Slug = "slug" }));+ //Act+ await _collectionImportDataObject.Execute(collections);++ //Assert+ Assert.IsTrue(_repository.Table.Any());+ Assert.AreEqual(3, _repository.Table.Count());+ Assert.AreEqual("update3", _repository.Table.FirstOrDefault(x => x.Id == collection3.Id).Name);+ Assert.AreEqual(3, _repository.Table.FirstOrDefault(x => x.Id == collection3.Id).DisplayOrder);+ Assert.AreEqual(false, _repository.Table.FirstOrDefault(x => x.Id == collection3.Id).Published);++ }++ [TestMethod()]+ public async Task ExecuteTest_Import_Insert_Update()+ {+ //Arrange+ var collection3 = new Collection() {+ Name = "insert3"+ };+ await _collectionService.InsertCollection(collection3);++ var collections = new List<CollectionDto>();+ collections.Add(new CollectionDto() { Name = "update1", Published = false, DisplayOrder = 1 });+ collections.Add(new CollectionDto() { Name = "update2", Published = false, DisplayOrder = 2 });+ collections.Add(new CollectionDto() { Id = collection3.Id, Name = "update3", Published = false, DisplayOrder = 3 });++ _collectionLayoutServiceMock.Setup(c => c.GetCollectionLayoutById(It.IsAny<string>())).Returns(Task.FromResult(new CollectionLayout()));+ _collectionLayoutServiceMock.Setup(c => c.GetAllCollectionLayouts()).Returns(Task.FromResult<IList<CollectionLayout>>(new List<CollectionLayout>() { new CollectionLayout() }));+ _languageServiceMock.Setup(c => c.GetAllLanguages(It.IsAny<bool>(), It.IsAny<string>())).Returns(Task.FromResult<IList<Language>>(new List<Language>()));+ _slugServiceMock.Setup(c => c.GetBySlug(It.IsAny<string>())).Returns(Task.FromResult(new Domain.Seo.EntityUrl() { Slug = "slug" }));+ //Act+ await _collectionImportDataObject.Execute(collections);++ //Assert+ Assert.IsTrue(_repository.Table.Any());+ Assert.AreEqual(3, _repository.Table.Count());+ Assert.AreEqual("update3", _repository.Table.FirstOrDefault(x => x.Id == collection3.Id).Name);+ Assert.AreEqual(3, _repository.Table.FirstOrDefault(x => x.Id == collection3.Id).DisplayOrder);+ Assert.AreEqual(false, _repository.Table.FirstOrDefault(x => x.Id == collection3.Id).Published);++ }++ private void InitAutoMapper()+ {+ var typeSearcher = new TypeSearcher();+ //find mapper configurations provided by other assemblies+ var mapperConfigurations = typeSearcher.ClassesOfType<IAutoMapperProfile>();++ //create and sort instances of mapper configurations+ var instances = mapperConfigurations+ .Select(mapperConfiguration => (IAutoMapperProfile)Activator.CreateInstance(mapperConfiguration))+ .OrderBy(mapperConfiguration => mapperConfiguration.Order);++ //create AutoMapper configuration+ var config = new MapperConfiguration(cfg =>+ {+ foreach (var instance in instances)+ {+ cfg.AddProfile(instance.GetType());+ }+ });++ //register automapper+ AutoMapperConfig.Init(config);+ }+ }+}\ No newline at end of filediff --git a/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/ProductImportDataObjectTests.cs b/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/ProductImportDataObjectTests.csnew file mode 100644index 000000000..37b116a72--- /dev/null+++ b/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/ProductImportDataObjectTests.cs@@ -0,0 +1,247 @@+using AutoMapper;+using Grand.Business.Catalog.Services.Products;+using Grand.Business.Catalog.Services.ExportImport.Dto;+using Grand.Business.Common.Services.Security;+using Grand.Business.Core.Interfaces.Catalog.Products;+using Grand.Business.Core.Interfaces.Common.Localization;+using Grand.Business.Core.Interfaces.Common.Seo;+using Grand.Business.Core.Interfaces.Storage;+using Grand.Data.Tests.MongoDb;+using Grand.Domain.Catalog;+using Grand.Domain.Customers;+using Grand.Domain.Data;+using Grand.Domain.Localization;+using Grand.Infrastructure;+using Grand.Infrastructure.Caching;+using Grand.Infrastructure.Mapper;+using Grand.Infrastructure.Tests.Caching;+using Grand.Infrastructure.TypeSearchers;+using MediatR;+using Microsoft.VisualStudio.TestTools.UnitTesting;+using Moq;+using Grand.Business.Core.Interfaces.Checkout.Shipping;+using Grand.Business.Core.Interfaces.Catalog.Tax;+using Grand.Business.Core.Interfaces.Catalog.Directory;+using Grand.Business.Core.Interfaces.Catalog.Categories;+using Grand.Business.Core.Interfaces.Catalog.Brands;+using Grand.Business.Core.Interfaces.Catalog.Collections;++namespace Grand.Business.Catalog.Services.ExportImport.Tests+{+ [TestClass()]+ public class ProductImportDataObjectTests+ {+ private ProductImportDataObject _productImportDataObject;+ private IProductService _productService;+ private Mock<IWorkContext> _workContextMock;+ private Mock<IPictureService> _pictureServiceMock;+ private Mock<IProductLayoutService> _productLayoutServiceMock;+ private Mock<ISlugService> _slugServiceMock;+ private Mock<ILanguageService> _languageServiceMock;++ private Mock<IDeliveryDateService> _deliveryDateServiceMock;+ private Mock<ITaxCategoryService> _taxServiceMock;+ private Mock<IWarehouseService> _warehouseServiceMock;+ private Mock<IMeasureService> _measureServiceMock;+ private Mock<ICategoryService> _categoryServiceMock;+ private Mock<IProductCategoryService> _productCategoryServiceMock;+ private Mock<IBrandService> _brandServiceMock;+ private Mock<ICollectionService> _collectionServiceMock;+ private Mock<IProductCollectionService> _productCollectionServiceMock;++ private IRepository<Product> _repository;+ private Mock<IMediator> _mediatorMock;+ private MemoryCacheBase _cacheBase;++ [TestInitialize()]+ public void Init()+ {+ InitAutoMapper();++ _repository = new MongoDBRepositoryTest<Product>();++ _pictureServiceMock = new Mock<IPictureService>();+ _productLayoutServiceMock = new Mock<IProductLayoutService>();+ _slugServiceMock = new Mock<ISlugService>();+ _languageServiceMock = new Mock<ILanguageService>();+ _deliveryDateServiceMock = new Mock<IDeliveryDateService>();+ _brandServiceMock = new Mock<IBrandService>();+ _collectionServiceMock = new Mock<ICollectionService>();+ _taxServiceMock = new Mock<ITaxCategoryService>();+ _categoryServiceMock = new Mock<ICategoryService>();+ _warehouseServiceMock = new Mock<IWarehouseService>();+ _measureServiceMock = new Mock<IMeasureService>();+ _productCategoryServiceMock = new Mock<IProductCategoryService>();+ _productCollectionServiceMock = new Mock<IProductCollectionService>();++ _workContextMock = new Mock<IWorkContext>();+ _workContextMock.Setup(c => c.CurrentStore).Returns(() => new Domain.Stores.Store() { Id = "" });+ _workContextMock.Setup(c => c.CurrentCustomer).Returns(() => new Customer());++ _mediatorMock = new Mock<IMediator>();+ _cacheBase = new MemoryCacheBase(MemoryCacheTest.Get(), _mediatorMock.Object);+ _productService = new ProductService(_cacheBase, _repository, _workContextMock.Object, _mediatorMock.Object, new AclService());++ _productImportDataObject = new ProductImportDataObject+ (_productService, _pictureServiceMock.Object, _productLayoutServiceMock.Object, _deliveryDateServiceMock.Object,+ _taxServiceMock.Object, _warehouseServiceMock.Object, _measureServiceMock.Object, _slugServiceMock.Object, _languageServiceMock.Object,+ _categoryServiceMock.Object, _productCategoryServiceMock.Object, _brandServiceMock.Object, _collectionServiceMock.Object,+ _productCollectionServiceMock.Object,+ new Domain.Seo.SeoSettings());+ }++ [TestMethod()]+ public async Task ExecuteTest_Import_Insert()+ {+ //Arrange+ var products = new List<ProductDto>();+ products.Add(new ProductDto() { Name = "test1", Published = true });+ products.Add(new ProductDto() { Name = "test2", Published = true });+ products.Add(new ProductDto() { Name = "test3", Published = true });++ _productLayoutServiceMock.Setup(c => c.GetProductLayoutById(It.IsAny<string>())).Returns(Task.FromResult(new ProductLayout()));+ _productLayoutServiceMock.Setup(c => c.GetAllProductLayouts()).Returns(Task.FromResult<IList<ProductLayout>>(new List<ProductLayout>() { new ProductLayout() }));++ _deliveryDateServiceMock.Setup(c => c.GetDeliveryDateById(It.IsAny<string>())).Returns(Task.FromResult(new Domain.Shipping.DeliveryDate()));+ _deliveryDateServiceMock.Setup(c => c.GetAllDeliveryDates()).Returns(Task.FromResult<IList<Domain.Shipping.DeliveryDate>>(new List<Domain.Shipping.DeliveryDate>() { new Domain.Shipping.DeliveryDate() }));++ _taxServiceMock.Setup(c => c.GetTaxCategoryById(It.IsAny<string>())).Returns(Task.FromResult(new Domain.Tax.TaxCategory()));+ _taxServiceMock.Setup(c => c.GetAllTaxCategories()).Returns(Task.FromResult<IList<Domain.Tax.TaxCategory>>(new List<Domain.Tax.TaxCategory>() { new Domain.Tax.TaxCategory() }));++ _warehouseServiceMock.Setup(c => c.GetWarehouseById(It.IsAny<string>())).Returns(Task.FromResult(new Domain.Shipping.Warehouse()));+ _warehouseServiceMock.Setup(c => c.GetAllWarehouses()).Returns(Task.FromResult<IList<Domain.Shipping.Warehouse>>(new List<Domain.Shipping.Warehouse>() { new Domain.Shipping.Warehouse() }));++ _measureServiceMock.Setup(c => c.GetMeasureUnitById(It.IsAny<string>())).Returns(Task.FromResult(new Domain.Directory.MeasureUnit()));+ _measureServiceMock.Setup(c => c.GetAllMeasureUnits()).Returns(Task.FromResult<IList<Domain.Directory.MeasureUnit>>(new List<Domain.Directory.MeasureUnit>() { new Domain.Directory.MeasureUnit() }));++ _languageServiceMock.Setup(c => c.GetAllLanguages(It.IsAny<bool>(), It.IsAny<string>())).Returns(Task.FromResult<IList<Language>>(new List<Language>()));+ _slugServiceMock.Setup(c => c.GetBySlug(It.IsAny<string>())).Returns(Task.FromResult(new Domain.Seo.EntityUrl() { Slug = "slug" }));++ //Act+ await _productImportDataObject.Execute(products);++ //Assert+ Assert.IsTrue(_repository.Table.Any());+ Assert.AreEqual(3, _repository.Table.Count());++ }+ [TestMethod()]+ public async Task ExecuteTest_Import_Update()+ {+ //Arrange+ var product1 = new Product() {+ Name = "insert1"+ };+ await _productService.InsertProduct(product1);+ var product2 = new Product() {+ Name = "insert2"+ };+ await _productService.InsertProduct(product2);+ var product3 = new Product() {+ Name = "insert3"+ };+ await _productService.InsertProduct(product3);+++ var products = new List<ProductDto>();+ products.Add(new ProductDto() { Id = product1.Id, Name = "update1", Published = false, DisplayOrder = 1 });+ products.Add(new ProductDto() { Id = product2.Id, Name = "update2", Published = false, DisplayOrder = 2 });+ products.Add(new ProductDto() { Id = product3.Id, Name = "update3", Published = false, DisplayOrder = 3 });++ _productLayoutServiceMock.Setup(c => c.GetProductLayoutById(It.IsAny<string>())).Returns(Task.FromResult(new ProductLayout()));+ _productLayoutServiceMock.Setup(c => c.GetAllProductLayouts()).Returns(Task.FromResult<IList<ProductLayout>>(new List<ProductLayout>() { new ProductLayout() }));+ _languageServiceMock.Setup(c => c.GetAllLanguages(It.IsAny<bool>(), It.IsAny<string>())).Returns(Task.FromResult<IList<Language>>(new List<Language>()));++ _deliveryDateServiceMock.Setup(c => c.GetDeliveryDateById(It.IsAny<string>())).Returns(Task.FromResult(new Domain.Shipping.DeliveryDate()));+ _deliveryDateServiceMock.Setup(c => c.GetAllDeliveryDates()).Returns(Task.FromResult<IList<Domain.Shipping.DeliveryDate>>(new List<Domain.Shipping.DeliveryDate>() { new Domain.Shipping.DeliveryDate() }));++ _taxServiceMock.Setup(c => c.GetTaxCategoryById(It.IsAny<string>())).Returns(Task.FromResult(new Domain.Tax.TaxCategory()));+ _taxServiceMock.Setup(c => c.GetAllTaxCategories()).Returns(Task.FromResult<IList<Domain.Tax.TaxCategory>>(new List<Domain.Tax.TaxCategory>() { new Domain.Tax.TaxCategory() }));++ _warehouseServiceMock.Setup(c => c.GetWarehouseById(It.IsAny<string>())).Returns(Task.FromResult(new Domain.Shipping.Warehouse()));+ _warehouseServiceMock.Setup(c => c.GetAllWarehouses()).Returns(Task.FromResult<IList<Domain.Shipping.Warehouse>>(new List<Domain.Shipping.Warehouse>() { new Domain.Shipping.Warehouse() }));++ _measureServiceMock.Setup(c => c.GetMeasureUnitById(It.IsAny<string>())).Returns(Task.FromResult(new Domain.Directory.MeasureUnit()));+ _measureServiceMock.Setup(c => c.GetAllMeasureUnits()).Returns(Task.FromResult<IList<Domain.Directory.MeasureUnit>>(new List<Domain.Directory.MeasureUnit>() { new Domain.Directory.MeasureUnit() }));++ _slugServiceMock.Setup(c => c.GetBySlug(It.IsAny<string>())).Returns(Task.FromResult(new Domain.Seo.EntityUrl() { Slug = "slug" }));+ //Act+ await _productImportDataObject.Execute(products);++ //Assert+ Assert.IsTrue(_repository.Table.Any());+ Assert.AreEqual(3, _repository.Table.Count());+ Assert.AreEqual("update3", _repository.Table.FirstOrDefault(x => x.Id == product3.Id).Name);+ Assert.AreEqual(3, _repository.Table.FirstOrDefault(x => x.Id == product3.Id).DisplayOrder);+ Assert.AreEqual(false, _repository.Table.FirstOrDefault(x => x.Id == product3.Id).Published);++ }++ [TestMethod()]+ public async Task ExecuteTest_Import_Insert_Update()+ {+ //Arrange+ var product3 = new Product() {+ Name = "insert3"+ };+ await _productService.InsertProduct(product3);++ var products = new List<ProductDto>();+ products.Add(new ProductDto() { Name = "update1", Published = false, DisplayOrder = 1 });+ products.Add(new ProductDto() { Name = "update2", Published = false, DisplayOrder = 2 });+ products.Add(new ProductDto() { Id = product3.Id, Name = "update3", Published = false, DisplayOrder = 3 });++ _productLayoutServiceMock.Setup(c => c.GetProductLayoutById(It.IsAny<string>())).Returns(Task.FromResult(new ProductLayout()));+ _productLayoutServiceMock.Setup(c => c.GetAllProductLayouts()).Returns(Task.FromResult<IList<ProductLayout>>(new List<ProductLayout>() { new ProductLayout() }));+ _languageServiceMock.Setup(c => c.GetAllLanguages(It.IsAny<bool>(), It.IsAny<string>())).Returns(Task.FromResult<IList<Language>>(new List<Language>()));++ _deliveryDateServiceMock.Setup(c => c.GetDeliveryDateById(It.IsAny<string>())).Returns(Task.FromResult(new Domain.Shipping.DeliveryDate()));+ _deliveryDateServiceMock.Setup(c => c.GetAllDeliveryDates()).Returns(Task.FromResult<IList<Domain.Shipping.DeliveryDate>>(new List<Domain.Shipping.DeliveryDate>() { new Domain.Shipping.DeliveryDate() }));++ _taxServiceMock.Setup(c => c.GetTaxCategoryById(It.IsAny<string>())).Returns(Task.FromResult(new Domain.Tax.TaxCategory()));+ _taxServiceMock.Setup(c => c.GetAllTaxCategories()).Returns(Task.FromResult<IList<Domain.Tax.TaxCategory>>(new List<Domain.Tax.TaxCategory>() { new Domain.Tax.TaxCategory() }));++ _warehouseServiceMock.Setup(c => c.GetWarehouseById(It.IsAny<string>())).Returns(Task.FromResult(new Domain.Shipping.Warehouse()));+ _warehouseServiceMock.Setup(c => c.GetAllWarehouses()).Returns(Task.FromResult<IList<Domain.Shipping.Warehouse>>(new List<Domain.Shipping.Warehouse>() { new Domain.Shipping.Warehouse() }));++ _measureServiceMock.Setup(c => c.GetMeasureUnitById(It.IsAny<string>())).Returns(Task.FromResult(new Domain.Directory.MeasureUnit()));+ _measureServiceMock.Setup(c => c.GetAllMeasureUnits()).Returns(Task.FromResult<IList<Domain.Directory.MeasureUnit>>(new List<Domain.Directory.MeasureUnit>() { new Domain.Directory.MeasureUnit() }));++ _slugServiceMock.Setup(c => c.GetBySlug(It.IsAny<string>())).Returns(Task.FromResult(new Domain.Seo.EntityUrl() { Slug = "slug" }));+ //Act+ await _productImportDataObject.Execute(products);++ //Assert+ Assert.IsTrue(_repository.Table.Any());+ Assert.AreEqual(3, _repository.Table.Count());+ Assert.AreEqual("update3", _repository.Table.FirstOrDefault(x => x.Id == product3.Id).Name);+ Assert.AreEqual(3, _repository.Table.FirstOrDefault(x => x.Id == product3.Id).DisplayOrder);+ Assert.AreEqual(false, _repository.Table.FirstOrDefault(x => x.Id == product3.Id).Published);++ }++ private void InitAutoMapper()+ {+ var typeSearcher = new TypeSearcher();+ //find mapper configurations provided by other assemblies+ var mapperConfigurations = typeSearcher.ClassesOfType<IAutoMapperProfile>();++ //create and sort instances of mapper configurations+ var instances = mapperConfigurations+ .Select(mapperConfiguration => (IAutoMapperProfile)Activator.CreateInstance(mapperConfiguration))+ .OrderBy(mapperConfiguration => mapperConfiguration.Order);++ //create AutoMapper configuration+ var config = new MapperConfiguration(cfg =>+ {+ foreach (var instance in instances)+ {+ cfg.AddProfile(instance.GetType());+ }+ });++ //register automapper+ AutoMapperConfig.Init(config);+ }+ }+}\ No newline at end of filediff --git a/src/Tests/Grand.Business.Common.Tests/Services/ExportImport/CountryImportDataObjectTests.cs b/src/Tests/Grand.Business.Common.Tests/Services/ExportImport/CountryImportDataObjectTests.csnew file mode 100644index 000000000..f920051f2--- /dev/null+++ b/src/Tests/Grand.Business.Common.Tests/Services/ExportImport/CountryImportDataObjectTests.cs@@ -0,0 +1,102 @@+using Microsoft.VisualStudio.TestTools.UnitTesting;+using Grand.Business.Common.Services.ExportImport;+using System;+using System.Collections.Generic;+using System.Linq;+using System.Text;+using System.Threading.Tasks;+using Grand.Business.Core.Interfaces.Common.Directory;+using Grand.Domain.Data;+using Grand.Domain.Directory;+using Moq;+using MediatR;+using Grand.Infrastructure.Caching;+using Grand.Data.Tests.MongoDb;+using Grand.Infrastructure.Tests.Caching;+using Grand.Business.Common.Services.Directory;++namespace Grand.Business.Common.Services.ExportImport.Tests+{+ [TestClass()]+ public class CountryImportDataObjectTests+ {+ private CountryImportDataObject _countryImportDataObject;+ private ICountryService _countryService;++ private IRepository<Country> _repository;+ private Mock<IMediator> _mediatorMock;+ private MemoryCacheBase _cacheBase;++ [TestInitialize()]+ public void Init()+ {+ _repository = new MongoDBRepositoryTest<Country>();++ _mediatorMock = new Mock<IMediator>();+ _cacheBase = new MemoryCacheBase(MemoryCacheTest.Get(), _mediatorMock.Object);+ _countryService = new CountryService(_repository, _mediatorMock.Object, _cacheBase);+ _countryImportDataObject = new CountryImportDataObject(_countryService);+ }++ [TestMethod()]+ public async Task ExecuteTest_Import_Insert_NewStates()+ {+ //Arrange+ var country = new Country() { TwoLetterIsoCode = "US", Name = "USA" };+ await _repository.InsertAsync(country);+ var states = new List<CountryStates>() {+ new CountryStates() { Country = country.TwoLetterIsoCode, Abbreviation = "AK", StateProvinceName = "Alaska", Published = true },+ new CountryStates() { Country = country.TwoLetterIsoCode, Abbreviation = "CA", StateProvinceName = "California", Published = true, DisplayOrder = 1 }+ };+ //Act+ await _countryImportDataObject.Execute(states);++ //Assert+ Assert.IsTrue(_repository.Table.FirstOrDefault(x => x.Id == country.Id).StateProvinces.Any());++ }+ [TestMethod()]+ public async Task ExecuteTest_Import_Update_States()+ {+ //Arrange+ var country = new Country() { TwoLetterIsoCode = "US", Name = "USA" };+ country.StateProvinces.Add(new StateProvince() { Abbreviation = "AK" });+ country.StateProvinces.Add(new StateProvince() { Abbreviation = "CA" });++ await _repository.InsertAsync(country);+ var states = new List<CountryStates>() {+ new CountryStates() { Country = country.TwoLetterIsoCode, Abbreviation = "AK", StateProvinceName = "Alaska", Published = true },+ new CountryStates() { Country = country.TwoLetterIsoCode, Abbreviation = "CA", StateProvinceName = "California", Published = true, DisplayOrder = 1 }+ };+ //Act+ await _countryImportDataObject.Execute(states);++ //Assert+ Assert.IsTrue(_repository.Table.FirstOrDefault(x => x.Id == country.Id).StateProvinces.Any(x=>x.Published));++ }++ [TestMethod()]+ public async Task ExecuteTest_Import_Insert_Update_States()+ {+ //Arrange+ var country = new Country() { TwoLetterIsoCode = "US", Name = "USA" };+ country.StateProvinces.Add(new StateProvince() { Abbreviation = "AK" });+ country.StateProvinces.Add(new StateProvince() { Abbreviation = "CA" });++ await _repository.InsertAsync(country);+ var states = new List<CountryStates>() {+ new CountryStates() { Country = country.TwoLetterIsoCode, Abbreviation = "AK", StateProvinceName = "Alaska", Published = true },+ new CountryStates() { Country = country.TwoLetterIsoCode, Abbreviation = "CA", StateProvinceName = "California", Published = true, DisplayOrder = 1 },+ new CountryStates() { Country = country.TwoLetterIsoCode, Abbreviation = "HI", StateProvinceName = "Hawaii", Published = true, DisplayOrder = 1 }+ };+ //Act+ await _countryImportDataObject.Execute(states);++ //Assert+ Assert.IsTrue(_repository.Table.FirstOrDefault(x => x.Id == country.Id).StateProvinces.Any(x => x.Published));+ Assert.IsTrue(_repository.Table.FirstOrDefault(x => x.Id == country.Id).StateProvinces.Count == 3);++ }+ }+}\ No newline at end of file