-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathInventoryLoaderIncompleteFlagTests.cs
More file actions
115 lines (98 loc) · 3.65 KB
/
InventoryLoaderIncompleteFlagTests.cs
File metadata and controls
115 lines (98 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System.IO.Compression;
using ByteSync.Common.Business.EndPoints;
using ByteSync.Common.Business.Inventories;
using ByteSync.Common.Business.Misc;
using ByteSync.Common.Controls.Json;
using ByteSync.Models.FileSystems;
using ByteSync.Models.Inventories;
using ByteSync.Services.Inventories;
using FluentAssertions;
using NUnit.Framework;
namespace ByteSync.Client.UnitTests.Services.Inventories;
[TestFixture]
public class InventoryLoaderIncompleteFlagTests
{
private string _tempDirectory = null!;
[SetUp]
public void Setup()
{
_tempDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(_tempDirectory);
}
[TearDown]
public void TearDown()
{
if (Directory.Exists(_tempDirectory))
{
Directory.Delete(_tempDirectory, true);
}
}
[Test]
public void Load_ShouldMarkPartIncomplete_WhenInaccessibleDescriptionsExist()
{
var inventory = BuildInventoryWithInaccessibleDirectory();
var zipPath = CreateInventoryZipFile(_tempDirectory, inventory);
using var loader = new InventoryLoader(zipPath);
var loadedInventory = loader.Inventory;
loadedInventory.InventoryParts.Should().HaveCount(1);
loadedInventory.InventoryParts[0].IsIncompleteDueToAccess.Should().BeTrue();
}
[Test]
public void Load_ShouldKeepSkippedCountsByReason()
{
// Arrange
var inventory = BuildInventoryWithInaccessibleDirectory();
var part = inventory.InventoryParts[0];
part.RecordSkippedEntry(SkipReason.Hidden);
part.RecordSkippedEntry(SkipReason.Hidden);
part.RecordSkippedEntry(SkipReason.NoiseEntry);
var zipPath = CreateInventoryZipFile(_tempDirectory, inventory);
// Act
using var loader = new InventoryLoader(zipPath);
var loadedPart = loader.Inventory.InventoryParts[0];
// Assert
loadedPart.SkippedCount.Should().Be(3);
loadedPart.GetSkippedCountByReason(SkipReason.Hidden).Should().Be(2);
loadedPart.GetSkippedCountByReason(SkipReason.NoiseEntry).Should().Be(1);
loadedPart.GetSkippedCountByReason(SkipReason.Offline).Should().Be(0);
}
private static Inventory BuildInventoryWithInaccessibleDirectory()
{
var inventory = new Inventory
{
InventoryId = "INV_A",
Code = "A",
MachineName = "MachineA",
Endpoint = new ByteSyncEndpoint
{
ClientInstanceId = "CII_A",
OSPlatform = OSPlatforms.Windows
}
};
var part = new InventoryPart(inventory, "c:/rootA", FileSystemTypes.Directory)
{
Code = "A1",
IsIncompleteDueToAccess = false
};
inventory.Add(part);
var inaccessibleDir = new DirectoryDescription
{
InventoryPart = part,
RelativePath = "/blocked",
IsAccessible = false
};
part.DirectoryDescriptions.Add(inaccessibleDir);
return inventory;
}
private static string CreateInventoryZipFile(string directory, Inventory inventory)
{
var zipPath = Path.Combine(directory, $"{Guid.NewGuid()}.zip");
using var zip = ZipFile.Open(zipPath, ZipArchiveMode.Create);
var entry = zip.CreateEntry("inventory.json");
using var entryStream = entry.Open();
using var writer = new StreamWriter(entryStream);
var json = JsonHelper.Serialize(inventory);
writer.Write(json);
return zipPath;
}
}