Skip to content
Newer
Older
100644 311 lines (260 sloc) 12.5 KB
c2eb0fc @AdaTheDev Initial drop. Functionality to extract tweet data from .js files as e…
authored Feb 10, 2013
1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using System.Linq;
63406cf @AdaTheDev Parse tweet data files into Tweet data structure
authored Feb 18, 2013
5 using ElasticTweets.Library.Data;
c2eb0fc @AdaTheDev Initial drop. Functionality to extract tweet data from .js files as e…
authored Feb 10, 2013
6 using ElasticTweets.Library.IO;
7 using ElasticTweets.Library.Providers;
8 using Moq;
9 using NUnit.Framework;
10 using Nest;
ba32148 @AdaTheDev Handle dodgy data files with invalid JSON contents and rationalise fi…
authored Feb 11, 2013
11 using Newtonsoft.Json;
c2eb0fc @AdaTheDev Initial drop. Functionality to extract tweet data from .js files as e…
authored Feb 10, 2013
12
13 // ReSharper disable InconsistentNaming
14 namespace ElasticTweets.Library.UnitTests
15 {
16 [TestFixture]
17 public class ImporterFixture
18 {
19 private const string TestSourceDirectory = @"C:\Test-Not-Real\";
3b2b99b @AdaTheDev Check ElasticSearch client bulk response for errors and create file r…
authored Feb 11, 2013
20 private const string TestResponseErrorMessage = "Dummy error";
c2eb0fc @AdaTheDev Initial drop. Functionality to extract tweet data from .js files as e…
authored Feb 10, 2013
21
22 private Importer _importer;
23 private Mock<IFileSystem> _mockedFileSystem;
24 private Mock<IElasticConnectionSettings> _mockedConnectionSettings;
25 private Mock<IClientProvider> _mockedClientProvider;
26 private Mock<IElasticClient> _mockedClient;
27 private Mock<ITweetDataFileParser> _mockedFileParser;
63406cf @AdaTheDev Parse tweet data files into Tweet data structure
authored Feb 18, 2013
28 private List<Tweet> _testTweets;
3b2b99b @AdaTheDev Check ElasticSearch client bulk response for errors and create file r…
authored Feb 11, 2013
29 private Mock<IBulkResponse> _mockedClientSuccessResponse;
30
c2eb0fc @AdaTheDev Initial drop. Functionality to extract tweet data from .js files as e…
authored Feb 10, 2013
31 [SetUp]
32 public void Setup()
33 {
34 _mockedFileSystem = new Mock<IFileSystem>(MockBehavior.Strict);
35 _mockedConnectionSettings = new Mock<IElasticConnectionSettings>(MockBehavior.Strict);
36 _mockedClientProvider = new Mock<IClientProvider>(MockBehavior.Strict);
37 _mockedClient = new Mock<IElasticClient>(MockBehavior.Strict);
38 _mockedFileParser = new Mock<ITweetDataFileParser>(MockBehavior.Strict);
3b2b99b @AdaTheDev Check ElasticSearch client bulk response for errors and create file r…
authored Feb 11, 2013
39 _mockedClientSuccessResponse = new Mock<IBulkResponse>(MockBehavior.Strict);
40 _mockedClientSuccessResponse.Setup(r => r.Items)
41 .Returns(new BulkOperationResponseItem[] {new BulkIndexResponseItem()});
42 _mockedClientSuccessResponse.Setup(r => r.ConnectionStatus).Returns(new ConnectionStatus("Test"));
43
63406cf @AdaTheDev Parse tweet data files into Tweet data structure
authored Feb 18, 2013
44 _testTweets = new List<Tweet>();
c2eb0fc @AdaTheDev Initial drop. Functionality to extract tweet data from .js files as e…
authored Feb 10, 2013
45 }
46
3b2b99b @AdaTheDev Check ElasticSearch client bulk response for errors and create file r…
authored Feb 11, 2013
47 public void InitialiseImporter(IBulkResponse mockedBulkResponse = null)
c2eb0fc @AdaTheDev Initial drop. Functionality to extract tweet data from .js files as e…
authored Feb 10, 2013
48 {
49 _mockedFileSystem.Setup(fs => fs.DirectoryExists(TestSourceDirectory)).Returns(true);
50 _mockedClientProvider.Setup(cp => cp.GetClient(_mockedConnectionSettings.Object))
51 .Returns(_mockedClient.Object);
ba32148 @AdaTheDev Handle dodgy data files with invalid JSON contents and rationalise fi…
authored Feb 11, 2013
52
c2eb0fc @AdaTheDev Initial drop. Functionality to extract tweet data from .js files as e…
authored Feb 10, 2013
53 _mockedFileParser.Setup(fp => fp.GetTweets(It.IsAny<string>())).Returns(_testTweets);
ba32148 @AdaTheDev Handle dodgy data files with invalid JSON contents and rationalise fi…
authored Feb 11, 2013
54
3b2b99b @AdaTheDev Check ElasticSearch client bulk response for errors and create file r…
authored Feb 11, 2013
55 if (mockedBulkResponse == null)
63406cf @AdaTheDev Parse tweet data files into Tweet data structure
authored Feb 18, 2013
56 _mockedClient.Setup(c => c.IndexMany(It.IsAny<IEnumerable<Tweet>>())).Returns(_mockedClientSuccessResponse.Object);
3b2b99b @AdaTheDev Check ElasticSearch client bulk response for errors and create file r…
authored Feb 11, 2013
57 else
63406cf @AdaTheDev Parse tweet data files into Tweet data structure
authored Feb 18, 2013
58 _mockedClient.Setup(c => c.IndexMany(It.IsAny<IEnumerable<Tweet>>())).Returns(mockedBulkResponse);
c2eb0fc @AdaTheDev Initial drop. Functionality to extract tweet data from .js files as e…
authored Feb 10, 2013
59
e4109b0 @AdaTheDev Updated unit tests to take into account ElasticSearch connection vali…
authored Feb 11, 2013
60 _mockedClient.Setup(c => c.IsValid).Returns(true);
61
c2eb0fc @AdaTheDev Initial drop. Functionality to extract tweet data from .js files as e…
authored Feb 10, 2013
62 _importer = new Importer(_mockedFileSystem.Object, _mockedFileParser.Object, _mockedClientProvider.Object, _mockedConnectionSettings.Object, TestSourceDirectory);
3b2b99b @AdaTheDev Check ElasticSearch client bulk response for errors and create file r…
authored Feb 11, 2013
63 }
64
65 private void SetupFileSystem()
66 {
67 _mockedFileSystem.Setup(fs => fs.GetFiles(TestSourceDirectory, "*.js")).Returns(new[] { "1.js" });
68 _mockedFileSystem.Setup(fs => fs.ReadAllText(It.IsAny<string>())).Returns("");
69 }
c2eb0fc @AdaTheDev Initial drop. Functionality to extract tweet data from .js files as e…
authored Feb 10, 2013
70
71 #region Constructor Tests
72 [Test]
73 [ExpectedException(typeof(ArgumentNullException))]
74 public void Constructor_ThrowsWhenNullConnectionSettingsSupplied()
75 {
76 try
77 {
78 // ReSharper disable ObjectCreationAsStatement
79 new Importer(_mockedFileSystem.Object, _mockedFileParser.Object, _mockedClientProvider.Object, null, TestSourceDirectory);
80 // ReSharper restore ObjectCreationAsStatement
81 }
82 catch (ArgumentNullException exception)
83 {
84 Assert.AreEqual("elasticConnectionSettings", exception.ParamName, "Incorrect ParamName");
85 throw;
86 }
87 }
88
89 [Test]
90 [ExpectedException(typeof(ArgumentNullException))]
91 public void Constructor_ThrowsWhenNullFileSystemSupplied()
92 {
93 try
94 {
95 // ReSharper disable ObjectCreationAsStatement
96 new Importer(null, _mockedFileParser.Object, _mockedClientProvider.Object, _mockedConnectionSettings.Object, TestSourceDirectory);
97 // ReSharper restore ObjectCreationAsStatement
98 }
99 catch (ArgumentNullException exception)
100 {
101 Assert.AreEqual("fileSystem", exception.ParamName, "Incorrect ParamName");
102 throw;
103 }
104 }
105
106 [Test]
107 [ExpectedException(typeof(ArgumentNullException))]
108 public void Constructor_ThrowsWhenNullClientProviderSupplied()
109 {
110 try
111 {
112 // ReSharper disable ObjectCreationAsStatement
113 new Importer(_mockedFileSystem.Object, _mockedFileParser.Object, null, _mockedConnectionSettings.Object, TestSourceDirectory);
114 // ReSharper restore ObjectCreationAsStatement
115 }
116 catch (ArgumentNullException exception)
117 {
118 Assert.AreEqual("clientProvider", exception.ParamName, "Incorrect ParamName");
119 throw;
120 }
121 }
122
123 [Test]
124 [ExpectedException(typeof(ArgumentNullException))]
125 public void Constructor_ThrowsWhenNullFileParserSupplied()
126 {
127 try
128 {
129 // ReSharper disable ObjectCreationAsStatement
130 new Importer(_mockedFileSystem.Object, null, _mockedClientProvider.Object, _mockedConnectionSettings.Object, TestSourceDirectory);
131 // ReSharper restore ObjectCreationAsStatement
132 }
133 catch (ArgumentNullException exception)
134 {
135 Assert.AreEqual("tweetDataFileParser", exception.ParamName, "Incorrect ParamName");
136 throw;
137 }
138 }
139
140 [Test]
141 public void Constructor_ChecksSourceDirectoryExists()
142 {
143 _mockedFileSystem.Setup(fs => fs.DirectoryExists(It.IsAny<string>())).Returns(false);
144
145 try
146 {
147 // ReSharper disable ObjectCreationAsStatement
148 new Importer(_mockedFileSystem.Object, _mockedFileParser.Object, _mockedClientProvider.Object, _mockedConnectionSettings.Object, TestSourceDirectory);
149 // ReSharper restore ObjectCreationAsStatement
150 // ReSharper disable EmptyGeneralCatchClause
151 }catch{}
152 // ReSharper restore EmptyGeneralCatchClause
153
154 _mockedFileSystem.Verify(fs => fs.DirectoryExists(TestSourceDirectory), Times.Once());
155 }
156
157 [Test]
158 [ExpectedException(typeof(DirectoryNotFoundException))]
159 public void Constructor_ThrowsWhenSourceDirectoryDoesNotExist()
160 {
161 _mockedFileSystem.Setup(fs => fs.DirectoryExists(It.IsAny<string>())).Returns(false);
162
163 // ReSharper disable ObjectCreationAsStatement
164 new Importer(_mockedFileSystem.Object, _mockedFileParser.Object, _mockedClientProvider.Object, _mockedConnectionSettings.Object, TestSourceDirectory);
165 // ReSharper restore ObjectCreationAsStatement
166 }
167
168 [Test]
169 public void Constructor_SetsConnectionSettings()
170 {
171 InitialiseImporter();
172
173 Assert.AreSame(_mockedConnectionSettings.Object, _importer.ElasticConnectionSettings);
174 }
175
176 [Test]
177 public void Constructor_SetsSourceDirectory()
178 {
179 InitialiseImporter();
180
181 Assert.AreSame(TestSourceDirectory, _importer.SourceDirectory);
182 }
183 #endregion
184
185 #region Import Tests
e4109b0 @AdaTheDev Updated unit tests to take into account ElasticSearch connection vali…
authored Feb 11, 2013
186 [Test]
187 public void Import_ChecksElasticSearchConnectionIsValid()
188 {
189 InitialiseImporter();
190 _mockedFileSystem.Setup(fs => fs.GetFiles(TestSourceDirectory, "*.js")).Returns(new[] { "1.js" });
191
192 _importer.Import();
193
194 _mockedClient.Verify(c => c.IsValid, Times.Once());
195 }
196
197 [Test]
198 [ExpectedException(typeof(ElasticSearchException))]
199 public void Import_ThrowsWhenElasticSearchConnectionIsNotValid()
200 {
201 InitialiseImporter();
202 _mockedClient.Setup(c => c.IsValid).Returns(false);
203
204 _importer.Import();
205 }
206
c2eb0fc @AdaTheDev Initial drop. Functionality to extract tweet data from .js files as e…
authored Feb 10, 2013
207 [Test]
208 public void Import_RetrievesFileNamesOnceThroughFileSystem()
209 {
210 InitialiseImporter();
211 _mockedFileSystem.Setup(fs => fs.GetFiles(TestSourceDirectory, "*.js")).Returns(new[] {"1.js"});
212
213 _importer.Import();
214
215 _mockedFileSystem.Verify(fs => fs.GetFiles(TestSourceDirectory, "*.js"), Times.Once());
216 }
217
218 [Test]
219 public void Import_GetsClientFromClientProvider()
220 {
221 InitialiseImporter();
222 _mockedFileSystem.Setup(fs => fs.GetFiles(TestSourceDirectory, "*.js")).Returns(new []{"1.js"});
223 _mockedFileSystem.Setup(fs => fs.ReadAllText(It.IsAny<string>())).Returns("");
224
225 _importer.Import();
226
227 _mockedClientProvider.Verify(cp => cp.GetClient(_mockedConnectionSettings.Object), Times.Once());
228 }
229
230 [Test]
231 public void Import_DoesNotCallElasticClientWhenNoTweetsPresent()
232 {
233 InitialiseImporter();
234 _mockedFileSystem.Setup(fs => fs.GetFiles(TestSourceDirectory, "*.js")).Returns(new[] { "1.js" });
235 _mockedFileSystem.Setup(fs => fs.ReadAllText(It.IsAny<string>())).Returns("");
236
237 _importer.Import();
238
239 _mockedClient.Verify(c => c.IndexMany(It.IsAny<IEnumerable<dynamic>>()), Times.Never());
240 }
241
242 [Test]
243 public void Import_CallsElasticClientToIndexTweets()
244 {
245 InitialiseImporter();
246 _mockedFileSystem.Setup(fs => fs.GetFiles(TestSourceDirectory, "*.js")).Returns(new[] { "1.js" });
247 _mockedFileSystem.Setup(fs => fs.ReadAllText(It.IsAny<string>())).Returns("");
63406cf @AdaTheDev Parse tweet data files into Tweet data structure
authored Feb 18, 2013
248 _testTweets.Add(new Tweet{id=1});
c2eb0fc @AdaTheDev Initial drop. Functionality to extract tweet data from .js files as e…
authored Feb 10, 2013
249
250 _importer.Import();
251
63406cf @AdaTheDev Parse tweet data files into Tweet data structure
authored Feb 18, 2013
252 _mockedClient.Verify(c => c.IndexMany(It.IsAny<IEnumerable<Tweet>>()), Times.Once());
c2eb0fc @AdaTheDev Initial drop. Functionality to extract tweet data from .js files as e…
authored Feb 10, 2013
253 }
254
255 [Test]
ba32148 @AdaTheDev Handle dodgy data files with invalid JSON contents and rationalise fi…
authored Feb 11, 2013
256 public void Import_ReturnsCorrectImportResultForValidFile()
c2eb0fc @AdaTheDev Initial drop. Functionality to extract tweet data from .js files as e…
authored Feb 10, 2013
257 {
258 InitialiseImporter();
ba32148 @AdaTheDev Handle dodgy data files with invalid JSON contents and rationalise fi…
authored Feb 11, 2013
259 SetupFileSystem();
63406cf @AdaTheDev Parse tweet data files into Tweet data structure
authored Feb 18, 2013
260 _testTweets.Add(new Tweet{ id = 1 });
c2eb0fc @AdaTheDev Initial drop. Functionality to extract tweet data from .js files as e…
authored Feb 10, 2013
261
262 var result = _importer.Import();
263
ba32148 @AdaTheDev Handle dodgy data files with invalid JSON contents and rationalise fi…
authored Feb 11, 2013
264 Assert.AreEqual(1, result.Files.Count(), "1 ImportFileResult expected");
265 var file = result.Files.First();
c2eb0fc @AdaTheDev Initial drop. Functionality to extract tweet data from .js files as e…
authored Feb 10, 2013
266 Assert.AreEqual(1, file.NumberOfTweets, "Incorrect NumberOfTweets");
267 Assert.AreEqual("1.js", file.FileName, "Incorrect FileName");
ba32148 @AdaTheDev Handle dodgy data files with invalid JSON contents and rationalise fi…
authored Feb 11, 2013
268 Assert.IsTrue(file.Success, "Success should be True");
3b2b99b @AdaTheDev Check ElasticSearch client bulk response for errors and create file r…
authored Feb 11, 2013
269
270 _mockedClientSuccessResponse.Verify(r => r.ConnectionStatus, Times.Once());
271 _mockedClientSuccessResponse.Verify(r => r.Items, Times.Once());
ba32148 @AdaTheDev Handle dodgy data files with invalid JSON contents and rationalise fi…
authored Feb 11, 2013
272 }
273
3b2b99b @AdaTheDev Check ElasticSearch client bulk response for errors and create file r…
authored Feb 11, 2013
274 [Test]
275 public void Import_ReturnsCorrectImportResultWhenElasticClientReturnsError()
ba32148 @AdaTheDev Handle dodgy data files with invalid JSON contents and rationalise fi…
authored Feb 11, 2013
276 {
3b2b99b @AdaTheDev Check ElasticSearch client bulk response for errors and create file r…
authored Feb 11, 2013
277 var failureResponse = new Mock<IBulkResponse>(MockBehavior.Strict);
278 failureResponse.Setup(r => r.ConnectionStatus).Returns(new ConnectionStatus(new Exception(TestResponseErrorMessage)));
279 InitialiseImporter(failureResponse.Object);
280 SetupFileSystem();
63406cf @AdaTheDev Parse tweet data files into Tweet data structure
authored Feb 18, 2013
281 _testTweets.Add(new Tweet{ id = 1 });
ba32148 @AdaTheDev Handle dodgy data files with invalid JSON contents and rationalise fi…
authored Feb 11, 2013
282
3b2b99b @AdaTheDev Check ElasticSearch client bulk response for errors and create file r…
authored Feb 11, 2013
283 var result = _importer.Import();
284
285 Assert.AreEqual(1, result.Files.Count(), "1 ImportFileResult expected");
286 var file = result.Files.First();
287 Assert.AreEqual("1.js", file.FileName, "Incorrect FileName");
288 Assert.AreEqual(TestResponseErrorMessage, file.ErrorMessage, "Incorrect ErrorMessage");
289 Assert.IsFalse(file.Success, "Success should be False");
290
291 failureResponse.Verify(r => r.ConnectionStatus, Times.Exactly(2));
292 }
293
ba32148 @AdaTheDev Handle dodgy data files with invalid JSON contents and rationalise fi…
authored Feb 11, 2013
294 [Test]
295 public void Import_ReturnsFailureResultForFileWithInvalidJson()
296 {
297 InitialiseImporter();
298 SetupFileSystem();
299 _mockedFileParser.Setup(p => p.GetTweets(It.IsAny<string>())).Throws(new JsonReaderException("Dummy Error"));
300
301 var result = _importer.Import();
c2eb0fc @AdaTheDev Initial drop. Functionality to extract tweet data from .js files as e…
authored Feb 10, 2013
302
ba32148 @AdaTheDev Handle dodgy data files with invalid JSON contents and rationalise fi…
authored Feb 11, 2013
303 Assert.AreEqual(1, result.Files.Count(), "1 ImportFileResult expected");
304 var file = result.Files.First();
305 Assert.IsFalse(file.Success, "Success should be false");
306 Assert.That(file.ErrorMessage.StartsWith("File contains invalid JSON"), "Unexpected error message");
c2eb0fc @AdaTheDev Initial drop. Functionality to extract tweet data from .js files as e…
authored Feb 10, 2013
307 }
308 #endregion
309 }
310 }
311 // ReSharper restore InconsistentNaming
Something went wrong with that request. Please try again.