Java tutorial
/******************************************************************************* * Educational Online Test Delivery System * Copyright (c) 2013 American Institutes for Research * * Distributed under the AIR Open Source License, Version 1.0 * See accompanying file AIR-License-1_0.txt or at * http://www.smarterapp.org/documents/American_Institutes_for_Research_Open_Source_Software_License.pdf ******************************************************************************/ package org.opentestsystem.delivery.testreg.integration; import static org.hamcrest.collection.IsCollectionWithSize.hasSize; import static org.hamcrest.core.Is.is; import static org.hamcrest.core.IsNull.notNullValue; import static org.junit.Assert.assertThat; import static org.opentestsystem.delivery.testreg.domain.FormatType.DESIGNATEDSUPPORTSANDACCOMMODATIONS; import static org.opentestsystem.delivery.testreg.domain.FormatType.DISTRICT; import static org.opentestsystem.delivery.testreg.domain.FormatType.EXPLICITELIGIBILITY; import static org.opentestsystem.delivery.testreg.domain.FormatType.GROUPOFDISTRICTS; import static org.opentestsystem.delivery.testreg.domain.FormatType.GROUPOFINSTITUTIONS; import static org.opentestsystem.delivery.testreg.domain.FormatType.GROUPOFSTATES; import static org.opentestsystem.delivery.testreg.domain.FormatType.INSTITUTION; import static org.opentestsystem.delivery.testreg.domain.FormatType.STATE; import static org.opentestsystem.delivery.testreg.domain.FormatType.STUDENT; import static org.opentestsystem.delivery.testreg.domain.FormatType.USER; import java.io.InputStream; import java.util.Arrays; import java.util.List; import java.util.Map; import javax.annotation.Resource; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; import org.opentestsystem.delivery.testreg.domain.FormatType; import org.opentestsystem.delivery.testreg.rest.FileType; import org.opentestsystem.delivery.testreg.rest.TemplateCreator; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.TestContextManager; import org.springframework.test.context.TestExecutionListeners; import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; import org.springframework.test.context.web.WebAppConfiguration; @WebAppConfiguration @RunWith(Parameterized.class) @ContextConfiguration("classpath:spring/entityfile-context.xml") @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class }) public class DownloadTemplateIntegrationTest { @Resource(name = "templateDownloadCreators") private Map<FileType, TemplateCreator> templateDownloadCreators; private FormatType formatType; private String columnString; private final TestContextManager testContextManager; private static final int HEADER_ROW = 0; public DownloadTemplateIntegrationTest(FormatType formatType, String columnString) { this.formatType = formatType; this.columnString = columnString; this.testContextManager = new TestContextManager(getClass()); } @Before public void injectDependencies() throws Exception { this.testContextManager.prepareTestInstance(this); } @Parameters public static List<?> formatVsColumns() { return Arrays.asList(new Object[][] { { STATE, "StateAbbreviation|StateName|ParentEntityType|ParentExternalId|Delete" }, { DISTRICT, "LocalEducationAgencyIdentifier|OrganizationName|NCESLEAID|ParentEntityType|ParentExternalId|StateAbbreviation|Delete" }, { INSTITUTION, "InstitutionIdentifier|NameOfInstitution|ParentEntityType|NCESInstitutionId|ParentExternalId|StateAbbreviation|Delete" }, { GROUPOFSTATES, "GroupOfStatesIdentifier|GroupOfStatesName|ParentEntityType|ParentExternalId|Delete" }, { GROUPOFDISTRICTS, "GroupOfDistrictsIdentifier|GroupOfDistrictsName|ParentEntityType|ParentExternalId|StateAbbreviation|Delete" }, { GROUPOFINSTITUTIONS, "GroupOfInstitutionsIdentifier|GroupOfInstitutionsName|ParentEntityType|ParentExternalId|StateAbbreviation|Delete" }, { USER, "FirstName|LastOrSurname|ElectronicMailAddress|TelephoneNumber|Role|AssociatedEntityID|Level|StateAbbreviation|Delete" }, { STUDENT, "StateAbbreviation|ResponsibleDistrictIdentifier|ResponsibleInstitutionIdentifier|LastOrSurname|FirstName|MiddleName|Birthdate" + "|SSID|AlternateSSID|GradeLevelWhenAssessed|Sex|HispanicOrLatinoEthnicity" + "|AmericanIndianOrAlaskaNative|Asian|BlackOrAfricanAmerican|White|NativeHawaiianOrOtherPacificIslander" + "|DemographicRaceTwoOrMoreRaces|IDEAIndicator|LEPStatus|Section504Status|EconomicDisadvantageStatus" + "|LanguageCode|EnglishLanguageProficiencyLevel|MigrantStatus|FirstEntryDateIntoUSSchool|LimitedEnglishProficiencyEntryDate" + "|LEPExitDate|TitleIIILanguageInstructionProgramType|PrimaryDisabilityType|Delete" }, { DESIGNATEDSUPPORTSANDACCOMMODATIONS, "StudentIdentifier|StateAbbreviation|Subject|AmericanSignLanguage|ColorContrast|ClosedCaptioning|Language|Masking|PermissiveMode|PrintOnDemand|PrintSize|StreamlinedInterface|TexttoSpeech|Translation|NonEmbeddedDesignatedSupports|NonEmbeddedAccommodations|Other" }, { EXPLICITELIGIBILITY, "StudentIdentifier|AssessmentAdministrationAssessmentFamily|StateAbbreviation|ResponsibleDistrictIdentifier|Subject|TestName|TestVersion|TestForm|Delete" } }); } @Test public void testDownloadTabTemplate() throws Exception { assertTextTemplate('\t', templateDownloadCreators.get(FileType.TXT).createTemplate(formatType)); } @Test public void testDownloadCSVTemplate() throws Exception { assertTextTemplate(',', templateDownloadCreators.get(FileType.CSV).createTemplate(formatType)); } @Test public void testDownloadXLSTemplate() throws Exception { assertExcelTemplate(templateDownloadCreators.get(FileType.XLS).createTemplate(formatType)); } @Test public void testDownloadXLSXTemplate() throws Exception { assertExcelTemplate(templateDownloadCreators.get(FileType.XLSX).createTemplate(formatType)); } private void assertTextTemplate(char delimiter, InputStream inpustream) throws Exception { List<String> template = IOUtils.readLines(inpustream); assertThat(template, is(notNullValue())); assertThat(template, hasSize(1)); assertThat(template.get(HEADER_ROW), is(columnString.replace('|', delimiter))); } private void assertExcelTemplate(InputStream inpustream) throws Exception { Workbook workbook = WorkbookFactory.create(inpustream); assertThat(workbook.getNumberOfSheets(), is(1)); String sheetName = new String(); if (formatType.name().length() > 32) { sheetName = formatType.name().substring(0, 31); } else { sheetName = formatType.name(); } Sheet sheet = workbook.getSheet(sheetName); assertThat(sheet, is(notNullValue())); String[] columns = StringUtils.splitByWholeSeparatorPreserveAllTokens(columnString, "|"); assertThat(sheet.getRow(HEADER_ROW).getLastCellNum(), is((short) columns.length)); int cellNo = 0; for (String columnName : columns) { assertThat(sheet.getRow(HEADER_ROW).getCell(cellNo++).getStringCellValue(), is(columnName)); } } }