public void GivenDataExtractModelWithSelectedCustomFields_WhenRetrieveStudentsList_ThenCustomFieldsIncluded()
        {
            StudentProfileExportModel model = new StudentProfileExportModel { SelectedCustomFieldIds = EducationContext.CustomFields.Select(f => f.Id) };

            var result = Target.RetrieveStudentsList(model);

            Assert.IsTrue(result.Any(r => r.CustomFieldValues.Count > 0));
        }
        public void GivenDefaultModel_WhenRetrieveStudentsList_ThenApprovedProvidersIncluded()
        {
            var defaultModel = new StudentProfileExportModel();

            var actual = Target.RetrieveStudentsList(defaultModel);

            Assert.IsTrue(actual.Any(s => s.ApprovedProviders.Count > 0));
        }
        public void GivenDefaultModel_WhenRetrieveStudentsList_ThenStudentAssignedOfferingsDataIncluded()
        {
            var defaultModel = new StudentProfileExportModel();

            var actual = Target.RetrieveStudentsList(defaultModel);

            Assert.IsTrue(actual.Any(s => s.StudentAssignedOfferings.Where(a => a.ServiceOffering.Program != null && a.ServiceOffering.Provider != null && a.ServiceOffering.ServiceType != null).Count() > 0));
        }
        public void GivenModel_WhenGenerateStudentProfileExport_ThenFileResultReturned()
        {
            StudentProfileExportModel model = new StudentProfileExportModel();
            string templatePath = Path.Combine(UploadTemplateFolderPath, DataFileController.TemplateFile);
            MockLogicManager.Expect(m => m.GenerateStudentProfileExport(User, model, templatePath)).Return(new MemoryStream());

            var result = Target.StudentProfileExport(model) as FileStreamResult;

            Assert.IsNotNull(result);
        }
        public void GivenModel_WhenGenerateStudentProfileExport_ThenFileResultUsesValidContentType()
        {
            StudentProfileExportModel model = new StudentProfileExportModel();
            string templatePath = Path.Combine(UploadTemplateFolderPath, DataFileController.TemplateFile);
            Stream expected = new MemoryStream();
            MockLogicManager.Expect(m => m.GenerateStudentProfileExport(User, model, templatePath)).Return(expected);

            var result = Target.StudentProfileExport(model) as FileStreamResult;

            Assert.AreEqual(ExcelWriter.ContentType, result.ContentType);
        }
 public ActionResult StudentProfileExport(StudentProfileExportModel model)
 {
     try
     {
         LogicManager.CheckStudentCount(model, 65000);
     }
     catch(ArgumentOutOfRangeException)
     {
         ModelState.AddModelError("RowCount", "Export will generate more than the maximum amount of records");
         return PartialView(model);
     }
     MemoryStream stream = LogicManager.GenerateStudentProfileExport((EducationSecurityPrincipal)User, model, TemplatePath) as MemoryStream;
     stream.Position = 0;
     return File(stream, ExcelWriter.ContentType);
 }
 public void CheckStudentCount(StudentProfileExportModel model, int studentCountLimit)
 {
     if (StudentRepository.Items.Where(st => (model.SelectedSchoolIds.Count() == 0 ||
                                              model.SelectedSchoolIds.Contains(st.SchoolId))
                                          && (model.SelectedGrades.Count() == 0 ||
                                              model.SelectedGrades.Contains(st.Grade))).Count() > studentCountLimit)
     {
         throw new ArgumentOutOfRangeException("Result set to be returned will be higher than the file size limits will allow.");
     }
 }
 public Stream GenerateStudentProfileExport(EducationSecurityPrincipal user, StudentProfileExportModel model, string templatePath)
 {
     var students = RetrieveStudentsList(model);
     var configuration = new StudentProfileExportFieldDescriptor
     {
         BirthDateIncluded = model.BirthDateIncluded,
         ParentNameIncluded = model.ParentNameIncluded,
         SelectedServiceTypes = ServiceTypeRepository.Items.Where(t => model.SelectedServiceTypeIds.Contains(t.Id)).ToList()
     };
     List<CustomField> displayFields = new List<CustomField>();
     foreach (var field in CustomFieldRepository.Items.Where(f => model.SelectedCustomFieldIds.Contains(f.Id)))
     {
         IPermission permission = PermissionFactory.Current.Create("StudentProfileExportCustomFieldData", field);
         if (permission.TryGrantAccess(user))
         {
             displayFields.Add(field);
         }
     }
     configuration.SelectedCustomFields = displayFields;
     byte[] templateData = File.ReadAllBytes(templatePath);
     MemoryStream stream = new MemoryStream();
     stream.Write(templateData, 0, (int)templateData.Length);
     using (IExportFile export = ExportFileFactory.Current.Create(typeof(StudentProfileExportFile)))
     {
         var mapper = export.GenerateMapper();
         List<IEnumerable<object>> data = new List<IEnumerable<object>>();
         foreach (var student in students)
         {
             data.Add(mapper.MapData(configuration, student, user, Auditor));
         }
         export.Create(stream);
         export.SetupColumnHeaders(mapper.MapColumnHeadings(configuration));
         export.FillData(data);
         export.SetupFooter(CloudConfigurationManager.GetSetting("StudentProfileExportFooter"));
         RepositoryContainer.Save();
     }
     return stream;
 }
        public void GivenStudentHasOptOut_AndUserIsDataAdmin_WhenGenerateStudentProfileExport_ThenMapDataCalled()
        {
            StudentProfileExportModel model = new StudentProfileExportModel();
            IExportFile mockExportFile = MockRepository.GenerateMock<IExportFile>();
            IExportDataMapper mockDataMapper = MockRepository.GenerateMock<IExportDataMapper>();
            List<IEnumerable<object>> data = new List<IEnumerable<object>>();
            byte[] byteArray = File.ReadAllBytes(@"TestData\StudentProfileExportTemplate.xltx");
            MemoryStream stream = new MemoryStream(byteArray);
            var students = Target.RetrieveStudentsList(model);
            foreach (var student in students)
            {
                student.HasParentalOptOut = true;
                mockDataMapper.Expect(m => m.MapData(Arg<StudentProfileExportFieldDescriptor>.Matches(a => AssertDescriptorMatches(model, a)), Arg.Is(student), Arg.Is(User), Arg.Is(MockUserAuditor))).Return(new List<object>());
            }
            List<string> colHeadings = new List<string>();
            mockDataMapper.Expect(m => m.MapColumnHeadings(Arg<StudentProfileExportFieldDescriptor>.Matches(a => AssertDescriptorMatches(model, a)))).Return(colHeadings);
            mockExportFile.Expect(m => m.GenerateMapper()).Return(mockDataMapper);
            ExportFileFactory.Current.Expect(m => m.Create(typeof(StudentProfileExportFile))).Return(mockExportFile);
            User.Identity.User.UserRoles.Add(new UserRole { Role = new Role { Name = SecurityRoles.DataAdmin } });

            var result = Target.GenerateStudentProfileExport(User, model, @"TestData\StudentProfileExportTemplate.xltx") as Stream;

            foreach (var student in students)
            {
                mockDataMapper.AssertWasCalled(m => m.MapData(Arg<StudentProfileExportFieldDescriptor>.Matches(a => AssertDescriptorMatches(model, a)), Arg.Is(student), Arg.Is(User), Arg.Is(MockUserAuditor)));
            }
        }
        public void GivenDataExtractModel_WhenRetrieveStudentsList_ThenCollectionOfStudentsReturned()
        {
            StudentProfileExportModel model = new StudentProfileExportModel();

            var result = Target.RetrieveStudentsList(model);

            Assert.IsNotNull(result);
        }
 private bool AssertDescriptorMatches(StudentProfileExportModel expectedState, StudentProfileExportFieldDescriptor actualState)
 {
     Assert.AreEqual(expectedState.BirthDateIncluded, actualState.BirthDateIncluded);
     Assert.AreEqual(expectedState.ParentNameIncluded, actualState.ParentNameIncluded);
     CollectionAssert.AreEqual(Repositories.MockCustomFieldRepository.Items.Where(c => expectedState.SelectedCustomFieldIds.Contains(c.Id)).ToList(), actualState.SelectedCustomFields.ToList());
     CollectionAssert.AreEqual(Repositories.MockServiceTypeRepository.Items.Where(c => expectedState.SelectedServiceTypeIds.Contains(c.Id)).ToList(), actualState.SelectedServiceTypes.ToList());
     return true;
 }
        public void GivenSelectedSchools_WhenRetrieveStudentsList_ThenCollectionFilteredBySchools()
        {
            List<School> selectedSchools = new List<School> { Data.Schools[0], Data.Schools[1] };
            var expected = Data.Students.Where(s => selectedSchools.Contains(s.School));
            StudentProfileExportModel model = new StudentProfileExportModel { SelectedSchoolIds = selectedSchools.Select(s => s.Id) };

            var actual = Target.RetrieveStudentsList(model);

            CollectionAssert.AreEqual(expected.Select(e => e.Id).ToList(), actual.Select(a => a.Id).ToList());
        }
        public void GivenModel_AndCustomFieldSelected_WhenGenerateStudentProfileExport_ThenFileContainsCustomFieldValue()
        {
            StudentProfileExportModel model = new StudentProfileExportModel
            {
                SelectedCustomFieldIds = new List<int> { EducationContext.CustomFields.Where(c => c.Name == "Tardies").Single().Id },
                SelectedSchoolIds = new List<int> { EducationContext.Schools.First().Id },
                SelectedGrades = new List<int> { 10, 12 }
            };
            string outputPath = Path.ChangeExtension(Path.Combine("TestData", MethodBase.GetCurrentMethod().Name), ".xlsx");

            var result = Target.GenerateStudentProfileExport(User, model, @"TestData\StudentProfileExportTemplate.xltx") as MemoryStream;

            File.WriteAllBytes(outputPath, result.ToArray());
            using (var workbook = new XLWorkbook(outputPath))
            {
                IXLWorksheet worksheet = workbook.Worksheet(1);
                Assert.AreEqual("1200", worksheet.Cell("E3").Value);
            }
        }
        public void GivenModel_AndServiceTypeSelected_WhenGenerateStudentProfileExport_ThenFileContainsServiceTypeValue()
        {
            StudentProfileExportModel model = new StudentProfileExportModel
            {
                SelectedServiceTypeIds = new List<int> { 2 },
                SelectedSchoolIds = new List<int> { EducationContext.Schools.First().Id },
                SelectedGrades = new List<int> { 10, 12 }
            };
            string outputPath = Path.ChangeExtension(Path.Combine("TestData", MethodBase.GetCurrentMethod().Name), ".xlsx");

            var result = Target.GenerateStudentProfileExport(User, model, @"TestData\StudentProfileExportTemplate.xltx") as MemoryStream;

            File.WriteAllBytes(outputPath, result.ToArray());
            using (var workbook = new XLWorkbook(outputPath))
            {
                IXLWorksheet worksheet = workbook.Worksheet(1);
                Assert.AreEqual("Big Brothers, Big Sisters Provide College Access/One on One Activities", worksheet.Cell("E3").Value);
            }
        }
        public void GivenModel_AndIExportFile_WhenGenerateStudentProfileExport_ThenStreamReturned()
        {
            StudentProfileExportModel model = new StudentProfileExportModel();
            IExportFile mockExportFile = MockRepository.GenerateMock<IExportFile>();
            IExportDataMapper mockDataMapper = MockRepository.GenerateMock<IExportDataMapper>();
            List<IEnumerable<object>> data = new List<IEnumerable<object>>();
            byte[] byteArray = File.ReadAllBytes(@"TestData\StudentProfileExportTemplate.xltx");
            MemoryStream stream = new MemoryStream(byteArray);
            var students = Target.RetrieveStudentsList(model);
            foreach (var student in students)
            {
                mockDataMapper.Expect(m => m.MapData(model, student, User, MockUserAuditor)).Return(new List<object>());
            }
            List<string> colHeadings = new List<string>();
            mockDataMapper.Expect(m => m.MapColumnHeadings(model)).Return(colHeadings);
            mockExportFile.Expect(m => m.GenerateMapper()).Return(mockDataMapper);
            ExportFileFactory.Current.Expect(m => m.Create(typeof(StudentProfileExportFile))).Return(mockExportFile);

            var result = Target.GenerateStudentProfileExport(User, model, @"TestData\StudentProfileExportTemplate.xltx") as Stream;

            Assert.IsNotNull(result);
        }
        public void GivenDataExtractModelWithSelectedServiceTypes_WhenRetreiveStudentsList_ThenStudentAssignedOfferingsIncluded()
        {
            StudentProfileExportModel model = new StudentProfileExportModel { SelectedServiceTypeIds = EducationContext.ServiceTypes.Select(t => t.Id) };

            var result = Target.RetrieveStudentsList(model);

            Assert.IsTrue(result.First().StudentAssignedOfferings.Count() > 0);
        }
        public void GivenModel_AndIExportFile_WhenGenerateStudentProfileExport_ThenFillDataCalled()
        {
            StudentProfileExportModel model = new StudentProfileExportModel();
            IExportFile mockExportFile = MockRepository.GenerateMock<IExportFile>();
            IExportDataMapper mockDataMapper = MockRepository.GenerateMock<IExportDataMapper>();
            List<IEnumerable<object>> data = new List<IEnumerable<object>>();
            byte[] byteArray = File.ReadAllBytes(@"TestData\StudentProfileExportTemplate.xltx");
            MemoryStream stream = new MemoryStream(byteArray);
            var students = Target.RetrieveStudentsList(model);
            var studentFieldData = new Dictionary<Student, IEnumerable<object>>();
            foreach (var student in students)
            {
                IEnumerable<object> rowData = new List<object>();
                mockDataMapper.Expect(m => m.MapData(Arg<StudentProfileExportFieldDescriptor>.Matches(a => AssertDescriptorMatches(model, a)), Arg.Is(student), Arg.Is(User), Arg.Is(MockUserAuditor))).Return(rowData);
                studentFieldData.Add(student, rowData);
            }
            List<string> colHeadings = new List<string>();
            mockDataMapper.Expect(m => m.MapColumnHeadings(Arg<StudentProfileExportFieldDescriptor>.Matches(a => AssertDescriptorMatches(model, a)))).Return(colHeadings);
            mockExportFile.Expect(m => m.GenerateMapper()).Return(mockDataMapper);
            ExportFileFactory.Current.Expect(m => m.Create(typeof(StudentProfileExportFile))).Return(mockExportFile);

            var result = Target.GenerateStudentProfileExport(User, model, @"TestData\StudentProfileExportTemplate.xltx") as Stream;

            foreach (var student in students)
            {
                mockDataMapper.AssertWasCalled(m => m.MapData(Arg<StudentProfileExportFieldDescriptor>.Matches(a => AssertDescriptorMatches(model, a)), Arg.Is(student), Arg.Is(User), Arg.Is(MockUserAuditor)));
            }
            mockExportFile.AssertWasCalled(m => m.FillData(Arg<IEnumerable<IEnumerable<object>>>.Matches(a => AssertDataMatches(studentFieldData, a))));
        }
        public void GivenModel_AndIExportFile_AndUserDoesntHaveAccessToAllPrivateHealthFields_WhenGenerateStudentProfileExport_ThenMapColumnHeadingsCalledWithCorrectColumnHeaders()
        {
            StudentProfileExportModel model = new StudentProfileExportModel { SelectedCustomFieldIds = new List<int> { 1, 2 } };
            StudentProfileExportModel expectedModel = new StudentProfileExportModel { SelectedCustomFieldIds = new List<int> { 1 } };
            PermissionFactory.Current.Expect(m => m.Create("StudentProfileExportCustomFieldData", Data.CustomFields.Single(c => c.Id == 1))).Return(MockRepository.GenerateMock<IPermission>());
            IPermission permission = MockRepository.GenerateMock<IPermission>();
            permission.Expect(p => p.GrantAccess(User)).Throw(new EntityAccessUnauthorizedException());
            PermissionFactory.Current.Expect(m => m.Create("StudentProfileExportCustomFieldData", Data.CustomFields.Single(c => c.Id == 2))).Return(permission);
            IExportFile mockExportFile = MockRepository.GenerateMock<IExportFile>();
            IExportDataMapper mockDataMapper = MockRepository.GenerateMock<IExportDataMapper>();
            List<IEnumerable<object>> data = new List<IEnumerable<object>>();
            byte[] byteArray = File.ReadAllBytes(@"TestData\StudentProfileExportTemplate.xltx");
            MemoryStream stream = new MemoryStream(byteArray);
            var students = Target.RetrieveStudentsList(model);
            foreach (var student in students)
            {
                mockDataMapper.Expect(m => m.MapData(Arg<StudentProfileExportFieldDescriptor>.Matches(a => AssertDescriptorMatches(expectedModel, a)), Arg.Is(student), Arg.Is(User), Arg.Is(MockUserAuditor))).Return(new List<object>());
            }
            List<string> colHeadings = new List<string>();
            mockDataMapper.Expect(m => m.MapColumnHeadings(Arg<StudentProfileExportFieldDescriptor>.Matches(a => AssertDescriptorMatches(expectedModel, a)))).Return(colHeadings);
            mockExportFile.Expect(m => m.GenerateMapper()).Return(mockDataMapper);
            ExportFileFactory.Current.Expect(m => m.Create(typeof(StudentProfileExportFile))).Return(mockExportFile);

            var result = Target.GenerateStudentProfileExport(User, model, @"TestData\StudentProfileExportTemplate.xltx") as Stream;

            mockDataMapper.AssertWasCalled(m => m.MapColumnHeadings(Arg<StudentProfileExportFieldDescriptor>.Matches(a => AssertDescriptorMatches(expectedModel, a))));
        }
        public void GivenModelWillGenerateMoreResultsThanFileSizeLimit_WhenCheckStudentCount_ThenThrowException()
        {
            StudentProfileExportModel model = new StudentProfileExportModel();

            Target.ExpectException<ArgumentOutOfRangeException>(() => Target.CheckStudentCount(model, 2));
        }
 public IEnumerable<Student> RetrieveStudentsList(StudentProfileExportModel model)
 {
     IQueryable<Student> students = StudentRepository.Items.
                                                      Include(s => s.School).
                                                      Include(s => s.ApprovedProviders).
                                                      Include("CustomFieldValues.CustomDataOrigin").
                                                      Include("StudentAssignedOfferings.ServiceOffering.ServiceType").
                                                      Include("StudentAssignedOfferings.ServiceOffering.Provider").
                                                      Include("StudentAssignedOfferings.ServiceOffering.Program");
     return students.Where(st => (!model.SelectedSchoolIds.Any() || model.SelectedSchoolIds.Contains(st.SchoolId))
                                 &&
                                 (!model.SelectedGrades.Any() || model.SelectedGrades.Contains(st.Grade)));
 }
        public void GivenSelectedGrades_WhenRetrieveStudentsList_ThenCollectionFilteredByGrade()
        {
            var expected = Data.Students.Where(s => s.Grade == 9 || s.Grade == 11);
            StudentProfileExportModel model = new StudentProfileExportModel { SelectedGrades = new List<int> { 9, 11 } };

            var actual = Target.RetrieveStudentsList(model);

            CollectionAssert.AreEqual(expected.Select(e => e.Id).ToList(), actual.Select(a => a.Id).ToList());
        }
        public void GivenModel_WhenGenerateStudentProfileExport_ThenStreamReturnedContainsExcelFile()
        {
            StudentProfileExportModel model = new StudentProfileExportModel
            {
                SelectedCustomFieldIds = new List<int> { EducationContext.CustomFields.First().Id },
                SelectedServiceTypeIds = new List<int> { EducationContext.ServiceTypes.First().Id },
                SelectedSchoolIds = new List<int> { EducationContext.Schools.First().Id },
                SelectedGrades = new List<int> { 10, 12 }
            };
            string outputPath = Path.ChangeExtension(Path.Combine("TestData", MethodBase.GetCurrentMethod().Name), ".xlsx");

            var result = Target.GenerateStudentProfileExport(User, model, @"TestData\StudentProfileExportTemplate.xltx") as MemoryStream;

            Assert.IsNotNull(result);
            File.WriteAllBytes(outputPath, result.ToArray());
            using (var workbook = new XLWorkbook(outputPath))
            {
                Assert.IsNotNull(workbook);
            }
        }
        public void GivenSelectedSchools_AndSelectedGrades_WhenRetrieveStudentsList_ThenFilteredOnBoth()
        {
            List<School> selectedSchools = new List<School> { Data.Schools[0], Data.Schools[1] };
            var expected = Data.Students.Where(s => selectedSchools.Contains(s.School) && (s.Grade == 9 || s.Grade == 11));
            StudentProfileExportModel model = new StudentProfileExportModel { SelectedSchoolIds = selectedSchools.Select(s => s.Id), SelectedGrades = new List<int> { 9, 11 } };

            var actual = Target.RetrieveStudentsList(model);

            CollectionAssert.AreEqual(expected.Select(e => e.Id).ToList(), actual.Select(a => a.Id).ToList());
        }
        public void GivenModel_AndStudentCountLimit_WhenCheckStudentCount_ThenSucceed()
        {
            StudentProfileExportModel model = new StudentProfileExportModel();

            Target.CheckStudentCount(model, 20);
        }
        public void GivenModel_AndAllFieldsSelected_AndAllSchoolsSelected_WhenGenerateStudentProfileExport_ThenFileContainsServiceTypeValue()
        {
            StudentProfileExportModel model = new StudentProfileExportModel
            {
                SelectedServiceTypeIds = EducationContext.ServiceTypes.Select(t => t.Id).ToList(),
                SelectedSchoolIds = EducationContext.Schools.Select(s => s.Id).ToList(),
                SelectedCustomFieldIds = EducationContext.CustomFields.Select(f => f.Id).ToList(),
                BirthDateIncluded = true,
                ParentNameIncluded = true
            };
            string outputPath = Path.ChangeExtension(Path.Combine("TestData", MethodBase.GetCurrentMethod().Name), ".xlsx");

            var result = Target.GenerateStudentProfileExport(User, model, @"TestData\StudentProfileExportTemplate.xltx") as MemoryStream;

            File.WriteAllBytes(outputPath, result.ToArray());
            using (var workbook = new XLWorkbook(outputPath))
            {
                Assert.IsNotNull(workbook);
            }
        }
        public void GivenTooManyStudents_WhenGenerateStudentProfileExport_ThenPartialViewResultReturned()
        {
            StudentProfileExportModel model = new StudentProfileExportModel();
            MockLogicManager.Expect(m => m.CheckStudentCount(model, 65000)).Throw(new ArgumentOutOfRangeException("blah"));

            var result = Target.StudentProfileExport(model) as PartialViewResult;

            Assert.IsNotNull(result);
        }
        public void GivenNoSelectedSchools_AndNoSelectedGrades_WhenRetrieveStudentsList_ThenAllStudentsReturned()
        {
            var expected = Data.Students;
            StudentProfileExportModel model = new StudentProfileExportModel();

            var actual = Target.RetrieveStudentsList(model);

            CollectionAssert.AreEqual(expected.Select(e => e.Id).ToList(), actual.Select(a => a.Id).ToList());
        }