Commit 67c04c63 by Ashutosh Mestry Committed by Madhan Neethiraj

ATLAS-2132: incorrect error for invalid file path/unreadable file provided during import

parent 1dee77af
...@@ -36,6 +36,7 @@ import javax.inject.Inject; ...@@ -36,6 +36,7 @@ import javax.inject.Inject;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException;
@Component @Component
public class ImportService { public class ImportService {
...@@ -108,7 +109,7 @@ public class ImportService { ...@@ -108,7 +109,7 @@ public class ImportService {
public AtlasImportResult run(AtlasImportRequest request, String userName, String hostName, String requestingIP) public AtlasImportResult run(AtlasImportRequest request, String userName, String hostName, String requestingIP)
throws AtlasBaseException { throws AtlasBaseException {
String fileName = (String) request.getFileName(); String fileName = request.getFileName();
if (StringUtils.isBlank(fileName)) { if (StringUtils.isBlank(fileName)) {
throw new AtlasBaseException(AtlasErrorCode.INVALID_PARAMETERS, "FILENAME parameter not found"); throw new AtlasBaseException(AtlasErrorCode.INVALID_PARAMETERS, "FILENAME parameter not found");
...@@ -122,7 +123,6 @@ public class ImportService { ...@@ -122,7 +123,6 @@ public class ImportService {
String transforms = MapUtils.isNotEmpty(request.getOptions()) ? request.getOptions().get(AtlasImportRequest.TRANSFORMS_KEY) : null; String transforms = MapUtils.isNotEmpty(request.getOptions()) ? request.getOptions().get(AtlasImportRequest.TRANSFORMS_KEY) : null;
File file = new File(fileName); File file = new File(fileName);
ZipSource source = new ZipSource(new ByteArrayInputStream(FileUtils.readFileToByteArray(file)), ImportTransforms.fromJson(transforms)); ZipSource source = new ZipSource(new ByteArrayInputStream(FileUtils.readFileToByteArray(file)), ImportTransforms.fromJson(transforms));
result = run(source, request, userName, hostName, requestingIP); result = run(source, request, userName, hostName, requestingIP);
} catch (AtlasBaseException excp) { } catch (AtlasBaseException excp) {
LOG.error("import(user={}, from={}, fileName={}): failed", userName, requestingIP, excp); LOG.error("import(user={}, from={}, fileName={}): failed", userName, requestingIP, excp);
...@@ -132,6 +132,10 @@ public class ImportService { ...@@ -132,6 +132,10 @@ public class ImportService {
LOG.error("import(user={}, from={}, fileName={}): file not found", userName, requestingIP, excp); LOG.error("import(user={}, from={}, fileName={}): file not found", userName, requestingIP, excp);
throw new AtlasBaseException(AtlasErrorCode.INVALID_PARAMETERS, fileName + ": file not found"); throw new AtlasBaseException(AtlasErrorCode.INVALID_PARAMETERS, fileName + ": file not found");
} catch (IOException excp) {
LOG.error("import(user={}, from={}, fileName={}): cannot read file", userName, requestingIP, excp);
throw new AtlasBaseException(AtlasErrorCode.INVALID_PARAMETERS, fileName + ": cannot read file");
} catch (Exception excp) { } catch (Exception excp) {
LOG.error("import(user={}, from={}, fileName={}): failed", userName, requestingIP, excp); LOG.error("import(user={}, from={}, fileName={}): failed", userName, requestingIP, excp);
......
...@@ -27,6 +27,8 @@ import org.apache.atlas.model.impexp.AtlasImportRequest; ...@@ -27,6 +27,8 @@ import org.apache.atlas.model.impexp.AtlasImportRequest;
import org.apache.atlas.store.AtlasTypeDefStore; import org.apache.atlas.store.AtlasTypeDefStore;
import org.apache.atlas.type.AtlasClassificationType; import org.apache.atlas.type.AtlasClassificationType;
import org.apache.atlas.type.AtlasTypeRegistry; import org.apache.atlas.type.AtlasTypeRegistry;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.testng.ITestContext; import org.testng.ITestContext;
...@@ -40,6 +42,8 @@ import java.util.HashMap; ...@@ -40,6 +42,8 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import static org.apache.atlas.repository.impexp.ZipFileResourceTestUtils.*; import static org.apache.atlas.repository.impexp.ZipFileResourceTestUtils.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertNotNull;
...@@ -72,7 +76,7 @@ public class ImportServiceTest { ...@@ -72,7 +76,7 @@ public class ImportServiceTest {
@Test(dataProvider = "sales") @Test(dataProvider = "sales")
public void importDB1(ZipSource zipSource) throws AtlasBaseException, IOException { public void importDB1(ZipSource zipSource) throws AtlasBaseException, IOException {
loadModelFromJson("0000-Area0/0010-base_model.json", typeDefStore, typeRegistry); loadBaseModel();
runAndVerifyQuickStart_v1_Import(importService, zipSource); runAndVerifyQuickStart_v1_Import(importService, zipSource);
} }
...@@ -83,10 +87,14 @@ public class ImportServiceTest { ...@@ -83,10 +87,14 @@ public class ImportServiceTest {
@Test(dataProvider = "reporting") @Test(dataProvider = "reporting")
public void importDB2(ZipSource zipSource) throws AtlasBaseException, IOException { public void importDB2(ZipSource zipSource) throws AtlasBaseException, IOException {
loadModelFromJson("0000-Area0/0010-base_model.json", typeDefStore, typeRegistry); loadBaseModel();
runAndVerifyQuickStart_v1_Import(importService, zipSource); runAndVerifyQuickStart_v1_Import(importService, zipSource);
} }
private void loadBaseModel() throws IOException, AtlasBaseException {
loadModelFromJson("0000-Area0/0010-base_model.json", typeDefStore, typeRegistry);
}
@DataProvider(name = "logging") @DataProvider(name = "logging")
public static Object[][] getDataFromLogging(ITestContext context) throws IOException { public static Object[][] getDataFromLogging(ITestContext context) throws IOException {
return getZipSource("logging-v1-full.zip"); return getZipSource("logging-v1-full.zip");
...@@ -94,7 +102,7 @@ public class ImportServiceTest { ...@@ -94,7 +102,7 @@ public class ImportServiceTest {
@Test(dataProvider = "logging") @Test(dataProvider = "logging")
public void importDB3(ZipSource zipSource) throws AtlasBaseException, IOException { public void importDB3(ZipSource zipSource) throws AtlasBaseException, IOException {
loadModelFromJson("0000-Area0/0010-base_model.json", typeDefStore, typeRegistry); loadBaseModel();
runAndVerifyQuickStart_v1_Import(importService, zipSource); runAndVerifyQuickStart_v1_Import(importService, zipSource);
} }
...@@ -105,7 +113,7 @@ public class ImportServiceTest { ...@@ -105,7 +113,7 @@ public class ImportServiceTest {
@Test(dataProvider = "salesNewTypeAttrs", dependsOnMethods = "importDB1") @Test(dataProvider = "salesNewTypeAttrs", dependsOnMethods = "importDB1")
public void importDB4(ZipSource zipSource) throws AtlasBaseException, IOException { public void importDB4(ZipSource zipSource) throws AtlasBaseException, IOException {
loadModelFromJson("0000-Area0/0010-base_model.json", typeDefStore, typeRegistry); loadBaseModel();
runImportWithParameters(importService, getDefaultImportRequest(), zipSource); runImportWithParameters(importService, getDefaultImportRequest(), zipSource);
} }
...@@ -154,8 +162,8 @@ public class ImportServiceTest { ...@@ -154,8 +162,8 @@ public class ImportServiceTest {
@Test(dataProvider = "ctas") @Test(dataProvider = "ctas")
public void importCTAS(ZipSource zipSource) throws IOException, AtlasBaseException { public void importCTAS(ZipSource zipSource) throws IOException, AtlasBaseException {
loadModelFromJson("0000-Area0/0010-base_model.json", typeDefStore, typeRegistry); loadBaseModel();
loadModelFromJson("1000-Hadoop/1030-hive_model.json", typeDefStore, typeRegistry); loadHiveModel();
runImportWithNoParameters(importService, zipSource); runImportWithNoParameters(importService, zipSource);
} }
...@@ -168,8 +176,8 @@ public class ImportServiceTest { ...@@ -168,8 +176,8 @@ public class ImportServiceTest {
@Test(dataProvider = "hdfs_path1", expectedExceptions = AtlasBaseException.class) @Test(dataProvider = "hdfs_path1", expectedExceptions = AtlasBaseException.class)
public void importHdfs_path1(ZipSource zipSource) throws IOException, AtlasBaseException { public void importHdfs_path1(ZipSource zipSource) throws IOException, AtlasBaseException {
loadModelFromJson("0000-Area0/0010-base_model.json", typeDefStore, typeRegistry); loadBaseModel();
loadModelFromJson("1000-Hadoop/1020-fs_model.json", typeDefStore, typeRegistry); loadFsModel();
loadModelFromResourcesJson("tag1.json", typeDefStore, typeRegistry); loadModelFromResourcesJson("tag1.json", typeDefStore, typeRegistry);
try { try {
...@@ -182,4 +190,35 @@ public class ImportServiceTest { ...@@ -182,4 +190,35 @@ public class ImportServiceTest {
throw e; throw e;
} }
} }
@Test
public void importServiceProcessesIOException() {
ImportService importService = new ImportService(typeDefStore, typeRegistry, null);
AtlasImportRequest req = mock(AtlasImportRequest.class);
Answer<Map> answer = new Answer<Map>() {
@Override
public Map answer(InvocationOnMock invocationOnMock) throws Throwable {
throw new IOException("file is read only");
}
};
when(req.getFileName()).thenReturn("some-file.zip");
when(req.getOptions()).thenAnswer(answer);
try {
importService.run(req, "a", "b", "c");
}
catch (AtlasBaseException ex) {
assertEquals(ex.getAtlasErrorCode().getErrorCode(), AtlasErrorCode.INVALID_PARAMETERS.getErrorCode());
}
}
private void loadFsModel() throws IOException, AtlasBaseException {
loadModelFromJson("1000-Hadoop/1020-fs_model.json", typeDefStore, typeRegistry);
}
private void loadHiveModel() throws IOException, AtlasBaseException {
loadModelFromJson("1000-Hadoop/1030-hive_model.json", typeDefStore, typeRegistry);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment