public void QueryPassesOnCall() { var urn = string.Empty; var args = new QueryArguments(); var result = new QueryResult(); var invokerMock = new Mock<IActionInvoker>(MockBehavior.Strict); invokerMock.Setup(i => i.Query(urn, args)).Returns(result).Verifiable(); var channel = new WcfChannel(invokerMock.Object); var actual = channel.Query(urn, args); Assert.AreSame(result, actual); invokerMock.Verify(); }
/// <summary> /// Generates the definition. /// </summary> /// <param name="arguments">The arguments.</param> /// <param name="method">The method.</param> /// <param name="messageFormats">The message formats.</param> /// <param name="namespaces">The namespaces.</param> /// <returns> /// The definition for the remote action. /// </returns> private static RemoteActionDefinition GenerateDefinition(QueryArguments arguments, MethodInfo method, Dictionary<Type, string> messageFormats, Dictionary<string, string> namespaces) { var description = ExtractDescription(method); var definition = new RemoteActionDefinition { Name = method.Name, Description = description }; // Generate the message formats switch (arguments.DataToInclude) { case DataDefinitions.InputOnly: definition.InputData = GenerateMessageFormat( messageFormats, namespaces, method.GetParameters()[0].ParameterType); break; case DataDefinitions.OutputOnly: definition.OutputData = GenerateMessageFormat( messageFormats, namespaces, method.ReturnType); break; case DataDefinitions.Both: definition.InputData = GenerateMessageFormat( messageFormats, namespaces, method.GetParameters()[0].ParameterType); definition.OutputData = GenerateMessageFormat( messageFormats, namespaces, method.ReturnType); break; } return definition; }
/// <summary> /// Queries the details on an item action. /// </summary> /// <param name="urn">The URN to query for actions.</param> /// <param name="arguments">The arguments for the query.</param> /// <returns> /// The allowed actions on the URN. /// </returns> public QueryResult Query(string urn, QueryArguments arguments) { var result = new QueryResult(); arguments = arguments ?? new QueryArguments(); var item = this.LocateItem(urn); if (item == null) { logger.Warn("Request made to unknown URN '{0}'", urn); result.ResultCode = RemoteResultCode.UnknownUrn; return result; } var itemType = item.GetType(); var actions = new List<RemoteActionDefinition>(); var filterRegex = (arguments == null) || string.IsNullOrEmpty(arguments.FilterPattern) ? null : new Regex(arguments.FilterPattern); var messageFormats = new Dictionary<Type, string>(); var namespaces = new Dictionary<string, string>(); foreach (var method in itemType.GetMethods(BindingFlags.Instance | BindingFlags.Public)) { var actionAttributes = method.GetCustomAttributes( typeof(RemoteActionAttribute), false); if ((actionAttributes.Length > 0) && ((filterRegex == null) || filterRegex.IsMatch(method.Name))) { // TODO: Validate security here var definition = GenerateDefinition(arguments, method, messageFormats, namespaces); actions.Add(definition); } } // Generate the result result.Actions = actions.ToArray(); return result; }
/// <summary> /// Queries the specified urn. /// </summary> /// <param name="urn">The URN to query for actions.</param> /// <param name="arguments">The arguments for the query.</param> /// <returns> /// The allowed actions on the URN. /// </returns> public QueryResult Query(string urn, QueryArguments arguments) { var logId = Guid.NewGuid(); logger.Debug("Performing query on '{0}' - {1}", urn, logId); try { var result = this.Invoker.Query(urn, arguments); result.LogId = logId; logger.Debug("Query completed for '{0}' - {1}", urn, logId); return result; } catch (Exception error) { logger.ErrorException( "Error happened on query for '" + urn + "' - " + logId + ": " + error.Message, error); return new QueryResult { LogId = logId, ResultCode = RemoteResultCode.FatalError }; } }
public void QueryFiltersActions() { var testItem = new TestItem("Baby"); var server = new Server("Test", testItem); var invoker = new ActionInvoker(server); var args = new QueryArguments { FilterPattern = "DoSomething", DataToInclude = DataDefinitions.None }; var result = invoker.Query("urn:ccnet:test:baby", args); Assert.IsNotNull(result); Assert.AreEqual(RemoteResultCode.Success, result.ResultCode); var expected = new[] { new RemoteActionDefinition { Name = "DoSomething", Description = "This will do something" } }; CollectionAssert.AreEqual(expected, result.Actions, new DefinitionComparer()); }