• 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

89.66
/Rdmp.Core/ReusableLibraryCode/DataAccess/DataAccessPortal.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.Linq;
9
using FAnsi.Discovery;
10
using Rdmp.Core.ReusableLibraryCode.Exceptions;
11

12
namespace Rdmp.Core.ReusableLibraryCode.DataAccess;
13

14
/// <summary>
15
/// Translation class for converting IDataAccessPoints into DiscoveredServer / DiscoveredDatabase / ConnectionStrings etc.  IDataAccessPoints are named
16
/// servers/databases which might have usernames/passwords associated with them (or might use Integrated Security).  Each IDataAccessPoint can have multiple
17
/// credentials that can be used with it depending on the DataAccessContext.  Therefore when using the DataAccessPortal you always have to specify the
18
/// Context of the activity you are doing e.g. DataAccessContext.DataLoad.
19
/// </summary>
20
public static class DataAccessPortal
21
{
22
    public static DiscoveredServer ExpectServer(IDataAccessPoint dataAccessPoint, DataAccessContext context,
23
        bool setInitialDatabase = true) => GetServer(dataAccessPoint, context, setInitialDatabase);
1,046✔
24

25
    public static DiscoveredDatabase ExpectDatabase(IDataAccessPoint dataAccessPoint, DataAccessContext context) =>
26
        GetServer(dataAccessPoint, context, true).GetCurrentDatabase();
1,832✔
27

28
    public static DiscoveredServer ExpectDistinctServer(IDataAccessPoint[] collection, DataAccessContext context,
29
        bool setInitialDatabase) =>
30
        GetServer(GetDistinct(collection, context, setInitialDatabase), context, setInitialDatabase);
1,482✔
31

32
    private static DiscoveredServer GetServer(IDataAccessPoint dataAccessPoint, DataAccessContext context,
33
        bool setInitialDatabase)
34
    {
35
        var credentials = dataAccessPoint.GetCredentialsIfExists(context);
4,298✔
36

37
        if (string.IsNullOrWhiteSpace(dataAccessPoint.Server))
4,298✔
38
            throw new NullReferenceException(
22✔
39
                $"Could not get connection string because Server was null on dataAccessPoint '{dataAccessPoint}'");
22✔
40

41
        string dbName = null;
4,276✔
42

43
        if (setInitialDatabase)
4,276✔
44
            if (!string.IsNullOrWhiteSpace(dataAccessPoint.Database))
3,300!
45
                dbName = dataAccessPoint.GetQuerySyntaxHelper().GetRuntimeName(dataAccessPoint.Database);
3,300✔
46
            else
47
                throw new Exception(
×
48
                    $"Could not get server with setInitialDatabase=true because no Database was set on IDataAccessPoint {dataAccessPoint}");
×
49

50
        var server = new DiscoveredServer(dataAccessPoint.Server, dbName, dataAccessPoint.DatabaseType,
4,276✔
51
            credentials?.Username, credentials?.GetDecryptedPassword());
4,276✔
52

53
        return server;
4,276✔
54
    }
55

56
    private static IDataAccessPoint GetDistinct(IDataAccessPoint[] collection, DataAccessContext context,
57
        bool setInitialDatabase)
58
    {
59
        ///////////////////////Exception handling///////////////////////////////////////////////
60
        if (!collection.Any())
1,482!
61
            throw new Exception("No IDataAccessPoints were passed, so no connection string builder can be created");
×
62

63
        var first = collection.First();
1,482✔
64

65
        //There can be only one - server
66
        foreach (var accessPoint in collection)
6,606✔
67
        {
68
            if (first.Server == null)
1,846!
69
                throw new Exception($"Server is null for {first}");
×
70

71
            if (!first.Server.Equals(accessPoint.Server, StringComparison.CurrentCultureIgnoreCase))
1,842✔
72
                throw new ExpectedIdenticalStringsException(
42✔
73
                    $"There was a mismatch in server names for data access points {first} and {accessPoint} server names must match exactly",
42✔
74
                    first.Server, accessPoint.Server);
42✔
75

76
            if (first.DatabaseType != accessPoint.DatabaseType)
1,800✔
77
                throw new ExpectedIdenticalStringsException(
2✔
78
                    $"There was a mismatch on DatabaseType for data access points {first} and {accessPoint}",
2✔
79
                    first.DatabaseType.ToString(), accessPoint.DatabaseType.ToString());
2✔
80

81
            if (setInitialDatabase)
1,798✔
82
            {
83
                if (string.IsNullOrWhiteSpace(first.Database))
766!
84
                    throw new Exception($"DataAccessPoint '{first}' does not have a Database specified on it");
×
85

86
                var querySyntaxHelper = accessPoint.GetQuerySyntaxHelper();
766✔
87

88
                var firstDbName = querySyntaxHelper.GetRuntimeName(first.Database);
766✔
89
                var currentDbName = querySyntaxHelper.GetRuntimeName(accessPoint.Database);
766✔
90

91
                if (!firstDbName.Equals(currentDbName))
766✔
92
                    throw new ExpectedIdenticalStringsException(
2✔
93
                        $"All data access points must be into the same database, access points '{first}' and '{accessPoint}' are into different databases",
2✔
94
                        firstDbName, currentDbName);
2✔
95
            }
96
        }
97

98
        //There can be only one - credentials (but there might not be any)
99
        var credentials = collection.Select(t => t.GetCredentialsIfExists(context)).ToArray();
3,182✔
100

101
        //if there are credentials
102
        if (credentials.Any(c => c != null))
3,064✔
103
            if (credentials.Any(c => c == null)) //all objects in collection must have a credentials if any of them do
782✔
104
                throw new Exception(
2✔
105
                    $"IDataAccessPoint collection could not agree whether to use Credentials or not {Environment.NewLine}Objects wanting to use Credentials{string.Join(",", collection.Where(c => c.GetCredentialsIfExists(context) != null).Select(s => s.ToString()))}{Environment.NewLine}Objects not wanting to use Credentials{string.Join(",", collection.Where(c => c.GetCredentialsIfExists(context) == null).Select(s => s.ToString()))}{Environment.NewLine}"
12✔
106
                );
2✔
107
            else
108
            //There can be only one - Username
109
            if (credentials.Select(c => c.Username).Distinct().Count() != 1)
778✔
110
                throw new Exception(
6✔
111
                    $"IDataAccessPoint collection could not agree on a single Username to use to access the data under context {context} (Servers were {string.Join($",{Environment.NewLine}", collection.Select(c => $"{c} = {c.Database} - {c.DatabaseType}"))})");
18✔
112
            else
113
            //There can be only one - Password
114
            if (credentials.Select(c => c.GetDecryptedPassword()).Distinct().Count() != 1)
760✔
115
                throw new Exception(
4✔
116
                    $"IDataAccessPoint collection could not agree on a single Password to use to access the data under context {context} (Servers were {string.Join($",{Environment.NewLine}", collection.Select(c => $"{c} = {c.Database} - {c.DatabaseType}"))})");
12✔
117

118

119
        ///////////////////////////////////////////////////////////////////////////////
120

121
        //the bit that actually matters:
122
        return first;
1,420✔
123
    }
124
}
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

© 2025 Coveralls, Inc