using System; using Microsoft.VisualStudio.TestTools.UnitTesting; using Edge.Core.Processor; using Edge.Core.IndustryStandardInterface.Pump; using HengShan_Pump_NonIC.MessageEntity; using System.Threading; using Wayne.FDCPOSLibrary; using HengShan_Pump_NonIC; using static HengShan_Pump_NonIC.MessageEntity.GetNozzleStatusResponse; using Mocks; namespace Test_HengShan_Pump_NonIC { [TestClass] public class FdcPumpControllerUnitTest { private GenericDeviceProcessor processor; [Microsoft.VisualStudio.TestTools.UnitTesting.TestInitialize()] public void InitProcessor() { var mockCommunicator = new ComPortCommunicatorMock(); mockCommunicator.OnRawDataWriting += (s, a) => { }; this.processor = new GenericDeviceProcessor( new HengShan_Pump_NonIC.PumpHandler(1, "" + " " + " " + ""), mockCommunicator, null); } [TestMethod] public void Should_Send_Polling_Pump_Message_Test() { var mockCommunicator = this.processor.Communicator as ComPortCommunicatorMock; var fdcPumpController = processor.Context.Handler as IFdcPumpController; int pollingMessageSendTimes = 0; processor.Context.Outgoing.OnWriting += (s, a) => { if (a.Message is GetNozzleStatusRequest) pollingMessageSendTimes++; }; processor.Start(); /* fire anything, otherwise the polling will not start since Context is null*/ var statusResponse_Fuelling = new GetNozzleStatusResponse(); statusResponse_Fuelling.AddPumpStatus(PumpStatus.加油结束); mockCommunicator.FireOnDataReceived(statusResponse_Fuelling); // hardcode for now. Thread.Sleep(2000); Assert.AreEqual(true, pollingMessageSendTimes >= 1, "polling message should send more than 1 times"); } [TestMethod] public void IdleState_Test() { var mockCommunicator = this.processor.Communicator as ComPortCommunicatorMock; var fdcPumpController = processor.Context.Handler as IFdcPumpController; int onStateChangeCalledTimes = 0; FdcPumpControllerOnStateChangeEventArg onStateChangeEventArg = null; fdcPumpController.OnStateChange += (s, a) => { onStateChangeEventArg = a; onStateChangeCalledTimes++; }; /* fire */ var statusResponse_Fuelling = new GetNozzleStatusResponse(); statusResponse_Fuelling.AddPumpStatus(PumpStatus.加油结束); mockCommunicator.FireOnDataReceived(statusResponse_Fuelling); Assert.AreEqual(1, onStateChangeCalledTimes, "OnStateChange should be called just 1 time"); Assert.AreNotEqual(null, onStateChangeEventArg, "OnStateChange should be called."); Assert.AreEqual(LogicalDeviceState.FDC_READY, onStateChangeEventArg.NewPumpState, "OnStateChange should have the new state of FDC_READY"); Assert.AreEqual(LogicalDeviceState.FDC_READY, fdcPumpController.QueryStatusAsync().Result, "status should change to: FDC_READY, but now is: " + fdcPumpController.QueryStatusAsync().Result); //Thread.Sleep(100000); } [TestMethod] public void FuellingState_Test() { var mockCommunicator = this.processor.Communicator as ComPortCommunicatorMock; IFdcPumpController fdcPumpController = processor.Context.Handler as IFdcPumpController; int onStateChangeCalledTimes = 0; FdcPumpControllerOnStateChangeEventArg onStateChangeEventArg = null; fdcPumpController.OnStateChange += (s, a) => { onStateChangeEventArg = a; onStateChangeCalledTimes++; }; /* fire */ var statusResponse_Fuelling = new GetNozzleStatusResponse(); statusResponse_Fuelling.AddPumpStatus(PumpStatus.允许加油); statusResponse_Fuelling.AddPumpStatus(PumpStatus.油枪打开); statusResponse_Fuelling.AddPumpStatus(PumpStatus.加油过程); mockCommunicator.FireOnDataReceived(statusResponse_Fuelling); Assert.AreEqual(1, onStateChangeCalledTimes, "OnStateChange should be called just 1 time"); Assert.AreNotEqual(null, onStateChangeEventArg, "OnStateChange should be called."); Assert.AreEqual(LogicalDeviceState.FDC_FUELLING, onStateChangeEventArg.NewPumpState, "OnStateChange should have the new state of FDC_FUELLING"); Assert.AreEqual(LogicalDeviceState.FDC_FUELLING, fdcPumpController.QueryStatusAsync().Result, "status should change to: FDC_FUELLING, but now is: " + fdcPumpController.QueryStatusAsync().Result); //Thread.Sleep(100000); } [TestMethod] public void FuellingState_Test1() { var mockCommunicator = this.processor.Communicator as ComPortCommunicatorMock; IFdcPumpController fdcPumpController = processor.Context.Handler as IFdcPumpController; int onStateChangeCalledTimes = 0; FdcPumpControllerOnStateChangeEventArg onStateChangeEventArg = null; fdcPumpController.OnStateChange += (s, a) => { onStateChangeEventArg = a; onStateChangeCalledTimes++; }; /* fire multiple times, stateChange should only fired one time*/ var statusResponse_Fuelling = new GetNozzleStatusResponse(); statusResponse_Fuelling.AddPumpStatus(PumpStatus.允许加油); statusResponse_Fuelling.AddPumpStatus(PumpStatus.油枪打开); statusResponse_Fuelling.AddPumpStatus(PumpStatus.加油过程); mockCommunicator.FireOnDataReceived(statusResponse_Fuelling); mockCommunicator.FireOnDataReceived(statusResponse_Fuelling); mockCommunicator.FireOnDataReceived(statusResponse_Fuelling); mockCommunicator.FireOnDataReceived(statusResponse_Fuelling); Assert.AreEqual(1, onStateChangeCalledTimes, "OnStateChange should be called just 1 time"); Assert.AreNotEqual(null, onStateChangeEventArg, "OnStateChange should be called."); Assert.AreEqual(LogicalDeviceState.FDC_FUELLING, onStateChangeEventArg.NewPumpState, "OnStateChange should have the new state of FDC_FUELLING"); Assert.AreEqual(LogicalDeviceState.FDC_FUELLING, fdcPumpController.QueryStatusAsync().Result, "status should change to: FDC_FUELLING, but now is: " + fdcPumpController.QueryStatusAsync().Result); //Thread.Sleep(100000); } [TestMethod] public void FuellingState_Test2() { var mockCommunicator = this.processor.Communicator as ComPortCommunicatorMock; IFdcPumpController fdcPumpController = processor.Context.Handler as IFdcPumpController; int onStateChangeCalledTimes = 0; FdcPumpControllerOnStateChangeEventArg onStateChangeEventArg = null; fdcPumpController.OnStateChange += (s, a) => { onStateChangeEventArg = a; onStateChangeCalledTimes++; }; /* fire multiple times, stateChange should only fired one time*/ var statusResponse_Fuelling = new GetNozzleStatusResponse(); statusResponse_Fuelling.AddPumpStatus(PumpStatus.允许加油); statusResponse_Fuelling.AddPumpStatus(PumpStatus.油枪打开); statusResponse_Fuelling.AddPumpStatus(PumpStatus.加油过程); mockCommunicator.FireOnDataReceived(statusResponse_Fuelling); mockCommunicator.FireOnDataReceived(statusResponse_Fuelling); mockCommunicator.FireOnDataReceived(statusResponse_Fuelling); mockCommunicator.FireOnDataReceived(statusResponse_Fuelling); Assert.AreEqual(1, onStateChangeCalledTimes, "OnStateChange should be called just 1 time"); Assert.AreNotEqual(null, onStateChangeEventArg, "OnStateChange should be called."); Assert.AreEqual(LogicalDeviceState.FDC_FUELLING, onStateChangeEventArg.NewPumpState, "OnStateChange should have the new state of FDC_FUELLING"); //Thread.Sleep(PumpHandler.lastLogicalDeviceStateExpiredTime * 1000 + 1000); Assert.AreEqual(LogicalDeviceState.FDC_OFFLINE, fdcPumpController.QueryStatusAsync().Result, "status should change to: FDC_OFFLINE since many time passed, but now is: " + fdcPumpController.QueryStatusAsync().Result); } [TestMethod] public void FuellingState_Test3() { var mockCommunicator = this.processor.Communicator as ComPortCommunicatorMock; IFdcPumpController fdcPumpController = processor.Context.Handler as IFdcPumpController; int onStateChangeCalledTimes = 0; FdcPumpControllerOnStateChangeEventArg onStateChangeEventArg = null; fdcPumpController.OnStateChange += (s, a) => { onStateChangeEventArg = a; onStateChangeCalledTimes++; }; /* fire multiple times, stateChange should only fired one time*/ var statusResponse_Fuelling = new GetNozzleStatusResponse(); statusResponse_Fuelling.AddPumpStatus(PumpStatus.允许加油); statusResponse_Fuelling.AddPumpStatus(PumpStatus.油枪打开); statusResponse_Fuelling.AddPumpStatus(PumpStatus.加油过程); mockCommunicator.FireOnDataReceived(statusResponse_Fuelling); mockCommunicator.FireOnDataReceived(statusResponse_Fuelling); mockCommunicator.FireOnDataReceived(statusResponse_Fuelling); mockCommunicator.FireOnDataReceived(statusResponse_Fuelling); Assert.AreEqual(1, onStateChangeCalledTimes, "OnStateChange should be called just 1 time"); Assert.AreNotEqual(null, onStateChangeEventArg, "OnStateChange should be called."); Assert.AreEqual(LogicalDeviceState.FDC_FUELLING, onStateChangeEventArg.NewPumpState, "OnStateChange should have the new state of FDC_FUELLING"); //Thread.Sleep(PumpHandler.lastLogicalDeviceStateExpiredTime * 1000 + 1000); Assert.AreEqual(LogicalDeviceState.FDC_OFFLINE, fdcPumpController.QueryStatusAsync().Result, "status should change to: FDC_OFFLINE since many time passed, but now is: " + fdcPumpController.QueryStatusAsync().Result); mockCommunicator.FireOnDataReceived(statusResponse_Fuelling); Assert.AreEqual(LogicalDeviceState.FDC_FUELLING, fdcPumpController.QueryStatusAsync().Result, "status should change to: FDC_FUELLING , but now is: " + fdcPumpController.QueryStatusAsync().Result); } [TestMethod] public void PayableTransaction_State_Test1() { int fuelingAmount = 344; var mockCommunicator = this.processor.Communicator as ComPortCommunicatorMock; IFdcPumpController fdcPumpController = processor.Context.Handler as IFdcPumpController; int onStateChangeCalledTimes = 0; FdcPumpControllerOnStateChangeEventArg onStateChangeEventArg = null; fdcPumpController.OnStateChange += (s, a) => { onStateChangeEventArg = a; onStateChangeCalledTimes++; }; int onCurrentFuellingStatusChangeCalledTimes = 0; FdcTransactionDoneEventArg onCurrentFuellingStatusChangeEventArg = null; fdcPumpController.OnCurrentFuellingStatusChange += (s, a) => { onCurrentFuellingStatusChangeEventArg = a; onCurrentFuellingStatusChangeCalledTimes++; }; /* fire multiple times, stateChange should only fired one time*/ var statusResponse_Fuelling = new GetNozzleStatusResponse(); statusResponse_Fuelling.加油金额 = fuelingAmount; statusResponse_Fuelling.AddPumpStatus(PumpStatus.加油结束); mockCommunicator.FireOnDataReceived(statusResponse_Fuelling); Assert.AreEqual(1, onStateChangeCalledTimes, "OnStateChange should be called just 1 time"); Assert.AreNotEqual(null, onStateChangeEventArg, "OnStateChange should be called."); Assert.AreEqual(LogicalDeviceState.FDC_READY, onStateChangeEventArg.NewPumpState, "OnStateChange should have the new state of FDC_READY"); Assert.AreEqual(LogicalDeviceState.FDC_READY, fdcPumpController.QueryStatusAsync().Result, "status should change to: FDC_READY, but now is: " + fdcPumpController.QueryStatusAsync().Result); Assert.AreEqual(1, onCurrentFuellingStatusChangeCalledTimes, "OnCurrentFuellingStatusChange should be called just 1 time"); Assert.AreNotEqual(null, onCurrentFuellingStatusChangeEventArg, "OnCurrentFuellingStatusChange should be called."); Assert.AreEqual(true, onCurrentFuellingStatusChangeEventArg.Transaction.Finished, "Transaction should Finished."); Assert.AreEqual(fuelingAmount, onCurrentFuellingStatusChangeEventArg.Transaction.Amount, "Transaction amount should match."); } [TestMethod] public void PayableTransaction_State_Test2() { int fuelingAmount = 344; var mockCommunicator = this.processor.Communicator as ComPortCommunicatorMock; IFdcPumpController fdcPumpController = processor.Context.Handler as IFdcPumpController; int onStateChangeCalledTimes = 0; FdcPumpControllerOnStateChangeEventArg onStateChangeEventArg = null; fdcPumpController.OnStateChange += (s, a) => { onStateChangeEventArg = a; onStateChangeCalledTimes++; }; int onCurrentFuellingStatusChangeCalledTimes = 0; FdcTransactionDoneEventArg onCurrentFuellingStatusChangeEventArg = null; fdcPumpController.OnCurrentFuellingStatusChange += (s, a) => { onCurrentFuellingStatusChangeEventArg = a; onCurrentFuellingStatusChangeCalledTimes++; }; /* fire multiple times, stateChange should only fired one time*/ var statusResponse_Fuelling = new GetNozzleStatusResponse(); statusResponse_Fuelling.加油金额 = fuelingAmount; statusResponse_Fuelling.AddPumpStatus(PumpStatus.加油结束); mockCommunicator.FireOnDataReceived(statusResponse_Fuelling); Assert.AreEqual(1, onStateChangeCalledTimes, "OnStateChange should be called just 1 time"); Assert.AreNotEqual(null, onStateChangeEventArg, "OnStateChange should be called."); Assert.AreEqual(LogicalDeviceState.FDC_READY, onStateChangeEventArg.NewPumpState, "OnStateChange should have the new state of FDC_READY"); Assert.AreEqual(LogicalDeviceState.FDC_READY, fdcPumpController.QueryStatusAsync().Result, "status should change to: FDC_READY, but now is: " + fdcPumpController.QueryStatusAsync().Result); Assert.AreEqual(1, onCurrentFuellingStatusChangeCalledTimes, "OnCurrentFuellingStatusChange should be called just 1 time"); Assert.AreNotEqual(null, onCurrentFuellingStatusChangeEventArg, "OnCurrentFuellingStatusChange should be called."); Assert.AreEqual(true, onCurrentFuellingStatusChangeEventArg.Transaction.Finished, "Transaction should Finished."); Assert.AreEqual(fuelingAmount, onCurrentFuellingStatusChangeEventArg.Transaction.Amount, "Transaction amount should match."); mockCommunicator.FireOnDataReceived(statusResponse_Fuelling); mockCommunicator.FireOnDataReceived(statusResponse_Fuelling); mockCommunicator.FireOnDataReceived(statusResponse_Fuelling); Assert.AreEqual(1, onStateChangeCalledTimes, "OnStateChange should be called just 1 time"); Assert.AreEqual(LogicalDeviceState.FDC_READY, fdcPumpController.QueryStatusAsync().Result, "status should change to: FDC_READY, but now is: " + fdcPumpController.QueryStatusAsync().Result); Assert.AreEqual(1, onCurrentFuellingStatusChangeCalledTimes, "OnCurrentFuellingStatusChange should be called just 1 time"); } [TestMethod] public void Authorize_State_Test1() { var mockCommunicator = this.processor.Communicator as ComPortCommunicatorMock; IFdcPumpController fdcPumpController = processor.Context.Handler as IFdcPumpController; int onStateChangeCalledTimes = 0; FdcPumpControllerOnStateChangeEventArg onStateChangeEventArg = null; fdcPumpController.OnStateChange += (s, a) => { Console.WriteLine("OnStateChange to: " + a.NewPumpState); onStateChangeEventArg = a; onStateChangeCalledTimes++; }; /* set to idle (FDC_Ready) */ var statusResponse_Fuelling = new GetNozzleStatusResponse(); statusResponse_Fuelling.AddPumpStatus(PumpStatus.加油结束); mockCommunicator.FireOnDataReceived(statusResponse_Fuelling); Assert.AreEqual(1, onStateChangeCalledTimes, "OnStateChange should be called"); Assert.AreNotEqual(null, onStateChangeEventArg, "OnStateChange should be called"); ThreadPool.QueueUserWorkItem(o => { Thread.Sleep(1000); /* fire StartResponse */ var startResponse = new StartResponse(); startResponse.EnumResult = NonICMessageTemplateBase.Result.成功; mockCommunicator.FireOnDataReceived(startResponse); }); var result = fdcPumpController.AuthorizeAsync(1).Result; Assert.AreEqual(true, result, "Authorize should return succeed"); Assert.AreEqual(2, onStateChangeCalledTimes, "OnStateChange should be called just 2 time"); Assert.AreNotEqual(null, onStateChangeEventArg, "OnStateChange should be called."); Assert.AreEqual(LogicalDeviceState.FDC_AUTHORISED, onStateChangeEventArg.NewPumpState, "OnStateChange should have the new state of FDC_AUTHORISED"); Assert.AreEqual(LogicalDeviceState.FDC_AUTHORISED, fdcPumpController.QueryStatusAsync().Result, "status should change to: FDC_AUTHORISED, but now is: " + fdcPumpController.QueryStatusAsync().Result); } [TestMethod] public void Authorize_With_Amount_Test() { var mockCommunicator = this.processor.Communicator as ComPortCommunicatorMock; IFdcPumpController fdcPumpController = processor.Context.Handler as IFdcPumpController; int onStateChangeCalledTimes = 0; FdcPumpControllerOnStateChangeEventArg onStateChangeEventArg = null; fdcPumpController.OnStateChange += (s, a) => { onStateChangeEventArg = a; onStateChangeCalledTimes++; }; /* set to idle (FDC_Ready) */ var statusResponse_Fuelling = new GetNozzleStatusResponse(); statusResponse_Fuelling.AddPumpStatus(PumpStatus.加油结束); mockCommunicator.FireOnDataReceived(statusResponse_Fuelling); Assert.AreEqual(1, onStateChangeCalledTimes, "OnStateChange should be called"); Assert.AreNotEqual(null, onStateChangeEventArg, "OnStateChange should be called"); ThreadPool.QueueUserWorkItem(o => { Thread.Sleep(1000); /* fire AuthPumpWithAmountResponse */ var authPumpWithAmountResponse = new AuthPumpWithAmountResponse(); authPumpWithAmountResponse.EnumResult = NonICMessageTemplateBase.Result.成功; mockCommunicator.FireOnDataReceived(authPumpWithAmountResponse); Thread.Sleep(200); /* fire StartResponse */ var startResponse = new StartResponse(); startResponse.EnumResult = NonICMessageTemplateBase.Result.成功; mockCommunicator.FireOnDataReceived(startResponse); }); var result = fdcPumpController.AuthorizeWithAmountAsync(1983, 0).Result; // wait StartResponse Thread.Sleep(1000); Assert.AreEqual(true, result, "Authorize should return succeed"); Assert.AreEqual(2, onStateChangeCalledTimes, "OnStateChange should be called 2 times"); Assert.AreNotEqual(null, onStateChangeEventArg, "OnStateChange should be called."); Assert.AreEqual(LogicalDeviceState.FDC_AUTHORISED, onStateChangeEventArg.NewPumpState, "OnStateChange should have the new state of FDC_AUTHORISED"); Assert.AreEqual(LogicalDeviceState.FDC_AUTHORISED, fdcPumpController.QueryStatusAsync().Result, "status should change to: FDC_AUTHORISED, but now is: " + fdcPumpController.QueryStatusAsync().Result); } [TestMethod] public void Authorize_With_Vol_Test() { var mockCommunicator = this.processor.Communicator as ComPortCommunicatorMock; IFdcPumpController fdcPumpController = processor.Context.Handler as IFdcPumpController; int onStateChangeCalledTimes = 0; FdcPumpControllerOnStateChangeEventArg onStateChangeEventArg = null; fdcPumpController.OnStateChange += (s, a) => { onStateChangeEventArg = a; onStateChangeCalledTimes++; }; /* set to idle (FDC_Ready) */ var statusResponse_Fuelling = new GetNozzleStatusResponse(); statusResponse_Fuelling.AddPumpStatus(PumpStatus.加油结束); mockCommunicator.FireOnDataReceived(statusResponse_Fuelling); Assert.AreEqual(1, onStateChangeCalledTimes, "OnStateChange should be called"); Assert.AreNotEqual(null, onStateChangeEventArg, "OnStateChange should be called"); ThreadPool.QueueUserWorkItem(o => { Thread.Sleep(500); /* fire AuthPumpWithAmountResponse */ var authPumpWithGalResponse = new AuthPumpWithGallonResponse(); authPumpWithGalResponse.EnumResult = NonICMessageTemplateBase.Result.成功; mockCommunicator.FireOnDataReceived(authPumpWithGalResponse); Thread.Sleep(1000); /* fire StartResponse */ var startResponse = new StartResponse(); startResponse.EnumResult = NonICMessageTemplateBase.Result.成功; mockCommunicator.FireOnDataReceived(startResponse); }); var result = fdcPumpController.AuthorizeWithVolumeAsync(1983, 0).Result; // wait StartResponse Thread.Sleep(1000); Assert.AreEqual(true, result, "Authorize should return succeed"); Assert.AreEqual(2, onStateChangeCalledTimes, "OnStateChange should be called 2 times"); Assert.AreNotEqual(null, onStateChangeEventArg, "OnStateChange should be called."); Assert.AreEqual(LogicalDeviceState.FDC_AUTHORISED, onStateChangeEventArg.NewPumpState, "OnStateChange should have the new state of FDC_AUTHORISED"); Assert.AreEqual(LogicalDeviceState.FDC_AUTHORISED, fdcPumpController.QueryStatusAsync().Result, "status should change to: FDC_AUTHORISED, but now is: " + fdcPumpController.QueryStatusAsync().Result); } } }