From 9932ed032b8fdb45a0f01bf2ab47bab31a38be14 Mon Sep 17 00:00:00 2001 From: Stephanie Buadu <47737608+acn-sbuad@users.noreply.github.com> Date: Tue, 30 Jun 2020 17:06:49 +0200 Subject: [PATCH] composing lastChanged/lastChangedBy in instanceRepository (#4446) * composing lastChanged/lastChangedBy in instanceRepository * indentation * syntax fix * updated mock * updated local test and mocks * unit tests for findlastchangeby instance helper * updated testdata * fixed bug in mock Co-authored-by: Stephanie Buadu Co-authored-by: jeevananthank --- ...5-a9f2-45d4-90b1-f6d93ad40713.pretest.json | 1 + .../Mocks/Services/InstanceMockSI.cs | 36 +++++++++- .../Storage/Helpers/InstanceHelper.cs | 22 ++++--- .../Storage/Repository/InstanceRepository.cs | 17 +++-- .../HelperTests/InstanceHelperTest.cs | 66 ++++++++++++++++++- .../Repository/InstanceRepositoryMock.cs | 7 +- .../UnitTest/TestData/TestData.cs | 10 ++- .../Helpers/Storage/InstanceHelper.cs | 33 +++++----- .../Implementation/InstanceRepository.cs | 5 ++ 9 files changed, 165 insertions(+), 32 deletions(-) diff --git a/src/Altinn.Apps/AppTemplates/AspNet/App.IntegrationTests/Data/Instances/tdd/endring-av-navn/1337/26133fb5-a9f2-45d4-90b1-f6d93ad40713.pretest.json b/src/Altinn.Apps/AppTemplates/AspNet/App.IntegrationTests/Data/Instances/tdd/endring-av-navn/1337/26133fb5-a9f2-45d4-90b1-f6d93ad40713.pretest.json index f92fee3b640..3afb379891c 100644 --- a/src/Altinn.Apps/AppTemplates/AspNet/App.IntegrationTests/Data/Instances/tdd/endring-av-navn/1337/26133fb5-a9f2-45d4-90b1-f6d93ad40713.pretest.json +++ b/src/Altinn.Apps/AppTemplates/AspNet/App.IntegrationTests/Data/Instances/tdd/endring-av-navn/1337/26133fb5-a9f2-45d4-90b1-f6d93ad40713.pretest.json @@ -7,6 +7,7 @@ "org": "tdd", "created": "2019-07-31T09:57:23.4729995Z", "lastChanged": "2019-07-31T09:57:23.4729995Z", + "lastChangedBy": "12345", "dueBefore": "2019-07-05T00:00:00Z", "visibleAfter": "2019-06-05T00:00:00Z", "process": { diff --git a/src/Altinn.Apps/AppTemplates/AspNet/App.IntegrationTests/Mocks/Services/InstanceMockSI.cs b/src/Altinn.Apps/AppTemplates/AspNet/App.IntegrationTests/Mocks/Services/InstanceMockSI.cs index 4d33d77b38b..b92152e8822 100644 --- a/src/Altinn.Apps/AppTemplates/AspNet/App.IntegrationTests/Mocks/Services/InstanceMockSI.cs +++ b/src/Altinn.Apps/AppTemplates/AspNet/App.IntegrationTests/Mocks/Services/InstanceMockSI.cs @@ -60,7 +60,9 @@ public Task GetInstance(string app, string org, int instanceOwnerId, G if (instance != null) { instance.Data = GetDataElements(org, app, instanceOwnerId, instanceId); + (instance.LastChangedBy, instance.LastChanged) = FindLastChanged(instance); } + return Task.FromResult(instance); } @@ -198,7 +200,7 @@ public async Task UpdateReadStatus(int instanceOwnerPartyId, Guid inst return await Task.FromResult(storedInstance); } - + return null; } @@ -215,5 +217,37 @@ private string GetInstancePath(int instanceOwnerPartyId, Guid instanceGuid) return string.Empty; } + + private static (string LastChangedBy, DateTime? LastChanged) FindLastChanged(Instance instance) + { + string lastChangedBy = instance.LastChangedBy; + DateTime? lastChanged = instance.LastChanged; + if (instance.Data == null || instance.Data.Count == 0) + { + return (lastChangedBy, lastChanged); + } + + List newerDataElements = instance.Data.FindAll(dataElement => + dataElement.LastChanged != null + && dataElement.LastChangedBy != null + && dataElement.LastChanged > instance.LastChanged); + + if (newerDataElements.Count == 0) + { + return (lastChangedBy, lastChanged); + } + + lastChanged = (DateTime)instance.LastChanged; + newerDataElements.ForEach((DataElement dataElement) => + { + if (dataElement.LastChanged > lastChanged) + { + lastChangedBy = dataElement.LastChangedBy; + lastChanged = (DateTime)dataElement.LastChanged; + } + }); + + return (lastChangedBy, lastChanged); + } } } diff --git a/src/Altinn.Platform/Altinn.Platform.Storage/Storage/Helpers/InstanceHelper.cs b/src/Altinn.Platform/Altinn.Platform.Storage/Storage/Helpers/InstanceHelper.cs index 713322e4177..561be0b48c5 100644 --- a/src/Altinn.Platform/Altinn.Platform.Storage/Storage/Helpers/InstanceHelper.cs +++ b/src/Altinn.Platform/Altinn.Platform.Storage/Storage/Helpers/InstanceHelper.cs @@ -33,7 +33,7 @@ public static MessageBoxInstance ConvertToMessageBoxInstance(Instance instance) DueDateTime = instance.DueBefore, Id = instanceGuid, InstanceOwnerId = instance.InstanceOwner.PartyId, - LastChangedBy = FindLastChangedBy(instance), + LastChangedBy = FindLastChanged(instance).LastChangedBy, Org = instance.Org, AppName = instance.AppId.Split('/')[1], ProcessCurrentTask = GetSBLStatusForCurrentTask(instance), @@ -119,12 +119,18 @@ public static string GetSBLStatusForCurrentTask(Instance instance) } } - private static string FindLastChangedBy(Instance instance) + /// + /// Finds last changed by for an instance and its listed data elements + /// + /// The instance + /// Last changed by + public static (string LastChangedBy, DateTime? LastChanged) FindLastChanged(Instance instance) { - string result = instance.LastChangedBy; + string lastChangedBy = instance.LastChangedBy; + DateTime? lastChanged = instance.LastChanged; if (instance.Data == null || instance.Data.Count == 0) { - return result; + return (lastChangedBy, lastChanged); } List newerDataElements = instance.Data.FindAll(dataElement => @@ -134,20 +140,20 @@ private static string FindLastChangedBy(Instance instance) if (newerDataElements.Count == 0) { - return result; + return (lastChangedBy, lastChanged); } - DateTime lastChanged = (DateTime)instance.LastChanged; + lastChanged = (DateTime)instance.LastChanged; newerDataElements.ForEach((DataElement dataElement) => { if (dataElement.LastChanged > lastChanged) { - result = dataElement.LastChangedBy; + lastChangedBy = dataElement.LastChangedBy; lastChanged = (DateTime)dataElement.LastChanged; } }); - return result; + return (lastChangedBy, lastChanged); } } } diff --git a/src/Altinn.Platform/Altinn.Platform.Storage/Storage/Repository/InstanceRepository.cs b/src/Altinn.Platform/Altinn.Platform.Storage/Storage/Repository/InstanceRepository.cs index 9057f4e1754..fbb1d177347 100644 --- a/src/Altinn.Platform/Altinn.Platform.Storage/Storage/Repository/InstanceRepository.cs +++ b/src/Altinn.Platform/Altinn.Platform.Storage/Storage/Repository/InstanceRepository.cs @@ -21,7 +21,7 @@ namespace Altinn.Platform.Storage.Repository /// Repository operations for application instances. /// public class InstanceRepository : IInstanceRepository - { + { private const string CollectionId = "instances"; private const string PartitionKey = "/instanceOwner/partyId"; @@ -59,7 +59,7 @@ public InstanceRepository( databaseUri, documentCollection).GetAwaiter().GetResult(); - _client.OpenAsync(); + _client.OpenAsync(); } /// @@ -542,8 +542,13 @@ private void PreProcess(Instance instance) } /// - /// Converts the instanceId (id) of the instance from {instanceGuid} to {instanceOwnerPartyId}/{instanceGuid} to be used outside cosmos. + /// Prepares the instance for exposure to end users and app owners. /// + /// + /// - Converts the instanceId (id) of the instance from {instanceGuid} to {instanceOwnerPartyId}/{instanceGuid} to be used outside cosmos. + /// - Retrieves all dataelements from data repository + /// - Sets correct LastChanged/LastChangedBy by comparing instance and data elements + /// /// the instance to preprocess private async Task PostProcess(Instance instance) { @@ -552,6 +557,10 @@ private async Task PostProcess(Instance instance) instance.Id = instanceId; instance.Data = await _dataRepository.ReadAll(instanceGuid); + + (string lastChangedBy, DateTime? lastChanged) = InstanceHelper.FindLastChanged(instance); + instance.LastChanged = lastChanged; + instance.LastChangedBy = lastChangedBy; } /// @@ -563,7 +572,7 @@ private async Task PostProcess(List instances) foreach (Instance item in instances) { await PostProcess(item); - } + } } /// diff --git a/src/Altinn.Platform/Altinn.Platform.Storage/UnitTest/HelperTests/InstanceHelperTest.cs b/src/Altinn.Platform/Altinn.Platform.Storage/UnitTest/HelperTests/InstanceHelperTest.cs index 674b4e08cb0..af6c0113e8f 100644 --- a/src/Altinn.Platform/Altinn.Platform.Storage/UnitTest/HelperTests/InstanceHelperTest.cs +++ b/src/Altinn.Platform/Altinn.Platform.Storage/UnitTest/HelperTests/InstanceHelperTest.cs @@ -83,7 +83,7 @@ public void ConvertToMessageBoxSingleInstance_TC03() string actualLastChangedBy = actual.LastChangedBy; // Assert - Assert.Equal(lastChangedBy, actualLastChangedBy); + Assert.Equal(lastChangedBy, actualLastChangedBy); } /// @@ -140,5 +140,69 @@ public void GetSBLStatusForCurrentTask_TC04() string sblStatus = InstanceHelper.GetSBLStatusForCurrentTask(instance); Assert.Equal("default", sblStatus); } + + /// + /// Scenario: Find last changed by from the instance and date elements + /// Expected: lastChangedBy is an user id is from the instance without dateelements + /// Success: lastChangedBy equals {expectedlastChangedBy} and lastchanged equals {expectedlastChanged} + /// + [Fact] + public void FindLastChangedBy_TC01() + { + // Arrange + Instance instance = TestData.Instance_2_2; + string expectedlastChangedBy = "20000000"; + DateTime expectedlastChanged = Convert.ToDateTime("2019-08-20T19:19:22.2135489Z"); + + // Act + (string lastChangedBy, DateTime? lastChanged) = InstanceHelper.FindLastChanged(instance); + + // Assert + Assert.Equal(expectedlastChangedBy, lastChangedBy); + Assert.Equal(expectedlastChanged, lastChanged); + } + + /// + /// Scenario: Find last changed by from the instance and date elements + /// Expected: lastChangedBy is an user id from one dataelement + /// Success: lastChangedBy equals {expectedlastChangedBy} and lastchanged equals {expectedlastChanged} + /// + [Fact] + public void FindLastChangedBy_TC02() + { + // Arrange + Instance instance = TestData.Instance_1_2; + string expectedlastChangedBy = "20000001"; + DateTime expectedlastChanged = Convert.ToDateTime("2019-09-20T21:19:22.2135489Z"); + + // Act + (string lastChangedBy, DateTime? lastChanged) = InstanceHelper.FindLastChanged(instance); + + // Assert + Assert.Equal(expectedlastChangedBy, lastChangedBy); + Assert.Equal(expectedlastChanged, lastChanged); + } + + /// + /// Scenario: Find last changed by from the instance and date elements + /// Expected: lastChangedBy is an user id from the dataelements list that has the latest lastChanged datetime + /// Success: lastChangedBy equals {expectedlastChangedBy} and lastchanged equals {expectedlastChanged} + /// + [Fact] + public void FindLastChangedBy_TC03() + { + // Arrange + Instance instance = TestData.Instance_2_1; + string expectedlastChangedBy = "20000001"; + DateTime expectedlastChanged = Convert.ToDateTime("2019-10-20T21:19:22.2135489Z"); + + // Act + (string lastChangedBy, DateTime? lastChanged) = InstanceHelper.FindLastChanged(instance); + + // Assert + Assert.Equal(expectedlastChangedBy, lastChangedBy); + Assert.Equal(expectedlastChanged, lastChanged); + } } } + diff --git a/src/Altinn.Platform/Altinn.Platform.Storage/UnitTest/Mocks/Repository/InstanceRepositoryMock.cs b/src/Altinn.Platform/Altinn.Platform.Storage/UnitTest/Mocks/Repository/InstanceRepositoryMock.cs index d01707a2777..ed89a629cfe 100644 --- a/src/Altinn.Platform/Altinn.Platform.Storage/UnitTest/Mocks/Repository/InstanceRepositoryMock.cs +++ b/src/Altinn.Platform/Altinn.Platform.Storage/UnitTest/Mocks/Repository/InstanceRepositoryMock.cs @@ -5,7 +5,7 @@ using System.Net; using System.Reflection; using System.Threading.Tasks; - +using Altinn.Platform.Storage.Helpers; using Altinn.Platform.Storage.Interface.Models; using Altinn.Platform.Storage.Repository; using Altinn.Platform.Storage.UnitTest.Utils; @@ -274,6 +274,11 @@ private void PostProcess(Instance instance) string instanceId = $"{instance.InstanceOwner.PartyId}/{instance.Id}"; instance.Id = instanceId; + // Should instanceData be included here as well? + + (string lastChangedBy, DateTime? lastChanged) = InstanceHelper.FindLastChanged(instance); + instance.LastChanged = lastChanged; + instance.LastChangedBy = lastChangedBy; } /// diff --git a/src/Altinn.Platform/Altinn.Platform.Storage/UnitTest/TestData/TestData.cs b/src/Altinn.Platform/Altinn.Platform.Storage/UnitTest/TestData/TestData.cs index 2c8b9f3f76d..a9484a2310d 100644 --- a/src/Altinn.Platform/Altinn.Platform.Storage/UnitTest/TestData/TestData.cs +++ b/src/Altinn.Platform/Altinn.Platform.Storage/UnitTest/TestData/TestData.cs @@ -9,6 +9,7 @@ public static class TestData { public static string InstanceOwnerPartyId_1 = "50000000"; public static string UserId_1 = "20000000"; + public static string UserId_2 = "20000001"; public static string Org_1 = "TDD"; public static string Org_2 = "SPF"; @@ -102,7 +103,10 @@ private static ProcessState CreateProcessState() Created = Convert.ToDateTime("2019-08-20T19:20:21.7920255Z"), InstanceOwner = new InstanceOwner { PartyId = InstanceOwnerPartyId_1 }, Status = new InstanceStatus - { + { + }, + Data = new List() { + new DataElement() { LastChangedBy= UserId_2, LastChanged = Convert.ToDateTime("2019-09-20T21:19:22.2135489Z") } }, LastChangedBy = UserId_1, LastChanged = Convert.ToDateTime("2019-08-20T21:19:22.2135489Z"), @@ -121,6 +125,10 @@ private static ProcessState CreateProcessState() Status = new InstanceStatus { }, + Data = new List() { + new DataElement() { LastChangedBy = UserId_2, LastChanged = Convert.ToDateTime("2019-09-20T21:19:22.2135489Z") }, + new DataElement() { LastChangedBy = UserId_2, LastChanged = Convert.ToDateTime("2019-10-20T21:19:22.2135489Z") } + }, LastChangedBy = UserId_1, LastChanged = Convert.ToDateTime("2019-08-20T23:19:22.2135489Z"), Org = Org_1, diff --git a/src/development/LocalTest/Helpers/Storage/InstanceHelper.cs b/src/development/LocalTest/Helpers/Storage/InstanceHelper.cs index b862a2cd037..561be0b48c5 100644 --- a/src/development/LocalTest/Helpers/Storage/InstanceHelper.cs +++ b/src/development/LocalTest/Helpers/Storage/InstanceHelper.cs @@ -33,7 +33,7 @@ public static MessageBoxInstance ConvertToMessageBoxInstance(Instance instance) DueDateTime = instance.DueBefore, Id = instanceGuid, InstanceOwnerId = instance.InstanceOwner.PartyId, - LastChangedBy = FindLastChangedBy(instance), + LastChangedBy = FindLastChanged(instance).LastChangedBy, Org = instance.Org, AppName = instance.AppId.Split('/')[1], ProcessCurrentTask = GetSBLStatusForCurrentTask(instance), @@ -100,22 +100,17 @@ public static string GetSBLStatusForCurrentTask(Instance instance) { if (instance.Process != null) { - string currentTask = instance.Process.CurrentTask?.ElementId; - if (currentTask != null) - { - return "FormFilling"; - } - else if (string.IsNullOrEmpty(currentTask) && instance.Process.Ended != null && instance.Status?.Archived == null) + if (instance.Process.Ended != null && instance.Status?.Archived == null) { return "Submit"; } - else if (string.IsNullOrEmpty(currentTask) && instance.Process.Ended != null && instance.Status?.Archived != null) + else if (instance.Process.Ended != null && instance.Status?.Archived != null) { return "Archived"; } else { - return instance.Process.CurrentTask?.ElementId; + return "FormFilling"; } } else @@ -124,12 +119,18 @@ public static string GetSBLStatusForCurrentTask(Instance instance) } } - private static string FindLastChangedBy(Instance instance) + /// + /// Finds last changed by for an instance and its listed data elements + /// + /// The instance + /// Last changed by + public static (string LastChangedBy, DateTime? LastChanged) FindLastChanged(Instance instance) { - string result = instance.LastChangedBy; + string lastChangedBy = instance.LastChangedBy; + DateTime? lastChanged = instance.LastChanged; if (instance.Data == null || instance.Data.Count == 0) { - return result; + return (lastChangedBy, lastChanged); } List newerDataElements = instance.Data.FindAll(dataElement => @@ -139,20 +140,20 @@ private static string FindLastChangedBy(Instance instance) if (newerDataElements.Count == 0) { - return result; + return (lastChangedBy, lastChanged); } - DateTime lastChanged = (DateTime)instance.LastChanged; + lastChanged = (DateTime)instance.LastChanged; newerDataElements.ForEach((DataElement dataElement) => { if (dataElement.LastChanged > lastChanged) { - result = dataElement.LastChangedBy; + lastChangedBy = dataElement.LastChangedBy; lastChanged = (DateTime)dataElement.LastChanged; } }); - return result; + return (lastChangedBy, lastChanged); } } } diff --git a/src/development/LocalTest/Services/Storage/Implementation/InstanceRepository.cs b/src/development/LocalTest/Services/Storage/Implementation/InstanceRepository.cs index 5f0c36539f4..55648fd8d00 100644 --- a/src/development/LocalTest/Services/Storage/Implementation/InstanceRepository.cs +++ b/src/development/LocalTest/Services/Storage/Implementation/InstanceRepository.cs @@ -1,3 +1,4 @@ +using Altinn.Platform.Storage.Helpers; using Altinn.Platform.Storage.Interface.Models; using Altinn.Platform.Storage.Repository; using LocalTest.Configuration; @@ -100,6 +101,10 @@ private async Task PostProcess(Instance instance) instance.Id = instanceId; instance.Data = await _dataRepository.ReadAll(instanceGuid); + + (string lastChangedBy, DateTime? lastChanged) = InstanceHelper.FindLastChanged(instance); + instance.LastChanged = lastChanged; + instance.LastChangedBy = lastChangedBy; } ///