Repository: atlas Updated Branches: refs/heads/branch-0.8 e3a592cef -> 01f3f14ea
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/01f3f14e Tree: http://git-wip-us.apache.org/repos/asf/atlas/tree/01f3f14e Diff: http://git-wip-us.apache.org/repos/asf/atlas/diff/01f3f14e Branch: refs/heads/branch-0.8 Commit: 01f3f14ea78092000a1855c6bd7838a02647e8cf Parents: e3a592c Author: Ashutosh Mestry <ames...@hortonworks.com> Authored: Thu Sep 27 09:27:30 2018 -0700 Committer: Ashutosh Mestry <ames...@hortonworks.com> Committed: Thu Sep 27 15:01:23 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/01f3f14e/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 dde7d3a..8163390 100644 --- a/intg/src/main/java/org/apache/atlas/AtlasErrorCode.java +++ b/intg/src/main/java/org/apache/atlas/AtlasErrorCode.java @@ -77,6 +77,7 @@ public enum AtlasErrorCode { SAVED_SEARCH_CHANGE_USER(400, "ATLAS-400-00-056", "saved-search {0} can not be moved from user {1} to {2}"), INVALID_QUERY_PARAM_LENGTH(400, "ATLAS-400-00-057" , "Length of query param {0} exceeds the limit"), INVALID_QUERY_LENGTH(400, "ATLAS-400-00-057" , "Invalid query length, update {0} to change the limit" ), + IMPORT_ATTEMPTING_EMPTY_ZIP(400, "ATLAS-400-00-058", "Attempting to import empty ZIP file."), // All Not found enums go here UNKNOWN_CLASSIFICATION(400, "ATLAS-400-00-046", "{0}: Unknown/invalid classification"), http://git-wip-us.apache.org/repos/asf/atlas/blob/01f3f14e/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 7d96044..691f933 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); } @@ -179,7 +185,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() { @@ -282,6 +293,4 @@ public class ZipSource implements EntityImportStream { public int getPosition() { return currentPosition; } - - } http://git-wip-us.apache.org/repos/asf/atlas/blob/01f3f14e/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 b556fe9..693a163 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 @@ -85,7 +85,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"); } @@ -96,7 +96,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"); } @@ -107,7 +107,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"); } @@ -118,7 +118,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"); } @@ -129,7 +129,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"); } @@ -167,7 +167,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"); } @@ -180,7 +180,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"); } @@ -265,4 +265,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/01f3f14e/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 720aa14..124853e 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 @@ -89,17 +89,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/01f3f14e/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/01f3f14e/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