Exemple #1
0
        public void WhenCalendarEventExistsThenMappedObjectShouldBeReturned()
        {
            // Arrange
            var calendarEvent = new CalendarEvent
            {
                Id        = 1,
                IsDeleted = false,
            };

            this.repositoryMock
            .Setup(r => r.CalendarEvents)
            .Returns(new List <CalendarEvent> {
                calendarEvent
            }.AsQueryable());

            var mappedEvent = new CalendarEventDO();

            this.calendarEventMapperMock
            .Setup(m => m.Map(It.IsAny <CalendarEvent>()))
            .Returns(mappedEvent);

            // Act
            var result = this.sut.GetCalendarEvent(1);

            // Assert
            result.ShouldBe(mappedEvent);
        }
        public void WhenCreationIsPerformedThenEntityShouldBeSavedToRepository()
        {
            // Arrange
            this.repositoryMock
            .Setup(r => r.CalendarEvents)
            .Returns(new List <CalendarEvent>().AsQueryable());

            var newCalendarEvent = new CalendarEventDO
            {
                CreateRequestId = Guid.NewGuid(),
            };

            // Act
            this.sut.CreateCalendarEvent(newCalendarEvent);

            // Assert
            this.repositoryMock.Verify(r => r.AddCalendarEvent(It.IsAny <CalendarEvent>()), Times.Once);
            this.repositoryMock.Verify(r => r.SaveChanges(), Times.Once);
        }
        public IActionResult CreateEvent(CreateEventRequest request)
        {
            var domainObject = new CalendarEventDO
            {
                CreateRequestId = request.RequestId,
                Summary         = request.Summary,
                Location        = request.Location,
                StartDate       = request.StartDate,
                EndDate         = request.EndDate,
            };

            try
            {
                this.eventCreatorService.CreateCalendarEvent(domainObject);
            }
            catch (DuplicateRequestException)
            {
                return(this.Conflict());
            }

            return(this.Accepted());
        }
        /// <summary>
        /// Creates a new calendar event.
        /// </summary>
        /// <param name="calendarEvent">The calendar event details.</param>
        /// <exception cref="DuplicateRequestException">Exception thrown when the request has been made multiple times.</exception>
        public void CreateCalendarEvent(CalendarEventDO calendarEvent)
        {
            // Guards
            EnsureArg.IsNotNull(calendarEvent, nameof(calendarEvent));
            this.calendarEventValidator.ValidateSummary(calendarEvent.Summary);
            this.calendarEventValidator.ValidateLocation(calendarEvent.Location);
            this.calendarEventValidator.ValidateDateRange(calendarEvent.StartDate, calendarEvent.EndDate);

            var entity = new CalendarEvent
            {
                CreateRequestId = calendarEvent.CreateRequestId,
                Summary         = calendarEvent.Summary,
                Location        = calendarEvent.Location,
                StartDate       = calendarEvent.StartDate.Date,
                EndDate         = calendarEvent.EndDate.Date,
                IsDeleted       = false,
            };

            using (var repo = this.repositoryFactory.Create())
            {
                // No explicit requirement for duplicate request protection,
                // it is however desirable to ensure that two records are not
                // created if the API is hit twice in quick succession with the same data
                var existingRequestRecord = repo.CalendarEvents
                                            .Where(e => e.CreateRequestId == calendarEvent.CreateRequestId)
                                            .SingleOrDefault();

                if (existingRequestRecord != null)
                {
                    this.logger.LogError($"New calendar event could not be created. Detected duplicate record for request ID '{calendarEvent.CreateRequestId}'.");
                    throw new DuplicateRequestException();
                }

                repo.AddCalendarEvent(entity);
                repo.SaveChanges();
            }

            this.logger.LogInformation($"New calendar event created for request ID '{calendarEvent.CreateRequestId}'.");
        }
        public void WhenRecordWithMatchingCreateRequestIdAlreadyExistsThenDuplicateRequestExceptionShouldBeThrown()
        {
            // Arrange
            var existingCalendarEvent = new CalendarEvent
            {
                Id = 1,
                CreateRequestId = Guid.NewGuid(),
            };

            this.repositoryMock
            .Setup(r => r.CalendarEvents)
            .Returns(new List <CalendarEvent> {
                existingCalendarEvent
            }.AsQueryable());

            var newCalendarEvent = new CalendarEventDO
            {
                CreateRequestId = existingCalendarEvent.CreateRequestId,
            };

            // Act + Assert
            Should.Throw <DuplicateRequestException>(() => this.sut.CreateCalendarEvent(newCalendarEvent));
        }