public void ConstructorTest_ParametersAreValid_Success()
 {
     var controller = new ClientsController(MockApiServices, MockUnitOfWork, MockClientService, MockAccountService);
 }
        public async Task GetAllAsync_ReturnAllEntities_Success()
        {
            var clientSource = new List<ClientModel> {new ClientModel(), new ClientModel(), new ClientModel()};
            // arrange
            ClientServiceMockRepository.Setup(service => service.GetAllAsync<ClientModel>())
                .Returns(Task.FromResult(clientSource));

            const string requestedUrl = "http://localhost/clients";
            // act
            using (var config = new HttpConfiguration())
            {
                using (var request = new HttpRequestMessage(HttpMethod.Get, requestedUrl))
                {
                    request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, config);
                    using (var controller = new ClientsController(MockApiServices, MockUnitOfWork, MockClientService, MockAccountService))
                    {
                        controller.Request = request;
                        controller.Configuration = config;
                        using (HttpResponseMessage response = await controller.GetAllAsync())
                        {
                            // assert
                            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
                            Assert.AreEqual(new Uri(requestedUrl), response.RequestMessage.RequestUri);
                            List<ClientModel> clients = await response.Content.ReadAsAsync<List<ClientModel>>();
                            Assert.IsNotNull(clients);
                            Assert.AreEqual(clientSource.Count, clients.Count);
                        }
                    }
                }
            }
        }
 public void ConstructorTest_AccountServiceIsNull_ThrowException()
 {
     var controller = new ClientsController(MockApiServices, MockUnitOfWork, MockClientService, null);
 }
        public async Task DeleteAsync_EntityFound_Success()
        {
            const string entityId = "E0C3202D-8C3B-4934-AE19-7C0BB9C73DD4";
            // arrange
            ClientServiceMockRepository.Setup(
                service => service.DeleteAsync(It.Is<string>(value => value == entityId)))
                .Returns(Task.FromResult(0));
            ClientServiceMockRepository.Setup(
                service => service.DeleteAsync(It.Is<string>(value => value != entityId)))
                .Throws(new EntityNotFoundException());

            const string requestedUrl = "http://localhost/clients/" + entityId;
            // act
            using (var config = new HttpConfiguration())
            {
                using (var request = new HttpRequestMessage(HttpMethod.Delete, requestedUrl))
                {
                    request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, config);
                    using (var controller = new ClientsController(MockApiServices, MockUnitOfWork, MockClientService, MockAccountService))
                    {
                        controller.Request = request;
                        controller.Configuration = config;
                        using (HttpResponseMessage response = await controller.DeleteAsync(entityId))
                        {
                            // assert
                            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
                            Assert.AreEqual(new Uri(requestedUrl), response.RequestMessage.RequestUri);
                        }
                    }
                }
            }
        }
        public async Task CreateAsync_ModelIsValid_Success()
        {
            const string name = "Alexey";
            var model = new PostClientModel { Name = name };
            // arrange
            ClientServiceMockRepository.Setup(
                service => service.CreateAsync<PostClientModel, ClientModel>(model))
                .ReturnsAsync(new ClientModel { Name = name, Id = Guid.NewGuid().ToString() });

            const string requestedUrl = "http://localhost/clients";
            using (var config = new HttpConfiguration())
            {
                config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "{controller}/{id}",
                    defaults: new { id = RouteParameter.Optional }
                );
                using (var request = new HttpRequestMessage(HttpMethod.Post, requestedUrl))
                {
                    request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, config);
                    using (var controller = new ClientsController(MockApiServices, MockUnitOfWork, MockClientService, MockAccountService))
                    {
                        controller.RequestContext.RouteData = new HttpRouteData(route: new HttpRoute(),
                            values: new HttpRouteValueDictionary {{"controller", "clients"}});
                        controller.Request = request;
                        controller.Configuration = config;
                        using (HttpResponseMessage response = await controller.CreateAsync(model))
                        {
                            Assert.AreEqual(HttpStatusCode.Created, response.StatusCode);
                            Assert.AreEqual(new Uri(requestedUrl), response.RequestMessage.RequestUri);
                            ClientModel createdModel = await response.Content.ReadAsAsync<ClientModel>();
                            Assert.IsNotNull(createdModel);
                            Assert.IsNotNull(createdModel.Name);
                            Assert.AreEqual(name, createdModel.Name);
                        }
                    }
                }
            }
        }
 public async Task CreateAsync_ModelIsNull_ReturnBadRequestCode()
 {
     const string requestedUrl = "http://localhost/clients";
     using (var config = new HttpConfiguration())
     {
         using (var request = new HttpRequestMessage(HttpMethod.Post, requestedUrl))
         {
             request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, config);
             using (var controller = new ClientsController(MockApiServices, MockUnitOfWork, MockClientService, MockAccountService))
             {
                 controller.Request = request;
                 controller.Configuration = config;
                 using (HttpResponseMessage response = await controller.CreateAsync(null))
                 {
                     Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode);
                 }
             }
         }
     }
 }
        public async Task GetAsync_EntityNotFound_ReturnNotFoundCode()
        {
            const string entityId = "E0C3202D-8C3B-4934-AE19-7C0BB9C73DD4";
            // arrange
            ClientServiceMockRepository.Setup(
                service => service.GetAsync<ClientModel>(It.Is<string>(value => value == entityId)))
                .Returns(Task.FromResult(new ClientModel() {Id = entityId}));
            ClientServiceMockRepository.Setup(
                service => service.GetAsync<ClientModel>(It.Is<string>(value => value != entityId)))
                .Throws(new EntityNotFoundException());

            const string requestedId = "3620BAA2-2D7E-40DE-942A-21E5F2933221";
            const string requestedUrl = "http://localhost/clients/" + requestedId;
            // act
            using (var config = new HttpConfiguration())
            {
                using (var request = new HttpRequestMessage(HttpMethod.Get, requestedUrl))
                {
                    request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, config);
                    using (var controller = new ClientsController(MockApiServices, MockUnitOfWork, MockClientService, MockAccountService))
                    {
                        controller.Request = request;
                        controller.Configuration = config;
                        using (HttpResponseMessage response = await controller.GetAsync(requestedId))
                        {
                            // assert
                            Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode);
                        }
                    }
                }
            }
        }