Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
standeren committed Mar 4, 2024
1 parent d8d972b commit 347660a
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 18 deletions.
25 changes: 23 additions & 2 deletions backend/src/DataModeling/Metamodel/ElementMetadata.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

Expand Down Expand Up @@ -29,123 +30,143 @@ public ElementMetadata()
/// Gets or sets the ID of the parent element. If a
/// </summary>
[JsonProperty(PropertyName = "parentElement")]
[JsonPropertyName("parentElement")]
public string ParentElement { get; set; }

/// <summary>
/// Gets or sets the type of this element
/// </summary>
[JsonProperty(PropertyName = "typeName")]
[JsonPropertyName("typeName")]
public string TypeName { get; set; }

/// <summary>
/// Gets or sets the element name. The Name should not contain '-'
/// </summary>
[JsonProperty(PropertyName = "name")]
[JsonPropertyName("name")]
public string Name { get; set; }

/// <summary>
/// Gets or sets the data binding name for the element. If an DataBindingName is null it cannot be used for a data-field.
/// It is used by the frontend to filter elements that can be used in single-field ux-components.
/// </summary>
[JsonProperty(PropertyName = "dataBindingName")]
[JsonPropertyName("dataBindingName")]
public string DataBindingName { get; set; }

/// <summary>
/// Gets or sets the element xpath
/// </summary>
[JsonProperty(PropertyName = "xPath")]
[JsonPropertyName("xPath")]
public string XPath { get; set; }

/// <summary>
/// Gets or sets the restrictions for this element (<see cref="Restriction"/>)
/// </summary>
[JsonProperty(PropertyName = "restrictions")]
[JsonPropertyName("restrictions")]
public Dictionary<string, Restriction> Restrictions { get; set; }

/// <summary>
/// Gets or sets the <see cref="ElementType"/>
/// </summary>
[JsonConverter(typeof(StringEnumConverter))]
[Newtonsoft.Json.JsonConverter(typeof(StringEnumConverter))]
[JsonProperty(PropertyName = "type")]
[JsonPropertyName("type")]
public ElementType Type { get; set; }

/// <summary>
/// Gets or sets the <see cref="BaseValueType">xsd value type</see> of this element
/// </summary>
[JsonConverter(typeof(StringEnumConverter))]
[Newtonsoft.Json.JsonConverter(typeof(StringEnumConverter))]
[JsonProperty(PropertyName = "xsdValueType")]
[JsonPropertyName("xsdValueType")]
public BaseValueType? XsdValueType { get; set; }

/// <summary>
/// Gets or sets any text associated with this element
/// </summary>
[JsonProperty(PropertyName = "texts")]
[JsonPropertyName("texts")]
public Dictionary<string, string> Texts { get; set; }

/// <summary>
/// Gets or sets custom properties for this element
/// </summary>
[JsonProperty(PropertyName = "customProperties")]
[JsonPropertyName("customProperties")]
public Dictionary<string, string> CustomProperties { get; set; }

/// <summary>
/// Gets or sets the maximum number of times this element can be repeated
/// </summary>
[JsonProperty(PropertyName = "maxOccurs")]
[JsonPropertyName("maxOccurs")]
public int MaxOccurs { get; set; }

/// <summary>
/// Gets or sets the minimum number of times this element can be repeated
/// </summary>
[JsonProperty(PropertyName = "minOccurs")]
[JsonPropertyName("minOccurs")]
public int MinOccurs { get; set; }

/// <summary>
/// Gets or sets the XName for this element. The XML name.
/// </summary>
[JsonProperty(PropertyName = "xName")]
[JsonPropertyName("xName")]
public string XName { get; set; }

/// <summary>
/// Gets or sets a value indicating whether this element is the content of the parent tag. Special case of SimpleContent.
/// </summary>
[JsonProperty(PropertyName = "isTagContent")]
[JsonPropertyName("isTagContent")]
public bool IsTagContent { get; set; }

/// <summary>
/// Gets or sets the fixed value of an element
/// </summary>
[JsonProperty(PropertyName = "fixedValue")]
[JsonPropertyName("fixedValue")]
public string FixedValue { get; set; }

/// <summary>
/// Gets or sets a value indicating whether this element is read only
/// </summary>
[JsonProperty(PropertyName = "isReadOnly")]
[JsonPropertyName("isReadOnly")]
public bool IsReadOnly { get; set; }

/// <summary>
/// The reference to the Xml Schema declaration
/// </summary>
[JsonProperty(PropertyName = "xmlSchemaXPath")]
[JsonPropertyName("xmlSchemaXPath")]
public string XmlSchemaXPath { get; set; }

/// <summary>
/// Used to point to the reference to the Json Schema declaration
/// </summary>
[JsonProperty(PropertyName = "jsonSchemaPointer")]
[JsonPropertyName("jsonSchemaPointer")]
public string JsonSchemaPointer { get; set; }

/// <summary>
/// Used to display the property in a view. Contains name, cardinality and type. Example: melding.person.name: [0..1] string
/// </summary>
[JsonProperty(PropertyName = "displayString")]
[JsonPropertyName("displayString")]
public string DisplayString { get; set; }

/// <summary>
/// Used for xsd context. Indicates if nillable is set in xsd schema.
/// </summary>
[JsonProperty(PropertyName = "nillable")]
[JsonPropertyName("nillable")]
public bool? Nillable { get; set; }
}
}
2 changes: 2 additions & 0 deletions backend/src/DataModeling/Metamodel/ModelMetadata.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;
using Newtonsoft.Json;

namespace Altinn.Studio.DataModeling.Metamodel
Expand Down Expand Up @@ -41,6 +42,7 @@ public class ModelMetadata
/// Gets or sets all elements for the service (<see cref="ElementMetadata"/>)
/// </summary>
[JsonProperty(PropertyName = "elements")]
[JsonPropertyName("elements")]
public Dictionary<string, ElementMetadata> Elements { get; set; } = new Dictionary<string, ElementMetadata>();
}
}
3 changes: 1 addition & 2 deletions backend/src/Designer/Controllers/AppDevelopmentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Altinn.Studio.DataModeling.Metamodel;
using Altinn.Studio.Designer.Configuration;
using Altinn.Studio.Designer.Filters;
using Altinn.Studio.Designer.Helpers;
Expand Down Expand Up @@ -275,7 +274,7 @@ public async Task<IActionResult> GetLayoutSettings(string org, string app, [From
public async Task<IActionResult> GetModelMetadata(string org, string app, [FromQuery] string layoutSetName, CancellationToken cancellationToken)
{
string developer = AuthenticationHelper.GetDeveloperUserName(HttpContext);
ModelMetadata modelMetadata = await _appDevelopmentService.GetModelMetadata(AltinnRepoEditingContext.FromOrgRepoDeveloper(org, app, developer), layoutSetName, cancellationToken);
JsonNode modelMetadata = await _appDevelopmentService.GetModelMetadata(AltinnRepoEditingContext.FromOrgRepoDeveloper(org, app, developer), layoutSetName, cancellationToken);
return Ok(modelMetadata);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;
using Altinn.Studio.DataModeling.Metamodel;
using Altinn.Studio.Designer.Configuration;
using Altinn.Studio.Designer.Helpers;
using Altinn.Studio.Designer.Models;
Expand Down Expand Up @@ -143,7 +144,8 @@ public async Task<string> GetModelMetadata(string modelName)
string modelMetadataFileName = GetPathToModelMetadata(modelName);
if (!FileExistsByRelativePath(modelMetadataFileName))
{
return "{}";
ModelMetadata emptyModel = JsonSerializer.Deserialize<ModelMetadata>("{}");
return JsonSerializer.Serialize(emptyModel);
}
return await ReadTextByRelativePathAsync(modelMetadataFileName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
using System.Threading;
using System.Threading.Tasks;
using Altinn.App.Core.Models;
using Altinn.Studio.DataModeling.Metamodel;
using Altinn.Studio.Designer.Helpers;
using Altinn.Studio.Designer.Infrastructure.GitRepository;
using Altinn.Studio.Designer.Models;
using Altinn.Studio.Designer.Services.Interfaces;
using JetBrains.Annotations;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using NuGet.Versioning;
using LayoutSets = Altinn.Studio.Designer.Models.LayoutSets;
using PlatformStorageModels = Altinn.Platform.Storage.Interface.Models;
Expand Down Expand Up @@ -149,7 +147,8 @@ public async Task SaveLayoutSettings(AltinnRepoEditingContext altinnRepoEditingC
}

/// <inheritdoc />
public async Task<ModelMetadata> GetModelMetadata(AltinnRepoEditingContext altinnRepoEditingContext, string layoutSetName, CancellationToken cancellationToken = default)
public async Task<JsonNode> GetModelMetadata(AltinnRepoEditingContext altinnRepoEditingContext,
string layoutSetName, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
AltinnAppGitRepository altinnAppGitRepository =
Expand All @@ -161,7 +160,7 @@ public async Task<ModelMetadata> GetModelMetadata(AltinnRepoEditingContext altin
string taskId = await GetTaskIdBasedOnLayoutSet(altinnRepoEditingContext, layoutSetName, cancellationToken);
string modelName = GetModelName(applicationMetadata, taskId);
string fileContent = await altinnAppGitRepository.GetModelMetadata(modelName);
return JsonConvert.DeserializeObject<ModelMetadata>(fileContent);
return JsonNode.Parse(fileContent);
}

private string GetModelName(ApplicationMetadata applicationMetadata, [CanBeNull] string taskId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ public interface IAppDevelopmentService
/// <param name="layoutSetName">Name of current layoutSet in ux-editor that edited layout belongs to</param>
/// <param name="cancellationToken">An <see cref="CancellationToken"/> that observes if operation is cancelled.</param>
/// <returns>The service metadata for an app.</returns>
public Task<ModelMetadata> GetModelMetadata(AltinnRepoEditingContext altinnRepoEditingContext, [CanBeNull] string layoutSetName, CancellationToken cancellationToken = default);
public Task<JsonNode> GetModelMetadata(
AltinnRepoEditingContext altinnRepoEditingContext, [CanBeNull] string layoutSetName,
CancellationToken cancellationToken = default);


/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,11 @@ public async Task GetModelMetadata_Should_Return_Empty_Model_When_No_ModelMetada

using var response = await HttpClient.SendAsync(httpRequestMessage);
response.StatusCode.Should().Be(HttpStatusCode.OK);

string responseContent = await response.Content.ReadAsStringAsync();
string responseContentLowerCase = responseContent.ToLowerInvariant();
string expectedResposeContentLowerCase = JsonConvert
.SerializeObject(JsonConvert.DeserializeObject<ModelMetadata>("{}")).ToLowerInvariant();
responseContentLowerCase.Should().Be(expectedResposeContentLowerCase);
string expectedResponse = JsonConvert
.SerializeObject(JsonConvert.DeserializeObject<ModelMetadata>("{}"));
responseContent.Should().Be(expectedResponse);
}

private async Task<string> AddModelMetadataToRepo(string createdFolderPath, string expectedModelMetadataPath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const renderExpressionContent = (props: Partial<ExpressionContentProps> = {}) =>

const queryClient = createQueryClientMock();
queryClient.setQueryData([QueryKey.FormLayouts, org, app, layoutSetName], layouts);
queryClient.setQueryData([QueryKey.DatamodelMetadata, org, app], []);
queryClient.setQueryData([QueryKey.DatamodelMetadata, org, app, layoutSetName], []);

return renderWithProviders(<ExpressionContent {...defaultProps} {...props} />, {
appContextProps,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const ExpressionContent = ({
const { org, app } = useStudioUrlParams();
const { selectedLayoutSet } = useAppContext();
const { data: formLayoutsData } = useFormLayoutsQuery(org, app, selectedLayoutSet);
const { data: datamodelMetadata } = useDatamodelMetadataQuery(org, app);
const { data: datamodelMetadata } = useDatamodelMetadataQuery(org, app, selectedLayoutSet);

const dataLookupOptions: DataLookupOptions = useMemo(
() => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ const renderExpressions = (formItemContext: Partial<FormItemContext> = {}) => {

const queryClient = createQueryClientMock();
queryClient.setQueryData([QueryKey.FormLayouts, org, app, layoutSetName], layouts);
queryClient.setQueryData([QueryKey.DatamodelMetadata, org, app], []);
queryClient.setQueryData([QueryKey.DatamodelMetadata, org, app, layoutSetName], []);

return renderWithProviders(
<FormItemContext.Provider value={{ ...defaultFormItemContext, ...formItemContext }}>
Expand Down

0 comments on commit 347660a

Please sign in to comment.