public void GetMonitoredResult_ForEndpoint_ValidUser_Return10LastResults() { //ARANGE var fakeLoggerFactory = new NullLoggerFactory(); var options = new DbContextOptionsBuilder <DatabaseContext>(). UseInMemoryDatabase(DateTime.Now.ToString(Guid.NewGuid().ToString())).EnableDetailedErrors().Options; var owner = new Owner(); var fakeUsersList = FakeDataGenerator.GenerateUsers(2); owner.RegisterOwner(fakeUsersList[0]); var fakeMonitoredEndpointList = new List <MonitoredEndpoint>(); fakeMonitoredEndpointList.AddRange(FakeDataGenerator.GenerateEndpoint(1, fakeUsersList[0])); fakeMonitoredEndpointList.AddRange(FakeDataGenerator.GenerateEndpoint(1, fakeUsersList[1], 2)); var fakeMonitoringResultList = new List <MonitoringResult>(); var expectedCollection = new List <MonitoringResult>(); fakeMonitoringResultList.AddRange(FakeDataGenerator.GenerateMonitoringResult(11, fakeMonitoredEndpointList[0], fakeMonitoredEndpointList[0].Owner)); expectedCollection = fakeMonitoringResultList.TakeLast(10).ToList(); fakeMonitoringResultList.AddRange(FakeDataGenerator.GenerateMonitoringResult(5, fakeMonitoredEndpointList[1], fakeMonitoredEndpointList[1].Owner, 12)); using (var context = new DatabaseContext(options, fakeLoggerFactory)) { context.User.AddRange(fakeUsersList); context.MonitoredEndpoint.AddRange(fakeMonitoredEndpointList); context.MonitoringResult.AddRange(fakeMonitoringResultList); context.SaveChanges(); var fakeHttpContext = new Mock <HttpContext>(); var fakeHttpRequest = new Mock <HttpRequest>(); fakeHttpContext.SetupGet <HttpRequest>(x => x.Request).Returns(fakeHttpRequest.Object); var handlerContext = new ControllerContext() { HttpContext = fakeHttpContext.Object, }; //ACT var monitoredEndpointController = new MonitoredResultController(owner, context, fakeLoggerFactory); var controllerResult = monitoredEndpointController.GetMonitoringResultForEndopoint(fakeMonitoredEndpointList[0].Id).Result; //ASSERT Assert.NotNull(controllerResult); Assert.NotNull(controllerResult.Value); var resutlCollectionOrdered = controllerResult.Value.OrderBy(x => x.DateOfCheck).ToList(); Assert.Equal(expectedCollection, resutlCollectionOrdered); context.Database.EnsureDeleted(); } }
public void GetMonitoredEndpoint_ForId_InvalidUser_ReturnEmpty() { //ARANGE var fakeLoggerFactory = new NullLoggerFactory(); var options = new DbContextOptionsBuilder <DatabaseContext>(). UseInMemoryDatabase(Guid.NewGuid().ToString()).Options; var owner = new Owner(); var fakeUsersList = FakeDataGenerator.GenerateUsers(2); owner.RegisterOwner(fakeUsersList[1]); var fakeMonitoredEndpointList = new List <MonitoredEndpoint>(); fakeMonitoredEndpointList.AddRange(FakeDataGenerator.GenerateEndpoint(1, fakeUsersList[0])); fakeMonitoredEndpointList.AddRange(FakeDataGenerator.GenerateEndpoint(1, fakeUsersList[1], 2)); int idToGet = fakeMonitoredEndpointList.First(x => x.UserForeignKey != owner.Data.Id).Id; using (var context = new DatabaseContext(options, fakeLoggerFactory)) { context.User.AddRange(fakeUsersList); context.MonitoredEndpoint.AddRange(fakeMonitoredEndpointList); context.SaveChanges(); var fakeHttpContext = new Mock <HttpContext>(); var fakeHttpRequest = new Mock <HttpRequest>(); fakeHttpContext.SetupGet <HttpRequest>(x => x.Request).Returns(fakeHttpRequest.Object); var handlerContext = new ControllerContext() { HttpContext = fakeHttpContext.Object, }; //ACT var monitoredEndpointController = new MonitoredEndpointController(owner, context, fakeLoggerFactory); var controllerResult = monitoredEndpointController.GetMonitoredEndpoint(idToGet).Result; //ASSERT Assert.Null(controllerResult.Value);; context.Database.EnsureDeleted(); } }
public void GetMonitoredEndpoint_ForId_ValidUser_ReturnSameObject() { //ARANGE var fakeLoggerFactory = new NullLoggerFactory(); var options = new DbContextOptionsBuilder <DatabaseContext>(). UseInMemoryDatabase(Guid.NewGuid().ToString()).Options; var owner = new Owner(); var fakeUsersList = FakeDataGenerator.GenerateUsers(2); owner.RegisterOwner(fakeUsersList[0]); var fakeEndpoint1 = FakeDataGenerator.GenerateEndpoint(1, owner.Data).Single(); var fakeEndpoint2 = FakeDataGenerator.GenerateEndpoint(1, fakeUsersList[1], 2).Single(); using (var context = new DatabaseContext(options, fakeLoggerFactory)) { context.User.AddRange(fakeUsersList); context.Add(fakeEndpoint1); context.Add(fakeEndpoint2); context.SaveChanges(); var fakeHttpContext = new Mock <HttpContext>(); var fakeHttpRequest = new Mock <HttpRequest>(); fakeHttpContext.SetupGet <HttpRequest>(x => x.Request).Returns(fakeHttpRequest.Object); var handlerContext = new ControllerContext() { HttpContext = fakeHttpContext.Object, }; //ACT var monitoredEndpointController = new MonitoredEndpointController(owner, context, fakeLoggerFactory); var controllerResult = monitoredEndpointController.GetMonitoredEndpoint(1).Result; //ASSERT Assert.NotNull(controllerResult); Assert.NotNull(controllerResult.Value); Assert.IsType <MonitoredEndpoint>(controllerResult.Value); Assert.Equal(fakeEndpoint1, controllerResult.Value); context.Database.EnsureDeleted(); } }