public void GivenJsonAndDevice_WhenCallSumValuesAndReturnEquality_ThenCommandResultIsTrue()
        {
            using (StreamReader r = new StreamReader(@".\TestAssets\command-processor-call-sumvaluesandreturnequality.json"))
            {
                string json = r.ReadToEnd();
                using (var device = CreateDevice())
                {
                    var devices = new List <object>();
                    devices.Add(device);

                    foreach (var commandResult in CommandProcessorUtils.Execute(devices, json))
                    {
                        Assert.IsNotNull(commandResult.Result);
                        Assert.IsTrue(commandResult.Result.GetType() == typeof(bool));
                        Assert.IsTrue((bool)commandResult.Result);

                        Assert.IsTrue(commandResult.Success);

                        //Only time we need to check this, no need in any other tests
                        Assert.IsTrue(commandResult.Function == "SumValuesAndReturnEquality");
                        Assert.IsTrue(commandResult.Description == "Example description.");
                    }
                }
            }
        }
 public void GivenDevicesIsNull_WhenRunCommandProcessor_ThenArgumentNullExceptionIsThrown()
 {
     using (StreamReader r = new StreamReader(@".\TestAssets\command-processor-call-1-function.json"))
     {
         string json = r.ReadToEnd();
         foreach (var result in CommandProcessorUtils.Execute(null, json))
         {
         }
     }
 }
        public void GivenJsonIsNull_WhenRunCommandProcessor_ThenArgumentNullExceptionIsThrown()
        {
            using (var device = CreateDevice())
            {
                var devices = new List <object>();
                devices.Add(device);

                foreach (var result in CommandProcessorUtils.Execute(devices, null))
                {
                }
            }
        }
        public void GivenJsonIsInvalid_WhenRunCommandProcessor_ThenArgumentExceptionIsThrown()
        {
            using (StreamReader r = new StreamReader(@".\TestAssets\command-processor-fail-validation.json"))
            {
                string json = r.ReadToEnd();
                using (var device = CreateDevice())
                {
                    var devices = new List <object>();
                    devices.Add(device);

                    foreach (var result in CommandProcessorUtils.Execute(devices, json))
                    {
                    }
                }
            }
        }
        private Task <MethodResponse> CommandProcessorExecute(MethodRequest methodRequest, object userContext)
        {
            var    jsonParsed   = JObject.Parse(methodRequest.DataAsJson);
            Guid   id           = (Guid)jsonParsed["Id"];
            string jsonCommands = (string)jsonParsed["Commands"];

            bool jsonValid = CommandProcessorUtils.Valid(jsonCommands);

            if (jsonValid)
            {
                //The potentially long running Execute function is run on its own thread
                Task.Run(() => Execute(_devices, jsonCommands, id));
            }

            //Retrun success to indicate the json was valid, but not that the commands have been processed
            return(methodRequest.GetMethodResponse(jsonValid));
        }
        public void GivenJsonAndDevice_WhenExecuteCommandWithExecuteAfter5Seconds_ThenExecutionTimeGreaterThan5Seconds()
        {
            using (StreamReader r = new StreamReader(@".\TestAssets\command-processor-execute-after.json"))
            {
                string json = r.ReadToEnd();
                using (var device = CreateDevice())
                {
                    var devices = new List <object>();
                    devices.Add(device);

                    foreach (var commandResult in CommandProcessorUtils.Execute(devices, json))
                    {
                        Assert.IsTrue(commandResult.ExecutionTime.TotalSeconds > 5);
                    }
                }
            }
        }
        private void Execute(IEnumerable <object> devices, string json, Guid id)
        {
            foreach (var commandResult in CommandProcessorUtils.Execute(devices, json, id))
            {
                if (_deviceClient != null)
                {
                    var serializeData  = JsonConvert.SerializeObject(commandResult);
                    var commandMessage = new Message(Encoding.ASCII.GetBytes(serializeData));
                    commandMessage.Properties.Add("user-id", id.ToString());
                    commandMessage.Properties.Add("user-success", commandResult.Success.ToString());
                    commandMessage.Properties.Add("user-command-count", commandResult.Count.ToString());
                    commandMessage.Properties.Add("user-command-index", commandResult.Index.ToString());
                    commandMessage.Properties.Add("user-error-message", commandResult.ErrorMessage);

                    _deviceClient.SendEventAsync(commandMessage).Wait();
                }
            }
        }
        public void GivenJsonAndDevice_WhenCallFunctionWithIncorrectlyNamedParameters_ThenSuccessIsFalesAndResultIsNull()
        {
            using (StreamReader r = new StreamReader(@".\TestAssets\command-processor-call-function-with-incorrectly-named-parameters.json"))
            {
                string json = r.ReadToEnd();
                using (var device = CreateDevice())
                {
                    var devices = new List <object>();
                    devices.Add(device);

                    foreach (var commandResult in CommandProcessorUtils.Execute(devices, json))
                    {
                        Assert.IsFalse(commandResult.Success);
                        Assert.IsNull(commandResult.Result);
                    }
                }
            }
        }
        public void GivenJsonAndDevice_WhenCallFunctionThatDoesNotExist_ThenSuccessIsFalesAndResultIsNull()
        {
            using (StreamReader r = new StreamReader(@".\TestAssets\command-processor-call-function-that-does-not-exist.json"))
            {
                string json = r.ReadToEnd();
                using (var device = CreateDevice())
                {
                    var devices = new List <object>();
                    devices.Add(device);

                    foreach (var commandResult in CommandProcessorUtils.Execute(devices, json))
                    {
                        Assert.IsFalse(commandResult.Success);
                        Assert.IsNull(commandResult.Result);
                    }
                }
            }
        }
        public void GivenJsonAndDevice_WhenCallFunctionWithEnumParameter_ThenCommandResultIsCorrect()
        {
            using (StreamReader r = new StreamReader(@".\TestAssets\command-processor-call-function-with-enum-parameter.json"))
            {
                string json = r.ReadToEnd();
                using (var device = CreateDevice())
                {
                    var devices = new List <object>();
                    devices.Add(device);

                    foreach (var commandResult in CommandProcessorUtils.Execute(devices, json))
                    {
                        Assert.IsTrue(commandResult.Success);
                        Assert.IsTrue((DummyDeviceSetting)commandResult.Result == DummyDeviceSetting.Setting2);
                    }
                }
            }
        }
        public void GivenCommandsWithNoDeviceIndexOrAssemblyWithDefaults_WhenExecuteCommands_ThenSuccessIsTrueResultIsNotNullAndDeviceIndexIs0()
        {
            using (StreamReader r = new StreamReader(@".\TestAssets\command-processor-default-device-index-and-assembly.json"))
            {
                string json = r.ReadToEnd();
                using (var device = CreateDevice())
                {
                    var devices = new List <object>();
                    devices.Add(device);

                    foreach (var commandResult in CommandProcessorUtils.Execute(devices, json))
                    {
                        Assert.IsTrue(commandResult.Success);
                        Assert.IsNotNull(commandResult.Result);
                        Assert.AreEqual(0, commandResult.DeviceIndex);
                    }
                }
            }
        }
        public void GivenJsonAndDevice_WhenCallGetSumValuesAndReturnAnswer_ThenCommandResultIsCorrect()
        {
            using (StreamReader r = new StreamReader(@".\TestAssets\command-processor-call-sumvaluesandreturnanswer.json"))
            {
                string json = r.ReadToEnd();
                using (var device = CreateDevice())
                {
                    var devices = new List <object>();
                    devices.Add(device);

                    foreach (var commandResult in CommandProcessorUtils.Execute(devices, json))
                    {
                        Assert.IsTrue(commandResult.Success);
                        Assert.IsNotNull(commandResult.Result);
                        Assert.IsTrue((int?)commandResult.Result == 42);
                    }
                }
            }
        }
        public void GivenJsonAndDevice_WhenCall2Functions_ThenFunctionCallCountIs2()
        {
            using (StreamReader r = new StreamReader(@".\TestAssets\command-processor-call-2-functions.json"))
            {
                string json = r.ReadToEnd();
                using (var device = CreateDevice())
                {
                    var devices = new List <object>();
                    devices.Add(device);

                    int functionCallCount = 0;
                    foreach (var commandResult in CommandProcessorUtils.Execute(devices, json))
                    {
                        functionCallCount++;
                    }

                    Assert.IsTrue(functionCallCount == 2);
                }
            }
        }
        public void GivenJsonAndDevice_WhenCallSetFunctionWithAPropertyTypeParameter_ThenCommandResultIsCorrect()
        {
            using (StreamReader r = new StreamReader(@".\TestAssets\command-processor-call-function-with-a-property-type-parameter.json"))
            {
                string json = r.ReadToEnd();
                using (var device = CreateDevice())
                {
                    var devices = new List <object>();
                    devices.Add(device);

                    foreach (var commandResult in CommandProcessorUtils.Execute(devices, json))
                    {
                        Assert.IsTrue(commandResult.Success);
                        Assert.IsNotNull(commandResult.Result);
                        Assert.IsTrue(commandResult.Result.GetType() == typeof(bool));
                        Assert.IsTrue((bool)commandResult.Result);
                    }
                }
            }
        }