public async void CreateResource_Success_WithPreviousVersion()
        {
            // Arrange
            var requestBuilder = new ResourceBuilder()
                                 .GenerateSampleData();

            var request  = requestBuilder.BuildRequestDto();
            var resource = requestBuilder.Build();

            request.HasPreviousVersion = Graph.Metadata.Constants.Entity.IdPrefix + Guid.NewGuid();

            var resourceCto      = new ResourcesCTO(null, null);
            var validationFacade = new EntityValidationFacade(ResourceCrudAction.Create, resource, resourceCto, request.HasPreviousVersion, _metadata, null);

            var preProcessServiceResult = Task.FromResult(new Tuple <ValidationResult, bool, EntityValidationFacade>(null, false, validationFacade));

            _mockPreProcessService.Setup(t => t.ValidateAndPreProcessResource(It.IsAny <string>(), request, It.IsAny <ResourcesCTO>(), ResourceCrudAction.Create, It.IsAny <bool>(), It.IsAny <string>())).Returns(preProcessServiceResult);

            // Act
            var result = await _service.CreateResource(request);

            // Assert
            Assert.NotNull(result);
            _mockResourceRepo.Verify(t => t.Create(validationFacade.RequestResource, It.IsAny <IList <MetadataProperty> >()), Times.Once);
            _mockResourceLinkingService.Verify(t => t.LinkResourceIntoList(validationFacade.RequestResource.PidUri, It.IsAny <Uri>()), Times.Once);
        }
        public async void EditResource_Success()
        {
            // Arrange
            var requestBuilder = new ResourceBuilder()
                                 .GenerateSampleData();

            var request = requestBuilder.BuildRequestDto();

            var resource    = requestBuilder.Build();
            var publishedId = Graph.Metadata.Constants.Entity.IdPrefix + Guid.NewGuid();

            resource.Id = publishedId;

            var resourceCto = new ResourcesCTO(null, resource);

            _mockResourceRepo.Setup(s => s.GetResourcesByPidUri(resource.PidUri, It.IsAny <IList <string> >())).Returns(resourceCto);

            var validationFacade        = new EntityValidationFacade(ResourceCrudAction.Create, resource, resourceCto, request.HasPreviousVersion, _metadata, null);
            var validationResult        = new ValidationResult();
            var preProcessServiceResult = new Tuple <ValidationResult, bool, EntityValidationFacade>(validationResult, false, validationFacade);

            var newResourceId = string.Empty;

            _mockPreProcessService.Setup(t => t.ValidateAndPreProcessResource(It.IsAny <string>(), request, It.IsAny <ResourcesCTO>(), ResourceCrudAction.Update, It.IsAny <bool>(), It.IsAny <string>()))
            .ReturnsAsync(preProcessServiceResult)
            .Callback <string, ResourceRequestDTO, ResourcesCTO, ResourceCrudAction, bool, string>((a, b, c, d, e, f) =>
            {
                newResourceId = a;
                validationResult.Results.Add(new ValidationResultProperty(a, "some path", "some value", "some message", ValidationResultSeverity.Warning));
            });

            // Act
            var result = await _service.EditResource(resource.PidUri, request);

            // Assert
            Assert.All(result.ValidationResult.Results, t =>
            {
                Assert.Equal(t.Node, newResourceId);
            });

            _mockResourceRepo.Verify(s => s.DeleteDraft(result.Resource.PidUri, new Uri(result.Resource.Id)), Times.Once);
            _mockResourceRepo.Verify(s => s.Create(resource, _metadata), Times.Once);
            _mockResourceRepo.Verify(s => s.CreateLinkingProperty(resource.PidUri, new Uri(
                                                                      Graph.Metadata.Constants.Resource.HasPidEntryDraft),
                                                                  Graph.Metadata.Constants.Resource.ColidEntryLifecycleStatus.Published,
                                                                  Graph.Metadata.Constants.Resource.ColidEntryLifecycleStatus.Draft), Times.Once);
            _mockIdentifierService.Verify(s => s.DeleteAllUnpublishedIdentifiers(It.IsAny <Entity>()), Times.Never);
        }
        public async void CreateResource_Should_ThrowError_ValidationFailed()
        {
            // Arrange
            var requestBuilder = new ResourceBuilder()
                                 .GenerateSampleData();

            var request  = requestBuilder.BuildRequestDto();
            var resource = requestBuilder.Build();

            var entityValidationFacade = new EntityValidationFacade(ResourceCrudAction.Create, resource, null, null, _metadata, null);
            var result = Task.FromResult(new Tuple <ValidationResult, bool, EntityValidationFacade>(null, true, entityValidationFacade));

            _mockPreProcessService.Setup(t => t.ValidateAndPreProcessResource(It.IsAny <string>(), request, It.IsAny <ResourcesCTO>(), ResourceCrudAction.Create, It.IsAny <bool>(), It.IsAny <string>())).Returns(result);

            // Act
            await Assert.ThrowsAsync <ResourceValidationException>(() => _service.CreateResource(request));
        }
        public async void EditResource__Should_ThrowError_ResourceValidation()
        {
            // Arrange
            var requestBuilder = new ResourceBuilder()
                                 .GenerateSampleData();

            var request = requestBuilder.BuildRequestDto();

            var resource    = requestBuilder.Build();
            var publishedId = Graph.Metadata.Constants.Entity.IdPrefix + Guid.NewGuid();

            resource.Id = publishedId;

            var resourceCto = new ResourcesCTO(null, resource);

            _mockResourceRepo.Setup(s => s.GetResourcesByPidUri(resource.PidUri, It.IsAny <IList <string> >())).Returns(resourceCto);

            var validationFacade        = new EntityValidationFacade(ResourceCrudAction.Create, resource, resourceCto, request.HasPreviousVersion, _metadata, null);
            var validationResult        = new ValidationResult();
            var preProcessServiceResult = new Tuple <ValidationResult, bool, EntityValidationFacade>(validationResult, true, validationFacade);

            var newResourceId = string.Empty;

            _mockPreProcessService.Setup(t => t.ValidateAndPreProcessResource(It.IsAny <string>(), request, It.IsAny <ResourcesCTO>(), ResourceCrudAction.Update, It.IsAny <bool>(), It.IsAny <string>()))
            .ReturnsAsync(preProcessServiceResult)
            .Callback <string, ResourceRequestDTO, ResourcesCTO, ResourceCrudAction, bool, string>((a, b, c, d, e, f) =>
            {
                newResourceId = a;
                validationResult.Results.Add(new ValidationResultProperty(a, "some path", "some value", "some message", ValidationResultSeverity.Warning));
            });

            // Act
            var result = await Assert.ThrowsAsync <ResourceValidationException>(() => _service.EditResource(resource.PidUri, request));

            // Assert
            _mockResourceRepo.Verify(s => s.CreateTransaction(), Times.Never);
            Assert.All(result.ValidationResult.Results, t => Assert.Equal(t.Node, publishedId));
        }