public async Task GetAGatewayByExistingSerialNumber() { //Setting all var nameBD = Guid.NewGuid().ToString(); var context = BuildContext(nameBD); context.Gateways.Add(new Gateway() { SerialNumber = "gw-001", Name = "Gateway 1", IpAddress = "10.0.0.1" }); context.Gateways.Add(new Gateway() { SerialNumber = "gw-002", Name = "Gateway 2", IpAddress = "10.0.0.2" }); await context.SaveChangesAsync(); var testContext = BuildContext(nameBD); //Tests var serialNumber = "gw-001"; var controller = new GatewaysController(testContext); var response = await controller.Get(serialNumber); //Verification var gateway = response.Value as Gateway; //A Gateway object Assert.AreEqual(serialNumber, gateway.SerialNumber); }
//This mock is not actually being used as mocking the entire context is actually a lot harder than setting up and using //an in memory real one. Even though this goes into the realm of integration tests I am going to use it as a unit test. //private readonly Mock<Gateways.Models.GatewayContext> gatewayContextMock = new Mock<Gateways.Models.GatewayContext>(); public GatewaysControllerTests() { var options = new DbContextOptionsBuilder <GatewayContext>().UseInMemoryDatabase("Test Gateways").Options; gatewayContext = new GatewayContext(options); gatewaysController = new GatewaysController(gatewayContext); }
public async Task GetAGatewayByNonExistingSerialNumber() { //Setting all var nameBD = Guid.NewGuid().ToString(); var context = BuildContext(nameBD); context.Gateways.Add(new Gateway() { SerialNumber = "gw-001", Name = "Gateway 1", IpAddress = "10.0.0.1" }); context.Gateways.Add(new Gateway() { SerialNumber = "gw-002", Name = "Gateway 2", IpAddress = "10.0.0.2" }); await context.SaveChangesAsync(); var testContext = BuildContext(nameBD); //Tests var serialNumber = "gw-003"; var controller = new GatewaysController(testContext); var response = await controller.Get(serialNumber); //Verification var result = response.Result as StatusCodeResult; //Not Found Http Status Code Assert.AreEqual(404, result.StatusCode); }
public async Task GetAllGateways() { //Setting all var nameBD = Guid.NewGuid().ToString(); var context = BuildContext(nameBD); context.Gateways.Add(new Gateway() { SerialNumber = "gw-001", Name = "Gateway 1", IpAddress = "10.0.0.1" }); context.Gateways.Add(new Gateway() { SerialNumber = "gw-002", Name = "Gateway 2", IpAddress = "10.0.0.2" }); await context.SaveChangesAsync(); var testContext = BuildContext(nameBD); //Tests var controller = new GatewaysController(testContext); var response = await controller.Get(); //Verification var gateways = response.Value; //A total of 2 Gateways Assert.AreEqual(2, gateways.Count); }
public async Task AddAGatewayGoodIpAddress() { //Setting all var nameBD = Guid.NewGuid().ToString(); var context = BuildContext(nameBD); var testContext = BuildContext(nameBD); //Tests var gateway = new Gateway() { SerialNumber = "gw-004", Name = "Gateway 4", IpAddress = "10.0.0.4" }; var controller = new GatewaysController(context); var response = await controller.Post(gateway); //Verification var result = response.Result; Assert.IsNotNull(result); var countGateways = await testContext.Gateways.CountAsync(); Assert.AreEqual(1, countGateways); }
public void Setup() { var config = new MapperConfiguration(opts => { opts.AddProfile(new ModelToResourceProfile()); opts.AddProfile(new ResourceToModelProfile()); }); var _mapper = config.CreateMapper(); // Use this mapper to instantiate your class var optionsBuilder = new DbContextOptionsBuilder <AppDbContext>(); optionsBuilder.UseInMemoryDatabase("data-in-memory"); var _dbcontext = new AppDbContext(optionsBuilder.Options); var _deviceRepo = new DeviceRepo(_dbcontext); var _gatewayRepo = new GatewayRepo(_dbcontext); var _gatewayService = new GatewayService(_gatewayRepo); var _deviceSerivce = new DeviceService(_deviceRepo, _gatewayRepo); _deviceController = new DevicesController(_deviceSerivce, _mapper); _gatewayController = new GatewaysController(_gatewayService, _mapper); var g1 = new Gateway() { Id = Guid.NewGuid(), IPAddress = new System.Net.IPAddress(new byte[] { 192, 168, 0, 1 }), Name = "Gateway 1" }; var g2 = new Gateway() { Id = Guid.NewGuid(), IPAddress = new System.Net.IPAddress(new byte[] { 192, 168, 0, 2 }), Name = "Gateway 2" }; _dbcontext.Gateways.AddRange(g1, g2); var d11 = new Device() { Id = 101, Vendor = "Cisco", Status = DeviceStatus.OnLine, GatewayId = g1.Id }; var d12 = new Device() { Id = 102, Vendor = "Dell", Status = DeviceStatus.OffLine, GatewayId = g1.Id }; var d13 = new Device() { Id = 103, Vendor = "HP", Status = DeviceStatus.OffLine, GatewayId = g1.Id }; var d21 = new Device() { Id = 201, Vendor = "Huawei", Status = DeviceStatus.OffLine, GatewayId = g2.Id }; var d22 = new Device() { Id = 202, Vendor = "Fujitsu", Status = DeviceStatus.OnLine, GatewayId = g2.Id }; _dbcontext.Devices.AddRange(d11, d12, d13, d21, d22); _dbcontext.SaveChanges(); }
public async Task AddAGatewayBadIpAddress() { //Setting all var nameBD = Guid.NewGuid().ToString(); var context = BuildContext(nameBD); var testContext = BuildContext(nameBD); //Tests var gateway = new Gateway() { SerialNumber = "gw-004", Name = "Gateway 4", IpAddress = "0.0.0.255" }; var controller = new GatewaysController(context); var response = await controller.Post(gateway); //Verification var result = response.Result as BadRequestObjectResult; //Bad Request Http Status Code Assert.AreEqual(400, result.StatusCode); }
public void Dispose() { gatewaysController = null; gatewayContext.Dispose(); gatewayContext = null; }