This repository has been archived by the owner on Jan 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from Kentico/Product-updates
Product updates
- Loading branch information
Showing
12 changed files
with
379 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using CMS.Ecommerce; | ||
|
||
using Kentico.Recombee.Mappers; | ||
|
||
using NUnit.Framework; | ||
using NSubstitute; | ||
|
||
namespace Kentico.Recombee.Admin.Tests | ||
{ | ||
[TestFixture] | ||
public class ProductMapperTests | ||
{ | ||
[Test] | ||
public void Map_ProductIsNull_ThrowsArgumentNullException() | ||
{ | ||
Assert.That(() => { ProductMapper.Map(null); }, Throws.ArgumentNullException); | ||
} | ||
|
||
|
||
[Test] | ||
public void Map_ProductPropertiesMappedToCorrectStructure() | ||
{ | ||
var sku = Substitute.For<SKUInfo>(); | ||
sku.SKUPrice.Returns(99.9M); | ||
sku.SKUID.Returns(1); | ||
|
||
var productPage = Substitute.For<SKUTreeNode>(); | ||
productPage.SKU = sku; | ||
productPage.DocumentSKUName.Returns("DocumentSKUName"); | ||
productPage.DocumentSKUShortDescription.Returns("DocumentSKUShortDescription"); | ||
productPage.DocumentSKUDescription.Returns("DocumentSKUDescription"); | ||
productPage.DocumentCulture.Returns("DocumentCulture"); | ||
productPage.ClassName.Returns("ClassName"); | ||
|
||
var result = ProductMapper.Map(productPage); | ||
|
||
Assert.Multiple(() => { | ||
Assert.That(result["Name"], Is.EqualTo("DocumentSKUName")); | ||
Assert.That(result["Description"], Is.EqualTo("DocumentSKUShortDescription")); | ||
Assert.That(result["Content"], Is.EqualTo("DocumentSKUDescription")); | ||
Assert.That(result["Culture"], Is.EqualTo("DocumentCulture")); | ||
Assert.That(result["ClassName"], Is.EqualTo("ClassName")); | ||
Assert.That(result["Price"], Is.EqualTo(99.9M)); | ||
Assert.That(result["Type"], Is.EqualTo("Product")); | ||
}); | ||
} | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
Kentico.Recombee.Admin.Tests/ProductUpdatesProcessorTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using CMS.Core; | ||
using CMS.Ecommerce; | ||
using CMS.Tests; | ||
|
||
using NSubstitute; | ||
using NUnit.Framework; | ||
|
||
namespace Kentico.Recombee.Admin.Tests | ||
{ | ||
[TestFixture] | ||
public class ProductUpdatesProcessorTests : UnitTests | ||
{ | ||
private IProductUpdatesProcessor processor; | ||
private IRecombeeClientService clientService; | ||
|
||
|
||
[SetUp] | ||
public void SetUp() | ||
{ | ||
clientService = Substitute.For<IRecombeeClientService>(); | ||
Service.Use<IRecombeeClientService>(clientService); | ||
|
||
processor = Service.Resolve<IProductUpdatesProcessor>(); | ||
} | ||
|
||
|
||
[Test] | ||
public void Ctor_NullArgument_ThrowsArgumentNullException() | ||
{ | ||
Assert.That(() => { new ProductUpdatesProcessor(null); }, Throws.ArgumentNullException); | ||
} | ||
|
||
|
||
[Test] | ||
public void Methods_NullArgumentt_ThrowsArgumentNullException() | ||
{ | ||
Assert.Multiple(() => | ||
{ | ||
Assert.That(() => { processor.AddProduct(null); }, Throws.ArgumentNullException); | ||
Assert.That(() => { processor.DeleteProduct(null); }, Throws.ArgumentNullException); | ||
}); | ||
} | ||
|
||
|
||
[Test] | ||
public void AddProduct_ProductIsAdded() | ||
{ | ||
var productPage = Substitute.For<SKUTreeNode>(); | ||
|
||
processor.AddProduct(productPage); | ||
|
||
clientService.Received().UpdateProduct(Arg.Is(productPage)); | ||
} | ||
|
||
|
||
[Test] | ||
public void DeleteProduct_ProductIsDeleted() | ||
{ | ||
var productPage = Substitute.For<SKUTreeNode>(); | ||
|
||
processor.DeleteProduct(productPage); | ||
|
||
clientService.Received().Delete(Arg.Is(productPage)); | ||
} | ||
|
||
|
||
[Test] | ||
public void UpdateProduct_ProductIsUdated() | ||
{ | ||
var productPage = Substitute.For<SKUTreeNode>(); | ||
|
||
processor.UpdateProduct(productPage); | ||
|
||
clientService.Received().UpdateProduct(Arg.Is(productPage)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using CMS; | ||
using CMS.Ecommerce; | ||
|
||
using Kentico.Recombee; | ||
|
||
[assembly: RegisterImplementation(typeof(IProductUpdatesProcessor), typeof(ProductUpdatesProcessor))] | ||
|
||
namespace Kentico.Recombee | ||
{ | ||
/// <summary> | ||
/// The contract for an implementation handling product updates. | ||
/// </summary> | ||
public interface IProductUpdatesProcessor | ||
{ | ||
/// <summary> | ||
/// Process added product in Recombee. | ||
/// </summary> | ||
/// <param name="productPage">Product page.</param> | ||
void AddProduct(SKUTreeNode productPage); | ||
|
||
|
||
/// <summary> | ||
/// Deletes given product from recombee database. | ||
/// </summary> | ||
/// <param name="user">Product to delete.</param> | ||
void DeleteProduct(SKUTreeNode productPage); | ||
|
||
|
||
/// <summary> | ||
/// Updates given product from recombee database. | ||
/// </summary> | ||
/// <param name="user">Product to update.</param> | ||
void UpdateProduct(SKUTreeNode productPage); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
using CMS.Ecommerce; | ||
|
||
namespace Kentico.Recombee.Mappers | ||
{ | ||
/// <summary> | ||
/// Encapsulates mapping Kentico objects to Recombee parameters. | ||
/// </summary> | ||
public static class ProductMapper | ||
{ | ||
/// <summary> | ||
/// Maps <paramref name="productPage"/> to Recombee structure. | ||
/// </summary> | ||
/// <param name="productPage">Product page.</param> | ||
public static Dictionary<string, object> Map(SKUTreeNode productPage) | ||
{ | ||
if (productPage is null) | ||
{ | ||
throw new ArgumentNullException(nameof(productPage)); | ||
} | ||
|
||
return new Dictionary<string, object> | ||
{ | ||
{ "Name", productPage.DocumentSKUName }, | ||
{ "Description", productPage.DocumentSKUShortDescription }, | ||
{ "Content", productPage.DocumentSKUDescription}, | ||
{ "Culture", productPage.DocumentCulture}, | ||
{ "ClassName", productPage.ClassName}, | ||
{ "Price", productPage.SKU.SKUPrice }, | ||
{ "Type", "Product"}, | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using System; | ||
using CMS.Ecommerce; | ||
|
||
namespace Kentico.Recombee | ||
{ | ||
/// <summary> | ||
/// Reflects product actions in Recombee database. | ||
/// </summary> | ||
public class ProductUpdatesProcessor : IProductUpdatesProcessor | ||
{ | ||
private readonly IRecombeeClientService recombeeClientService; | ||
|
||
/// <summary> | ||
/// Creates an instance of the <see cref="ProductUpdatesProcessor"/> class. | ||
/// </summary> | ||
/// <param name="recombeeClientService">Client service.</param> | ||
public ProductUpdatesProcessor(IRecombeeClientService recombeeClientService) | ||
{ | ||
this.recombeeClientService = recombeeClientService ?? throw new ArgumentNullException(nameof(recombeeClientService)); | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Process added product in Recombee. | ||
/// </summary> | ||
/// <param name="productPage">Product page.</param> | ||
public void AddProduct(SKUTreeNode productPage) | ||
{ | ||
if (productPage is null) | ||
{ | ||
throw new ArgumentNullException(nameof(productPage)); | ||
} | ||
|
||
UpdateProduct(productPage); | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Deletes given product from recombee database. | ||
/// </summary> | ||
/// <param name="user">Product to delete.</param> | ||
public void DeleteProduct(SKUTreeNode productPage) | ||
{ | ||
if (productPage is null) | ||
{ | ||
throw new ArgumentNullException(nameof(productPage)); | ||
} | ||
|
||
recombeeClientService.Delete(productPage); | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Updates given product from recombee database. | ||
/// </summary> | ||
/// <param name="user">Product to update.</param> | ||
public void UpdateProduct(SKUTreeNode productPage) | ||
{ | ||
if (productPage is null) | ||
{ | ||
throw new ArgumentNullException(nameof(productPage)); | ||
} | ||
|
||
recombeeClientService.UpdateProduct(productPage); | ||
} | ||
} | ||
} |
Oops, something went wrong.