Exemplo n.º 1
0
        public async Task Create_CallServiceCreateClient_When_PassingNewClient()
        {
            var newClient = new NewIdSrvClientDto {
                Name = "c", Secret = "s"
            };

            this.ClientServiceMock.Setup(v => v.CreateClientAsync(newClient)).ReturnsAsync(true);
            var controller = new ClientsController(this.ClientServiceMock.Object);
            await controller.Create(newClient);

            this.ClientServiceMock.Verify(v => v.CreateClientAsync(newClient), Times.Once);
        }
Exemplo n.º 2
0
        public void Create_ThrowsClientRepositoryException_When_RepositoryReturnUnexpetedResponse()
        {
            this.ClientRepository
            .Setup(v => v.CreateAsync(It.IsAny <NewIdSrvClientDto>()))
            .ReturnsAsync(this.UnexpectedRepositoryResponse);
            var controller = new ClientController(this.ClientRepository.Object);
            var clientDto  = new NewIdSrvClientDto {
                Name = "u", Secret = "p"
            };

            Assert.ThrowsAsync <ClientRepositoryException>(() => controller.Create(clientDto));
        }
Exemplo n.º 3
0
        public async Task Create_InvokeCreateFromRepository_With_PassedDto_When_PassingNameAndSecretAndUriInDto()
        {
            this.ClientRepository
            .Setup(v => v.CreateAsync(It.IsAny <NewIdSrvClientDto>()))
            .ReturnsAsync(RepositoryResponse.Success);
            var controller = new ClientController(this.ClientRepository.Object);
            var clientDto  = new NewIdSrvClientDto {
                Name = "u", Secret = "p", Uri = "u"
            };
            IHttpActionResult httpResult = await controller.Create(clientDto);

            this.ClientRepository.Verify(v => v.CreateAsync(clientDto));
        }
Exemplo n.º 4
0
        public async Task Create_ReturnConflict_When_RepositoryReturnConflict()
        {
            this.ClientRepository
            .Setup(v => v.CreateAsync(It.IsAny <NewIdSrvClientDto>()))
            .ReturnsAsync(RepositoryResponse.Conflict);
            var controller = new ClientController(this.ClientRepository.Object);
            var clientDto  = new NewIdSrvClientDto {
                Name = "u", Secret = "p"
            };
            IHttpActionResult httpResult = await controller.Create(clientDto);

            Assert.NotNull(httpResult);
            Assert.IsInstanceOf <ConflictResult>(httpResult);
        }
Exemplo n.º 5
0
        public async Task Create_ReturnOk_When_PassingDtoWithNameAndSecretAndUri_And_RepositoryReturnSuccess()
        {
            this.ClientRepository
            .Setup(v => v.CreateAsync(It.IsAny <NewIdSrvClientDto>()))
            .ReturnsAsync(RepositoryResponse.Success);
            var controller   = new ClientController(this.ClientRepository.Object);
            var newClientDto = new NewIdSrvClientDto {
                Name = "u", Secret = "p", Uri = "u"
            };
            IHttpActionResult httpResult = await controller.Create(newClientDto);

            Assert.NotNull(httpResult);
            Assert.IsInstanceOf <OkResult>(httpResult);
        }
Exemplo n.º 6
0
        public async Task <IHttpActionResult> Create(NewIdSrvClientDto client)
        {
            if (client == null || client.Name == null || client.Secret == null)
            {
                return(this.BadRequest());
            }

            RepositoryResponse response = await this.ClientRepository.CreateAsync(client);

            return
                (response == RepositoryResponse.Success ? this.Ok() :
                 response == RepositoryResponse.Conflict ? this.Conflict() as IHttpActionResult :
                 throw new ClientRepositoryException());
        }
Exemplo n.º 7
0
        public async Task <ActionResult> Create(NewIdSrvClientDto client)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(client));
            }

            bool created = await this.ClientService.CreateClientAsync(client);

            if (!created)
            {
                this.ModelState.AddModelError(string.Empty, "Такой клиент уже существует");
            }

            return(created ? this.ViewSuccess("Клиент успешно создан") : this.View(client) as ActionResult);
        }
Exemplo n.º 8
0
        public async Task Create_ReturnSelf_With_InvalilModelState_And_PassedModel_When_ClientServiceCanNotCreateClient()
        {
            var newClient = new NewIdSrvClientDto {
                Name = "n", Secret = "s"
            };

            this.ClientServiceMock
            .Setup(v => v.CreateClientAsync(It.IsAny <NewIdSrvClientDto>()))
            .Returns(Task.FromResult(false));
            var          controller = new ClientsController(this.ClientServiceMock.Object);
            ActionResult result     = await controller.Create(newClient);

            Assert.IsInstanceOf <ViewResult>(result);
            var viewResult = result as ViewResult;

            Assert.NotNull(viewResult);
            Assert.AreEqual(string.Empty, viewResult.ViewName);
            Assert.IsFalse(controller.ModelState.IsValid);
            Assert.AreEqual(newClient, controller.ViewData.Model);
        }
Exemplo n.º 9
0
        /// <inheritdoc/>
        public async Task <RepositoryResponse> CreateAsync(NewIdSrvClientDto client)
        {
            if (client == null || client.Name == null || client.Secret == null)
            {
                throw new ArgumentNullException(nameof(client));
            }

            using (IDbConnection connection = await this.GetConnection())
            {
                var compiler = new SqlServerCompiler();
                var db       = new QueryFactory(connection, compiler);
                try
                {
                    int inserted = await db.Query("Clients").InsertAsync(client);

                    return(inserted == 1 ? RepositoryResponse.Success : RepositoryResponse.Conflict);
                }
                catch (SqlCeException ex) when(ex.NativeError == 25016)
                {
                    return(RepositoryResponse.Conflict);
                }
            }
        }
Exemplo n.º 10
0
 /// <inheritdoc/>
 /// <remarks>
 /// В случае ошибки подключения к WebApi возникнет системное исключение,
 /// внутри метода оно никак не перехватывается. Метод может вернуть false только
 /// в случае успешного подключения (когда ответ WebApi интепретируется как ошибка).
 /// </remarks>
 public async Task <bool> CreateClientAsync(NewIdSrvClientDto newClient)
 {
     return(await RestApiHelpers.CallBoolApi(() => this.RestClient.CreateAsync(newClient)));
 }