Skip to content

Commit

Permalink
Merge branch 'release/v2.11.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
lhSunde committed Jul 5, 2023
2 parents 19693ae + ad950a6 commit d3d3f43
Show file tree
Hide file tree
Showing 29 changed files with 546 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void ExtractAndIdentifyTarFiles()
pathToExtractedFilesRegex = pathToExtractedFilesRegex.Replace("\\", "\\\\");

var extractorMock = new Mock<ICompressionUtility>();
extractorMock.Setup(e => e.ExtractFolderFromArchive(It.IsAny<FileInfo>(), It.IsAny<DirectoryInfo>()));
extractorMock.Setup(e => e.ExtractFolderFromArchive(It.IsAny<FileInfo>(), It.IsAny<DirectoryInfo>(), false, uuid.ToString()));

var archiveType = ArchiveType.Noark5;
var identifierMock = new Mock<IArchiveTypeIdentifier>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ private static TestRun CreateTestRun(XmlElementHelper xmlElementHelper)
.WithWorkingDirectoryRoot(testdataDirectory)
.Build();

testArchive.DocumentFiles.Register(true);

TestRun testRun = xmlElementHelper.RunEventsOnTest(new N5_30_DocumentFilesChecksumControl(testArchive));
return testRun;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void ReferencedFilesExists()
.Add("dokumentobjekt", new XmlElementHelper()
.Add("referanseDokumentfil", "dokumenter/5000001.pdf")))))))));

Archive testArchive = TestUtil.CreateArchiveExtraction(
Archive testArchive = CreateTestArchive(
Path.Combine("TestData", "Noark5", "DocumentfilesControl", "ArchiveReferencedFiles")
);

Expand Down Expand Up @@ -87,7 +87,7 @@ public void ReferencedFilesAreMissing()
.Add("dokumentobjekt", new XmlElementHelper()
.Add("referanseDokumentfil", "dokumenter/5000002.pdf")))))))));

Archive testArchive = TestUtil.CreateArchiveExtraction(
Archive testArchive = CreateTestArchive(
Path.Combine("TestData", "Noark5", "DocumentfilesControl", "ArchiveReferencedFiles")
);

Expand Down Expand Up @@ -124,7 +124,7 @@ public void ReferencedFilesAreMissingFromArchiveWithOneArchivePart()
.Add("dokumentobjekt", new XmlElementHelper()
.Add("referanseDokumentfil", "dokumenter/5000002.pdf")))))))));

Archive testArchive = TestUtil.CreateArchiveExtraction(
Archive testArchive = CreateTestArchive(
Path.Combine("TestData", "Noark5", "DocumentfilesControl", "ArchiveReferencedFiles")
);

Expand Down Expand Up @@ -166,13 +166,22 @@ public void SlashOrBackSlashDirectorSeparatorIsIndifferent()
.Add("dokumentobjekt", new XmlElementHelper()
.Add("referanseDokumentfil", "dokumenter/5000001.pdf")))))))));

Archive testArchive = TestUtil.CreateArchiveExtraction(
Archive testArchive = CreateTestArchive(
Path.Combine("TestData", "Noark5", "DocumentfilesControl", "ArchiveReferencedFiles")
);

TestRun testRun = helper.RunEventsOnTest(new N5_32_ControlDocumentFilesExists(testArchive));

testRun.TestResults.GetNumberOfResults().Should().Be(0);
}

private static Archive CreateTestArchive(string pathToArchive)
{
Archive archive = TestUtil.CreateArchiveExtraction(pathToArchive);

archive.DocumentFiles.Register(false);

return archive;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ private static TestRun CreateTestRun(XmlElementHelper xmlElementHelper)
.WithArchiveType(ArchiveType.Noark5)
.WithWorkingDirectoryRoot(testdataDirectory).Build();

testArchive.DocumentFiles.Register(false);

TestRun testRun = xmlElementHelper.RunEventsOnTest(new N5_33_DocumentfilesReferenceControl(testArchive));
return testRun;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void EmptyDocumentFilesAreReported()
.Add("referanseDokumentfil", "dokumenter/empty.txt")
.Add("filstoerrelse", "0")))))))));

Archive testArchive = TestUtil.CreateArchiveExtraction(
Archive testArchive = CreateTestArchive(
Path.Combine("TestData", "Noark5", "DocumentfilesControl", "EmptyFiles")
);

Expand Down Expand Up @@ -69,7 +69,7 @@ public void EmptyDocumentFileWithDocumentedSizeNotZeroIsReported()
.Add("referanseDokumentfil", "dokumenter/empty.txt")
.Add("filstoerrelse", "1")))))))));

Archive testArchive = TestUtil.CreateArchiveExtraction(
Archive testArchive = CreateTestArchive(
Path.Combine("TestData", "Noark5", "DocumentfilesControl", "EmptyFiles")
);

Expand Down Expand Up @@ -101,7 +101,7 @@ public void NonEmptyDocumentFilesAreNotReported()
.Add("referanseDokumentfil", "dokumenter/5000000.pdf")
.Add("filstoerrelse", "20637")))))))));

Archive testArchive = TestUtil.CreateArchiveExtraction(
Archive testArchive = CreateTestArchive(
Path.Combine("TestData", "Noark5", "DocumentfilesControl", "EmptyFiles")
);

Expand Down Expand Up @@ -136,7 +136,7 @@ public void EmptyDocumentFileSubjectToStatusUtgaarIsNotReported(string superFold
.Add("saksstatus", folderStatus))
.Add("saksstatus", superFolderStatus))))));

Archive testArchive = TestUtil.CreateArchiveExtraction(
Archive testArchive = CreateTestArchive(
Path.Combine("TestData", "Noark5", "DocumentfilesControl", "EmptyFiles")
);

Expand Down Expand Up @@ -177,7 +177,7 @@ public void OneEmptyDocumentFileInOneOfTwoArchivePartsIsReported()
.Add("referanseDokumentfil", "dokumenter/5000000.pdf")
.Add("filstoerrelse", "20637")))))))));

Archive testArchive = TestUtil.CreateArchiveExtraction(
Archive testArchive = CreateTestArchive(
Path.Combine("TestData", "Noark5", "DocumentfilesControl", "EmptyFiles")
);

Expand Down Expand Up @@ -218,7 +218,7 @@ public void FilePathsWithBackOrForwardSlashAreParsed()
.Add("referanseDokumentfil", "dokumenter\\5000000.pdf")
.Add("filstoerrelse", "20637")))))))));

Archive testArchive = TestUtil.CreateArchiveExtraction(
Archive testArchive = CreateTestArchive(
Path.Combine("TestData", "Noark5", "DocumentfilesControl", "EmptyFiles")
);

Expand All @@ -245,7 +245,7 @@ public void DocumentedAndActualFileSizeMismatchIsNotReported()
.Add("referanseDokumentfil", "dokumenter/5000000.pdf")
.Add("filstoerrelse", "0")))))))));

Archive testArchive = TestUtil.CreateArchiveExtraction(
Archive testArchive = CreateTestArchive(
Path.Combine("TestData", "Noark5", "DocumentfilesControl", "EmptyFiles")
);

Expand All @@ -271,7 +271,7 @@ public void NonExistingFileIsNotReported()
.Add("dokumentobjekt", new XmlElementHelper()
.Add("referanseDokumentfil", "non-existing.file")))))))));

Archive testArchive = TestUtil.CreateArchiveExtraction(
Archive testArchive = CreateTestArchive(
Path.Combine("TestData", "Noark5", "DocumentfilesControl", "EmptyFiles")
);

Expand All @@ -281,5 +281,14 @@ public void NonExistingFileIsNotReported()

testRun.TestResults.GetNumberOfResults().Should().Be(1);
}

private static Archive CreateTestArchive(string pathToArchive)
{
Archive archive = TestUtil.CreateArchiveExtraction(pathToArchive);

archive.DocumentFiles.Register(false);

return archive;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ private static TestRun CreateTestRun(string workingDirectory)
.WithWorkingDirectoryExternalContent(workingDirectory + "\\content")
.Build();

archive.DocumentFiles.Register(false);

var validateNumberOfDocumentfiles = new N5_28_ValidateNumberOfDocumentfiles();

validateNumberOfDocumentfiles.Test(archive);
Expand Down
51 changes: 51 additions & 0 deletions src/Arkivverket.Arkade.Core.Tests/Util/ChecksumTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,57 @@ public void GenerateChecksum()
_output.WriteLine("Checksum: " + checksum);
}


[Fact]
public void GenerateChecksumFromStream()
{
string pathToFile = AppDomain.CurrentDomain.BaseDirectory + "\\Util\\demo.txt";
_output.WriteLine("File path: " + pathToFile);

string checksum;
using (FileStream fileStream = File.OpenRead(pathToFile))
{
checksum = new Sha256ChecksumGenerator().GenerateChecksum(fileStream);
}
checksum.Should().Be("8661245AF5506ABE3347B7906A54EDCAE280EC8638E6801A373A948AD2A35D96"); // generated with another tool

_output.WriteLine("Checksum: " + checksum);
}

[Fact]
public void GenerateChecksumByBlockTransform()
{
string pathToFile = AppDomain.CurrentDomain.BaseDirectory + "\\Util\\demo.txt";
_output.WriteLine("File path: " + pathToFile);

var checksumGenerator = new Sha256ChecksumGenerator();
string checksum;

using (FileStream fileStream = File.OpenRead(pathToFile))
{
checksumGenerator.Initialize();

var buffer = new byte[32 * 1024];

while (true)
{
int numRead = fileStream.Read(buffer, 0, buffer.Length);
if (numRead <= 0)
{
break;
}
checksumGenerator.TransformBlock(buffer, numRead);
}

checksum = checksumGenerator.GenerateChecksum();
}

checksum.Should().Be("8661245AF5506ABE3347B7906A54EDCAE280EC8638E6801A373A948AD2A35D96"); // generated with another tool

_output.WriteLine("Checksum: " + checksum);
}


[Fact]
public void CheckForFileExistence()
{
Expand Down
2 changes: 1 addition & 1 deletion src/Arkivverket.Arkade.Core.Tests/Util/TarTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void CompressAndExtractArchive()
new TarCompressionUtility().CompressFolderContentToArchiveFile(pathToTargetTarFile, pathSourceFolder);
_output.WriteLine("Generated Archive");

new TarCompressionUtility().ExtractFolderFromArchive(pathToTargetTarFile, pathToExtractedDirectory);
new TarCompressionUtility().ExtractFolderFromArchive(pathToTargetTarFile, pathToExtractedDirectory, false, null);
_output.WriteLine("Extracted Archive");

// Check that correct number of files were extracted:
Expand Down
46 changes: 13 additions & 33 deletions src/Arkivverket.Arkade.Core/Base/Archive.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Reflection;
Expand All @@ -20,19 +19,22 @@ public class Archive
private static readonly ILogger Log = Serilog.Log.ForContext(MethodBase.GetCurrentMethod().DeclaringType);
private static IStatusEventHandler _statusEventHandler;

internal string? ArchiveFileFullName { get; }

public bool IsNoark5TarArchive => ArchiveFileFullName != null && ArchiveType is ArchiveType.Noark5;

public Uuid Uuid { get; }
public WorkingDirectory WorkingDirectory { get; }
public ArchiveType ArchiveType { get; }
private DirectoryInfo DocumentsDirectory { get; set; }
private ReadOnlyDictionary<string, DocumentFile> _documentFiles;
public ReadOnlyDictionary<string, DocumentFile> DocumentFiles => _documentFiles ?? GetDocumentFiles();
internal DocumentFiles DocumentFiles { get; }
public AddmlXmlUnit AddmlXmlUnit { get; }
public AddmlInfo AddmlInfo { get; }
public IArchiveDetails Details { get; }
public List<ArchiveXmlUnit> XmlUnits { get; private set; }

public Archive(ArchiveType archiveType, Uuid uuid, WorkingDirectory workingDirectory,
IStatusEventHandler statusEventHandler)
IStatusEventHandler statusEventHandler, string archiveFileFullName=null)
{
_statusEventHandler = statusEventHandler;

Expand All @@ -42,12 +44,14 @@ public Archive(ArchiveType archiveType, Uuid uuid, WorkingDirectory workingDirec

WorkingDirectory = workingDirectory;

ArchiveFileFullName = archiveFileFullName;

if (archiveType == ArchiveType.Siard)
{
Details = SetupSiardArchiveDetails(workingDirectory);
return;
}

AddmlXmlUnit = SetupAddmlXmlUnit();

if (!AddmlXmlUnit.File.Exists)
Expand All @@ -67,6 +71,10 @@ public Archive(ArchiveType archiveType, Uuid uuid, WorkingDirectory workingDirec
AddmlXmlUnit.Schema = new ArkadeBuiltInXmlSchema(AddmlXsdFileName, Details.ArchiveStandard);

SetupArchiveXmlUnits();

DocumentFiles = archiveFileFullName == null
? new DocumentFiles(GetDocumentsDirectory())
: new DocumentFiles(archiveFileFullName);
}
}

Expand Down Expand Up @@ -168,34 +176,6 @@ private void SetupArchiveXmlUnits()
}
}

private ReadOnlyDictionary<string, DocumentFile> GetDocumentFiles()
{
Log.Information("Registering document files.");

var documentFiles = new Dictionary<string, DocumentFile>();

DirectoryInfo documentsDirectory = GetDocumentsDirectory();

if (documentsDirectory.Exists)
{
foreach (FileInfo documentFileInfo in documentsDirectory.GetFiles("*", SearchOption.AllDirectories))
{
string relativePath = documentsDirectory.Parent != null
? Path.GetRelativePath(documentsDirectory.Parent.FullName, documentFileInfo.FullName)
: documentFileInfo.FullName;

documentFiles.Add(relativePath.Replace('\\', '/'), new DocumentFile(documentFileInfo));
}
}

// Instantiate field for next access:
_documentFiles = new ReadOnlyDictionary<string, DocumentFile>(documentFiles);

Log.Information($"{documentFiles.Count} document files registered.");

return _documentFiles;
}

private bool AddmlVersionIsSupported()
{
if (SupportedNoark5Versions.Contains(Details.ArchiveStandard))
Expand Down
3 changes: 3 additions & 0 deletions src/Arkivverket.Arkade.Core/Base/ArkadeApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ public void RunTests(TestSession testSession)

LanguageManager.SetResourcesLanguageForTesting(testSession.OutputLanguage);

if (testSession.TestRunContainsDocumentFileDependentTests)
testSession.Archive.DocumentFiles.Register(includeChecksums: testSession.TestRunContainsChecksumControl);

ITestEngine testEngine = _testEngineFactory.GetTestEngine(testSession);
testSession.TestSuite = testEngine.RunTestsOnArchive(testSession);

Expand Down
12 changes: 5 additions & 7 deletions src/Arkivverket.Arkade.Core/Base/DocumentFile.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
using System.IO;
using System;

namespace Arkivverket.Arkade.Core.Base
{
public class DocumentFile
{
public FileInfo FileInfo { get; }
public string FullName { get; init; }
public string Extension { get; init; }
public long Size { get; init; }
public DateTime CreationTime { get; init; }
public string CheckSum { get; set; }

public DocumentFile(FileInfo fileInfo)
{
FileInfo = fileInfo;
}
}
}
Loading

0 comments on commit d3d3f43

Please sign in to comment.