Exemplo n.º 1
0
        private static void VerifyLinksPayload(Workspace w, CommonPayload payload, LinqQueryBuilder linqBuilder)
        {
            ArrayList expectedEntities = CommonPayload.CreateList(linqBuilder.QueryResult);

            if (payload == null)
            {
                AstoriaTestLog.AreEqual(expectedEntities.Count, 0, "Unexpected null $ref payload");
            }
            else
            {
                KeyExpressions expectedKeys = new KeyExpressions();
                foreach (object o in expectedEntities)
                {
                    KeyExpression keyExp = w.CreateKeyExpressionFromProviderObject(o);
                    expectedKeys.Add(keyExp);
                }

                List <string> linksFound = new List <string>();

                if (payload.Resources == null)
                {
                    linksFound.Add(payload.Value);
                }
                else
                {
                    foreach (PayloadObject o in (payload.Resources as List <PayloadObject>))
                    {
                        if (o.PayloadProperties.Any(p => p.Name == "uri"))
                        {
                            linksFound.Add((o["uri"] as PayloadSimpleProperty).Value);
                        }
                    }
                }

                AstoriaTestLog.AreEqual(expectedKeys.Count, linksFound.Count, "Number of expected entities does not match number of links found");

                foreach (string link in linksFound)
                {
                    KeyExpression match = null;
                    foreach (KeyExpression expectedKey in expectedKeys)
                    {
                        if (compareKeyURI(link, expectedKey))
                        {
                            match = expectedKey;
                            break;
                        }
                    }

                    if (match != null)
                    {
                        expectedKeys.Remove(match);
                    }
                    else
                    {
                        AstoriaTestLog.WriteLineIgnore("Unexpected URI: '" + link + "'");
                        AstoriaTestLog.FailAndThrow("Unexpected URI found in $ref payload");
                    }
                }
            }
        }
Exemplo n.º 2
0
        public virtual void Compare(IQueryable baseline)
        {
            System.Collections.ArrayList baseLineEntities = CommonPayload.CreateList(baseline);

            if (this.Resources is PayloadComplexProperty)
            {
                PayloadComplexProperty complexProperty = (PayloadComplexProperty)this.Resources;
                this.CompareComplexType(complexProperty, baseLineEntities[0], false);
            }
            else if (this.Resources is PayloadSimpleProperty)
            {
                PayloadSimpleProperty simpleProperty = (PayloadSimpleProperty)this.Resources;
                this.CompareSimpleType(simpleProperty, baseLineEntities[0], false);
            }
            else if (this.Resources is List <PayloadObject> )
            {
                List <PayloadObject> payloadObjects = (List <PayloadObject>) this.Resources;
                Compare(baseLineEntities.OfType <object>().ToList(), payloadObjects, false);
            }
        }
Exemplo n.º 3
0
        public static void Verify(Workspace w, ExpNode q, IEnumerable results, ResourceType resType, ComplexType cType, bool bCount)
        {
            long count = 0;

            if (bCount)
            {
                object[] args    = new object[] { results };
                Type     qorType = typeof(QueryOperationResponseWrapper <>).MakeGenericType(resType.ClientClrType);
                object   qor     = Activator.CreateInstance(qorType, args);

                PropertyInfo pi = qor.GetType().GetProperty("GetTotalCount");
                object       i  = pi.GetValue(qor, new object[] { });

                count = (long)i;

                LinqQueryBuilder lb = new LinqQueryBuilder(w);
                lb.CountingMode = true;
                lb.Build(q);

                object baselineElementsCount = CommonPayload.CreateList(lb.QueryResult).Count;
                AstoriaTestLog.IsTrue(count == Convert.ToInt64(baselineElementsCount), "Count is different.Count is " + count.ToString() + ". Baseline count is " + baselineElementsCount.ToString());
            }

            LinqQueryBuilder linqBuilder = new LinqQueryBuilder(w);

            linqBuilder.Build(q);

            IQueryable baselines = linqBuilder.QueryResult;

            IEnumerator b = null;

            IEnumerator r = null;

            try
            {
                b = TrustedMethods.IQueryableGetEnumerator(baselines);
                r = results.GetEnumerator();
            }
            catch (InvalidOperationException invalidOperation)
            {
                if (!AstoriaTestProperties.IsRemoteClient)
                {
                    throw invalidOperation;
                }
            }

            PropertyInfo propertyInfo    = null;
            Object       expectedResult1 = null;
            Object       expectedResult2 = null;

            try
            {
                while (b.MoveNext() && r.MoveNext())
                {
                    if (b.Current == null)
                    {
                        return;
                    }

                    if (r.Current == null)
                    {
                        throw new TestFailedException("Less results than expected");
                    }
                    //skip verification for Binary data type for Linq to Sql
                    if (AstoriaTestProperties.DataLayerProviderKinds[0] == DataLayerProviderKind.LinqToSql && b.Current is System.Byte[])
                    {
                        return;
                    }

                    if (b.Current is System.Byte[])
                    {
                        byte[] newBase = (byte[])b.Current;
                        byte[] newAct  = (byte[])r.Current;

                        if (newBase.Length != newAct.Length)
                        {
                            throw new TestFailedException("Failed to compare the results!");
                        }
                    }
#if !ClientSKUFramework
                    else if (b.Current is System.Data.Linq.Binary)
                    {
                        System.Data.Linq.Binary newBase = (System.Data.Linq.Binary)b.Current;
                        System.Data.Linq.Binary newAct  = new System.Data.Linq.Binary((byte[])r.Current);

                        if (newBase.Length != newAct.Length)
                        {
                            throw new TestFailedException("Failed to compare the results!");
                        }
                    }
#endif

                    else if (b.Current is System.Xml.Linq.XElement)
                    {
                        if (b.Current.ToString() != r.Current.ToString())
                        {
                            throw new TestFailedException("Failed to compare the results!");
                        }
                    }
                    else
                    {
                        if (!b.Current.Equals(r.Current))
                        {
                            if (cType != null)
                            {
                                foreach (ResourceProperty property in cType.Properties)
                                {
                                    if (!property.IsNavigation && !property.IsComplexType && !(property.Type is CollectionType))
                                    {
                                        propertyInfo    = b.Current.GetType().GetProperty(property.Name);
                                        expectedResult1 = propertyInfo.GetValue(b.Current, null);

                                        PropertyInfo propertyInfo2 = r.Current.GetType().GetProperty(property.Name);
                                        expectedResult2 = propertyInfo2.GetValue(r.Current, null);

                                        if (expectedResult1 != expectedResult2)
                                        {
                                            if (expectedResult1 is System.Xml.Linq.XElement)
                                            {
                                                expectedResult1 = ((System.Xml.Linq.XElement)expectedResult1).ToString(System.Xml.Linq.SaveOptions.None);
                                            }
                                            if (expectedResult2 is System.Xml.Linq.XElement)
                                            {
                                                expectedResult2 = ((System.Xml.Linq.XElement)expectedResult2).ToString(System.Xml.Linq.SaveOptions.None);
                                            }
                                            #if !ClientSKUFramework
                                            if (expectedResult1 is byte[])
                                            {
                                                expectedResult1 = new System.Data.Linq.Binary((byte[])expectedResult1);
                                            }

                                            if (expectedResult2 is byte[])
                                            {
                                                expectedResult2 = new System.Data.Linq.Binary((byte[])expectedResult2);
                                            }
                                            #endif


                                            AstoriaTestLog.AreEqual(expectedResult1, expectedResult2, String.Format("Resource value for {0} does not match", property.Name), false);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                foreach (ResourceProperty property in resType.Properties)
                                {
                                    if (!property.IsNavigation && !property.IsComplexType && !(property.Type is CollectionType))
                                    {
                                        //skip verification for Binary data type for Linq to Sql
                                        if (AstoriaTestProperties.DataLayerProviderKinds[0] == DataLayerProviderKind.LinqToSql && property.Type.Name == "LinqToSqlBinary")
                                        {
                                            return;
                                        }

                                        propertyInfo    = b.Current.GetType().GetProperty(property.Name);
                                        expectedResult1 = propertyInfo.GetValue(b.Current, null);

                                        PropertyInfo propertyinfo2 = r.Current.GetType().GetProperty(property.Name);
                                        expectedResult2 = propertyinfo2.GetValue(r.Current, null);

                                        if (expectedResult1 != expectedResult2)
                                        {
                                            if (expectedResult1 is System.Xml.Linq.XElement)
                                            {
                                                expectedResult1 = ((System.Xml.Linq.XElement)expectedResult1).ToString(System.Xml.Linq.SaveOptions.None);
                                            }

                                            if (expectedResult2 is System.Xml.Linq.XElement)
                                            {
                                                expectedResult2 = ((System.Xml.Linq.XElement)expectedResult2).ToString(System.Xml.Linq.SaveOptions.None);
                                            }
                                            #if !ClientSKUFramework
                                            if (expectedResult1 is byte[])
                                            {
                                                expectedResult1 = new System.Data.Linq.Binary((byte[])expectedResult1);
                                            }

                                            if (expectedResult2 is byte[])
                                            {
                                                expectedResult2 = new System.Data.Linq.Binary((byte[])expectedResult2);
                                            }
                                            #endif
                                            AstoriaTestLog.AreEqual(expectedResult1, expectedResult2, String.Format("Resource value for {0} does not match", property.Name), false);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                AstoriaTestLog.WriteLine(e.ToString());
                throw;
            }
        }
Exemplo n.º 4
0
        public static void ValidateCountException(AstoriaResponse response, AstoriaRequest request, TestFailedException tfe)
        {
            // Errors are expected for older versions.
            if (!Versioning.Server.SupportsV2Features)
            {
                if (request.URI.Contains("$inlinecount=allpages"))
                {
                    // Query parameter not recognized.
                    if (response.ActualStatusCode == System.Net.HttpStatusCode.BadRequest)
                    {
                        return;
                    }
                }
                //anything other than http 404 or 400 is an invalid status code for a count query
                else if (
                    response.ActualStatusCode != System.Net.HttpStatusCode.BadRequest ||
                    response.ActualStatusCode != System.Net.HttpStatusCode.NotFound)
                {
                    throw tfe;
                }
                else
                {
                    // Resource segment $count not found.
                    if (response.ActualStatusCode == System.Net.HttpStatusCode.NotFound || response.ActualStatusCode == System.Net.HttpStatusCode.BadRequest)
                    {
                        return;
                    }
                }
            }

            LinqQueryBuilder linqBuilder = new LinqQueryBuilder(request.Workspace);

            linqBuilder.CountingMode = request.URI.Contains("$inlinecount=allpages");
            try
            {
                linqBuilder.Build(request.Query);
            }
            catch (KeyNotFoundException kfe)
            {
                if (!AstoriaTestProperties.DataLayerProviderKinds.Contains(DataLayerProviderKind.NonClr))
                {
                    throw kfe;
                }
            }
            bool isInvalidError = true;

            if (response.ActualStatusCode == System.Net.HttpStatusCode.NotFound)
            {
                if (request.Query.Input is CountExpression)
                {
                    ExpNode internalQuery = ((CountExpression)request.Query.Input).ScanNode;
                    if (internalQuery is KeyExpression || internalQuery is PredicateExpression)
                    {
                        isInvalidError = false;
                    }
                }
                if (isInvalidError)
                {
                    if (CommonPayload.CreateList(linqBuilder.QueryResult).Count == 0)
                    {
                        isInvalidError = false;
                    }
                }
            }
            if (response.ActualStatusCode == System.Net.HttpStatusCode.BadRequest)
            {
                if (request.Query.Input is CountExpression)
                {
                    ExpNode internalQuery = ((CountExpression)request.Query.Input).ScanNode;
                    if (internalQuery is KeyExpression || internalQuery is PredicateExpression)
                    {
                        isInvalidError = false;
                    }
                }
                else if (request.Query.Input is KeyExpression)
                {
                    isInvalidError = false;
                }
            }

            if (isInvalidError)
            {
                throw tfe;
            }
        }
Exemplo n.º 5
0
        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);
                    }
                }
            }
        }