public async Task GetFile_FileAlreadyExpiredBeforeNow_NotFound() { // Arrange var moq = new Mock <IFilesRepository>(); var moqFileStreamRepo = new Mock <IFileStreamRepository>(); var moqConfigRepo = new Mock <IConfigRepository>(); var moqLog = new Mock <sl.IServiceLogRepository>(); var contentString = "Test2 content"; var fileChunk = new FileChunk() { ChunkNum = 1, ChunkContent = Encoding.UTF8.GetBytes(contentString), ChunkSize = Encoding.UTF8.GetBytes(contentString).Length }; var file = new File { FileId = new Guid("22222222-2222-2222-2222-222222222222"), FileName = "Test2.txt", StoredTime = I18NHelper.DateTimeParseExactInvariant("2015-09-05T22:57:31.7824054-04:00", "o"), FileType = "application/octet-stream", FileSize = fileChunk.ChunkSize, ChunkCount = 1, ExpiredTime = DateTime.UtcNow }; var moqDbConnection = new Mock <DbConnection>(); moq.Setup(t => t.CreateConnection()).Returns(moqDbConnection.Object); moq.Setup(t => t.GetFileHead(It.IsAny <Guid>())).ReturnsAsync(file); moq.Setup(t => t.GetFileInfo(It.IsAny <Guid>())).Returns(file); moq.Setup(t => t.ReadChunkContent(moqDbConnection.Object, file.FileId, 1)).Returns(fileChunk.ChunkContent); moqConfigRepo.Setup(t => t.FileChunkSize).Returns(1 * 1024 * 1024); var controller = new FilesController(moq.Object, moqFileStreamRepo.Object, moqConfigRepo.Object, moqLog.Object) { Request = new HttpRequestMessage { RequestUri = new Uri("http://localhost/files") }, Configuration = new HttpConfiguration() }; controller.Configuration.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "files/{id}", defaults: new { id = RouteParameter.Optional }); // Act var actionResult = await controller.GetFileContent("22222222222222222222222222222222"); System.Threading.CancellationToken cancellationToken = new System.Threading.CancellationToken(); HttpResponseMessage response = await actionResult.ExecuteAsync(cancellationToken); // Assert Assert.IsTrue(response.StatusCode == HttpStatusCode.NotFound, "Expired file should return status code as NotFound"); }
public async Task Read_File_Chunks_Success() { // This tests reading file chunks and pushing them to the output stream // Arrange var moqFilesRepo = new Mock <IFilesRepository>(); var moqConfigRepo = new Mock <IConfigRepository>(); int fileChunkSize = 125; var contentString = GetRandomString(125); // set the size of the content to force two loops to retrieve total of 250 bytes byte[] fileStreamContent = Encoding.UTF8.GetBytes(contentString + contentString); var file = new File { FileId = new Guid("22222222-2222-2222-2222-222222222222"), FileName = "Test2.txt", StoredTime = I18NHelper.DateTimeParseExactInvariant("2015-09-05T22:57:31.7824054-04:00", "o"), FileType = "application/octet-stream", FileSize = fileStreamContent.Length, ChunkCount = 2 }; var moqDbConnection = new Mock <DbConnection>(); moqFilesRepo.Setup(t => t.CreateConnection()).Returns(moqDbConnection.Object); moqFilesRepo.Setup(t => t.GetFileHead(It.IsAny <Guid>())).ReturnsAsync(file); moqFilesRepo.Setup(t => t.ReadChunkContent(moqDbConnection.Object, It.IsAny <Guid>(), It.IsAny <int>())).Returns(fileStreamContent.Take <byte>(125).ToArray <byte>()); moqConfigRepo.Setup(t => t.LegacyFileChunkSize).Returns(fileChunkSize); moqFilesRepo.Setup(t => t.GetFileInfo(It.IsAny <Guid>())).Returns(file); // Act SqlPushStream sqlPushStream = new SqlPushStream(); sqlPushStream.Initialize(moqFilesRepo.Object, file.FileId); HttpContent responseContent = new PushStreamContent(sqlPushStream.WriteToStream, new MediaTypeHeaderValue(file.ContentType)); Stream response = await responseContent.ReadAsStreamAsync(); string originalContent = Encoding.UTF8.GetString(fileStreamContent); string resultContent = string.Empty; using (var memoryStream = new MemoryStream()) { response.CopyTo(memoryStream); resultContent = Encoding.UTF8.GetString(memoryStream.ToArray()); } // Assert Assert.IsTrue(originalContent.Equals(resultContent)); }
public async Task HeadFile_GetHeadForExistentFileStreamFile_Success() { // Arrange var moq = new Mock <IFilesRepository>(); var moqFileStreamRepo = new Mock <IFileStreamRepository>(); var moqConfigRepo = new Mock <IConfigRepository>(); var moqLog = new Mock <sl.IServiceLogRepository>(); var file = new File { FileId = new Guid("33333333-3333-3333-3333-333333333333"), FileName = "Test3.txt", StoredTime = I18NHelper.DateTimeParseExactInvariant("2015-09-05T22:57:31.7824054-04:00", "o"), FileType = "text/html", ChunkCount = 1, IsLegacyFile = true }; moq.Setup(t => t.GetFileHead(It.IsAny <Guid>())).ReturnsAsync((File)null); moqFileStreamRepo.Setup(t => t.FileExists(file.FileId)).Returns(true); moqFileStreamRepo.Setup(t => t.GetFileHead(file.FileId)).Returns(file); var controller = new FilesController(moq.Object, moqFileStreamRepo.Object, moqConfigRepo.Object, moqLog.Object) { Request = new HttpRequestMessage { RequestUri = new Uri("http://localhost/files"), Method = HttpMethod.Head }, Configuration = new HttpConfiguration() }; controller.Configuration.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "files/{id}", defaults: new { id = RouteParameter.Optional }); // Act var actionResult = await controller.GetFileHead("33333333333333333333333333333333"); System.Threading.CancellationToken cancellationToken = new System.Threading.CancellationToken(); HttpResponseMessage response = await actionResult.ExecuteAsync(cancellationToken); var content = response.Content; var contentType = content.Headers.ContentType; var fileName = content.Headers.ContentDisposition.FileName; var storedTime = response.Headers.GetValues("Stored-Date"); // Assert Assert.IsTrue(response.IsSuccessStatusCode); Assert.AreEqual("Test3.txt", fileName); Assert.AreEqual("application/octet-stream", contentType.MediaType, string.Format("Returned content type {0} does not match expected {1}", contentType, "text/html")); Assert.AreEqual(DateTime.Parse("2015-09-05T22:57:31.7824054-04:00"), DateTime.Parse(storedTime.First())); }
public async Task GetFile_ProperRequestFileStream_Success() { // Arrange var moq = new Mock <IFilesRepository>(); var moqFileStreamRepo = new Mock <IFileStreamRepository>(); var moqConfigRepo = new Mock <IConfigRepository>(); var moqLog = new Mock <sl.IServiceLogRepository>(); var contentString = "Test2 content"; var fileChunk = new FileChunk() { ChunkNum = 1, ChunkContent = Encoding.UTF8.GetBytes(contentString), ChunkSize = Encoding.UTF8.GetBytes(contentString).Length }; var file = new File { FileId = new Guid("22222222-2222-2222-2222-222222222222"), FileName = "Test2.txt", StoredTime = I18NHelper.DateTimeParseExactInvariant("2015-09-05T22:57:31.7824054-04:00", "o"), FileType = "application/octet-stream", FileSize = fileChunk.ChunkSize, ChunkCount = 1, IsLegacyFile = true }; var moqDbConnection = new Mock <DbConnection>(); moqConfigRepo.Setup(t => t.FileChunkSize).Returns(1 * 1024 * 1024); moqConfigRepo.Setup(t => t.LegacyFileChunkSize).Returns(1); moq.Setup(t => t.GetFileHead(It.IsAny <Guid>())).ReturnsAsync((File)null); moq.Setup(t => t.GetFileInfo(It.IsAny <Guid>())).Returns(file); moq.Setup(t => t.ReadChunkContent(moqDbConnection.Object, file.FileId, 1)).Returns(fileChunk.ChunkContent); moqFileStreamRepo.Setup(t => t.CreateConnection()).Returns(moqDbConnection.Object); moqFileStreamRepo.Setup(t => t.FileExists(It.IsAny <Guid>())).Returns(true); moqFileStreamRepo.Setup(t => t.GetFileHead(It.IsAny <Guid>())).Returns(file); moqFileStreamRepo.Setup(t => t.ReadChunkContent(moqDbConnection.Object, file.FileId, 1, 0)).Returns(fileChunk.ChunkContent); var controller = new FilesController(moq.Object, moqFileStreamRepo.Object, moqConfigRepo.Object, moqLog.Object) { Request = new HttpRequestMessage { RequestUri = new Uri("http://localhost/files") }, Configuration = new HttpConfiguration() }; controller.Configuration.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "files/{id}", defaults: new { id = RouteParameter.Optional }); // Act var actionResult = await controller.GetFileContent("22222222222222222222222222222222"); System.Threading.CancellationToken cancellationToken = new System.Threading.CancellationToken(); HttpResponseMessage response = await actionResult.ExecuteAsync(cancellationToken); var content = response.Content; var contentType = content.Headers.ContentType; var fileName = content.Headers.ContentDisposition.FileName; var storedTime = response.Headers.GetValues("Stored-Date"); var fileContent = await content.ReadAsStringAsync(); // Assert Assert.IsTrue(response.IsSuccessStatusCode); Assert.AreEqual("Test2.txt", fileName); Assert.AreEqual("application/octet-stream", contentType.MediaType); Assert.AreEqual("Test2 content", fileContent, "Improper content was returned"); Assert.AreEqual(DateTime.Parse("2015-09-05T22:57:31.7824054-04:00"), DateTime.Parse(storedTime.First())); }