public void TestAddUriStructureOk() { try { ConfigJsonHandler configJsonHandler = new ConfigJsonHandler(); SchemaConfigFileOperations schemaConfigFileOperations = new SchemaConfigFileOperations(configJsonHandler); CreateBackUpConfig(); UriStructureGeneral uriSchema = configJsonHandler.GetUrisConfig(); UriStructure newUriStructure = CreateUriStructureExample("newUriExample"); ResourcesClass newResourcesClass = CreateResourceClassExample("newUriExample", "rsp", "pipaon"); int oldResourcesClassesCount = uriSchema.ResourcesClasses.Count; int oldUriStructuresCount = uriSchema.UriStructures.Count; configJsonHandler.AddUriStructureInfo(newUriStructure, newResourcesClass); schemaConfigFileOperations.SaveConfigJson(); ConfigJsonHandler configJsonHandler2 = new ConfigJsonHandler(); UriStructureGeneral uriSchema2 = configJsonHandler2.GetUrisConfig(); RestoreBackUpConfig(); configJsonHandler2.LoadConfigJson(); Assert.True(oldResourcesClassesCount + 1 == uriSchema2.ResourcesClasses.Count && oldUriStructuresCount + 1 == uriSchema2.UriStructures.Count); } catch (Exception) { RestoreBackUpConfig(); } }
///<summary> ///Devuelve una uri a partir de una resourceClass y y una lista de valores ///</summary> ///<param name="resourceClass">nombre de la resourceClass a usar para generar la uri</param> ///<param name="queryString">diccionario con los valores cogidos de la url de la petición</param> public string GetURI(string resourceClass, Dictionary <string, string> queryString) { string uri = ""; ResourcesClass resourceClassObject = ParserResourceClass(resourceClass); if (resourceClassObject != null) { string parsedLabelResourceClass = resourceClassObject.LabelResourceClass; string resourceURL = resourceClassObject.ResourceURI; UriStructure urlStructure = UriStructure.UriStructures.FirstOrDefault(structure => structure.Name.Equals(resourceURL)); if (urlStructure != null) { string parsedCharacter = ParserCharacter(urlStructure.Components.ToList()); if (!string.IsNullOrEmpty(parsedCharacter)) { uri = GetUriByStructure(urlStructure, parsedCharacter, parsedLabelResourceClass, queryString); } else { throw new ParametersNotConfiguredException($"Character for {resourceURL} not configured"); } } else { throw new ParametersNotConfiguredException($"Structure for {resourceURL} not configured"); } return(uri); } else { throw new ParametersNotConfiguredException($"resource class: '{resourceClass}' not configured"); } }
public void TestAddUriStructureOkController() { ConfigJsonHandler configJsonHandler = new ConfigJsonHandler(); ISchemaConfigOperations schemaConfigOperations = new SchemaConfigMemoryOperations(configJsonHandler); SchemaController schemaController = new SchemaController(configJsonHandler, schemaConfigOperations); UriStructure newUriStructure = CreateUriStructureExample("newUriExample"); ResourcesClass newResourcesClass = CreateResourceClassExample("newUriExample", "rsp", "pipaon"); List <ResourcesClass> lista = new List <ResourcesClass>(); lista.Add(newResourcesClass); InfoUriStructure structure = new InfoUriStructure(); structure.ResourcesClass = lista; structure.UriStructure = newUriStructure; var result = schemaController.AddUriStructure(structure); if (result is BadRequestObjectResult) { Assert.True(false); } else { Assert.True(true); } }
//Operations with the Schema ///<summary> ///Elimina del objeto de uris, una estructura uri con sus resourceClass asociadas ///</summary> ///<param name="uriStructure">estructura de uris a añadir</param> ///<param name="resourcesClass">lista de resource class asociadas a uriStructure</param> private void DeleteUriStructureInfo(UriStructure uriStructure, List <ResourcesClass> resourcesClass) { _uriSchema.UriStructures.Remove(uriStructure); foreach (ResourcesClass item in resourcesClass) { _uriSchema.ResourcesClasses.Remove(item); } }
public void TestAddUriStructureFailUriStructureBadInfoException() { ConfigJsonHandler configJsonHandler = new ConfigJsonHandler(); UriStructure newUriStructure = CreateUriStructureExample("newUriExample"); ResourcesClass newResourcesClass = CreateResourceClassExample("newUriExample", "rsp", ""); Assert.Throws <UriStructureBadInfoException>(() => configJsonHandler.AddUriStructureInfo(newUriStructure, newResourcesClass)); }
///<summary> ///Genera una uri ///</summary> ///<param name="urlStructure">estructura URL para la construcción de la uri</param> ///<param name="parsedCharacter">Character a usar pra la generación de la uri, este character debe estar configurado en el fichero de configuración</param> ///<param name="parsedResourceClass">etiqueta a mostrar en la uri de la resource class de la cual queremos generar la uri</param> ///<param name="queryString">diccionario con los valores cogidos de la url de la petición</param> private string GetUriByStructure(UriStructure urlStructure, string parsedCharacter, string parsedResourceClass, Dictionary <string, string> queryString) { string uri = ""; bool error = false; string errorMessage = ""; bool containsKey = false; foreach (Component component in urlStructure.Components.OrderBy(structure => structure.UriComponentOrder)) { string componentName = component.UriComponent; switch (componentName) { case UriComponentsList.Base: uri = $"{uri}{UriStructure.Base}{component.FinalCharacter}"; break; case UriComponentsList.Character: uri = $"{uri}{parsedCharacter}{component.FinalCharacter}"; break; case UriComponentsList.ResourceClass: uri = $"{uri}{parsedResourceClass}{component.FinalCharacter}"; break; case UriComponentsList.Identifier: containsKey = queryString.ContainsKey(UriComponentsList.Identifier); if (!containsKey && component.Mandatory) { error = true; } else if (containsKey) { string id = queryString[UriComponentsList.Identifier]; uri = $"{uri}{id}{component.FinalCharacter}"; } break; default: containsKey = queryString.ContainsKey(componentName); if (!containsKey && component.Mandatory) { error = true; errorMessage = $"{errorMessage} parameter {componentName} missing \n"; } else if (containsKey) { string componentVariable = queryString[componentName]; uri = $"{uri}{componentVariable}{component.FinalCharacter}"; } break; } } if (error) { throw new ParametersNotConfiguredException(errorMessage); } return(uri); }
public void TestAddUriStructureFailUriStructureConfiguredException() { try { ConfigJsonHandler configJsonHandler = new ConfigJsonHandler(); CreateBackUpConfig(); UriStructure newUriStructure = CreateUriStructureExample("uriResourceStructure"); ResourcesClass newResourcesClass = CreateResourceClassExample("uriResourceStructure", "rsp", ""); Assert.Throws <UriStructureConfiguredException>(() => configJsonHandler.AddUriStructureInfo(newUriStructure, newResourcesClass)); } catch (Exception) { RestoreBackUpConfig(); } }
public void TestAddUriStructureFailMatchNames() { try { ConfigJsonHandler configJsonHandler = new ConfigJsonHandler(); CreateBackUpConfig(); UriStructure newUriStructure = CreateUriStructureExample("newUriExamp"); ResourcesClass newResourcesClass = CreateResourceClassExample("newUriExample", "rsp", "pipaon"); Assert.Throws <UriStructureBadInfoException>(() => configJsonHandler.AddUriStructureInfo(newUriStructure, newResourcesClass)); } catch (Exception) { RestoreBackUpConfig(); } }
public void TestAddUriStructureOk() { ConfigJsonHandler configJsonHandler = new ConfigJsonHandler(); UriStructureGeneral uriSchema = configJsonHandler.GetUrisConfig(); UriStructure newUriStructure = CreateUriStructureExample("newUriExample"); ResourcesClass newResourcesClass = CreateResourceClassExample("newUriExample", "rsp", "pipaon"); int oldResourcesClassesCount = uriSchema.ResourcesClasses.Count; int oldUriStructuresCount = uriSchema.UriStructures.Count; configJsonHandler.AddUriStructureInfo(newUriStructure, newResourcesClass); ISchemaConfigOperations schemaConfigOperations = new SchemaConfigMemoryOperations(configJsonHandler); schemaConfigOperations.SaveConfigJson(); UriStructureGeneral uriSchema2 = configJsonHandler.GetUrisConfig(); Assert.True(oldResourcesClassesCount + 1 == uriSchema2.ResourcesClasses.Count && oldUriStructuresCount + 1 == uriSchema2.UriStructures.Count); }
public IActionResult GetUriStructureInfo(string name) { UriStructure uri = _configJsonHandler.GetUriStructure(name); if (uri != null) { List <ResourcesClass> resourceClass = _configJsonHandler.GetResourceClass(name); InfoUriStructure infoUriStructure = new InfoUriStructure(); infoUriStructure.UriStructure = uri; infoUriStructure.ResourcesClass = resourceClass; return(Ok(infoUriStructure)); } else { return(BadRequest($"{{\"error\": \"No data of uriStructure {name}\"}}")); } }
private UriStructure CreateUriStructureExample(string name) { UriStructure newUriStructure = new UriStructure() { Name = name, Components = new List <Component>() { new Component() { UriComponent = "base", UriComponentValue = "base", UriComponentOrder = 1, Mandatory = true, FinalCharacter = "/" }, new Component() { UriComponent = "character", UriComponentValue = "@RESOURCE", UriComponentOrder = 2, Mandatory = true, FinalCharacter = "/" }, new Component() { UriComponent = "resourceClass", UriComponentValue = "@RESOURCECLASS", UriComponentOrder = 3, Mandatory = true, FinalCharacter = "/" }, new Component() { UriComponent = "identifier", UriComponentValue = "@ID", UriComponentOrder = 4, Mandatory = true, FinalCharacter = "" } } }; return(newUriStructure); }
/// <summary> /// Creates the complete URI. /// </summary> /// <param name="uriStructure">The URI structure.</param> /// <param name="isRegex">if set to <c>true</c> the result should be is regex escaped.</param> /// <returns>The formatted URI.</returns> private static string CreateCompleteUri(UriStructure uriStructure, bool isRegex) { if (uriStructure.IsAbsolute) { return(uriStructure.Path); } var subPath = uriStructure.Path ?? string.Empty; var basePath = BaseUri.ToString().TrimEnd('/', ' '); if (isRegex) { basePath = Regex.Escape(basePath); } var seperator = subPath.StartsWith("/") ? string.Empty : "/"; return(string.Concat(basePath, seperator, subPath)); }
///<summary> ///Añade del objeto de uris una estructura de uris y una resource class asiciada a esa estrcutura ///</summary> ///<param name="uriStructure">estructura uri</param> ///<param name="resourcesClass">resource class asociada a uriStructure</param> ///<exception cref="UriStructureConfiguredException">UriStructure Already exist in config file</exception> ///<exception cref="UriStructureBadInfoException">there is a mismatch between uriStructure and resourceClass given</exception> public void AddUriStructureInfo(UriStructure uriStructure, ResourcesClass resourcesClass) { if (!_uriSchema.UriStructures.Any(uriStructures => uriStructures.Name.Equals(uriStructure)) && (!string.IsNullOrEmpty(uriStructure.Name) && uriStructure.Name.Equals(resourcesClass.ResourceURI)) && (uriStructure.Components.Count > 1 && !string.IsNullOrEmpty(resourcesClass.LabelResourceClass) && !string.IsNullOrEmpty(resourcesClass.ResourceClass))) { _uriSchema.UriStructures.Add(uriStructure); _uriSchema.ResourcesClasses.Add(resourcesClass); } else if (ExistUriStructure(uriStructure.Name)) { throw new UriStructureConfiguredException($"UriStructure {uriStructure.Name} already exist"); } else if (!uriStructure.Name.Equals(resourcesClass.ResourceURI)) { throw new UriStructureBadInfoException($"UriStructure name: {uriStructure.Name} and ResourcesClass ResourceURI: {resourcesClass.ResourceURI} no match"); } else { throw new UriStructureBadInfoException($"Data component is empty"); } }
///<summary> ///Genera una uri ///</summary> ///<param name="urlStructure">estructura URL para la construcción de la uri</param> ///<param name="parsedCharacter">Character a usar pra la generación de la uri, este character debe estar configurado en el fichero de configuración</param> ///<param name="resourceClassObject">Resource class object</param> ///<param name="queryString">diccionario con los valores cogidos de la url de la petición</param> private string GetUriByStructure(UriStructure urlStructure, string parsedCharacter, ResourcesClass resourceClassObject, Dictionary <string, string> queryString) { string uri = ""; bool error = false; string errorMessage = ""; bool containsKey = false; foreach (Component component in urlStructure.Components.OrderBy(structure => structure.UriComponentOrder)) { string componentName = component.UriComponent; switch (componentName) { case UriComponentsList.Base: uri = $"{uri}{UriStructure.Base}{component.FinalCharacter}"; break; case UriComponentsList.Character: uri = $"{uri}{parsedCharacter}{component.FinalCharacter}"; break; case UriComponentsList.ResourceClass: if (resourceClassObject != null && resourceClassObject.BlankNode == true) { uri = "N"; } else { uri = $"{uri}{resourceClassObject.LabelResourceClass}{component.FinalCharacter}"; } break; case UriComponentsList.Identifier: containsKey = queryString.ContainsKey(UriComponentsList.Identifier); string id = string.Empty; if (!containsKey && component.Mandatory) { error = true; } else if (containsKey && !string.IsNullOrEmpty(queryString[UriComponentsList.Identifier])) { id = queryString[UriComponentsList.Identifier]; } else { id = Guid.NewGuid().ToString(); } uri = $"{uri}{id}{component.FinalCharacter}"; break; default: containsKey = queryString.ContainsKey(componentName); if (!containsKey && component.Mandatory) { error = true; errorMessage = $"{errorMessage} parameter {componentName} missing \n"; } else if (containsKey) { string componentVariable = queryString[componentName]; uri = $"{uri}{componentVariable}{component.FinalCharacter}"; } break; } } if (error) { throw new ParametersNotConfiguredException(errorMessage); } return(uri); }
/// <summary> /// Creates the complete URI. /// </summary> /// <param name="uriStructure">The URI structure.</param> /// <param name="isRegex">if set to <c>true</c> the result should be is regex escaped.</param> /// <returns>The formatted URI.</returns> private static string CreateCompleteUri(UriStructure uriStructure, bool isRegex) { if (uriStructure.IsAbsolute) { return uriStructure.Path; } var subPath = uriStructure.Path ?? string.Empty; var basePath = BaseUri.ToString().TrimEnd('/', ' '); if (isRegex) { basePath = Regex.Escape(basePath); } var seperator = subPath.StartsWith("/") ? string.Empty : "/"; return string.Concat(basePath, seperator, subPath); }
public static void Get() { UriStructureGeneral structureGeneral = new UriStructureGeneral() { Base = "http://graph.um.es" }; List <Characters> characters = new List <Characters>(); Characters charac = new Characters() { Character = "resource", LabelCharacter = "res" }; characters.Add(charac); structureGeneral.Characters = characters; Component baseC = new Component() { UriComponent = "base", UriComponentValue = "base", UriComponentOrder = 1, Mandatory = true, FinalCharacter = "/" }; Component character = new Component() { UriComponent = "character", UriComponentValue = "character@resource", UriComponentOrder = 2, Mandatory = true, FinalCharacter = "/" }; Component resource = new Component() { UriComponent = "resourceClass", UriComponentValue = "resourceClass", UriComponentOrder = 3, Mandatory = true, FinalCharacter = "/" }; Component identifier = new Component() { UriComponent = "identifier", UriComponentValue = "@ID", UriComponentOrder = 4, Mandatory = true, FinalCharacter = "" }; List <Component> componentes = new List <Component>(); componentes.Add(baseC); componentes.Add(character); componentes.Add(resource); componentes.Add(identifier); UriStructure uriStructure = new UriStructure() { Name = "test", Components = componentes }; structureGeneral.UriStructures = new List <UriStructure>(); structureGeneral.UriStructures.Add(uriStructure); ResourcesClass clas = new ResourcesClass() { LabelResourceClass = "project-object", ResourceClass = "Project", ResourceURI = "test" }; ResourcesClass clas2 = new ResourcesClass() { LabelResourceClass = "researcher", ResourceClass = "Researcher", ResourceURI = "test" }; structureGeneral.ResourcesClasses = new List <ResourcesClass>(); structureGeneral.ResourcesClasses.Add(clas); structureGeneral.ResourcesClasses.Add(clas2); string uriSchemaJson = JsonConvert.SerializeObject(structureGeneral); ConfigJsonHandler config = new ConfigJsonHandler(uriSchemaJson); Dictionary <string, string> queryString = new Dictionary <string, string>(); queryString.Add("identifier", "123d"); UriFormer uriFormer = new UriFormer(config.GetUrisConfig()); string uri = uriFormer.GetURI("Project", queryString); Console.WriteLine(uri); }
public IActionResult Get() { UriStructureGeneral structureGeneral = new UriStructureGeneral() { Base = "http://graph.um.es" }; List <Characters> characters = new List <Characters>(); Characters charac = new Characters() { Character = "resource", LabelCharacter = "res" }; characters.Add(charac); structureGeneral.Characters = characters; Component baseC = new Component() { UriComponent = "base", UriComponentValue = "base", UriComponentOrder = 1, Mandatory = true, FinalCharacter = "/" }; Component character = new Component() { UriComponent = "character", UriComponentValue = "character@RESOURCE", UriComponentOrder = 2, Mandatory = true, FinalCharacter = "/" }; Component resource = new Component() { UriComponent = "resourceClass", UriComponentValue = "resourceClass@RESOURCECLASS", UriComponentOrder = 3, Mandatory = true, FinalCharacter = "/" }; Component identifier = new Component() { UriComponent = "identifier", UriComponentValue = "@ID", UriComponentOrder = 4, Mandatory = true, FinalCharacter = "" }; List <Component> componentes = new List <Component>(); componentes.Add(baseC); componentes.Add(character); componentes.Add(resource); componentes.Add(identifier); UriStructure uriStructure = new UriStructure() { Name = "test", Components = componentes }; structureGeneral.UriStructures = new List <UriStructure>(); structureGeneral.UriStructures.Add(uriStructure); ResourcesClass clas = new ResourcesClass() { LabelResourceClass = "prueba", ResourceClass = "Test", ResourceURI = "test" }; structureGeneral.ResourcesClasses = new List <ResourcesClass>(); structureGeneral.ResourcesClasses.Add(clas); string uriSchemaJson = JsonConvert.SerializeObject(structureGeneral); ConfigJsonHandler config = new ConfigJsonHandler(uriSchemaJson); FactoryController factoryController = new FactoryController(config); return(factoryController.GenerateUri("Test", "1234")); }