Skip to content

Commit

Permalink
fix endpoint and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mirkoSekulic committed Feb 5, 2024
1 parent a146799 commit 25ada23
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 63 deletions.
17 changes: 11 additions & 6 deletions backend/src/Designer/Controllers/ProcessModelingController.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mime;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Altinn.Studio.Designer.Events;
Expand Down Expand Up @@ -67,12 +67,17 @@ await _processModelingService.SaveProcessDefinitionAsync(
return Ok();
}

[HttpPut("process-definition-latest")]
public async Task<IActionResult> UpsertProcessDefinition(string org, string repo, [FromForm] ProcessDefinitionDto processDefinitionDto, CancellationToken cancellationToken)
[HttpPost("process-definition-latest")]
public async Task<IActionResult> UpsertProcessDefinitionAndNotify(string org, string repo, [FromForm] IFormFile content, [FromForm] string metadata, CancellationToken cancellationToken)
{
Request.EnableBuffering();

Stream stream = processDefinitionDto.Content.OpenReadStream();
var metadataObject = metadata is not null
? JsonSerializer.Deserialize<ProcessDefinitionMetadata>(metadata,
new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase })
: null;

Stream stream = content.OpenReadStream();

try
{
Expand All @@ -87,9 +92,9 @@ public async Task<IActionResult> UpsertProcessDefinition(string org, string repo
await _processModelingService.SaveProcessDefinitionAsync(
AltinnRepoEditingContext.FromOrgRepoDeveloper(org, repo, developer), stream, cancellationToken);

if (processDefinitionDto.ProcessDefinitionMetadata?.TaskIdChanges is not null)
if (metadataObject?.TaskIdChanges is not null)
{
foreach (TaskIdChange taskIdChange in processDefinitionDto.ProcessDefinitionMetadata.TaskIdChanges)
foreach (TaskIdChange taskIdChange in metadataObject.TaskIdChanges)
{
await _mediator.Publish(new ProcessTaskIdChangedEvent
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public class ProcessTaskIdChangedApplicationMetadataHandler : INotificationHandl
{
public Task Handle(ProcessTaskIdChangedEvent notification, CancellationToken cancellationToken)
{
// TODO: Implement logic to handle the event here: https://github.com/Altinn/altinn-studio/issues/12220
// Here we should think how to handle errors in the handlers. Should we throw exceptions or use websocket to send error messages to the client?
return Task.CompletedTask;
}
}
10 changes: 0 additions & 10 deletions backend/src/Designer/Models/Dto/ProcessDefinitionDto.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Mime;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Xml.Linq;
using Altinn.Studio.Designer.Models.Dto;
using Designer.Tests.Controllers.ApiTests;
using Designer.Tests.Utils;
using FluentAssertions;
using Microsoft.AspNetCore.Mvc.Testing;
using SharedResources.Tests;
using Xunit;

namespace Designer.Tests.Controllers.ProcessModelingController;

public class UpsertProcessDefinitionAndNotifyTests : DisagnerEndpointsTestsBase<UpsertProcessDefinitionAndNotifyTests>, IClassFixture<WebApplicationFactory<Program>>
{
private static string VersionPrefix(string org, string repository) => $"/designer/api/{org}/{repository}/process-modelling/process-definition-latest";

public UpsertProcessDefinitionAndNotifyTests(WebApplicationFactory<Program> factory) : base(factory)
{
}

[Theory]
[MemberData(nameof(UpsertProcessDefinitionAndNotifyTestData))]
public async Task UpsertProcessDefinition_ShouldReturnOk(string org, string app, string developer, string bpmnFilePath, ProcessDefinitionMetadata metadata)
{
string targetRepository = TestDataHelper.GenerateTestRepoName();
await CopyRepositoryForTest(org, app, developer, targetRepository);
string fileContent = SharedResourcesHelper.LoadTestDataAsString(bpmnFilePath);
fileContent = metadata.TaskIdChanges.Aggregate(fileContent, (current, metadataTaskIdChange) => current.Replace(metadataTaskIdChange.OldId, metadataTaskIdChange.NewId));
var fileStream = GenerateStreamFromString(fileContent);

string url = VersionPrefix(org, targetRepository);

var form = new MultipartFormDataContent();

Check warning

Code scanning / CodeQL

Missing Dispose call on local IDisposable Warning test

Disposable 'MultipartFormDataContent' is created but not disposed.
string metadataString = JsonSerializer.Serialize(metadata,
new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
form.Add(new StreamContent(fileStream), "content", "process.bpmn");
form.Add(new StringContent(metadataString, Encoding.UTF8, MediaTypeNames.Application.Json), "metadata");

using var response = await HttpClient.PostAsync(url, form);
response.StatusCode.Should().Be(HttpStatusCode.OK);

string savedFile = TestDataHelper.GetFileFromRepo(org, targetRepository, developer, "App/config/process/process.bpmn");

XDocument expectedXml = XDocument.Parse(fileContent);
XDocument savedXml = XDocument.Parse(savedFile);
XNode.DeepEquals(savedXml, expectedXml).Should().BeTrue();
}

private static Stream GenerateStreamFromString(string s)
{
var stream = new MemoryStream();
var writer = new StreamWriter(stream);

Check warning

Code scanning / CodeQL

Missing Dispose call on local IDisposable Warning test

Disposable 'StreamWriter' is created but not disposed.
writer.Write(s);
writer.Flush();
stream.Position = 0;
return stream;
}

public static IEnumerable<object[]> UpsertProcessDefinitionAndNotifyTestData()
{
yield return new object[] { "ttd", "empty-app", "testUser", "App/config/process/process.bpmn",
new ProcessDefinitionMetadata
{
TaskIdChanges = new List<TaskIdChange> { new() { OldId = "Task_1", NewId = "SomeNewId" } }
} };
}
}
124 changes: 77 additions & 47 deletions testdata/App/config/process/process.bpmn
Original file line number Diff line number Diff line change
@@ -1,48 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions id="Altinn_SingleDataTask_Process_Definition"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:altinn="http://altinn.no"
xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL"
xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI"
xmlns:dc="http://www.omg.org/spec/DD/20100524/DC"
xmlns:di="http://www.omg.org/spec/DD/20100524/DI" targetNamespace="http://bpmn.io/schema/bpmn">
<bpmn:process id="SingleDataTask" isExecutable="false">
<bpmn:startEvent id="StartEvent_1" name="Start">
<bpmn:outgoing>To_Task_1</bpmn:outgoing>
</bpmn:startEvent>
<bpmn:task id="Task_1" name="Utfylling (message)" altinn:tasktype="data">
<bpmn:incoming>To_Task_1</bpmn:incoming>
<bpmn:outgoing>To_Task_2</bpmn:outgoing>
</bpmn:task>
<bpmn:task id="Task_2" name="Utfylling (changename)" altinn:tasktype="data">
<bpmn:incoming>To_Task_2</bpmn:incoming>
<bpmn:outgoing>To_Task_3</bpmn:outgoing>
</bpmn:task>
<bpmn:task id="Task_3" name="Utfylling (group)" altinn:tasktype="data">
<bpmn:incoming>To_Task_3</bpmn:incoming>
<bpmn:outgoing>To_Task_4</bpmn:outgoing>
</bpmn:task>
<bpmn:task id="Task_4" name="Utfylling (likert)" altinn:tasktype="data">
<bpmn:incoming>To_Task_4</bpmn:incoming>
<bpmn:outgoing>To_Task_5</bpmn:outgoing>
</bpmn:task>
<bpmn:task id="Task_5" name="Utfylling (datalist)" altinn:tasktype="data">
<bpmn:incoming>To_Task_5</bpmn:incoming>
<bpmn:outgoing>To_Task_6</bpmn:outgoing>
</bpmn:task>
<bpmn:task id="Task_6" name="Tilbakemelding" altinn:tasktype="confirmation">
<bpmn:incoming>To_Task_6</bpmn:incoming>
<bpmn:outgoing>To_End</bpmn:outgoing>
</bpmn:task>
<bpmn:endEvent id="EndEvent_1" name="End">
<bpmn:incoming>To_End</bpmn:incoming>
</bpmn:endEvent>
<bpmn:sequenceFlow id="To_Task_1" sourceRef="StartEvent_1" targetRef="Task_1" />
<bpmn:sequenceFlow id="To_Task_2" sourceRef="Task_1" targetRef="Task_2" />
<bpmn:sequenceFlow id="To_Task_3" sourceRef="Task_2" targetRef="Task_3" />
<bpmn:sequenceFlow id="To_Task_4" sourceRef="Task_3" targetRef="Task_4" />
<bpmn:sequenceFlow id="To_Task_5" sourceRef="Task_4" targetRef="Task_5" />
<bpmn:sequenceFlow id="To_Task_6" sourceRef="Task_5" targetRef="Task_6" />
<bpmn:sequenceFlow id="To_End" sourceRef="Task_6" targetRef="EndEvent_1" />
</bpmn:process>
<?xml version="1.0" encoding="utf-8"?>
<bpmn:definitions id="Altinn_SingleDataTask_Process_Definition" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:altinn="http://altinn.no/process" xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" targetNamespace="http://bpmn.io/schema/bpmn">
<bpmn:process id="SingleDataTask" isExecutable="false">
<bpmn:startEvent id="StartEvent_1" name="Start">
<bpmn:outgoing>To_Task_1</bpmn:outgoing>
</bpmn:startEvent>
<bpmn:task id="Task_1" name="Utfylling (message)">
<bpmn:incoming>To_Task_1</bpmn:incoming>
<bpmn:outgoing>To_Task_2</bpmn:outgoing>
<bpmn:extensionElements>
<altinn:taskExtension>
<altinn:taskType>data</altinn:taskType>
</altinn:taskExtension>
</bpmn:extensionElements>
</bpmn:task>
<bpmn:task id="Task_2" name="Utfylling (changename)">
<bpmn:incoming>To_Task_2</bpmn:incoming>
<bpmn:outgoing>To_Task_3</bpmn:outgoing>
<bpmn:extensionElements>
<altinn:taskExtension>
<altinn:taskType>data</altinn:taskType>
<altinn:actions>
<altinn:action type="serverAction">fill</altinn:action>
</altinn:actions>
</altinn:taskExtension>
</bpmn:extensionElements>
</bpmn:task>
<bpmn:task id="Task_3" name="Utfylling (group)">
<bpmn:incoming>To_Task_3</bpmn:incoming>
<bpmn:outgoing>To_Task_4</bpmn:outgoing>
<bpmn:extensionElements>
<altinn:taskExtension>
<altinn:taskType>data</altinn:taskType>
</altinn:taskExtension>
</bpmn:extensionElements>
</bpmn:task>
<bpmn:task id="Task_4" name="Utfylling (likert)">
<bpmn:incoming>To_Task_4</bpmn:incoming>
<bpmn:outgoing>To_Task_5</bpmn:outgoing>
<bpmn:extensionElements>
<altinn:taskExtension>
<altinn:taskType>data</altinn:taskType>
</altinn:taskExtension>
</bpmn:extensionElements>
</bpmn:task>
<bpmn:task id="Task_5" name="Utfylling (datalist)">
<bpmn:incoming>To_Task_5</bpmn:incoming>
<bpmn:outgoing>To_Task_6</bpmn:outgoing>
<bpmn:extensionElements>
<altinn:taskExtension>
<altinn:taskType>data</altinn:taskType>
</altinn:taskExtension>
</bpmn:extensionElements>
</bpmn:task>
<bpmn:task id="Task_6" name="Tilbakemelding">
<bpmn:incoming>To_Task_6</bpmn:incoming>
<bpmn:outgoing>To_End</bpmn:outgoing>
<bpmn:extensionElements>
<altinn:taskExtension>
<altinn:taskType>confirmation</altinn:taskType>
<altinn:actions>
<altinn:action>confirm</altinn:action>
</altinn:actions>
</altinn:taskExtension>
</bpmn:extensionElements>
</bpmn:task>
<bpmn:endEvent id="EndEvent_1" name="End">
<bpmn:incoming>To_End</bpmn:incoming>
</bpmn:endEvent>
<bpmn:sequenceFlow id="To_Task_1" sourceRef="StartEvent_1" targetRef="Task_1" />
<bpmn:sequenceFlow id="To_Task_2" sourceRef="Task_1" targetRef="Task_2" />
<bpmn:sequenceFlow id="To_Task_3" sourceRef="Task_2" targetRef="Task_3" />
<bpmn:sequenceFlow id="To_Task_4" sourceRef="Task_3" targetRef="Task_4" />
<bpmn:sequenceFlow id="To_Task_5" sourceRef="Task_4" targetRef="Task_5" />
<bpmn:sequenceFlow id="To_Task_6" sourceRef="Task_5" targetRef="Task_6" />
<bpmn:sequenceFlow id="To_End" sourceRef="Task_6" targetRef="EndEvent_1" />
</bpmn:process>
</bpmn:definitions>

0 comments on commit 25ada23

Please sign in to comment.