public void Arguments_from_the_call_should_be_sent_to_the_argument_predicate() { var fake = A.Fake <IFoo>(); ArgumentCollection arguments = null; ThisCall.To(fake).WhenArgumentsMatch(x => { arguments = x; return(true); }).Throws(new ApplicationException()); fake.Baz(null, null); try { fake.Baz(1, "a"); } catch (ApplicationException) { } Assert.That(arguments.ToArray(), Has.Some.EqualTo(1).And.Some.EqualTo("a")); }
private void GenerateReadAction(ControllerSourceUnit srcUnit, HandlerInfo handler, CqrsControllerBuilder builder) { if (handler.RequestType.GetProperties().Any(x => x.Name == "Id" || x.Name == $"{handler.Service.Name}Id")) { // this seems to be a query for one record if (!handler.IsResponseCollection) { } else { // this is query for many records } } else { // this seems to be a query for many records. if (!handler.IsResponseCollection) { } else { ArgumentCollection arguments = new ArgumentCollection(); var queryArg = arguments.Add(handler.RequestType.Name.EndsWithSingleSuffix("Request", "Query"), "request", "FromQuery"); // this is query for many records var responseType = handler.ResponseType.Name.EndsWithSingleSuffix("Response", "Result"); builder.AppendAction(handler, "Get", responseType, HttpVerb.Get, handler.IsResponseCollection, $"api/{handler.Service.Name}", arguments.ToArray()); srcUnit.Responses.Append(GenerateResponseType(responseType, handler.ResponseType)); srcUnit.ResultToResponseMappers.Append(GenerateResultToResponseMapper(responseType, handler.ResponseType)); srcUnit.Requests.Append(GenerateRequestType(queryArg.Type, handler.RequestType)); srcUnit.RequestToCommandMappers.Append(GenerateRequestToCommandMapper(queryArg.Type, handler.RequestType)); builder.AddUsing(handler.ResponseType.Namespace + ".WebApi"); builder.AddUsing(handler.RequestType.Namespace + ".WebApi"); builder.AddUsing(handler.ResponseType.Namespace); builder.AddUsing(handler.RequestType.Namespace); } } }
private static void GenerateWriteAction(ControllerSourceUnit srcUnit, HandlerInfo handler, CqrsControllerBuilder builder) { if (handler.RequestType.GetProperties().Any(x => x.Name == "Id" || x.Name == $"{handler.Service.Name}Id")) { var prop = handler.RequestType.GetProperties() .First(x => x.Name == "Id" || x.Name == $"{handler.Service.Name}Id"); ArgumentCollection arguments = new ArgumentCollection(); var propTypeInfo = (FullTypeNameInfo)prop.PropertyType; var idArg = arguments.Add(propTypeInfo.ToString(), prop.Name.StartLower()); var cmdArg = arguments.Add(handler.RequestType.Name.EndsWithSingleSuffix("Request", "Command"), "request", "FromBody"); // this is update action // lets check if this put or post. // Put assumes that we have command starting with name "Update" or "Modify" if (handler.RequestType.Name.StartsWithPrefixes("Update", "Modify", "Edit")) { // this is pure PUT // Id is taken from command.id // ResultResponse Put(int id, updaterequest request); var responseType = handler.ResponseType.Name.EndsWithSingleSuffix("Response", "Result"); builder.AppendAction(handler, "Put", responseType, HttpVerb.Put, false, $"api/{handler.Service.Name}/{{{idArg.Name}}}", arguments.ToArray()); srcUnit.Responses.Append(GenerateResponseType(responseType, handler.ResponseType)); srcUnit.ResultToResponseMappers.Append(GenerateResultToResponseMapper(responseType, handler.ResponseType)); srcUnit.Requests.Append(GenerateRequestType(cmdArg.Type, handler.RequestType, prop.PropertyType, prop.Name)); srcUnit.RequestToCommandMappers.Append(GenerateRequestToCommandMapper(cmdArg.Type, handler.RequestType, prop.PropertyType, prop.Name)); builder.AddUsing(handler.ResponseType.Namespace + ".WebApi"); builder.AddUsing(handler.RequestType.Namespace + ".WebApi"); builder.AddUsing(handler.ResponseType.Namespace); builder.AddUsing(handler.RequestType.Namespace); } else { // This is custom method var responseType = handler.ResponseType.Name.EndsWithSingleSuffix("Response", "Result"); string name = handler.RequestType.Name.RemoveSuffixWords("Command").RemoveWords(handler.Service.Name); builder.AppendAction(handler, name, responseType, HttpVerb.Post, handler.IsResponseCollection, $"api/{handler.Service.Name}/{{{idArg.Name}}}/{name}", arguments.ToArray()); srcUnit.Responses.Append(GenerateResponseType(responseType, handler.ResponseType)); srcUnit.ResultToResponseMappers.Append(GenerateResultToResponseMapper(responseType, handler.ResponseType)); srcUnit.Requests.Append(GenerateRequestType(cmdArg.Type, handler.RequestType, prop.PropertyType, prop.Name)); srcUnit.RequestToCommandMappers.Append(GenerateRequestToCommandMapper(cmdArg.Type, handler.RequestType, prop.PropertyType, prop.Name)); builder.AddUsing(handler.ResponseType.Namespace + ".WebApi"); builder.AddUsing(handler.RequestType.Namespace + ".WebApi"); builder.AddUsing(handler.ResponseType.Namespace); builder.AddUsing(handler.RequestType.Namespace); } } else { ArgumentCollection arguments = new ArgumentCollection(); var cmdArg = arguments.Add(handler.RequestType.Name.EndsWithSingleSuffix("Request", "Command"), "request", "FromBody"); // this might be create method if (handler.RequestType.Name.StartsWithPrefixes("Create", "Insert", "Add")) { var responseType = handler.ResponseType.Name.EndsWithSingleSuffix("Response", "Result"); builder.AppendAction(handler, "Post", responseType, HttpVerb.Post, handler.IsResponseCollection, $"api/{handler.Service.Name}", arguments.ToArray()); srcUnit.Responses.Append(GenerateResponseType(responseType, handler.ResponseType)); srcUnit.ResultToResponseMappers.Append(GenerateResultToResponseMapper(responseType, handler.ResponseType)); srcUnit.Requests.Append(GenerateRequestType(cmdArg.Type, handler.RequestType)); srcUnit.RequestToCommandMappers.Append(GenerateRequestToCommandMapper(cmdArg.Type, handler.RequestType)); builder.AddUsing(handler.ResponseType.Namespace + ".WebApi"); builder.AddUsing(handler.RequestType.Namespace + ".WebApi"); builder.AddUsing(handler.ResponseType.Namespace); builder.AddUsing(handler.RequestType.Namespace); } else { // This is unkown method. var responseType = handler.ResponseType.Name.EndsWithSingleSuffix("Response", "Result"); string name = handler.RequestType.Name.RemoveSuffixWords("Command").RemoveWords(handler.Service.Name); builder.AppendAction(handler, name, responseType, HttpVerb.Post, handler.IsResponseCollection, $"api/{handler.Service.Name}/{name}", arguments.ToArray()); srcUnit.Responses.Append(GenerateResponseType(responseType, handler.ResponseType)); srcUnit.ResultToResponseMappers.Append(GenerateResultToResponseMapper(responseType, handler.ResponseType)); srcUnit.Requests.Append(GenerateRequestType(cmdArg.Type, handler.RequestType)); srcUnit.RequestToCommandMappers.Append(GenerateRequestToCommandMapper(cmdArg.Type, handler.RequestType)); builder.AddUsing(handler.ResponseType.Namespace + ".WebApi"); builder.AddUsing(handler.RequestType.Namespace + ".WebApi"); builder.AddUsing(handler.ResponseType.Namespace); builder.AddUsing(handler.RequestType.Namespace); } } }