-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathCachedFileRetrieverTests.cs
More file actions
216 lines (182 loc) · 8.96 KB
/
Copy pathCachedFileRetrieverTests.cs
File metadata and controls
216 lines (182 loc) · 8.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright (c) The University of Dundee 2018-2019
// This file is part of the Research Data Management Platform (RDMP).
// 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.
// 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.
// You should have received a copy of the GNU General Public License along with RDMP. If not, see <https://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
using System.IO;
using FAnsi.Discovery;
using NSubstitute;
using NUnit.Framework;
using Rdmp.Core.Caching.Layouts;
using Rdmp.Core.Curation;
using Rdmp.Core.Curation.Data;
using Rdmp.Core.Curation.Data.Cache;
using Rdmp.Core.Curation.Data.DataLoad;
using Rdmp.Core.DataFlowPipeline;
using Rdmp.Core.DataLoad;
using Rdmp.Core.DataLoad.Engine.DataProvider.FromCache;
using Rdmp.Core.DataLoad.Engine.Job;
using Rdmp.Core.DataLoad.Engine.Job.Scheduling;
using Rdmp.Core.Logging;
using Rdmp.Core.ReusableLibraryCode.Progress;
using Tests.Common;
namespace Rdmp.Core.Tests.DataLoad.Engine.Integration;
public class CachedFileRetrieverTests : DatabaseTests
{
private readonly ILoadProgress _lpMock;
public CachedFileRetrieverTests()
{
var cpMock = Substitute.For<ICacheProgress>();
_lpMock = Substitute.For<ILoadProgress>();
_lpMock.CacheProgress.Returns(cpMock);
}
[Test(Description =
"RDMPDEV-185: Tests the scenario where the files in ForLoading do not match the files that are expected given the job specification. In this case the load process should not continue, otherwise the wrong data will be loaded.")]
public void AttemptToLoadDataWithFilesInForLoading_DisagreementBetweenCacheAndForLoading()
{
var tempDirPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
var tempDir = Directory.CreateDirectory(tempDirPath);
try
{
// Different file in ForLoading than exists in cache
var loadDirectory = LoadDirectory.CreateDirectoryStructure(tempDir, "CachedFileRetriever");
var cachedFilePath = Path.Combine(loadDirectory.Cache.FullName, "2016-01-02.zip");
File.WriteAllText(cachedFilePath, "");
File.WriteAllText(Path.Combine(loadDirectory.ForLoading.FullName, "2016-01-01.zip"), "");
// Set SetUp retriever
var cacheLayout = new ZipCacheLayoutOnePerDay(loadDirectory.Cache, new NoSubdirectoriesCachePathResolver());
var retriever = new TestCachedFileRetriever
{
ExtractFilesFromArchive = false,
LoadProgress = _lpMock,
Layout = cacheLayout
};
// Set SetUp job
var job = CreateTestJob(loadDirectory);
job.DatesToRetrieve = new List<DateTime>
{
new(2016, 01, 02)
};
// Should fail after determining that the files in ForLoading do not match the job specification
var ex = Assert.Throws<InvalidOperationException>(() =>
retriever.Fetch(job, new GracefulCancellationToken()));
Assert.That(
ex.Message, Does.StartWith("The files in ForLoading do not match what this job expects to be loading from the cache."),
ex.Message + Environment.NewLine + Environment.NewLine + ex.StackTrace);
}
finally
{
tempDir.Delete(true);
}
}
[Test(Description =
"RDMPDEV-185: Tests the scenario where the files in ForLoading match the files that are expected given the job specification, e.g. a load has after the cache has been populated and a subsequent load with *exactly the same parameters* has been triggered. In this case the load can proceed.")]
public void AttemptToLoadDataWithFilesInForLoading_AgreementBetweenForLoadingAndCache()
{
var tempDirPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
var tempDir = Directory.CreateDirectory(tempDirPath);
try
{
// File in cache is the same file as in ForLoading (20160101.zip)
var loadDirectory = LoadDirectory.CreateDirectoryStructure(tempDir, "CachedFileRetriever");
var cachedFilePath = Path.Combine(loadDirectory.Cache.FullName, "2016-01-01.zip");
File.WriteAllText(cachedFilePath, "");
File.WriteAllText(Path.Combine(loadDirectory.ForLoading.FullName, "2016-01-01.zip"), "");
// Set SetUp retriever
var cacheLayout = new ZipCacheLayoutOnePerDay(loadDirectory.Cache, new NoSubdirectoriesCachePathResolver());
var retriever = new TestCachedFileRetriever
{
ExtractFilesFromArchive = false,
LoadProgress = _lpMock,
Layout = cacheLayout
};
// Set SetUp job
var job = CreateTestJob(loadDirectory);
job.DatesToRetrieve = new List<DateTime>
{
new(2016, 01, 01)
};
// Should complete successfully, the file in ForLoading matches the job specification
retriever.Fetch(job, new GracefulCancellationToken());
// And ForLoading should still have the file in it (i.e. it hasn't mysteriously disappeared)
Assert.That(File.Exists(Path.Combine(loadDirectory.ForLoading.FullName, "2016-01-01.zip")));
}
finally
{
tempDir.Delete(true);
}
}
[Test(Description = "RDMPDEV-185: Tests the default scenario where there are no files in ForLoading.")]
public void AttemptToLoadDataWithoutFilesInForLoading()
{
var tempDirPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
var tempDir = Directory.CreateDirectory(tempDirPath);
try
{
// File in cache only, no files in ForLoading
var loadDirectory = LoadDirectory.CreateDirectoryStructure(tempDir, "CachedFileRetriever");
var cachedFilePath = Path.Combine(loadDirectory.Cache.FullName, "2016-01-01.zip");
File.WriteAllText(cachedFilePath, "");
// Set SetUp retriever
var cacheLayout = new ZipCacheLayoutOnePerDay(loadDirectory.Cache, new NoSubdirectoriesCachePathResolver());
var retriever = new TestCachedFileRetriever
{
ExtractFilesFromArchive = false,
LoadProgress = _lpMock,
Layout = cacheLayout
};
// Set SetUp job
var job = CreateTestJob(loadDirectory);
job.DatesToRetrieve = new List<DateTime>
{
new(2016, 01, 01)
};
// Should complete successfully, there are no files in ForLoading to worry about
retriever.Fetch(job, new GracefulCancellationToken());
// And the retriever should have copied the cached archive file into ForLoading
Assert.That(File.Exists(Path.Combine(loadDirectory.ForLoading.FullName, "2016-01-01.zip")));
}
finally
{
tempDir.Delete(true);
}
}
private ScheduledDataLoadJob CreateTestJob(ILoadDirectory directory)
{
var catalogue = Substitute.For<ICatalogue>();
catalogue.GetTableInfoList(false).Returns(Array.Empty<TableInfo>());
catalogue.GetLookupTableInfoList().Returns(Array.Empty<TableInfo>());
var logManager = Substitute.For<ILogManager>();
var loadMetadata = Substitute.For<ILoadMetadata>();
loadMetadata.GetAllCatalogues().Returns(new[] { catalogue });
var link = Substitute.For<ILoadMetadataCatalogueLinkage>();
link.CatalogueID.Returns(catalogue.ID);
link.LoadMetadataID.Returns(loadMetadata.ID);
link.Name.Returns("Test linkage");
catalogue.LoggingDataTasks.Returns(new List<ILoadMetadataCatalogueLinkage>() { link });
var j = new ScheduledDataLoadJob(RepositoryLocator, "Test job", logManager, loadMetadata, directory,
ThrowImmediatelyDataLoadEventListener.Quiet, null)
{
LoadProgress = _lpMock
};
return j;
}
}
internal class TestCachedFileRetriever : CachedFileRetriever
{
public ICacheLayout Layout;
public override void Initialize(ILoadDirectory directory, DiscoveredDatabase dbInfo)
{
}
public override ExitCodeType Fetch(IDataLoadJob dataLoadJob, GracefulCancellationToken cancellationToken)
{
var scheduledJob = ConvertToScheduledJob(dataLoadJob);
GetDataLoadWorkload(scheduledJob);
ExtractJobs(scheduledJob);
return ExitCodeType.Success;
}
protected override ICacheLayout CreateCacheLayout(ICacheProgress cacheProgress, IDataLoadEventListener listener) =>
Layout;
}