public void ShouldSerializeArrayRamlType()
        {
            var ramlTypesOrderedDictionary = new RamlTypesOrderedDictionary();
            ramlTypesOrderedDictionary.Add("Person", new RamlType
            {
                Object = new ObjectType
                {
                    Properties = new Dictionary<string, RamlType>()
                    {
                        {"firstname", new RamlType { Type = "string"}},
                        {"lastname", new RamlType { Type = "string", Required = true }}
                    }
                }
            });

            ramlTypesOrderedDictionary.Add("Persons", new RamlType
            {
                Array = new ArrayType
                {
                    Items = new RamlType
                    {
                        Type = "Person"
                    }
                }
            });
            var doc = new RamlDocument
            {
                Types = ramlTypesOrderedDictionary
            };
            var res = serializer.Serialize(doc);
            Assert.IsTrue(res.Contains("  - Persons:" + Environment.NewLine));
            Assert.IsTrue(res.Contains("      type: Person[]" + Environment.NewLine));
        }
        private static SecurityScheme GetSchemeWithType(RamlDocument ramlDocument, string type)
        {

            return ramlDocument.SecuritySchemes.First(x => x.Values.Any(y => y.Type.First().Key == type))
                .Values
                .First(y => y.Type.First().Key == type);
        }
		private IEnumerable<Property> GetSecurityParameters(RamlDocument ramlDocument, Method method)
		{
			var securityParams = new Collection<Property>();
			if (ramlDocument.SecuritySchemes == null || !ramlDocument.SecuritySchemes.Any())
				return securityParams;

			if ((ramlDocument.SecuredBy == null || !ramlDocument.SecuredBy.Any())
				&& (method.SecuredBy == null || !method.SecuredBy.Any()))
				return securityParams;

			var securedBy = method.SecuredBy != null && method.SecuredBy.Any() ? method.SecuredBy : ramlDocument.SecuredBy;

			if (securedBy == null)
				return securityParams;

			var secured = securedBy.First();

			var dic = ramlDocument.SecuritySchemes.FirstOrDefault(s => s.ContainsKey(secured));
			if (dic == null)
				return securityParams;

			var descriptor = ramlDocument.SecuritySchemes.First(s => s.ContainsKey(secured))[secured].DescribedBy;
			if (descriptor == null || descriptor.QueryParameters == null || !descriptor.QueryParameters.Any())
				return securityParams;

			return QueryParametersParser.ConvertParametersToProperties(descriptor.QueryParameters);
		}
        private void SetPreview(RamlDocument document)
        {
            Dispatcher.Invoke(() =>
            {
                try
                {
                    
                    ResourcesLabel.Text = GetResourcesPreview(document);
                    StopProgress();
                    btnOk.IsEnabled = true;
                    SetNamespace(RamlTempFilePath);

                    if (NetNamingMapper.HasIndalidChars(txtFileName.Text))
                    {
                        ShowErrorStopProgressAndDisableOk("The specied file name has invalid chars");
                        txtFileName.Focus();
                    }
                }
                catch (Exception ex)
                {
                    ShowErrorStopProgressAndDisableOk("Error while parsing raml file. " + ex.Message);
                    ActivityLog.LogError(VisualStudioAutomationHelper.RamlVsToolsActivityLogSource, VisualStudioAutomationHelper.GetExceptionInfo(ex));
                }
            });
        }
	    protected MethodsGeneratorBase(RamlDocument raml, IDictionary<string, ApiObject> schemaResponseObjects,
            IDictionary<string, ApiObject> schemaRequestObjects, IDictionary<string, string> linkKeysWithObjectNames)
        {
            this.raml = raml;
            this.schemaResponseObjects = schemaResponseObjects;
	        this.schemaRequestObjects = schemaRequestObjects;
	        this.linkKeysWithObjectNames = linkKeysWithObjectNames;
        }
 private static bool AreOAuth2EndpointsDefined(RamlDocument ramlDocument)
 {
     return ramlDocument.SecuritySchemes
         .Any(x => x.Values
             .Any(y => y.Settings != null
                       && !string.IsNullOrWhiteSpace(y.Settings.AuthorizationUri)
                       && !string.IsNullOrWhiteSpace(y.Settings.AccessTokenUri)));
 }
        public void Should_Build_Uri_Parameters_From_Resources()
        {
            var doc = new RamlDocument { Title = "test" };

            var uriParameters = new Dictionary<string, Parameter>
                                {
                                    {
                                        "code", new Parameter
                                                {
                                                    DisplayName = "code",
                                                    Type = "integer"
                                                }
                                    },
                                    {
                                        "token", new Parameter
                                                 {
                                                     DisplayName = "token",
                                                     Type = "string"
                                                 }
                                    }
                                };

            var methods = new List<Method>
                          {
                              new Method
                              {
                                  Verb = "get"
                              }
                          };

            var resources = new Collection<Resource>
                            {
                                new Resource
                                {
                                    RelativeUri = "/movies/{token}/abc/{code}/asd/{extra}",
                                    Methods = methods,
                                    UriParameters = uriParameters
                                },
                                new Resource
                                {
                                    RelativeUri = "/directors/{a}/abc/{b}{c}/asd/{d}",
                                    Methods = methods
                                }
                            };

            doc.Resources = resources;

            var service = new ClientGeneratorService(doc, "test");
            var model = service.BuildModel();
            Assert.AreEqual(2, model.UriParameterObjects.Count);

            var moviesUriParamObj = model.UriParameterObjects.First(o => o.Key.ToLowerInvariant().Contains("movies")).Value;
            var directorsUriParamObj = model.UriParameterObjects.First(o => o.Key.ToLowerInvariant().Contains("directors")).Value;

            Assert.AreEqual(3, moviesUriParamObj.Properties.Count);
            Assert.AreEqual(4, directorsUriParamObj.Properties.Count);
        }
        public void should_build_uri_parameter_objects()
        {
            var doc = new RamlDocument { Title = "test" };

            var uriParams = new Dictionary<string, Parameter>()
                            {
                                {
                                    "code", new Parameter
                                            {
                                                DisplayName = "code",
                                                Type = "integer"
                                            }
                                },
                                {
                                    "token", new Parameter
                                             {
                                                 DisplayName = "token",
                                                 Type = "string"
                                             }
                                }
                            };



            var methods = new List<Method>
                          {
                              new Method
                              {
                                  Verb = "get",
                              },
                              new Method
                              {
                                  Verb = "post"
                              }
                          };

            var resources = new Collection<Resource>
                            {
                                new Resource
                                {
                                    RelativeUri = "/movies",
                                    UriParameters = uriParams,
                                    Methods = methods
                                }
                            };

            doc.Resources = resources;

            var service = new ClientGeneratorService(doc, "test");
            var model = service.BuildModel();

            Assert.AreEqual(1, model.UriParameterObjects.Count);
            Assert.AreEqual(2, model.UriParameterObjects.First().Value.Properties.Count);
            Assert.IsTrue(model.Classes.First().Methods.All(m => m.UriParametersType == "MoviesUriParameters"));
            Assert.AreEqual(1, model.Objects.Count());
        }
        public void should_keep_original_names()
        {
            var doc = new RamlDocument { Title = "test" };

            var uriParams = new Dictionary<string, Parameter>()
                            {
                                {
                                    "code-oneOne", new Parameter
                                            {
                                                DisplayName = "code",
                                                Type = "integer"
                                            }
                                },
                                {
                                    "token_Two", new Parameter
                                             {
                                                 DisplayName = "token",
                                                 Type = "string"
                                             }
                                }
                            };



            var methods = new List<Method>
                          {
                              new Method
                              {
                                  Verb = "get",
                              },
                              new Method
                              {
                                  Verb = "post"
                              }
                          };

            var resources = new Collection<Resource>
                            {
                                new Resource
                                {
                                    RelativeUri = "/movies",
                                    UriParameters = uriParams,
                                    Methods = methods
                                }
                            };

            doc.Resources = resources;

            var service = new ClientGeneratorService(doc, "test");
            var model = service.BuildModel();

            Assert.AreEqual("code-oneOne", model.UriParameterObjects.First().Value.Properties.First(p => p.Name == "CodeoneOne").OriginalName);
            Assert.AreEqual("token_Two", model.UriParameterObjects.First().Value.Properties.First(p => p.Name == "Token_Two").OriginalName);
        }
 public ClientMethodsGenerator(RamlDocument raml, IDictionary<string, ApiObject> schemaResponseObjects, 
     IDictionary<string, ApiObject> uriParameterObjects, IDictionary<string, ApiObject> queryObjects, 
     IDictionary<string, ApiObject> headerObjects, IDictionary<string, ApiObject> responseHeadersObjects,
     IDictionary<string, ApiObject> schemaRequestObjects, IDictionary<string, string> linkKeysWithObjectNames)
     : base(raml, schemaResponseObjects, schemaRequestObjects, linkKeysWithObjectNames)
 {
     this.uriParameterObjects = uriParameterObjects;
     this.queryObjects = queryObjects;
     this.headerObjects = headerObjects;
     this.responseHeadersObjects = responseHeadersObjects;
 }
 public RamlDocWrapper(string ramlFilePath)
 {
     //MuleSoft Raml parser
     var parser = new RamlParser();
     try
     {
         ramlDocument = parser.LoadAsync(ramlFilePath).Result;
     }
     catch (AggregateException ae)            //Exceptions received from assync RamlParser
     {
         throw ae.Flatten();
     }
 }
        public string Serialize(RamlDocument ramlDocument)
        {
            var sb = new StringBuilder(ramlDocument.Resources.Count + ramlDocument.Resources.Sum(r => r.Resources.Count) * 20);

            sb.AppendLine("#%RAML " + (ramlDocument.RamlVersion == RamlVersion.Version08 ? "0.8" : "1.0"));

            RamlSerializerHelper.SerializeProperty(sb, "title", ramlDocument.Title);

            RamlSerializerHelper.SerializeProperty(sb, "baseUri", ramlDocument.BaseUri);

            RamlSerializerHelper.SerializeProperty(sb, "version", ramlDocument.Version);

            RamlSerializerHelper.SerializeProperty(sb, "mediaType", ramlDocument.MediaType);

            SerializeArrayProperty(sb, "securedBy", ramlDocument.SecuredBy);

            SerializeProtocols(sb, ramlDocument.Protocols);

            SerializeParameters(sb, "uriParameters", ramlDocument.BaseUriParameters);

            if (ramlDocument.Documentation.Any())
            {
                sb.AppendLine("documentation:");
                foreach (var docItem in ramlDocument.Documentation)
                {
                    RamlSerializerHelper.SerializeProperty(sb, "- title", docItem.Title, 2);
                    RamlSerializerHelper.SerializeMultilineProperty(sb, "content", docItem.Content, 4);
                    raml1TypesSerializer.SerializeAnnotations(sb, docItem.Annotations, 4);
                }
                sb.AppendLine();
            }

            SerializeSecuritySchemes(sb, ramlDocument.SecuritySchemes);

            raml1TypesSerializer.Serialize(sb, ramlDocument.Types);

            SerializeSchemas(sb, ramlDocument.Schemas);

            SerializeResources(sb, ramlDocument.Resources);

            SerializeAnnotationTypes(sb, ramlDocument.AnnotationTypes);

            raml1TypesSerializer.SerializeAnnotations(sb, ramlDocument.Annotations);

            return sb.ToString();
        }
		public RamlDocument Build(IDictionary<string, object> dynamicRaml)
		{
			var doc = new RamlDocument(dynamicRaml);
			doc.BaseUri = (string) dynamicRaml["baseUri"];
			doc.Title = (string)dynamicRaml["title"];
			doc.Version = dynamicRaml.ContainsKey("version") ? (string)dynamicRaml["version"] : null;
			doc.MediaType = dynamicRaml.ContainsKey("mediaType") ? (string)dynamicRaml["mediaType"] : null;
			doc.Documentation = GetDocumentation(dynamicRaml);
			doc.BaseUriParameters = ParametersBuilder.GetUriParameters(dynamicRaml, "baseUriParameters");
			doc.SecuredBy = GetSecuredBy(dynamicRaml);
			doc.Method = dynamicRaml.ContainsKey("method") ? (string)dynamicRaml["method"] : null;
			doc.Protocols = ProtocolsBuilder.Get(dynamicRaml);
			doc.SecuritySchemes = GetSecuritySchemes(dynamicRaml);
			doc.ResourceTypes = GetResourceTypes(dynamicRaml);
			doc.Traits = GetTraits(dynamicRaml);
			doc.Schemas = GetSchemas(dynamicRaml);
			doc.Resources = GetResources(dynamicRaml);
			return doc;
		}
        public static Security GetSecurity(RamlDocument ramlDocument)
        {
            if (ramlDocument.SecuritySchemes == null || !ramlDocument.SecuritySchemes.Any())
                return null;

            SecurityScheme securityScheme = null;
            if (IsTypeDefined(ramlDocument, OAuth2Type))
            {
                securityScheme = GetSchemeWithType(ramlDocument, OAuth2Type);
            }
            else if (AreOAuth2EndpointsDefined(ramlDocument))
            {
                securityScheme = GetSchemeWithOAuth2EndpointsDefined(ramlDocument);
            }
            else if (IsTypeDefined(ramlDocument, OAuth1Type))
            {
                securityScheme = GetSchemeWithType(ramlDocument, OAuth1Type);
            }

            if (securityScheme == null || (securityScheme.Settings == null && securityScheme.DescribedBy == null))
                return new Security();

            var settings = securityScheme.Settings;
            var schemeDescriptor = securityScheme.DescribedBy;

            return new Security
                   {
                       AccessTokenUri = settings == null ? null : settings.AccessTokenUri,
                       AuthorizationGrants = settings == null ? null : settings.AuthorizationGrants,
                       AuthorizationUri = settings == null ? null : settings.AuthorizationUri,
                       Scopes = settings == null ? null : settings.Scopes,
                       RequestTokenUri = settings == null ? null : settings.RequestTokenUri,
                       TokenCredentialsUri = settings == null ? null : settings.TokenCredentialsUri,
                       Headers = schemeDescriptor == null || schemeDescriptor.Headers == null
                           ? new List<GeneratorParameter>()
                           : ParametersMapper.Map(schemeDescriptor.Headers),
                       QueryParameters = schemeDescriptor == null || schemeDescriptor.QueryParameters == null
                           ? new List<GeneratorParameter>()
                           : ParametersMapper.Map(schemeDescriptor.QueryParameters)
                   };
        }
        public void Should_Generate_One_Method_Per_Verb()
        {
            var methods = new List<Method>
                          {
                              new Method
                              {
                                  Verb = "get"
                              },
                              new Method
                              {
                                  Verb = "post"
                              }
                          };

            var resource = new Resource
            {
                RelativeUri = "/abc{token}{code}",
                Methods = methods,
            };

            var schemaResponseObjects = new Dictionary<string, ApiObject>();
            var schemaRequestObjects = new Dictionary<string, ApiObject>();
            var ramlDocument = new RamlDocument();
            var uriParameterObjects = new Dictionary<string, ApiObject>();
            var queryObjects = new Dictionary<string, ApiObject>();
            var headerObjects = new Dictionary<string, ApiObject>();
            var responseHeadersObjects = new Dictionary<string, ApiObject>();
            var linkedKeyWithObjectNames = new Dictionary<string, string>();
            var classObject = new ClassObject();
            var schemaObjects = new Dictionary<string, ApiObject>();

            var generator = new ClientMethodsGenerator(ramlDocument, schemaResponseObjects,
                uriParameterObjects, queryObjects, headerObjects, responseHeadersObjects, schemaRequestObjects, linkedKeyWithObjectNames,
                schemaObjects);

            var generatorMethods = generator.GetMethods(resource, "/", classObject, "Test", new Dictionary<string, Parameter>());

            Assert.AreEqual(2, generatorMethods.Count);
        }
		private void OrganizeResourcesHierarchically(RamlDocument raml, Dictionary<string, Resource> resourcesDic)
		{
			foreach (var kv in resourcesDic)
			{
				var matchingResources = resourcesDic.Where(r => r.Key != kv.Key && kv.Key.StartsWith(r.Key + "/"));
				if (matchingResources.Any())
				{
					var parent = matchingResources.OrderByDescending(r => r.Key.Length).First();
					kv.Value.RelativeUri = kv.Value.RelativeUri.Substring(parent.Key.Length); // remove parent route from relative uri
					parent.Value.Resources.Add(kv.Value);
				}
				else
				{
					raml.Resources.Add(kv.Value);
				}
			}
		}
		private void SetSecurityScheme(RamlDocument raml)
		{
			var securitySchemes = new List<IDictionary<string, SecurityScheme>>();

			if (raml.SecuritySchemes != null && raml.SecuritySchemes.Any())
				securitySchemes = raml.SecuritySchemes.ToList();

			var schemes = new Dictionary<string, SecurityScheme> { { securityType, securityScheme } };
			securitySchemes.Add(schemes);

			raml.SecuritySchemes = securitySchemes;
		}
		public WebApiGeneratorService(RamlDocument raml) : base(raml)
		{
		}
 protected GeneratorServiceBase(RamlDocument raml, string targetNamespace)
 {
     this.raml = raml;
     this.targetNamespace = targetNamespace;
     apiObjectsCleaner = new ApiObjectsCleaner(schemaRequestObjects, schemaResponseObjects, schemaObjects);
 }
        public WebApiMethodsGenerator(RamlDocument raml, IDictionary<string, ApiObject> schemaResponseObjects, 
            IDictionary<string, ApiObject> schemaRequestObjects, IDictionary<string, string> linkKeysWithObjectNames)
            : base(raml, schemaResponseObjects, schemaRequestObjects, linkKeysWithObjectNames)
		{
		}
        public void Should_Inherit_Uri_Parameters_When_Nested_Resources_ClientCase2()
        {
            var doc = new RamlDocument { Title = "test" };

            var uriParameters1 = new Dictionary<string, Parameter>
                                {
                                    {
                                        "id", new Parameter
                                                {
                                                    DisplayName = "id",
                                                    Type = "integer"
                                                }
                                    }
                                };

            var uriParameters2 = new Dictionary<string, Parameter>
                                {
                                    {
                                        "current", new Parameter
                                                 {
                                                     DisplayName = "current",
                                                     Type = "boolean"
                                                 }
                                    }
                                };

            var methods = new List<Method>
                          {
                              new Method
                              {
                                  Verb = "get"
                              }
                          };

            var resources = new Collection<Resource>
            {
                new Resource
                {
                    RelativeUri = "/{id}",
                    Methods = methods,
                    UriParameters = uriParameters1,
                    Resources = new Collection<Resource>
                    {
                        new Resource
                        {
                            RelativeUri = "/something",
                            Methods = methods,
                            Resources = new Collection<Resource>
                            {
                                new Resource
                                {
                                    RelativeUri = "/{current}",
                                    Methods = methods,
                                    UriParameters = uriParameters2,
                                    Resources = new Collection<Resource>
                                    {
                                        new Resource
                                        {
                                            RelativeUri = "/deep",
                                            Methods = methods
                                        }
                                    }

                                }
                            }
                        }
                    }
                }
            };

            doc.Resources = resources;

            var service = new ClientGeneratorService(doc, "test", "TargetNamespace");
            var model = service.BuildModel();
            Assert.AreEqual("bool", model.Classes.First(m => m.Name.Contains("Deep")).Methods.First().UriParameters.First(p => p.Name == "current").Type);
            Assert.AreEqual("int", model.Classes.First(m => m.Name.Contains("Deep")).Methods.First().UriParameters.First(p => p.Name == "id").Type);
        }
 private static bool IsTypeDefined(RamlDocument ramlDocument, string type)
 {
     return ramlDocument.SecuritySchemes.Any(x => x.Values.Any(y => y.Type.First().Key == type));
 }
        public void Should_Build_Uri_Parameters_When_Nested_Resources()
        {
            var doc = new RamlDocument { Title = "test" };

            var uriParameters = new Dictionary<string, Parameter>
                                {
                                    {
                                        "code", new Parameter
                                                {
                                                    DisplayName = "code",
                                                    Type = "integer"
                                                }
                                    },
                                    {
                                        "token", new Parameter
                                                 {
                                                     DisplayName = "token",
                                                     Type = "string"
                                                 }
                                    }
                                };

            var methods = new List<Method>
                          {
                              new Method
                              {
                                  Verb = "get"
                              }
                          };

            var resources = new Collection<Resource>
                            {
                                new Resource
                                {
                                    RelativeUri = "/movies/{token}{extra}/abc/{code}{extra}/asd",
                                    Methods = methods,
                                    UriParameters = uriParameters,
                                    Resources = new[]
                                                {
                                                    new Resource
                                                    {
                                                        RelativeUri = "/{a}{b}",
                                                        Methods = methods
                                                    }

                                                }
                                }
                            };

            doc.Resources = resources;

            var service = new ClientGeneratorService(doc, "test");
            var model = service.BuildModel();
            Assert.AreEqual(2, model.UriParameterObjects.Count);

            var moviesUriParamObj = model.UriParameterObjects.First().Value;
            var directorsUriParamObj = model.UriParameterObjects.Last().Value;

            Assert.AreEqual(3, moviesUriParamObj.Properties.Count);
            Assert.AreEqual(5, directorsUriParamObj.Properties.Count);
        }
 public ClientGeneratorService(RamlDocument raml, string rootClassName)
     : base(raml)
 {
     this.rootClassName = rootClassName;
 }
        public void Should_Get_Multiple_Type_When_Many_Response_Schemas_On_Resource()
        {
            var doc = new RamlDocument { Title = "test" };

            var okResponse = new Response
            {
                Code = "200",
                Body = new Dictionary<string, MimeType>
                                        {
                                            {
                                                "application/json", new MimeType
                                                                    {
                                                                        Schema = movieSchema
                                                                    }
                                            }
                                        }
            };

            var errorResponse = new Response
            {
                Code = "400",
                Body = new Dictionary<string, MimeType>
                                           {
                                               {
                                                   "application/json", new MimeType
                                                                       {
                                                                           Schema = errorSchema
                                                                       }
                                               }
                                           }
            };

            var notFoundResponse = new Response
            {
                Code = "404",
                Body = new Dictionary<string, MimeType>
                                              {
                                                  {
                                                      "application/json", new MimeType
                                                                          {
                                                                              Schema = notFoundSchema
                                                                          }
                                                  }
                                              }
            };

            var methods = new List<Method>
                          {
                              new Method
                              {
                                  Verb = "get",
                                  Responses = new[] { okResponse, errorResponse, notFoundResponse }
                              }
                          };

            var resources = new Collection<Resource>
                            {
                                new Resource
                                {
                                    RelativeUri = "/movies",
                                    Methods = methods
                                }
                            };

            doc.Resources = resources;

            var service = new ClientGeneratorService(doc, "test", "TestNs");
            var model = service.BuildModel();
            Assert.AreEqual(4, model.ResponseObjects.Count);

            var multipleModel = model.ResponseObjects.First(o => o.Key.Contains("Multiple")).Value;

            Assert.AreEqual(3, multipleModel.Properties.Count);
        }
		protected GeneratorServiceBase(RamlDocument raml)
		{
			this.raml = raml;
			apiObjectsCleaner = new ApiObjectsCleaner(schemaRequestObjects, schemaResponseObjects);
		}
		public RamlDocument GetRaml(string title = null)
		{
			if (string.IsNullOrWhiteSpace(title))
				title = "Api";

			var raml = new RamlDocument {Title = title, BaseUri = baseUri};
			var resourcesDic = new Dictionary<string, Resource>();
			var parameterDescriptionsDic = new Dictionary<string, Collection<ApiParameterDescription>>();
			foreach (var api in apiExplorer.ApiDescriptions)
			{
				var relativeUri = !api.Route.RouteTemplate.StartsWith("/") ? "/" + api.Route.RouteTemplate : api.Route.RouteTemplate;
				if (relativeUri.Contains("{controller}"))
				{
					relativeUri = relativeUri.Replace("{controller}", api.ActionDescriptor.ControllerDescriptor.ControllerName);

					if (relativeUri.EndsWith("/"))
						relativeUri = relativeUri.Substring(0, relativeUri.Length - 1);
				}

				foreach (var apiParam in GetParametersFromUrl(relativeUri))
				{
					relativeUri = RemoveNonExistingParametersFromRoute(relativeUri, api, apiParam.Key);
				}

				Resource resource;
				if (!resourcesDic.ContainsKey(relativeUri))
				{
					resource = new Resource
					               {
						               Methods = GetMethods(api, new Collection<string>()),
									   RelativeUri = relativeUri,
					               };
					parameterDescriptionsDic.Add(relativeUri, api.ParameterDescriptions);
					resourcesDic.Add(relativeUri, resource);
				}
				else
				{
					resource = resourcesDic[relativeUri];
					foreach (var apiParameterDescription in api.ParameterDescriptions)
					{
						parameterDescriptionsDic[relativeUri].Add(apiParameterDescription);
					}
					AddMethods(resource, api, resource.Methods.Select(m => m.Verb).ToList());
				}

				if(SetResourceProperties != null)
					SetResourceProperties(resource, api);

				if(SetResourcePropertiesByAction != null)
					SetResourcePropertiesByAction(resource, api.ActionDescriptor);

				if(SetResourcePropertiesByController != null)
					SetResourcePropertiesByController(resource, api.ActionDescriptor.ControllerDescriptor);
			}

		    raml.Schemas = new List<IDictionary<string, string>> { schemas };

			OrganizeResourcesHierarchically(raml, resourcesDic);

			SetUriParameters(raml.Resources, parameterDescriptionsDic, string.Empty);

			if(SetRamlProperties != null)
				SetRamlProperties(raml);

			if (SecuritySchemes != null)
				raml.SecuritySchemes = SecuritySchemes;
			
			if (!string.IsNullOrWhiteSpace(securityType) && securityScheme != null)
				SetSecurityScheme(raml);

			if (SecuredBy != null)
				raml.SecuredBy = SecuredBy;

			if(Protocols != null)
				raml.Protocols = Protocols;

			return raml;
		}
 private static string GetResourcesPreview(RamlDocument ramlDoc)
 {
     return GetChildResources(ramlDoc.Resources, 0);
 }
        public void Should_Build_Multiple_Headers_When_Many_Response_With_Headers()
        {
            var doc = new RamlDocument {Title = "test"};

            var okHeaders = new List<Parameter>
                            {
                                new Parameter
                                {
                                    DisplayName = "code",
                                    Type = "integer"
                                },
                                new Parameter
                                {
                                    DisplayName = "token",
                                    Type = "string"
                                }
                            };

            var errorHeaders = new List<Parameter>
                            {
                                new Parameter
                                {
                                    DisplayName = "code",
                                    Type = "integer"
                                },
                                new Parameter
                                {
                                    DisplayName = "error",
                                    Type = "string"
                                }
                            };

            var badRequestHeaders = new List<Parameter>
                            {
                                new Parameter
                                {
                                    DisplayName = "code",
                                    Type = "integer"
                                },
                                new Parameter
                                {
                                    DisplayName = "description",
                                    Type = "string"
                                }
                            };

            var okResponse = new Response
                             {
                                 Code = "200",
                                 Headers = okHeaders
                             };

            var errorResponse = new Response
                                {
                                    Code = "400",
                                    Headers = errorHeaders
                                };

            var notFoundResponse = new Response
                                   {
                                       Code = "404",
                                       Headers = badRequestHeaders
                                   };

            var methods = new List<Method>
                          {
                              new Method
                              {
                                  Verb = "get",
                                  Responses = new[] { okResponse, errorResponse, notFoundResponse }
                              }
                          };

            var resources = new Collection<Resource>
                            {
                                new Resource
                                {
                                    RelativeUri = "movies",
                                    Methods = methods
                                }
                            };

            doc.Resources = resources;

            var service = new ClientGeneratorService(doc, "test", "TargetNamespace");
            var model = service.BuildModel();
            Assert.AreEqual(4, model.ResponseHeaderObjects.Count);

            var multipleModel = model.ResponseHeaderObjects.First(o => o.Key.Contains("Multiple")).Value;

            Assert.AreEqual(3, multipleModel.Properties.Count);

            Assert.AreEqual("Models." + model.ResponseHeaderObjects.First(o => o.Key.Contains("Multiple")).Value.Name,
                model.Classes.First().Methods.First().ResponseHeaderType);
        }
        public void Should_Get_Multiple_Type_When_Many_Response_Schemas_On_Root()
        {
            var doc = new RamlDocument {Title = "test"};
            var schemas = new List<Dictionary<string, string>>();
            var schemaMovie = new Dictionary<string, string>();
            schemaMovie.Add("movie", movieSchema);
            var schemaError = new Dictionary<string, string>();
            schemaMovie.Add("error", errorSchema);
            var schemaNotFound = new Dictionary<string, string>();
            schemaMovie.Add("notfound", notFoundSchema);
            schemas.Add(schemaMovie);
            schemas.Add(schemaError);
            schemas.Add(schemaNotFound);
            doc.Schemas = schemas;

            var okResponse = new Response
                             {
                                 Code = "200",
                                 Body = new Dictionary<string, MimeType>
                                        {
                                            {
                                                "application/json", new MimeType
                                                                    {
                                                                        Schema = "movie"
                                                                    }
                                            }
                                        }
                             };

            var errorResponse = new Response
                                {
                                    Code = "400",
                                    Body = new Dictionary<string, MimeType>
                                           {
                                               {
                                                   "application/json", new MimeType
                                                                       {
                                                                           Schema = "error"
                                                                       }
                                               }
                                           }
                                };

            var notFoundResponse = new Response
                                   {
                                       Code = "404",
                                       Body = new Dictionary<string, MimeType>
                                              {
                                                  {
                                                      "application/json", new MimeType
                                                                          {
                                                                              Schema = "notfound"
                                                                          }
                                                  }
                                              }
                                   };

            var methods = new List<Method>
                          {
                              new Method
                              {
                                  Verb = "get",
                                  Responses = new[] { okResponse, errorResponse, notFoundResponse }
                              }
                          };

            var resources = new Collection<Resource>
                            {
                                new Resource
                                {
                                    RelativeUri = "/movies",
                                    Methods = methods
                                }
                            };

            doc.Resources = resources;

            var service = new ClientGeneratorService(doc, "test", "TestNs");
            var model = service.BuildModel();
            Assert.AreEqual(1, model.ResponseObjects.Count);
            Assert.AreEqual(3, model.SchemaObjects.Count);

            var multipleModel = model.ResponseObjects.First(o => o.Key.Contains("Multiple")).Value;

            Assert.AreEqual(3, multipleModel.Properties.Count);

            Assert.AreEqual(1, multipleModel.Properties.Count(p => p.Type == "Movie"));
            Assert.AreEqual(1, multipleModel.Properties.Count(p => p.Type == "Error"));
            Assert.AreEqual(1, multipleModel.Properties.Count(p => p.Type == "Notfound"));
        }