public void GetAll_SereviceReturnException_ReturnOk()
        {
            AddressService.ResetCalls();

            var  exceptionMessage = "some exception message";
            long clientId         = 1;
            var  label            = "test address 1";

            AddressService.Setup(x => x.GetAddresses(clientId, label, false, 1, 20)).Throws(new Exception(exceptionMessage));

            var controller = new AddressController(AddressService.Object, Logger.Object)
            {
                ControllerContext = new ControllerContext {
                    HttpContext = new DefaultHttpContext()
                }
            };

            controller.ControllerContext.HttpContext.Items.Add("clientId", clientId);

            var actionResult = controller.GetAll(label, false, true, 1, 20);
            var result       = actionResult as OkObjectResult;
            var result1      = actionResult as ObjectResult;

            AddressService.Verify(x => x.GetAddresses(clientId, label, false, 1, 20), Times.Once);
            Assert.AreEqual(result, null);
            Assert.IsTrue(result == null);
            Assert.IsTrue(result1 != null);
            Assert.AreEqual(result1.StatusCode, 500);
            Assert.AreEqual(result1.Value, exceptionMessage);
        }
        public void GetAllAddresses()
        {
            var mock = new Mock <IRepository <Address> >();

            mock.Setup(repo => repo.GetAll()).Returns(GetAllTest());
            var factoryMock = new Mock <IRepositoryFactory>();

            factoryMock.Setup(f => f.GetRepository <Address>()).Returns(mock.Object);
            var serviceMock = new Mock <ICleaningServiceServiceFactory>();

            factoryMock.Setup(f => f.GetRepository <Address>()).Returns(mock.Object);
            _addressController = new AddressController(serviceMock.Object, factoryMock.Object);

            var json    = _addressController.GetAll();
            var address = json.Value as List <Address>;

            Assert.NotNull(json);
            Assert.NotNull(address);
            Assert.AreEqual(address.Count, 2);

            //var mock1 = new Mock<IRepository<Address>>();
            //mock1.Setup(repo => repo.GetAll().FirstOrDefault()).Returns(GetFirstOrDefaultTest(1));
            //var factoryMock1 = new Mock<IRepositoryFactory>();
            //factoryMock1.Setup(f => f.GetRepository<Address>()).Returns(mock1.Object);
            //_addressController = new AddressController(factoryMock1.Object);

            //json = _addressController.GetAll();
            //address = json.Value as List<Address>;
            //Assert.AreEqual();
        }
        public void Create()
        {
            var     memoryStore = new List <Address>();
            Address addressStub = new Address {
                City = "Lviv"
            };
            var mock = new Mock <IRepository <Address> >();

            mock.Setup(repo => repo.GetAll()).Returns(memoryStore.AsQueryable());
            mock.Setup(repo => repo.Create(It.IsAny <Address>())).Returns((Address address) => {
                address.Id = 1;
                memoryStore.Add(address);
                return(address);
            });
            var factoryMock = new Mock <IRepositoryFactory>();

            factoryMock.Setup(f => f.GetRepository <Address>()).Returns(mock.Object);
            var serviceMock = new Mock <ICleaningServiceServiceFactory>();

            factoryMock.Setup(f => f.GetRepository <Address>()).Returns(mock.Object);
            _addressController = new AddressController(serviceMock.Object, factoryMock.Object);
            var emptyJson = _addressController.GetAll();

            Assert.IsNotNull(emptyJson);
            var emptyStore = emptyJson.Value as List <Address>;

            Assert.IsNotNull(emptyStore);
            Assert.AreEqual(emptyStore.Count, 0);
            var json = _addressController.Create(addressStub);

            Assert.IsNotNull(json);
            var result = json.Value as Address;

            Assert.NotNull(result);
            Assert.AreEqual(result.Id, 1);
            Assert.AreEqual(result.City, addressStub.City);
            var notEmptyJson = _addressController.GetAll();

            Assert.IsNotNull(notEmptyJson);
            var notEmptyStore = notEmptyJson.Value as List <Address>;

            Assert.IsNotNull(notEmptyStore);
            Assert.AreEqual(notEmptyStore.Count, 1);
        }
        private void ChangeAddress_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (ChangeAddress.Text.Equals(""))
            {
                boxic2.Visibility = Visibility.Collapsed;
                Adrese.Visibility = Visibility.Collapsed;
            }

            City          city    = null;
            List <string> gradovi = new List <string>();

            Adrese.ItemsSource = new List <string>();
            if (!ChangeCurrentCity.Text.Equals(""))
            {
                try
                {
                    string[] parts   = ChangeCurrentCity.Text.Split(", ");
                    string   citystr = parts[0];
                    City     ci      = new City()
                    {
                        Name = citystr
                    };
                    city = cityController.GetCityByName(ci);
                } catch (Exception)
                {
                }
            }
            if (city != null)
            {
                foreach (Address address in addressController.GetAdressesByCity(city))
                {
                    if (address.Street.ToLower().Contains(ChangeAddress.Text))
                    {
                        gradovi.Add(address.Street + ", " + address.Number.ToString());
                    }
                }
            }
            else
            {
                foreach (Address address in addressController.GetAll())
                {
                    if (address.Street.ToLower().Contains(ChangeAddress.Text))
                    {
                        gradovi.Add(address.Street + ", " + address.Number);
                    }
                }
            }
            if (gradovi.Count > 0)
            {
                boxic2.Visibility = Visibility.Visible;
                Adrese.Visibility = Visibility.Visible;
            }
            Adrese.ItemsSource = gradovi;
        }
        public void GetAll_AddressesExisted_ReturnCorrectPagging()
        {
            AddressService.ResetCalls();

            var  id         = 128;
            var  value      = "htpjhjh6556hj";
            var  nodeId     = 3;
            var  privateKey = "4653fgdg5dxgfdh4get14";
            long clientId   = 1;
            var  label      = "test address 1";
            var  amount     = 456.1245m;

            AddressService.Setup(x => x.GetAddresses(clientId, label, false, 2, 2)).Returns(new PagedList <Address>
            {
                List = new List <Address>
                {
                    new Address {
                        Id = id, IsUsed = true, NodeId = nodeId, Value = value, IsDeleted = false, PrivateKey = privateKey, ClientId = clientId, Label = label, Amount = amount
                    }
                },
                PageCount  = 2,
                PageIndex  = 2,
                PageSize   = 2,
                TotalCount = 3
            });

            var controller = new AddressController(AddressService.Object, Logger.Object)
            {
                ControllerContext = new ControllerContext {
                    HttpContext = new DefaultHttpContext()
                }
            };

            controller.ControllerContext.HttpContext.Items.Add("clientId", clientId);

            var actionResult = controller.GetAll(label, false, false, 2, 2);
            var result       = actionResult as OkObjectResult;
            var pagedResult  = result.Value as PagedList <WebApi.Areas.V1.Models.Address>;

            AddressService.Verify(x => x.GetAddresses(clientId, label, false, 2, 2), Times.Once);
            Assert.AreEqual(result.StatusCode, 200);
            Assert.AreEqual(pagedResult.PageCount, 2);
            Assert.AreEqual(pagedResult.PageIndex, 2);
            Assert.AreEqual(pagedResult.PageSize, 2);
            Assert.AreEqual(pagedResult.TotalCount, 3);
            Assert.AreEqual(pagedResult.List.Count(), 1);
            Assert.IsTrue(pagedResult.List.Any(t => t.IsUsed == true && t.Id == id && t.Value == value && t.NodeId == nodeId && t.ClientId == clientId && t.Label == label && t.Amount == amount));
        }
        public void GetAll_AddressesNotExisted_ReturnCorrect()
        {
            AddressService.ResetCalls();

            long clientId = 1;
            var  label    = "test address 1";

            AddressService.Setup(x => x.GetAddresses(clientId, label, false, 1, 20)).Returns(new PagedList <Address>
            {
                List       = new List <Address>(),
                PageCount  = 0,
                PageIndex  = 1,
                PageSize   = 20,
                TotalCount = 0
            });

            var controller = new AddressController(AddressService.Object, Logger.Object)
            {
                ControllerContext = new ControllerContext {
                    HttpContext = new DefaultHttpContext()
                }
            };

            controller.ControllerContext.HttpContext.Items.Add("clientId", clientId);

            var actionResult = controller.GetAll(label, false, false, 1, 20);
            var result       = actionResult as OkObjectResult;
            var pagedResult  = result.Value as PagedList <WebApi.Areas.V1.Models.Address>;

            AddressService.Verify(x => x.GetAddresses(clientId, label, false, 1, 20), Times.Once);
            Assert.AreEqual(result.StatusCode, 200);
            Assert.AreEqual(pagedResult.PageCount, 0);
            Assert.AreEqual(pagedResult.PageIndex, 1);
            Assert.AreEqual(pagedResult.PageSize, 20);
            Assert.AreEqual(pagedResult.TotalCount, 0);
            Assert.AreEqual(pagedResult.List.Count(), 0);
        }
        public void GetAllWithPrivateKeys_AddressesExisted_ReturnOk()
        {
            AddressService.ResetCalls();

            var  id1         = 126;
            var  id2         = 127;
            var  id3         = 128;
            var  value1      = "hgktb865fjygf";
            var  value2      = "hktgh568547gt";
            var  value3      = "htpjhjh6556hj";
            var  privateKey1 = "4653fgdg5dxgfdh4get14";
            var  privateKey2 = "2356fgdg5dxgfdh4get64";
            var  privateKey3 = "7345fgdg5dxgfdh4get465";
            var  nodeId1     = 12;
            var  nodeId2     = 22;
            long clientId    = 1;
            var  label       = "test address 1";
            var  amount1     = 456.1245m;
            var  amount2     = 12.4512m;
            var  amount3     = 145.4512m;

            AddressService.Setup(x => x.GetAddresses(clientId, label, false, 1, 20)).Returns(new PagedList <Address>
            {
                List = new List <Address>
                {
                    new Address {
                        Id = id1, IsUsed = true, NodeId = nodeId1, Value = value1, IsDeleted = false, PrivateKey = privateKey1, ClientId = clientId, Label = label, Amount = amount1
                    },
                    new Address {
                        Id = id2, IsUsed = true, NodeId = nodeId2, Value = value2, IsDeleted = false, PrivateKey = privateKey2, ClientId = clientId, Label = label, Amount = amount2
                    },
                    new Address {
                        Id = id3, IsUsed = true, NodeId = nodeId2, Value = value3, IsDeleted = false, PrivateKey = privateKey3, ClientId = clientId, Label = label, Amount = amount3
                    }
                },
                PageCount  = 1,
                PageIndex  = 1,
                PageSize   = 20,
                TotalCount = 3
            });

            var controller = new AddressController(AddressService.Object, Logger.Object)
            {
                ControllerContext = new ControllerContext {
                    HttpContext = new DefaultHttpContext()
                }
            };

            controller.ControllerContext.HttpContext.Items.Add("clientId", clientId);

            var actionResult = controller.GetAll(label, false, true, 1, 20);
            var result       = actionResult as OkObjectResult;
            var pagedResult  = result.Value as PagedList <AddressWithPrivateKey>;

            AddressService.Verify(x => x.GetAddresses(clientId, label, false, 1, 20), Times.Once);
            Assert.AreEqual(result.StatusCode, 200);
            Assert.AreEqual(pagedResult.PageCount, 1);
            Assert.AreEqual(pagedResult.PageIndex, 1);
            Assert.AreEqual(pagedResult.PageSize, 20);
            Assert.AreEqual(pagedResult.TotalCount, 3);
            Assert.AreEqual(pagedResult.List.Count(), 3);
            Assert.IsTrue(pagedResult.List.Any(t => t.IsUsed == true && t.Id == id1 && t.Value == value1 && t.NodeId == nodeId1 && t.ClientId == clientId && t.Label == label && t.PrivateKey == privateKey1 && t.Amount == amount1));
            Assert.IsTrue(pagedResult.List.Any(t => t.IsUsed == true && t.Id == id2 && t.Value == value2 && t.NodeId == nodeId2 && t.ClientId == clientId && t.Label == label && t.PrivateKey == privateKey2 && t.Amount == amount2));
            Assert.IsTrue(pagedResult.List.Any(t => t.IsUsed == true && t.Id == id3 && t.Value == value3 && t.NodeId == nodeId2 && t.ClientId == clientId && t.Label == label && t.PrivateKey == privateKey3 && t.Amount == amount3));
        }