public void ShouldFindCustomerAndRemoveIt()
        {
            ICustomerRepository repository = Substitute.For<ICustomerRepository>();
            Customer dto = new Customer();
            setReturnedCustomer(repository, dto);

            CustomerAdapter adapter = new CustomerAdapter(repository);
            adapter.RemoveCustomer(Guid.Empty.ToString("N"));

            repository.Received().GetCustomer(Guid.Empty);
            repository.Received().Remove(dto);
        }
 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 Update(Customer original, Customer modified)
 {
     if (original == null)
     {
         throw new ArgumentNullException("original");
     }
     if (modified == null)
     {
         throw new ArgumentNullException("modified");
     }
     original.Name = modified.Name;
     original.BirthDate = modified.BirthDate;
     original.Height = modified.Height;
     entities.SaveChanges();
 }
 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 ShouldGetCustomersFromRepository()
        {
            ICustomerRepository repository = Substitute.For<ICustomerRepository>();
            Customer[] customers = new Customer[] { new Customer() };
            repository.GetCustomers().Returns(customers);

            ICustomerMapper mapper = Substitute.For<ICustomerMapper>();
            mapper.Convert(Arg.Any<Customer>()).Returns(new CustomerData());

            CustomerAdapter adapter = new CustomerAdapter(repository) { CustomerMapper = mapper };
            IEnumerable<CustomerData> results = adapter.GetCustomers();

            repository.Received().GetCustomers();
            mapper.Received(1).Convert(customers[0]);

            Assert.IsNotNull(results, "Null was returned by the adapter.");
            Assert.AreEqual(1, results.Count(), "The wrong number of view models were returned.");
        }
        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 ShouldConvertDataObjectToViewModel()
        {
            Customer customer = new Customer()
            {
                CustomerId = Guid.NewGuid(),
                Name = "Bob",
                Height = 123,
                BirthDate = new DateTime(2012, 12, 31)
            };

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

            PrimitiveMapper primitiveMapper = new PrimitiveMapper();
            Assert.AreEqual(customer.BirthDate, primitiveMapper.ToDateTime(viewModel.BirthDate), "The birth date was not mapped.");
            Assert.AreEqual(customer.CustomerId, Guid.Parse(viewModel.CustomerId), "The customer ID was not mapped.");
            Assert.AreEqual(customer.Height, viewModel.Height, "The height was not mapped.");
            Assert.AreEqual(customer.Name, viewModel.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 ShouldConvertReturnedAddressItemsToViewModel()
        {
            ICustomerRepository customerRepository = Substitute.For<ICustomerRepository>();
            Customer customer = new Customer();
            customerRepository.GetCustomer(Arg.Any<Guid>()).Returns(customer);

            IAddressItemRepository itemRepository = Substitute.For<IAddressItemRepository>();
            AddressItem[] items = new AddressItem[] { new AddressItem() };
            itemRepository.GetAddressItems(customer).Returns(items);

            IAddressItemMapper mapper = Substitute.For<IAddressItemMapper>();
            mapper.Convert(Arg.Any<AddressItem>()).Returns(new AddressItemData());

            AddressItemAdapter adapter = new AddressItemAdapter(customerRepository, itemRepository);
            adapter.AddressItemMapper = mapper;
            string customerId = Guid.Empty.ToString("N");
            IEnumerable<AddressItemData> models = adapter.GetAddressItems(customerId);

            Assert.AreEqual(1, models.Count(), "The wrong number of view models were returned.");
            customerRepository.Received(1).GetCustomer(Arg.Any<Guid>());
            itemRepository.Received(1).GetAddressItems(customer);
            mapper.Received(1).Convert(Arg.Any<AddressItem>());
        }
 public IEnumerable<AddressItem> GetAddressItems(Customer customer)
 {
     return entities.AddressItems.Where(s => s.Customer == customer).ToArray();
 }
 private static void setReturnedCustomer(ICustomerRepository repository, Customer customer)
 {
     repository.GetCustomer(Arg.Any<Guid>()).Returns(customer);
 }
        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 void Remove(Customer customer)
 {
     entities.Customers.Remove(customer);
     entities.SaveChanges();
 }
 public void Add(Customer customer)
 {
     entities.Customers.Add(customer);
     entities.SaveChanges();
 }