示例#1
0
        public async Task SaveScheduledPresentation_WithValidScheduledPresentation_ShouldReturnScheduledPresentation()
        {
            // Arrange
            var queuePresentationAddedMock = new Mock <PresentationAddedQueue>();
            var queueScheduleAddedMock     = new Mock <PresentationScheduleAddedQueue>();
            var presentationRepositoryMock = new Mock <IPresentationRepository>();

            presentationRepositoryMock
            .Setup(presentationRepository =>
                   presentationRepository.SaveScheduledPresentationAsync(It.IsAny <ScheduledPresentation>()))
            .ReturnsAsync((ScheduledPresentation scheduledPresentationInput) => scheduledPresentationInput);
            var presentationManager = new PresentationManager(presentationRepositoryMock.Object,
                                                              queuePresentationAddedMock.Object, queueScheduleAddedMock.Object);

            var startTime             = DateTime.Now;
            var scheduledPresentation = new ScheduledPresentation
            {
                Presentation = new Presentation(),
                StartTime    = startTime,
                EndTime      = startTime.AddMinutes(1)
            };

            // Act
            var savedScheduledPresentation =
                await presentationManager.SaveScheduledPresentationAsync(scheduledPresentation);

            // Assert
            Assert.NotNull(savedScheduledPresentation);
            Assert.NotNull(savedScheduledPresentation.Presentation);
            Assert.Equal(scheduledPresentation.StartTime, savedScheduledPresentation.StartTime);
            Assert.Equal(scheduledPresentation.EndTime, savedScheduledPresentation.EndTime);
        }
示例#2
0
        public async Task SaveScheduledPresentation_WithStartTimeGreaterThanEndTime_ShouldThrowException()
        {
            // Arrange
            var queuePresentationAddedMock = new Mock <PresentationAddedQueue>();
            var queueScheduleAddedMock     = new Mock <PresentationScheduleAddedQueue>();
            var presentationRepositoryMock = new Mock <IPresentationRepository>();

            presentationRepositoryMock
            .Setup(presentationRepository =>
                   presentationRepository.SaveScheduledPresentationAsync(It.IsAny <ScheduledPresentation>()));
            var presentationManager = new PresentationManager(presentationRepositoryMock.Object,
                                                              queuePresentationAddedMock.Object, queueScheduleAddedMock.Object);
            var startTime = DateTime.Now;

            var scheduledPresentation = new ScheduledPresentation
            {
                Presentation = new Presentation(),
                StartTime    = startTime,
                EndTime      = startTime.AddMinutes(-1)
            };

            // Act
            var ex = await Assert.ThrowsAsync <ArgumentOutOfRangeException>(() =>
                                                                            presentationManager.SaveScheduledPresentationAsync(scheduledPresentation));

            // Assert
            Assert.Equal("StartTime", ex.ParamName);
            Assert.Equal(startTime, ex.ActualValue);
            Assert.StartsWith(
                "The start time of the presentation can not be greater then the end time (Parameter 'StartTime')",
                ex.Message);
        }
示例#3
0
        public async Task SaveScheduledPresentation_WithNullPresentation_ShouldThrowException()
        {
            // Arrange
            var queuePresentationAddedMock = new Mock <PresentationAddedQueue>();
            var queueScheduleAddedMock     = new Mock <PresentationScheduleAddedQueue>();
            var presentationRepositoryMock = new Mock <IPresentationRepository>();

            presentationRepositoryMock
            .Setup(presentationRepository =>
                   presentationRepository.SaveScheduledPresentationAsync(It.IsAny <ScheduledPresentation>()));
            var presentationManager = new PresentationManager(presentationRepositoryMock.Object,
                                                              queuePresentationAddedMock.Object, queueScheduleAddedMock.Object);

            var scheduledPresentation = new ScheduledPresentation
            {
                Presentation = null
            };

            // Act
            var ex = await Assert.ThrowsAsync <ArgumentNullException>(() =>
                                                                      presentationManager.SaveScheduledPresentationAsync(scheduledPresentation));

            // Assert
            Assert.Equal("Presentation", ex.ParamName);
            Assert.Equal("The presentation can not be null (Parameter 'Presentation')", ex.Message);
        }
示例#4
0
        public async Task <ScheduledPresentation> SaveScheduledPresentationAsync(ScheduledPresentation scheduledPresentation)
        {
            // Validate the fields
            if (scheduledPresentation == null)
            {
                throw new ArgumentNullException(nameof(scheduledPresentation), "The scheduled presentation can not be null");
            }

            if (scheduledPresentation.Presentation == null)
            {
                throw new ArgumentNullException(nameof(scheduledPresentation.Presentation), "The presentation can not be null");
            }

            // Rules validation
            if (scheduledPresentation.StartTime > scheduledPresentation.EndTime)
            {
                throw new ArgumentOutOfRangeException(nameof(scheduledPresentation.StartTime),
                                                      scheduledPresentation.StartTime,
                                                      "The start time of the presentation can not be greater then the end time");
            }

            var savedScheduledPresentation = await _presentationRepository.SaveScheduledPresentationAsync(scheduledPresentation);

            var addedPresentationMessage = new Domain.Models.Messages.Presentations.Added {
                PresentationId = savedScheduledPresentation.PresentationId
            };
            await _presentationScheduleAddedQueue.AddMessageAsync(addedPresentationMessage);

            return(savedScheduledPresentation);
        }
示例#5
0
        public async Task <ScheduledPresentation> SaveScheduledPresentationAsync(ScheduledPresentation scheduledPresentation)
        {
            if (scheduledPresentation == null)
            {
                throw new ArgumentNullException(nameof(scheduledPresentation), "The scheduled presentation can not be null");
            }

            if (scheduledPresentation.Presentation == null)
            {
                throw new ArgumentNullException(nameof(scheduledPresentation.Presentation), "The presentation can not be null");
            }

            var dbScheduledPresentation = _mapper.Map <Sqlite.Models.ScheduledPresentation>(scheduledPresentation);

            await using (_presentationContext)
            {
                if (scheduledPresentation.ScheduledPresentationId == 0)
                {
                    var presentation = await _presentationContext.Presentations.FirstOrDefaultAsync(p =>
                                                                                                    p.PresentationId == scheduledPresentation.Presentation.PresentationId);

                    if (presentation == null)
                    {
                        // The specified Presentation was not found
                        throw new ApplicationException("The presentation was not found.");
                    }

                    if (presentation.ScheduledPresentations == null)
                    {
                        presentation.ScheduledPresentations = new List <Models.ScheduledPresentation> {
                            dbScheduledPresentation
                        };
                    }
                    else
                    {
                        presentation.ScheduledPresentations.Add(dbScheduledPresentation);
                    }
                }
                else
                {
                    _presentationContext.Entry(dbScheduledPresentation).State = EntityState.Modified;
                }

                var result = await _presentationContext.SaveChangesAsync();

                if (result > 0)
                {
                    // This is because the scheduled presentation object does not have the ScheduledPresentationId and
                    // we need to get this from EF
                    scheduledPresentation.ScheduledPresentationId = dbScheduledPresentation.ScheduledPresentationId;
                }

                return(result != 0 ? scheduledPresentation : null);
            }
        }
示例#6
0
        public async Task <ScheduledPresentation> SaveScheduledPresentationAsync(ScheduledPresentation scheduledPresentation)
        {
            var url         = $"{_settings.ApiRootUri}scheduledPresentations/";
            var jsonRequest = JsonSerializer.Serialize(scheduledPresentation);
            var jsonContent = new StringContent(jsonRequest, Encoding.UTF8, "application/json");

            var response = await _httpClient.PostAsync(url, jsonContent);

            if (response.StatusCode != HttpStatusCode.Created)
            {
                throw new HttpRequestException(
                          $"Invalid status code in the HttpResponseMessage: {response.StatusCode}.");
            }

            var content = await response.Content.ReadAsStringAsync();

            var options = new JsonSerializerOptions
            {
                PropertyNameCaseInsensitive = true,
            };

            scheduledPresentation = JsonSerializer.Deserialize <ScheduledPresentation>(content, options);
            return(scheduledPresentation);
        }
示例#7
0
 public Task <ScheduledPresentation> SaveScheduledPresentationAsync(ScheduledPresentation scheduledPresentation)
 {
     return(_presentationRepositoryStorage.SaveScheduledPresentationAsync(scheduledPresentation));
 }