private static ParseRepresentationAttrResult GetEmptyToken(SchemaAttributeResponse attr) { RepresentationAttribute result = null; switch (attr.Type) { case Common.Constants.SchemaAttributeTypes.String: case Common.Constants.SchemaAttributeTypes.Reference: result = GetEmptyToken(attr, string.Empty); break; case Common.Constants.SchemaAttributeTypes.Boolean: result = GetEmptyToken(attr, default(bool)); break; case Common.Constants.SchemaAttributeTypes.Decimal: result = GetEmptyToken(attr, default(decimal)); break; case Common.Constants.SchemaAttributeTypes.DateTime: result = GetEmptyToken(attr, default(DateTime)); break; case Common.Constants.SchemaAttributeTypes.Integer: result = GetEmptyToken(attr, default(int)); break; case Common.Constants.SchemaAttributeTypes.Complex: result = new ComplexRepresentationAttribute(attr); break; default: return(new ParseRepresentationAttrResult { IsParsed = false, ErrorMessage = string.Format(ErrorMessages.TheAttributeTypeIsNotSupported, attr.Type) }); } return(new ParseRepresentationAttrResult { IsParsed = true, RepresentationAttribute = result }); }
public async Task When_Trying_To_Remove_Person_FirstName_Then_It_Is_Removed_From_The_Representation() { // ARRANGE const string id = "id"; ErrorResponse errorResponse = new ErrorResponse { Status = (int)HttpStatusCode.BadRequest }; InitializeFakeObjects(); Representation result = null; var representation = new Representation(); var person = new ComplexRepresentationAttribute(new SchemaAttributeResponse { Name = "person", Type = Common.Constants.SchemaAttributeTypes.Complex }); person.Values = new[] { new SingularRepresentationAttribute <string>(new SchemaAttributeResponse { Name = "firstName", Type = Common.Constants.SchemaAttributeTypes.String }, "thierry") { Parent = person } }; representation.Attributes = new[] { person }; _representationStoreStub.Setup(r => r.GetRepresentation(It.IsAny <string>())) .Returns(Task.FromResult(representation)); _patchRequestParserStub.Setup(p => p.Parse(It.IsAny <JObject>(), out errorResponse)) .Returns(new[] { new PatchOperation { Type = PatchOperations.remove, Path = "person.firstName" } }); _filterParserStub.Setup(f => f.Parse(It.IsAny <string>())) .Returns(new Filter { Expression = new AttributeExpression { Path = new AttributePath { Name = "person", Next = new AttributePath { Name = "firstName" } } } }); _responseParserStub.Setup(p => p.Parse(It.IsAny <Representation>(), It.IsAny <string>(), It.IsAny <string>(), OperationTypes.Modification)) .Callback((Representation repr, string loc, string schem, OperationTypes op) => { result = repr; }) .Returns(Task.FromResult(new Response { Location = "location", Object = new JObject() })); // ACT await _patchRepresentationAction.Execute(id, new JObject(), "schema_id", "http://localhost:{id}"); // ASSERT Assert.NotNull(result); Assert.True(result.Attributes.Count() == 1); var complex = result.Attributes.First() as ComplexRepresentationAttribute; Assert.NotNull(complex); Assert.False(complex.Values.Any()); }
public RepresentationAttribute Transform(Model.RepresentationAttribute attr) { if (attr == null || (attr.Children == null && string.IsNullOrWhiteSpace(attr.Value)) && attr.ValueNumber == default(double)) { return(null); } SchemaAttributeResponse schemaAttr = null; if (attr.SchemaAttribute != null) { schemaAttr = Transform(attr.SchemaAttribute); } if (attr.SchemaAttribute != null && attr.SchemaAttribute.Type == Common.Constants.SchemaAttributeTypes.Complex || attr.SchemaAttribute == null && attr.Children != null && attr.Children.Any()) { ComplexRepresentationAttribute result = new ComplexRepresentationAttribute(schemaAttr); result.Values = new List <RepresentationAttribute>(); foreach (var child in attr.Children) { var transformed = Transform(child); if (transformed == null) { continue; } transformed.Parent = result; result.Values = result.Values.Concat(new[] { transformed }); } return(result); } var isArr = attr.SchemaAttribute.MultiValued; switch (attr.SchemaAttribute.Type) { case Common.Constants.SchemaAttributeTypes.String: if (isArr) { var values = new List <string>(); if (attr.Values != null) { foreach (var value in attr.Values) { values.Add(value.Value); } } return(new SingularRepresentationAttribute <IEnumerable <string> >(schemaAttr, values)); } return(new SingularRepresentationAttribute <string>(schemaAttr, attr.Value)); case Common.Constants.SchemaAttributeTypes.Boolean: bool r = false; if (isArr) { var values = new List <bool>(); if (attr.Values != null) { foreach (var value in attr.Values) { if (bool.TryParse(value.Value, out r)) { values.Add(r); } } } return(new SingularRepresentationAttribute <IEnumerable <bool> >(schemaAttr, values)); } bool.TryParse(attr.Value, out r); return(new SingularRepresentationAttribute <bool>(schemaAttr, r)); case Common.Constants.SchemaAttributeTypes.DateTime: DateTime dateTime = DateTime.Now; double d; if (isArr) { var values = new List <DateTime>(); if (attr.Values != null) { foreach (var value in attr.Values) { if (double.TryParse(value.Value, out d)) { values.Add(d.ToDateTime()); } } } return(new SingularRepresentationAttribute <IEnumerable <DateTime> >(schemaAttr, values)); } if (attr.ValueNumber != default(double)) { dateTime = attr.ValueNumber.ToDateTime(); } return(new SingularRepresentationAttribute <DateTime>(schemaAttr, dateTime)); case Common.Constants.SchemaAttributeTypes.Decimal: decimal dec; if (isArr) { var values = new List <decimal>(); if (attr.Values != null) { foreach (var value in attr.Values) { if (decimal.TryParse(value.Value, out dec)) { values.Add(dec); } } } return(new SingularRepresentationAttribute <IEnumerable <decimal> >(schemaAttr, values)); } return(new SingularRepresentationAttribute <decimal>(schemaAttr, (decimal)attr.ValueNumber)); case Common.Constants.SchemaAttributeTypes.Integer: int i; if (isArr) { var values = new List <int>(); if (attr.Values != null) { foreach (var value in attr.Values) { if (int.TryParse(value.Value, out i)) { values.Add(i); } } } return(new SingularRepresentationAttribute <IEnumerable <int> >(schemaAttr, values)); } return(new SingularRepresentationAttribute <int>(schemaAttr, (int)attr.ValueNumber)); } return(null); }
/// <summary> /// Parse json and returns the representation. /// </summary> /// <exception cref="ArgumentNullException">Thrown when one of the parameter is null.</exception> /// <param name="jObj">JSON</param> /// <param name="attribute">Schema attribute</param> /// <param name="checkStrategy">Strategy used to check the parameters.</param> /// <returns>Representation or null.</returns> public ParseRepresentationAttrResult GetRepresentation(JToken jObj, SchemaAttributeResponse attribute, CheckStrategies checkStrategy) { if (jObj == null) { throw new ArgumentNullException(nameof(jObj)); } if (attribute == null) { throw new ArgumentNullException(nameof(attribute)); } Action <ComplexSchemaAttributeResponse, List <RepresentationAttribute>, JToken, RepresentationAttribute> setRepresentationCallback = (attr, lst, tok, reprAttr) => { foreach (var subAttribute in attr.SubAttributes) { var rep = GetRepresentation(tok, subAttribute, checkStrategy); if (rep.IsParsed) { rep.RepresentationAttribute.Parent = reprAttr; lst.Add(rep.RepresentationAttribute); } } }; var token = jObj.SelectToken(attribute.Name); // 1. Check the attribute is required if (token == null) { if (attribute.Required && checkStrategy == CheckStrategies.Strong) { return(new ParseRepresentationAttrResult { IsParsed = false, ErrorMessage = string.Format(ErrorMessages.TheAttributeIsRequired, attribute.Name) }); } // Add empty attribute. return(GetEmptyToken(attribute)); } // 2. Check is an array JArray jArr = null; if (attribute.MultiValued) { jArr = token as JArray; if (jArr == null) { return(new ParseRepresentationAttrResult { IsParsed = false, ErrorMessage = string.Format(ErrorMessages.TheAttributeIsNotAnArray, attribute.Name) }); } } // 3. Create complex attribute var complexAttribute = attribute as ComplexSchemaAttributeResponse; if (complexAttribute != null) { var representation = new ComplexRepresentationAttribute(complexAttribute); var values = new List <RepresentationAttribute>(); if (complexAttribute.MultiValued) { // 3.1 Contains an array if (jArr.Count > 0) { foreach (var subToken in token) { var subRepresentation = new ComplexRepresentationAttribute(null); var subValues = new List <RepresentationAttribute>(); setRepresentationCallback(complexAttribute, subValues, subToken, subRepresentation); subRepresentation.Values = subValues; values.Add(subRepresentation); subRepresentation.Parent = representation; } } else { values.Add(new ComplexRepresentationAttribute(null) { Values = new List <RepresentationAttribute>() }); } } else { // 3.2 Doesn't contain array setRepresentationCallback(complexAttribute, values, token, representation); } representation.Values = values; return(new ParseRepresentationAttrResult { IsParsed = true, RepresentationAttribute = representation }); } RepresentationAttribute result = null; // 4. Create singular attribute. // Note : Don't cast to object to avoid unecessaries boxing operations ... switch (attribute.Type) { case Common.Constants.SchemaAttributeTypes.String: result = GetSingularToken <string>(jArr, attribute, token); break; case Common.Constants.SchemaAttributeTypes.Boolean: result = GetSingularToken <bool>(jArr, attribute, token); break; case Common.Constants.SchemaAttributeTypes.Decimal: result = GetSingularToken <decimal>(jArr, attribute, token); break; case Common.Constants.SchemaAttributeTypes.DateTime: result = GetSingularToken <DateTime>(jArr, attribute, token); break; case Common.Constants.SchemaAttributeTypes.Integer: result = GetSingularToken <int>(jArr, attribute, token); break; default: return(new ParseRepresentationAttrResult { IsParsed = false, ErrorMessage = string.Format(ErrorMessages.TheAttributeTypeIsNotSupported, attribute.Type) }); } if (result == null) { return(new ParseRepresentationAttrResult { IsParsed = false, ErrorMessage = string.Format(ErrorMessages.TheAttributeTypeIsNotCorrect, attribute.Name, attribute.Type) }); } return(new ParseRepresentationAttrResult { RepresentationAttribute = result, IsParsed = true }); }
private static bool Equals(ComplexRepresentationAttribute attr, ComparisonOperators op, string value) { return(attr.Values != null && op == ComparisonOperators.pr); }
public RepresentationAttribute Transform(Model.RepresentationAttribute attr) { if (attr == null || (attr.Children == null && string.IsNullOrWhiteSpace(attr.Value))) { return(null); } SchemaAttributeResponse schemaAttr = null; if (attr.SchemaAttribute != null) { schemaAttr = Transform(attr.SchemaAttribute); } if (attr.SchemaAttribute != null && attr.SchemaAttribute.Type == Common.Constants.SchemaAttributeTypes.Complex || attr.SchemaAttribute == null && attr.Children != null && attr.Children.Any()) { ComplexRepresentationAttribute result = new ComplexRepresentationAttribute(schemaAttr); result.Values = new List <RepresentationAttribute>(); foreach (var child in attr.Children) { var transformed = Transform(child); if (transformed == null) { continue; } transformed.Parent = result; result.Values = result.Values.Concat(new[] { transformed }); } return(result); } var isArr = attr.SchemaAttribute.MultiValued; switch (attr.SchemaAttribute.Type) { case Common.Constants.SchemaAttributeTypes.String: if (isArr) { var record = JsonConvert.DeserializeObject <IEnumerable <string> >(attr.Value); return(new SingularRepresentationAttribute <IEnumerable <string> >(schemaAttr, record)); } var str = JsonConvert.DeserializeObject <string>(attr.Value); return(new SingularRepresentationAttribute <string>(schemaAttr, str)); case Common.Constants.SchemaAttributeTypes.Boolean: if (isArr) { var record = JsonConvert.DeserializeObject <IEnumerable <bool> >(attr.Value); return(new SingularRepresentationAttribute <IEnumerable <bool> >(schemaAttr, record)); } var b = JsonConvert.DeserializeObject <bool>(attr.Value); return(new SingularRepresentationAttribute <bool>(schemaAttr, b)); case Common.Constants.SchemaAttributeTypes.DateTime: if (isArr) { var record = JsonConvert.DeserializeObject <IEnumerable <DateTime> >(attr.Value); return(new SingularRepresentationAttribute <IEnumerable <DateTime> >(schemaAttr, record)); } var datetime = JsonConvert.DeserializeObject <DateTime>(attr.Value); return(new SingularRepresentationAttribute <DateTime>(schemaAttr, datetime)); case Common.Constants.SchemaAttributeTypes.Decimal: if (isArr) { var record = JsonConvert.DeserializeObject <IEnumerable <decimal> >(attr.Value); return(new SingularRepresentationAttribute <IEnumerable <decimal> >(schemaAttr, record)); } var dec = JsonConvert.DeserializeObject <decimal>(attr.Value); return(new SingularRepresentationAttribute <decimal>(schemaAttr, dec)); case Common.Constants.SchemaAttributeTypes.Integer: if (isArr) { var record = JsonConvert.DeserializeObject <IEnumerable <int> >(attr.Value); return(new SingularRepresentationAttribute <IEnumerable <int> >(schemaAttr, record)); } var i = JsonConvert.DeserializeObject <int>(attr.Value); return(new SingularRepresentationAttribute <int>(schemaAttr, i)); } return(null); }
public IEnumerable <RepresentationAttribute> Evaluate(IEnumerable <RepresentationAttribute> representationAttrs) { var representations = representationAttrs.Where(r => r.SchemaAttribute != null && r.SchemaAttribute.Name == Name); if (!representations.Any()) { var lst = new List <RepresentationAttribute>(); foreach (var representationAttr in representationAttrs) { var c = representationAttr as ComplexRepresentationAttribute; if (c == null || !c.Values.Any(val => val.SchemaAttribute != null && val.SchemaAttribute.Name == Name)) { continue; } lst.AddRange(c.Values.Where(val => val.SchemaAttribute.Name == Name)); } if (!lst.Any()) { return(new RepresentationAttribute[0]); } representations = lst; } // representations = representations.Select(r => (RepresentationAttribute)r.Clone()).ToList(); if ((ValueFilter != null && representations.Any(r => !r.SchemaAttribute.MultiValued)) || (Next != null && representations.Any(r => r.SchemaAttribute.Type != Common.Constants.SchemaAttributeTypes.Complex))) { return(null); } if (ValueFilter != null || Next != null) { var subAttrs = new List <RepresentationAttribute>(); var lst = new List <RepresentationAttribute>(); foreach (var representation in representations) { var record = representation; var complexAttr = representation as ComplexRepresentationAttribute; if (complexAttr != null && complexAttr.Values != null && complexAttr.Values.Any()) { if (ValueFilter != null) { var val = ValueFilter.Evaluate(complexAttr.Values); if (val != null && val.Any()) { record = new ComplexRepresentationAttribute(complexAttr.SchemaAttribute) { Values = val }; lst.Add(record); } } if (Next != null) { var values = Next.Evaluate(((ComplexRepresentationAttribute)record).Values); if (values != null && values.Any()) { subAttrs.AddRange(values); } } } } representations = lst; if (subAttrs.Any()) { return(subAttrs); } } return(representations); }