//TODO test different formatting options: unicode, octobraille, nacb where applicable

        public byte[] TestBrailleAPI(string language, string format, int charsPerLine, int linesPerPage, string contraction, int dots)
        {
            Encoding enc             = RoboBrailleProcessor.GetEncodingByCountryCode((Language)Enum.Parse(typeof(Language), language, true));
            var      apiFileContents = enc.GetBytes(IOController.GetTestForLanguage(language));

            PageNumbering pn = PageNumbering.none;

            var brj = new BrailleJob()
            {
                Id                = Guid.NewGuid(),
                FileContent       = apiFileContents,
                UserId            = Guid.Parse("d2b97532-e8c5-e411-8270-f0def103cfd0"),
                FileExtension     = ".txt",
                FileName          = "testBraille",
                MimeType          = "plaint/text",
                Status            = JobStatus.Started,
                SubmitTime        = DateTime.Now,
                DownloadCounter   = 0,
                InputFileHash     = RoboBrailleProcessor.GetMD5Hash(apiFileContents),
                BrailleLanguage   = (Language)Enum.Parse(typeof(Language), language, true),
                BrailleFormat     = (BrailleFormat)Enum.Parse(typeof(BrailleFormat), dots.ToString(), true),
                CharactersPerLine = charsPerLine,
                LinesPerPage      = linesPerPage,
                Contraction       = (BrailleContraction)Enum.Parse(typeof(BrailleContraction), contraction, true),
                ConversionPath    = ConversionPath.texttobraille,
                OutputFormat      = (OutputFormat)Enum.Parse(typeof(OutputFormat), format, true),
                PageNumbering     = pn
            };

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

            byte[] apiRes = apiTask.Result;
            return(apiRes);
        }
Exemplo n.º 2
0
        static async Task <byte[]> WebAPICall(BrailleJob brj)
        {
            //return Encoding.ASCII.GetBytes("test");
            byte[] byteRes = null;
            BrailleJobRepository brJobRepo = new BrailleJobRepository();
            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();
                File.WriteAllBytes(resultPath + @"\api.txt", byteRes);
            }
            else
            {
                //fail
                //throw new Exception("Task with job ID: " + jobID + " failed");
            }
            return(byteRes);
        }
        public async Task <IHttpActionResult> Post(BrailleJob 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);
            }
        }
        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);
        }
Exemplo n.º 5
0
        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));
        }
Exemplo n.º 6
0
        public void TestBraille()
        {
            //setup
            Encoding enc = RoboBrailleProcessor.GetEncodingByCountryCode((Language)Enum.Parse(typeof(Language), language, true));

            resultPath = InputSourceRepository.GetTestResultDirectory() + @"\Braille-" + language + "-" + contraction + "-" + format;
            Directory.CreateDirectory(resultPath);
            InputSourceRepository isr = new InputSourceRepository();

            File.WriteAllBytes(brailleTest, enc.GetBytes(isr.GetTestForLanguage(language)));

            PageNumbering pn = PageNumbering.none;

            if (((OutputFormat)Enum.Parse(typeof(OutputFormat), format, true)).Equals(OutputFormat.Pef))
            {
                charsPerLine = new Random().Next(25, 33);
                linesPerPage = new Random().Next(25, 33);
                switch (new Random().Next(0, 2))
                {
                case 0: pn = PageNumbering.none; break;

                case 1: pn = PageNumbering.left; break;

                case 2: pn = PageNumbering.right; break;

                default: break;
                }
            }

            BrailleJob brj = new BrailleJob()
            {
                Id                = Guid.NewGuid(),
                FileContent       = File.ReadAllBytes(brailleTest),
                UserId            = Guid.Parse("d2b97532-e8c5-e411-8270-f0def103cfd0"),
                FileExtension     = ".txt",
                FileName          = "testBraille",
                MimeType          = "plaint/text",
                Status            = JobStatus.Started,
                SubmitTime        = DateTime.Now,
                DownloadCounter   = 0,
                InputFileHash     = new byte[8],
                BrailleLanguage   = (Language)Enum.Parse(typeof(Language), language, true),
                BrailleFormat     = BrailleFormat.sixdot,
                CharactersPerLine = charsPerLine,
                LinesPerPage      = linesPerPage,
                Contraction       = (BrailleContraction)Enum.Parse(typeof(BrailleContraction), contraction, true),
                ConversionPath    = ConversionPath.texttobraille,
                OutputFormat      = (OutputFormat)Enum.Parse(typeof(OutputFormat), format, true),
                PageNumbering     = pn
            };

            if (language == "daDK")
            {
                switch (contraction)
                {
                case "small": contraction = "Lille"; break;

                case "large": contraction = "Grade2"; break;

                case "full": contraction = "Grade1"; break;

                default: break;
                }
            }
            if (language == "nnNO")
            {
                switch (contraction)
                {
                case "level0": contraction = "Grade1"; break;

                case "level1": contraction = "Kortskrift1"; break;

                case "level2": contraction = "Kortskrift2"; break;

                default: break;
                }
                language = "nbNO";
            }
            string        destFile = Guid.NewGuid().ToString() + ".txt";
            SensusRequest sr       = new SensusRequest()
            {
                Process         = "Braille",
                SubProcess      = contraction,
                Option          = format,
                Language        = language.ToLowerInvariant().Insert(2, "-"),
                Prefix          = "",
                RequesterID     = "sensus-test",
                FTPServer       = "2.109.50.19",
                FTPUser         = "******",
                FTPPassword     = "******",
                SourcePath      = brailleTest,
                DestinationFile = destFile
            };

            //act
            var apiTask = Task.Run(() => WebAPICall(brj));

            //var ftpTask = Task.Run(() => FTPCall(sr));
            //Task.WaitAll(new Task[] { apiTask, ftpTask });
            byte[] apiRes = apiTask.Result;
            //byte[] ftpRes =  ftpTask.Result;

            //assert
            //NUnit.Framework.Assert.IsNotNull(ftpRes);
            //string expected = enc.GetString(ftpRes).Trim();
            NUnit.Framework.Assert.IsNotNull(apiRes);
            string result = enc.GetString(apiRes).Trim();
            //byte assertion fails because the files are not the same encoding

            //NUnit.Framework.Assert.AreEqual(ftpRes, apiRes);
            //NUnit.Framework.Assert.AreEqual(expected,result);
        }