Skip to content

Commit

Permalink
Rewrite of process engine to support actions in tasks resolves #205 and
Browse files Browse the repository at this point in the history
#207 (#237)

* New process engine seems to work.
Needs more tests and verification

* refactored to make class more testable

* added tests for ProcessEngine

* Refactor and delete old and unused code

* added tests for ProcessEventDispatcher

* Add tests and fix ProcessNavigator

* add available actions to currentTask and perform authcheck

* action passed along from PUT process/next to gateway filters

* fix bug in AppProcessState ctor

* add fields for read/write and check users permissions

* Fix test stub implementation of IProcessExclusiveGateway

* Fixing some reported code smells

* Some code smell fixes.
Added logic to dispatch abandon event if action is reject

* remove unfinished test file

* add tests for method in ProcessClient

* add test for classes extending storage classes

* add test for null values in extensions

* revert code changes due to test

* add frontend feature and parse request body on process/next if present

* add frontend feature and parse request body on process/next if present

* fix codeql warning

* add v8 as target of github workflows in addition to main

* Fix return type of all methods in ProcessController returning ProcessState

* Authorize action moved to AuthorizationClient
TaskType is substituted with corresponding action earlier
Resolvs #207

* Fixed some issues after review and added some more tests

* fix codeQL warning and improve test

* Fix some code smells

* Update src/Altinn.App.Api/Controllers/InstancesController.cs

Co-authored-by: Ronny Birkeli <ronny.birkeli@gmail.com>

* fix build error

---------

Co-authored-by: Ronny Birkeli <ronny.birkeli@gmail.com>
  • Loading branch information
tjololo and RonnyB71 authored May 15, 2023
1 parent 588555a commit 6ab302e
Show file tree
Hide file tree
Showing 72 changed files with 3,809 additions and 1,716 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ name: "CodeQL"

on:
push:
branches: [ "main" ]
branches: [ "main", "v8" ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ "main" ]
branches: [ "main", "v8" ]
schedule:
- cron: '37 20 * * 3'

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/dotnet-test.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: Build and Test on windows, macos and ubuntu
on:
push:
branches: [ main ]
branches: [ main, v8 ]
pull_request:
branches: [ main ]
branches: [ main, v8 ]
types: [opened, synchronize, reopened]
workflow_dispatch:
jobs:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test-and-analyze-fork.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Code test and analysis (fork)
on:
pull_request:
branches: [ main ]
branches: [ main, v8 ]
types: [opened, synchronize, reopened, ready_for_review]
jobs:
test:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test-and-analyze.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: Code test and analysis
on:
push:
branches: [ main ]
branches: [ main, v8 ]
pull_request:
branches: [ main ]
branches: [ main, v8 ]
types: [opened, synchronize, reopened]
workflow_dispatch:
jobs:
Expand Down
87 changes: 60 additions & 27 deletions src/Altinn.App.Api/Controllers/InstancesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

using System.Net;
using System.Text;

using Altinn.App.Api.Helpers.RequestHandling;
using Altinn.App.Api.Infrastructure.Filters;
using Altinn.App.Api.Mappers;
Expand All @@ -16,6 +15,7 @@
using Altinn.App.Core.Interface;
using Altinn.App.Core.Internal.App;
using Altinn.App.Core.Internal.AppModel;
using Altinn.App.Core.Internal.Process;
using Altinn.App.Core.Models;
using Altinn.App.Core.Models.Validation;
using Altinn.Authorization.ABAC.Xacml.JsonProfile;
Expand All @@ -25,12 +25,10 @@
using Altinn.Platform.Profile.Models;
using Altinn.Platform.Register.Models;
using Altinn.Platform.Storage.Interface.Models;

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Primitives;

using Newtonsoft.Json;

namespace Altinn.App.Api.Controllers
Expand Down Expand Up @@ -60,8 +58,8 @@ public class InstancesController : ControllerBase
private readonly IInstantiationValidator _instantiationValidator;
private readonly IPDP _pdp;
private readonly IPrefill _prefillService;
private readonly IProcessEngine _processEngine;
private readonly AppSettings _appSettings;
private readonly IProcessEngine _processEngine;

private const long RequestSizeLimit = 2000 * 1024 * 1024;

Expand All @@ -81,7 +79,7 @@ public InstancesController(
IEvents eventsService,
IOptions<AppSettings> appSettings,
IPrefill prefillService,
IProfile profileClient,
IProfile profileClient,
IProcessEngine processEngine)
{
_logger = logger;
Expand Down Expand Up @@ -208,6 +206,7 @@ public async Task<ActionResult<Instance>> Post(
}
else
{
// create minimum instance template
instanceTemplate = new Instance
{
InstanceOwner = new InstanceOwner { PartyId = instanceOwnerPartyId.Value.ToString() }
Expand Down Expand Up @@ -267,12 +266,24 @@ public async Task<ActionResult<Instance>> Post(

Instance instance;
instanceTemplate.Process = null;
ProcessChangeContext processChangeContext = new ProcessChangeContext(instanceTemplate, User);
ProcessStateChange? change = null;

try
{
// start process
processChangeContext.DontUpdateProcessAndDispatchEvents = true;
processChangeContext = await _processEngine.StartProcess(processChangeContext);
// start process and goto next task
ProcessStartRequest processStartRequest = new ProcessStartRequest
{
Instance = instanceTemplate,
User = User,
Dryrun = true
};
var result = await _processEngine.StartProcess(processStartRequest);
if (!result.Success)
{
return Conflict(result.ErrorMessage);
}

change = result.ProcessStateChange;

// create the instance
instance = await _instanceClient.CreateInstance(org, app, instanceTemplate);
Expand All @@ -290,9 +301,14 @@ public async Task<ActionResult<Instance>> Post(
instance = await _instanceClient.GetInstance(app, org, int.Parse(instance.InstanceOwner.PartyId), Guid.Parse(instance.Id.Split("/")[1]));

// notify app and store events
processChangeContext.Instance = instance;
processChangeContext.DontUpdateProcessAndDispatchEvents = false;
await _processEngine.StartTask(processChangeContext);
var request = new ProcessStartRequest()
{
Instance = instance,
User = User,
Dryrun = false,
};
_logger.LogInformation("Events sent to process engine: {Events}", change?.Events);
await _processEngine.UpdateInstanceAndRerunEvents(request, change?.Events);
}
catch (Exception exception)
{
Expand Down Expand Up @@ -404,15 +420,21 @@ public async Task<ActionResult<Instance>> PostSimplified(
}

Instance instance;
ProcessChangeResult processResult;
try
{
// start process and goto next task
instanceTemplate.Process = null;

// start process
ProcessChangeContext processChangeContext = new ProcessChangeContext(instanceTemplate, User);
processChangeContext.Prefill = instansiationInstance.Prefill;
processChangeContext.DontUpdateProcessAndDispatchEvents = true;
processChangeContext = await _processEngine.StartProcess(processChangeContext);
var request = new ProcessStartRequest()
{
Instance = instanceTemplate,
User = User,
Dryrun = true,
Prefill = instansiationInstance.Prefill
};

processResult = await _processEngine.StartProcess(request);

Instance? source = null;

Expand Down Expand Up @@ -445,9 +467,14 @@ public async Task<ActionResult<Instance>> PostSimplified(

instance = await _instanceClient.GetInstance(instance);

processChangeContext.Instance = instance;
processChangeContext.DontUpdateProcessAndDispatchEvents = false;
await _processEngine.StartTask(processChangeContext);
var updateRequest = new ProcessStartRequest()
{
Instance = instance,
User = User,
Dryrun = false,
Prefill = instansiationInstance.Prefill
};
await _processEngine.UpdateInstanceAndRerunEvents(updateRequest, processResult.ProcessStateChange?.Events);
}
catch (Exception exception)
{
Expand Down Expand Up @@ -535,22 +562,28 @@ public async Task<ActionResult> CopyInstance(
{
return StatusCode((int)HttpStatusCode.Forbidden, validationResult);
}

ProcessChangeContext processChangeContext = new(targetInstance, User)
ProcessStartRequest processStartRequest = new()
{
DontUpdateProcessAndDispatchEvents = true
Instance = targetInstance,
User = User,
Dryrun = true
};
processChangeContext = await _processEngine.StartProcess(processChangeContext);
var startResult = await _processEngine.StartProcess(processStartRequest);

targetInstance = await _instanceClient.CreateInstance(org, app, targetInstance);

await CopyDataFromSourceInstance(application, targetInstance, sourceInstance);

targetInstance = await _instanceClient.GetInstance(targetInstance);

processChangeContext.Instance = targetInstance;
processChangeContext.DontUpdateProcessAndDispatchEvents = false;
await _processEngine.StartTask(processChangeContext);
ProcessStartRequest rerunRequest = new()
{
Instance = targetInstance,
Dryrun = false,
User = User
};
await _processEngine.UpdateInstanceAndRerunEvents(rerunRequest, startResult.ProcessStateChange?.Events);

await RegisterEvent("app.instance.created", targetInstance);

Expand Down
Loading

0 comments on commit 6ab302e

Please sign in to comment.