/// <summary>
 /// Invokes the 'NamedUpdateWithTypeValidation' method of the specified <see cref="MockEntity4"/> entity.
 /// </summary>
 /// <param name="entity">The <see cref="MockEntity4"/> entity instance.</param>
 /// <param name="array">The value for the 'array' parameter for this action.</param>
 /// <param name="complexObject">The value for the 'complexObject' parameter for this action.</param>
 public void NamedUpdateWithTypeValidation(MockEntity4 entity, MockComplexObject2[] array, MockComplexObject2 complexObject)
 {
     entity.NamedUpdateWithTypeValidation(array, complexObject);
 }
 /// <summary>
 /// Invokes the 'NamedUpdateWithNoEntityValidation' method of the specified <see cref="MockEntity6"/> entity.
 /// </summary>
 /// <param name="entity">The <see cref="MockEntity6"/> entity instance.</param>
 /// <param name="complexObject">The value for the 'complexObject' parameter for this action.</param>
 public void NamedUpdateWithNoEntityValidation(MockEntity6 entity, MockComplexObject2 complexObject)
 {
     entity.NamedUpdateWithNoEntityValidation(complexObject);
 }
 partial void OnNamedUpdateWithNoEntityValidationInvoking(MockComplexObject2 complexObject);
 public void NamedUpdateWithNoEntityValidation(MockComplexObject2 complexObject)
 {
     this.OnNamedUpdateWithNoEntityValidationInvoking(complexObject);
     base.InvokeAction("NamedUpdateWithNoEntityValidation", complexObject);
     this.OnNamedUpdateWithNoEntityValidationInvoked();
 }
 public void NamedUpdateWithTypeValidation(MockComplexObject2[] array, MockComplexObject2 complexObject)
 {
     this.OnNamedUpdateWithTypeValidationInvoking(array, complexObject);
     base.InvokeAction("NamedUpdateWithTypeValidation", array, complexObject);
     this.OnNamedUpdateWithTypeValidationInvoked();
 }
 partial void OnNamedUpdateWithTypeValidationInvoking(MockComplexObject2[] array, MockComplexObject2 complexObject);
 partial void OnProperty1Changing(MockComplexObject2 value);
        public void NamedUpdate_CustomValidation_NoEntityValidation()
        {
            string methodName = "NamedUpdateWithNoEntityValidation";
            MockComplexObject2 complexObject = new MockComplexObject2();

            string format1 = "{0} invalid on client.";
            ValidationResult registeredResult = CustomMethodTests.CreateRegisteredValidationResult(format1, typeof(MockComplexObject2));
            ValidationResult expectedClientResult = CustomMethodTests.CreateExpectedValidationResult(format1, typeof(MockComplexObject2), true, methodName + ".complexObject.PlaceholderName");

            SubmitOperation submitOp = null;
            LoadOperation<MockEntity6> loadOp = null;
            MockEntity6 entity = null;

            Uri uri = new Uri(TestURIs.RootURI, "TestDomainServices-NamedUpdates-NamedUpdate_CustomValidation.svc");
            NamedUpdate_CustomValidation ctx = null;

            this.EnqueueCallback(() =>
                {
                    ctx = new NamedUpdate_CustomValidation(uri);
                    loadOp = ctx.Load(ctx.GetEntities6Query(), TestHelperMethods.DefaultOperationAction, null);
                });

            this.EnqueueConditional(() => loadOp.IsComplete);

            this.EnqueueCallback(() =>
                {
                    TestHelperMethods.AssertOperationSuccess(loadOp);
                    entity = loadOp.Entities.First();

                    // Make the param invalid and the call should fail.
                    DynamicTestValidator.ForcedValidationResults.Add(typeof(MockComplexObject2), registeredResult);
                    ExceptionHelper.ExpectValidationException(() =>
                        {
                            entity.NamedUpdateWithNoEntityValidation(complexObject);
                        }, expectedClientResult.ErrorMessage, typeof(CustomValidationAttribute), complexObject);

                    DynamicTestValidator.Reset();
                    entity.NamedUpdateWithNoEntityValidation(complexObject);

                    // This time the client submit should fail.
                    DynamicTestValidator.ForcedValidationResults.Add(typeof(MockComplexObject2), registeredResult);
                    submitOp = ctx.SubmitChanges(TestHelperMethods.DefaultOperationAction, null);
                });

            this.EnqueueConditional(() => submitOp.IsComplete);

            this.EnqueueCallback(() =>
                {
                    Assert.IsTrue(submitOp.HasError, "Client call: Submit should have failed.");
                    UnitTestHelper.AssertValidationResultsAreEqual(new ValidationResult[] { expectedClientResult }, entity.ValidationErrors);

                    // Now the server submit should fail.
                    DynamicTestValidator.Reset();
                    submitOp = ctx.SubmitChanges(TestHelperMethods.DefaultOperationAction, null);
                });

            this.EnqueueConditional(() => submitOp.IsComplete);

            this.EnqueueCallback(() =>
                {
                    Assert.IsTrue(submitOp.HasError, "Server call: Submit should have failed.");
                    var expectedServerResults = CustomMethodTests.GetExpectedErrors(methodName);
                    UnitTestHelper.AssertValidationResultsAreEqual(expectedServerResults, entity.ValidationErrors);
                });

            this.EnqueueTestComplete();
        }
        public void NamedUpdate_CustomValidation_Type()
        {
            MockComplexObject2[] complexArray = new MockComplexObject2[] { new MockComplexObject2() };
            MockComplexObject2 complexObject = new MockComplexObject2 { Property1 = new MockComplexObject2() };

            SubmitOperation submitOp = null;
            LoadOperation<MockEntity4> loadOp = null;
            MockEntity4 entity = null;

            Uri uri = new Uri(TestURIs.RootURI, "TestDomainServices-NamedUpdates-NamedUpdate_CustomValidation.svc");
            NamedUpdate_CustomValidation ctx = null;

            Action<object, bool, ValidationResult, ValidationResult> typeVariation = (invalidParam, deepValidation, registeredResult, expectedResult) =>
            {
                this.EnqueueCallback(() =>
                {
                    ctx = new NamedUpdate_CustomValidation(uri);
                    loadOp = ctx.Load(ctx.GetEntities4Query(), TestHelperMethods.DefaultOperationAction, null);
                });

                this.EnqueueConditional(() => loadOp.IsComplete);

                this.EnqueueCallback(() =>
                {
                    TestHelperMethods.AssertOperationSuccess(loadOp);
                    entity = loadOp.Entities.First();

                    if (invalidParam == null)
                    {
                        invalidParam = entity;
                    }

                    // make param invalid
                    DynamicTestValidator.Reset();
                    DynamicTestValidator.ForcedValidationResults.Add(invalidParam, registeredResult);

                    // deep type validation should not throw on call
                    if (deepValidation)
                    {
                        entity.NamedUpdateWithTypeValidation(complexArray, complexObject);
                        ctx.MockEntity4s.Clear();
                        entity.Reset();
                        ctx.MockEntity4s.Attach(entity);
                    }
                    // shallow type validation should throw on call
                    else
                    {
                        ValidationException vex = ExceptionHelper.ExpectValidationException(() =>
                        {
                            entity.NamedUpdateWithTypeValidation(complexArray, complexObject);
                        }, expectedResult.ErrorMessage, typeof(CustomValidationAttribute), invalidParam);
                        // The exception varies from the expected because the framework does not alter the result.
                        // This means the expected MemberNames is equivalent to the registered MemberNames
                        UnitTestHelper.AssertValidationResultsAreEqual(new ValidationResult[] { new ValidationResult(expectedResult.ErrorMessage, registeredResult.MemberNames) }, new ValidationResult[] { vex.ValidationResult });
                    }

                    // ensure type validation fails on client submit
                    DynamicTestValidator.Reset();
                    entity.NamedUpdateWithTypeValidation(complexArray, complexObject);

                    // make param invalid once again
                    DynamicTestValidator.ForcedValidationResults.Add(invalidParam, registeredResult);

                    // actually submit
                    submitOp = ctx.SubmitChanges(TestHelperMethods.DefaultOperationAction, null);
                });

                this.EnqueueConditional(() => submitOp.IsComplete);

                this.EnqueueCallback(() =>
                {
                    Assert.IsTrue(submitOp.HasError, "No error on client submit of an incorrect entity.");

                    // compare to expected
                    UnitTestHelper.AssertValidationResultsAreEqual(new ValidationResult[] { expectedResult }, entity.ValidationResultCollection);

                    // ensure type validation fails on server submit
                    DynamicTestValidator.Reset();
                    submitOp = ctx.SubmitChanges(TestHelperMethods.DefaultOperationAction, null);
                });

                this.EnqueueConditional(() => submitOp.IsComplete);

                this.EnqueueCallback(() =>
                {
                    Assert.IsTrue(submitOp.HasError, "No error on server submit of an incorrect entity.");

                    // compare to expected, the server errors no matter what we send it, so its
                    // expected validation failures can be static.
                    IEnumerable<ValidationResult> expectedServerValidationResults = CustomMethodTests.GetExpectedErrors("NamedUpdateWithTypeValidation");
                    UnitTestHelper.AssertValidationResultsAreEqual(expectedServerValidationResults, entity.ValidationResultCollection);
                });
            };

            // Run variations
            string format1 = "{0} invalid on the client";
            typeVariation(null, false,
                CustomMethodTests.CreateRegisteredValidationResult(format1, typeof(MockEntity4)),
                CustomMethodTests.CreateExpectedValidationResult(format1, typeof(MockEntity4), false));

            typeVariation(complexArray[0], true,
                CustomMethodTests.CreateRegisteredValidationResult(format1, typeof(MockComplexObject2)),
                CustomMethodTests.CreateExpectedValidationResult(format1, typeof(MockComplexObject2), false, "NamedUpdateWithTypeValidation.array().PlaceholderName"));

            typeVariation(complexObject, false,
                CustomMethodTests.CreateRegisteredValidationResult(format1, typeof(MockComplexObject2)),
                CustomMethodTests.CreateExpectedValidationResult(format1, typeof(MockComplexObject2), false, "NamedUpdateWithTypeValidation.complexObject.PlaceholderName"));
            
            typeVariation(complexObject.Property1, true,
                CustomMethodTests.CreateRegisteredValidationResult(format1, typeof(MockComplexObject2)),
                CustomMethodTests.CreateExpectedValidationResult(format1, typeof(MockComplexObject2), false, "NamedUpdateWithTypeValidation.complexObject.Property1.PlaceholderName"));

            this.EnqueueTestComplete();
        }