Beispiel #1
0
        protected void ComparePropertyValues(NodeType type, PayloadProperty first, PayloadProperty second, bool checkValue)
        {
            if (type is ComplexType)
            {
                if (first.IsNull)
                {
                    if (!second.IsNull)
                    {
                        AstoriaTestLog.FailAndThrow("Second property is unexpectedly non-null");
                    }
                    return;
                }
                else if (second.IsNull)
                {
                    AstoriaTestLog.FailAndThrow("Second property is unexpectedly null");
                }

                PayloadComplexProperty firstComplex  = first as PayloadComplexProperty;
                PayloadComplexProperty secondComplex = second as PayloadComplexProperty;

                if (firstComplex == null)
                {
                    AstoriaTestLog.FailAndThrow("First property was not a complex property");
                }
                if (secondComplex == null)
                {
                    AstoriaTestLog.FailAndThrow("Second property was not a complex property");
                }

                foreach (string propertyName in firstComplex.PayloadProperties.Keys.Union(secondComplex.PayloadProperties.Keys))
                {
                    // TODO: verify typing
                    if (propertyName == "__metadata")
                    {
                        continue;
                    }

                    PayloadProperty firstProperty;
                    bool            firstHadProperty = firstComplex.PayloadProperties.TryGetValue(propertyName, out firstProperty);

                    PayloadProperty secondProperty;
                    bool            secondHadProperty = secondComplex.PayloadProperties.TryGetValue(propertyName, out secondProperty);

                    // so that we can ignore this case later on, check it now
                    if (!firstHadProperty && !secondHadProperty)
                    {
                        AstoriaTestLog.FailAndThrow("Property list contained property '" + propertyName + "' despite neither complex property containing it");
                    }

                    // since a complex type is never open, there shouldnt be any unexpected properties
                    NodeProperty property = (type as ComplexType).Properties.SingleOrDefault(p => p.Name == propertyName);
                    if (property == null)
                    {
                        if (firstHadProperty && secondHadProperty)
                        {
                            AstoriaTestLog.FailAndThrow("Both complex properties contained sub-property '" + propertyName + "' which is not defined on type '" + type.Name + "'");
                        }
                        else if (firstHadProperty)
                        {
                            AstoriaTestLog.FailAndThrow("First complex property contained sub-property '" + propertyName + "' which is not defined on type '" + type.Name + "'");
                        }
                        else if (secondHadProperty)
                        {
                            AstoriaTestLog.FailAndThrow("Second complex property contained sub-property '" + propertyName + "' which is not defined on type '" + type.Name + "'");
                        }
                    }

                    if (!firstHadProperty)
                    {
                        AstoriaTestLog.FailAndThrow("First complex property property missing sub-property '" + propertyName + "'");
                    }
                    else if (!secondHadProperty)
                    {
                        AstoriaTestLog.FailAndThrow("Second complex property property missing sub-property '" + propertyName + "'");
                    }

                    ComparePropertyValues(property, firstProperty, secondProperty, checkValue && !property.Facets.ServerGenerated);
                }
            }
            else
            {
                PayloadSimpleProperty firstSimple  = first as PayloadSimpleProperty;
                PayloadSimpleProperty secondSimple = second as PayloadSimpleProperty;

                if (firstSimple == null)
                {
                    AstoriaTestLog.FailAndThrow("First property was not a simple property");
                }
                if (secondSimple == null)
                {
                    AstoriaTestLog.FailAndThrow("Second property was not a simple property");
                }

                object expectedObject = CommonPayload.DeserializeStringToObject(firstSimple.Value, type.ClrType, false, first.ParentObject.Format);
                if (checkValue)
                {
                    CommonPayload.ComparePrimitiveValuesObjectAndString(expectedObject, type.ClrType, secondSimple.Value, false, second.ParentObject.Format, true);
                }
                else
                {
                    CommonPayload.DeserializeStringToObject(secondSimple.Value, type.ClrType, false, second.ParentObject.Format);
                }
            }
        }
        public static void VerifyGet(AstoriaResponse response)
        {
            AstoriaRequest request = response.Request;

            if (request.Query != null)
            {
                LinqQueryBuilder linqBuilder = new LinqQueryBuilder(request.Workspace);
                linqBuilder.Build(request.Query);
                if (request.URI.Contains("$value"))
                {
                    CommonPayload payload;

                    // $value queries will sometimes return unexpected 404s due to null values
                    // if we get a 404 unexpectedly, the underlying result must be null
                    if (response.ActualStatusCode == System.Net.HttpStatusCode.NotFound)
                    {
                        payload = request.CommonPayload;
                    }

                    IEnumerable enumerable     = linqBuilder.QueryResult;
                    object      expectedResult = CommonPayload.GetSingleValueFromIQueryable(linqBuilder.QueryResult, false);

                    ResourceProperty rp = request.GetPropertyFromQuery();
                    CommonPayload.ComparePrimitiveValuesObjectAndString(expectedResult, rp.Type.ClrType, response.Payload, true, request.Format, false);
                }
                else if (request.URI.Contains("$count") || request.URI.Contains("$inlinecount"))
                {
                    if (request.URI.Contains("$count"))
                    {
                        // Versioning: make sure the server always returns 2.0.
                        VerifySpecificResponseVersion(response, 2, 0);

                        // PlainText payload.
                        try
                        {
                            VerifyStatusCode(response);
                            if (response.Request.ExpectedStatusCode == System.Net.HttpStatusCode.OK)
                            {
                                int countPayload  = Int32.Parse(response.Payload);
                                int countBaseline = CommonPayload.CreateList(linqBuilder.QueryResult).Count;
                                AstoriaTestLog.AreEqual(countPayload, countBaseline, "Counts in payload (" + countPayload + ") and baseline (" + countBaseline + ") differ.");
                            }
                        }
                        catch (TestFailedException tfe)
                        {
                            //When the underlying resource has zero elements, the server now throws a 404 error , causing false negatives
                            ValidateCountException(response, request, tfe);
                        }
                    }
                    else
                    {
                        VerifySpecificResponseVersion(response, 2, 0);

                        try
                        {
                            // JSON, Atom or PlainXml ($ref) payload.
                            VerifyStatusCode(response);
                            if (response.Request.ExpectedStatusCode == System.Net.HttpStatusCode.OK)
                            {
                                CommonPayload    payload             = response.CommonPayload;
                                LinqQueryBuilder baselineLinqBuilder = new LinqQueryBuilder(request.Workspace);
                                baselineLinqBuilder.CountingMode = true;
                                baselineLinqBuilder.Build(request.Query);

                                object baselineElementsCount = CommonPayload.CreateList(baselineLinqBuilder.QueryResult).Count;
                                payload.CompareCountInline(linqBuilder.QueryResult, baselineElementsCount);
                                AstoriaTestLog.WriteLine(linqBuilder.QueryExpression);
                                if (request.URI.Contains("$ref"))
                                {
                                    VerifyLinksPayload(request.Workspace, payload, linqBuilder);
                                }
                            }
                        }
                        catch (TestFailedException tfe)
                        {
                            //When the underlying resource has zero elements, the server now throws a 404 error , causing false negatives
                            ValidateCountException(response, request, tfe);
                        }
                    }
                }
                else
                {
                    if (response.ActualStatusCode == System.Net.HttpStatusCode.NoContent && response.Payload == "")
                    {
                        response.Payload = null; // allow result from Linq query and payload to be compared (NoContent is a get to a null Nav prop)
                    }
                    if (response.ActualStatusCode == System.Net.HttpStatusCode.NotFound)
                    {
                        return;
                    }

                    CommonPayload payload = response.CommonPayload;
                    if (request.URI.Contains("$ref"))
                    {
                        VerifyLinksPayload(request.Workspace, payload, linqBuilder);
                    }
                    else if (payload.Resources == null)
                    {
                        if (request.Format == SerializationFormatKind.JSON)
                        {
                            payload.CompareValue(linqBuilder.QueryResult, true, false);
                        }
                        else
                        {
                            payload.CompareValue(linqBuilder.QueryResult, false, false);
                        }
                    }
                    else
                    {
                        payload.Compare(linqBuilder.QueryResult);
                    }
                }
            }
        }
Beispiel #3
0
        private void CompareResetProperty(NodeProperty property, PayloadProperty value)
        {
            bool complexType = property.Type is ComplexType;

            Workspace w          = value.ParentObject.Payload.Workspace;
            bool      expectNull = false;

            if (w.DataLayerProviderKind == DataLayerProviderKind.NonClr && (!property.Facets.IsClrProperty || complexType))
            {
                expectNull = true;
            }
            if (w.DataLayerProviderKind == DataLayerProviderKind.InMemoryLinq && complexType)
            {
                expectNull = true;
            }
            if (!property.Facets.IsDeclaredProperty)
            {
                expectNull = true;
            }
            if (property.Facets.Nullable)
            {
                expectNull = true;
            }

            if (property.Type is ComplexType)
            {
                if (value.IsNull)
                {
                    if (!expectNull)
                    {
                        AstoriaTestLog.FailAndThrow("Complex property '" + property.Name + " is unexpectedly null after reset");
                    }
                }
                else
                {
                    if (!(value is PayloadComplexProperty))
                    {
                        AstoriaTestLog.FailAndThrow("Property '" + property.Name + "' is complex, but a simple property was found instead");
                    }

                    ComplexType            ct = property.Type as ComplexType;
                    PayloadComplexProperty complexProperty = value as PayloadComplexProperty;
                    foreach (NodeProperty subProperty in ct.Properties)
                    {
                        PayloadProperty subValue;
                        if (!complexProperty.PayloadProperties.TryGetValue(subProperty.Name, out subValue))
                        {
                            AstoriaTestLog.FailAndThrow("Property of complex type '" + ct.Name + "' missing sub-property '" + subProperty.Name + "'");
                        }

                        CompareResetProperty(subProperty, subValue);
                    }
                }
            }
            else
            {
                PayloadSimpleProperty simpleProperty = value as PayloadSimpleProperty;
                if (simpleProperty == null)
                {
                    AstoriaTestLog.FailAndThrow("Property was unexpectedly not a simple property");
                }

                Type   propertyType = property.Type.ClrType;
                object expected     = null;
                if (!expectNull)
                {
                    expected = DefaultValue(propertyType);
                }
                CommonPayload.ComparePrimitiveValuesObjectAndString(expected, propertyType, simpleProperty.Value, false, value.ParentObject.Format, true);
            }
        }