001/* 002 * The contents of this file are subject to the license and copyright detailed 003 * in the LICENSE and NOTICE files at the root of the source tree. 004 */ 005package org.duraspace.bagit; 006 007import static org.assertj.core.api.Assertions.assertThat; 008import static org.duraspace.bagit.SerializationSupport.APPLICATION_GTAR; 009import static org.duraspace.bagit.SerializationSupport.APPLICATION_GZIP; 010import static org.duraspace.bagit.SerializationSupport.APPLICATION_TAR; 011import static org.duraspace.bagit.SerializationSupport.APPLICATION_X_COMPRESSED_TAR; 012import static org.duraspace.bagit.SerializationSupport.APPLICATION_X_GTAR; 013import static org.duraspace.bagit.SerializationSupport.APPLICATION_X_GZIP; 014import static org.duraspace.bagit.SerializationSupport.APPLICATION_X_TAR; 015import static org.duraspace.bagit.SerializationSupport.APPLICATION_ZIP; 016 017import java.io.IOException; 018import java.net.URL; 019import java.nio.file.Files; 020import java.nio.file.Path; 021import java.nio.file.Paths; 022import java.util.Map; 023 024import org.junit.Test; 025 026/** 027 * Verify our type mappings are what we expect 028 * 029 * all zip types map to application/zip 030 * all tar types map to application/tar 031 * all gzip types map to application/gzip 032 * 033 * @author mikejritter 034 */ 035public class SerializationSupportTest { 036 037 private final Map<String, String> commonTypeMap = SerializationSupport.getCommonTypeMap(); 038 039 @Test 040 public void testZipMappings() { 041 final String zipIdentifier = "zip"; 042 043 assertThat(commonTypeMap).hasEntrySatisfying(zipIdentifier, 044 value -> assertThat(value).isEqualTo(APPLICATION_ZIP)); 045 assertThat(commonTypeMap).hasEntrySatisfying(APPLICATION_ZIP, 046 value -> assertThat(value).isEqualTo(APPLICATION_ZIP)); 047 } 048 049 @Test 050 public void testTarMappings() { 051 final String tarIdentifier = "tar"; 052 053 assertThat(commonTypeMap).hasEntrySatisfying(tarIdentifier, 054 value -> assertThat(value).isEqualTo(APPLICATION_TAR)); 055 assertThat(commonTypeMap).hasEntrySatisfying(APPLICATION_TAR, 056 value -> assertThat(value).isEqualTo(APPLICATION_TAR)); 057 assertThat(commonTypeMap).hasEntrySatisfying(APPLICATION_X_TAR, 058 value -> assertThat(value).isEqualTo(APPLICATION_TAR)); 059 assertThat(commonTypeMap).hasEntrySatisfying(APPLICATION_GTAR, 060 value -> assertThat(value).isEqualTo(APPLICATION_TAR)); 061 assertThat(commonTypeMap).hasEntrySatisfying(APPLICATION_X_GTAR, 062 value -> assertThat(value).isEqualTo(APPLICATION_TAR)); 063 } 064 065 @Test 066 public void testGZipMappings() { 067 final String tgzIdentifier = "tgz"; 068 final String gzipIdentifier = "gzip"; 069 final String tarGzIdentifier = "tar+gz"; 070 071 assertThat(commonTypeMap).hasEntrySatisfying(tgzIdentifier, 072 value -> assertThat(value).isEqualTo(APPLICATION_GZIP)); 073 assertThat(commonTypeMap).hasEntrySatisfying(gzipIdentifier, 074 value -> assertThat(value).isEqualTo(APPLICATION_GZIP)); 075 assertThat(commonTypeMap).hasEntrySatisfying(tarGzIdentifier, 076 value -> assertThat(value).isEqualTo(APPLICATION_GZIP)); 077 assertThat(commonTypeMap).hasEntrySatisfying(APPLICATION_GZIP, 078 value -> assertThat(value).isEqualTo(APPLICATION_GZIP)); 079 assertThat(commonTypeMap).hasEntrySatisfying(APPLICATION_X_GZIP, 080 value -> assertThat(value).isEqualTo(APPLICATION_GZIP)); 081 assertThat(commonTypeMap).hasEntrySatisfying(APPLICATION_X_COMPRESSED_TAR, 082 value -> assertThat(value).isEqualTo(APPLICATION_GZIP)); 083 } 084 085 @Test(expected = RuntimeException.class) 086 public void testDeserializerForFileNotFound() throws IOException { 087 final BagProfile profile = new BagProfile(BagProfile.BuiltIn.DEFAULT); 088 final Path notFound = Paths.get("file-not-found"); 089 SerializationSupport.deserializerFor(notFound, profile); 090 } 091 092 @Test(expected = RuntimeException.class) 093 public void testDeserializerNoProfileSupport() throws Exception { 094 // Currently the DEFAULT profile only supports application/tar, so send a file which is not a tarball 095 // see: profiles/default.json 096 final BagProfile profile = new BagProfile(BagProfile.BuiltIn.DEFAULT); 097 final URL url = SerializationSupportTest.class.getClassLoader().getResource("sample/compress/bag-zip.zip"); 098 assertThat(url).isNotNull(); 099 100 final Path notSupported = Paths.get(url.toURI()); 101 SerializationSupport.deserializerFor(notSupported, profile); 102 } 103 104 @Test(expected = UnsupportedOperationException.class) 105 public void testDeserializationNotSupported() throws Exception { 106 // A deserialization format which exists in a profile, but not by bagit-support 107 // currently json because we have many json resources available 108 final URL profileUrl = SerializationSupportTest.class.getClassLoader().getResource("profiles/profile.json"); 109 assertThat(profileUrl).isNotNull(); 110 final Path profileJson = Paths.get(profileUrl.toURI()); 111 final BagProfile profile = new BagProfile(Files.newInputStream(profileJson)); 112 113 SerializationSupport.deserializerFor(profileJson, profile); 114 } 115 116 @Test(expected = RuntimeException.class) 117 public void testSerializerNoProfileSupport() throws IOException { 118 // A serialization/compression format which does not exist in the profile, currently xz 119 final String xz = "application/x-xz"; 120 final BagProfile profile = new BagProfile(BagProfile.BuiltIn.DEFAULT); 121 SerializationSupport.serializerFor(xz, profile); 122 } 123 124 @Test(expected = UnsupportedOperationException.class) 125 public void testSerializerNotSupported() throws IOException { 126 // A serialization/compression format which exists in a profile, but not by bagit-support 127 // currently 7zip fits this 128 final String sevenZip = "application/x-7z-compressed"; 129 final BagProfile profile = new BagProfile(BagProfile.BuiltIn.BEYOND_THE_REPOSITORY); 130 SerializationSupport.serializerFor(sevenZip, profile); 131 } 132 133}