public async Task Create_LatestPublishedReleaseForSubjectNotFound() { var request = new PermalinkCreateViewModel { Query = { SubjectId = Guid.NewGuid() } }; var releaseRepository = new Mock <IReleaseRepository>(MockBehavior.Strict); var subjectRepository = new Mock <ISubjectRepository>(MockBehavior.Strict); releaseRepository .Setup(s => s.GetLatestPublishedRelease(_publicationId)) .Returns((Release?)null); subjectRepository .Setup(s => s.GetPublicationIdForSubject(request.Query.SubjectId)) .ReturnsAsync(_publicationId); var service = BuildService(releaseRepository: releaseRepository.Object, subjectRepository: subjectRepository.Object); var result = await service.Create(request); MockUtils.VerifyAllMocks( releaseRepository, subjectRepository); result.AssertNotFound(); }
public async Task <Either <ActionResult, PermalinkViewModel> > Create(PermalinkCreateViewModel request) { var publicationId = await _subjectRepository.GetPublicationIdForSubject(request.Query.SubjectId); var release = _releaseRepository.GetLatestPublishedRelease(publicationId); if (release == null) { return(new NotFoundResult()); } return(await Create(release.Id, request)); }
public async Task <Either <ActionResult, PermalinkViewModel> > Create(Guid releaseId, PermalinkCreateViewModel request) { return(await _tableBuilderService.Query(releaseId, request.Query).OnSuccess(async result => { var permalinkTableResult = new PermalinkTableBuilderResult(result); var permalink = new Permalink(request.Configuration, permalinkTableResult, request.Query); await _blobStorageService.UploadAsJson(containerName: Permalinks, path: permalink.Id.ToString(), content: permalink, settings: BuildJsonSerializerSettings()); return await BuildViewModel(permalink); })); }
public async Task Create_WithoutReleaseId() { var subject = new Subject { Id = Guid.NewGuid() }; var request = new PermalinkCreateViewModel { Query = { SubjectId = subject.Id } }; var release = new Release { Id = Guid.NewGuid() }; var tableResult = new TableBuilderResultViewModel { SubjectMeta = new ResultSubjectMetaViewModel() }; var blobStorageService = new Mock <IBlobStorageService>(MockBehavior.Strict); var releaseRepository = new Mock <IReleaseRepository>(MockBehavior.Strict); var subjectRepository = new Mock <ISubjectRepository>(MockBehavior.Strict); var tableBuilderService = new Mock <ITableBuilderService>(MockBehavior.Strict); // Permalink id is assigned on creation and used as the blob path // Capture it so we can compare it with the view model result string blobPath = string.Empty; var blobPathCapture = new CaptureMatch <string>(callback => blobPath = callback); blobStorageService.Setup(s => s.UploadAsJson( Permalinks, Capture.With(blobPathCapture), It.Is <Permalink>(p => p.Configuration.Equals(request.Configuration) && p.FullTable.IsDeepEqualTo(new PermalinkTableBuilderResult(tableResult)) && p.Query.Equals(request.Query)), It.IsAny <JsonSerializerSettings>())) .Returns(Task.CompletedTask); releaseRepository .Setup(s => s.GetLatestPublishedRelease(_publicationId)) .Returns(release); subjectRepository .Setup(s => s.Get(subject.Id)) .ReturnsAsync(subject); subjectRepository .Setup(s => s.GetPublicationIdForSubject(subject.Id)) .ReturnsAsync(_publicationId); subjectRepository .Setup(s => s.IsSubjectForLatestPublishedRelease(subject.Id)) .ReturnsAsync(true); tableBuilderService .Setup(s => s.Query(release.Id, request.Query, CancellationToken.None)) .ReturnsAsync(tableResult); var service = BuildService(blobStorageService: blobStorageService.Object, releaseRepository: releaseRepository.Object, subjectRepository: subjectRepository.Object, tableBuilderService: tableBuilderService.Object); var result = (await service.Create(request)).AssertRight(); MockUtils.VerifyAllMocks( blobStorageService, releaseRepository, subjectRepository, tableBuilderService); Assert.Equal(Guid.Parse(blobPath), result.Id); Assert.InRange(DateTime.UtcNow.Subtract(result.Created).Milliseconds, 0, 1500); Assert.False(result.Invalidated); }
public async Task <ActionResult <PermalinkViewModel> > Create(Guid releaseId, [FromBody] PermalinkCreateViewModel request) { return(await _permalinkService.Create(releaseId, request).HandleFailuresOrOk()); }