public virtual ActionResult Create(CustomerData data)
 {
     if (!ModelState.IsValid)
     {
         return View(Views.Create, data);
     }
     adapter.AddCustomer(data);
     return RedirectToAction(MVC.Classic.Index());
 }
 public virtual ActionResult Edit(CustomerData data)
 {
     if (!ModelState.IsValid)
     {
         return View(Views.Edit, data);
     }
     adapter.UpdateCustomer(data);
     return RedirectToAction(MVC.Classic.Index());
 }
        public void ShouldDeleteCustomerAndRedirectToIndex()
        {
            ICustomerAdapter adapter = Substitute.For<ICustomerAdapter>();
            using (ClassicController controller = new ClassicController(adapter))
            {
                CustomerData data = new CustomerData() { CustomerId = Guid.NewGuid().ToString("N") };
                ActionResult result = controller.Delete(data);

                adapter.Received().RemoveCustomer(data.CustomerId);

                ActionResultHelper.AssertRedirected(result, controller.Name, controller.ActionNames.Index);
            }
        }
        public void ShouldCreateCustomerAndReturnToIndex()
        {
            ICustomerAdapter adapter = Substitute.For<ICustomerAdapter>();
            using (ClassicController controller = new ClassicController(adapter))
            {
                CustomerData data = new CustomerData();
                ActionResult result = controller.Create(data);

                adapter.Received().AddCustomer(data);

                ActionResultHelper.AssertRedirected(result, controller.Name, controller.ActionNames.Index);
            }
        }
 public CustomerData Convert(Customer customer)
 {
     if (customer == null)
     {
         return null;
     }
     PrimitiveMapper mapper = new PrimitiveMapper();
     CustomerData viewModel = new CustomerData();
     viewModel.CustomerId = mapper.ToString(customer.CustomerId);
     viewModel.Name = customer.Name;
     viewModel.BirthDate = mapper.ToString(customer.BirthDate);
     viewModel.Height = customer.Height;
     return viewModel;
 }
        public void ShouldCreateAndReturnCustomer()
        {
            ICustomerAdapter adapter = Substitute.For<ICustomerAdapter>();
            CustomerData added = new CustomerData();
            adapter.AddCustomer(Arg.Any<CustomerData>()).Returns(added);
            using (KnockoutController controller = new KnockoutController(adapter))
            {
                CustomerData data = new CustomerData();
                ActionResult result = controller.Create(data);

                CustomerData model = ActionResultHelper.AssertJsonWithHttpStatusCode<CustomerData>(result, HttpStatusCode.Created);
                Assert.AreSame(added, model, "The added customer was not returned.");

                adapter.Received().AddCustomer(data);
            }
        }
 public Customer Convert(CustomerData viewModel)
 {
     if (viewModel == null)
     {
         return null;
     }
     PrimitiveMapper mapper = new PrimitiveMapper();
     Customer customer = new Customer();
     if (!String.IsNullOrWhiteSpace(viewModel.CustomerId))
     {
         customer.CustomerId = mapper.ToGuid(viewModel.CustomerId);
     }
     customer.Name = viewModel.Name;
     customer.BirthDate = mapper.ToDateTime(viewModel.BirthDate);
     customer.Height = viewModel.Height;
     return customer;
 }
        public void ShouldConvertViewModelToDataObjectIgnoringMissingCustomerId()
        {
            CustomerData viewModel = new CustomerData()
            {
                CustomerId = null,
                Name = "Bob",
                Height = 123,
                BirthDate = "12/31/2012"
            };

            CustomerMapper mapper = new CustomerMapper();
            Customer customer = mapper.Convert(viewModel);

            PrimitiveMapper primitiveMapper = new PrimitiveMapper();
            Assert.AreEqual(viewModel.BirthDate, primitiveMapper.ToString(customer.BirthDate), "The birth date was not mapped.");
            Assert.AreEqual(viewModel.Height, customer.Height, "The height was not mapped.");
            Assert.AreEqual(viewModel.Name, customer.Name, "The name was not mapped.");
        }
        public void ShouldFindOriginalAndPassModifiedToRepository()
        {
            ICustomerRepository repository = Substitute.For<ICustomerRepository>();
            Customer original = new Customer();
            setReturnedCustomer(repository, original);

            ICustomerMapper mapper = Substitute.For<ICustomerMapper>();
            Customer modified = new Customer();
            CustomerData viewModel = new CustomerData() { CustomerId = Guid.Empty.ToString("N") };
            mapper.Convert(Arg.Any<CustomerData>()).Returns(modified);

            CustomerAdapter adapter = new CustomerAdapter(repository) { CustomerMapper = mapper };
            adapter.UpdateCustomer(viewModel);

            repository.Received().GetCustomer(Arg.Any<Guid>());
            mapper.Received().Convert(viewModel);
            repository.Received().Update(original, modified);
        }
        public void ShouldConvertViewModelToDataObject()
        {
            CustomerData viewModel = new CustomerData()
            {
                CustomerId = Guid.NewGuid().ToString("N"),
                Name = "Bob",
                Height = 123,
                BirthDate = "12/31/2012"
            };

            CustomerMapper mapper = new CustomerMapper();
            Customer customer = mapper.Convert(viewModel);

            PrimitiveMapper primitiveMapper = new PrimitiveMapper();
            Assert.AreEqual(viewModel.BirthDate, primitiveMapper.ToString(customer.BirthDate), "The birth date was not mapped.");
            Assert.AreEqual(Guid.Parse(viewModel.CustomerId), customer.CustomerId, "The customer ID was not mapped.");
            Assert.AreEqual(viewModel.Height, customer.Height, "The height was not mapped.");
            Assert.AreEqual(viewModel.Name, customer.Name, "The name was not mapped.");
        }
        public void ShouldAddConvertedCustomerToRepositoryAndReturnUpdated()
        {
            ICustomerRepository repository = Substitute.For<ICustomerRepository>();
            ICustomerMapper mapper = Substitute.For<ICustomerMapper>();
            Customer dto = new Customer();
            CustomerData viewModelInput = new CustomerData();
            CustomerData viewModelOutput = new CustomerData();
            mapper.Convert(viewModelInput).Returns(dto);
            mapper.Convert(dto).Returns(viewModelOutput);

            CustomerAdapter adapter = new CustomerAdapter(repository) { CustomerMapper = mapper };

            CustomerData result = adapter.AddCustomer(viewModelInput);

            Assert.AreSame(viewModelOutput, result, "The updated view model was not returned.");

            mapper.Received().Convert(viewModelInput);
            repository.Received().Add(dto);
            mapper.Received().Convert(dto);
        }
        public void ShouldCreateAndReturnCustomer()
        {
            ICustomerAdapter adapter = Substitute.For<ICustomerAdapter>();
            CustomerData added = new CustomerData();
            adapter.AddCustomer(Arg.Any<CustomerData>()).Returns(added);
            using (CustomersController controller = new CustomersController(adapter))
            using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "~/api/customers"))
            {
                request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration());
                CustomerData data = new CustomerData();
                HttpResponseMessage message = controller.Post(request, data);

                Assert.AreEqual(HttpStatusCode.Created, message.StatusCode, "The wrong status code was returned.");
                Assert.IsInstanceOfType(message.Content, typeof(ObjectContent), "The content was not the serialized result.");
                ObjectContent content = (ObjectContent)message.Content;
                Assert.AreSame(added, content.Value, "The wrong object was returned.");

                adapter.Received().AddCustomer(data);
            }
        }
        public void ShouldBuildIndexViewWithCustomersOrderByName()
        {
            ICustomerAdapter adapter = Substitute.For<ICustomerAdapter>();
            CustomerData[] customers = new CustomerData[]
            {
                new CustomerData() { Name = "Gary" },
                new CustomerData() { Name = "Louis" },
                new CustomerData() { Name = "Bob" }
            };
            adapter.GetCustomers().Returns(customers);

            using (ClassicController controller = new ClassicController(adapter))
            {
                ActionResult result = controller.Index();

                adapter.Received().GetCustomers();

                CustomerData[] model = ActionResultHelper.AssertViewWithModel<CustomerData[]>(result, controller.Views.Index);
                string[] names = model.Select(customer => customer.Name).ToArray();
                string[] expected = new string[] { "Bob", "Gary", "Louis" };
                CollectionAssert.AreEqual(expected, names, "The customers were not ordered by name.");
            }
        }
        public void ShouldGetCustomerById()
        {
            ICustomerAdapter adapter = Substitute.For<ICustomerAdapter>();
            CustomerData data = new CustomerData();
            adapter.GetCustomer(Arg.Any<string>()).Returns(data);
            using (CustomersController controller = new CustomersController(adapter))
            {
                string customerId = Guid.NewGuid().ToString("N");
                using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "~/api/customers/" + customerId))
                {
                    request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration());
                    HttpResponseMessage response = controller.Get(request, customerId);
                    Assert.IsInstanceOfType(response.Content, typeof(ObjectContent<CustomerData>), "Customer data was not returned.");
                    ObjectContent<CustomerData> content = (ObjectContent<CustomerData>)response.Content;
                    CustomerData result = (CustomerData)content.Value;

                    Assert.AreSame(data, result, "The wrong customer was returned.");

                    adapter.Received().GetCustomer(customerId);
                }
            }
        }
        public void ShouldUpdateCustomer()
        {
            ICustomerAdapter adapter = Substitute.For<ICustomerAdapter>();
            using (KnockoutController controller = new KnockoutController(adapter))
            {
                CustomerData data = new CustomerData();
                ActionResult result = controller.Edit(data);

                ActionResultHelper.AssertHttpStatusCode(result, HttpStatusCode.OK);

                adapter.Received().UpdateCustomer(data);
            }
        }
        public void ShouldRedisplayViewWhenEditValidationFails()
        {
            ICustomerAdapter adapter = Substitute.For<ICustomerAdapter>();
            using (ClassicController controller = new ClassicController(adapter))
            {
                controller.ModelState.AddModelError("txtName", "You must provide a name for the customer.");

                CustomerData data = new CustomerData();
                ActionResult result = controller.Edit(data);

                CustomerData model = ActionResultHelper.AssertViewWithModel<CustomerData>(result, controller.Views.Edit);
                Assert.AreSame(data, model, "The model was not passed to the view.");
            }
        }
 public HttpResponseMessage Put(HttpRequestMessage request, CustomerData data)
 {
     adapter.UpdateCustomer(data);
     return request.CreateResponse(HttpStatusCode.OK, "The customer was updated.");
 }
 public HttpResponseMessage Post(HttpRequestMessage request, CustomerData data)
 {
     CustomerData result = adapter.AddCustomer(data);
     return request.CreateResponse(HttpStatusCode.Created, result);
 }
        public void ShouldRetrieveCustomerForEdit()
        {
            ICustomerAdapter adapter = Substitute.For<ICustomerAdapter>();
            CustomerData data = new CustomerData();
            adapter.GetCustomer(Arg.Any<string>()).Returns(data);

            using (ClassicController controller = new ClassicController(adapter))
            {
                string customerId = Guid.NewGuid().ToString("N");
                ActionResult result = controller.Edit(customerId);

                CustomerData model = ActionResultHelper.AssertViewWithModel<CustomerData>(result, controller.Views.Edit);

                adapter.Received().GetCustomer(customerId);

                Assert.AreSame(data, model, "The customer data was not passed to the view.");
            }
        }
        public void ShouldMapCustomerToViewModel()
        {
            ICustomerRepository repository = Substitute.For<ICustomerRepository>();
            Customer dto = new Customer();
            setReturnedCustomer(repository, dto);

            ICustomerMapper mapper = Substitute.For<ICustomerMapper>();
            CustomerData viewModel = new CustomerData();
            mapper.Convert(dto).Returns(viewModel);

            CustomerAdapter adapter = new CustomerAdapter(repository) { CustomerMapper = mapper };
            PrimitiveMapper primitiveMapper = new PrimitiveMapper();
            CustomerData data = adapter.GetCustomer(primitiveMapper.ToString(Guid.Empty));

            repository.Received().GetCustomer(dto.CustomerId);
            mapper.Received().Convert(dto);

            Assert.IsNotNull(data, "The returned view model was null.");
        }
 public CustomerData AddCustomer(CustomerData customerData)
 {
     Customer customer = CustomerMapper.Convert(customerData);
     customerRepository.Add(customer);
     return CustomerMapper.Convert(customer);
 }
 public virtual ActionResult Create(CustomerData data)
 {
     CustomerData result = adapter.AddCustomer(data);
     return new JsonWithHttpStatusCodeResult(HttpStatusCode.Created) { Data = result };
 }
 public virtual ActionResult Delete(CustomerData customerData)
 {
     adapter.RemoveCustomer(customerData.CustomerId);
     return RedirectToAction(MVC.Classic.Index());
 }
        public void ShouldRedisplayViewIfCreateValidationFails()
        {
            ICustomerAdapter adapter = Substitute.For<ICustomerAdapter>();
            using (ClassicController controller = new ClassicController(adapter))
            {
                controller.ModelState.AddModelError("txtName", "You must provide a customer name.");

                CustomerData data = new CustomerData();
                ActionResult result = controller.Create(data);

                CustomerData model = ActionResultHelper.AssertViewWithModel<CustomerData>(result, controller.Views.Create);
                Assert.AreSame(data, model, "The customer data was not set as the model.");
            }
        }
 public virtual ActionResult Edit(CustomerData data)
 {
     adapter.UpdateCustomer(data);
     return new HttpStatusCodeResult(HttpStatusCode.OK, "The customer was updated.");
 }
 public void UpdateCustomer(CustomerData customerData)
 {
     if (customerData == null)
     {
         throw new AdapterException(HttpStatusCode.BadRequest, "No information provided for the customer being updated.");
     }
     Customer original = getCustomer(customerData.CustomerId);
     Customer modified = CustomerMapper.Convert(customerData);
     customerRepository.Update(original, modified);
 }