public void Submit_Multiple_Success()
        {
            Order order = new Order { OrderID = 1, OrderDate = DateTime.Now };
            Product product = new Product { ProductID = 1, ProductName = "Choco Wafers" };
            ChangeSetEntry[] changeSet = new ChangeSetEntry[] { 
                new ChangeSetEntry { Id = 1, Entity = order, Operation = ChangeOperation.Insert },
                new ChangeSetEntry { Id = 2, Entity = product, Operation = ChangeOperation.Update }
            };

            ChangeSetEntry[] resultChangeSet = this.ExecuteSubmit(TestConstants.CatalogUrl + "Submit", "Catalog", changeSet);
            Assert.Equal(2, resultChangeSet.Length);
            Assert.True(resultChangeSet.All(p => !p.HasError));
        }
        public void GetOriginal()
        {
            ChangeSet changeSet = this.GenerateChangeSet();
            ChangeSetEntry op = changeSet.ChangeSetEntries.First();

            Product currentEntity = new Product();
            Product originalEntity = new Product();

            op.Entity = currentEntity;
            op.OriginalEntity = originalEntity;

            Product changeSetOriginalEntity = changeSet.GetOriginal(currentEntity);

            // Verify we returned the original
            Assert.Same(originalEntity, changeSetOriginalEntity);
        }
        public void Changeset_OriginalInvalidForInserts()
        {
            // can't specify an original for an insert operation
            Product curr = new Product { ProductID = 1 };
            Product orig = new Product { ProductID = 1 };
            ChangeSetEntry entry = new ChangeSetEntry { Id = 1, Entity = curr, OriginalEntity = orig, Operation = ChangeOperation.Insert };
            ChangeSet cs = null;
            Assert.Throws<InvalidOperationException>(delegate
            {
                cs = new ChangeSet(new ChangeSetEntry[] { entry });
            },
            String.Format(Resource.InvalidChangeSet, Resource.InvalidChangeSet_InsertsCantHaveOriginal));

            // get original should throw for insert operations
            entry = new ChangeSetEntry { Id = 1, Entity = curr, OriginalEntity = null, Operation = ChangeOperation.Insert };
            cs = new ChangeSet(new ChangeSetEntry[] { entry });
            Assert.Throws<InvalidOperationException>(delegate
            {
                cs.GetOriginal(curr);
            },
            String.Format(Resource.ChangeSet_OriginalNotValidForInsert));
        }
        public void GetOriginal_EntityExistsMoreThanOnce()
        {
            ChangeSet changeSet = this.GenerateChangeSet();
            ChangeSetEntry op1 = changeSet.ChangeSetEntries.Skip(0).First();
            ChangeSetEntry op2 = changeSet.ChangeSetEntries.Skip(1).First();
            ChangeSetEntry op3 = changeSet.ChangeSetEntries.Skip(2).First();

            Product currentEntity = new Product(), originalEntity = new Product();

            op1.Entity = currentEntity;
            op1.OriginalEntity = originalEntity;

            op2.Entity = currentEntity;
            op2.OriginalEntity = originalEntity;

            op3.Entity = currentEntity;
            op3.OriginalEntity = null;

            Product changeSetOriginalEntity = changeSet.GetOriginal(currentEntity);

            // Verify we returned the original
            Assert.Same(originalEntity, changeSetOriginalEntity);
        }
 public void UpdateProduct(Product product)
 {
 }
        public void Submit_ResolveActions_UnsupportedAction()
        {
            Product product = new Product { ProductID = 1, ProductName = "Choco Wafers" };
            ChangeSetEntry[] changeSet = new ChangeSetEntry[] { 
                new ChangeSetEntry { Id = 1, Entity = product, Operation = ChangeOperation.Delete }
            };

            HttpConfiguration configuration = new HttpConfiguration();
            HttpControllerDescriptor controllerDescriptor = new HttpControllerDescriptor(configuration, "NorthwindEFTestController", typeof(NorthwindEFTestController));
            DataControllerDescription description = DataControllerDescription.GetDescription(controllerDescriptor);
            Assert.Throws<InvalidOperationException>(
                () => DataController.ResolveActions(description, changeSet),
                String.Format(Resource.DataController_InvalidAction, "Delete", "Product"));
        }
        public void Submit_Authorization_Fail_Global()
        {
            TestAuthAttribute.Reset();

            Product product = new Product { ProductID = 1, ProductName = "Choco Wafers" };
            ChangeSetEntry[] changeSet = new ChangeSetEntry[] { 
                new ChangeSetEntry { Id = 1, Entity = product, Operation = ChangeOperation.Update }
            };

            TestAuthAttribute.FailLevel = "Global";
            HttpResponseMessage response = this.ExecuteSelfHostRequest("http://testhost/TestAuth/Submit", "TestAuth", changeSet);

            Assert.True(TestAuthAttribute.Log.SequenceEqual(new string[] { "Global" }));
            Assert.Equal("Not Authorized", response.ReasonPhrase);
            Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
        }
        public void Submit_Authorization_Success()
        {
            TestAuthAttribute.Reset();

            Product product = new Product { ProductID = 1, ProductName = "Choco Wafers" };
            ChangeSetEntry[] changeSet = new ChangeSetEntry[] { 
                new ChangeSetEntry { Id = 1, Entity = product, Operation = ChangeOperation.Update }
            };

            ChangeSetEntry[] resultChangeSet = this.ExecuteSubmit("http://testhost/TestAuth/Submit", "TestAuth", changeSet);
            Assert.Equal(1, resultChangeSet.Length);
            Assert.True(TestAuthAttribute.Log.SequenceEqual(new string[] { "Global", "Class", "SubmitMethod", "UserMethod" }));
        }
 public void UpdateProduct(Product product)
 {
     // demonstrate that the current ActionContext can be accessed by
     // controller actions
     string host = this.ActionContext.ControllerContext.Request.Headers.Host;
 }