ATLAS-2897: Elegant handling of empty zip files.
Project: http://git-wip-us.apache.org/repos/asf/atlas/repo Commit: http://git-wip-us.apache.org/repos/asf/atlas/commit/19f9fddf Tree: http://git-wip-us.apache.org/repos/asf/atlas/tree/19f9fddf Diff: http://git-wip-us.apache.org/repos/asf/atlas/diff/19f9fddf Branch: refs/heads/branch-1.0 Commit: 19f9fddf54327d7f07629e8a0a2fc7e4e79e24e9 Parents: f3a153b Author: Ashutosh Mestry <ames...@hortonworks.com> Authored: Thu Sep 27 09:27:30 2018 -0700 Committer: Ashutosh Mestry <ames...@hortonworks.com> Committed: Thu Nov 1 15:42:56 2018 -0700 ---------------------------------------------------------------------- .../java/org/apache/atlas/AtlasErrorCode.java | 1 + .../atlas/repository/impexp/ZipSource.java | 21 +++++++++++----- .../repository/impexp/ImportServiceTest.java | 24 +++++++++++++------ .../impexp/ZipFileResourceTestUtils.java | 6 ++--- .../atlas/repository/impexp/ZipSourceTest.java | 8 +++---- repository/src/test/resources/empty.zip | Bin 0 -> 22 bytes 6 files changed, 40 insertions(+), 20 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/atlas/blob/19f9fddf/intg/src/main/java/org/apache/atlas/AtlasErrorCode.java ---------------------------------------------------------------------- diff --git a/intg/src/main/java/org/apache/atlas/AtlasErrorCode.java b/intg/src/main/java/org/apache/atlas/AtlasErrorCode.java index d29ea8e..bf78c20 100644 --- a/intg/src/main/java/org/apache/atlas/AtlasErrorCode.java +++ b/intg/src/main/java/org/apache/atlas/AtlasErrorCode.java @@ -153,6 +153,7 @@ public enum AtlasErrorCode { INVALID_TIMEBOUNDRY_END_TIME(400, "ATLAS-400-00-87C", "Invalid endTime {0}"), INVALID_TIMEBOUNDRY_DATERANGE(400, "ATLAS-400-00-87D", "Invalid dateRange: startTime {0} must be before endTime {1}"), PROPAGATED_CLASSIFICATION_REMOVAL_NOT_SUPPORTED(400, "ATLAS-400-00-87E", "Removal of classification {0}, which is propagated from entity {1}, is not supported"), + IMPORT_ATTEMPTING_EMPTY_ZIP(400, "ATLAS-400-00-87F", "Attempting to import empty ZIP file."), UNAUTHORIZED_ACCESS(403, "ATLAS-403-00-001", "{0} is not authorized to perform {1}"), http://git-wip-us.apache.org/repos/asf/atlas/blob/19f9fddf/repository/src/main/java/org/apache/atlas/repository/impexp/ZipSource.java ---------------------------------------------------------------------- diff --git a/repository/src/main/java/org/apache/atlas/repository/impexp/ZipSource.java b/repository/src/main/java/org/apache/atlas/repository/impexp/ZipSource.java index a292b96..bfa0441 100644 --- a/repository/src/main/java/org/apache/atlas/repository/impexp/ZipSource.java +++ b/repository/src/main/java/org/apache/atlas/repository/impexp/ZipSource.java @@ -40,6 +40,8 @@ import java.util.Map; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; +import static org.apache.atlas.AtlasErrorCode.IMPORT_ATTEMPTING_EMPTY_ZIP; + public class ZipSource implements EntityImportStream { private static final Logger LOG = LoggerFactory.getLogger(ZipSource.class); @@ -52,16 +54,20 @@ public class ZipSource implements EntityImportStream { private List<BaseEntityHandler> entityHandlers; private int currentPosition; - public ZipSource(InputStream inputStream) throws IOException { + public ZipSource(InputStream inputStream) throws IOException, AtlasBaseException { this(inputStream, null); } - public ZipSource(InputStream inputStream, ImportTransforms importTransform) throws IOException { + public ZipSource(InputStream inputStream, ImportTransforms importTransform) throws IOException, AtlasBaseException { this.inputStream = inputStream; this.guidEntityJsonMap = new HashMap<>(); this.importTransform = importTransform; updateGuidZipEntryMap(); + if (MapUtils.isEmpty(guidEntityJsonMap)) { + throw new AtlasBaseException(IMPORT_ATTEMPTING_EMPTY_ZIP, "Attempting to import empty ZIP."); + } + setCreationOrder(); } @@ -82,7 +88,7 @@ public class ZipSource implements EntityImportStream { public AtlasTypesDef getTypesDef() throws AtlasBaseException { final String fileName = ZipExportFileNames.ATLAS_TYPESDEF_NAME.toString(); - String s = (String) getFromCache(fileName); + String s = getFromCache(fileName); return convertFromJson(AtlasTypesDef.class, s); } @@ -185,7 +191,12 @@ public class ZipSource implements EntityImportStream { } private String getFromCache(String entryName) { - return guidEntityJsonMap.get(entryName); + String s = guidEntityJsonMap.get(entryName); + if (StringUtils.isEmpty(s)) { + LOG.warn("Could not fetch requested contents of file: {}", entryName); + } + + return s; } public void close() { @@ -288,6 +299,4 @@ public class ZipSource implements EntityImportStream { public int getPosition() { return currentPosition; } - - } http://git-wip-us.apache.org/repos/asf/atlas/blob/19f9fddf/repository/src/test/java/org/apache/atlas/repository/impexp/ImportServiceTest.java ---------------------------------------------------------------------- diff --git a/repository/src/test/java/org/apache/atlas/repository/impexp/ImportServiceTest.java b/repository/src/test/java/org/apache/atlas/repository/impexp/ImportServiceTest.java index a13df66..e0bbb11 100644 --- a/repository/src/test/java/org/apache/atlas/repository/impexp/ImportServiceTest.java +++ b/repository/src/test/java/org/apache/atlas/repository/impexp/ImportServiceTest.java @@ -113,7 +113,7 @@ public class ImportServiceTest extends ExportImportTestBase { } @DataProvider(name = "sales") - public static Object[][] getDataFromQuickStart_v1_Sales(ITestContext context) throws IOException { + public static Object[][] getDataFromQuickStart_v1_Sales(ITestContext context) throws IOException, AtlasBaseException { return getZipSource("sales-v1-full.zip"); } @@ -130,7 +130,7 @@ public class ImportServiceTest extends ExportImportTestBase { } @DataProvider(name = "reporting") - public static Object[][] getDataFromReporting() throws IOException { + public static Object[][] getDataFromReporting() throws IOException, AtlasBaseException { return getZipSource("reporting-v1-full.zip"); } @@ -141,7 +141,7 @@ public class ImportServiceTest extends ExportImportTestBase { } @DataProvider(name = "logging") - public static Object[][] getDataFromLogging(ITestContext context) throws IOException { + public static Object[][] getDataFromLogging(ITestContext context) throws IOException, AtlasBaseException { return getZipSource("logging-v1-full.zip"); } @@ -152,7 +152,7 @@ public class ImportServiceTest extends ExportImportTestBase { } @DataProvider(name = "salesNewTypeAttrs") - public static Object[][] getDataFromSalesNewTypeAttrs(ITestContext context) throws IOException { + public static Object[][] getDataFromSalesNewTypeAttrs(ITestContext context) throws IOException, AtlasBaseException { return getZipSource("salesNewTypeAttrs.zip"); } @@ -163,7 +163,7 @@ public class ImportServiceTest extends ExportImportTestBase { } @DataProvider(name = "salesNewTypeAttrs-next") - public static Object[][] getDataFromSalesNewTypeAttrsNext(ITestContext context) throws IOException { + public static Object[][] getDataFromSalesNewTypeAttrsNext(ITestContext context) throws IOException, AtlasBaseException { return getZipSource("salesNewTypeAttrs-next.zip"); } @@ -200,7 +200,7 @@ public class ImportServiceTest extends ExportImportTestBase { } @DataProvider(name = "ctas") - public static Object[][] getDataFromCtas(ITestContext context) throws IOException { + public static Object[][] getDataFromCtas(ITestContext context) throws IOException, AtlasBaseException { return getZipSource("ctas.zip"); } @@ -276,7 +276,7 @@ public class ImportServiceTest extends ExportImportTestBase { } @DataProvider(name = "hdfs_path1") - public static Object[][] getDataFromHdfsPath1(ITestContext context) throws IOException { + public static Object[][] getDataFromHdfsPath1(ITestContext context) throws IOException, AtlasBaseException { return getZipSource("hdfs_path1.zip"); } @@ -421,4 +421,14 @@ public class ImportServiceTest extends ExportImportTestBase { assertTrue(importTransforms.getTransforms().containsKey("hive_column")); assertEquals(importTransforms.getTransforms().get("hive_table").get("qualifiedName").size(), 2); } + + @Test(dataProvider = "empty-zip", expectedExceptions = AtlasBaseException.class) + public void importEmptyZip(ZipSource zipSource) { + + } + + @Test(expectedExceptions = AtlasBaseException.class) + public void importEmptyZip() throws IOException, AtlasBaseException { + getZipSource("empty.zip"); + } } http://git-wip-us.apache.org/repos/asf/atlas/blob/19f9fddf/repository/src/test/java/org/apache/atlas/repository/impexp/ZipFileResourceTestUtils.java ---------------------------------------------------------------------- diff --git a/repository/src/test/java/org/apache/atlas/repository/impexp/ZipFileResourceTestUtils.java b/repository/src/test/java/org/apache/atlas/repository/impexp/ZipFileResourceTestUtils.java index faf68fe..fe473b8 100644 --- a/repository/src/test/java/org/apache/atlas/repository/impexp/ZipFileResourceTestUtils.java +++ b/repository/src/test/java/org/apache/atlas/repository/impexp/ZipFileResourceTestUtils.java @@ -150,17 +150,17 @@ public class ZipFileResourceTestUtils { return s; } - public static Object[][] getZipSource(String fileName) throws IOException { + public static Object[][] getZipSource(String fileName) throws IOException, AtlasBaseException { return new Object[][]{{getZipSourceFrom(fileName)}}; } - public static ZipSource getZipSourceFrom(String fileName) throws IOException { + public static ZipSource getZipSourceFrom(String fileName) throws IOException, AtlasBaseException { FileInputStream fs = ZipFileResourceTestUtils.getFileInputStream(fileName); return new ZipSource(fs); } - private static ZipSource getZipSourceFrom(ByteArrayOutputStream baos) throws IOException { + private static ZipSource getZipSourceFrom(ByteArrayOutputStream baos) throws IOException, AtlasBaseException { ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray()); ZipSource zipSource = new ZipSource(bis); return zipSource; http://git-wip-us.apache.org/repos/asf/atlas/blob/19f9fddf/repository/src/test/java/org/apache/atlas/repository/impexp/ZipSourceTest.java ---------------------------------------------------------------------- diff --git a/repository/src/test/java/org/apache/atlas/repository/impexp/ZipSourceTest.java b/repository/src/test/java/org/apache/atlas/repository/impexp/ZipSourceTest.java index 1c1c68f..f0034fa 100644 --- a/repository/src/test/java/org/apache/atlas/repository/impexp/ZipSourceTest.java +++ b/repository/src/test/java/org/apache/atlas/repository/impexp/ZipSourceTest.java @@ -38,21 +38,21 @@ import static org.testng.AssertJUnit.assertTrue; public class ZipSourceTest { @DataProvider(name = "zipFileStocks") - public static Object[][] getDataFromZipFile() throws IOException { + public static Object[][] getDataFromZipFile() throws IOException, AtlasBaseException { FileInputStream fs = ZipFileResourceTestUtils.getFileInputStream("stocks.zip"); return new Object[][] {{ new ZipSource(fs) }}; } @DataProvider(name = "zipFileStocksFloat") - public static Object[][] getDataFromZipFileWithLongFloats() throws IOException { + public static Object[][] getDataFromZipFileWithLongFloats() throws IOException, AtlasBaseException { FileInputStream fs = ZipFileResourceTestUtils.getFileInputStream("stocks-float.zip"); return new Object[][] {{ new ZipSource(fs) }}; } @DataProvider(name = "sales") - public static Object[][] getDataFromQuickStart_v1_Sales(ITestContext context) throws IOException { + public static Object[][] getDataFromQuickStart_v1_Sales(ITestContext context) throws IOException, AtlasBaseException { return getZipSource("sales-v1-full.zip"); } @@ -66,7 +66,7 @@ public class ZipSourceTest { } @Test(dataProvider = "zipFileStocks") - public void examineContents_BehavesAsExpected(ZipSource zipSource) throws IOException, AtlasBaseException { + public void examineContents_BehavesAsExpected(ZipSource zipSource) throws AtlasBaseException { List<String> creationOrder = zipSource.getCreationOrder(); assertNotNull(creationOrder); http://git-wip-us.apache.org/repos/asf/atlas/blob/19f9fddf/repository/src/test/resources/empty.zip ---------------------------------------------------------------------- diff --git a/repository/src/test/resources/empty.zip b/repository/src/test/resources/empty.zip new file mode 100644 index 0000000..15cb0ec Binary files /dev/null and b/repository/src/test/resources/empty.zip differ