public void ShouldConvertValidStringIntoGuid()
 {
     PrimitiveMapper mapper = new PrimitiveMapper();
     Guid guid = Guid.NewGuid();
     Guid actual = mapper.ToGuid(mapper.ToString(guid));
     Assert.AreEqual(guid, actual, "The GUID was not parsed correctly.");
 }
 public void ShouldCovertValidStringToDate()
 {
     PrimitiveMapper mapper = new PrimitiveMapper();
     DateTime original = DateTime.Today;  // date only
     DateTime actual = mapper.ToDateTime(mapper.ToString(original));
     Assert.AreEqual(original, actual, "The date/time was not parsed correctly.");
 }
 public IEnumerable<AddressItemData> GetAddressItems(string customerId)
 {
     PrimitiveMapper mapper = new PrimitiveMapper();
     Customer customer = customerRepository.GetCustomer(mapper.ToGuid(customerId));
     if (customer == null)
     {
         throw new AdapterException(HttpStatusCode.NotFound, "A customer with the given ID was not found.");
     }
     return itemRepository.GetAddressItems(customer).Select(s => AddressItemMapper.Convert(s)).ToArray();
 }
 public void ShouldThrowAdapterExceptionForInvalidGuid()
 {
     try
     {
         PrimitiveMapper mapper = new PrimitiveMapper();
         mapper.ToGuid("123");
         Assert.Fail("The invalid GUID should have caused an exception to be thrown.");
     }
     catch (AdapterException exception)
     {
         Assert.AreEqual(HttpStatusCode.BadRequest, exception.StatusCode, "The wrong status code was sent.");
     }
 }
 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;
 }
Esempio n. 6
0
        public AddressItemData Convert(AddressItem item)
        {
            if (item == null)
            {
                return(null);
            }
            PrimitiveMapper mapper = new PrimitiveMapper();
            AddressItemData data   = new AddressItemData();

            data.AddressItemId = mapper.ToString(item.AddressItemId);
            data.CustomerId    = mapper.ToString(item.CustomerId);
            data.Key           = item.Key;
            data.Value         = item.Value;
            return(data);
        }
        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 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;
 }
Esempio n. 9
0
        public AddressItem Convert(AddressItemData data)
        {
            if (data == null)
            {
                return(null);
            }
            PrimitiveMapper mapper = new PrimitiveMapper();
            AddressItem     item   = new AddressItem();

            if (!String.IsNullOrWhiteSpace(data.AddressItemId))
            {
                item.AddressItemId = mapper.ToGuid(data.AddressItemId);
            }
            item.CustomerId = mapper.ToGuid(data.CustomerId);
            item.Key        = data.Key;
            item.Value      = data.Key;
            return(item);
        }
        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 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 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 RemoveAddressItem(string settingId)
 {
     PrimitiveMapper mapper = new PrimitiveMapper();
     AddressItem item = itemRepository.GetAddressItem(mapper.ToGuid(settingId));
     if (item == null)
     {
         throw new AdapterException(HttpStatusCode.NotFound, "An address item with the given ID was not found.");
     }
     itemRepository.Remove(item);
 }
        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.");
        }
 private Customer getCustomer(string customerId)
 {
     PrimitiveMapper mapper = new PrimitiveMapper();
     Guid customerGuid = mapper.ToGuid(customerId);
     Customer customer = customerRepository.GetCustomer(customerGuid);
     if (customer == null)
     {
         throw new AdapterException(HttpStatusCode.NotFound, "A customer with the given ID was not found.");
     }
     return customer;
 }