Esempio n. 1
0
        public async Task Update_InvalidInput_ReturnsNull(int id, string name)
        {
            #region Arrange
            var dbContext = new ApplicationDbContext(_dbContextOptions);
            await dbContext.Database.EnsureDeletedAsync();

            var language = new Domain.Language
            {
                Name = "Name"
            };
            dbContext.Languages.Add(language);

            await dbContext.SaveChangesAsync();

            var newLanguage = new AdminLanguageModel
            {
                ID   = id,
                Name = name
            };

            var appLanguage = new Language(dbContext);
            #endregion

            #region Act
            var actualLanguage = await appLanguage.Update(newLanguage);

            #endregion

            #region Assert
            Assert.Null(actualLanguage);
            #endregion
        }
Esempio n. 2
0
        public async Task Create_InvalidInput_ReturnNull(string name)
        {
            #region Arrange
            var dbContext = new ApplicationDbContext(_dbContextOptions);
            await dbContext.Database.EnsureDeletedAsync();

            await dbContext.SaveChangesAsync();

            var newLanguage = new AdminLanguageModel
            {
                Name = name
            };

            var appLanguage = new Language(dbContext);
            #endregion

            #region Act
            var actualLanguage = await appLanguage.Create(newLanguage);

            #endregion

            #region Assert
            Assert.Null(actualLanguage);
            #endregion
        }
Esempio n. 3
0
        public async Task Create_InvalidRequest_ReturnsJsonResponseAndBadRequestWithErrors(string name, IEnumerable <string> expectedErrorNames, IEnumerable <string> expectedErrorValues)
        {
            #region Arrange
            await DeleteDbContent();

            var client = GetHttpClient();

            var newLanguage = new AdminLanguageModel
            {
                Name = name
            };
            #endregion

            #region Act
            var response = await client.PostAsJsonAsync("/api/language", newLanguage);

            var responseBody = await response.Content.ReadAsStreamAsync();

            var actualLanguage = await JsonSerializer.DeserializeAsync <JsonElement>(responseBody);

            var errorProp = actualLanguage.GetProperty("errors");
            var errors    = errorProp.EnumerateObject();
            #endregion

            #region Assert
            Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
            Assert.Equal(expectedErrorNames.Count(), errors.Count());
            Assert.All(expectedErrorNames, errorName => Assert.Contains(errorName, errors.Select(prop => prop.Name)));
            Assert.All(expectedErrorValues, errorValue => Assert.Contains(errorValue, errors.Select(prop => prop.Value[0].ToString())));
            #endregion
        }
Esempio n. 4
0
        public async Task Create_ValidInput_ReturnsCorrectData(string name)
        {
            #region Arrange
            var dbContext = new ApplicationDbContext(_dbContextOptions);
            await dbContext.Database.EnsureDeletedAsync();

            var newLanguage = new AdminLanguageModel
            {
                Name = name
            };

            var expectedLanguage = new LanguageModel
            {
                ID   = 1,
                Name = name
            };

            var appLanguage = new Language(dbContext);
            #endregion

            #region Act
            var actualLanguage = await appLanguage.Create(newLanguage);

            #endregion

            #region Assert
            Assert.Equal(expectedLanguage.ID, actualLanguage.ID);
            Assert.Equal(expectedLanguage.Name, actualLanguage.Name);
            #endregion
        }
Esempio n. 5
0
        public async Task Create_ValidRequest_ReturnsJsonResponseAndCreated(string name)
        {
            #region Arrange
            await DeleteDbContent();

            var client = GetHttpClient();

            var newLanguage = new AdminLanguageModel
            {
                Name = name
            };

            var expectedLanguage = new LanguageModel
            {
                ID   = 1,
                Name = name
            };
            #endregion

            #region Act
            var response = await client.PostAsJsonAsync("/api/language", newLanguage);

            var responseBody = await response.Content.ReadAsStreamAsync();

            var actualLanguage = await JsonSerializer.DeserializeAsync <LanguageModel>(responseBody);

            #endregion

            #region Assert
            Assert.Equal(HttpStatusCode.Created, response.StatusCode);
            Assert.Equal(expectedLanguage.ID, actualLanguage.ID);
            Assert.Equal(expectedLanguage.Name, actualLanguage.Name);
            #endregion
        }
Esempio n. 6
0
        public async Task <IActionResult> Update([FromBody] AdminLanguageModel adminLanguageModel)
        {
            if (await language.Update(adminLanguageModel) is LanguageModel result && result != null)
            {
                return(Ok(result));
            }

            return(NotFound());
        }
Esempio n. 7
0
        public async Task <IActionResult> Create([FromBody] AdminLanguageModel adminLanguageModel)
        {
            if (await language.Create(adminLanguageModel) is LanguageModel result && result != null)
            {
                return(CreatedAtAction(nameof(Read), new { id = result.ID }, result));
            }

            return(StatusCode((int)HttpStatusCode.InternalServerError));
        }
Esempio n. 8
0
        public async Task <LanguageModel> Update(AdminLanguageModel adminLanguageModel)
        {
            var language = _applicationDbContext.Languages.FirstOrDefault(x => x.ID == adminLanguageModel.ID);

            if (language != null && _languageValidation.IsInputValid(adminLanguageModel))
            {
                language.Name = adminLanguageModel.Name;

                await _applicationDbContext.SaveChangesAsync();

                return(await Read(language.ID));
            }

            return(null);
        }
Esempio n. 9
0
        public async Task <LanguageModel> Create(AdminLanguageModel adminLanguageModel)
        {
            if (_languageValidation.IsInputValid(adminLanguageModel))
            {
                var language = new Domain.Language
                {
                    Name = adminLanguageModel.Name
                };

                _applicationDbContext.Languages.Add(language);

                await _applicationDbContext.SaveChangesAsync();

                return(await Read(language.ID));
            }

            return(null);
        }
Esempio n. 10
0
        public async Task Update_ValidRequest_ReturnsJsonResponseAndOk(int id, string name)
        {
            #region Arrange
            await DeleteDbContent();

            var client    = GetHttpClient();
            var dbContext = GetDbContext();

            dbContext.Languages.Add(new Domain.Language
            {
                Name = "Name"
            });
            await dbContext.SaveChangesAsync();

            var newLanguage = new AdminLanguageModel
            {
                ID   = id,
                Name = name
            };

            var expectedLanguage = new LanguageModel
            {
                ID   = id,
                Name = name
            };
            #endregion

            #region Act
            var response = await client.PutAsJsonAsync($"/api/language/{id}", newLanguage);

            var responseBody = await response.Content.ReadAsStreamAsync();

            var actualLanguage = await JsonSerializer.DeserializeAsync <LanguageModel>(responseBody);

            #endregion

            #region Assert
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            Assert.Equal(expectedLanguage.ID, actualLanguage.ID);
            Assert.Equal(expectedLanguage.Name, actualLanguage.Name);
            #endregion
        }
Esempio n. 11
0
        public async Task Update_InvalidRequest_ReturnsJsonResponseAndBadRequestWithErrors(int id, string name, IEnumerable <string> expectedErrorNames, IEnumerable <string> expectedErrorMessages)
        {
            #region Arrange
            await DeleteDbContent();

            var client    = GetHttpClient();
            var dbContext = GetDbContext();

            dbContext.Languages.Add(new Domain.Language
            {
                Name = "Name"
            });
            await dbContext.SaveChangesAsync();

            var newLanguage = new AdminLanguageModel
            {
                ID   = id,
                Name = name
            };
            #endregion

            #region Act
            var response = await client.PutAsJsonAsync($"/api/language/{id}", newLanguage);

            var responseBody = await response.Content.ReadAsStreamAsync();

            var actualLanguage = await JsonSerializer.DeserializeAsync <JsonElement>(responseBody);

            var errorProp = actualLanguage.GetProperty("errors");
            var errors    = errorProp.EnumerateObject();
            #endregion

            #region Assert
            Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
            Assert.Equal(expectedErrorNames.Count(), errors.Count());
            Assert.All(expectedErrorNames, errorName => Assert.Contains(errorName, errors.Select(prop => prop.Name)));
            Assert.All(expectedErrorMessages, errorMessage => Assert.Contains(errorMessage, errors.Select(prop => prop.Value[0].ToString())));
            #endregion
        }
Esempio n. 12
0
        public async Task Update_InvalidRequest_ReturnsJsonResponseAndNotFound(int id)
        {
            #region Arrange
            await DeleteDbContent();

            var client = GetHttpClient();

            var newLanguage = new AdminLanguageModel
            {
                ID   = id,
                Name = "New Name"
            };
            #endregion

            #region Act
            var response = await client.PutAsJsonAsync($"/api/language/{id}", newLanguage);

            #endregion

            #region Assert
            Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
            #endregion
        }
Esempio n. 13
0
        public bool IsInputValid(AdminLanguageModel adminLanguageModel)
        {
            bool isNameOk = !(adminLanguageModel.Name == null || adminLanguageModel.Name == "");

            return(isNameOk);
        }