-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathInventoryComparerIncompletePartsFlatTests.cs
More file actions
164 lines (137 loc) · 5.75 KB
/
InventoryComparerIncompletePartsFlatTests.cs
File metadata and controls
164 lines (137 loc) · 5.75 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System.IO.Compression;
using ByteSync.Business.Sessions;
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.Comparisons;
using FluentAssertions;
using ByteSync.TestsCommon;
using NUnit.Framework;
namespace ByteSync.Client.UnitTests.Services.Comparisons;
[TestFixture]
public class InventoryComparerIncompletePartsFlatTests : AbstractTester
{
private string _tempDirectory = null!;
[SetUp]
public void Setup()
{
CreateTestDirectory();
_tempDirectory = TestDirectory.FullName;
}
[TearDown]
public void TearDown()
{
if (TestDirectory?.Exists == true)
{
TestDirectory.Delete(true);
}
}
[Test]
public void Compare_Flat_WithIncompletePart_AddsAccessIssuePlaceholderAndSkipsPartContent()
{
var inventoryA = CreateInventoryWithFile("INV_A", "A", "/file.txt", false);
var inventoryB = CreateInventoryWithFile("INV_B", "B", "/file.txt", true);
var inventoryFileA = CreateInventoryZipFile(_tempDirectory, inventoryA);
var inventoryFileB = CreateInventoryZipFile(_tempDirectory, inventoryB);
var sessionSettings = new SessionSettings
{
DataType = DataTypes.Files,
MatchingMode = MatchingModes.Flat,
LinkingCase = LinkingCases.Insensitive
};
var initialStatusBuilder = new InitialStatusBuilder();
using var comparer = new InventoryComparer(sessionSettings, initialStatusBuilder);
comparer.AddInventory(inventoryFileA);
comparer.AddInventory(inventoryFileB);
var result = comparer.Compare();
result.ComparisonItems.Should().HaveCount(1);
var item = result.ComparisonItems.Single();
item.PathIdentity.LinkingKeyValue.Should().Be("file.txt");
var inventoryAResult = result.Inventories.Single(i => i.Code == "A");
var inventoryBResult = result.Inventories.Single(i => i.Code == "B");
var partA = inventoryAResult.InventoryParts.Single();
var partB = inventoryBResult.InventoryParts.Single();
item.ContentIdentities.Should().HaveCount(2);
var contentIdentityA = item.ContentIdentities.Single(ci => ci.IsPresentIn(partA));
contentIdentityA.Core.Should().NotBeNull();
var contentIdentityB = item.ContentIdentities.Single(ci => ci.IsPresentIn(partB));
contentIdentityB.Core.Should().BeNull();
contentIdentityB.HasAccessIssue.Should().BeTrue();
contentIdentityB.AccessIssueInventoryParts.Should().Contain(partB);
var placeholderFileDescription = contentIdentityB.GetFileSystemDescriptions(partB).OfType<FileDescription>().Single();
placeholderFileDescription.IsAccessible.Should().BeFalse();
placeholderFileDescription.RelativePath.Should().Be("/file.txt");
}
[Test]
public void Compare_Flat_AllPartsIncomplete_ReturnsNoItems()
{
var inventoryB = CreateInventoryWithFile("INV_B", "B", "/file.txt", true);
var inventoryFileB = CreateInventoryZipFile(_tempDirectory, inventoryB);
var sessionSettings = new SessionSettings
{
DataType = DataTypes.Files,
MatchingMode = MatchingModes.Flat,
LinkingCase = LinkingCases.Insensitive
};
var initialStatusBuilder = new InitialStatusBuilder();
using var comparer = new InventoryComparer(sessionSettings, initialStatusBuilder);
comparer.AddInventory(inventoryFileB);
var result = comparer.Compare();
result.ComparisonItems.Should().BeEmpty();
}
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;
}
private static Inventory CreateInventoryWithFile(string inventoryId, string code, string relativeFilePath, bool isIncomplete)
{
var inventory = new Inventory
{
InventoryId = inventoryId,
Code = code,
MachineName = $"Machine{code}",
Endpoint = new ByteSyncEndpoint
{
ClientInstanceId = $"CII_{code}",
OSPlatform = OSPlatforms.Windows
}
};
var part = new InventoryPart(inventory, $"c:/root{code}", FileSystemTypes.Directory)
{
Code = $"{code}1",
IsIncompleteDueToAccess = isIncomplete
};
inventory.Add(part);
if (isIncomplete)
{
var blockedDirectory = new DirectoryDescription
{
InventoryPart = part,
RelativePath = "/blocked",
IsAccessible = false
};
part.DirectoryDescriptions.Add(blockedDirectory);
}
var fileDescription = new FileDescription
{
InventoryPart = part,
RelativePath = relativeFilePath,
Size = 100,
CreationTimeUtc = DateTime.UtcNow,
LastWriteTimeUtc = DateTime.UtcNow,
IsAccessible = true
};
part.FileDescriptions.Add(fileDescription);
return inventory;
}
}