public void Invoke_MissingInputs_ReturnsExpectedMissingInputs( string text, ICollection <ArmTemplateParameter> inputParameters, ICollection <ArmTemplateVariable> inputVariables, ICollection <MissingInput> expectedMissingInputs ) { var expression = new ArmTemplateExpression(text); var deployment = new MockArmDeployment() .MockInvoke("string", "value") .Object; var actual = expression.Invoke(deployment, ArmValueTypes.@string, inputParameters, inputVariables); Assert.IsType <List <MissingInput> >(actual); var missingInputs = actual as List <MissingInput>; Assert.Equal(expectedMissingInputs.Count, missingInputs.Count); Assert.All(expectedMissingInputs, e => { Assert.Contains(missingInputs, a => { return(a.Name == e.Name && a.InputType == e.InputType); }); }); }
public TemplateBuilder AddParameters(ArmTemplateExpression expression, ICollection<ArmTemplateParameter> inputParameters) { // Adding input variables to the template only if the expression references parameters(s) if (expression.ParameterNames.Any()) { MissingInputs.AddRange(CheckForMissingInputs(expression, inputParameters)); if (inputParameters == null || inputParameters.Count < 1) { return this; } JObject paramsObj = new JObject(); foreach (var inputParam in inputParameters) { paramsObj.Add( new JProperty(inputParam.Name, new JObject { { "type", JToken.FromObject(inputParam.Type) }, { "defaultValue", JToken.FromObject(inputParam.Value) } } ) ); } Template["parameters"] = paramsObj; } return this; }
public void AddVariables_ValidInputVariables_SetsExpectedTemplateVariables( ArmTemplateExpression expression, List <ArmTemplateVariable> inputVariables ) { var actual = new TemplateBuilder() .AddVariables(expression, inputVariables) .Template["variables"]; Assert.IsType <JObject>(actual); foreach (var v in inputVariables) { if (v.Value.GetType().FullName.Contains("AnonymousType")) { foreach (var property in v.Value.GetType().GetProperties()) { var expectedValueString = property.GetValue(v.Value).ToString(); var actualValueString = actual[v.Name][property.Name].ToString(); Assert.Equal(expectedValueString, actualValueString); } } else { Assert.Equal(actual[v.Name], v.Value); } } }
public void Constructor_ValidExpressions_SetsText(string text) { var expression = new ArmTemplateExpression(text); var actual = expression.Text; Assert.Equal(text, actual); }
public void Constructor_ValidExpressions_SetsVariablesAndParameters(string text, string[] expectedVariables, string[] expectedParameters) { var actual = new ArmTemplateExpression(text); Assert.Equal(text, actual.Text); Assert.Equal(expectedVariables, actual.VariableNames); Assert.Equal(expectedParameters, actual.ParameterNames); }
public void AddVariables_NullInputVariables_AddMissingInputsOfTypeVariable() { var expression = new ArmTemplateExpression(@"[createArray(1, variables('num2'), variables('num3'))]"); var sut = new TemplateBuilder(); sut.AddVariables(expression, null); Assert.Equal(2, sut.MissingInputs.Count()); Assert.All(sut.MissingInputs, m => m.InputType.Equals(InputTypes.Variable)); }
public void AddParameters_NullOrEmptyInputParameters_ThrowExpectedException() { var expression = new ArmTemplateExpression(@"[createArray(1, parameters('num2'), parameters('num3'))]"); var sut = new TemplateBuilder(); sut.AddParameters(expression, new List <ArmTemplateParameter>()); Assert.Equal(2, sut.MissingInputs.Count()); Assert.All(sut.MissingInputs, m => m.InputType.Equals(InputTypes.Parameter)); }
public void Invoke_NullDeployment_ThrowsArgumentNullException() { var expression = new ArmTemplateExpression(@"[not(true)]"); var expectedOutputType = ArmValueTypes.@bool; Action act = () => { expression.Invoke(null, expectedOutputType, null, null); }; var ex = Record.Exception(act); Assert.IsType <ArgumentNullException>(ex); }
public void AddExpression_SetsExpectedTemplateOutputTypeAndValue(string expressionText, ArmValueTypes expectedOutputType) { var expression = new ArmTemplateExpression(expressionText); var actual = new TemplateBuilder() .AddExpression(expression, expectedOutputType) .Template["outputs"]["expression"]; Assert.IsType <JObject>(actual); Assert.Equal(expectedOutputType.ToString(), actual["Type"].ToString()); Assert.Equal(expressionText, actual["Value"].ToString()); }
public void AddParameters_MissingInputParameters_ThrowExpectedException() { var expression = new ArmTemplateExpression(@"[createArray(1, parameters('num2'), parameters('num3'))]"); var inputParameters = new List <ArmTemplateParameter>() { new ArmTemplateParameter("num3", 3, "int") }; var sut = new TemplateBuilder(); sut.AddParameters(expression, inputParameters); Assert.Single(sut.MissingInputs); Assert.All(sut.MissingInputs, m => m.InputType.Equals(InputTypes.Parameter)); Assert.Contains(sut.MissingInputs, m => m.Name == "num2"); }
public void AddVariables_MissingInputVariables_ThrowExpectedException() { var expression = new ArmTemplateExpression(@"[createArray(1, variables('num2'), variables('num3'))]"); var inputVariables = new List <ArmTemplateVariable>() { new ArmTemplateVariable("NotUsed", 0), new ArmTemplateVariable("num3", 3) }; var sut = new TemplateBuilder(); sut.AddVariables(expression, inputVariables); Assert.Single(sut.MissingInputs); Assert.All(sut.MissingInputs, m => m.InputType.Equals(InputTypes.Variable)); Assert.Contains(sut.MissingInputs, m => m.Name == "num2"); }
public void Invoke_NoVariableOrParameter_ReturnsExpectedOutput( string text, ArmValueTypes expectedOutputType, string outputValue, string expectedoutputString ) { var expression = new ArmTemplateExpression(text); var deployment = new MockArmDeployment() .MockInvoke(expectedOutputType.ToString(), outputValue) .Object; var actual = expression.Invoke(deployment, expectedOutputType, null, null); Assert.Equal(expectedoutputString, actual.ToString()); }
public TemplateBuilder AddExpression(ArmTemplateExpression expression, ArmValueTypes expectedOutputType) { if (expression is null) { throw new ArgumentNullException(nameof(expression)); } JObject OutputObj = new JObject( new JProperty( "expression", new JObject( new JProperty("Type", expectedOutputType.ToString()), new JProperty("Value", expression.Text) ) ) ); Template["outputs"] = OutputObj; return this; }
public void Invoke_WithParameters_ReturnsExpectedOutput( string text, ICollection <ArmTemplateParameter> inputParameters, ICollection <ArmTemplateVariable> inputVariables, ArmValueTypes expectedOutputType, string outputValue, string expectedOutputString ) { var expression = new ArmTemplateExpression(text); var deployment = new MockArmDeployment() .MockInvoke(expectedOutputType.ToString(), outputValue) .Object; var actual = expression.Invoke(deployment, expectedOutputType, inputParameters, inputVariables); Assert.Equal(expectedOutputString, actual.ToString()); }
public TemplateBuilder AddVariables(ArmTemplateExpression expression, ICollection<ArmTemplateVariable> inputVariables) { // Adding input variables to the template only if the expression references variable(s) if (expression.VariableNames.Any()) { MissingInputs.AddRange(CheckForMissingInputs(expression, inputVariables)); if (inputVariables == null || inputVariables.Count < 1) { return this; } JObject variablesObj = new JObject(); foreach (var inputVar in inputVariables) { variablesObj.Add(new JProperty(inputVar.Name, JToken.FromObject(inputVar.Value))); } Template["variables"] = variablesObj; } return this; }
static void Main(string[] args) { var container = ContainerConfig.Configure(); using (var scope = container.BeginLifetimeScope()) { var app = scope.Resolve <IApplication>(); app.Init(); var expression = new ArmTemplateExpression(@"[parameters('obj').Property1]"); var deployment = new ArmDeployment(app.Client, app.AzureRegion); var inputVariables = new List <ArmTemplateVariable>() { new ArmTemplateVariable("num2", 7) }; var inputParams = new List <ArmTemplateParameter>() { new ArmTemplateParameter("obj", new { Property1 = "customString", Property2 = true }, "object") }; try { var result = expression.Invoke(deployment, ArmValueTypes.@string, inputParams, inputVariables); if (result is List <MissingInput> ) { var missingInputs = result as List <MissingInput>; missingInputs.ForEach(m => WriteLine($"Please enter a value for {m.InputType} \"{m.Name}\".")); } else { Console.WriteLine(result); } } catch (Exception ex) { WriteLine(ex.Message); } } Console.ReadKey(); }
private ICollection<MissingInput> CheckForMissingInputs<T>(ArmTemplateExpression expression, ICollection<T> inputs) where T : IArmTemplateInput { var output = new List<MissingInput>(); var inputType = typeof(T) == typeof(ArmTemplateVariable) ? InputTypes.Variable : InputTypes.Parameter; var referencesInExpression = inputType == InputTypes.Variable ? expression.VariableNames : expression.ParameterNames; var missingInputs = inputs == null || inputs.Equals(default(T)) ? referencesInExpression : referencesInExpression.Except(inputs.Select(i => i.Name)); if (missingInputs.Any()) { missingInputs.ToList() .ForEach(i => output.Add(new MissingInput(i, inputType))); } return output; }
public void AddParameters_ValidInputParameters_SetsExpectedTemplateParameters( ArmTemplateExpression expression, List <ArmTemplateParameter> inputParameters ) { var actual = new TemplateBuilder() .AddParameters(expression, inputParameters) .Template["parameters"]; Assert.IsType <JObject>(actual); foreach (var p in inputParameters) { if (p.Type == "object") { Assert.Equal(p.Type, actual[p.Name]["type"].ToString()); foreach (var property in p.Value.GetType().GetProperties()) { var actualValueString = actual[p.Name]["defaultValue"][property.Name].ToString(); Assert.Equal(property.GetValue(p.Value).ToString(), actualValueString); } } else if (p.Type == "array") { Assert.Equal(p.Type, actual[p.Name]["type"]); var valueArray = (Array)p.Value; foreach (var i in valueArray) { Assert.Contains($"{i.ToString()}", actual[p.Name]["defaultValue"].ToString()); } } else { Assert.Equal(p.Type, actual[p.Name]["type"]); Assert.Equal(p.Value.ToString(), actual[p.Name]["defaultValue"].ToString()); } } }