Skip to content

Commit

Permalink
[BMSPT-309] document guid is generated based on the document referenc…
Browse files Browse the repository at this point in the history
…e guid and file name when the bcf is upgraded from 2.1 to 3.0
  • Loading branch information
BalintBende committed Jul 30, 2024
1 parent 38be9e8 commit 9c8e034
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 12 deletions.
7 changes: 4 additions & 3 deletions bcf-toolkit.sln.DotSettings.user
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=00488d9b_002Ddfb9_002D47fc_002Da7da_002D60da862eab61/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="All tests from Solution #2" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
&lt;Solution /&gt;
&lt;/SessionState&gt;</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=4dc5af78_002D6a8f_002D4f5b_002D8833_002Df0ff81e0c049/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="All tests from Solution #3" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
&lt;Solution /&gt;
&lt;/SessionState&gt;</s:String>


<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=64259359_002Daf01_002D4100_002Db5a8_002D06c7d51d9f83/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="BuildMaximumInformationBcfFromStreamTest" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
Expand Down Expand Up @@ -35,9 +38,7 @@
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=87607a24_002D24bc_002D46e1_002Da035_002D28c18ba9b643/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="All tests from Solution" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
&lt;Solution /&gt;
&lt;/SessionState&gt;</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=9b61e927_002Deb40_002D473c_002D8f88_002D4fda2fb23707/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="All tests from Solution #3" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
&lt;Solution /&gt;
&lt;/SessionState&gt;</s:String>




Expand Down
29 changes: 20 additions & 9 deletions src/bcf-toolkit/Converter/Bcf21/SchemaConverterToBcf30.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Linq;
using BcfToolkit.Builder.Bcf30;
using BcfToolkit.Utils;

namespace BcfToolkit.Converter.Bcf21;

Expand All @@ -20,10 +21,13 @@ public static Model.Bcf30.Bcf Convert(Model.Bcf21.Bcf from) {
var builder = new BcfBuilder();
builder
.AddMarkups(from.Markups.Select(ConvertMarkup).ToList(), true)
.SetDocument(UpdateDocumentInfo(from.Markups
.SelectMany(m => m.Topic.DocumentReference)
.Where(r => !r.IsExternal)
.ToList()));
.SetDocument(dI => dI
.AddDocuments(from.Markups
.SelectMany(m => m.Topic.DocumentReference)
.Where(r => !r.IsExternal)
.ToList()
.Select(ConvertDocument)
.ToList()));

var project = from.Project;

Expand Down Expand Up @@ -120,11 +124,15 @@ private static Model.Bcf30.DocumentReference ConvertDocumentReference(
Model.Bcf21.TopicDocumentReference from) {
var builder = new DocumentReferenceBuilder();

var guid = from.Guid ??= Guid.NewGuid().ToString();
builder
.SetGuid(from.Guid ??= Guid.NewGuid().ToString())
.SetGuid(guid)
.SetUrl(from.IsExternal ? from.ReferencedDocument : null)
//TODO: generate guid based on guid and description
.SetDocumentGuid(Guid.NewGuid().ToString());
// pattern for document guid
// {guid of the referenced document}{name of the referenced document}
.SetDocumentGuid(!from.IsExternal
? GuidUtils.NewGuidByContent($"{guid}{from.ReferencedDocument}")
: null);

if (from.Description != string.Empty) {
builder.SetDescription(from.Description);
Expand Down Expand Up @@ -336,7 +344,7 @@ private static Model.Bcf30.Bitmap ConvertBitmap(
.Build();
}

private static Action<DocumentInfoBuilder> UpdateDocumentInfo(
private static Action<DocumentInfoBuilder> ConvertDocumentInfo(
List<Model.Bcf21.TopicDocumentReference> docReferences) {
return dI => dI
.AddDocuments(docReferences.Select(ConvertDocument).ToList());
Expand All @@ -347,7 +355,10 @@ private static Model.Bcf30.Document ConvertDocument(
var builder = new DocumentBuilder();
builder
.SetFileName(Path.GetFileName(docReference.ReferencedDocument))
.SetGuid(docReference.Guid)
// pattern for document guid
// {guid of the referenced document}{name of the referenced document}
.SetGuid(GuidUtils.NewGuidByContent(
$"{docReference.Guid}{docReference.ReferencedDocument}"))
.SetDocumentData(docReference.DocumentData);

if (docReference.Description != string.Empty) {
Expand Down
18 changes: 18 additions & 0 deletions src/bcf-toolkit/Utils/Guid.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Security.Cryptography;
using System.Text;

namespace BcfToolkit.Utils;

public static class GuidUtils {
/// <summary>
/// Method generates a new guid based on the input string. It creates an
/// MD5 hash from the data, then turns to GUID.
/// </summary>
/// <param name="input">Input data on which the hash is generated.</param>
/// <returns>Returns the GUID based on the input data.</returns>
public static string NewGuidByContent(string input) {
var hash = MD5.HashData(Encoding.UTF8.GetBytes(input));
return new Guid(hash).ToString();
}
}
9 changes: 9 additions & 0 deletions src/tests/Converter/Bcf21/ConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,17 @@ public async Task BuildV30BcfFromStreamWithInternalDocumentTest() {
Assert.That(typeof(BcfToolkit.Model.Bcf30.Bcf), Is.EqualTo(bcf.GetType()));
var document =
bcf.Document?.Documents.FirstOrDefault(d => d.Filename.Equals("markup.xsd"));
var documentRef = bcf
.Markups
.FirstOrDefault(m =>
m.Topic.Guid.Equals("7ddc3ef0-0ab7-43f1-918a-45e38b42369c"))?
.Topic
.DocumentReferences
.FirstOrDefault(d =>
d.Description.Equals("Markup.xsd Schema"));
Assert.That(document?.DocumentData.Mime, Is.EqualTo("data:application/xml;base64"));
Assert.That(document?.DocumentData.Data.Length, Is.EqualTo(10644));
Assert.That(documentRef?.DocumentGuid, Is.EqualTo(document?.Guid));
}

/// <summary>
Expand Down

0 comments on commit 9c8e034

Please sign in to comment.