Example #1
0
        public void Constructor_ShouldSetFieldAttachmentSpecificProperties()
        {
            var dut = new FieldValueAttachment(TestPlant, BlobStorageId, "FileA");

            Assert.AreEqual($"PlantA/FieldValue/{BlobStorageId.ToString()}", dut.BlobPath);
            // Other properties are tested in base class
        }
Example #2
0
        public async Task <Result <int> > Handle(UploadFieldValueAttachmentCommand request, CancellationToken cancellationToken)
        {
            var tag = await _projectRepository.GetTagByTagIdAsync(request.TagId);

            var requirement = tag.Requirements.Single(r => r.Id == request.RequirementId);

            var requirementDefinition =
                await _requirementTypeRepository.GetRequirementDefinitionByIdAsync(requirement.RequirementDefinitionId);

            var attachment = requirement.GetAlreadyRecordedAttachment(request.FieldId, requirementDefinition);

            string fullBlobPath;

            if (attachment == null)
            {
                attachment = new FieldValueAttachment(_plantProvider.Plant, Guid.NewGuid(), request.FileName);
            }
            else
            {
                fullBlobPath = attachment.GetFullBlobPath(_attachmentOptions.CurrentValue.BlobContainer);
                await _blobStorage.DeleteAsync(fullBlobPath, cancellationToken);

                attachment.SetFileName(request.FileName);
            }

            fullBlobPath = attachment.GetFullBlobPath(_attachmentOptions.CurrentValue.BlobContainer);

            await _blobStorage.UploadAsync(fullBlobPath, request.Content, true, cancellationToken);

            requirement.RecordAttachment(attachment, request.FieldId, requirementDefinition);

            await _unitOfWork.SaveChangesAsync(cancellationToken);

            return(new SuccessResult <int>(attachment.Id));
        }
        protected override void SetupNewDatabase(DbContextOptions <PreservationContext> dbContextOptions)
        {
            using var context = new PreservationContext(dbContextOptions, _plantProvider, _eventDispatcher, _currentUserProvider);

            _uri = new Uri("http://whatever/file.txt");
            _attachmentOptionsMock = new Mock <IOptionsMonitor <AttachmentOptions> >();
            var options = new AttachmentOptions
            {
                BlobContainer = BlobContainer
            };

            _attachmentOptionsMock
            .Setup(x => x.CurrentValue)
            .Returns(options);

            var journey = AddJourneyWithStep(context, "J1", "S", AddMode(context, "M1", false), AddResponsible(context, "R1"));

            var reqDef          = AddRequirementTypeWith1DefWithoutField(context, "T1", "D1", RequirementTypeIcon.Other).RequirementDefinitions.Single();
            var attachmentField = new Field(TestPlant, "Label", FieldType.Attachment, 0);

            reqDef.AddField(attachmentField);
            context.SaveChangesAsync().Wait();

            var requirement = new TagRequirement(TestPlant, 2, reqDef);

            var tag = new Tag(TestPlant,
                              TagType.Standard,
                              "TagNo",
                              "Description",
                              journey.Steps.ElementAt(0),
                              new List <TagRequirement> {
                requirement
            });

            context.Tags.Add(tag);
            tag.StartPreservation();
            context.SaveChangesAsync().Wait();

            _tagId = tag.Id;

            _requirementId     = requirement.Id;
            _attachmentFieldId = attachmentField.Id;

            var fieldValueAttachment = new FieldValueAttachment(TestPlant, Guid.Empty, "FilA.txt");

            requirement.RecordAttachment(fieldValueAttachment, _attachmentFieldId, reqDef);
            context.SaveChangesAsync().Wait();

            var fullBlobPath = fieldValueAttachment.GetFullBlobPath(BlobContainer);

            _blobStorageMock = new Mock <IBlobStorage>();
            _blobStorageMock
            .Setup(b => b.GetDownloadSasUri(fullBlobPath, It.IsAny <DateTimeOffset>(), It.IsAny <DateTimeOffset>()))
            .Returns(_uri);
        }
Example #4
0
        public void SetFileName_ShouldSetFileName()
        {
            // Assert
            var dut = new FieldValueAttachment(TestPlant, BlobStorageId, "FileA");

            // Act
            dut.SetFileName("FileB");

            // Assert
            Assert.AreEqual("FileB", dut.FileName);
        }
        private void AssertAttachmentField(FieldDetailsDto f, FieldValueAttachment expectedValue)
        {
            Assert.AreEqual(FieldType.Attachment, f.FieldType);
            Assert.IsFalse(f.ShowPrevious);
            Assert.IsNull(f.Unit);

            Assert.IsInstanceOfType(f.CurrentValue, typeof(AttachmentDetailsDto));
            var attachmentDto = f.CurrentValue as AttachmentDetailsDto;

            Assert.IsNotNull(attachmentDto);
            Assert.AreEqual(expectedValue.Id, attachmentDto.Id);
            Assert.AreEqual(expectedValue.FileName, attachmentDto.FileName);
        }
        public void Constructor_ShouldSetProperties_ForAttachment()
        {
            var field = new Field(_testPlant, "", FieldType.Attachment, 0);

            var fileName   = "FilA.txt";
            var attachment = new FieldValueAttachment(_testPlant, Guid.Empty, fileName);

            attachment.SetProtectedIdForTesting(11);

            var dut = new FieldDetailsDto(
                field,
                new AttachmentValue(_testPlant, field, attachment),
                null);

            var dto = dut.CurrentValue as AttachmentDetailsDto;

            Assert.IsNotNull(dto);
            Assert.AreEqual(11, dto.Id);
            Assert.AreEqual(fileName, dto.FileName);
        }
        public async Task Handler_ShouldReturnTagRequirementsWithAttachment_AfterRecordingAttachment()
        {
            var attachmentField      = _attachmentFieldId;
            var fieldValueAttachment = new FieldValueAttachment(TestPlant, Guid.Empty, "FilA.txt");

            using (var context = new PreservationContext(_dbContextOptions, _plantProviderMock.Object, _eventDispatcher, _currentUserProvider))
            {
                var tag = context.Tags.Include(t => t.Requirements).Single();
                tag.StartPreservation();
                context.SaveChangesAsync().Wait();

                var requirementDefinition = context.RequirementDefinitions.Include(rd => rd.Fields)
                                            .Single(rd => rd.Id == _requirementDefinitionWithOneAttachmentId);
                var requirement = context.TagRequirements.Single(r => r.Id == _requirementWithOneAttachmentId);

                requirement.RecordAttachment(
                    fieldValueAttachment,
                    attachmentField,
                    requirementDefinition);
                context.SaveChangesAsync().Wait();
            }

            using (var context = new PreservationContext(_dbContextOptions, _plantProviderMock.Object, _eventDispatcher, _currentUserProvider))
            {
                var query = new GetTagRequirementsQuery(_tagId, false, false);
                var dut   = new GetTagRequirementsQueryHandler(context);

                var result = await dut.Handle(query, default);

                Assert.IsNotNull(result);
                Assert.AreEqual(ResultType.Ok, result.ResultType);

                var requirementWithAttachment = result.Data.Single(r => r.Id == _requirementWithOneAttachmentId);
                Assert.AreEqual(1, requirementWithAttachment.Fields.Count);

                var fieldWithAttachment = requirementWithAttachment.Fields.Single(f => f.Id == attachmentField);
                AssertAttachmentField(fieldWithAttachment, fieldValueAttachment);
            }
        }