Skip to content

Commit

Permalink
Merge PSI functionality into PDPs (#878)
Browse files Browse the repository at this point in the history
  • Loading branch information
lyonsil authored May 1, 2024
1 parent 99a61ca commit bddb74e
Show file tree
Hide file tree
Showing 62 changed files with 2,219 additions and 4,162 deletions.
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
using System.Diagnostics.CodeAnalysis;
using Paranext.DataProvider.MessageTransports;
using Paranext.DataProvider.Projects;
using System.Diagnostics.CodeAnalysis;

namespace TestParanextDataProvider
{
[ExcludeFromCodeCoverage]
internal class DummyParatextProjectStorageInterpreter : ParatextProjectStorageInterpreter
internal class DummyParatextProjectDataProvider : ParatextProjectDataProvider
{
private readonly Dictionary<string, byte[]> _inMemoryFiles = new();

public DummyParatextProjectStorageInterpreter(PapiClient papiClient, DummyLocalParatextProjects paratextProjects) : base(papiClient, paratextProjects)
{
}

public DummyParatextProjectDataProvider(
string name,
PapiClient papiClient,
ProjectDetails projectDetails,
LocalParatextProjects paratextProjects
)
: base(name, papiClient, projectDetails, paratextProjects) { }

protected override IProjectStreamManager CreateStreamManager(ProjectDetails projectDetails)
{
return new InMemoryStreamManager(this);
}

#region InMemoryStreamManager class

private sealed class InMemoryStreamManager : IProjectStreamManager
{
private readonly DummyParatextProjectStorageInterpreter _owner;
private readonly DummyParatextProjectDataProvider _owner;

public InMemoryStreamManager(DummyParatextProjectStorageInterpreter owner)
public InMemoryStreamManager(DummyParatextProjectDataProvider owner)
{
_owner = owner;
}

#region Implementation of IProjectStreamManager

public void Initialize()
{
// Nothing to do
Expand All @@ -51,24 +57,31 @@ public bool DeleteDataStream(string streamName)
{
throw new NotImplementedException();
}

#endregion
}

#endregion

#region InMemoryFile class

private sealed class InMemoryFile : MemoryStream
{
private readonly DummyParatextProjectStorageInterpreter _owner;
private readonly DummyParatextProjectDataProvider _owner;
private readonly string _streamName;

public InMemoryFile(DummyParatextProjectStorageInterpreter owner, string streamName,
byte[] existingData) : base(existingData)
public InMemoryFile(
DummyParatextProjectDataProvider owner,
string streamName,
byte[] existingData
)
: base(existingData)
{
_owner = owner;
_streamName = streamName;
}
public InMemoryFile(DummyParatextProjectStorageInterpreter owner, string streamName)

public InMemoryFile(DummyParatextProjectDataProvider owner, string streamName)
{
_owner = owner;
_streamName = streamName;
Expand All @@ -82,6 +95,7 @@ protected override void Dispose(bool disposing)
base.Dispose(disposing);
}
}

#endregion
}
}
30 changes: 25 additions & 5 deletions c-sharp-tests/PapiTestBase.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System.Diagnostics.CodeAnalysis;
using System.Text;
using Paranext.DataProvider.Projects;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Web;
using System.Xml.Linq;
using Paratext.Data;
using Paranext.DataProvider.MessageHandlers;
using Paranext.DataProvider.Messages;
using System.Web;
using System.Diagnostics.CodeAnalysis;
using Paranext.DataProvider.Projects;
using Paratext.Data;

namespace TestParanextDataProvider
{
Expand Down Expand Up @@ -99,7 +100,7 @@ protected static ProjectDetails CreateProjectDetails(
string projectType = ""
)
{
ProjectMetadata metadata = new(id, name, "ParatextFolders", projectType);
ProjectMetadata metadata = new(id, name, projectType);
return new ProjectDetails(metadata, "testDirectoryThatDoesNotExist");
}

Expand Down Expand Up @@ -274,6 +275,25 @@ int expectedRequestId
Assert.That(response.RequestId, Is.EqualTo(expectedRequestId));
});
}

/// <summary>
/// Verifies a ResponseToRequest from a PDP call
/// </summary>
protected static void VerifyResponseToRequest(
ResponseToRequest response,
string? expectedErrorMessage,
string? expectedContents
)
{
Assert.Multiple(() =>
{
Assert.That(response.ErrorMessage, Is.EqualTo(expectedErrorMessage));
Assert.That(response.Success, Is.EqualTo(expectedErrorMessage == null));
string? contents = response.Contents as string;
Assert.That(contents, Is.EqualTo(expectedContents));
});
}

#endregion
}
}
2 changes: 1 addition & 1 deletion c-sharp-tests/Projects/LocalParatextProjectsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,6 @@ private void CreateTempProject(string folder, ProjectMetadata metadata)

private static ProjectMetadata CreateParatextProjectMetadata(string name)
{
return new ProjectMetadata(TEST_ID, name, "paratextFolders", "paratext");
return new ProjectMetadata(TEST_ID, name, "paratext");
}
}
88 changes: 73 additions & 15 deletions c-sharp-tests/Projects/ParatextDataProviderTests.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
using System.Diagnostics.CodeAnalysis;
using Paranext.DataProvider.Messages;
using Paranext.DataProvider.Projects;
using PtxUtils;
using System.Text.Json;
using System.Text.Json.Nodes;
using Paranext.DataProvider.MessageHandlers;
using Paranext.DataProvider.Messages;
using Paranext.DataProvider.Projects;
using Paratext.Data;
using SIL.Scripture;

namespace TestParanextDataProvider.Projects
{
[ExcludeFromCodeCoverage]
internal class ParatextDataProviderTests : PsiTestBase
internal class ParatextDataProviderTests : PapiTestBase
{
private const string PdpName = "soup";
private const string PdpDataRequest = "object:soup-pdp-data.function";
Expand Down Expand Up @@ -45,7 +45,8 @@ public async Task GetFunctions_MissingParameter(string function)
Random random = new();
int requesterId = random.Next();

ParatextProjectDataProvider provider = new(Psi, PdpName, Client, _projectDetails);
DummyParatextProjectDataProvider provider =
new(PdpName, Client, _projectDetails, ParatextProjects);
await provider.RegisterDataProvider();

JsonElement serverMessage = CreateRequestMessage(function);
Expand Down Expand Up @@ -98,7 +99,8 @@ string expectedError
Random random = new();
int requesterId = random.Next();

ParatextProjectDataProvider provider = new(Psi, PdpName, Client, _projectDetails);
DummyParatextProjectDataProvider provider =
new(PdpName, Client, _projectDetails, ParatextProjects);
await provider.RegisterDataProvider();

JsonElement serverMessage = CreateRequestMessage(
Expand Down Expand Up @@ -158,7 +160,8 @@ string expectedResult
null
);

ParatextProjectDataProvider provider = new(Psi, PdpName, Client, _projectDetails);
DummyParatextProjectDataProvider provider =
new(PdpName, Client, _projectDetails, ParatextProjects);
await provider.RegisterDataProvider();

JsonElement serverMessage = CreateRequestMessage(
Expand Down Expand Up @@ -211,7 +214,8 @@ string expectedResult
null
);

ParatextProjectDataProvider provider = new(Psi, PdpName, Client, _projectDetails);
DummyParatextProjectDataProvider provider =
new(PdpName, Client, _projectDetails, ParatextProjects);
await provider.RegisterDataProvider();

// Set up an event listener to listen for the update
Expand Down Expand Up @@ -242,14 +246,14 @@ string expectedResult
null,
requestType,
requesterId,
ParatextProjectStorageInterpreter.AllScriptureDataTypes
ParatextProjectDataProvider.AllScriptureDataTypes
);

// Verify an update event was sent out properly
Assert.That(updateEvents.Count, Is.EqualTo(1));
Assert.That(
updateEvents[0].Event,
Is.EqualTo(ParatextProjectStorageInterpreter.AllScriptureDataTypes)
Is.EqualTo(ParatextProjectDataProvider.AllScriptureDataTypes)
);

// Verify the new text was saved to disk
Expand Down Expand Up @@ -304,7 +308,8 @@ string expectedResult
null
);

ParatextProjectDataProvider provider = new(Psi, PdpName, Client, _projectDetails);
DummyParatextProjectDataProvider provider =
new(PdpName, Client, _projectDetails, ParatextProjects);
await provider.RegisterDataProvider();

// Set up an event listener to listen for the update
Expand Down Expand Up @@ -335,14 +340,14 @@ string expectedResult
null,
requestType,
requesterId,
ParatextProjectStorageInterpreter.AllScriptureDataTypes
ParatextProjectDataProvider.AllScriptureDataTypes
);

// Verify an update event was sent out properly
Assert.That(updateEvents.Count, Is.EqualTo(1));
Assert.That(
updateEvents[0].Event,
Is.EqualTo(ParatextProjectStorageInterpreter.AllScriptureDataTypes)
Is.EqualTo(ParatextProjectDataProvider.AllScriptureDataTypes)
);

// Verify the new text was saved to disk
Expand All @@ -364,13 +369,65 @@ string expectedResult
VerifyUsfmSame(stringContents!, newValue, _scrText, 1);
}

[TestCase(null, "myFile.txt", "Must provide an extension name")]
[TestCase("", "myFile.txt", "Must provide an extension name")]
[TestCase("myExtension", null, "Must provide a data qualifier")]
[TestCase("myExtension", "", "Must provide a data qualifier")]
public async Task GetExtensionData_InvalidParameters_ReturnsError(
string? extensionName,
string? dataQualifier,
string? expectedError
)
{
DummyParatextProjectDataProvider provider =
new(PdpName, Client, _projectDetails, ParatextProjects);
await provider.RegisterDataProvider();

ResponseToRequest response = provider.GetExtensionData(
new ProjectDataScope
{
ExtensionName = extensionName,
DataQualifier = dataQualifier
}
);

VerifyResponseToRequest(response, expectedError, null);
}

[TestCase(null, "myFile.txt", "Must provide an extension name")]
[TestCase("", "myFile.txt", "Must provide an extension name")]
[TestCase("myExtension", null, "Must provide a data qualifier")]
[TestCase("myExtension", "", "Must provide a data qualifier")]
public async Task SetExtensionData_InvalidParameters_ReturnsError(
string? extensionName,
string? dataQualifier,
string? expectedError
)
{
DummyParatextProjectDataProvider provider =
new(PdpName, Client, _projectDetails, ParatextProjects);
await provider.RegisterDataProvider();

ResponseToRequest response = provider.SetExtensionData(
new ProjectDataScope
{
ExtensionName = extensionName,
DataQualifier = dataQualifier
},
"Random data"
);

VerifyResponseToRequest(response, expectedError, null);
}

[Test]
public async Task GetExtensionData_NoData_ReturnsError()
{
Random random = new();
int requesterId = random.Next();

ParatextProjectDataProvider provider = new(Psi, PdpName, Client, _projectDetails);
DummyParatextProjectDataProvider provider =
new(PdpName, Client, _projectDetails, ParatextProjects);
await provider.RegisterDataProvider();

JsonNode scope = CreateDataScope("myExtension", "myFile.txt");
Expand All @@ -390,7 +447,8 @@ public async Task SetAndGetExtensionData_SavesAndGetsData()
Random random = new();
int requesterId = random.Next();

ParatextProjectDataProvider provider = new(Psi, PdpName, Client, _projectDetails);
DummyParatextProjectDataProvider provider =
new(PdpName, Client, _projectDetails, ParatextProjects);
await provider.RegisterDataProvider();

JsonNode scope = CreateDataScope("myExtension", "myFile.txt");
Expand Down
Loading

0 comments on commit bddb74e

Please sign in to comment.