public void TestGateReaderTicketValidation()
        {
            FakeDeviceClient   fakeDeviceClient = new FakeDeviceClient();
            FakeEventScheduler fakeScheduler    = new FakeEventScheduler();

            deviceconfig.PercentOfWrongWay = 0; // make sure we don't have any wrong way swipes

            TestContext.WriteLine(string.Empty);
            TestContext.WriteLine(">> Testing the purchase ticket simulated event..");

            GateReaderDevice device = new GateReaderDevice(deviceconfig, fakeDeviceClient, fakeScheduler);

            device.InitializeAsync().Wait();

            // execute a validate ticket event and check the result, it should always be false
            Assert.IsFalse(fakeScheduler.EventList[0].EventDelegate());
            // that delegate should have sent one message to the cloud
            Assert.AreEqual(1, fakeDeviceClient.sendMessageLog.Count);

            // check the message sent to make sure its correct
            // create a sample request for comparison
            ValidateTicketRequest expectedRequest = new ValidateTicketRequest()
            {
                DeviceId      = deviceconfig.DeviceId,
                DeviceType    = deviceconfig.DeviceType,
                TransactionId = "fakeId",
                CreateTime    = System.DateTime.UtcNow,
                MethodName    = "ReceiveTicketValidationResponse"
            };
            // get request message into an object so we can compare it
            ValidateTicketRequest actualRequest = JsonConvert.DeserializeObject <ValidateTicketRequest>(fakeDeviceClient.sendMessageLog[0]);

            // compare properties to make sure they're valid.
            Assert.AreEqual(expectedRequest.DeviceId, actualRequest.DeviceId);
            Assert.AreEqual(expectedRequest.DeviceType, actualRequest.DeviceType);
            Assert.AreEqual(expectedRequest.MessageType, actualRequest.MessageType);
            Assert.AreEqual(expectedRequest.MethodName, actualRequest.MethodName);

            //
            /// test the CloudToEvent PurchaseResponse call we expect back
            //
            TestContext.WriteLine(string.Empty);
            TestContext.WriteLine(">> Testing the ticket successfully validated direct method..");

            // test the direct method itself
            ValidateTicketResponse approvePurchaseMethodkRequest = new ValidateTicketResponse()
            {
                IsApproved  = true,
                DeviceId    = expectedRequest.DeviceId,
                DeviceType  = expectedRequest.DeviceType,
                MessageType = expectedRequest.MessageType,
            };
            string requestString = JsonConvert.SerializeObject(approvePurchaseMethodkRequest);
            // execute the method
            MethodRequest  methodRequest = new MethodRequest(expectedRequest.MethodName, Encoding.UTF8.GetBytes(requestString));
            MethodResponse methodresult  = fakeDeviceClient.directMethods[0](methodRequest, null).Result;

            // check results
            Assert.AreEqual(200, methodresult.Status); // got back an ok
        }
        public void Run()
        {
            // 95% chance that ticket validation is successful is approved
            Random gen = new Random();
            bool   purchaseApproved = gen.Next(100) <= 95;

            // process each message
            ValidateTicketRequest ticketRequestMessage = JsonConvert.DeserializeObject <ValidateTicketRequest>(payloadMessage);

            try
            {
                string methodName    = ticketRequestMessage.MethodName;
                string deviceId      = ticketRequestMessage.DeviceId;
                string transactionId = ticketRequestMessage.TransactionId;

                var payload = new PurchaseTicketPayload()
                {
                    TransactionId = transactionId,
                    IsApproved    = purchaseApproved,
                    DeviceId      = ticketRequestMessage.DeviceId,
                    DeviceType    = ticketRequestMessage.DeviceType,
                    MessageType   = ticketRequestMessage.MessageType,
                };

                log.LogInformation($"Response Method: {methodName}");

                InvokeMethod(methodName, payload).GetAwaiter().GetResult();
            }
            catch (Exception ex)
            {
                log.LogError(ex.Message);
            }
        }
        /// <summary>
        /// This event is called by a timer to simulate a card/ticket being scanned. It checks the direction of the gate (in/out)
        /// And if the swipe is in the right direction will ask the "cloud" to validate the card and wait for that result.
        /// </summary>
        private bool SimulatedTicketSwipeOccured()
        {
            GateDirection randomDirection = this.Direction; // default to the 'right' direction

            // randomize if swipe was from 'right' direction, if so we need to validate the ticket
            Random gen = new Random();

            if (gen.Next(100) > (100 - this.deviceConfig.PercentOfWrongWay)) // if from 'wrong' direction
            {
                // get other direction
                randomDirection = (this.Direction == GateDirection.In ? GateDirection.Out : GateDirection.In);
            }
            bool needtovalidate = randomDirection == this.Direction; // don't restart timer

            // if the swipe was from the right direction, as for the ticket to be validated
            if (needtovalidate)
            {
                ValidateTicketRequest cloudRequest = new ValidateTicketRequest()
                {
                    DeviceId      = this.deviceId,
                    DeviceType    = this.deviceType,
                    TransactionId = Guid.NewGuid().ToString(),
                    CreateTime    = System.DateTime.UtcNow,
                    MethodName    = "ReceiveTicketValidationResponse" // must match callback method
                };

                var messageString = JsonConvert.SerializeObject(cloudRequest);
                SendMessageToCloud(messageString);

                Console.WriteLine("{0} > Sending message: {1}", DateTime.Now, messageString);
                Console.WriteLine();
            }
            // if the swipe was from the wrong direction, we ignore it
            else
            {
                Console.WriteLine();
                Console.WriteLine("Swipe from Wrong Direction, ignoring");
            }

            // if we asked for the ticket to be validated, return false so we don't trigger another swipe
            return(needtovalidate == false);
        }
        public void TicketValidationTest()
        {
            // sample payload to test
            ValidateTicketRequest testRequest = new ValidateTicketRequest()
            {
                DeviceId      = "testID",
                DeviceType    = DeviceType.GateReader,
                TransactionId = "fakeTransactionId",
                CreateTime    = System.DateTime.UtcNow,
                MethodName    = "ReceiveTicketValidationResponse"
            };
            string requestPayload = JsonConvert.SerializeObject(testRequest);

            TestContext.WriteLine(string.Empty);
            TestContext.WriteLine(">> Testing the ticket validation method invocation.");

            FakeInvokeDeviceMethod serviceClient = new FakeInvokeDeviceMethod();
            ValidateTicketAction   action        = new ValidateTicketAction(serviceClient, requestPayload, logger);

            action.Run();

            // Assert that serviceClient.invocations count is 1
            Assert.That(Equals(1, serviceClient.invocations.Count));

            TestContext.WriteLine(string.Empty);
            TestContext.WriteLine(">> Testing the ticket validation method response.");

            // get the result and deserialize is back into our response object
            ValidateTicketResponse actualResponse = JsonConvert.DeserializeObject <ValidateTicketResponse>(serviceClient.invocations[0].method.GetPayloadAsJson());

            // Assert that various response values against expected
            Assert.AreEqual(testRequest.DeviceId, actualResponse.DeviceId, "Device IDs do not match");
            Assert.AreEqual(testRequest.DeviceType, actualResponse.DeviceType, "Device Types do not match");
            Assert.AreEqual(testRequest.MessageType, actualResponse.MessageType, "Message Types do not match");
            Assert.AreEqual(testRequest.TransactionId, actualResponse.TransactionId, "Trahsaction IDs do not match");
            Assert.AreEqual(testRequest.DeviceId, actualResponse.DeviceId, "Device IDs do not match");
            Assert.IsTrue((actualResponse.IsApproved == true || actualResponse.IsApproved == false), "IsApproved need not match range of allowed values, must be true or false");
            // Assert that the return MethodName is also correct
            Assert.AreEqual(testRequest.MethodName, serviceClient.invocations[0].method.MethodName);
        }