public void BuildFilter_AliasInFunction_enum_undefined() { Uri fullUri = new Uri("http://gobbledygook/People?$filter=null%20ne%20Fully.Qualified.Namespace.GetPetCount(colorPattern%3D%40p1)&@p1=Fully.Qualified.Namespace.ColorPattern'238563'"); ODataUriParser odataUriParser = new ODataUriParser(HardCodedTestModel.TestModel, serviceRoot, fullUri); SetODataUriParserSettingsTo(this.settings, odataUriParser.Settings); odataUriParser.UrlKeyDelimiter = ODataUrlKeyDelimiter.Parentheses; ODataUri odataUri = odataUriParser.ParseUri(); var rightNode = Assert.IsType <BinaryOperatorNode>(odataUri.Filter.Expression).Right; NamedFunctionParameterNode p = Assert.IsType <NamedFunctionParameterNode>(Assert.IsType <SingleResourceFunctionCallNode>(rightNode).Parameters.First()); ParameterAliasNode aliasNode = Assert.IsType <ParameterAliasNode>(p.Value); Assert.Equal("@p1", aliasNode.Alias); Assert.True(aliasNode.TypeReference.IsEnum()); Assert.Equal("Fully.Qualified.Namespace.ColorPattern", aliasNode.TypeReference.Definition.FullTypeName()); IDictionary <string, SingleValueNode> aliasNodes = odataUri.ParameterAliasNodes; var node = Assert.IsType <ConstantNode>(aliasNodes["@p1"]); var enumValue = Assert.IsType <ODataEnumValue>(node.Value); Assert.Equal("Fully.Qualified.Namespace.ColorPattern", enumValue.TypeName); Assert.Equal("238563", enumValue.Value); Assert.True(aliasNodes["@p1"].TypeReference.IsEnum()); Uri actualUri = odataUri.BuildUri(ODataUrlKeyDelimiter.Parentheses); Assert.Equal(fullUri, actualUri); actualUri = odataUri.BuildUri(ODataUrlKeyDelimiter.Slash); Assert.Equal(fullUri, actualUri); }
/// <summary> /// Visit a ParameterAliasNode /// </summary> /// <param name="nodeIn">The node to visit</param> /// <returns>The translated expression</returns> public override Expression Visit(ParameterAliasNode nodeIn) { this.CheckArgumentNull(nodeIn, "ParameterAliasNode"); var aliasName = nodeIn.Alias; return(TranslateParameterAlias(aliasName)); }
internal static object TranslateNode(object value) { if (value == null) { return(null); } ConstantNode node = value as ConstantNode; if (node != null) { return(node.Value); } ConvertNode convertNode = value as ConvertNode; if (convertNode != null) { object source = TranslateNode(convertNode.Source); return(source); } ParameterAliasNode parameterAliasNode = value as ParameterAliasNode; if (parameterAliasNode != null) { return(parameterAliasNode.Alias); } throw new NotSupportedException(); }
internal static object TranslateNode(object value) { if (value == null) { return(null); } ConstantNode node = value as ConstantNode; if (node != null) { return(node.Value); } ConvertNode convertNode = value as ConvertNode; if (convertNode != null) { object source = TranslateNode(convertNode.Source); return(source); } ParameterAliasNode parameterAliasNode = value as ParameterAliasNode; if (parameterAliasNode != null) { return(parameterAliasNode.Alias); } throw Error.NotSupported(SRResources.CannotRecognizeNodeType, typeof(ODataParameterHelper), value.GetType().FullName); }
public void FilterSegmentShouldNotBeSingleResult() { const string filterExpression = "@a1"; FilterClause filterClause = CreateFilterClause(filterExpression); ParameterAliasNode expression = filterClause.Expression as ParameterAliasNode; FilterSegment filterSegment = new FilterSegment(expression, filterClause.RangeVariable, HardCodedTestModel.GetPet1Set()); filterSegment.SingleResult.Should().BeFalse(); }
public void FilterSegmentShouldHaveSameTypeAsRangeVariableTypeDefinition() { const string filterExpression = "@a1"; FilterClause filterClause = CreateFilterClause(filterExpression); ParameterAliasNode expression = filterClause.Expression as ParameterAliasNode; FilterSegment filterSegment = new FilterSegment(expression, filterClause.RangeVariable, HardCodedTestModel.GetPet1Set()); filterSegment.EdmType.Equals(filterClause.RangeVariable.TypeReference.Definition).Should().BeFalse(); }
public void FilterSegmentWithAllParametersShouldConstructSuccessfully() { const string filterExpression = "@a1"; FilterClause filterClause = CreateFilterClause(filterExpression); ParameterAliasNode expression = filterClause.Expression as ParameterAliasNode; FilterSegment filterSegment = new FilterSegment(expression, filterClause.RangeVariable, HardCodedTestModel.GetPet1Set()); filterSegment.Expression.Equals(expression).Should().BeTrue(); filterSegment.RangeVariable.Equals(filterClause.RangeVariable).Should().BeTrue(); }
public void FilterSegmentConstructWithNullNavigationSourceShouldThrowException() { const string filterExpression = "@a1"; FilterClause filterClause = CreateFilterClause(filterExpression); ParameterAliasNode expression = filterClause.Expression as ParameterAliasNode; Action create = () => new FilterSegment(expression, filterClause.RangeVariable, null); create.ShouldThrow <ArgumentNullException>().WithMessage("Value cannot be null.\r\nParameter name: navigationSource"); }
public void FilterSegmentConstructWithNullRangeVariableShouldThrowException() { const string filterExpression = "@a1"; FilterClause filterClause = CreateFilterClause(filterExpression); ParameterAliasNode expression = filterClause.Expression as ParameterAliasNode; Action create = () => new FilterSegment(expression, null, HardCodedTestModel.GetPet1Set()); create.ShouldThrow <ArgumentNullException>().WithMessage("Value cannot be null.\r\nParameter name: rangeVariable"); }
public void FilterSegmentConstructWithNullNavigationSourceShouldThrowException() { const string filterExpression = "@a1"; FilterClause filterClause = CreateFilterClause(filterExpression); ParameterAliasNode expression = filterClause.Expression as ParameterAliasNode; Action create = () => new FilterSegment(expression, filterClause.RangeVariable, null); Assert.Throws <ArgumentNullException>("navigationSource", create); }
public void FilterSegmentConstructWithNullRangeVariableShouldThrowException() { const string filterExpression = "@a1"; FilterClause filterClause = CreateFilterClause(filterExpression); ParameterAliasNode expression = filterClause.Expression as ParameterAliasNode; Action create = () => new FilterSegment(expression, null, HardCodedTestModel.GetPet1Set()); Assert.Throws <ArgumentNullException>("rangeVariable", create); }
public void FilterSegmentsWithDifferentNavigationSourcesShouldNotBeEqual() { const string filterExpression1 = "@a1"; FilterClause filterClause = CreateFilterClause(filterExpression1); ParameterAliasNode expression = filterClause.Expression as ParameterAliasNode; FilterSegment filterSegment1 = new FilterSegment(expression, filterClause.RangeVariable, HardCodedTestModel.GetPet1Set()); FilterSegment filterSegment2 = new FilterSegment(expression, filterClause.RangeVariable, HardCodedTestModel.GetPeopleSet()); filterSegment1.Equals(filterSegment2).Should().BeFalse(); filterSegment2.Equals(filterSegment1).Should().BeFalse(); }
public void FilterSegmentsWithSameExpressionShouldBeEqual() { const string filterExpression = "@a1"; FilterClause filterClause = CreateFilterClause(filterExpression); ParameterAliasNode expression = filterClause.Expression as ParameterAliasNode; FilterSegment filterSegment1 = new FilterSegment(expression, filterClause.RangeVariable, HardCodedTestModel.GetPet1Set()); FilterSegment filterSegment2 = new FilterSegment(expression, filterClause.RangeVariable, HardCodedTestModel.GetPet1Set()); filterSegment1.Equals(filterSegment2).Should().BeTrue(); filterSegment2.Equals(filterSegment1).Should().BeTrue(); }
/// <summary> /// Translate a ParameterAliasNode. /// </summary> /// <param name="nodeIn">The node to be translated.</param> /// <returns>The translated node.</returns> public override QueryNode Visit(ParameterAliasNode nodeIn) { SingleValueNode node = ODataPathSegmentTranslator.TranslateParameterAlias(nodeIn, _parameterAliasNode); if (node == null) { return(new ConstantNode(null)); } else { return(node.Accept(this)); } }
/// <summary> /// Writes single entity function call node to string. /// </summary> /// <param name="node">Node to write to string</param> /// <returns>String representation of node.</returns> private static string ToString(ParameterAliasNode node) { if (node != null) { return(tabHelper.Prefix + "ParameterAliasNode" + tabHelper.Indent(() => tabHelper.Prefix + "Parameter Name = " + node.Alias + tabHelper.Prefix + "Type = " + node.TypeReference )); } return(String.Empty); }
public void CanTranslate_ParameterAliasNode() { // Arrange var translator = new ParameterAliasNodeTranslator( new Dictionary<string, SingleValueNode> { { "@p", _parameterAliasMappedNode } }); var parameterAliasNode = new ParameterAliasNode("@p", null); // Act QueryNode translatedNode = parameterAliasNode.Accept(translator); // Assert Assert.Same(_parameterAliasMappedNode, translatedNode); }
public void ReturnsNull_ParameterAliasNotFound() { // Arrange var translator = new ParameterAliasNodeTranslator(new Dictionary<string, SingleValueNode>()); var parameterAliasNode = new ParameterAliasNode("@unknown", null); // Act QueryNode translatedNode = parameterAliasNode.Accept(translator); // Assert var constantNode = Assert.IsType<ConstantNode>(translatedNode); Assert.Null(constantNode.Value); }
public void ReturnsNull_ParameterAliasNotFound() { // Arrange var translator = new ParameterAliasNodeTranslator(new Dictionary <string, SingleValueNode>()); var parameterAliasNode = new ParameterAliasNode("@unknown", null); // Act QueryNode translatedNode = parameterAliasNode.Accept(translator); // Assert var constantNode = Assert.IsType <ConstantNode>(translatedNode); Assert.Null(constantNode.Value); }
private static string TranslateNode(object node, string functionName, string parameterName) { // If the function parameter is null, for example myFunction(param=null), // the input node here is not null, it is a contant node with a value as "null". // However, if a function call (or key) using parameter alias but without providing the parameter alias value, // the input node here is a null. if (node == null) { // We can't throw ODataException here because ODataException will be caught and return 404 response with empty message. throw new InvalidOperationException(Error.Format(SRResources.MissingConvertNode, parameterName, functionName)); } ConstantNode constantNode = node as ConstantNode; if (constantNode != null) { UriTemplateExpression uriTemplateExpression = constantNode.Value as UriTemplateExpression; if (uriTemplateExpression != null) { return(uriTemplateExpression.LiteralText); } // Make the enum prefix free to work. ODataEnumValue enumValue = constantNode.Value as ODataEnumValue; if (enumValue != null) { return(ODataUriUtils.ConvertToUriLiteral(enumValue, ODataVersion.V4)); } return(constantNode.LiteralText); } ConvertNode convertNode = node as ConvertNode; if (convertNode != null) { return(TranslateNode(convertNode.Source, functionName, parameterName)); } ParameterAliasNode parameterAliasNode = node as ParameterAliasNode; if (parameterAliasNode != null) { return(parameterAliasNode.Alias); } //return node.ToString(); throw Error.NotSupported(SRResources.CannotRecognizeNodeType, typeof(ODataPathSegmentHandler), node.GetType().FullName); }
// Translate the node in ODL path to string literal. private string TranslateNode(object node) { if (node == null) { throw Error.ArgumentNull("node"); } ConstantNode constantNode = node as ConstantNode; if (constantNode != null) { if (_enableUriTemplateParsing) { UriTemplateExpression uriTemplateExpression = constantNode.Value as UriTemplateExpression; if (uriTemplateExpression != null) { return(uriTemplateExpression.LiteralText); } } // Make the enum prefix free to work. ODataEnumValue enumValue = constantNode.Value as ODataEnumValue; if (enumValue != null) { return(ODataUriUtils.ConvertToUriLiteral(enumValue, ODataVersion.V4)); } return(constantNode.LiteralText); } ConvertNode convertNode = node as ConvertNode; if (convertNode != null) { return(TranslateNode(convertNode.Source)); } ParameterAliasNode parameterAliasNode = node as ParameterAliasNode; if (parameterAliasNode != null) { return(TranslateParameterAlias(parameterAliasNode.Alias)); } throw Error.NotSupported( SRResources.CannotRecognizeNodeType, typeof(ODataPathSegmentTranslator), node.GetType().FullName); }
public void CanTranslate_ParameterAliasNode() { // Arrange var translator = new ParameterAliasNodeTranslator( new Dictionary <string, SingleValueNode> { { "@p", _parameterAliasMappedNode } }); var parameterAliasNode = new ParameterAliasNode("@p", null); // Act QueryNode translatedNode = parameterAliasNode.Accept(translator); // Assert Assert.Same(_parameterAliasMappedNode, translatedNode); }
public void FilterSegmentsWithDifferentExpressionsShouldNotBeEqual() { const string filterExpression1 = "@a1"; const string filterExpression2 = "@a2"; FilterClause filterClause1 = CreateFilterClause(filterExpression1); FilterClause filterClause2 = CreateFilterClause(filterExpression2); ParameterAliasNode expression1 = filterClause1.Expression as ParameterAliasNode; ParameterAliasNode expression2 = filterClause2.Expression as ParameterAliasNode; FilterSegment filterSegment1 = new FilterSegment(expression1, filterClause1.RangeVariable, HardCodedTestModel.GetPet1Set()); FilterSegment filterSegment2 = new FilterSegment(expression2, filterClause2.RangeVariable, HardCodedTestModel.GetPet1Set()); filterSegment1.Equals(filterSegment2).Should().BeFalse(); filterSegment2.Equals(filterSegment1).Should().BeFalse(); }
private static string TranslateNode(object node) { Contract.Assert(node != null); ConstantNode constantNode = node as ConstantNode; if (constantNode != null) { UriTemplateExpression uriTemplateExpression = constantNode.Value as UriTemplateExpression; if (uriTemplateExpression != null) { return(uriTemplateExpression.LiteralText); } // Make the enum prefix free to work. ODataEnumValue enumValue = constantNode.Value as ODataEnumValue; if (enumValue != null) { return(ODataUriUtils.ConvertToUriLiteral(enumValue, ODataVersion.V4)); } return(constantNode.LiteralText); } ConvertNode convertNode = node as ConvertNode; if (convertNode != null) { return(TranslateNode(convertNode.Source)); } ParameterAliasNode parameterAliasNode = node as ParameterAliasNode; if (parameterAliasNode != null) { return(parameterAliasNode.Alias); } //return node.ToString(); throw Error.NotSupported(SRResources.CannotRecognizeNodeType, typeof(ODataPathSegmentHandler), node.GetType().FullName); }
private static string TranslateNode(object node) { Contract.Assert(node != null); ConstantNode constantNode = node as ConstantNode; if (constantNode != null) { UriTemplateExpression uriTemplateExpression = constantNode.Value as UriTemplateExpression; if (uriTemplateExpression != null) { return(uriTemplateExpression.LiteralText); } // Make the enum prefix free to work. ODataEnumValue enumValue = constantNode.Value as ODataEnumValue; if (enumValue != null) { return(ODataUriUtils.ConvertToUriLiteral(enumValue, ODataVersion.V4)); } return(constantNode.LiteralText); } ConvertNode convertNode = node as ConvertNode; if (convertNode != null) { return(TranslateNode(convertNode.Source)); } ParameterAliasNode parameterAliasNode = node as ParameterAliasNode; if (parameterAliasNode != null) { return(parameterAliasNode.Alias); } //return node.ToString(); throw new NotSupportedException($"Cannot recongnize {node.GetType().FullName}"); }
private object TranslateNode(object node) { if (node == null) { throw Error.ArgumentNull("node"); } ConstantNode constantNode = node as ConstantNode; if (constantNode != null) { return(constantNode); } ConvertNode convertNode = node as ConvertNode; if (convertNode != null) { object value = TranslateNode(convertNode.Source); return(ConvertNode(value, convertNode.TypeReference)); } ParameterAliasNode parameterAliasNode = node as ParameterAliasNode; if (parameterAliasNode != null) { SingleValueNode singleValueNode; if (_parameterAliasNodes.TryGetValue(parameterAliasNode.Alias, out singleValueNode) && singleValueNode != null) { return(TranslateNode(singleValueNode)); } // if not found the parameter alias, return null return(null); } throw Error.NotSupported(SRResources.CannotRecognizeNodeType, typeof(ODataPathSegmentTranslator), node.GetType().FullName); }
internal static SingleValueNode TranslateParameterAlias( SingleValueNode node, IDictionary <string, SingleValueNode> parameterAliasNodes) { if (node == null) { throw Error.ArgumentNull("node"); } if (parameterAliasNodes == null) { throw Error.ArgumentNull("parameterAliasNodes"); } ParameterAliasNode parameterAliasNode = node as ParameterAliasNode; if (parameterAliasNode == null) { return(node); } SingleValueNode singleValueNode; if (parameterAliasNodes.TryGetValue(parameterAliasNode.Alias, out singleValueNode) && singleValueNode != null) { if (singleValueNode is ParameterAliasNode) { singleValueNode = TranslateParameterAlias(singleValueNode, parameterAliasNodes); } return(singleValueNode); } // Parameter alias value is assumed to be null if it is not found. // Do not need to translate the parameter alias node from the query string // because this method only deals with the parameter alias node mapping from ODL parser. return(null); }
internal static string TranslateNode(object node) { ConstantNode constantNode = node as ConstantNode; if (constantNode != null) { UriTemplateExpression uriTemplateExpression = constantNode.Value as UriTemplateExpression; if (uriTemplateExpression != null) { return(uriTemplateExpression.LiteralText); } // Make the enum prefix free to work. ODataEnumValue enumValue = constantNode.Value as ODataEnumValue; if (enumValue != null) { return(ODataUriUtils.ConvertToUriLiteral(enumValue, ODataVersion.V4)); } return(constantNode.LiteralText); } ConvertNode convertNode = node as ConvertNode; if (convertNode != null) { return(TranslateNode(convertNode.Source)); } ParameterAliasNode parameterAliasNode = node as ParameterAliasNode; if (parameterAliasNode != null) { return(parameterAliasNode.Alias); } return(ODataUriUtils.ConvertToUriLiteral(node, ODataVersion.V4)); }
/// <summary> /// Writes single entity function call node to string. /// </summary> /// <param name="node">Node to write to string</param> /// <returns>String representation of node.</returns> private static string ToString(ParameterAliasNode node) { if (node != null) { return tabHelper.Prefix + "ParameterAliasNode" + tabHelper.Indent(() => tabHelper.Prefix + "Parameter Name = " + node.Alias + tabHelper.Prefix + "Type = " + node.TypeReference ); } return String.Empty; }
public static string WriteParameterValue(object value) { if (value == null) { return("Null"); } string outputString = String.Empty; ODataPrimitiveValue primitiveValue = value as ODataPrimitiveValue; if (primitiveValue != null) { value = primitiveValue.Value; } var geogPoint = value as GeographyPoint; if (geogPoint != null) { outputString = String.Format("GeographyPoint(Latitude = {0}, Longitude = {1}", geogPoint.Latitude, geogPoint.Longitude); if (geogPoint.M != null) { outputString += String.Format(", M = {0}", geogPoint.M); } if (geogPoint.Z != null) { outputString += String.Format(", Z = {0}", geogPoint.Z); } outputString += ")"; } var geomPoint = value as GeometryPoint; if (geomPoint != null) { outputString = String.Format("GeometryPoint(X = {0}, Y = {1}", geomPoint.X, geomPoint.Y); if (geomPoint.M != null) { outputString += String.Format(", M = {0}", geomPoint.M); } if (geomPoint.Z != null) { outputString += String.Format(", Z = {0}", geomPoint.Z); } outputString += ")"; } var str = value as string; if (str != null) { outputString = str; } if (value is double) { outputString = ((double)value).ToString(); } if (value is int) { outputString = ((int)value).ToString(); } var complex = value as ODataComplexValue; if (complex != null) { outputString = "Complex Value("; if (complex.TypeName != null) { outputString += String.Format("Type Name = {0}", complex.TypeName); if (complex.Properties.Any()) { outputString += String.Format(", "); } } if (complex.Properties.Any()) { outputString += String.Format("Properties = ("); foreach (var property in complex.Properties) { outputString += String.Format("{0}: {1}, ", property.Name, property.Value); } outputString = outputString.TrimEnd(',', ' ') + ")"; } outputString += ")"; } var nullval = value as ODataNullValue; if (nullval != null) { outputString = "Null"; } var aliasedparam = value as ODataUnresolvedFunctionParameterAlias; if (aliasedparam != null) { outputString = String.Format("Unresolved Aliased Parameter({0})", aliasedparam.Alias); } ConstantNode constNode = value as ConstantNode; if (constNode != null) { outputString = String.Format("ConstantNode(Type = {0}, Value = {1})", constNode.TypeReference, WriteParameterValue(constNode.Value)); } ParameterAliasNode parameterAliasNode = value as ParameterAliasNode; if (parameterAliasNode != null) { outputString = String.Format("ParameterAliasNode(Type Name = {0}, Alias Name = {1})", parameterAliasNode.TypeReference, parameterAliasNode.Alias); } if (outputString == String.Empty) { throw new NotImplementedException("Type for property value not yet supported in ToString visitor"); } return(outputString); }
/// <summary> /// Translates a <see cref="ParameterAliasNode"/> into a corresponding <see cref="String"/>. /// </summary> /// <param name="node">The node to translate.</param> /// <returns>The translated String.</returns> public override String Visit(ParameterAliasNode node) { ExceptionUtils.CheckArgumentNotNull(node, "node"); return(node.Alias); }
/// <summary> /// Translates a <see cref="ParameterAliasNode"/> into a corresponding <see cref="string"/>. /// </summary> /// <param name="node">The node to translate.</param> /// <returns>The translated string.</returns> public override string Visit(ParameterAliasNode node) { return(node.Alias); }
/// <summary> /// Visit a ParameterAliasNode /// </summary> /// <param name="nodeIn">The node to visit</param> /// <returns>The translated expression</returns> public virtual T Visit(ParameterAliasNode nodeIn) { throw new NotImplementedException(); }
public override object Visit(ParameterAliasNode nodeIn) { return(null); }