Skip to content

Commit

Permalink
use inmemory cache for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mirkoSekulic committed Nov 11, 2024
1 parent 868441a commit 907f98f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ public async Task Get_ApplicationMetadata_Ok()
Assert.Equal(HttpStatusCode.OK, response.StatusCode);

string responseBody = await response.Content.ReadAsStringAsync();
ApplicationMetadata expectedApplicationMetadata = JsonSerializer.Deserialize<ApplicationMetadata>(expectedApplicationMetadataString, SerializerOptions);
ApplicationMetadata expectedApplicationMetadata = JsonSerializer.Deserialize<ApplicationMetadata>(expectedApplicationMetadataString, JsonSerializerOptions);
expectedApplicationMetadata.AltinnNugetVersion = string.Empty;
string expectedJson = JsonSerializer.Serialize(expectedApplicationMetadata, SerializerOptions);
string expectedJson = JsonSerializer.Serialize(expectedApplicationMetadata, JsonSerializerOptions);
JsonUtils.DeepEquals(expectedJson, responseBody).Should().BeTrue();
}

Expand All @@ -72,9 +72,9 @@ public async Task Get_ApplicationMetadata_With_V8_Altinn_Nuget_Version_Ok()
Assert.Equal(HttpStatusCode.OK, response.StatusCode);

string responseBody = await response.Content.ReadAsStringAsync();
ApplicationMetadata expectedApplicationMetadata = JsonSerializer.Deserialize<ApplicationMetadata>(expectedApplicationMetadataString, SerializerOptions);
ApplicationMetadata expectedApplicationMetadata = JsonSerializer.Deserialize<ApplicationMetadata>(expectedApplicationMetadataString, JsonSerializerOptions);
expectedApplicationMetadata.AltinnNugetVersion = "8.0.0.0";
string expectedJson = JsonSerializer.Serialize(expectedApplicationMetadata, SerializerOptions);
string expectedJson = JsonSerializer.Serialize(expectedApplicationMetadata, JsonSerializerOptions);
JsonUtils.DeepEquals(expectedJson, responseBody).Should().BeTrue();
}

Expand All @@ -93,7 +93,7 @@ public async Task Get_ApplicationMetadata_WithLessDataTypesThanLayoutSetsFile_Ok
Assert.Equal(HttpStatusCode.OK, response.StatusCode);

string responseBody = await response.Content.ReadAsStringAsync();
ApplicationMetadata expectedApplicationMetadata = JsonSerializer.Deserialize<ApplicationMetadata>(expectedApplicationMetadataString, SerializerOptions);
ApplicationMetadata expectedApplicationMetadata = JsonSerializer.Deserialize<ApplicationMetadata>(expectedApplicationMetadataString, JsonSerializerOptions);
expectedApplicationMetadata.AltinnNugetVersion = "8.0.0.0";
// Add the mocked data type to expected app metadata
expectedApplicationMetadata.DataTypes.Add(new DataType()
Expand All @@ -110,15 +110,15 @@ public async Task Get_ApplicationMetadata_WithLessDataTypesThanLayoutSetsFile_Ok
expectedApplicationMetadata.PartyTypesAllowed.SubUnit = false;
expectedApplicationMetadata.PartyTypesAllowed.BankruptcyEstate = false;

string expectedJson = JsonSerializer.Serialize(expectedApplicationMetadata, SerializerOptions);
string expectedJson = JsonSerializer.Serialize(expectedApplicationMetadata, JsonSerializerOptions);
JsonUtils.DeepEquals(expectedJson, responseBody).Should().BeTrue();
}

[Fact]
public async Task Get_ApplicationMetadata_WithAllPartyTypesAllowedSetToFalse()
{
string originalApplicationMetadataString = TestDataHelper.GetFileFromRepo(Org, AppV4, Developer, "App/config/applicationmetadata.json");
ApplicationMetadata originalApplicationMetadata = JsonSerializer.Deserialize<ApplicationMetadata>(originalApplicationMetadataString, SerializerOptions);
ApplicationMetadata originalApplicationMetadata = JsonSerializer.Deserialize<ApplicationMetadata>(originalApplicationMetadataString, JsonSerializerOptions);
originalApplicationMetadata.PartyTypesAllowed.Person.Should().BeTrue();
originalApplicationMetadata.PartyTypesAllowed.Organisation.Should().BeTrue();
originalApplicationMetadata.PartyTypesAllowed.SubUnit.Should().BeTrue();
Expand All @@ -137,7 +137,7 @@ public async Task Get_ApplicationMetadata_WithAllPartyTypesAllowedSetToFalse()

string responseBody = await response.Content.ReadAsStringAsync();

ApplicationMetadata responseApplicationMetadata = JsonSerializer.Deserialize<ApplicationMetadata>(responseBody, SerializerOptions);
ApplicationMetadata responseApplicationMetadata = JsonSerializer.Deserialize<ApplicationMetadata>(responseBody, JsonSerializerOptions);
responseApplicationMetadata.PartyTypesAllowed.Person.Should().BeFalse();
responseApplicationMetadata.PartyTypesAllowed.Organisation.Should().BeFalse();
responseApplicationMetadata.PartyTypesAllowed.SubUnit.Should().BeFalse();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System.Text.Encodings.Web;
using System.Linq;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Serialization;
using Designer.Tests.Controllers.ApiTests;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.DependencyInjection;

namespace Designer.Tests.Controllers.PreviewController
{
Expand All @@ -22,12 +25,19 @@ public class PreviewControllerTestsBase<TTestClass> : DesignerEndpointsTestsBase
protected const string InstanceGuId = "f1e23d45-6789-1bcd-8c34-56789abcdef0";
protected const string AttachmentGuId = "f47ac10b-58cc-4372-a567-0e02b2c3d479";
protected const string MockedReferrerUrl = "https://studio-mock-url.no";
protected readonly JsonSerializerOptions SerializerOptions = new JsonSerializerOptions()

protected override void ConfigureTestServices(IServiceCollection services)
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
};
base.ConfigureTestServices(services);
var cacheServices = services.Where(
d => d.ServiceType == typeof(IDistributedCache)).ToList();
foreach (ServiceDescriptor serviceDescriptor in cacheServices)
{
services.Remove(serviceDescriptor);
}

services.AddDistributedMemoryCache();
}

public PreviewControllerTestsBase(WebApplicationFactory<Program> factory) : base(factory)
{
Expand Down

0 comments on commit 907f98f

Please sign in to comment.