public void TryMatch_ReturnsFalse_IfDifferentUnboundFunctionWithSameParamerters() { // Arrange IEdmModel model = new Mock <IEdmModel>().Object; IEdmEntityType returnType = new Mock <IEdmEntityType>().Object; EdmEntityContainer container = new EdmEntityContainer("NS", "Container"); EdmFunction function = new EdmFunction("NS", "Function", new EdmEntityTypeReference(returnType, isNullable: false)); EdmFunctionImport functionImport1 = new EdmFunctionImport(container, "FunctionImport1", function); EdmFunctionImport functionImport2 = new EdmFunctionImport(container, "FunctionImport2", function); Dictionary <string, string> parameterValues = new Dictionary <string, string>() { { "Parameter1", "1" }, { "Parameter2", "2" } }; Dictionary <string, string> parameterMappings = new Dictionary <string, string>() { { "Parameter1", "{param1}" }, { "Parameter2", "{param2}" } }; UnboundFunctionPathSegment segment = new UnboundFunctionPathSegment(functionImport1, model, parameterValues); var template = new UnboundFunctionPathSegmentTemplate( new UnboundFunctionPathSegment(functionImport2, model, parameterMappings)); Dictionary <string, object> values = new Dictionary <string, object>(); // Act bool result = template.TryMatch(segment, values); // Assert Assert.False(result); }
/// <inheritdoc /> public override bool TryMatch(ODataPathSegment pathSegment, IDictionary <string, object> values) { if (pathSegment.SegmentKind == ODataSegmentKinds.UnboundFunction) { UnboundFunctionPathSegment functionSegment = (UnboundFunctionPathSegment)pathSegment; if (_functionName == functionSegment.FunctionName) { var enumNames = functionSegment.Function.Function.Parameters.Where(p => p.Type.IsEnum()).Select(p => p.Name); if (KeyValuePathSegmentTemplate.TryMatch(ParameterMappings, functionSegment.Values, values, enumNames)) { foreach (KeyValuePair <string, string> nameAndValue in functionSegment.Values) { string name = nameAndValue.Key; object value = functionSegment.GetParameterValue(name); ProcedureRoutingConventionHelpers.AddFunctionParameters(functionSegment.Function.Function, name, value, values, values, ParameterMappings); } return(true); } } } return(false); }
public void Property_SegmentKind_IsUnboundFunction() { // Arrange Dictionary<string, string> parameters = new Dictionary<string, string>(); UnboundFunctionPathSegment segment = new UnboundFunctionPathSegment("function", parameters); // Assert Assert.Equal(ODataSegmentKinds.UnboundFunction, segment.SegmentKind); }
public void Property_SegmentKind_IsUnboundFunction() { // Arrange Dictionary <string, string> parameters = new Dictionary <string, string>(); UnboundFunctionPathSegment segment = new UnboundFunctionPathSegment("function", parameters); // Assert Assert.Equal(ODataSegmentKinds.UnboundFunction, segment.SegmentKind); }
public void GetEdmType_ThrowsArgumentException_IfArgumentNotNull() { // Arrange var segment = new UnboundFunctionPathSegment("GetTopCustomer", parameterValues: null); Mock <IEdmType> edmType = new Mock <IEdmType>(); // Act & Assert Assert.Throws <ArgumentException>(() => segment.GetEdmType(edmType.Object)); }
/// <summary> /// Initializes a new instance of the <see cref="UnboundFunctionPathSegmentTemplate"/> class. /// </summary> /// <param name="function">The function segment to be templatized.</param> public UnboundFunctionPathSegmentTemplate(UnboundFunctionPathSegment function) { if (function == null) { throw Error.ArgumentNull("function"); } _functionName = function.FunctionName; ParameterMappings = KeyValuePathSegmentTemplate.BuildParameterMappings(function.Values, function.ToString()); }
/// <inheritdoc /> public override bool TryMatch(ODataPathSegment pathSegment, IDictionary <string, object> values) { if (pathSegment.SegmentKind == ODataSegmentKinds.UnboundFunction) { UnboundFunctionPathSegment functionSegment = (UnboundFunctionPathSegment)pathSegment; return(functionSegment.Function == Function && functionSegment.FunctionName == FunctionName); } return(false); }
public void Ctor_TakingFunction_InitializesFunctionNameProperty() { // Arrange IEdmFunctionImport function = _container.FindOperationImports("MyFunction").SingleOrDefault() as IEdmFunctionImport; // Act UnboundFunctionPathSegment functionPathSegment = new UnboundFunctionPathSegment(function, _model, null); // Assert Assert.Equal("MyFunction", functionPathSegment.FunctionName); }
public void Ctor_TakingFunction_InitializesFunctionProperty() { // Arrange Mock<IEdmFunctionImport> edmFunction = new Mock<IEdmFunctionImport>(); edmFunction.Setup(a => a.Name).Returns("Function"); // Act UnboundFunctionPathSegment functionPathSegment = new UnboundFunctionPathSegment(edmFunction.Object, _model, null); // Assert Assert.Same(edmFunction.Object, functionPathSegment.Function); }
public void GetNavigationSource_ReturnsNull_UnboundFunctionEntitySetType() { // Arrange IEdmFunctionImport functionImport = _container.FindOperationImports("MyFunction").SingleOrDefault() as IEdmFunctionImport; UnboundFunctionPathSegment segment = new UnboundFunctionPathSegment(functionImport, _model, parameterValues: null); // Act var result = segment.GetNavigationSource(previousNavigationSource: null); // Assert Assert.Null(result); }
/// <inheritdoc /> public override bool TryMatch(ODataPathSegment pathSegment, IDictionary <string, object> values) { if (pathSegment.SegmentKind == ODataSegmentKinds.UnboundFunction) { UnboundFunctionPathSegment functionSegment = (UnboundFunctionPathSegment)pathSegment; if (_functionName == functionSegment.FunctionName) { return(KeyValuePathSegmentTemplate.TryMatch(ParameterMappings, functionSegment.Values, values)); } } return(false); }
public void GetEdmType_ReturnsNotNull_PrimitiveReturnType() { // Arrange IEdmFunctionImport functionImport = _container.FindOperationImports("MyFunction").SingleOrDefault() as IEdmFunctionImport; UnboundFunctionPathSegment segment = new UnboundFunctionPathSegment(functionImport, _model, parameterValues: null); // Act var result = segment.GetEdmType(previousEdmType: null); // Assert Assert.NotNull(result); Assert.Equal("Edm.String", result.FullTypeName()); }
public void GetNavigationSource_ReturnsNotNull_UnboundFunctionEntitySetType() { // Arrange IEdmFunctionImport functionImport = _container.FindOperationImports("TopCustomer").SingleOrDefault() as IEdmFunctionImport; UnboundFunctionPathSegment segment = new UnboundFunctionPathSegment(functionImport, _model, parameterValues: null); // Act var result = segment.GetNavigationSource(previousNavigationSource: null); // Assert Assert.NotNull(result); Assert.Equal("System.Web.OData.Routing.MyCustomer", result.EntityType().FullName()); }
public void Ctor_TakingFunction_InitializesFunctionProperty() { // Arrange Mock <IEdmFunctionImport> edmFunction = new Mock <IEdmFunctionImport>(); edmFunction.Setup(a => a.Name).Returns("Function"); // Act UnboundFunctionPathSegment functionPathSegment = new UnboundFunctionPathSegment(edmFunction.Object, _model, null); // Assert Assert.Same(edmFunction.Object, functionPathSegment.Function); }
/// <inheritdoc /> public override bool TryMatch(ODataPathSegment pathSegment, IDictionary <string, object> values) { if (pathSegment.SegmentKind == ODataSegmentKinds.UnboundFunction) { UnboundFunctionPathSegment functionSegment = (UnboundFunctionPathSegment)pathSegment; if (_functionName == functionSegment.FunctionName) { var enumNames = functionSegment.Function.Function.Parameters.Where(p => p.Type.IsEnum()).Select(p => p.Name); return(KeyValuePathSegmentTemplate.TryMatch(ParameterMappings, functionSegment.Values, values, enumNames)); } } return(false); }
public void ToString_ReturnsSameString_UnboundFunction() { // Arrange Dictionary <string, string> parameters = new Dictionary <string, string>(); parameters.Add("Id", "123"); parameters.Add("Name", "John"); UnboundFunctionPathSegment segment = new UnboundFunctionPathSegment("function", parameters); // Act string actual = segment.ToString(); // Assert Assert.Equal("function(Id=123,Name=John)", actual); }
public void TryMatch_ReturnsTrue_IfSameUnboundFunction() { // Arrange IEdmFunctionImport function = _container.FindOperationImports("TopCustomer").SingleOrDefault() as IEdmFunctionImport; UnboundFunctionPathSegment template = new UnboundFunctionPathSegment(function, _model, parameterValues: null); UnboundFunctionPathSegment segment = new UnboundFunctionPathSegment(function, _model, parameterValues: null); // Act Dictionary <string, object> values = new Dictionary <string, object>(); bool result = template.TryMatch(segment, values); // Assert Assert.True(result); Assert.Empty(values); }
/// <summary> /// Parses the first OData segment following the service base URI. /// </summary> /// <param name="model">The model to use for path parsing.</param> /// <param name="segment">The value of the segment to parse.</param> /// <param name="segments">The queue of pending segments.</param> /// <returns>A parsed representation of the segment.</returns> protected virtual ODataPathSegment ParseEntrySegment(IEdmModel model, string segment, Queue <string> segments) { if (segments == null) { throw Error.ArgumentNull("segments"); } if (String.IsNullOrEmpty(segment)) { throw Error.Argument(SRResources.SegmentNullOrEmpty); } if (segment == ODataSegmentKinds.Metadata) { return(new MetadataPathSegment()); } if (segment == ODataSegmentKinds.Batch) { return(new BatchPathSegment()); } IEdmEntityContainer container = ExtractEntityContainer(model); IEdmEntitySet entitySet = container.FindEntitySet(segment); if (entitySet != null) { return(new EntitySetPathSegment(entitySet)); } // It's not possible to use a bound function (or action) at root. // So, try to match an unbound action call IEdmActionImport action = container.FindActionImport(segment); if (action != null) { return(new UnboundActionPathSegment(action)); } // Try to match an unbound function call UnboundFunctionPathSegment pathSegment = TryMatchUnboundFunctionCall(segment, segments, model); if (pathSegment != null) { return(pathSegment); } // segment does not match the model return(null); }
private static UnboundFunctionPathSegment TryMatchUnboundFunctionCall(string segment, Queue <string> segments, IEdmModel model) { IEdmEntityContainer container = ExtractEntityContainer(model); string nextSegment = segments.Count > 0 ? segments.Peek() : null; IEnumerable <IEdmOperationImport> operationImports = container.FindMatchedOperationImports(segment); IEnumerable <IEdmFunctionImport> possibleFunctions = operationImports.OfType <IEdmFunctionImport>(); UnboundFunctionPathSegment unboundFunctionSegment = FunctionResolver.TryResolveUnbound(possibleFunctions, model, nextSegment); if (unboundFunctionSegment != null && FunctionResolver.IsEnclosedInParentheses(nextSegment)) { segments.Dequeue(); } return(unboundFunctionSegment); }
public void TryMatch_ReturnTrue_IfSameUnboundFunction() { // Arrange IEdmModel model = new Mock <IEdmModel>().Object; IEdmEntityType returnType = new Mock <IEdmEntityType>().Object; EdmEntityContainer container = new EdmEntityContainer("NS", "Container"); EdmFunction func = new EdmFunction("NS", "Function", new EdmEntityTypeReference(returnType, isNullable: false)); func.AddParameter("Parameter1", EdmCoreModel.Instance.GetInt32(isNullable: false)); func.AddParameter("Parameter2", EdmCoreModel.Instance.GetInt32(isNullable: false)); EdmFunctionImport function = new EdmFunctionImport(container, "Function", func); Dictionary <string, string> parameterValues = new Dictionary <string, string> { { "Parameter1", "1" }, { "Parameter2", "2" } }; Dictionary <string, string> parameterMappings = new Dictionary <string, string> { { "Parameter1", "{param1}" }, { "Parameter2", "{param2}" } }; UnboundFunctionPathSegment segment = new UnboundFunctionPathSegment(function, model, parameterValues); UnboundFunctionPathSegmentTemplate template = new UnboundFunctionPathSegmentTemplate( new UnboundFunctionPathSegment(function, model, parameterMappings)); // Act Dictionary <string, object> values = new Dictionary <string, object>(); bool result = template.TryMatch(segment, values); // Assert Assert.True(result); Assert.Equal(4, values.Count); Assert.Equal("1", values["param1"]); Assert.Equal("2", values["param2"]); Assert.Equal(1, (values[ODataParameterValue.ParameterValuePrefix + "param1"] as ODataParameterValue).Value); Assert.Equal(2, (values[ODataParameterValue.ParameterValuePrefix + "param2"] as ODataParameterValue).Value); }
public void TryMatch_ReturnTrue_IfSameUnboundFunction() { // Arrange IEdmModel model = new Mock<IEdmModel>().Object; IEdmEntityType returnType = new Mock<IEdmEntityType>().Object; EdmEntityContainer container = new EdmEntityContainer("NS", "Container"); EdmFunction func = new EdmFunction("NS", "Function", new EdmEntityTypeReference(returnType, isNullable: false)); func.AddParameter("Parameter1", EdmCoreModel.Instance.GetInt32(isNullable: false)); func.AddParameter("Parameter2", EdmCoreModel.Instance.GetInt32(isNullable: false)); EdmFunctionImport function = new EdmFunctionImport(container, "Function", func); Dictionary<string, string> parameterValues = new Dictionary<string, string> { { "Parameter1", "1" }, { "Parameter2", "2" } }; Dictionary<string, string> parameterMappings = new Dictionary<string, string> { { "Parameter1", "{param1}" }, { "Parameter2", "{param2}" } }; UnboundFunctionPathSegment segment = new UnboundFunctionPathSegment(function, model, parameterValues); UnboundFunctionPathSegmentTemplate template = new UnboundFunctionPathSegmentTemplate( new UnboundFunctionPathSegment(function, model, parameterMappings)); // Act Dictionary<string, object> values = new Dictionary<string,object>(); bool result = template.TryMatch(segment, values); // Assert Assert.True(result); Assert.Equal(4, values.Count); Assert.Equal("1", values["param1"]); Assert.Equal("2", values["param2"]); Assert.Equal(1, (values[ODataParameterValue.ParameterValuePrefix + "param1"] as ODataParameterValue).Value); Assert.Equal(2, (values[ODataParameterValue.ParameterValuePrefix + "param2"] as ODataParameterValue).Value); }
public void TryMatch_ReturnsFalse_IfDifferentUnboundFunctionWithSameParamerters() { // Arrange IEdmModel model = new Mock<IEdmModel>().Object; IEdmEntityType returnType = new Mock<IEdmEntityType>().Object; EdmEntityContainer container = new EdmEntityContainer("NS", "Container"); EdmFunction function = new EdmFunction("NS", "Function", new EdmEntityTypeReference(returnType, isNullable: false)); EdmFunctionImport functionImport1 = new EdmFunctionImport(container,"FunctionImport1", function); EdmFunctionImport functionImport2 = new EdmFunctionImport(container,"FunctionImport2", function); Dictionary<string, string> parameterValues = new Dictionary<string, string>() { { "Parameter1", "1" }, { "Parameter2", "2" } }; Dictionary<string, string> parameterMappings = new Dictionary<string, string>() { { "Parameter1", "{param1}" }, { "Parameter2", "{param2}" } }; UnboundFunctionPathSegment segment = new UnboundFunctionPathSegment(functionImport1, model, parameterValues); var template = new UnboundFunctionPathSegmentTemplate( new UnboundFunctionPathSegment(functionImport2, model, parameterMappings)); Dictionary<string, object> values = new Dictionary<string, object>(); // Act bool result = template.TryMatch(segment, values); // Assert Assert.False(result); }
public void GetEdmType_ThrowsArgumentException_IfArgumentNotNull() { // Arrange var segment = new UnboundFunctionPathSegment("GetTopCustomer", parameterValues: null); Mock<IEdmType> edmType = new Mock<IEdmType>(); // Act & Assert Assert.Throws<ArgumentException>(() => segment.GetEdmType(edmType.Object)); }
public void GetEntitySet_ReturnsNotNull_UnboundFunctionEntitySetType() { // Arrange IEdmFunctionImport functionImport = _container.FindOperationImports("TopCustomer").SingleOrDefault() as IEdmFunctionImport; UnboundFunctionPathSegment segment = new UnboundFunctionPathSegment(functionImport, _model, parameterValues: null); // Act var result = segment.GetEntitySet(previousEntitySet: null); // Assert Assert.NotNull(result); Assert.Equal("System.Web.OData.Routing.MyCustomer", result.ElementType.FullName()); }
public void GetEntitySet_ReturnsNull_UnboundFunctionEntitySetType() { // Arrange IEdmFunctionImport functionImport = _container.FindOperationImports("MyFunction").SingleOrDefault() as IEdmFunctionImport; UnboundFunctionPathSegment segment = new UnboundFunctionPathSegment(functionImport, _model, parameterValues: null); // Act var result = segment.GetEntitySet(previousEntitySet: null); // Assert Assert.Null(result); }
public void TryMatch_ReturnsTrue_IfSameUnboundFunction() { // Arrange IEdmFunctionImport function = _container.FindOperationImports("TopCustomer").SingleOrDefault() as IEdmFunctionImport; UnboundFunctionPathSegment template = new UnboundFunctionPathSegment(function, _model, parameterValues: null); UnboundFunctionPathSegment segment = new UnboundFunctionPathSegment(function, _model, parameterValues: null); // Act Dictionary<string, object> values = new Dictionary<string, object>(); bool result = template.TryMatch(segment, values); // Assert Assert.True(result); Assert.Empty(values); }
public void ToString_ReturnsSameString_UnboundFunction() { // Arrange Dictionary<string, string> parameters = new Dictionary<string, string>(); parameters.Add("Id", "123"); parameters.Add("Name", "John"); UnboundFunctionPathSegment segment = new UnboundFunctionPathSegment("function", parameters); // Act string actual = segment.ToString(); // Assert Assert.Equal("function(Id=123,Name=John)", actual); }