Skip to content

Commit

Permalink
make try-scope larger when fetching optionList and catch additional e…
Browse files Browse the repository at this point in the history
…xception
  • Loading branch information
standeren committed Jan 7, 2025
1 parent 1bd192d commit c5e7f41
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text.Json;
Expand Down Expand Up @@ -51,14 +52,15 @@ public async Task<List<Option>> GetOptionsList(string org, string repo, string d
cancellationToken.ThrowIfCancellationRequested();
var altinnAppGitRepository = _altinnGitRepositoryFactory.GetAltinnAppGitRepository(org, repo, developer);

string optionsListString = await altinnAppGitRepository.GetOptionsList(optionsListId, cancellationToken);
var optionsList = JsonSerializer.Deserialize<List<Option>>(optionsListString);
List<Option> optionsList;

string optionsListString = await altinnAppGitRepository.GetOptionsList(optionsListId, cancellationToken);
try
{
optionsList = JsonSerializer.Deserialize<List<Option>>(optionsListString);
optionsList.ForEach(ValidateOption);
}
catch (ValidationException)
catch (Exception ex) when (ex is ValidationException || ex is JsonException)
{
throw new InvalidOptionsFormatException($"One or more of the options have an invalid format in option list: {optionsListId}.");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using Altinn.Studio.Designer.Filters;
using Altinn.Studio.Designer.Models;
using Altinn.Studio.Designer.Models.Dto;
using Designer.Tests.Controllers.ApiTests;
using Designer.Tests.Utils;
using FluentAssertions;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
Expand Down Expand Up @@ -63,8 +65,24 @@ public async Task GetOptionLists_Returns200OK_WithOptionListsData()
{
// Arrange
const string repo = "app-with-options";
string apiUrl = $"/designer/api/ttd/{repo}/options/option-lists";
string targetRepository = TestDataHelper.GenerateTestRepoName();
await CopyRepositoryForTest("ttd", repo, "testUser", targetRepository);
string apiUrl = $"/designer/api/ttd/{targetRepository}/options/option-lists";
using HttpRequestMessage httpRequestMessage = new(HttpMethod.Get, apiUrl);
string optionListMissingValue = @"[{ ""label"": ""someLabel""}]";
string optionListMissingLabel = @"[{ ""value"": ""someValue""}]";
string optionListTrailingComma = @"[{ ""value"": ""someValue"", ""label"": ""someLabel"",}]";
string optionListLabelWithObject = @"[{ ""value"": ""someValue"", ""label"": {}}]";
string optionListLabelWithNumber = @"[{ ""value"": ""someValue"", ""label"": 12345}]";
string optionListLabelWithBool = @"[{ ""value"": ""someValue"", ""label"": true}]";
string repoPath = TestDataHelper.GetTestDataRepositoryDirectory("ttd", targetRepository, "testUser");
string filePath = Path.Combine(repoPath, "App/options");
await File.WriteAllTextAsync(Path.Combine(filePath, "optionListMissingValue.json"), optionListMissingValue);
await File.WriteAllTextAsync(Path.Combine(filePath, "optionListMissingLabel.json"), optionListMissingLabel);
await File.WriteAllTextAsync(Path.Combine(filePath, "optionListTrailingComma.json"), optionListTrailingComma);
await File.WriteAllTextAsync(Path.Combine(filePath, "optionListLabelWithObject.json"), optionListLabelWithObject);
await File.WriteAllTextAsync(Path.Combine(filePath, "optionListLabelWithNumber.json"), optionListLabelWithNumber);
await File.WriteAllTextAsync(Path.Combine(filePath, "optionListLabelWithBool.json"), optionListLabelWithBool);

// Act
using HttpResponseMessage response = await HttpClient.SendAsync(httpRequestMessage);
Expand All @@ -77,7 +95,13 @@ public async Task GetOptionLists_Returns200OK_WithOptionListsData()
{
new () { Title = "options-with-null-fields", Data = null, HasError = true },
new () { Title = "other-options", HasError = false },
new () { Title = "test-options", HasError = false }
new () { Title = "test-options", HasError = false },
new () { Title = "optionListMissingValue", Data = null, HasError = true },
new () { Title = "optionListMissingLabel", Data = null, HasError = true },
new () { Title = "optionListTrailingComma", Data = null, HasError = true },
new () { Title = "optionListLabelWithObject", Data = null, HasError = true },
new () { Title = "optionListLabelWithNumber", Data = null, HasError = true },
new () { Title = "optionListLabelWithBool", Data = null, HasError = true }
}, options => options.Excluding(x => x.Data));
}

Expand Down

0 comments on commit c5e7f41

Please sign in to comment.