Ejemplo n.º 1
0
        public void Update_ShouldFailWithConflict_WhenConflictValues()
        {
            int id           = 1;
            var conflictCode = _marks[1].Code;

            var newMarkRequest = new MarkUpdateRequest
            {
                Code = conflictCode,
            };

            // Act & Assert
            Assert.Throws <ConflictException>(() => _service.Update(id,
                                                                    newMarkRequest));

            _repository.Verify(mock => mock.Add(It.IsAny <Mark>()), Times.Never);
        }
Ejemplo n.º 2
0
        public async Task Update_ShouldReturnUnauthorized_WhenNoAccessToken()
        {
            // Arrange
            var id          = 1;
            var markRequest = new MarkUpdateRequest
            {
                Name = "NewUpdate",
            };
            string json        = JsonSerializer.Serialize(markRequest);
            var    httpContent = new StringContent(json, Encoding.UTF8, "application/json");
            var    endpoint    = $"/api/marks/{id}";

            // Act
            var response = await _authHttpClient.PatchAsync(endpoint, httpContent);

            // Assert
            Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
        }
Ejemplo n.º 3
0
        public async Task Update_ShouldReturnConflict_WhenConflictValues()
        {
            // Arrange
            int id          = 2;
            var markRequest = new MarkUpdateRequest
            {
                Code = TestData.marks[0].Code,
            };
            string json        = JsonSerializer.Serialize(markRequest);
            var    httpContent = new StringContent(json, Encoding.UTF8, "application/json");
            var    endpoint    = $"/api/marks/{id}";

            // Act
            var response = await _httpClient.PatchAsync(endpoint, httpContent);

            // Assert
            Assert.Equal(HttpStatusCode.Conflict, response.StatusCode);
        }
Ejemplo n.º 4
0
        public void Update_ShouldUpdateMark()
        {
            // Arrange
            int id = _rnd.Next(1, _marks.Count());
            int newGroupLeaderId     = _rnd.Next(1, TestData.employees.Count());
            int newChiefSpecialistId = _rnd.Next(1, TestData.employees.Count());
            int newMainBuilderId     = _rnd.Next(1, TestData.employees.Count());
            var newStringValue       = "NewUpdate";

            while (TestData.employees.SingleOrDefault(
                       v => v.Id == newGroupLeaderId).Department.Id !=
                   _marks.SingleOrDefault(v => v.Id == id).Department.Id)
            {
                newGroupLeaderId = _rnd.Next(1, TestData.employees.Count());
            }

            var newMarkRequest = new MarkUpdateRequest
            {
                Name = newStringValue,
                Code = newStringValue,
                // GroupLeaderId = newGroupLeaderId,
                // ChiefSpecialistId = newChiefSpecialistId,
                // MainBuilderId = newMainBuilderId,
            };

            // Act
            _service.Update(id,
                            newMarkRequest);

            // Assert
            _repository.Verify(mock => mock.Update(It.IsAny <Mark>()), Times.Once);
            Assert.Equal(newStringValue, _marks.SingleOrDefault(
                             v => v.Id == id).Name);
            Assert.Equal(newStringValue, _marks.SingleOrDefault(
                             v => v.Id == id).Code);
            // Assert.Equal(newGroupLeaderId, _marks.SingleOrDefault(
            //     v => v.Id == id).GroupLeader.Id);
            // Assert.Equal(newChiefSpecialistId, _marks.SingleOrDefault(
            //     v => v.Id == id).ChiefSpecialist.Id);
            // Assert.Equal(newMainBuilderId, _marks.SingleOrDefault(
            //     v => v.Id == id).MainBuilder.Id);
        }
Ejemplo n.º 5
0
 public ActionResult Update(int id, [FromBody] MarkUpdateRequest markRequest)
 {
     // DEBUG
     // Log.Information(JsonSerializer.Serialize(markRequest));
     if (!markRequest.Validate())
     {
         return(BadRequest());
     }
     try
     {
         _service.Update(id, markRequest);
     }
     catch (ArgumentNullException)
     {
         return(NotFound());
     }
     catch (ConflictException)
     {
         return(Conflict());
     }
     return(NoContent());
 }
Ejemplo n.º 6
0
        public void Update_ShouldFailWithNull_WhenWrongValues()
        {
            // Arrange
            int id = _rnd.Next(1, _marks.Count());
            int newGroupLeaderId = _rnd.Next(1, TestData.employees.Count());

            var newMarkRequest = new MarkUpdateRequest
            {
                GroupLeaderId = newGroupLeaderId,
            };
            var wrongMarkRequest1 = new MarkUpdateRequest
            {
                GroupLeaderId = 999,
            };
            var wrongMarkRequest2 = new MarkUpdateRequest
            {
                ChiefSpecialistId = 999,
            };
            var wrongMarkRequest3 = new MarkUpdateRequest
            {
                MainBuilderId = 999,
            };

            // Act & Assert
            Assert.Throws <ArgumentNullException>(
                () => _service.Update(999, newMarkRequest));
            Assert.Throws <ArgumentNullException>(
                () => _service.Update(id, null));
            Assert.Throws <ArgumentNullException>(
                () => _service.Update(id, wrongMarkRequest1));
            Assert.Throws <ArgumentNullException>(
                () => _service.Update(id, wrongMarkRequest2));
            Assert.Throws <ArgumentNullException>(
                () => _service.Update(id, wrongMarkRequest3));
            _repository.Verify(mock => mock.Update(It.IsAny <Mark>()), Times.Never);
        }
Ejemplo n.º 7
0
        public void Update(
            int id,
            MarkUpdateRequest mark)
        {
            if (mark == null)
            {
                throw new ArgumentNullException(nameof(mark));
            }
            var foundMark = _repository.GetById(id);

            if (foundMark == null)
            {
                throw new ArgumentNullException(nameof(foundMark));
            }
            if (mark.Name != null)
            {
                foundMark.Name = mark.Name;
            }

            if ((mark.Code != null) && (mark.SubnodeId != null))
            {
                foundMark.Code = mark.Code;

                var subnode = _subnodeRepo.GetById(mark.SubnodeId.GetValueOrDefault());
                if (subnode == null)
                {
                    throw new ArgumentNullException(nameof(subnode));
                }
                foundMark.Subnode = subnode;

                var uniqueConstraintViolationCheck = _repository.GetByUniqueKey(
                    subnode.Id, mark.Code);
                if (uniqueConstraintViolationCheck != null && uniqueConstraintViolationCheck.Id != id)
                {
                    throw new ConflictException(nameof(uniqueConstraintViolationCheck));
                }
            }
            else if (mark.Code != null)
            {
                foundMark.Code = mark.Code;

                var uniqueConstraintViolationCheck = _repository.GetByUniqueKey(
                    foundMark.Subnode.Id, mark.Code);
                if (uniqueConstraintViolationCheck != null && uniqueConstraintViolationCheck.Id != id)
                {
                    throw new ConflictException(nameof(uniqueConstraintViolationCheck));
                }
            }
            else if (mark.SubnodeId != null)
            {
                var subnode = _subnodeRepo.GetById(mark.SubnodeId.GetValueOrDefault());
                if (subnode == null)
                {
                    throw new ArgumentNullException(nameof(subnode));
                }
                foundMark.Subnode = subnode;

                var uniqueConstraintViolationCheck = _repository.GetByUniqueKey(
                    subnode.Id, foundMark.Code);
                if (uniqueConstraintViolationCheck != null && uniqueConstraintViolationCheck.Id != id)
                {
                    throw new ConflictException(nameof(uniqueConstraintViolationCheck));
                }
            }

            if (mark.DepartmentId != null)
            {
                var department = _departmentRepo.GetById(mark.DepartmentId.GetValueOrDefault());
                if (department == null)
                {
                    throw new ArgumentNullException(nameof(department));
                }
                foundMark.Department = department;
                // To Do: Check employees
            }
            if (mark.MainBuilderId != null)
            {
                var mainBuilder = _employeeRepo.GetById(mark.MainBuilderId.GetValueOrDefault());
                if (mainBuilder == null)
                {
                    throw new ArgumentNullException(nameof(mainBuilder));
                }
                if (mainBuilder.Department.Id != foundMark.Department.Id)
                {
                    throw new ConflictException("departmentId");
                }
                foundMark.MainBuilder = mainBuilder;
            }
            // Nullable section
            if (mark.ChiefSpecialistId != null)
            {
                int chiefSpecialistId = mark.ChiefSpecialistId.GetValueOrDefault();
                if (chiefSpecialistId == -1)
                {
                    foundMark.ChiefSpecialist = null;
                }
                else
                {
                    var chiefSpecialist = _employeeRepo.GetById(chiefSpecialistId);
                    if (chiefSpecialist == null)
                    {
                        throw new ArgumentNullException(nameof(chiefSpecialist));
                    }
                    if (chiefSpecialist.Department.Id != foundMark.Department.Id)
                    {
                        throw new ConflictException("departmentId");
                    }
                    foundMark.ChiefSpecialist = chiefSpecialist;
                }
            }
            if (mark.GroupLeaderId != null)
            {
                int groupLeaderId = mark.GroupLeaderId.GetValueOrDefault();
                if (groupLeaderId == -1)
                {
                    foundMark.GroupLeader = null;
                }
                else
                {
                    var groupLeader = _employeeRepo.GetById(groupLeaderId);
                    if (groupLeader == null)
                    {
                        throw new ArgumentNullException(nameof(groupLeader));
                    }
                    if (groupLeader.Department.Id != foundMark.Department.Id)
                    {
                        throw new ConflictException("departmentId");
                    }
                    foundMark.GroupLeader = groupLeader;
                }
            }
            foundMark.EditedDate = DateTime.Now;
            _repository.Update(foundMark);
        }