/// <summary>
 /// Throws an exception if there are any validation errors
 /// </summary>
 /// <param name="context">The <see cref="OperationContext"/> of the validation errors</param>
 /// <param name="validationErrors">The validation errors</param>
 /// <exception cref="DomainServiceTestHostException">is thrown if there are any validation errors</exception>
 public static void AssertNoValidationErrors(OperationContext context, IEnumerable<ValidationResult> validationErrors)
 {
     if ((validationErrors != null) && validationErrors.Any())
     {
         ErrorUtility.ReportErrors(context, validationErrors.Select(vr => ErrorUtility.GetErrorMessageForValidation(vr)));
     }
 }
 /// <summary>
 /// Throws an exception containing information about the context and error
 /// </summary>
 /// <param name="context">The <see cref="OperationContext"/> where the exception occurred</param>
 /// <param name="errorMessages">The error messages to include</param>
 /// <exception cref="DomainServiceTestHostException">is thrown</exception>
 private static void ReportErrors(OperationContext context, IEnumerable<string> errorMessages)
 {
     if ((errorMessages != null) && errorMessages.Any())
     {
         string message = string.Format(
             CultureInfo.CurrentCulture,
             "One or more errors occurred while calling the '{0}' operation on '{1}':\n {2}",
             context.OperationName,
             context.DomainServiceDescription.DomainServiceType,
             string.Join(", ", errorMessages));
         throw new DomainServiceTestHostException(message);
     }
 }
        public static InvokeDescription GetInvokeDescription(OperationContext context, Expression expression)
        {
            context.OperationName = Utility.GetNameFromLambda(expression);
            IEnumerable<object> parameterValues = Utility.GetParametersFromLambda(expression);

            DomainOperationEntry domainOperationEntry = context.DomainServiceDescription.GetInvokeOperation(context.OperationName);
            if (domainOperationEntry == null)
            {
                throw new InvalidOperationException(string.Format(
                    CultureInfo.CurrentCulture,
                    Resources.NoInvokeOperation,
                    context.OperationName,
                    context.DomainServiceDescription.DomainServiceType));
            }

            return new InvokeDescription(domainOperationEntry, parameterValues.ToArray());
        }
 /// <summary>
 /// Throws an exception if there are any change set errors
 /// </summary>
 /// <param name="context">The <see cref="OperationContext"/> of the change set</param>
 /// <param name="changeSet">The change set</param>
 /// <exception cref="DomainServiceTestHostException">is thrown if there are any change set errors</exception>
 public static void AssertNoChangeSetErrors(OperationContext context, ChangeSet changeSet)
 {
     if (changeSet.HasError)
     {
         List<string> errorMessages = new List<string>();
         foreach (ChangeSetEntry entry in changeSet.ChangeSetEntries)
         {
             if ((entry.ValidationErrors != null) && entry.ValidationErrors.Any())
             {
                 errorMessages.Add(ErrorUtility.GetErrorMessageForValidation(entry));
             }
             if ((entry.ConflictMembers != null) && entry.ConflictMembers.Any())
             {
                 errorMessages.Add(ErrorUtility.GetErrorMessageForConflicts(entry));
             }
         }
         ErrorUtility.ReportErrors(context, errorMessages);
     }
 }
        public static ChangeSetEntry GetCustomUpdateChangeSetEntry(OperationContext context, Expression expression, object original)
        {
            context.OperationName = Utility.GetNameFromLambda(expression);
            IEnumerable<object> parameterValues = Utility.GetParametersFromLambda(expression);
            object entity = parameterValues.First();

            ChangeSetEntry changeSetEntry = new ChangeSetEntry(Utility.DefaultChangeSetEntryId, entity, original, DomainOperation.Update);

            DomainOperationEntry domainOperationEntry = context.DomainServiceDescription.GetCustomMethod(entity.GetType(), context.OperationName);
            if (domainOperationEntry == null)
            {
                throw new InvalidOperationException(string.Format(
                    CultureInfo.CurrentCulture,
                    Resources.NoCustomUpdateOperation,
                    context.OperationName,
                    context.DomainServiceDescription.DomainServiceType));
            }
            changeSetEntry.EntityActions = new EntityActionCollection
            {
                { context.OperationName, parameterValues.Skip(1).ToArray() },
            };

            return changeSetEntry;
        }
 public static ChangeSetEntry GetChangeSetEntry(OperationContext context, object entity, object original, DomainOperation operationType)
 {
     Utility.EnsureOperationSupported(context, entity.GetType(), operationType);
     context.OperationName = context.DomainServiceDescription.GetSubmitMethod(entity.GetType(), operationType).Name;
     return new ChangeSetEntry(Utility.DefaultChangeSetEntryId, entity, original, operationType);
 }
 private static void EnsureOperationSupported(OperationContext context, Type entityType, DomainOperation operationType)
 {
     if (!context.DomainServiceDescription.IsOperationSupported(entityType, operationType))
     {
         throw new DomainServiceTestHostException(string.Format(
             CultureInfo.CurrentCulture,
             Resources.OperationNotSupported,
             operationType,
             entityType,
             context.DomainServiceDescription.DomainServiceType));
     }
 }