static async Task <byte[]> WebAPICall(BrailleJob brj) { byte[] byteRes = null; var brJobRepo = new BrailleRepository(); Guid jobID = await brJobRepo.SubmitWorkItem(brj); while (brJobRepo.GetWorkStatus(jobID) == 2) { //wait await Task.Delay(200); } if (brJobRepo.GetWorkStatus(jobID) == 1) { //sucess FileResult result = brJobRepo.GetResultContents(jobID); byteRes = result.getFileContents(); } else { //fail } return(byteRes); }
public async Task TestPostSensusBraille() { //init var mockJobs = new Mock <DbSet <Job> >(); var mockServiceUsers = new Mock <DbSet <ServiceUser> >(); var mockContext = new Mock <RoboBrailleDataContext>(); // arrange var users = new List <ServiceUser> { new ServiceUser { EmailAddress = "*****@*****.**", UserId = Guid.Parse("d2b97532-e8c5-e411-8270-f0def103cfd0"), ApiKey = Encoding.UTF8.GetBytes("7b76ae41-def3-e411-8030-0c8bfd2336cd"), FromDate = new DateTime(2015, 1, 1), ToDate = new DateTime(2020, 1, 1), UserName = "******", Jobs = null } }.AsQueryable(); BrailleJob brj = new BrailleJob() { Id = Guid.NewGuid(), FileContent = Encoding.GetEncoding(1252).GetBytes("Braille job one"), UserId = Guid.Parse("d2b97532-e8c5-e411-8270-f0def103cfd0"), FileExtension = ".txt", FileName = "test1", MimeType = "text/plain", Status = JobStatus.Started, SubmitTime = DateTime.Now, DownloadCounter = 0, InputFileHash = new byte[8], BrailleLanguage = Language.enGB, BrailleFormat = BrailleFormat.sixdot, CharactersPerLine = 20, LinesPerPage = 20, Contraction = BrailleContraction.grade1, ConversionPath = ConversionPath.texttobraille, OutputFormat = OutputFormat.Unicode }; BrailleJob brj2 = new BrailleJob() { Id = Guid.NewGuid(), FileContent = Encoding.GetEncoding(1252).GetBytes("Braille job two"), UserId = Guid.Parse("d2b87532-e8c5-e411-8270-f0def103cfd0"), FileExtension = ".txt", FileName = "test2", MimeType = "text/plain", Status = JobStatus.Started, SubmitTime = DateTime.Now, DownloadCounter = 2, InputFileHash = new byte[2], BrailleLanguage = Language.enGB, BrailleFormat = BrailleFormat.sixdot, CharactersPerLine = 20, LinesPerPage = 20, Contraction = BrailleContraction.grade1, ConversionPath = ConversionPath.texttobraille, OutputFormat = OutputFormat.Unicode }; var jobs = new List <BrailleJob> { brj2 }.AsQueryable(); mockJobs.As <IDbAsyncEnumerable <Job> >().Setup(m => m.GetAsyncEnumerator()).Returns(new TestDbAsyncEnumerator <Job>(jobs.GetEnumerator())); mockJobs.As <IQueryable <Job> >().Setup(m => m.Provider).Returns(new TestDbAsyncQueryProvider <Job>(jobs.Provider)); mockJobs.As <IQueryable <Job> >().Setup(m => m.Expression).Returns(jobs.Expression); mockJobs.As <IQueryable <Job> >().Setup(m => m.ElementType).Returns(jobs.ElementType); mockJobs.As <IQueryable <Job> >().Setup(m => m.GetEnumerator()).Returns(jobs.GetEnumerator()); mockServiceUsers.As <IDbAsyncEnumerable <ServiceUser> >().Setup(m => m.GetAsyncEnumerator()).Returns(new TestDbAsyncEnumerator <ServiceUser>(users.GetEnumerator())); mockServiceUsers.As <IQueryable <ServiceUser> >().Setup(m => m.Provider).Returns(new TestDbAsyncQueryProvider <ServiceUser>(users.Provider)); mockServiceUsers.As <IQueryable <ServiceUser> >().Setup(m => m.Expression).Returns(users.Expression); mockServiceUsers.As <IQueryable <ServiceUser> >().Setup(m => m.ElementType).Returns(users.ElementType); mockServiceUsers.As <IQueryable <ServiceUser> >().Setup(m => m.GetEnumerator()).Returns(users.GetEnumerator()); mockContext.Setup(m => m.Jobs).Returns(mockJobs.Object); mockContext.Setup(m => m.ServiceUsers).Returns(mockServiceUsers.Object); var repo = new BrailleRepository(mockContext.Object); var request = new HttpRequestMessage(); request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Authorization", "Hawk id=\"d2b97532-e8c5-e411-8270-f0def103cfd0\", ts=\"1470657024\", nonce=\"VkcMGB\", mac=\"hXW+BLRoqwlUaQZQtpPToOWnVAh5KbAXGGT5f8dLMVk=\""); var serviceController = new BrailleController(repo) { Request = request }; //call await serviceController.Post(brj); //test mockJobs.Verify(m => m.Add(It.IsAny <Job>()), Times.Once()); mockContext.Verify(m => m.SaveChanges(), Times.Exactly(1)); //twice if it is synced //mockContext.Verify(m => m.SaveChanges(), Times.Exactly(2)); }