-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathCustomerOrderServiceImplIntegrationTests.cs
More file actions
281 lines (261 loc) · 12.4 KB
/
CustomerOrderServiceImplIntegrationTests.cs
File metadata and controls
281 lines (261 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Internal;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Primitives;
using Moq;
using VirtoCommerce.AssetsModule.Core.Assets;
using VirtoCommerce.CoreModule.Core.Common;
using VirtoCommerce.OrdersModule.Core.Model;
using VirtoCommerce.OrdersModule.Core.Model.Search;
using VirtoCommerce.OrdersModule.Core.Services;
using VirtoCommerce.OrdersModule.Data.Repositories;
using VirtoCommerce.OrdersModule.Data.Services;
using VirtoCommerce.PaymentModule.Core.Model;
using VirtoCommerce.PaymentModule.Core.Model.Search;
using VirtoCommerce.PaymentModule.Core.Services;
using VirtoCommerce.Platform.Caching;
using VirtoCommerce.Platform.Core.Bus;
using VirtoCommerce.Platform.Core.Caching;
using VirtoCommerce.Platform.Core.ChangeLog;
using VirtoCommerce.Platform.Core.Common;
using VirtoCommerce.Platform.Core.DynamicProperties;
using VirtoCommerce.Platform.Core.Events;
using VirtoCommerce.Platform.Core.GenericCrud;
using VirtoCommerce.ShippingModule.Core.Model.Search;
using VirtoCommerce.ShippingModule.Core.Services;
using VirtoCommerce.StoreModule.Core.Services;
using Xunit;
using Address = VirtoCommerce.OrdersModule.Core.Model.Address;
namespace VirtoCommerce.OrdersModule.Tests
{
[Trait("Category", "IntegrationTest")]
[Trait("Category", "CI")]
public class CustomerOrderServiceImplIntegrationTests
{
private readonly Mock<IStoreService> _storeServiceMock;
private readonly Mock<IShippingMethodsRegistrar> _shippingMethodRegistrarMock;
private readonly Mock<IShippingMethodsSearchService> _shippingMethodsSearchServiceMock;
private readonly Mock<IPaymentMethodsRegistrar> _paymentMethodRegistrarMock;
private readonly Mock<IPaymentMethodsSearchService> _paymentMethodsSearchService;
private readonly Mock<ICustomerOrderTotalsCalculator> _customerOrderTotalsCalculatorMock;
private readonly Mock<ITenantUniqueNumberGenerator> _uniqueNumberGeneratorMock;
private readonly Mock<IDynamicPropertyService> _dynamicPropertyServiceMock;
private readonly IPlatformMemoryCache _platformMemoryCache;
private readonly Mock<IChangeLogService> _changeLogServiceMock;
private readonly Mock<ICacheEntry> _cacheEntryMock;
private readonly ICustomerOrderService _customerOrderService;
private readonly ICustomerOrderSearchService _customerOrderSearchService;
private readonly Mock<ILogger<PlatformMemoryCache>> _logMock;
private readonly Mock<ILogger<InProcessBus>> _logEventMock;
private readonly Mock<IBlobUrlResolver> _blobUrlResolver;
public CustomerOrderServiceImplIntegrationTests()
{
_storeServiceMock = new Mock<IStoreService>();
_shippingMethodRegistrarMock = new Mock<IShippingMethodsRegistrar>();
_shippingMethodsSearchServiceMock = new Mock<IShippingMethodsSearchService>();
_shippingMethodsSearchServiceMock.Setup(s => s.SearchAsync(It.IsAny<ShippingMethodsSearchCriteria>(), It.IsAny<bool>())).ReturnsAsync(new ShippingMethodsSearchResult());
_paymentMethodRegistrarMock = new Mock<IPaymentMethodsRegistrar>();
_paymentMethodsSearchService = new Mock<IPaymentMethodsSearchService>();
_paymentMethodsSearchService.Setup(s => s.SearchAsync(It.IsAny<PaymentMethodsSearchCriteria>(), It.IsAny<bool>())).ReturnsAsync(new PaymentMethodsSearchResult());
_uniqueNumberGeneratorMock = new Mock<ITenantUniqueNumberGenerator>();
_customerOrderTotalsCalculatorMock = new Mock<ICustomerOrderTotalsCalculator>();
_dynamicPropertyServiceMock = new Mock<IDynamicPropertyService>();
_cacheEntryMock = new Mock<ICacheEntry>();
_cacheEntryMock.SetupGet(c => c.ExpirationTokens).Returns(new List<IChangeToken>());
_changeLogServiceMock = new Mock<IChangeLogService>();
_logMock = new Mock<ILogger<PlatformMemoryCache>>();
_logEventMock = new Mock<ILogger<InProcessBus>>();
_blobUrlResolver = new Mock<IBlobUrlResolver>();
var cachingOptions = new OptionsWrapper<CachingOptions>(new CachingOptions { CacheEnabled = true });
var memoryCache = new MemoryCache(new MemoryCacheOptions()
{
Clock = new SystemClock(),
});
_platformMemoryCache = new PlatformMemoryCache(memoryCache, cachingOptions, _logMock.Object);
var container = new ServiceCollection();
container.AddDbContext<OrderDbContext>(options => options.UseSqlServer("Data Source=(local);Initial Catalog=VirtoCommerce3_orderTest;Persist Security Info=True;User ID=virto;Password=virto;MultipleActiveResultSets=True;Connect Timeout=30"));
container.AddScoped<IOrderRepository, OrderRepository>();
container.AddScoped<ICustomerOrderService, CustomerOrderService>();
container.AddScoped<ICustomerOrderSearchService, CustomerOrderSearchService>();
container.AddScoped<Func<IOrderRepository>>(provider => () => provider.CreateScope().ServiceProvider.GetService<IOrderRepository>());
container.AddScoped<IEventPublisher, InProcessBus>();
container.AddSingleton(tc => _customerOrderTotalsCalculatorMock.Object);
container.AddSingleton(x => _uniqueNumberGeneratorMock.Object);
container.AddSingleton(x => _dynamicPropertyServiceMock.Object);
container.AddSingleton(x => _storeServiceMock.Object);
container.AddSingleton(x => _shippingMethodRegistrarMock.Object);
container.AddSingleton(x => _shippingMethodsSearchServiceMock.Object);
container.AddSingleton(x => _paymentMethodRegistrarMock.Object);
container.AddSingleton(x => _paymentMethodsSearchService.Object);
container.AddSingleton(x => _platformMemoryCache);
container.AddSingleton(x => _changeLogServiceMock.Object);
container.AddSingleton(x => _logEventMock.Object);
container.AddSingleton(x => _blobUrlResolver.Object);
container.AddOptions<CrudOptions>();
var serviceProvider = container.BuildServiceProvider();
_customerOrderService = serviceProvider.GetService<ICustomerOrderService>();
_customerOrderSearchService = serviceProvider.GetService<ICustomerOrderSearchService>();
}
[Fact]
public async Task SaveChangesAsync_CreateNewOrder()
{
//Arrange
var order = GetTestOrder($"order{DateTime.Now:O}");
//Act
await _customerOrderService.SaveChangesAsync(new[] { order });
order = await _customerOrderService.GetByIdAsync(order.Id);
//Assert
Assert.Equal(PaymentStatus.Pending, order.InPayments.First().PaymentStatus);
Assert.NotNull(order);
Assert.Equal(PaymentStatus.Pending, order.InPayments.First().PaymentStatus);
Assert.NotNull(order.InPayments.First().Status);
}
[Fact]
public async Task SaveChangesAsync_CreateNewOrderAndUpdateOrderState()
{
//Arrange
var orderId = $"order{DateTime.Now:O}";
var order = GetTestOrder(orderId);
await _customerOrderService.SaveChangesAsync(new[] { order });
var criteria = new CustomerOrderSearchCriteria() { Take = 1, Ids = new[] { orderId } };
var orders = await _customerOrderSearchService.SearchAsync(criteria);
order = orders.Results.FirstOrDefault();
order.Status = "Authorized";
//Act
await _customerOrderService.SaveChangesAsync(new[] { order });
//Assert
Assert.NotNull(order);
Assert.Equal("Authorized", order.Status);
}
private static CustomerOrder GetTestOrder(string id)
{
var order = new CustomerOrder
{
Id = id,
Number = "CO" + id,
Currency = "USD",
CustomerId = "vasja customer",
EmployeeId = "employe",
StoreId = "test store",
Addresses = new[]
{
new Address
{
AddressType = AddressType.Shipping,
City = "london",
Phone = "+68787687",
PostalCode = "22222",
CountryCode = "ENG",
CountryName = "England",
Email = "user@mail.com",
FirstName = "first name",
LastName = "last name",
Line1 = "line 1",
Organization = "org1"
}
}.ToList(),
Discounts =
[
new Discount
{
PromotionId = "testPromotion",
Name = "Test Promotion",
Description = "This is a test promotion",
Currency = "USD",
DiscountAmount = 12,
Coupon = "ssss"
}
]
};
var item1 = new LineItem
{
Id = Guid.NewGuid().ToString(),
Sku = "shoes",
Price = 10,
ProductId = "shoes",
CatalogId = "catalog",
Currency = "USD",
CategoryId = "category",
Name = "shoes",
Quantity = 2,
FulfillmentLocationCode = "warehouse1",
ShippingMethodCode = "EMS",
Discounts = [ new Discount
{
PromotionId = "itemPromotion",
Name = "Test Promotion",
Description = "This is a test promotion",
Currency = "USD",
DiscountAmount = 12,
Coupon = "ssss"
}]
};
var item2 = new LineItem
{
Id = Guid.NewGuid().ToString(),
Sku = "t-shirt",
Price = 100,
ProductId = "t-shirt",
CatalogId = "catalog",
CategoryId = "category",
Currency = "USD",
Name = "t-shirt",
Quantity = 2,
FulfillmentLocationCode = "warehouse1",
ShippingMethodCode = "EMS"
};
order.Items = new List<LineItem>();
order.Items.Add(item1);
order.Items.Add(item2);
var shipment = new Shipment
{
Number = "SH" + id,
Currency = "USD",
DeliveryAddress = new Address
{
City = "london",
CountryName = "England",
Phone = "+68787687",
PostalCode = "2222",
CountryCode = "ENG",
Email = "user@mail.com",
FirstName = "first name",
LastName = "last name",
Line1 = "line 1",
Organization = "org1"
},
Discounts = [ new Discount
{
PromotionId = "testPromotion",
Name = "Test Promotion",
Description = "This is a test promotion",
Currency = "USD",
DiscountAmount = 12,
Coupon = ""
}],
};
shipment.Items = new List<ShipmentItem>();
shipment.Items.AddRange(order.Items.Select(x => new ShipmentItem(x)));
order.Shipments = new List<Shipment>();
order.Shipments.Add(shipment);
var payment = new PaymentIn
{
Number = "PI" + id,
PaymentStatus = PaymentStatus.Pending,
Currency = "USD",
Sum = 10,
CustomerId = "et"
};
order.InPayments = new List<PaymentIn>();
order.InPayments.Add(payment);
return order;
}
}
}