Esempio n. 1
0
        public byte[] TestAPI(string inputFileName, string destFormat, bool hasVideo, string extension = "html", string mime = "text/html")
        {
            //setup
            MSOfficeOutput msOutput = (MSOfficeOutput)Enum.Parse(typeof(MSOfficeOutput), destFormat);

            byte[] apiFileContent = inputFiles.Where(x => x.Key.EndsWith(inputFileName)).Select(x => x.Value).First();

            MSOfficeJob OfficeJob = new MSOfficeJob()
            {
                Id              = Guid.NewGuid(),
                FileContent     = apiFileContent,
                UserId          = Guid.Parse("d2b97532-e8c5-e411-8270-f0def103cfd0"),
                FileExtension   = extension,
                FileName        = "testOfficeJob",
                MimeType        = mime,
                Status          = JobStatus.Started,
                SubmitTime      = DateTime.Now,
                DownloadCounter = 0,
                InputFileHash   = RoboBrailleProcessor.GetMD5Hash(apiFileContent),
                MSOfficeOutput  = msOutput
            };

            if (hasVideo)
            {
                //TODO maybe mock Amara call
                OfficeJob.SubtitleLangauge = "en-US";
                OfficeJob.SubtitleFormat   = "srt";
            }

            var apiTask = Task.Run(() => WebAPICall(OfficeJob));

            return(apiTask.Result);
        }
        public async Task <IHttpActionResult> Post(MSOfficeJob job)
        {
            try
            {
                Guid userId = RoboBrailleProcessor.getUserIdFromJob(this.Request.Headers.Authorization.Parameter);
                job.UserId = userId;
                Guid jobId = await _repository.SubmitWorkItem(job);

                return(Ok(jobId.ToString("D")));
            }
            catch (Exception e)
            {
                var resp = new HttpResponseMessage(HttpStatusCode.InternalServerError)
                {
                    Content      = new StringContent(string.Format("Internal error: {0}", e)),
                    ReasonPhrase = e.Message
                };
                throw new HttpResponseException(resp);
            }
        }
Esempio n. 3
0
        public async Task <byte[]> WebAPICall(MSOfficeJob OfficeJob)
        {
            byte[]             byteRes = null;
            MSOfficeRepository repo    = new MSOfficeRepository();
            Guid jobID = await repo.SubmitWorkItem(OfficeJob);

            while (repo.GetWorkStatus(jobID) == 2)
            {
                //wait
                await Task.Delay(2000);
            }
            if (repo.GetWorkStatus(jobID) == 1)
            {
                //sucess
                FileResult result = repo.GetResultContents(jobID);
                byteRes = result.getFileContents();
            }
            else
            {
                //fail
                throw new Exception("Task with job ID: " + jobID + " failed");
            }
            return(byteRes);
        }
Esempio n. 4
0
        public async Task TestPostMSOfficeConversion()
        {
            //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();

            MSOfficeJob moj = new MSOfficeJob()
            {
                Id              = Guid.NewGuid(),
                FileContent     = new byte[512],
                UserId          = Guid.Parse("d2b97532-e8c5-e411-8270-f0def103cfd0"),
                FileExtension   = ".pdf",
                FileName        = "test",
                MimeType        = "application/pdf",
                Status          = JobStatus.Started,
                SubmitTime      = DateTime.Now,
                DownloadCounter = 0,
                InputFileHash   = new byte[8],
                MSOfficeOutput  = MSOfficeOutput.txt
            };
            MSOfficeJob moj2 = new MSOfficeJob()
            {
                Id              = Guid.NewGuid(),
                FileContent     = new byte[256],
                UserId          = Guid.Parse("d2b87532-e8c5-e411-8270-f0def103cfd0"),
                FileExtension   = ".txt",
                FileName        = "test2",
                MimeType        = "text/plain",
                Status          = JobStatus.Done,
                SubmitTime      = DateTime.Now,
                DownloadCounter = 2,
                InputFileHash   = new byte[2]
            };
            var jobs = new List <MSOfficeJob> {
                moj2
            }.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);

            //TODO refine the call to various libs to be more similar

            //TODO look at the code coverage to cover more code
            //TODO Test and call all possible paths through code mock the interfaces to various libs (not necessarily that important)
            var repo    = new MSOfficeRepository(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 MSOfficeConversionController(repo);

            serviceController.Request = request;
            //call
            await serviceController.Post(moj);

            //test
            mockJobs.Verify(m => m.Add(It.IsAny <Job>()), Times.Once());
            mockContext.Verify(m => m.SaveChanges(), Times.Once());
        }