AssessmentPackageFileImporter { public AssessmentPackage importAssessmentPackageData(final File importSandboxDirectory, final MultipartFile multipartFile) throws AssessmentPackageDataImportException { Assert.notNull(importSandboxDirectory, "importSandboxDirectory"); Assert.notNull(multipartFile, "multipartFile"); AssessmentPackage assessmentPackage = null; final String contentType = ServiceUtilities.computeContentType(multipartFile); if ("application/xml".equals(contentType) || "text/xml".equals(contentType) || contentType.endsWith("+xml")) { logger.debug("Import data uses a known XML MIME type {} so saving to {} and treating as XML", contentType, importSandboxDirectory); assessmentPackage = importStandaloneXml(importSandboxDirectory, multipartFile); } else { final boolean zipSuccess = tryUnpackZipFile(importSandboxDirectory, multipartFile); if (zipSuccess) { logger.debug("Import data was successfully expanded as a ZIP file"); assessmentPackage = processUnpackedZip(importSandboxDirectory); } else { logger.warn("Import data with MIME type {} was neither a supported XML MIME type nor a ZIP file (containing at least one entry)", contentType); throw new AssessmentPackageDataImportException(ImportFailureReason.NOT_XML_OR_ZIP); } } logger.info("Successfully imported data for new {}", assessmentPackage); return assessmentPackage; }  AssessmentPackage importAssessmentPackageData(final File importSandboxDirectory,
            final MultipartFile multipartFile);  }
@Test(expected=IllegalArgumentException.class) public void nullDirectory() throws Exception { assessmentPackageFileImporter.importAssessmentPackageData(null, getThisUnitTestMultipartFile("uk/ac/ed/ph/qtiworks/samples/ims/choice.xml", "text/xml")); }
@Test public void badMix() throws Exception { final MultipartFile multipartFile = getThisUnitTestMultipartFile("uk/ac/ed/ph/qtiworks/services/BadMix.zip", "application/zip"); try { assessmentPackageFileImporter.importAssessmentPackageData(importSandboxDirectory, multipartFile); Assert.fail("Should have failed"); } catch (final AssessmentPackageDataImportException e) { Assert.assertEquals(ImportFailureReason.UNSUPPORTED_PACKAGE_CONTENTS, e.getFailure().getReason()); final List<?> failureArguments = e.getFailure().getArguments(); Assert.assertEquals(2, failureArguments.size()); Assert.assertEquals(Integer.valueOf(1), failureArguments.get(0)); Assert.assertEquals(Integer.valueOf(2), failureArguments.get(1)); } }
@Test public void externalHref() throws Exception { final MultipartFile multipartFile = getThisUnitTestMultipartFile("uk/ac/ed/ph/qtiworks/services/ExternalHref.zip", "application/zip"); try { assessmentPackageFileImporter.importAssessmentPackageData(importSandboxDirectory, multipartFile); Assert.fail("Should have failed"); } catch (final AssessmentPackageDataImportException e) { Assert.assertEquals(ImportFailureReason.HREF_OUTSIDE_PACKAGE, e.getFailure().getReason()); final List<?> failureArguments = e.getFailure().getArguments(); Assert.assertEquals(1, failureArguments.size()); Assert.assertEquals("../outside.xml", failureArguments.get(0)); } }
@Test public void missingFile() throws Exception { final MultipartFile multipartFile = getThisUnitTestMultipartFile("uk/ac/ed/ph/qtiworks/services/MissingFile.zip", "application/zip"); try { assessmentPackageFileImporter.importAssessmentPackageData(importSandboxDirectory, multipartFile); Assert.fail("Should have failed"); } catch (final AssessmentPackageDataImportException e) { Assert.assertEquals(ImportFailureReason.FILE_MISSING, e.getFailure().getReason()); final List<?> failureArguments = e.getFailure().getArguments(); Assert.assertEquals(1, failureArguments.size()); Assert.assertEquals("missing.xml", failureArguments.get(0)); } }
@Test(expected=IllegalArgumentException.class) public void nullResource() throws Exception { assessmentPackageFileImporter.importAssessmentPackageData(importSandboxDirectory, null); }
@Test public void importStandaloneItemXml() throws Exception { final MultipartFile multipartFile = getThisUnitTestMultipartFile("uk/ac/ed/ph/qtiworks/samples/ims/choice.xml", "text/xml"); final AssessmentPackage result = assessmentPackageFileImporter.importAssessmentPackageData(importSandboxDirectory, multipartFile); Assert.assertEquals(importSandboxDirectory.getPath(), result.getSandboxPath()); Assert.assertEquals(AssessmentObjectType.ASSESSMENT_ITEM, result.getAssessmentType()); Assert.assertEquals(AssessmentPackageImportType.STANDALONE_ITEM_XML, result.getImportType()); }
@Test public void importItemPackageWithGoodContentType() throws Exception { final MultipartFile multipartFile = getThisUnitTestMultipartFile("uk/ac/ed/ph/qtiworks/services/Aardvark-cannon.zip", "application/zip"); final AssessmentPackage result = assessmentPackageFileImporter.importAssessmentPackageData(importSandboxDirectory, multipartFile); Assert.assertEquals(importSandboxDirectory.getPath(), result.getSandboxPath()); Assert.assertEquals(AssessmentObjectType.ASSESSMENT_ITEM, result.getAssessmentType()); Assert.assertEquals(AssessmentPackageImportType.CONTENT_PACKAGE, result.getImportType()); }
@Test public void importItemPackageWithNullContentType() throws Exception { final MultipartFile multipartFile = getThisUnitTestMultipartFile("uk/ac/ed/ph/qtiworks/services/Aardvark-cannon.zip", null); final AssessmentPackage result = assessmentPackageFileImporter.importAssessmentPackageData(importSandboxDirectory, multipartFile); Assert.assertEquals(importSandboxDirectory.getPath(), result.getSandboxPath()); Assert.assertEquals(AssessmentObjectType.ASSESSMENT_ITEM, result.getAssessmentType()); Assert.assertEquals(AssessmentPackageImportType.CONTENT_PACKAGE, result.getImportType()); }
@Test public void importTestPackagedWithGoodContentType() throws Exception { final MultipartFile multipartFile = getThisUnitTestMultipartFile("uk/ac/ed/ph/qtiworks/services/WebDeveloperTest1.zip", "application/zip"); final AssessmentPackage result = assessmentPackageFileImporter.importAssessmentPackageData(importSandboxDirectory, multipartFile); Assert.assertEquals(importSandboxDirectory.getPath(), result.getSandboxPath()); Assert.assertEquals(AssessmentObjectType.ASSESSMENT_TEST, result.getAssessmentType()); Assert.assertEquals(AssessmentPackageImportType.CONTENT_PACKAGE, result.getImportType()); }
@Test public void notXmlOrZip() throws Exception { final MultipartFile multipartFile = getThisUnitTestMultipartFile("uk/ac/ed/ph/qtiworks/QtiWorksRuntimeException.class", "application/x-java-class"); try { assessmentPackageFileImporter.importAssessmentPackageData(importSandboxDirectory, multipartFile); Assert.fail("Should have failed"); } catch (final AssessmentPackageDataImportException e) { Assert.assertEquals(ImportFailureReason.NOT_XML_OR_ZIP, e.getFailure().getReason()); } }
@Test public void badZipIncomplete() throws Exception { final MultipartFile multipartFile = getThisUnitTestMultipartFile("uk/ac/ed/ph/qtiworks/services/WebDeveloperTest1-incomplete.zip", "application/zip"); try { assessmentPackageFileImporter.importAssessmentPackageData(importSandboxDirectory, multipartFile); Assert.fail("Should have failed"); } catch (final AssessmentPackageDataImportException e) { Assert.assertEquals(ImportFailureReason.NOT_XML_OR_ZIP, e.getFailure().getReason()); } }
@Test public void notContentPackage() throws Exception { final MultipartFile multipartFile = getThisUnitTestMultipartFile("uk/ac/ed/ph/qtiworks/services/NotContentPackage.zip", "application/zip"); try { assessmentPackageFileImporter.importAssessmentPackageData(importSandboxDirectory, multipartFile); Assert.fail("Should have failed"); } catch (final AssessmentPackageDataImportException e) { Assert.assertEquals(ImportFailureReason.NOT_CONTENT_PACKAGE, e.getFailure().getReason()); } }
@Test public void badManifest() throws Exception { final MultipartFile multipartFile = getThisUnitTestMultipartFile("uk/ac/ed/ph/qtiworks/services/BadManifest.zip", "application/zip"); try { assessmentPackageFileImporter.importAssessmentPackageData(importSandboxDirectory, multipartFile); Assert.fail("Should have failed"); } catch (final AssessmentPackageDataImportException e) { Assert.assertEquals(ImportFailureReason.BAD_IMS_MANIFEST, e.getFailure().getReason()); } }