Example #1
0
 public void Update(TenantModel tenant)
 {
     FirstName = tenant.FirstName;
     LastName = tenant.LastName;
     Telephone = tenant.Telephone;
     EmailAddress = tenant.EmailAddress;
 }
        [TestMethod] // [2] Update Tenant
        public void PutTenantUpdatesTenant()
        {
           
           
            //Arrange
            var tCtrl = new TenantsController();

            var newTenant = new TenantModel
            {
                FirstName = "Test",
                LastName = "LastNameTest",
                Telephone = "12223334444",
                EmailAddress = "*****@*****.**"
            };

            //The result of the Post Request
            IHttpActionResult result = tCtrl.PostTenant(newTenant);

            //Cast result of the content result so that I can gether information form the Contente
            CreatedAtRouteNegotiatedContentResult<TenantModel> contentResult = (CreatedAtRouteNegotiatedContentResult<TenantModel>)result;

            //Result containts the tenant I have just created
            result = tCtrl.GetTenant(contentResult.Content.TenantId);

            //Get tenant model from result
            OkNegotiatedContentResult<TenantModel> tenantResult = (OkNegotiatedContentResult<TenantModel>)result;

            //Act
            result = tCtrl.PutTenant(tenantResult.Content.TenantId, newTenant);

            //Assert
            Assert.IsInstanceOfType(result, typeof(StatusCodeResult));
        }
Example #3
0
 public void Update(TenantModel modelTenant)
 {
     // Copy values from input object to Tenant tenant
     FirstName = modelTenant.FirstName;
     LastName = modelTenant.LastName;
     Telephone = modelTenant.Telephone;
     EmailAddress = modelTenant.EmailAddress;
 }
        public void DeleteTenantDeletesTenant()
        {
            //Arrange:
            // Instantiate TenantsController so its methods can be called
            // Create a new tenant to be deleted, and get its tenant ID
            var tenantController = new TenantsController();

            var tenant = new TenantModel
            {
                FirstName = "Testy",
                LastName = "Testering",
                Telephone = "555-1212",
                EmailAddress = "*****@*****.**"
            };
            IHttpActionResult result = tenantController.PostTenant(tenant);
            CreatedAtRouteNegotiatedContentResult<TenantModel> contentResult =
                (CreatedAtRouteNegotiatedContentResult<TenantModel>)result;

            int tenantIdToDelete = contentResult.Content.TenantId;

            // Add a lease corresponding to the tenant
            int createdLeaseId;
            using (var leaseController = new LeasesController())
            {
                var lease = new LeaseModel
                {
                    CreatedDate = new DateTime(2014, 9, 30),
                    PropertyId = 1,
                    TenantId = tenantIdToDelete,
                    StartDate = new DateTime(2015, 1, 30),
                    Rent = 800,
                    LeaseType = Constants.RentPeriod.Monthly
                };
                IHttpActionResult leaseResult = leaseController.PostLease(lease);
                CreatedAtRouteNegotiatedContentResult<LeaseModel> leaseContentResult =
                    (CreatedAtRouteNegotiatedContentResult<LeaseModel>)leaseResult;

                createdLeaseId = leaseContentResult.Content.LeaseId;
            }

            //Act: Call DeleteTenant
            result = tenantController.DeleteTenant(tenantIdToDelete);

            //Assert:
            // Verify that HTTP result is OK
            // Verify that reading deleted tenant returns result not found
            Assert.IsInstanceOfType(result, typeof(OkNegotiatedContentResult<TenantModel>));

            result = tenantController.GetTenant(tenantIdToDelete);
            Assert.IsInstanceOfType(result, typeof(NotFoundResult));

            // Verify that the lease created above was deleted
            using (var leaseController = new LeasesController())
            {
                IHttpActionResult leaseResult = leaseController.GetLease(createdLeaseId);
                Assert.IsInstanceOfType(leaseResult, typeof(NotFoundResult));
            }
        }
        public IHttpActionResult PostTenant(TenantModel tenant)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            var dbTenant = new Tenant();

            dbTenant.Update(tenant);

            db.Tenants.Add(dbTenant);
            db.SaveChanges();

            tenant.TenantId = dbTenant.TenantId;

            return CreatedAtRoute("DefaultApi", new { id = tenant.TenantId }, tenant);
        }
        public IHttpActionResult PutTenant(int id, TenantModel tenant)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != tenant.TenantId)
            {
                return BadRequest();
            }

            var dbTentant = db.Tenants.Find(id);

            dbTentant.Update(tenant);

            db.Entry(dbTentant).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!TenantExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }
        [TestMethod]// [3] Create Tenant
        public void PostTenantCreatesTenant()
        {
            //arrange 
            var Tcrtl = new TenantsController();

            //Act
            var newTenant = new TenantModel
            {
                FirstName = "Test",
                LastName = "LastNameTest",
                Telephone = "12223334444",
                EmailAddress = "*****@*****.**"
            };

            //Result of the post request
            IHttpActionResult result = Tcrtl.PostTenant(newTenant);

            //Assert
            Assert.IsInstanceOfType(result, typeof(CreatedAtRouteNegotiatedContentResult<TenantModel>));
        }
        [TestMethod] // [4] Delete Tenant
        public void DeletePropertyDeleteProperty()
        {
            //Arrange
            var tctrl = new TenantsController();

            //Act
            var newTenant = new TenantModel
            {
                FirstName = "Test",
                LastName = "LastNameTest",
                Telephone = "12223334444",
                EmailAddress = "*****@*****.**"
            };

            //Add 'new tenant' to database using post
            //save returned value as result
            IHttpActionResult result = tctrl.PostTenant(newTenant);

            //Cast result as content result so I can gather intel on it
            CreatedAtRouteNegotiatedContentResult<TenantModel> contentResult = (CreatedAtRouteNegotiatedContentResult<TenantModel>)result;

            //Result contains the property I had just created 
            result = tctrl.GetTenant(contentResult.Content.TenantId);

            //get tenantmodel from result
            OkNegotiatedContentResult<TenantModel> tenantResult = (OkNegotiatedContentResult<TenantModel>)result;

            //Act
            result = tctrl.DeleteTenant(contentResult.Content.TenantId);

            //Assert
            //If action returns not found
            Assert.IsNotInstanceOfType(result, typeof(NotFoundResult));

            //If action retruns OK()
            Assert.IsInstanceOfType(result, typeof(OkNegotiatedContentResult<TenantModel>));


        }
        public IHttpActionResult PostTenant(TenantModel tenant)
        {
            // Validate the request
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            //Set up new Tenant object,
            //  and populate it with the values from
            //  the input TenantModel object
            Tenant dbTenant = new Tenant();
            dbTenant.Update(tenant);

            // Add the new Tenant object to the list of Tenant objects
            db.Tenants.Add(dbTenant);

            // Save the changes to the DB
            try
            {
                db.SaveChanges();
            }
            catch (Exception)
            {

                throw new Exception("Unable to add the tenant to the database.");
            }

            // Update the TenantModel object with the new tenant ID
            //  that was placed in the Tenant object after the changes
            //  were saved to the DB
            tenant.TenantId = dbTenant.TenantId;
            return CreatedAtRoute("DefaultApi", new { id = dbTenant.TenantId }, tenant);
        }
        public IHttpActionResult PutTenant(int id, TenantModel tenant)
        {
            // Validate the request
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != tenant.TenantId)
            {
                return BadRequest();
            }

            if (!TenantExists(id))
            {
                return BadRequest();
            }

            // Get the tenant record corresponding to the tenant ID, then
            //   update its properties to the values in the input TenantModel object,
            //   and then set indicator that the record has been modified
            var dbTenant = db.Tenants.Find(id);
            dbTenant.Update(tenant);
            db.Entry(dbTenant).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!TenantExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw new Exception("Unable to update the tenant in the database.");
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }
        public void PostTenantCreatesTenant()
        {
            //Arrange: Instantiate TenantsController so its methods can be called
            var tenantController = new TenantsController();

            //Act:
            // Create a TenantModel object populated with test data,
            //  and call PostTenant
            var newTenant = new TenantModel
            {
                FirstName = "Testy",
                LastName = "McTest",
                Telephone = "555-1212",
                EmailAddress = "*****@*****.**"
            };
            IHttpActionResult result = tenantController.PostTenant(newTenant);

            //Assert:
            // Verify that the HTTP result is CreatedAtRouteNegotiatedContentResult
            // Verify that the HTTP result body contains a nonzero tenant ID
            Assert.IsInstanceOfType
                (result, typeof(CreatedAtRouteNegotiatedContentResult<TenantModel>));
            CreatedAtRouteNegotiatedContentResult<TenantModel> contentResult =
                (CreatedAtRouteNegotiatedContentResult<TenantModel>)result;
            Assert.IsTrue(contentResult.Content.TenantId != 0);

            // Delete the test tenant
            result = tenantController.DeleteTenant(contentResult.Content.TenantId);
        }