Skip to content

Commit

Permalink
add factory to service provider. Create internal access for test in P…
Browse files Browse the repository at this point in the history
…ersistence
  • Loading branch information
abaskk-msft committed Jul 18, 2024
1 parent 7a07194 commit 32c1de6
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 28 deletions.
4 changes: 1 addition & 3 deletions .version/PipelineAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

// This is pipeline generated file. Do not modify. This will be replaced with the actual versions in the actual Pipeline.
using System.Reflection;
using System.Runtime.CompilerServices;
[assembly: AssemblyVersion("0.0.0.0")]
[assembly: AssemblyFileVersion("0.0.0.0")]
[assembly: AssemblyInformationalVersion("0.0.0.0-dev-00000000")]
[assembly: InternalsVisibleTo("Persistence.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b5fc90e7027f67871e773a8fde8938c81dd402ba65b9201d60593e96c492651e889cc13f1415ebb53fac1131ae0bd333c5ee6021672d9718ea31a8aebd0da0072f25d87dba6fc90ffd598ed4da35e44c398c454307e8e33b8426143daec9f596836f97c8f74750e5975c64e2189f45def46b2a2b1247adc3652bf5c308055da9")]
[assembly: AssemblyInformationalVersion("0.0.0.0-dev-00000000")]
5 changes: 5 additions & 0 deletions src/Persistence.Tests/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.PowerPlatform.PowerApps.Persistence.MsApp;
using Microsoft.PowerPlatform.PowerApps.Persistence.Templates;
using Microsoft.PowerPlatform.PowerApps.Persistence.Yaml;
using Microsoft.PowerPlatform.PowerApps.Persistence.YamlValidator;

namespace Persistence.Tests;

Expand All @@ -20,6 +21,8 @@ public abstract class TestBase : VSTestBase

public IControlFactory ControlFactory { get; private set; }

public IValidatorFactory ValidatorFactory { get; private set; }

static TestBase()
{
ServiceProvider = BuildServiceProvider();
Expand All @@ -31,6 +34,7 @@ public TestBase()
ControlTemplateStore = ServiceProvider.GetRequiredService<IControlTemplateStore>();
MsappArchiveFactory = ServiceProvider.GetRequiredService<IMsappArchiveFactory>();
ControlFactory = ServiceProvider.GetRequiredService<IControlFactory>();
ValidatorFactory = ServiceProvider.GetRequiredService<IValidatorFactory>();
}

private static ServiceProvider BuildServiceProvider()
Expand All @@ -44,6 +48,7 @@ private static ServiceProvider BuildServiceProvider()
private static ServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddPowerAppsPersistence(useDefaultTemplates: true);
services.AddPowerAppsPersistenceYamlValidator();

return services.BuildServiceProvider();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Persistence.Tests/YamlValidator/ValidatorFactoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
namespace Persistence.Tests.YamlValidator;

[TestClass]
public class ValidatorFactoryTest
public class ValidatorFactoryTest : TestBase
{
[TestMethod]
public void GetValidatorTest()
{
var factory = new ValidatorFactory();
var validator = factory.GetValidator();
var validator = factory.CreateValidator();

Assert.IsNotNull(validator);
Assert.IsInstanceOfType(validator, typeof(IValidator));
Expand Down
5 changes: 2 additions & 3 deletions src/Persistence.Tests/YamlValidator/ValidatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace Persistence.Tests.YamlValidator;

[TestClass]
public class ValidatorTest
public class ValidatorTest : TestBase
{

private static readonly string _validPath = Path.Combine(".", "_TestData", "ValidatorTests", "ValidYaml") +
Expand All @@ -19,8 +19,7 @@ public class ValidatorTest

public ValidatorTest()
{
var validatorFactory = new ValidatorFactory();
_yamlValidator = validatorFactory.GetValidator();
_yamlValidator = ValidatorFactory.CreateValidator();
}

[TestMethod]
Expand Down
6 changes: 6 additions & 0 deletions src/Persistence/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.PowerPlatform.PowerApps.Persistence.MsApp;
using Microsoft.PowerPlatform.PowerApps.Persistence.Templates;
using Microsoft.PowerPlatform.PowerApps.Persistence.Yaml;
using Microsoft.PowerPlatform.PowerApps.Persistence.YamlValidator;

namespace Microsoft.PowerPlatform.PowerApps.Persistence.Extensions;

Expand Down Expand Up @@ -35,6 +36,11 @@ public static void AddPowerAppsPersistence(this IServiceCollection services, boo
});
}

public static void AddPowerAppsPersistenceYamlValidator(this IServiceCollection services)
{
services.AddSingleton<IValidatorFactory, ValidatorFactory>();
}

private static void AddMinimalTemplates(ControlTemplateStore store)
{
store.Add(new() { Name = "hostControl", DisplayName = "host", Id = BuiltInTemplates.Host.Id });
Expand Down
6 changes: 6 additions & 0 deletions src/Persistence/InternalsVisibleTo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Persistence.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b5fc90e7027f67871e773a8fde8938c81dd402ba65b9201d60593e96c492651e889cc13f1415ebb53fac1131ae0bd333c5ee6021672d9718ea31a8aebd0da0072f25d87dba6fc90ffd598ed4da35e44c398c454307e8e33b8426143daec9f596836f97c8f74750e5975c64e2189f45def46b2a2b1247adc3652bf5c308055da9")]
2 changes: 1 addition & 1 deletion src/Persistence/YamlValidator/IValidatorFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ namespace Microsoft.PowerPlatform.PowerApps.Persistence.YamlValidator;

public interface IValidatorFactory
{
IValidator GetValidator();
IValidator CreateValidator();
}
5 changes: 2 additions & 3 deletions src/Persistence/YamlValidator/SchemaLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ internal class SchemaLoader
private const string _schemaFolderPath = "schema";
private const string _subschemaFolderPath = "subschemas";

[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1822:Mark members as static",
Justification = "Suppress to make classes stateless")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "Suppress to make classes stateless")]
public JsonSchema Load()
{
var assembly = typeof(SchemaLoader).Assembly;
Expand All @@ -21,7 +20,7 @@ public JsonSchema Load()
foreach (var file in assembly.GetManifestResourceNames())
{
var fileStream = assembly.GetManifestResourceStream(file);
var assemblyName = Assembly.GetExecutingAssembly().GetName().Name;
var assemblyName = assembly.GetName().Name;
if (fileStream == null)
{
throw new IOException($"Resource {file} could not found in assembly {assemblyName}");
Expand Down
7 changes: 1 addition & 6 deletions src/Persistence/YamlValidator/Validator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using Json.Schema;
using Yaml2JsonNode;
using System.Text.Json;
using YamlDotNet.Core;
using YamlDotNet.RepresentationModel;

Expand All @@ -12,15 +11,11 @@ namespace Microsoft.PowerPlatform.PowerApps.Persistence.YamlValidator;
internal class Validator : IValidator
{
private readonly EvaluationOptions _verbosityOptions;
[System.Diagnostics.CodeAnalysis.SuppressMessage("CodeQuality", "IDE0052:Remove unread private member",
Justification = "Suppress serializing the raw validator errors into json will be useful for future use")]
private readonly JsonSerializerOptions _serializerOptions;
private readonly JsonSchema _schema;
public Validator(EvaluationOptions options, JsonSerializerOptions resultSerializeOptions, JsonSchema schema)
public Validator(EvaluationOptions options, JsonSchema schema)
{
// to do: add verbosity flag and allow users to choose verbosity of evaluation
_verbosityOptions = options;
_serializerOptions = resultSerializeOptions;
_schema = schema;
}
public ValidatorResults Validate(string yamlFileData)
Expand Down
12 changes: 2 additions & 10 deletions src/Persistence/YamlValidator/ValidatorFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
// Licensed under the MIT License.

using Json.Schema;
using System.Text.Json;

namespace Microsoft.PowerPlatform.PowerApps.Persistence.YamlValidator;

internal class ValidatorFactory : IValidatorFactory
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1822:Mark members as static",
Justification = "Suppress to make classes stateless")]
public IValidator GetValidator()
public IValidator CreateValidator()
{
// register schema in from memory into global schema registry
var schemaLoader = new SchemaLoader();
Expand All @@ -21,13 +20,6 @@ public IValidator GetValidator()
OutputFormat = OutputFormat.List
};

// pass in serailization options for validator results object to json
// This is unused for now but can be useful for producing raw json validation results which can be consumed elsewhere
var resultSerializeOptions = new JsonSerializerOptions
{
Converters = { new EvaluationResultsJsonConverter() }
};

return new Validator(evalOptions, resultSerializeOptions, serializedSchema);
return new Validator(evalOptions, serializedSchema);
}
}

0 comments on commit 32c1de6

Please sign in to comment.