public async void FunctionShouldReturnOkResult() { //Given var body = new SubscribeToMachineModel() { MachineId = "machine-id-1", WatchId = "watch-id-1" }; var req = new HttpRequestBuilder().Body(body).Build(); var watchService = new Mock <IWatchService>(); var machineService = new Mock <IMachineService>(); var machine = new AlarmSystem.Core.Entity.DB.Machine() { MachineId = "machine-id-1" }; watchService.Setup(ws => ws.SubscribeToMachine(It.IsAny <MachineWatch>())); machineService.Setup(ms => ms.GetMachineById(It.IsAny <string>())).Returns(machine); //When var res = (OkResult)await new SubscribeToMachine(watchService.Object, machineService.Object).Run(req, logger); //Then Assert.IsType <OkResult>(res); }
public async Task TestFunctionShouldReturnOkResultAsync() { //Given var alarmService = new Mock <IAlarmService>(); var watchService = new Mock <IWatchService>(); var machineService = new Mock <IMachineService>(); var alarmLogService = new Mock <IAlarmLogService>(); var notificationConnectionSetting = new Mock <INotificationHubConnectionSettings>(); var notificationHub = new Mock <INotificationHubClient>(); var body = new { MachinasdfeId = "test-id-1", AlarmCode = 42 }; var machine = new AlarmSystem.Core.Entity.DB.Machine { MachineId = "machine-id-test", Type = "machine-type-test", Name = "machine-name-test" }; var alarm = new AlarmSystem.Core.Entity.DB.Alarm { AlarmId = 1, Code = 1, Description = "alarm-description-test" }; var req = new HttpRequestBuilder().Body(body).Build(); var alarmSubs = new List <AlarmWatch>() { new AlarmWatch { Alarm = new AlarmSystem.Core.Entity.DB.Alarm { Code = 42, Description = "alarm-desc" }, WatchId = "watch-test-id-1" } }; var machineSubs = new List <MachineWatch>() { new MachineWatch { Machine = new Core.Entity.DB.Machine { MachineId = "test-id-1" }, WatchId = "watch-test-id-1" } }; //When alarmService.Setup(als => als.GetAlarmByCode(It.IsAny <int>())).Returns(alarm); machineService.Setup(ms => ms.GetMachineById(It.IsAny <string>())).Returns(machine); watchService.Setup(ws => ws.GetAlarmSubscriptionsByAlarmCode(It.IsAny <int>())).Returns(alarmSubs); watchService.Setup(ws => ws.GetMachineSubscriptionsByMachine(It.IsAny <string>())).Returns(machineSubs); notificationHub.Setup(nh => nh.SendDirectNotificationAsync(It.IsAny <Notification>(), It.IsAny <string>())); notificationConnectionSetting.Setup(ncs => ncs.Hub).Returns(notificationHub.Object); var res = (OkResult)await new SendAlert(watchService.Object, alarmService.Object, machineService.Object, alarmLogService.Object, notificationConnectionSetting.Object).Run(req, logger); //Then Assert.IsType <OkResult>(res); }
private MachineWatch ParseFunctionModelToDtoModel(SubscribeToMachineModel stmm) { AlarmSystem.Core.Entity.DB.Machine machine = _machineService.GetMachineById(stmm.MachineId); MachineWatch mw = new MachineWatch() { Machine = machine, WatchId = stmm.WatchId }; return(mw); }
private AlarmSystem.Core.Entity.DB.AlarmLog CreateAlarmLog(SendAlertModel sam) { AlarmSystem.Core.Entity.DB.Alarm alarm = _alarmService.GetAlarmByCode(sam.AlarmCode); AlarmSystem.Core.Entity.DB.Machine machine = _machineService.GetMachineById(sam.MachineId); var date = DateTime.UtcNow; long epochOfNow = new DateTimeOffset(date).ToUnixTimeMilliseconds(); AlarmSystem.Core.Entity.DB.AlarmLog al = new AlarmSystem.Core.Entity.DB.AlarmLog() { Alarm = alarm, Machine = machine, Date = epochOfNow }; _alarmLogService.CreateAlarmLog(al); return(al); }
public void MethodShouldCallCreateMachineMethodFromRepoOnce() { //Given var machineRepo = new Mock <IMachineRepository>(); var machine = new AlarmSystem.Core.Entity.DB.Machine { MachineId = "machine-id-test", Name = "machine-name-test", Type = "machine-type-test" }; //When machineRepo.Setup(mr => mr.CreateMachine(It.IsAny <AlarmSystem.Core.Entity.DB.Machine>())); var machineService = new MachineService(machineRepo.Object); machineService.CreateMachine(machine); //Then machineRepo.Verify(mr => mr.CreateMachine(It.IsAny <AlarmSystem.Core.Entity.DB.Machine>()), Times.Once); }
public async Task TestFunctionShouldReturnErrorMessageIfExceptionIsThrown1Async() { //Given var alarmService = new Mock <IAlarmService>(); var watchService = new Mock <IWatchService>(); var machineService = new Mock <IMachineService>(); var alarmLogService = new Mock <IAlarmLogService>(); var notificationConnectionSetting = new Mock <INotificationHubConnectionSettings>(); var notificationHub = new Mock <INotificationHubClient>(); var body = new { MachineId = "test-id-1", AlarmCode = 42 }; var machine = new AlarmSystem.Core.Entity.DB.Machine { MachineId = "machine-id-test", Type = "machine-type-test", Name = "machine-name-test" }; var alarm = new AlarmSystem.Core.Entity.DB.Alarm { AlarmId = 1, Code = 1, Description = "alarm-description-test" }; var req = new HttpRequestBuilder().Body(body).Build(); var alarmSubs = new List <AlarmWatch>(); //When alarmService.Setup(als => als.GetAlarmByCode(It.IsAny <int>())).Returns(alarm); machineService.Setup(ms => ms.GetMachineById(It.IsAny <string>())).Returns(machine); watchService.Setup(ws => ws.GetAlarmSubscriptionsByAlarmCode(It.IsAny <int>())).Returns(alarmSubs); watchService.Setup(ws => ws.GetMachineSubscriptionsByMachine(It.IsAny <string>())).Throws <InvalidDataException>(); notificationHub.Setup(nh => nh.SendDirectNotificationAsync(It.IsAny <Notification>(), It.IsAny <string>())); notificationConnectionSetting.Setup(ncs => ncs.Hub).Returns(notificationHub.Object); var res = (BadRequestObjectResult)await new SendAlert(watchService.Object, alarmService.Object, machineService.Object, alarmLogService.Object, notificationConnectionSetting.Object).Run(req, logger); //Then Assert.False(String.IsNullOrEmpty((string)res.Value)); }
public void MethodShouldNotCallMachineRepoAndThrowExceptionIfDataIsInvalid(string name, string type) { //Given var machineRepo = new Mock <IMachineRepository>(); var machine = new AlarmSystem.Core.Entity.DB.Machine { MachineId = "machine-id-test", Name = name, Type = type }; //When machineRepo.Setup(mr => mr.CreateMachine(It.IsAny <AlarmSystem.Core.Entity.DB.Machine>())); var machineService = new MachineService(machineRepo.Object); //Then Assert.Throws <InvalidDataException>(() => machineService.CreateMachine(machine)); machineRepo.Verify(mr => mr.CreateMachine(It.IsAny <AlarmSystem.Core.Entity.DB.Machine>()), Times.Never); }