Skip to content

Commit 96a3ada

Browse files
.
1 parent 0a37026 commit 96a3ada

File tree

14 files changed

+1006
-57
lines changed

14 files changed

+1006
-57
lines changed
Lines changed: 354 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,354 @@
1+
// -------------------------------------------------------------------------------------------------
2+
// <copyright file="ArchiveExtensionsTestFixture.cs" company="Starion Group S.A.">
3+
//
4+
// Copyright 2022-2026 Starion Group S.A.
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// </copyright>
19+
// -------------------------------------------------------------------------------------------------
20+
21+
namespace SysML2.NET.ModelInterchange.Tests
22+
{
23+
using System;
24+
using System.Collections.Generic;
25+
using System.IO;
26+
using System.Text;
27+
using System.Threading;
28+
using System.Threading.Tasks;
29+
30+
using NUnit.Framework;
31+
32+
using SysML2.NET.Extensions.ModelInterchange;
33+
34+
[TestFixture]
35+
public class ArchiveExtensionsTestFixture
36+
{
37+
[Test]
38+
public void Verify_that_TryGetModelEntryByIndexKey_throws_when_archive_is_null()
39+
{
40+
Assert.That(() =>
41+
{
42+
ArchiveExtensions.TryGetModelEntryByIndexKey(null, "Base", out _);
43+
}, Throws.ArgumentNullException);
44+
}
45+
46+
[TestCase(null)]
47+
[TestCase("")]
48+
[TestCase(" ")]
49+
public void Verify_that_TryGetModelEntryByIndexKey_throws_when_indexKey_is_invalid(string indexKey)
50+
{
51+
var archive = new Archive
52+
{
53+
Metadata = new InterchangeProjectMetadata { Index = new Dictionary<string, string>() },
54+
Models = Array.Empty<ModelEntry>()
55+
};
56+
57+
Assert.That(() =>
58+
{
59+
archive.TryGetModelEntryByIndexKey(indexKey, out _);
60+
}, Throws.ArgumentException);
61+
}
62+
63+
[Test]
64+
public void Verify_that_TryGetModelEntryByIndexKey_returns_false_when_metadata_index_is_missing()
65+
{
66+
var archive = new Archive
67+
{
68+
Metadata = new InterchangeProjectMetadata { Index = null },
69+
Models = Array.Empty<ModelEntry>()
70+
};
71+
72+
var ok = archive.TryGetModelEntryByIndexKey("Base", out var entry);
73+
74+
Assert.That(ok, Is.False);
75+
Assert.That(entry, Is.Null);
76+
}
77+
78+
[Test]
79+
public void Verify_that_TryGetModelEntryByIndexKey_returns_false_when_key_is_missing()
80+
{
81+
var archive = new Archive
82+
{
83+
Metadata = new InterchangeProjectMetadata
84+
{
85+
Index = new Dictionary<string, string>(StringComparer.Ordinal)
86+
{
87+
["Other"] = "Other.kerml"
88+
}
89+
},
90+
Models = new[]
91+
{
92+
CreateModelEntry("Other.kerml")
93+
}
94+
};
95+
96+
var ok = archive.TryGetModelEntryByIndexKey("Base", out var entry);
97+
98+
Assert.That(ok, Is.False);
99+
Assert.That(entry, Is.Null);
100+
}
101+
102+
[Test]
103+
public void Verify_that_TryGetModelEntryByIndexKey_returns_false_when_path_is_null_or_whitespace()
104+
{
105+
var archive = new Archive
106+
{
107+
Metadata = new InterchangeProjectMetadata
108+
{
109+
Index = new Dictionary<string, string>(StringComparer.Ordinal)
110+
{
111+
["Base"] = " "
112+
}
113+
},
114+
Models = new[]
115+
{
116+
CreateModelEntry("Base.kerml")
117+
}
118+
};
119+
120+
var ok = archive.TryGetModelEntryByIndexKey("Base", out var entry);
121+
122+
Assert.That(ok, Is.False);
123+
Assert.That(entry, Is.Null);
124+
}
125+
126+
[Test]
127+
public void Verify_that_TryGetModelEntryByIndexKey_returns_false_when_model_is_not_present()
128+
{
129+
var archive = new Archive
130+
{
131+
Metadata = new InterchangeProjectMetadata
132+
{
133+
Index = new Dictionary<string, string>(StringComparer.Ordinal)
134+
{
135+
["Base"] = "Base.kerml"
136+
}
137+
},
138+
Models = new[]
139+
{
140+
CreateModelEntry("Other.kerml")
141+
}
142+
};
143+
144+
var ok = archive.TryGetModelEntryByIndexKey("Base", out var entry);
145+
146+
Assert.That(ok, Is.False);
147+
Assert.That(entry, Is.Null);
148+
}
149+
150+
[Test]
151+
public void Verify_that_TryGetModelEntryByIndexKey_returns_true_and_sets_entry_when_found()
152+
{
153+
var expected = CreateModelEntry("Base.kerml");
154+
155+
var archive = new Archive
156+
{
157+
Metadata = new InterchangeProjectMetadata
158+
{
159+
Index = new Dictionary<string, string>(StringComparer.Ordinal)
160+
{
161+
["Base"] = "Base.kerml"
162+
}
163+
},
164+
Models = new[]
165+
{
166+
expected,
167+
CreateModelEntry("Other.kerml")
168+
}
169+
};
170+
171+
var ok = archive.TryGetModelEntryByIndexKey("Base", out var entry);
172+
173+
Assert.That(ok, Is.True);
174+
Assert.That(entry, Is.SameAs(expected));
175+
}
176+
177+
[Test]
178+
public void Verify_that_TryGetModelEntryByIndexKey_matches_using_normalized_paths()
179+
{
180+
// Index uses backslashes; model entry uses forward slashes.
181+
var expected = CreateModelEntry("folder/Base.kerml");
182+
183+
var archive = new Archive
184+
{
185+
Metadata = new InterchangeProjectMetadata
186+
{
187+
Index = new Dictionary<string, string>(StringComparer.Ordinal)
188+
{
189+
["Base"] = @"folder\Base.kerml"
190+
}
191+
},
192+
Models = new[]
193+
{
194+
expected
195+
}
196+
};
197+
198+
var ok = archive.TryGetModelEntryByIndexKey("Base", out var entry);
199+
200+
Assert.That(ok, Is.True);
201+
Assert.That(entry, Is.SameAs(expected));
202+
}
203+
204+
[Test]
205+
public void Verify_that_GetModelEntryByIndexKey_throws_when_archive_is_null()
206+
{
207+
Assert.That(() => ArchiveExtensions.GetModelEntryByIndexKey(null, "Base"), Throws.ArgumentNullException);
208+
}
209+
210+
[TestCase(null)]
211+
[TestCase("")]
212+
[TestCase(" ")]
213+
public void Verify_that_GetModelEntryByIndexKey_throws_when_indexKey_is_invalid(string indexKey)
214+
{
215+
var archive = new Archive
216+
{
217+
Metadata = new InterchangeProjectMetadata { Index = new Dictionary<string, string>() },
218+
Models = Array.Empty<ModelEntry>()
219+
};
220+
221+
Assert.That(() => archive.GetModelEntryByIndexKey(indexKey), Throws.ArgumentException);
222+
}
223+
224+
[Test]
225+
public void Verify_that_GetModelEntryByIndexKey_throws_when_metadata_index_is_not_available()
226+
{
227+
var archive = new Archive
228+
{
229+
Metadata = null,
230+
Models = Array.Empty<ModelEntry>()
231+
};
232+
233+
Assert.That(() => archive.GetModelEntryByIndexKey("Base"), Throws.InvalidOperationException);
234+
}
235+
236+
[Test]
237+
public void Verify_that_GetModelEntryByIndexKey_throws_when_key_is_not_found()
238+
{
239+
var archive = new Archive
240+
{
241+
Metadata = new InterchangeProjectMetadata
242+
{
243+
Index = new Dictionary<string, string>(StringComparer.Ordinal)
244+
{
245+
["Other"] = "Other.kerml"
246+
}
247+
},
248+
Models = new[] { CreateModelEntry("Other.kerml") }
249+
};
250+
251+
Assert.That(() => archive.GetModelEntryByIndexKey("Base"), Throws.TypeOf<KeyNotFoundException>());
252+
}
253+
254+
[Test]
255+
public void Verify_that_GetModelEntryByIndexKey_throws_when_index_entry_path_is_null_or_whitespace()
256+
{
257+
var archive = new Archive
258+
{
259+
Metadata = new InterchangeProjectMetadata
260+
{
261+
Index = new Dictionary<string, string>(StringComparer.Ordinal)
262+
{
263+
["Base"] = " "
264+
}
265+
},
266+
Models = new[] { CreateModelEntry("Base.kerml") }
267+
};
268+
269+
Assert.That(() => archive.GetModelEntryByIndexKey("Base"), Throws.TypeOf<KeyNotFoundException>());
270+
}
271+
272+
[Test]
273+
public void Verify_that_GetModelEntryByIndexKey_throws_when_model_entry_is_missing()
274+
{
275+
var archive = new Archive
276+
{
277+
Metadata = new InterchangeProjectMetadata
278+
{
279+
Index = new Dictionary<string, string>(StringComparer.Ordinal)
280+
{
281+
["Base"] = "Base.kerml"
282+
}
283+
},
284+
Models = new[] { CreateModelEntry("Other.kerml") }
285+
};
286+
287+
Assert.That(() => archive.GetModelEntryByIndexKey("Base"), Throws.TypeOf<FileNotFoundException>());
288+
}
289+
290+
[Test]
291+
public void Verify_that_GetModelEntryByIndexKey_returns_entry_when_found()
292+
{
293+
var expected = CreateModelEntry("Base.kerml");
294+
295+
var archive = new Archive
296+
{
297+
Metadata = new InterchangeProjectMetadata
298+
{
299+
Index = new Dictionary<string, string>(StringComparer.Ordinal)
300+
{
301+
["Base"] = "Base.kerml"
302+
}
303+
},
304+
Models = new[] { expected }
305+
};
306+
307+
var entry = archive.GetModelEntryByIndexKey("Base");
308+
309+
Assert.That(entry, Is.SameAs(expected));
310+
}
311+
312+
[Test]
313+
public async Task Verify_that_OpenModelByIndexKeyAsync_opens_stream_from_entry()
314+
{
315+
var expectedBytes = Encoding.UTF8.GetBytes("hello kerml");
316+
317+
var entry = new ModelEntry
318+
{
319+
Path = "Base.kerml",
320+
ContentType = "text/plain",
321+
OpenReadAsync = _ => new ValueTask<Stream>(new MemoryStream(expectedBytes, writable: false))
322+
};
323+
324+
var archive = new Archive
325+
{
326+
Metadata = new InterchangeProjectMetadata
327+
{
328+
Index = new Dictionary<string, string>(StringComparer.Ordinal)
329+
{
330+
["Base"] = "Base.kerml"
331+
}
332+
},
333+
Models = new[] { entry }
334+
};
335+
336+
await using var stream = await archive.OpenModelByIndexKeyAsync("Base", CancellationToken.None);
337+
338+
Assert.That(stream, Is.Not.Null);
339+
using var ms = new MemoryStream();
340+
await stream.CopyToAsync(ms);
341+
Assert.That(ms.ToArray(), Is.EqualTo(expectedBytes));
342+
}
343+
344+
private static ModelEntry CreateModelEntry(string path)
345+
{
346+
return new ModelEntry
347+
{
348+
Path = path,
349+
ContentType = "text/plain",
350+
OpenReadAsync = _ => new ValueTask<Stream>(new MemoryStream(Array.Empty<byte>(), writable: false))
351+
};
352+
}
353+
}
354+
}

0 commit comments

Comments
 (0)