Exemplo n.º 1
0
        public async Task Test_EFDataProvider_AuditEntityAsyncAction_TypeMapper_3()
        {
            //AuditEntityAction<T>(Func<AuditEvent, EventEntry, T, Task> asyncAction)

            Audit.Core.Configuration.Setup()
            .UseEntityFramework(config => config
                                .AuditTypeMapper(t => typeof(OrderAudit))
                                .AuditEntityAction <OrderAudit>(async(ae, ee, audEnt) =>
            {
                audEnt.AuditDate = DateTime.UtcNow;
                await Task.Delay(1000);
                audEnt.Status = "FromAction";
            }));

            var id = Guid.NewGuid().ToString();

            using (var ctx = new AuditPerTableContext())
            {
                var o = new Order()
                {
                    Number = id,
                    Status = "Pending"
                };
                ctx.Order.Add(o);
                await ctx.SaveChangesAsync();
            }

            using (var ctx = new AuditPerTableContext())
            {
                var order = await ctx.OrderAudit.AsQueryable().FirstOrDefaultAsync(o => o.Number == id);

                Assert.IsNotNull(order);
                Assert.AreEqual("FromAction", order.Status);
            }
        }
Exemplo n.º 2
0
        public async Task Test_EFDataProvider_ProxiedLazyLoading_Async()
        {
            var evs = new List <AuditEvent>();

            Audit.Core.Configuration.AddCustomAction(ActionType.OnEventSaved, scope =>
            {
                evs.Add(scope.Event);
            });

            var dp = new EntityFrameworkDataProvider();

            dp.AuditTypeMapper = t =>
            {
                if (t == typeof(Order))
                {
                    return(typeof(OrderAudit));
                }
                if (t == typeof(Orderline))
                {
                    return(typeof(OrderlineAudit));
                }
                return(null);
            };

            dp.AuditEntityAction = (ev, entry, obj) =>
            {
                var ab = obj as AuditBase;
                if (ab != null)
                {
                    ab.AuditDate   = DateTime.UtcNow;
                    ab.UserName    = ev.Environment.UserName;
                    ab.AuditStatus = entry.Action;
                }
                return(true);
            };

            Audit.Core.Configuration.Setup()
            .UseCustomProvider(dp);
            var id = Guid.NewGuid().ToString();

            using (var ctx = new AuditPerTableContext())
            {
                var o = await ctx.Order.FirstOrDefaultAsync();

                o.Number = id;
                await ctx.SaveChangesAsync();
            }

            using (var ctx = new AuditPerTableContext())
            {
                var orderAudits = await ctx.OrderAudit.AsNoTracking().Where(a => a.Number.Equals(id)).OrderByDescending(a => a.AuditDate).ToListAsync();

                Assert.AreEqual(1, orderAudits.Count);
                Assert.AreEqual("Update", orderAudits[0].AuditStatus);
                Assert.AreEqual(id, orderAudits[0].Number);
            }
            Assert.AreEqual(1, evs.Count);
            Assert.AreEqual(1, evs[0].GetEntityFrameworkEvent().Entries.Count);
            Assert.IsTrue(evs[0].GetEntityFrameworkEvent().Entries[0].GetEntry().Entity.GetType().FullName.StartsWith("Castle.Proxies."));
        }
Exemplo n.º 3
0
        public async Task Test_EFDataProvider_AuditEntityDisabledAsync()
        {
            var dp = new EntityFrameworkDataProvider();

            dp.AuditTypeMapper = t =>
            {
                if (t == typeof(Order))
                {
                    return(typeof(OrderAudit));
                }
                if (t == typeof(Orderline))
                {
                    return(typeof(OrderlineAudit));
                }
                return(null);
            };

            dp.AuditEntityAction = (ev, entry, obj) =>
            {
                // return false to avoid saving
                return(false);
            };

            Audit.Core.Configuration.Setup()
            .UseCustomProvider(dp);
            var id = Guid.NewGuid().ToString();

            using (var ctx = new AuditPerTableContext())
            {
                var o = new Order()
                {
                    Number     = id,
                    Status     = "Pending",
                    OrderLines = new Collection <Orderline>()
                    {
                        new Orderline()
                        {
                            Product = "p1: " + id, Quantity = 2
                        },
                        new Orderline()
                        {
                            Product = "p2: " + id, Quantity = 3
                        }
                    }
                };
                await ctx.AddAsync(o);

                await ctx.SaveChangesAsync();
            }

            using (var ctx = new AuditPerTableContext())
            {
                var order = await ctx.Order.SingleAsync(a => a.Number.Equals(id));

                var orderlineAudits = ctx.OrderlineAudit.AsNoTracking().Where(a => a.OrderId.Equals(order.Id)).ToList();
                Assert.AreEqual(0, orderlineAudits.Count);
            }
        }
Exemplo n.º 4
0
        public async Task Test_EFDataProvider_AuditEntityDisabled_Fluent_Async()
        {
            Audit.Core.Configuration.Setup()
            .UseEntityFramework(config => config
                                .AuditTypeMapper(typeName => Type.GetType(typeName + "Audit"))
                                .AuditEntityAction((ev, ent, audEnt) =>
            {
                return(false);
            })
                                );

            var id = Guid.NewGuid().ToString();

            using (var ctx = new AuditPerTableContext())
            {
                var o = new Order()
                {
                    Number     = id,
                    Status     = "Pending",
                    OrderLines = new Collection <Orderline>()
                    {
                        new Orderline()
                        {
                            Product = "p1: " + id, Quantity = 2
                        },
                        new Orderline()
                        {
                            Product = "p2: " + id, Quantity = 3
                        }
                    }
                };
                await ctx.AddAsync(o);

                await ctx.SaveChangesAsync();
            }

            using (var ctx = new AuditPerTableContext())
            {
                var order = await ctx.Order.SingleAsync(a => a.Number.Equals(id));

                var orderlineAudits = ctx.OrderlineAudit.AsNoTracking().Where(a => a.OrderId.Equals(order.Id)).ToList();
                Assert.AreEqual(0, orderlineAudits.Count);
            }
        }
Exemplo n.º 5
0
        public async Task Test_EFDataProvider_AuditEntityAsyncAction_2()
        {
            //AuditEntityAction(Func<AuditEvent, EventEntry, object, Task<bool>> entityAsyncAction);

            Audit.Core.Configuration.Setup()
            .UseEntityFramework(config => config
                                .AuditTypeExplicitMapper(m => m
                                                         .Map <Order, OrderAudit>()
                                                         .AuditEntityAction(async(ev, ent, obj) =>
            {
                var audEnt       = (OrderAudit)obj;
                audEnt.AuditDate = DateTime.UtcNow;
                await Task.Delay(1000);
                audEnt.Status = "FromAction";
                return(true);
            }))
                                );

            var id = Guid.NewGuid().ToString();

            using (var ctx = new AuditPerTableContext())
            {
                var o = new Order()
                {
                    Number = id,
                    Status = "Pending"
                };
                ctx.Order.Add(o);
                await ctx.SaveChangesAsync();
            }

            using (var ctx = new AuditPerTableContext())
            {
                var order = await ctx.OrderAudit.AsQueryable().FirstOrDefaultAsync(o => o.Number == id);

                Assert.IsNotNull(order);
                Assert.AreEqual("FromAction", order.Status);
            }
        }
Exemplo n.º 6
0
        public async Task Test_EFDataProvider_AuditEntityAsyncAction_Map_8()
        {
            //MapTable<TAuditEntity>(string tableName, Func<EventEntry, TAuditEntity, Task> entityAsyncAction = null)

            Audit.Core.Configuration.Setup()
            .UseEntityFramework(config => config
                                .AuditTypeExplicitMapper(m => m
                                                         .MapTable <OrderAudit>("Order", async(ee, audEnt) =>
            {
                audEnt.AuditDate = DateTime.UtcNow;
                await Task.Delay(1000);
                audEnt.Status = "FromAction";
            }))
                                );

            var id = Guid.NewGuid().ToString();

            using (var ctx = new AuditPerTableContext())
            {
                var o = new Order()
                {
                    Number = id,
                    Status = "Pending"
                };
                ctx.Order.Add(o);
                await ctx.SaveChangesAsync();
            }

            using (var ctx = new AuditPerTableContext())
            {
                var order = await ctx.OrderAudit.AsQueryable().FirstOrDefaultAsync(o => o.Number == id);

                Assert.IsNotNull(order);
                Assert.AreEqual("FromAction", order.Status);
            }
        }
Exemplo n.º 7
0
        public async Task Test_EFDataProvider_Async()
        {
            var dp = new EntityFrameworkDataProvider();

            dp.AuditTypeMapper = t =>
            {
                if (t == typeof(Order))
                {
                    return(typeof(OrderAudit));
                }
                if (t == typeof(Orderline))
                {
                    return(typeof(OrderlineAudit));
                }
                return(null);
            };
            dp.AuditEntityAction = (ev, entry, obj) =>
            {
                var ab = obj as AuditBase;
                if (ab != null)
                {
                    ab.AuditDate   = DateTime.UtcNow;
                    ab.UserName    = ev.Environment.UserName;
                    ab.AuditStatus = entry.Action;
                }
                return(true);
            };

            Audit.Core.Configuration.Setup()
            .UseCustomProvider(dp);
            var id = Guid.NewGuid().ToString();

            using (var ctx = new AuditPerTableContext())
            {
                var o = new Order()
                {
                    Number     = id,
                    Status     = "Pending",
                    OrderLines = new Collection <Orderline>()
                    {
                        new Orderline()
                        {
                            Product = "p1: " + id, Quantity = 2
                        },
                        new Orderline()
                        {
                            Product = "p2: " + id, Quantity = 3
                        }
                    }
                };
                await ctx.AddAsync(o);

                await ctx.SaveChangesAsync();
            }

            using (var ctx = new AuditPerTableContext())
            {
                var orderAudit = await ctx.OrderAudit.AsNoTracking().SingleOrDefaultAsync(a => a.Number.Equals(id));

                Assert.NotNull(orderAudit);
                var orderlineAudits = await ctx.OrderlineAudit.AsNoTracking().Where(a => a.OrderId.Equals(orderAudit.Id)).ToListAsync();

                Assert.AreEqual(2, orderlineAudits.Count);
                Assert.AreEqual("p1: " + id, orderlineAudits[0].Product);
                Assert.AreEqual("p2: " + id, orderlineAudits[1].Product);
                Assert.AreEqual("Insert", orderAudit.AuditStatus);
                Assert.AreEqual("Insert", orderlineAudits[0].AuditStatus);
                Assert.AreEqual("Insert", orderlineAudits[1].AuditStatus);
                Assert.AreEqual(orderlineAudits[0].UserName, orderlineAudits[1].UserName);
                Assert.AreEqual(orderlineAudits[0].UserName, orderAudit.UserName);
                Assert.IsFalse(string.IsNullOrWhiteSpace(orderlineAudits[0].UserName));
                Assert.IsFalse(string.IsNullOrWhiteSpace(orderlineAudits[1].UserName));
            }

            using (var ctx = new AuditPerTableContext())
            {
                var o = ctx.Order.Single(a => a.Number.Equals(id));
                o.Status = "Cancelled";
                await ctx.SaveChangesAsync();
            }

            using (var ctx = new AuditPerTableContext())
            {
                var orderAudits = await ctx.OrderAudit.AsNoTracking().Where(a => a.Number.Equals(id)).OrderByDescending(a => a.AuditDate).ToListAsync();

                Assert.AreEqual(2, orderAudits.Count);
                Assert.AreEqual("Update", orderAudits[0].AuditStatus);
                Assert.AreEqual("Cancelled", orderAudits[0].Status);
                Assert.AreEqual("Pending", orderAudits[1].Status);
                Assert.AreEqual("Insert", orderAudits[1].AuditStatus);
            }

            using (var ctx = new AuditPerTableContext())
            {
                var order = await ctx.Order.SingleAsync(a => a.Number.Equals(id));

                var ol = await ctx.Orderline.SingleAsync(a => a.OrderId.Equals(order.Id) && a.Product.StartsWith("p1"));

                ctx.Remove(ol);
                await ctx.SaveChangesAsync();
            }

            using (var ctx = new AuditPerTableContext())
            {
                var order = await ctx.Order.SingleAsync(a => a.Number.Equals(id));

                var orderlineAudits = await ctx.OrderlineAudit.AsNoTracking().Where(a => a.OrderId.Equals(order.Id)).OrderByDescending(a => a.AuditDate).ToListAsync();

                Assert.AreEqual(3, orderlineAudits.Count);
                Assert.AreEqual("Delete", orderlineAudits[0].AuditStatus);
                Assert.IsTrue(orderlineAudits[0].Product.StartsWith("p1"));
            }
        }
        public void Test_EFDataProvider()
        {
            Audit.Core.Configuration.Setup()
            .UseEntityFramework(x => x
                                .AuditTypeExplicitMapper(_ => _
                                                         .Map <Order, OrderAudit>((order, auditOrder) => { auditOrder.Status = "Order-" + order.Status; })
                                                         .Map <Orderline, OrderlineAudit>()
                                                         .AuditEntityAction <AuditBase>((ev, ent, auditEntity) =>
            {
                auditEntity.AuditDate   = DateTime.UtcNow;
                auditEntity.UserName    = ev.Environment.UserName;
                auditEntity.AuditStatus = ent.Action;
            })));

            var id = Guid.NewGuid().ToString();

            using (var ctx = new AuditPerTableContext())
            {
                var o = new Order()
                {
                    Number     = id,
                    Status     = "Pending",
                    OrderLines = new Collection <Orderline>()
                    {
                        new Orderline()
                        {
                            Product = "p1: " + id, Quantity = 2
                        },
                        new Orderline()
                        {
                            Product = "p2: " + id, Quantity = 3
                        }
                    }
                };
                ctx.Set(typeof(Order)).Add(o);
                ctx.SaveChanges();
            }

            using (var ctx = new AuditPerTableContext())
            {
                var order      = ctx.Order.Single(a => a.Number.Equals(id));
                var orderAudit = ctx.OrderAudit.AsNoTracking().SingleOrDefault(a => a.Id.Equals(order.Id));
                Assert.NotNull(orderAudit);
                var orderlineAudits = ctx.OrderlineAudit.AsNoTracking().Where(a => a.OrderId.Equals(order.Id)).ToList();
                Assert.AreEqual(2, orderlineAudits.Count);
                Assert.AreEqual("p1: " + id, orderlineAudits[0].Product);
                Assert.AreEqual("p2: " + id, orderlineAudits[1].Product);
                Assert.AreEqual("Insert", orderAudit.AuditStatus);
                Assert.AreEqual("Insert", orderlineAudits[0].AuditStatus);
                Assert.AreEqual("Insert", orderlineAudits[1].AuditStatus);
                Assert.AreEqual(orderlineAudits[0].UserName, orderlineAudits[1].UserName);
                Assert.AreEqual(orderlineAudits[0].UserName, orderAudit.UserName);
                Assert.IsFalse(string.IsNullOrWhiteSpace(orderlineAudits[0].UserName));
                Assert.IsFalse(string.IsNullOrWhiteSpace(orderlineAudits[1].UserName));
            }

            using (var ctx = new AuditPerTableContext())
            {
                var o = ctx.Order.Single(a => a.Number.Equals(id));
                o.Status = "Cancelled";
                ctx.SaveChanges();
            }

            using (var ctx = new AuditPerTableContext())
            {
                var orderAudits = ctx.OrderAudit.AsNoTracking().Where(a => a.Number.Equals(id)).OrderByDescending(a => a.AuditDate).ToList();
                Assert.AreEqual(2, orderAudits.Count);
                Assert.AreEqual("Update", orderAudits[0].AuditStatus);
                Assert.AreEqual("Order-Cancelled", orderAudits[0].Status);
                Assert.AreEqual("Order-Pending", orderAudits[1].Status);
                Assert.AreEqual("Insert", orderAudits[1].AuditStatus);
            }

            using (var ctx = new AuditPerTableContext())
            {
                var order = ctx.Order.Single(a => a.Number.Equals(id));
                var ol    = ctx.Orderline.Single(a => a.OrderId.Equals(order.Id) && a.Product.StartsWith("p1"));
                ctx.Orderline.Remove(ol);
                ctx.SaveChanges();
            }

            using (var ctx = new AuditPerTableContext())
            {
                var order           = ctx.Order.Single(a => a.Number.Equals(id));
                var orderlineAudits = ctx.OrderlineAudit.AsNoTracking().Where(a => a.OrderId.Equals(order.Id)).OrderByDescending(a => a.AuditDate).ToList();
                Assert.AreEqual(3, orderlineAudits.Count);
                Assert.AreEqual("Delete", orderlineAudits[0].AuditStatus);
                Assert.IsTrue(orderlineAudits[0].Product.StartsWith("p1"));
            }
        }