• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

HicServices / RDMP / 6245535001

20 Sep 2023 07:44AM UTC coverage: 57.013%. First build
6245535001

push

github

web-flow
8.1.0 Release (#1628)

* Bump Newtonsoft.Json from 13.0.1 to 13.0.2

Bumps [Newtonsoft.Json](https://github.com/JamesNK/Newtonsoft.Json) from 13.0.1 to 13.0.2.
- [Release notes](https://github.com/JamesNK/Newtonsoft.Json/releases)
- [Commits](https://github.com/JamesNK/Newtonsoft.Json/compare/13.0.1...13.0.2)

---
updated-dependencies:
- dependency-name: Newtonsoft.Json
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

* Bump NLog from 5.0.5 to 5.1.0

Bumps [NLog](https://github.com/NLog/NLog) from 5.0.5 to 5.1.0.
- [Release notes](https://github.com/NLog/NLog/releases)
- [Changelog](https://github.com/NLog/NLog/blob/dev/CHANGELOG.md)
- [Commits](https://github.com/NLog/NLog/compare/v5.0.5...v5.1.0)

---
updated-dependencies:
- dependency-name: NLog
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

* Bump NLog from 5.0.5 to 5.1.0

* Fix -r flag - should have been --results-directory all along

* Bump Newtonsoft.Json from 13.0.1 to 13.0.2

* Bump YamlDotNet from 12.0.2 to 12.1.0

Bumps [YamlDotNet](https://github.com/aaubry/YamlDotNet) from 12.0.2 to 12.1.0.
- [Release notes](https://github.com/aaubry/YamlDotNet/releases)
- [Commits](https://github.com/aaubry/YamlDotNet/compare/v12.0.2...v12.1.0)

---
updated-dependencies:
- dependency-name: YamlDotNet
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

* Bump Moq from 4.18.2 to 4.18.3

Bumps [Moq](https://github.com/moq/moq4) from 4.18.2 to 4.18.3.
- [Release notes](https://github.com/moq/moq4/releases)
- [Changelog](https://github.com/moq/moq4/blob/main/CHANGELOG.md)
- [Commits](https://github.com/moq/moq4/compare/v4.18.2...v4.18.3)

---
updated-dependencies:
- dependency-name: Moq
... (continued)

10732 of 20257 branches covered (0.0%)

Branch coverage included in aggregate %.

48141 of 48141 new or added lines in 1086 files covered. (100.0%)

30685 of 52388 relevant lines covered (58.57%)

7387.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.23
/Rdmp.Core/Curation/Data/DataLoad/ArgumentFactory.cs
1
// Copyright (c) The University of Dundee 2018-2019
2
// This file is part of the Research Data Management Platform (RDMP).
3
// RDMP is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
4
// RDMP is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
5
// You should have received a copy of the GNU General Public License along with RDMP. If not, see <https://www.gnu.org/licenses/>.
6

7
using System;
8
using System.Collections.Generic;
9
using System.Linq;
10
using System.Reflection;
11
using Rdmp.Core.MapsDirectlyToDatabaseTable;
12
using Rdmp.Core.ReusableLibraryCode.Exceptions;
13

14
namespace Rdmp.Core.Curation.Data.DataLoad;
15

16
/// <summary>
17
/// Provides methods for creating Argument values in the database for [DemandsInitialization] properties on classes (See Argument).  Every public property marked with
18
/// [DemandsInitialization] on a plugin component will allow the user to specify a value of the appropriate type.  This class will handle not only creating the IArguments
19
/// for a plugin component but also rationalising differences e.g. there is a new version of a class in the latest plugin with different [DemandsInitialization] properties,
20
/// which Argument values are no longer needed and which new ones must be created to store configuration values.
21
/// 
22
/// <para>Remember that a given plugin class can have multiple instances of it deployed into different pipelines with different argument values.</para>
23
/// </summary>
24
public class ArgumentFactory
25
{
26
    /// <inheritdoc cref = "CreateArgumentsForClassIfNotExistsGeneric(Type,IArgumentHost,IArgument[])"/>
27
    /// <typeparam name="T">A class with one or more Properties marked with DemandsInitialization</typeparam>
28
    /// <returns>Each new ProcessTaskArgument created - note that it will not return existing ones that were already present (and therefore not created)</returns>
29
    public static IEnumerable<IArgument> CreateArgumentsForClassIfNotExistsGeneric<T>(IArgumentHost host,
30
        IArgument[] existingArguments) => CreateArgumentsForClassIfNotExistsGeneric(typeof(T), host, existingArguments);
2✔
31

32
    /// <summary>
33
    /// Interrogates a class via reflection and enumerates its properties to find any that have the attribute [DemandsInitialization]
34
    /// Each one of these that is found is created as a ProcessTaskArgument of the appropriate Name and PropertyType under the parent ProcessTask
35
    /// </summary>
36
    /// <returns>Each new ProcessTaskArgument created - note that it will not return existing ones that were already present (and therefore not created)</returns>
37
    public static IEnumerable<IArgument> CreateArgumentsForClassIfNotExistsGeneric(
38
        Type underlyingClassTypeForWhichArgumentsWillPopulate, IArgumentHost host,
39
        IArgument[] existingArguments)
40
    {
41
        //get all the properties that must be set on AnySeparatorFileAttacher (Those marked with the attribute DemandsInitialization
42
        var propertiesWeHaveToSet = GetRequiredProperties(underlyingClassTypeForWhichArgumentsWillPopulate);
294✔
43

44
        foreach (var required in propertiesWeHaveToSet)
5,608✔
45
        {
46
            //there's already a property with the same name
47
            if (existingArguments.Any(a => a.Name.Equals(required.Name)))
2,514✔
48
                continue;
49

50
            //create a new one
51
            var argument = host.CreateNewArgument();
2,506✔
52

53
            //set the type and name
54
            argument.SetType(required.PropertyInfo.PropertyType);
2,506✔
55
            argument.Name = required.Name;
2,506✔
56

57
            var attribute = required.Demand;
2,506✔
58
            argument.Description = attribute.Description;
2,506✔
59

60
            if (attribute.DefaultValue != null)
2,506✔
61
                argument.SetValue(attribute.DefaultValue);
920✔
62

63
            if (argument is ISaveable saveable)
2,506✔
64
                saveable.SaveToDatabase();
2,506✔
65

66
            yield return argument;
2,506✔
67
        }
68
    }
294✔
69

70
    /// <summary>
71
    /// Gets all public properties of the given <paramref name="classType"/> decorated with <see cref="DemandsInitializationAttribute"/>.
72
    /// 
73
    /// <para>If there are any public properties encountered with <see cref="DemandsNestedInitializationAttribute"/> then the referenced class is also investigated in the same manner.</para>
74
    /// </summary>
75
    /// <param name="classType"></param>
76
    /// <returns></returns>
77
    public static List<RequiredPropertyInfo> GetRequiredProperties(Type classType)
78
    {
79
        var required = new List<RequiredPropertyInfo>();
308✔
80

81
        foreach (var propertyInfo in classType.GetProperties())
7,492✔
82
        {
83
            if (propertyInfo.GetCustomAttributes(typeof(DemandsNestedInitializationAttribute), true).Any())
3,438✔
84
            {
85
                var allNested = propertyInfo.PropertyType.GetProperties();
12✔
86
                foreach (var nestedPropInfo in allNested)
56✔
87
                {
88
                    //found a tagged attribute
89
                    //record the name of the property and the type it requires
90
                    var attribute = nestedPropInfo.GetCustomAttribute<DemandsInitializationAttribute>();
16✔
91

92
                    if (attribute != null)
16✔
93
                        required.Add(new RequiredPropertyInfo(attribute, nestedPropInfo, propertyInfo));
16✔
94
                }
95
            }
96

97
            var demands = propertyInfo.GetCustomAttributes(typeof(DemandsInitializationAttribute), true);
3,438✔
98

99
            if (demands.Length > 1)
3,438!
100
                throw new Exception(
×
101
                    $"Property {propertyInfo} on class {classType} has multiple declarations of DemandsInitializationAttribute");
×
102

103
            var demand = (DemandsInitializationAttribute)demands.SingleOrDefault();
3,438✔
104

105
            //found a tagged attribute
106
            if (demand != null)
3,438✔
107
                required.Add(new RequiredPropertyInfo(demand, propertyInfo));
2,576✔
108
        }
109

110
        return required;
308✔
111
    }
112

113
    /// <summary>
114
    /// Creates <see cref="IArgument"/> instances for all demanded properties (See <see cref="GetRequiredProperties"/>) of the given class and deletes any old arguments
115
    /// which are no longer required by the class (e.g. due to an API change).
116
    /// </summary>
117
    /// <param name="host"></param>
118
    /// <param name="underlyingClassTypeForWhichArgumentsWillPopulate"></param>
119
    public static void SyncArgumentsForClass(IArgumentHost host, Type underlyingClassTypeForWhichArgumentsWillPopulate)
120
    {
121
        if (host.GetClassNameWhoArgumentsAreFor() != underlyingClassTypeForWhichArgumentsWillPopulate.FullName)
6!
122
            throw new ExpectedIdenticalStringsException(
×
123
                "IArgumentHost is not currently hosting the Type requested for sync",
×
124
                host.GetClassNameWhoArgumentsAreFor(), underlyingClassTypeForWhichArgumentsWillPopulate.FullName);
×
125

126
        var existingArguments = host.GetAllArguments().ToList();
6✔
127
        var required = GetRequiredProperties(underlyingClassTypeForWhichArgumentsWillPopulate);
6✔
128

129
        //get rid of arguments that are no longer required
130
        foreach (var argumentsNotRequired in existingArguments.Where(e => required.All(r => r.Name != e.Name)))
20!
131
            ((IDeleteable)argumentsNotRequired).DeleteInDatabase();
×
132

133
        //create new arguments
134
        existingArguments.AddRange(CreateArgumentsForClassIfNotExistsGeneric(
6✔
135
            underlyingClassTypeForWhichArgumentsWillPopulate, host, existingArguments.ToArray()));
6✔
136

137
        //handle mismatches of Type/incompatible values / unloaded Types etc
138
        foreach (var r in required)
92✔
139
        {
140
            var existing = existingArguments.SingleOrDefault(e => e.Name == r.Name) ?? throw new Exception(
692!
141
                $"Despite creating new Arguments for class '{underlyingClassTypeForWhichArgumentsWillPopulate}' we do not have an IArgument called '{r.Name}' in the database (host='{host}')");
40✔
142
            if (existing.GetSystemType() != r.PropertyInfo.PropertyType)
40!
143
            {
144
                //user wants to fix the problem
145
                existing.SetType(r.PropertyInfo.PropertyType);
×
146
                ((ISaveable)existing).SaveToDatabase();
×
147
            }
148
        }
149
    }
6✔
150

151
    /// <summary>
152
    /// Synchronizes all arguments (See SyncArgumentsForClass) for the supplied class (<paramref name="underlyingClassTypeForWhichArgumentsWillPopulate"/>) and returns the mapping
153
    /// between <see cref="IArgument"/> (which stores the value) and public class property (<see cref="RequiredPropertyInfo"/>)
154
    /// </summary>
155
    /// <param name="host"></param>
156
    /// <param name="underlyingClassTypeForWhichArgumentsWillPopulate"></param>
157
    /// <returns></returns>
158
    public static Dictionary<IArgument, RequiredPropertyInfo> GetDemandDictionary(IArgumentHost host,
159
        Type underlyingClassTypeForWhichArgumentsWillPopulate)
160
    {
161
        var toReturn = new Dictionary<IArgument, RequiredPropertyInfo>();
6✔
162

163
        SyncArgumentsForClass(host, underlyingClassTypeForWhichArgumentsWillPopulate);
6✔
164

165
        var required = GetRequiredProperties(underlyingClassTypeForWhichArgumentsWillPopulate);
6✔
166

167
        foreach (var key in host.GetAllArguments())
92✔
168
            toReturn.Add(key, required.Single(e => e.Name == key.Name));
692✔
169

170
        return toReturn;
6✔
171
    }
172
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc