http://git-wip-us.apache.org/repos/asf/ambari/blob/e423a65e/contrib/views/hive/src/test/java/org/apache/ambari/view/hive/resources/savedQueries/SavedQueryServiceTest.java ---------------------------------------------------------------------- diff --git a/contrib/views/hive/src/test/java/org/apache/ambari/view/hive/resources/savedQueries/SavedQueryServiceTest.java b/contrib/views/hive/src/test/java/org/apache/ambari/view/hive/resources/savedQueries/SavedQueryServiceTest.java deleted file mode 100644 index 68cb0c8..0000000 --- a/contrib/views/hive/src/test/java/org/apache/ambari/view/hive/resources/savedQueries/SavedQueryServiceTest.java +++ /dev/null @@ -1,191 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ambari.view.hive.resources.savedQueries; - -import org.apache.ambari.view.hive.HDFSTest; -import org.apache.ambari.view.hive.utils.NotFoundFormattedException; -import org.json.simple.JSONObject; -import org.junit.*; -import org.junit.rules.ExpectedException; - -import javax.servlet.http.HttpServletResponse; -import javax.ws.rs.core.Response; -import javax.ws.rs.core.UriBuilder; -import javax.ws.rs.core.UriInfo; -import java.io.File; -import java.net.URI; -import java.util.List; -import java.util.Map; - -import static org.easymock.EasyMock.*; - -public class SavedQueryServiceTest extends HDFSTest { - //TODO: run without HDFS cluster - private SavedQueryService savedQueryService; - @Rule public ExpectedException thrown = ExpectedException.none(); - - @BeforeClass - public static void startUp() throws Exception { - HDFSTest.startUp(); // super - } - - @AfterClass - public static void shutDown() throws Exception { - HDFSTest.shutDown(); // super - } - - @Override - @Before - public void setUp() throws Exception { - super.setUp(); - savedQueryService = getService(SavedQueryService.class, handler, context); - savedQueryService.getSharedObjectsFactory().clear(); - } - - @Override - @After - public void tearDown() throws Exception { - super.tearDown(); - } - - @Override - protected void setupProperties(Map<String, String> properties, File baseDir) throws Exception { - super.setupProperties(properties, baseDir); - properties.put("scripts.dir", "/tmp/.hiveQueries"); - } - - private Response doCreateSavedQuery() { - return doCreateSavedQuery("Luke", "/tmp/luke.hql", savedQueryService); - } - - public static Response doCreateSavedQuery(String title, String path, SavedQueryService service) { - SavedQueryService.SavedQueryRequest request = new SavedQueryService.SavedQueryRequest(); - request.savedQuery = new SavedQuery(); - request.savedQuery.setTitle(title); - request.savedQuery.setQueryFile(path); - - UriInfo uriInfo = createNiceMock(UriInfo.class); - URI uri = UriBuilder.fromUri("http://host/a/b").build(); - expect(uriInfo.getAbsolutePath()).andReturn(uri); - - HttpServletResponse resp_obj = createNiceMock(HttpServletResponse.class); - - resp_obj.setHeader(eq("Location"), anyString()); - - replay(uriInfo, resp_obj); - return service.create(request, resp_obj, uriInfo); - } - - private Response doCreateSavedQuery(String title, String path) { - return doCreateSavedQuery(title, path, savedQueryService); - } - - @Test - public void createSavedQuery() { - Response response = doCreateSavedQuery(); - Assert.assertEquals(201, response.getStatus()); - - JSONObject obj = (JSONObject)response.getEntity(); - Assert.assertTrue(obj.containsKey("savedQuery")); - Assert.assertNotNull(((SavedQuery) obj.get("savedQuery")).getId()); - Assert.assertTrue(((SavedQuery) obj.get("savedQuery")).getId() != null); - } - - @Test - public void createSavedQueryAutoCreate() { - Response response = doCreateSavedQuery("Test", null); - Assert.assertEquals(201, response.getStatus()); - - JSONObject obj = (JSONObject)response.getEntity(); - Assert.assertTrue(obj.containsKey("savedQuery")); - Assert.assertNotNull(((SavedQuery) obj.get("savedQuery")).getId()); - Assert.assertFalse(((SavedQuery) obj.get("savedQuery")).getId() == null); - Assert.assertFalse(((SavedQuery) obj.get("savedQuery")).getQueryFile().isEmpty()); - } - - @Test - public void notFound() { - thrown.expect(NotFoundFormattedException.class); - savedQueryService.getOne("4242", null); - } - - @Test - public void update() { - Response created = doCreateSavedQuery(); - Object createdId = ((SavedQuery) ((JSONObject) created.getEntity()).get("savedQuery")).getId(); - - SavedQueryService.SavedQueryRequest request = new SavedQueryService.SavedQueryRequest(); - request.savedQuery = new SavedQuery(); - request.savedQuery.setTitle("Updated Query"); - - Response response = savedQueryService.update(request, String.valueOf(createdId)); - Assert.assertEquals(204, response.getStatus()); - - Response response2 = savedQueryService.getOne(String.valueOf(createdId), ""); - Assert.assertEquals(200, response2.getStatus()); - - JSONObject obj = ((JSONObject) response2.getEntity()); - Assert.assertTrue(obj.containsKey("savedQuery")); - Assert.assertEquals(((SavedQuery) obj.get("savedQuery")).getTitle(), request.savedQuery.getTitle()); - } - - @Test - public void delete() { - Response created = doCreateSavedQuery(); - Object createdId = ((SavedQuery) ((JSONObject) created.getEntity()).get("savedQuery")).getId(); - - Response response = savedQueryService.delete(String.valueOf(createdId)); - Assert.assertEquals(204, response.getStatus()); - - thrown.expect(NotFoundFormattedException.class); - savedQueryService.getOne(String.valueOf(createdId),null); - } - - @Test - public void list() { - doCreateSavedQuery("Title 1", "/path/to/file.hql"); - doCreateSavedQuery("Title 2", "/path/to/file.hql"); - - Response response = savedQueryService.getList(); - Assert.assertEquals(200, response.getStatus()); - - JSONObject obj = (JSONObject) response.getEntity(); - Assert.assertTrue(obj.containsKey("savedQueries")); - List<SavedQuery> items = (List<SavedQuery>) obj.get("savedQueries"); - boolean containsTitle = false; - for(SavedQuery item : items) - containsTitle = containsTitle || item.getTitle().compareTo("Title 1") == 0; - Assert.assertTrue(containsTitle); - - containsTitle = false; - for(SavedQuery item : items) - containsTitle = containsTitle || item.getTitle().compareTo("Title 2") == 0; - Assert.assertTrue(containsTitle); - } - @Test - public void downloadQuery() { - Response created = doCreateSavedQuery(); - Object createdId = ((SavedQuery) ((JSONObject) created.getEntity()).get("savedQuery")).getId(); - SavedQueryService.SavedQueryRequest request = new SavedQueryService.SavedQueryRequest(); - request.savedQuery = new SavedQuery(); - request.savedQuery.setTitle("Download Query"); - Response response = savedQueryService.getOne(String.valueOf(createdId), "download"); - Assert.assertEquals(200, response.getStatus()); - } -}
http://git-wip-us.apache.org/repos/asf/ambari/blob/e423a65e/contrib/views/hive/src/test/java/org/apache/ambari/view/hive/resources/udfs/UDFServiceTest.java ---------------------------------------------------------------------- diff --git a/contrib/views/hive/src/test/java/org/apache/ambari/view/hive/resources/udfs/UDFServiceTest.java b/contrib/views/hive/src/test/java/org/apache/ambari/view/hive/resources/udfs/UDFServiceTest.java deleted file mode 100644 index c8b70a8..0000000 --- a/contrib/views/hive/src/test/java/org/apache/ambari/view/hive/resources/udfs/UDFServiceTest.java +++ /dev/null @@ -1,119 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ambari.view.hive.resources.udfs; - -import org.apache.ambari.view.hive.BaseHiveTest; -import org.apache.ambari.view.hive.resources.udfs.UDF; -import org.apache.ambari.view.hive.resources.udfs.UDFService; -import org.apache.ambari.view.hive.utils.NotFoundFormattedException; -import org.json.simple.JSONObject; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; - -import javax.servlet.http.HttpServletResponse; -import javax.ws.rs.core.Response; -import javax.ws.rs.core.UriBuilder; -import javax.ws.rs.core.UriInfo; -import java.net.URI; - -import static org.easymock.EasyMock.*; - -public class UDFServiceTest extends BaseHiveTest { - @Rule public ExpectedException thrown = ExpectedException.none(); - private UDFService udfService; - - @Override - @Before - public void setUp() throws Exception { - super.setUp(); - udfService = getService(UDFService.class, handler, context); - } - - private Response doCreateUDF() { - UDFService.UDFRequest request = new UDFService.UDFRequest(); - request.udf = new UDF(); - request.udf.setClassname("/tmp/udf.jar"); - request.udf.setName("TestUDF"); - - UriInfo uriInfo = createNiceMock(UriInfo.class); - URI uri = UriBuilder.fromUri("http://host/a/b").build(); - expect(uriInfo.getAbsolutePath()).andReturn(uri); - - HttpServletResponse resp_obj = createNiceMock(HttpServletResponse.class); - - resp_obj.setHeader(eq("Location"), anyString()); - - replay(uriInfo, resp_obj); - return udfService.create(request, resp_obj, uriInfo); - } - - @Test - public void createUDF() { - Response response = doCreateUDF(); - Assert.assertEquals(201, response.getStatus()); - - JSONObject obj = (JSONObject)response.getEntity(); - Assert.assertTrue(obj.containsKey("udf")); - Assert.assertNotNull(((UDF) obj.get("udf")).getId()); - Assert.assertFalse(((UDF) obj.get("udf")).getId() == null); - } - - @Test - public void udfNotFound() { - thrown.expect(NotFoundFormattedException.class); - udfService.getOne("4242"); - } - - @Test - public void updateUDF() { - Response createdUDF = doCreateUDF(); - Object createdUdfId = ((UDF) ((JSONObject) createdUDF.getEntity()).get("udf")).getId(); - - UDFService.UDFRequest request = new UDFService.UDFRequest(); - request.udf = new UDF(); - request.udf.setClassname("/tmp/updatedUDF.jar"); - request.udf.setName("TestUDF2"); - - Response response = udfService.update(request, String.valueOf(createdUdfId)); - Assert.assertEquals(204, response.getStatus()); - - Response response2 = udfService.getOne(String.valueOf(createdUdfId)); - Assert.assertEquals(200, response2.getStatus()); - - JSONObject obj = ((JSONObject) response2.getEntity()); - Assert.assertTrue(obj.containsKey("udf")); - Assert.assertEquals(((UDF) obj.get("udf")).getName(), request.udf.getName()); - Assert.assertEquals(((UDF) obj.get("udf")).getClassname(), request.udf.getClassname()); - } - - @Test - public void deleteUDF() { - Response createdUDF = doCreateUDF(); - Object createdUdfId = ((UDF) ((JSONObject) createdUDF.getEntity()).get("udf")).getId(); - - Response response = udfService.delete(String.valueOf(createdUdfId)); - Assert.assertEquals(204, response.getStatus()); - - thrown.expect(NotFoundFormattedException.class); - udfService.getOne(String.valueOf(createdUdfId)); - } -} http://git-wip-us.apache.org/repos/asf/ambari/blob/e423a65e/contrib/views/hive/src/test/java/org/apache/ambari/view/hive/resources/upload/CSVParserTest.java ---------------------------------------------------------------------- diff --git a/contrib/views/hive/src/test/java/org/apache/ambari/view/hive/resources/upload/CSVParserTest.java b/contrib/views/hive/src/test/java/org/apache/ambari/view/hive/resources/upload/CSVParserTest.java deleted file mode 100644 index d278fde..0000000 --- a/contrib/views/hive/src/test/java/org/apache/ambari/view/hive/resources/upload/CSVParserTest.java +++ /dev/null @@ -1,275 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * <p/> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p/> - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ambari.view.hive.resources.upload; - -import org.apache.ambari.view.hive.client.Row; -import org.apache.ambari.view.hive.resources.uploads.parsers.ParseOptions; -import org.apache.ambari.view.hive.resources.uploads.parsers.csv.commonscsv.CSVParser; -import org.junit.Assert; -import org.junit.Test; - -import java.io.IOException; -import java.io.StringReader; -import java.util.Iterator; - -public class CSVParserTest { - - /** - * no exception in creating csvParser with emtpy stream - * @throws IOException - */ - @Test - public void testEmptyStream() throws Exception { - String csv = ""; - - try( - StringReader sr = new StringReader(csv); - CSVParser jp = new CSVParser(sr, new ParseOptions()); - ) { - Assert.assertEquals("There should not be any rows.",false, jp.iterator().hasNext()); - } - } - - /** - * in case of csv an empty line is still considered as row - * @throws IOException - */ - @Test - public void testEmptyRow() throws Exception { - String csv = " "; - - try( - StringReader sr = new StringReader(csv); - CSVParser jp = new CSVParser(sr, new ParseOptions()); - ) { - Iterator<Row> iterator = jp.iterator(); - - Assert.assertEquals("Iterator should be Empty", true, iterator.hasNext()); - Assert.assertArrayEquals("Row should not be empty",new Object[]{" "},iterator.next().getRow()); - } - } - - @Test - public void testParse1Row() throws Exception { - String csv = "value1,c,10,10.1"; - - try( - StringReader sr = new StringReader(csv); - CSVParser jp = new CSVParser(sr, new ParseOptions()); - ) { - Iterator<Row> iterator = jp.iterator(); - - Assert.assertEquals("Iterator Empty!", true, iterator.hasNext()); - Row row = iterator.next(); - Row expected = new Row(new Object[]{"value1", "c", "10", "10.1"}); - Assert.assertEquals("Row not equal!", expected, row); - - Assert.assertEquals("Should report no more rows!", false, iterator.hasNext()); - } - } - - @Test - public void testParseMultipleRow() throws Exception { - - String csv = "value1,c,10,10.1\n" + - "value2,c2,102,true"; - - try( - StringReader sr = new StringReader(csv); - CSVParser jp = new CSVParser(sr, new ParseOptions()); - ) { - - Iterator<Row> iterator = jp.iterator(); - - Assert.assertEquals("Failed to detect 1st row!", true, iterator.hasNext()); - Assert.assertEquals("Failed to match 1st row!", new Row(new Object[]{"value1", "c", "10", "10.1"}), iterator.next()); - - Assert.assertEquals("Failed to detect 2nd row!", true, iterator.hasNext()); - Assert.assertEquals("Failed to match 2nd row!", new Row(new Object[]{"value2", "c2", "102", Boolean.TRUE.toString()}), iterator.next()); - - Assert.assertEquals("Failed to detect end of rows!", false, iterator.hasNext()); - Assert.assertEquals("Failed to detect end of rows 2nd time!", false, iterator.hasNext()); - } - } - - - @Test - public void testQuotedEndline() throws Exception { - - String csv = "\"row1-\ncol1\",1,1.1\n\"row2-\\\ncol1\",2,2.2\n"; - ParseOptions po = new ParseOptions(); - - try( - StringReader sr = new StringReader(csv); - CSVParser jp = new CSVParser(sr, po); - ) { - - Iterator<Row> iterator = jp.iterator(); - - Row row = new Row(new Object[]{"row1-\ncol1", "1", "1.1"}); - Assert.assertEquals("Failed to detect 1st row!", true, iterator.hasNext()); - Assert.assertEquals("Failed to match 1st row!", row, iterator.next()); - - Row row2 = new Row(new Object[]{"row2-\\\ncol1", "2", "2.2"}); - Assert.assertEquals("Failed to detect 1st row!", true, iterator.hasNext()); - Assert.assertEquals("Failed to match 1st row!", row2, iterator.next()); - - } - } - - @Test - public void testQuotedDoubleQuote() throws Exception { - - String csv = "\"aaa\",\"b\"\"bb\",\"ccc\""; - ParseOptions po = new ParseOptions(); - - try( - StringReader sr = new StringReader(csv); - CSVParser jp = new CSVParser(sr, po); - ) { - - Iterator<Row> iterator = jp.iterator(); - - Row row = new Row(new Object[]{"aaa", "b\"bb", "ccc"}); - Assert.assertEquals("Failed to detect 1st row!", true, iterator.hasNext()); - Assert.assertEquals("Failed to match 1st row!", row, iterator.next()); - } - } - - @Test - public void testSpecialEscape() throws Exception { - - String csv = "\"aaa\",\"b$\"bb\",\"ccc\""; - ParseOptions po = new ParseOptions(); - po.setOption(ParseOptions.OPTIONS_CSV_ESCAPE_CHAR,'$'); - - try( - StringReader sr = new StringReader(csv); - CSVParser jp = new CSVParser(sr, po); - ) { - - Iterator<Row> iterator = jp.iterator(); - - Row row = new Row(new Object[]{"aaa", "b\"bb", "ccc"}); - Assert.assertEquals("Failed to detect 1st row!", true, iterator.hasNext()); - Assert.assertEquals("Failed to match 1st row!", row, iterator.next()); - } - } - - @Test - public void testSpecialEscapedEscape() throws Exception { - - String csv = "aaa,b$$bb,ccc"; - ParseOptions po = new ParseOptions(); - po.setOption(ParseOptions.OPTIONS_CSV_ESCAPE_CHAR,'$'); - - try( - StringReader sr = new StringReader(csv); - CSVParser jp = new CSVParser(sr, po); - ) { - - Iterator<Row> iterator = jp.iterator(); - - Row row = new Row(new Object[]{"aaa", "b$bb", "ccc"}); - Assert.assertEquals("Failed to detect 1st row!", true, iterator.hasNext()); - Assert.assertEquals("Failed to match 1st row!", row, iterator.next()); - } - } - - @Test - public void test001Escape() throws Exception { - - String csv = "aaa,b\001\"bb,ccc"; - ParseOptions po = new ParseOptions(); - po.setOption(ParseOptions.OPTIONS_CSV_ESCAPE_CHAR,'\001'); - - try( - StringReader sr = new StringReader(csv); - CSVParser jp = new CSVParser(sr, po); - ) { - - Iterator<Row> iterator = jp.iterator(); - Row row = new Row(new Object[]{"aaa", "b\"bb", "ccc"}); - Assert.assertEquals("Failed to detect 1st row!", true, iterator.hasNext()); - Assert.assertEquals("Failed to match 1st row!", row, iterator.next()); } - } - - @Test - public void testSpecialQuote() throws Exception { - - String csv = "\001aaa\001,\001b\001\001bb\001,\001ccc\001"; - ParseOptions po = new ParseOptions(); - po.setOption(ParseOptions.OPTIONS_CSV_QUOTE,'\001'); - - try( - StringReader sr = new StringReader(csv); - CSVParser jp = new CSVParser(sr, po); - ) { - - Iterator<Row> iterator = jp.iterator(); - Row row = new Row(new Object[]{"aaa", "b\001bb", "ccc"}); - Assert.assertEquals("Failed to detect 1st row!", true, iterator.hasNext()); - Assert.assertEquals("Failed to match 1st row!", row, iterator.next()); - } - } - - @Test - public void testSpaceAsDelimiterAndQuoted() throws Exception { - - String csv = "aaa \"b bb\" ccc\naaa2 bbb2 \"c cc2\""; - ParseOptions po = new ParseOptions(); -// po.setOption(ParseOptions.OPTIONS_CSV_ESCAPE_CHAR,'\001'); - po.setOption(ParseOptions.OPTIONS_CSV_DELIMITER,' '); - - try( - StringReader sr = new StringReader(csv); - CSVParser jp = new CSVParser(sr, po); - ) { - - Iterator<Row> iterator = jp.iterator(); - Row row = new Row(new Object[]{"aaa", "b bb", "ccc"}); - Assert.assertEquals("Failed to detect 1st row!", true, iterator.hasNext()); - Assert.assertEquals("Failed to match 1st row!", row, iterator.next()); - - Row row2 = new Row(new Object[]{"aaa2", "bbb2", "c cc2"}); - Assert.assertEquals("Failed to detect 1st row!", true, iterator.hasNext()); - Assert.assertEquals("Failed to match 1st row!", row2, iterator.next()); - } - } - - @Test - public void testFailedDelimiterEscaped() throws Exception { - - String csv = "aaa,b\\,bb,ccc"; - ParseOptions po = new ParseOptions(); - po.setOption(ParseOptions.OPTIONS_CSV_ESCAPE_CHAR,'\\'); - po.setOption(ParseOptions.OPTIONS_CSV_DELIMITER,','); - - try( - StringReader sr = new StringReader(csv); - CSVParser jp = new CSVParser(sr, po); - ) { - - Iterator<Row> iterator = jp.iterator(); - Row row = new Row(new Object[]{"aaa", "b,bb", "ccc"}); - Assert.assertEquals("Failed to detect 1st row!", true, iterator.hasNext()); - Assert.assertEquals("Failed to match 1st row!", row, iterator.next()); - } - } -} http://git-wip-us.apache.org/repos/asf/ambari/blob/e423a65e/contrib/views/hive/src/test/java/org/apache/ambari/view/hive/resources/upload/DataParserCSVTest.java ---------------------------------------------------------------------- diff --git a/contrib/views/hive/src/test/java/org/apache/ambari/view/hive/resources/upload/DataParserCSVTest.java b/contrib/views/hive/src/test/java/org/apache/ambari/view/hive/resources/upload/DataParserCSVTest.java deleted file mode 100644 index 7362c89..0000000 --- a/contrib/views/hive/src/test/java/org/apache/ambari/view/hive/resources/upload/DataParserCSVTest.java +++ /dev/null @@ -1,326 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * <p/> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p/> - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ambari.view.hive.resources.upload; - -import org.apache.ambari.view.hive.client.ColumnDescription; -import org.apache.ambari.view.hive.client.ColumnDescriptionShort; -import org.apache.ambari.view.hive.client.Row; -import org.apache.ambari.view.hive.resources.uploads.ColumnDescriptionImpl; -import org.apache.ambari.view.hive.resources.uploads.parsers.DataParser; -import org.apache.ambari.view.hive.resources.uploads.parsers.ParseOptions; -import org.apache.ambari.view.hive.resources.uploads.parsers.PreviewData; -import org.junit.Assert; -import org.junit.Test; - -import java.io.IOException; -import java.io.StringReader; - -public class DataParserCSVTest { - @Test - public void testParsePreviewCSV() throws Exception { - String str = "1,a\n" + - "2,b\n" + - "3,c\n"; - - - ParseOptions parseOptions = new ParseOptions(); - parseOptions.setOption(ParseOptions.OPTIONS_FILE_TYPE, ParseOptions.InputFileType.CSV.toString()); - parseOptions.setOption(ParseOptions.OPTIONS_HEADER, ParseOptions.HEADER.FIRST_RECORD.toString()); - - - try ( - StringReader sr = new StringReader(str); - DataParser dp = new DataParser(sr, parseOptions); - ){ - - PreviewData pd = dp.parsePreview(); - Assert.assertNotNull(pd.getPreviewRows()); - Assert.assertNotNull(pd.getHeader()); - Assert.assertEquals(2, pd.getPreviewRows().size()); // now it will not return the first row which is header - Assert.assertEquals(2, pd.getHeader().size()); - ColumnDescription[] cd = {new ColumnDescriptionImpl("1", ColumnDescriptionShort.DataTypes.INT.toString(), 0), - new ColumnDescriptionImpl("a", ColumnDescriptionShort.DataTypes.CHAR.toString(), 1)}; - - Object cols2[] = new Object[2]; - cols2[0] = "2"; - cols2[1] = "b"; - Row row2 = new Row(cols2); - - Object cols3[] = new Object[2]; - cols3[0] = "3"; - cols3[1] = "c"; - Row row3 = new Row(cols3); - - Row[] rows = { row2, row3}; - - Assert.assertArrayEquals("Header Not Correct.", cd, pd.getHeader().toArray()); - Assert.assertArrayEquals("Rows Not Correct.", rows, pd.getPreviewRows().toArray()); - } - } - - /** - * even if in one of the preview rows, datatype is not correct, then it should be assigned that datatype. - * but if first row is header then first row should not be acconted for detecting datatype - * @throws IOException - */ - @Test - public void testParsePreviewDataTypeDetectionCSV() throws Exception { - String str = "1,a,10,k\n" + - "2,b,6,8\n" + - "2.2,b,7,9\n" + - "2,b,abc,1\n" + - "2,b,9,3\n" + - "2,b,8,5\n" + - "2,b,7,3\n" + - "2,b,6,3\n" + - "3,c,c,3\n"; - - - ParseOptions parseOptions = new ParseOptions(); - parseOptions.setOption(ParseOptions.OPTIONS_FILE_TYPE, ParseOptions.InputFileType.CSV.toString()); - parseOptions.setOption(ParseOptions.OPTIONS_HEADER, ParseOptions.HEADER.FIRST_RECORD.toString()); - - try(StringReader sr = new StringReader(str); - DataParser dp= new DataParser(sr, parseOptions)) { - - PreviewData pd = dp.parsePreview(); - Assert.assertNotNull(pd.getHeader()); - Assert.assertEquals(4, pd.getHeader().size()); - ColumnDescription[] cd = { - // as row 3 contains 2.2 - new ColumnDescriptionImpl("1", ColumnDescriptionShort.DataTypes.DOUBLE.toString(), 0), - // as all are chars - new ColumnDescriptionImpl("a", ColumnDescriptionShort.DataTypes.CHAR.toString(), 1), - // as row 4 contains abc - new ColumnDescriptionImpl("10", ColumnDescriptionShort.DataTypes.STRING.toString(), 2), - // although row 1 contains k but it is in header and not counted in detecting datatype - new ColumnDescriptionImpl("k", ColumnDescriptionShort.DataTypes.INT.toString(), 3)}; - - Assert.assertArrayEquals("Header Not Correct.", cd, pd.getHeader().toArray()); - } - } - - /** - * even if in one of the preview rows, datatype is not correct, then it should be assigned that datatype. - * but if first row is header then first row should not be acconted for detecting datatype - * @throws IOException - */ - @Test - public void testParsePreviewDataTypeDetection2CSV() throws Exception { - String str = "1,a,10,k\n" + - "2,b,6,p\n" + - "2.2,b,7,9\n" + - "2,b,2.2,1\n" + - "2,b,9,3\n" + - "2,b,8,5\n" + - "2,b,7,3\n" + - "2,b,6,3\n" + - "3,c,c,3\n"; - - - ParseOptions parseOptions = new ParseOptions(); - parseOptions.setOption(ParseOptions.OPTIONS_FILE_TYPE, ParseOptions.InputFileType.CSV.toString()); - parseOptions.setOption(ParseOptions.OPTIONS_HEADER, ParseOptions.HEADER.FIRST_RECORD.toString()); - - - try(StringReader sr = new StringReader(str); - DataParser dp = new DataParser(sr, parseOptions)) { - - - PreviewData pd = dp.parsePreview(); - Assert.assertNotNull(pd.getHeader()); - Assert.assertEquals(4, pd.getHeader().size()); - ColumnDescription[] cd = { - // as row 3 contains 2.2 - new ColumnDescriptionImpl("1", ColumnDescriptionShort.DataTypes.DOUBLE.toString(), 0), - // as all are chars - new ColumnDescriptionImpl("a", ColumnDescriptionShort.DataTypes.CHAR.toString(), 1), - // some are int, char and some double .. nothing other than 'string' satisfies all the rows - new ColumnDescriptionImpl("10", ColumnDescriptionShort.DataTypes.STRING.toString(), 2), - // although row 1 contains k but it is in header and not counted in detecting datatype - // but row 2 also has a char p which will be acconted for datatype detection - new ColumnDescriptionImpl("k", ColumnDescriptionShort.DataTypes.CHAR.toString(), 3)}; - - Assert.assertArrayEquals("Header Not Correct.", cd, pd.getHeader().toArray()); - } - } - - /** - * One row csv will give default column names and 1st row in preview if HEADER.PROVIDED_BY_USER is selected - * @throws IOException - */ - @Test - public void testParsePreview1RowCSV() throws Exception { - String str = "1,a\n" ; - - ParseOptions parseOptions = new ParseOptions(); - parseOptions.setOption(ParseOptions.OPTIONS_FILE_TYPE, ParseOptions.InputFileType.CSV.toString()); - parseOptions.setOption(ParseOptions.OPTIONS_HEADER, ParseOptions.HEADER.PROVIDED_BY_USER.toString()); - - try( - StringReader sr = new StringReader(str); - DataParser dp = new DataParser(sr, parseOptions) - ) { - - - PreviewData pd = dp.parsePreview(); - Assert.assertNotNull(pd.getPreviewRows()); - Assert.assertNotNull(pd.getHeader()); - Assert.assertEquals(1, pd.getPreviewRows().size()); - Assert.assertEquals(2, pd.getHeader().size()); - ColumnDescription[] cd = {new ColumnDescriptionImpl("column1", ColumnDescriptionShort.DataTypes.INT.toString(), 0), - new ColumnDescriptionImpl("column2", ColumnDescriptionShort.DataTypes.CHAR.toString(), 1)}; - - Object cols1[] = new Object[2]; - cols1[0] = "1"; - cols1[1] = "a"; - Row row1 = new Row(cols1); - - Row[] rows = {row1}; - - Assert.assertArrayEquals("Header Not Correct.", cd, pd.getHeader().toArray()); - Assert.assertArrayEquals("Rows Not Correct.", rows, pd.getPreviewRows().toArray()); - } - } - - /** - * One row csv will throw exception in preview if HEADER.FIRST_RECORD is selected. - * @throws IOException - */ - @Test(expected = java.util.NoSuchElementException.class) - public void testParsePreview1RowCSVFirstRowHeader() throws Exception { - String str = "col1,col2\n" ; - - - ParseOptions parseOptions = new ParseOptions(); - parseOptions.setOption(ParseOptions.OPTIONS_FILE_TYPE, ParseOptions.InputFileType.CSV.toString()); - parseOptions.setOption(ParseOptions.OPTIONS_HEADER, ParseOptions.HEADER.FIRST_RECORD.toString()); - - - try( - StringReader sr = new StringReader(str); - DataParser dp = new DataParser(sr, parseOptions) - ) { - - - PreviewData pd = dp.parsePreview(); - } - } - - /** - * more number of columns in a row => igore the extra columns. Number of columns is decided by the first row. - * If other row contains more columns then those columns will be ignored - * Here first row has 2 columns and second row has 3 columns so the value 'x' is ignored - * @throws IOException - */ - @Test - public void testParsePreviewCSVMoreColumns() throws Exception { - String str = "1,a\n" + - "2,b,x\n" + // contains 3 cols, more number of columns - "3,c\n"; - - ParseOptions parseOptions = new ParseOptions(); - parseOptions.setOption(ParseOptions.OPTIONS_FILE_TYPE, ParseOptions.InputFileType.CSV.toString()); - parseOptions.setOption(ParseOptions.OPTIONS_HEADER, ParseOptions.HEADER.FIRST_RECORD.toString()); - - - try( - StringReader sr = new StringReader(str); - DataParser dp = new DataParser(sr, parseOptions) - ) { - - PreviewData pd = dp.parsePreview(); - Row row = new Row(new Object[]{"2","b"}); - - Assert.assertArrayEquals("Additional columns not properly handled.", row.getRow(),pd.getPreviewRows().get(0).getRow()); - } - } - - /** - * less number of columns => treat missing values as null. Number of columns is decided by the first row of the table - * if other rows has less number of columns then it treats other columns as null - * @throws IOException - */ - @Test - public void testParsePreviewCSVLessColumns() throws Exception { - String str = "1,a\n" + - "2\n" + // contains 1 col, less number of columns - "3,c\n"; - - ParseOptions parseOptions = new ParseOptions(); - parseOptions.setOption(ParseOptions.OPTIONS_FILE_TYPE, ParseOptions.InputFileType.CSV.toString()); - - try( - StringReader sr = new StringReader(str); - DataParser dp = new DataParser(sr, parseOptions) - ) { - - PreviewData pd = dp.parsePreview(); - Assert.assertEquals("Missing value not detected as null.",pd.getPreviewRows().get(1).getRow()[1],null); - } - } - - /** - * empty values are treated as empty string - * @throws IOException - */ - @Test - public void testEmptyColumn() throws Exception { - String str = "1,a,x\n" + - "2,,y\n" + // contains 1 col, less number of columns - "3,c,z\n"; - ParseOptions parseOptions = new ParseOptions(); - parseOptions.setOption(ParseOptions.OPTIONS_FILE_TYPE, ParseOptions.InputFileType.CSV.toString()); - parseOptions.setOption(ParseOptions.OPTIONS_HEADER, ParseOptions.HEADER.FIRST_RECORD.toString()); - - try( - StringReader sr = new StringReader(str); - DataParser dp = new DataParser(sr, parseOptions) - ) { - - PreviewData pd = dp.parsePreview(); - Assert.assertEquals("Empty column not detected properly.",pd.getPreviewRows().get(0).getRow()[1],""); - } - } - - /** - * empty values are treated as empty string - * @throws IOException - */ - @Test - public void testLastEmptyColumn() throws Exception { - String str = "1,a,x\n" + - "2,,\n" + // contains 1 col, less number of columns - "3,c,z\n"; - - ParseOptions parseOptions = new ParseOptions(); - parseOptions.setOption(ParseOptions.OPTIONS_FILE_TYPE, ParseOptions.InputFileType.CSV.toString()); - parseOptions.setOption(ParseOptions.OPTIONS_HEADER, ParseOptions.HEADER.FIRST_RECORD.toString()); - - try( - StringReader sr = new StringReader(str); - DataParser dp = new DataParser(sr, parseOptions) - ) { - - PreviewData pd = dp.parsePreview(); - Assert.assertEquals("Empty column not detected properly.",pd.getPreviewRows().get(0).getRow()[1],""); - Assert.assertEquals("Empty column not detected properly.",pd.getPreviewRows().get(0).getRow()[2],""); - } - } -} http://git-wip-us.apache.org/repos/asf/ambari/blob/e423a65e/contrib/views/hive/src/test/java/org/apache/ambari/view/hive/resources/upload/DataParserJSONTest.java ---------------------------------------------------------------------- diff --git a/contrib/views/hive/src/test/java/org/apache/ambari/view/hive/resources/upload/DataParserJSONTest.java b/contrib/views/hive/src/test/java/org/apache/ambari/view/hive/resources/upload/DataParserJSONTest.java deleted file mode 100644 index 2ee92df..0000000 --- a/contrib/views/hive/src/test/java/org/apache/ambari/view/hive/resources/upload/DataParserJSONTest.java +++ /dev/null @@ -1,263 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * <p/> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p/> - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ambari.view.hive.resources.upload; - -import org.apache.ambari.view.hive.client.ColumnDescription; -import org.apache.ambari.view.hive.client.ColumnDescriptionShort; -import org.apache.ambari.view.hive.client.Row; -import org.apache.ambari.view.hive.resources.uploads.ColumnDescriptionImpl; -import org.apache.ambari.view.hive.resources.uploads.parsers.DataParser; -import org.apache.ambari.view.hive.resources.uploads.parsers.ParseOptions; -import org.apache.ambari.view.hive.resources.uploads.parsers.PreviewData; -import org.junit.Assert; -import org.junit.Test; - -import java.io.IOException; -import java.io.StringReader; - -public class DataParserJSONTest { - - @Test - public void testParsePreviewJSON() throws Exception { - String str = "[ {\"col1\" : \"a\", \n\"col2\": \"abcd\" ,\"col3\": \"abcd\" ,\"col4\": \"abcd\" ,\"col5\": \"abcd\" ,\"col6\": \"abcd\" ,\"col7\": \"abcd\" ,\"col8\": \"abcd\" ,\"col9\": \"abcd\" ,\"col10\": \"abcd\" ,\"col11\": \"abcd\" ,\"col12\" : \"abcd\" ,\"col13\" : \"abcd\" ,\"col14\" : \"4.4\" }," - + "{\"col1\": \"b\", \n\"col2\": \"abcd\" ,\"col3\": \"abcd\" ,\"col4\": \"abcd\" ,\"col5\": \"abcd\" ,\"col6\": \"abcd\" ,\"col7\": \"abcd\" ,\"col8\": \"abcd\" ,\"col9\": \"abcd\" ,\"col10\": \"abcd\" ,\"col11\": \"abcd\" ,\"col12\" : \"abcd\" ,\"col13\" : \"abcd\" ,\"col14\" : \"5.4\" }," - + "{\"col1\": \"c\", \n\"col2\": \"abcd\" ,\"col3\": \"abcd\" ,\"col4\": \"abcd\" ,\"col5\": \"abcd\" ,\"col6\": \"abcd\" ,\"col7\": \"abcd\" ,\"col8\": \"abcd\" ,\"col9\": \"abcd\" ,\"col10\": \"abcd\" ,\"col11\": \"abcd\" ,\"col12\" : \"abcd\" ,\"col13\" : \"abcd\" ,\"col14\" : \"6.4\" }," - + "{\"col1\": \"d\", \n\"col2\": \"abcd\" ,\"col3\": \"abcd\" ,\"col4\": \"abcd\" ,\"col5\": \"abcd\" ,\"col6\": \"abcd\" ,\"col7\": \"abcd\" ,\"col8\": \"abcd\" ,\"col9\": \"abcd\" ,\"col10\": \"abcd\" ,\"col11\": \"abcd\" ,\"col12\" : \"abcd\" ,\"col13\" : \"abcd\" ,\"col14\" : \"7.4\" }," - + "{\"col1\": \"e\", \n\"col2\": \"abcd\" ,\"col3\": \"abcd\" ,\"col4\": \"abcd\" ,\"col5\": \"abcd\" ,\"col6\": \"abcd\" ,\"col7\": \"abcd\" ,\"col8\": \"abcd\" ,\"col9\": \"abcd\" ,\"col10\": \"abcd\" ,\"col11\": \"abcd\" ,\"col12\" : \"abcd\" ,\"col13\" : \"abcd\" ,\"col14\" : \"8.4\" }," - + "{\"col1\": \"f\", \n\"col2\": \"abcd\" ,\"col3\": \"abcd\" ,\"col4\": \"abcd\" ,\"col5\": \"abcd\" ,\"col6\": \"abcd\" ,\"col7\": \"abcd\" ,\"col8\": \"abcd\" ,\"col9\": \"abcd\" ,\"col10\": \"abcd\" ,\"col11\": \"abcd\" ,\"col12\" : \"abcd\" ,\"col13\" : \"abcd\" ,\"col14\" : \"9.4\" }," - + "{\"col1\": \"g\", \n\"col2\": \"abcd\" ,\"col3\": \"abcd\" ,\"col4\": \"abcd\" ,\"col5\": \"abcd\" ,\"col6\": \"abcd\" ,\"col7\": \"abcd\" ,\"col8\": \"abcd\" ,\"col9\": \"abcd\" ,\"col10\": \"abcd\" ,\"col11\": \"abcd\" ,\"col12\" : \"abcd\" ,\"col13\" : \"abcd\" ,\"col14\" : \"10.4\" }," - + "{\"col1\": \"h\", \n\"col2\": \"abcd\" ,\"col3\": \"abcd\" ,\"col4\": \"abcd\" ,\"col5\": \"abcd\" ,\"col6\": \"abcd\" ,\"col7\": \"abcd\" ,\"col8\": \"abcd\" ,\"col9\": \"abcd\" ,\"col10\": \"abcd\" ,\"col11\": \"abcd\" ,\"col12\" : \"abcd\" ,\"col13\" : \"abcd\" ,\"col14\" : \"11.4\" }," - + "{\"col1\": \"i\", \n\"col2\": \"abcd\" ,\"col3\": \"abcd\" ,\"col4\": \"abcd\" ,\"col5\": \"abcd\" ,\"col6\": \"abcd\" ,\"col7\": \"abcd\" ,\"col8\": \"abcd\" ,\"col9\": \"abcd\" ,\"col10\": \"abcd\" ,\"col11\": \"abcd\" ,\"col12\" : \"abcd\" ,\"col13\" : \"abcd\" ,\"col14\" : \"4\" }," - + "{\"col1\": \"j\", \n\"col2\": \"abcd\" ,\"col3\": \"abcd\" ,\"col4\": \"abcd\" ,\"col5\": \"abcd\" ,\"col6\": \"abcd\" ,\"col7\": \"abcd\" ,\"col8\": \"abcd\" ,\"col9\": \"abcd\" ,\"col10\": \"abcd\" ,\"col11\": \"abcd\" ,\"col12\" : \"abcd\" ,\"col13\" : \"abcd\" ,\"col14\" : \"5\" }," - + "{\"col1\": \"k\", \n\"col2\": \"abcd\" ,\"col3\": \"abcd\" ,\"col4\": \"abcd\" ,\"col5\": \"abcd\" ,\"col6\": \"abcd\" ,\"col7\": \"abcd\" ,\"col8\": \"abcd\" ,\"col9\": \"abcd\" ,\"col10\": \"abcd\" ,\"col11\": \"abcd\" ,\"col12\" : \"abcd\" ,\"col13\" : \"abcd\" ,\"col14\" : \"6\" }," - + "{\"col1\": \"l\", \n\"col2\": \"abcd\" ,\"col3\": \"abcd\" ,\"col4\": \"abcd\" ,\"col5\": \"abcd\" ,\"col6\": \"abcd\" ,\"col7\": \"abcd\" ,\"col8\": \"abcd\" ,\"col9\": \"abcd\" ,\"col10\": \"abcd\" ,\"col11\": \"abcd\" ,\"col12\" : \"abcd\" ,\"col13\" : \"abcd\" ,\"col14\" : \"7\" }," - + "{\"col1\": \"m\", \n\"col2\": \"abcd\" ,\"col3\": \"abcd\" ,\"col4\": \"abcd\" ,\"col5\": \"abcd\" ,\"col6\": \"abcd\" ,\"col7\": \"abcd\" ,\"col8\": \"abcd\" ,\"col9\": \"abcd\" ,\"col10\": \"abcd\" ,\"col11\": \"abcd\" ,\"col12\" : \"abcd\" ,\"col13\" : \"abcd\" ,\"col14\" : \"24.4\" }," - + "{\"col1\": \"n\", \n\"col2\": \"abcd\" ,\"col3\": \"abcd\" ,\"col4\": \"abcd\" ,\"col5\": \"abcd\" ,\"col6\": \"abcd\" ,\"col7\": \"abcd\" ,\"col8\": \"abcd\" ,\"col9\": \"abcd\" ,\"col10\": \"abcd\" ,\"col11\": \"abcd\" ,\"col12\" : \"abcd\" ,\"col13\" : \"abcd\" ,\"col14\" : \"14.4\" }," - + "{\"col1\": \"o\", \n\"col2\": \"abcd\" ,\"col3\": \"abcd\" ,\"col4\": \"abcd\" ,\"col5\": \"abcd\" ,\"col6\": \"abcd\" ,\"col7\": \"abcd\" ,\"col8\": \"abcd\" ,\"col9\": \"abcd\" ,\"col10\": \"abcd\" ,\"col11\": \"abcd\" ,\"col12\" : \"abcd\" ,\"col13\" : \"abcd\" ,\"col14\" : \"34.4\" }," - + "{\"col1\": \"p\", \n\"col2\": \"abcd\" ,\"col3\": \"abcd\" ,\"col4\": \"abcd\" ,\"col5\": \"abcd\" ,\"col6\": \"abcd\" ,\"col7\": \"abcd\" ,\"col8\": \"abcd\" ,\"col9\": \"abcd\" ,\"col10\": \"abcd\" ,\"col11\": \"abcd\" ,\"col12\" : \"abcd\" ,\"col13\" : \"abcd\" ,\"col14\" : \"44.4\" }," - + "{\"col1\": \"q\", \n\"col2\": \"abcd\" ,\"col3\": \"abcd\" ,\"col4\": \"abcd\" ,\"col5\": \"abcd\" ,\"col6\": \"abcd\" ,\"col7\": \"abcd\" ,\"col8\": \"abcd\" ,\"col9\": \"abcd\" ,\"col10\": \"abcd\" ,\"col11\": \"abcd\" ,\"col12\" : \"abcd\" ,\"col13\" : \"abcd\" ,\"col14\" : \"54.4\" }," - + "{\"col1\": \"r\", \n\"col2\": \"abcd\" ,\"col3\": \"abcd\" ,\"col4\": \"abcd\" ,\"col5\": \"abcd\" ,\"col6\": \"abcd\" ,\"col7\": \"abcd\" ,\"col8\": \"abcd\" ,\"col9\": \"abcd\" ,\"col10\": \"abcd\" ,\"col11\": \"abcd\" ,\"col12\" : \"abcd\" ,\"col13\" : \"abcd\" ,\"col14\" : \"64.4\" }" - + "]"; - - - ParseOptions parseOptions = new ParseOptions(); - parseOptions.setOption(ParseOptions.OPTIONS_FILE_TYPE, ParseOptions.InputFileType.JSON.toString()); - parseOptions.setOption(ParseOptions.OPTIONS_HEADER, ParseOptions.HEADER.EMBEDDED.toString()); - parseOptions.setOption(ParseOptions.OPTIONS_NUMBER_OF_PREVIEW_ROWS, 7); - - try( - StringReader sr = new StringReader(str); - DataParser dp = new DataParser(sr, parseOptions) - ) { - - PreviewData pd = dp.parsePreview(); - Assert.assertNotNull(pd.getPreviewRows()); - Assert.assertNotNull(pd.getHeader()); - Assert.assertEquals(7, pd.getPreviewRows().size()); // header row + preview rows - Assert.assertEquals(14, pd.getHeader().size()); - ColumnDescription[] cd = {new ColumnDescriptionImpl("col1", ColumnDescriptionShort.DataTypes.CHAR.toString(), 0), - new ColumnDescriptionImpl("col2", ColumnDescriptionShort.DataTypes.STRING.toString(), 1), - new ColumnDescriptionImpl("col3", ColumnDescriptionShort.DataTypes.STRING.toString(), 2), - new ColumnDescriptionImpl("col4", ColumnDescriptionShort.DataTypes.STRING.toString(), 3), - new ColumnDescriptionImpl("col5", ColumnDescriptionShort.DataTypes.STRING.toString(), 4), - new ColumnDescriptionImpl("col6", ColumnDescriptionShort.DataTypes.STRING.toString(), 5), - new ColumnDescriptionImpl("col7", ColumnDescriptionShort.DataTypes.STRING.toString(), 6), - new ColumnDescriptionImpl("col8", ColumnDescriptionShort.DataTypes.STRING.toString(), 7), - new ColumnDescriptionImpl("col9", ColumnDescriptionShort.DataTypes.STRING.toString(), 8), - new ColumnDescriptionImpl("col10", ColumnDescriptionShort.DataTypes.STRING.toString(), 9), - new ColumnDescriptionImpl("col11", ColumnDescriptionShort.DataTypes.STRING.toString(), 10), - new ColumnDescriptionImpl("col12", ColumnDescriptionShort.DataTypes.STRING.toString(), 11), - new ColumnDescriptionImpl("col13", ColumnDescriptionShort.DataTypes.STRING.toString(), 12), - new ColumnDescriptionImpl("col14", ColumnDescriptionShort.DataTypes.DOUBLE.toString(), 13)}; - - Row row2 = new Row(new Object[]{"a", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "4.4"}); - Row row3 = new Row(new Object[]{"b", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "5.4"}); - Row row4 = new Row(new Object[]{"c", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "6.4"}); - Row row5 = new Row(new Object[]{"d", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "7.4"}); - Row row6 = new Row(new Object[]{"e", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "8.4"}); - Row row7 = new Row(new Object[]{"f", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "9.4"}); - Row row8 = new Row(new Object[]{"g", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "10.4"}); - - Row[] rows = { row2, row3, row4, row5, row6, row7, row8}; - - Assert.assertArrayEquals("Header Not Correct.", cd, pd.getHeader().toArray()); - Assert.assertArrayEquals("Rows Not Correct.", rows, pd.getPreviewRows().toArray()); - } - } - - /** - * additional columns in rows of JSON are ignored. - * - * @throws IOException - */ - @Test - public void testParsePreviewCSVMoreColumns() throws Exception { - String str = "[ {\"col1\" : \"a\", \n\"col2\": \"abcd\" ,\"col3\": \"abcd\" ,\"col4\": \"abcd\" ,\"col5\": \"abcd\" ,\"col6\": \"abcd\" ,\"col7\": \"abcd\" ,\"col8\": \"abcd\" ,\"col9\": \"abcd\" ,\"col10\": \"abcd\" ,\"col11\": \"abcd\" ,\"col12\" : \"abcd\" ,\"col13\" : \"abcd\" ,\"col14\" : \"4.4\" }," - + "{\"col1\": \"b\", \n\"col2\": \"abcd\" ,\"col3\": \"abcd\" ,\"col4\": \"abcd\" ,\"col5\": \"abcd\" ,\"col6\": \"abcd\" ,\"col7\": \"abcd\" ,\"col8\": \"abcd\" ,\"col9\": \"abcd\" ,\"col10\": \"abcd\" ,\"col11\": \"abcd\" ,\"col12\" : \"abcd\" ,\"col13\" : \"abcd\" , \"col14\" : \"43.4\" ,\"col15\" : \"asafsfa\" }," - + "{\"col1\": \"c\", \n\"col2\": \"abcd\" ,\"col3\": \"abcd\" ,\"col4\": \"abcd\" ,\"col5\": \"abcd\" ,\"col6\": \"abcd\" ,\"col7\": \"abcd\" ,\"col8\": \"abcd\" ,\"col9\": \"abcd\" ,\"col10\": \"abcd\" ,\"col11\": \"abcd\" ,\"col12\" : \"abcd\" ,\"col13\" : \"abcd\" ,\"col14\" : \"6.4\" }," - + "{\"col1\": \"d\", \n\"col2\": \"abcd\" ,\"col3\": \"abcd\" ,\"col4\": \"abcd\" ,\"col5\": \"abcd\" ,\"col6\": \"abcd\" ,\"col7\": \"abcd\" ,\"col8\": \"abcd\" ,\"col9\": \"abcd\" ,\"col10\": \"abcd\" ,\"col11\": \"abcd\" ,\"col12\" : \"abcd\" ,\"col13\" : \"abcd\" ,\"col14\" : \"7.4\" }" - + "]"; - - - ParseOptions parseOptions = new ParseOptions(); - parseOptions.setOption(ParseOptions.OPTIONS_FILE_TYPE, ParseOptions.InputFileType.JSON.toString()); - - try( - StringReader sr = new StringReader(str); - DataParser dp = new DataParser(sr, parseOptions) - ) { - - PreviewData pd = dp.parsePreview(); - - Row row2 = new Row(new Object[]{"b", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "43.4"}); - Assert.assertArrayEquals("More number of columns do not give correct result.", row2.getRow(), pd.getPreviewRows().get(1).getRow()); - } - } - - /** - * less columns in json makes them null. - * - * @throws IOException - */ - @Test - public void testParsePreviewCSVLessColumns() throws Exception { - String str = "[ " + - "{\"col1\" : \"a\", \n\"col2\": \"abcd\" ,\"col3\": \"abcd\" ,\"col4\": \"abcd\" ,\"col5\": \"abcd\" ,\"col6\": \"abcd\" ,\"col7\": \"abcd\" ,\"col8\": \"abcd\" ,\"col9\": \"abcd\" ,\"col10\": \"abcd\" ,\"col11\": \"abcd\" ,\"col12\" : \"abcd\" ,\"col13\" : \"abcd\" ,\"col14\" : \"4.4\" }," - + "{\"col1\": \"b\", \n\"col2\": \"abcd\" ,\"col3\": \"abcd\" ,\"col4\": \"abcd\" ,\"col5\": \"abcd\" ,\"col6\": \"abcd\" ,\"col7\": \"abcd\" ,\"col8\": \"abcd\" ,\"col9\": \"abcd\" ,\"col10\": \"abcd\" ,\"col11\": \"abcd\" ,\"col12\" : \"abcd\" ,\"col13\" : \"abcd\" }," - + "{\"col1\": \"c\", \n\"col2\": \"abcd\" ,\"col3\": \"abcd\" ,\"col4\": \"abcd\" ,\"col5\": \"abcd\" ,\"col6\": \"abcd\" ,\"col7\": \"abcd\" ,\"col8\": \"abcd\" ,\"col9\": \"abcd\" ,\"col10\": \"abcd\" ,\"col11\": \"abcd\" ,\"col12\" : \"abcd\" ,\"col13\" : \"abcd\" ,\"col14\" : \"6.4\" }," - + "{\"col1\": \"d\", \n\"col2\": \"abcd\" ,\"col3\": \"abcd\" ,\"col4\": \"abcd\" ,\"col5\": \"abcd\" ,\"col6\": \"abcd\" ,\"col7\": \"abcd\" ,\"col8\": \"abcd\" ,\"col9\": \"abcd\" ,\"col10\": \"abcd\" ,\"col11\": \"abcd\" ,\"col12\" : \"abcd\" ,\"col13\" : \"abcd\" ,\"col14\" : \"7.4\" }" - + "]"; - - ParseOptions parseOptions = new ParseOptions(); - parseOptions.setOption(ParseOptions.OPTIONS_FILE_TYPE, ParseOptions.InputFileType.JSON.toString()); - parseOptions.setOption(ParseOptions.OPTIONS_HEADER, ParseOptions.HEADER.EMBEDDED.toString()); - - try( - StringReader sr = new StringReader(str); - DataParser dp = new DataParser(sr, parseOptions) - ) { - PreviewData pd = dp.parsePreview(); - - Assert.assertNull(pd.getPreviewRows().get(1).getRow()[13]); - } - } - - /** - * illegal json format gives error - * - * @throws IOException - */ - @Test(expected = IllegalArgumentException.class) - public void testWrongJsonFormat() throws Exception { - String str = "[ " + - "{\"col1\" : \"a\", \n\"col2\": \"abcd\" }," - + "{\"col1\": \"b\", \n\"col2\": \"abcd\" }," - + "{\"col1\": \"c\", \n\"col2\": \"abcd\" }," - + "{\"col1\": \"d\",, \n\"col2\": \"abcd\" }" // extra comma in this line - + "]"; - ParseOptions parseOptions = new ParseOptions(); - parseOptions.setOption(ParseOptions.OPTIONS_FILE_TYPE, ParseOptions.InputFileType.JSON.toString()); - parseOptions.setOption(ParseOptions.OPTIONS_HEADER, ParseOptions.HEADER.EMBEDDED.toString()); - - try( - StringReader sr = new StringReader(str); - DataParser dp = new DataParser(sr, parseOptions); - ) { - PreviewData pd = dp.parsePreview(); - } - } - - /** - * One row JSON will give embedde column names and 1st row in preview if HEADER.EMBEDDED is selected - * @throws IOException - */ - @Test - public void testParsePreview1RowJSON() throws Exception { - String str = "[ " - + "{\"col1\": \"d\", \n\"col2\": \"abcd\" }" // extra comma in this line - + "]"; - - ParseOptions parseOptions = new ParseOptions(); - parseOptions.setOption(ParseOptions.OPTIONS_FILE_TYPE, ParseOptions.InputFileType.JSON.toString()); - parseOptions.setOption(ParseOptions.OPTIONS_HEADER, ParseOptions.HEADER.EMBEDDED.toString()); - - try( - StringReader sr = new StringReader(str); - DataParser dp = new DataParser(sr, parseOptions); - ) { - - PreviewData pd = dp.parsePreview(); - Assert.assertNotNull(pd.getPreviewRows()); - Assert.assertNotNull(pd.getHeader()); - Assert.assertEquals(1, pd.getPreviewRows().size()); - Assert.assertEquals(2, pd.getHeader().size()); - ColumnDescription[] cd = {new ColumnDescriptionImpl("col1", ColumnDescriptionShort.DataTypes.CHAR.toString(), 0), - new ColumnDescriptionImpl("col2", ColumnDescriptionShort.DataTypes.STRING.toString(), 1)}; - - Object cols1[] = new Object[2]; - cols1[0] = "d"; - cols1[1] = "abcd"; - Row row1 = new Row(cols1); - - Row[] rows = {row1}; - - Assert.assertArrayEquals("Header Not Correct.", cd, pd.getHeader().toArray()); - Assert.assertArrayEquals("Rows Not Correct.", rows, pd.getPreviewRows().toArray()); - } - } - - /** - * One row JSON will give default column names and 1st row in preview if HEADER.PROVIDED_BY_USER is selected - * @throws IOException - */ - @Test - public void testParsePreview1RowJSONHeaderProvided() throws Exception { - String str = "[ " - + "{\"col1\": \"d\", \n\"col2\": \"abcd\" }" // extra comma in this line - + "]"; - - ParseOptions parseOptions = new ParseOptions(); - parseOptions.setOption(ParseOptions.OPTIONS_FILE_TYPE, ParseOptions.InputFileType.JSON.toString()); - parseOptions.setOption(ParseOptions.OPTIONS_HEADER, ParseOptions.HEADER.PROVIDED_BY_USER.toString()); - - try( - StringReader sr = new StringReader(str); - DataParser dp = new DataParser(sr, parseOptions); - ) { - - PreviewData pd = dp.parsePreview(); - Assert.assertNotNull(pd.getPreviewRows()); - Assert.assertNotNull(pd.getHeader()); - Assert.assertEquals(1, pd.getPreviewRows().size()); - Assert.assertEquals(2, pd.getHeader().size()); - ColumnDescription[] cd = {new ColumnDescriptionImpl("column1", ColumnDescriptionShort.DataTypes.CHAR.toString(), 0), - new ColumnDescriptionImpl("column2", ColumnDescriptionShort.DataTypes.STRING.toString(), 1)}; - - Object cols1[] = new Object[2]; - cols1[0] = "d"; - cols1[1] = "abcd"; - Row row1 = new Row(cols1); - - Row[] rows = {row1}; - - Assert.assertArrayEquals("Header Not Correct.", cd, pd.getHeader().toArray()); - Assert.assertArrayEquals("Rows Not Correct.", rows, pd.getPreviewRows().toArray()); - } - } -} http://git-wip-us.apache.org/repos/asf/ambari/blob/e423a65e/contrib/views/hive/src/test/java/org/apache/ambari/view/hive/resources/upload/DataParserXMLTest.java ---------------------------------------------------------------------- diff --git a/contrib/views/hive/src/test/java/org/apache/ambari/view/hive/resources/upload/DataParserXMLTest.java b/contrib/views/hive/src/test/java/org/apache/ambari/view/hive/resources/upload/DataParserXMLTest.java deleted file mode 100644 index 25be565..0000000 --- a/contrib/views/hive/src/test/java/org/apache/ambari/view/hive/resources/upload/DataParserXMLTest.java +++ /dev/null @@ -1,295 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * <p/> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p/> - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ambari.view.hive.resources.upload; - -import org.apache.ambari.view.hive.client.ColumnDescription; -import org.apache.ambari.view.hive.client.ColumnDescriptionShort; -import org.apache.ambari.view.hive.client.Row; -import org.apache.ambari.view.hive.resources.uploads.ColumnDescriptionImpl; -import org.apache.ambari.view.hive.resources.uploads.parsers.DataParser; -import org.apache.ambari.view.hive.resources.uploads.parsers.ParseOptions; -import org.apache.ambari.view.hive.resources.uploads.parsers.PreviewData; -import org.junit.Assert; -import org.junit.Test; - -import java.io.IOException; -import java.io.StringReader; - -public class DataParserXMLTest { - - @Test - public void testParsePreviewXML() throws Exception { - String str = "<table>" + - "<row>" + - "<col name=\"col1\">row1-col1-Value</col>" + - "<col name=\"col2\">row1-col2-Value</col>" + - "<col name=\"col3\">row1-col3-Value</col>" + - "<col name=\"col4\">10</col>" + - "<col name=\"col5\">11</col>" + - "</row>" + - "<row>" + - "<col name=\"col1\">row2-col1-Value</col>" + - "<col name=\"col2\">row2-col2-Value</col>" + - "<col name=\"col3\">row2-col3-Value</col>" + - "<col name=\"col4\">20</col>" + - "<col name=\"col5\">21</col>" + - "</row>" + - "</table>"; - - - ParseOptions parseOptions = new ParseOptions(); - parseOptions.setOption(ParseOptions.OPTIONS_FILE_TYPE, ParseOptions.InputFileType.XML.toString()); - parseOptions.setOption(ParseOptions.OPTIONS_HEADER, ParseOptions.HEADER.FIRST_RECORD.toString()); - - - try( - StringReader sr = new StringReader(str); - DataParser dp = new DataParser(sr, parseOptions); - ) { - - PreviewData pd = dp.parsePreview(); - Assert.assertNotNull(pd.getPreviewRows()); - Assert.assertNotNull(pd.getHeader()); - Assert.assertEquals(2, pd.getPreviewRows().size()); // header row + preview rows - Assert.assertEquals(5, pd.getHeader().size()); - ColumnDescription[] cd = {new ColumnDescriptionImpl("col1", ColumnDescriptionShort.DataTypes.STRING.toString(), 0), - new ColumnDescriptionImpl("col2", ColumnDescriptionShort.DataTypes.STRING.toString(), 1), - new ColumnDescriptionImpl("col3", ColumnDescriptionShort.DataTypes.STRING.toString(), 2), - new ColumnDescriptionImpl("col4", ColumnDescriptionShort.DataTypes.INT.toString(), 3), - new ColumnDescriptionImpl("col5", ColumnDescriptionShort.DataTypes.INT.toString(), 4) - }; - - Row row2 = new Row(new Object[]{"row1-col1-Value", "row1-col2-Value", "row1-col3-Value", "10", "11"}); - Row row3 = new Row(new Object[]{"row2-col1-Value", "row2-col2-Value", "row2-col3-Value", "20", "21"}); - - Row[] rows = {row2, row3}; - - Assert.assertArrayEquals("Header Not Correct.", cd, pd.getHeader().toArray()); - Assert.assertArrayEquals("Rows Not Correct.", rows, pd.getPreviewRows().toArray()); - } - } - - - /** - * additional columns in rows of XML are ignored. - * number of columns are decided by the first row of the table and here second row contains more columns so those are ignored. - * @throws IOException - */ - @Test - public void testParsePreviewCSVMoreColumns() throws Exception { - String str ="<table>" + - "<row>" + - "<col name=\"col1\">row1-col1-Value</col>" + - "<col name=\"col2\">row1-col2-Value</col>" + - "<col name=\"col3\">row1-col3-Value</col>" + - "<col name=\"col4\">10</col>" + - "<col name=\"col5\">11</col>" + - "</row>" + - "<row>" + - "<col name=\"col1\">row2-col1-Value</col>" + - "<col name=\"col2\">row2-col2-Value</col>" + - "<col name=\"col3\">row2-col3-Value</col>" + - "<col name=\"col99\">row2-col99-Value</col>" + // extra colummn - "<col name=\"col100\">row2-col100-Value</col>" + // extra column - "<col name=\"col4\">20</col>" + - "<col name=\"col5\">21</col>" + - "</row>" + - "</table>"; - - ParseOptions parseOptions = new ParseOptions(); - parseOptions.setOption(ParseOptions.OPTIONS_FILE_TYPE, ParseOptions.InputFileType.XML.toString()); - - - try( StringReader sr = new StringReader(str); - DataParser dp = new DataParser(sr, parseOptions); - ) { - - - PreviewData pd = dp.parsePreview(); - - Row row2 = new Row(new Object[]{"row2-col1-Value","row2-col2-Value","row2-col3-Value","20","21"}); - Assert.assertArrayEquals("More number of columns do not give correct result.", row2.getRow(), pd.getPreviewRows().get(1).getRow()); - } - } - - /** - * less columns in xml makes them null. - * number of columns are decided by the first row of the table and here second row does not contain col99 and col100 - * columns so those are set to null. - * @throws IOException - */ - @Test - public void testParsePreviewCSVLessColumns() throws Exception { - String str = "<table>" + - "<row>" + - "<col name=\"col1\">row1-col1-Value</col>" + - "<col name=\"col2\">row1-col2-Value</col>" + - "<col name=\"col3\">row1-col3-Value</col>" + - "<col name=\"col99\">row2-col99-Value</col>" + // extra colummn - "<col name=\"col100\">row2-col100-Value</col>" + // extra column - "<col name=\"col4\">10</col>" + - "<col name=\"col5\">11</col>" + - "</row>" + - "<row>" + - "<col name=\"col1\">row2-col1-Value</col>" + - "<col name=\"col2\">row2-col2-Value</col>" + - "<col name=\"col3\">row2-col3-Value</col>" + - "<col name=\"col4\">20</col>" + - "<col name=\"col5\">21</col>" + - "</row>" + - "</table>"; - - ParseOptions parseOptions = new ParseOptions(); - parseOptions.setOption(ParseOptions.OPTIONS_FILE_TYPE, ParseOptions.InputFileType.XML.toString()); - parseOptions.setOption(ParseOptions.OPTIONS_HEADER, ParseOptions.HEADER.FIRST_RECORD.toString()); - - try( - StringReader sr = new StringReader(str); - DataParser dp = new DataParser(sr, parseOptions); - ) { - PreviewData pd = dp.parsePreview(); - - Row row2 = new Row(new Object[]{"row2-col1-Value","row2-col2-Value","row2-col3-Value",null,null,"20","21"}); - Assert.assertArrayEquals("Less number of columns do not give correct result.", row2.getRow(), pd.getPreviewRows().get(1).getRow()); - } - } - - /** - * illegal xml format gives error. adding illegal tag gives error - * - * @throws IOException - */ - @Test(expected = IllegalArgumentException.class) - public void testWrongXMLFormat() throws Exception { - String str = "<table>" + - "<row>" + - "<ccc></ccc>" + // illegal tag. - "<col name=\"col1\">row1-col1-Value</col>" + - "<col name=\"col2\">row1-col2-Value</col>" + - "<col name=\"col3\">row1-col3-Value</col>" + - "<col name=\"col99\">row2-col99-Value</col>" + // extra colummn - "<col name=\"col100\">row2-col100-Value</col>" + // extra column - "<col name=\"col4\">10</col>" + - "<col name=\"col5\">11</col>" + - "</row>" + - "<row>" + - "<col name=\"col1\">row2-col1-Value</col>" + - "<col name=\"col2\">row2-col2-Value</col>" + - "<col name=\"col3\">row2-col3-Value</col>" + - "<col name=\"col4\">20</col>" + - "<col name=\"col5\">21</col>" + - "</row>" + - "</table>"; - - ParseOptions parseOptions = new ParseOptions(); - parseOptions.setOption(ParseOptions.OPTIONS_FILE_TYPE, ParseOptions.InputFileType.XML.toString()); - parseOptions.setOption(ParseOptions.OPTIONS_HEADER, ParseOptions.HEADER.FIRST_RECORD.toString()); - try( - StringReader sr = new StringReader(str); - DataParser dp = new DataParser(sr, parseOptions); - ) { - PreviewData pd = dp.parsePreview(); - } - } - - /** - * One row XML will give embedde column names and 1st row in preview if HEADER.EMBEDDED is selected - * @throws IOException - */ - @Test - public void testParsePreview1RowXML() throws Exception { - String str = "<table>" + - "<row>" + - "<col name=\"col1\">row1-col1-Value</col>" + - "<col name=\"col2\">11</col>" + - "</row>" + - "</table>"; - - - ParseOptions parseOptions = new ParseOptions(); - parseOptions.setOption(ParseOptions.OPTIONS_FILE_TYPE, ParseOptions.InputFileType.XML.toString()); - parseOptions.setOption(ParseOptions.OPTIONS_HEADER, ParseOptions.HEADER.EMBEDDED.toString()); - - try( - StringReader sr = new StringReader(str); - DataParser dp = new DataParser(sr, parseOptions); - ) { - - PreviewData pd = dp.parsePreview(); - Assert.assertNotNull(pd.getPreviewRows()); - Assert.assertNotNull(pd.getHeader()); - Assert.assertEquals(1, pd.getPreviewRows().size()); - Assert.assertEquals(2, pd.getHeader().size()); - ColumnDescription[] cd = {new ColumnDescriptionImpl("col1", ColumnDescriptionShort.DataTypes.STRING.toString(), 0), - new ColumnDescriptionImpl("col2", ColumnDescriptionShort.DataTypes.INT.toString(), 1)}; - - Object cols1[] = new Object[2]; - cols1[0] = "row1-col1-Value"; - cols1[1] = "11"; - Row row1 = new Row(cols1); - - Row[] rows = {row1}; - - Assert.assertArrayEquals("Header Not Correct.", cd, pd.getHeader().toArray()); - Assert.assertArrayEquals("Rows Not Correct.", rows, pd.getPreviewRows().toArray()); - } - } - - /** - * One row XML will give default column names and 1st row in preview if HEADER.PROVIDED_BY_USER is selected - * @throws IOException - */ - @Test - public void testParsePreview1RowXMLHeaderProvided() throws Exception { - String str = "<table>" + - "<row>" + - "<col name=\"col1\">row1-col1-Value</col>" + - "<col name=\"col2\">11</col>" + - "</row>" + - "</table>"; - - ParseOptions parseOptions = new ParseOptions(); - parseOptions.setOption(ParseOptions.OPTIONS_FILE_TYPE, ParseOptions.InputFileType.XML.toString()); - parseOptions.setOption(ParseOptions.OPTIONS_HEADER, ParseOptions.HEADER.PROVIDED_BY_USER.toString()); - - try( - StringReader sr = new StringReader(str); - DataParser dp = new DataParser(sr, parseOptions) - ) { - - PreviewData pd = dp.parsePreview(); - Assert.assertNotNull(pd.getPreviewRows()); - Assert.assertNotNull(pd.getHeader()); - Assert.assertEquals(1, pd.getPreviewRows().size()); - Assert.assertEquals(2, pd.getHeader().size()); - ColumnDescription[] cd = {new ColumnDescriptionImpl("column1", ColumnDescriptionShort.DataTypes.STRING.toString(), 0), - new ColumnDescriptionImpl("column2", ColumnDescriptionShort.DataTypes.INT.toString(), 1)}; - - Object cols1[] = new Object[2]; - cols1[0] = "row1-col1-Value"; - cols1[1] = "11"; - Row row1 = new Row(cols1); - - Row[] rows = {row1}; - - Assert.assertArrayEquals("Header Not Correct.", cd, pd.getHeader().toArray()); - Assert.assertArrayEquals("Rows Not Correct.", rows, pd.getPreviewRows().toArray()); - } - } -} http://git-wip-us.apache.org/repos/asf/ambari/blob/e423a65e/contrib/views/hive/src/test/java/org/apache/ambari/view/hive/resources/upload/JsonParserTest.java ---------------------------------------------------------------------- diff --git a/contrib/views/hive/src/test/java/org/apache/ambari/view/hive/resources/upload/JsonParserTest.java b/contrib/views/hive/src/test/java/org/apache/ambari/view/hive/resources/upload/JsonParserTest.java deleted file mode 100644 index cd571b3..0000000 --- a/contrib/views/hive/src/test/java/org/apache/ambari/view/hive/resources/upload/JsonParserTest.java +++ /dev/null @@ -1,147 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * <p/> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p/> - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ambari.view.hive.resources.upload; - -import com.google.gson.JsonArray; -import com.google.gson.JsonObject; -import org.apache.ambari.view.hive.client.Row; -import org.apache.ambari.view.hive.resources.uploads.parsers.json.JSONParser; -import org.apache.ambari.view.hive.resources.uploads.parsers.xml.XMLParser; -import org.junit.Assert; -import org.junit.Test; - -import java.io.IOException; -import java.io.StringReader; -import java.util.Iterator; - -public class JsonParserTest { - - @Test(expected = IOException.class) - public void testEmptyStream() throws Exception { - String json = ""; - - try( - StringReader sr = new StringReader(json); - JSONParser jp = new JSONParser(sr, null); - ) { - // PARSING WILL THROW ERROR - } - } - - @Test - public void testEmptyRow() throws Exception { - JsonObject jo = new JsonObject(); - JsonArray ja = new JsonArray(); - ja.add(jo); - String json = ja.toString(); - - try( - StringReader sr = new StringReader(json); - JSONParser jp = new JSONParser(sr, null) - ) { - - Iterator<Row> iterator = jp.iterator(); - - Assert.assertEquals("Iterator should not be Empty", true, iterator.hasNext()); - Assert.assertArrayEquals("Row should be empty",new Object[]{},iterator.next().getRow()); - } - } - - - @Test - public void testEmptyTable() throws Exception { - JsonArray ja = new JsonArray(); - String json = ja.toString(); - - try( - StringReader sr = new StringReader(json); - JSONParser jp = new JSONParser(sr, null); - ) { - Iterator<Row> iterator = jp.iterator(); - Assert.assertEquals("Iterator Empty!", false, iterator.hasNext()); - } - } - - @Test - public void testParse1Row() throws Exception { - JsonObject jo = new JsonObject(); - jo.addProperty("key1","value1"); - jo.addProperty("key2",'c'); - jo.addProperty("key3",10); - jo.addProperty("key4",10.1); - - JsonArray ja = new JsonArray(); - ja.add(jo); - String json = ja.toString(); - - try(StringReader sr = new StringReader(json); - - JSONParser jp = new JSONParser(sr, null) - ) { - Iterator<Row> iterator = jp.iterator(); - - Assert.assertEquals("Iterator Empty!", true, iterator.hasNext()); - Row row = iterator.next(); - Row expected = new Row(new Object[]{"value1", "c", "10", "10.1"}); - Assert.assertEquals("Row not equal!", expected, row); - - Assert.assertEquals("Should report no more rows!", false, iterator.hasNext()); - } - } - - @Test - public void testParseMultipleRow() throws Exception { - JsonObject jo1 = new JsonObject(); - jo1.addProperty("key1","value1"); - jo1.addProperty("key2","c"); - jo1.addProperty("key3","10"); - jo1.addProperty("key4","10.1"); - - JsonObject jo2 = new JsonObject(); - jo2.addProperty("key1","value2"); - jo2.addProperty("key2","c2"); - jo2.addProperty("key3","102"); - jo2.addProperty("key4",true); - - - JsonArray ja = new JsonArray(); - ja.add(jo1); - ja.add(jo2); - - String json = ja.toString(); - - - - try( - StringReader sr = new StringReader(json); - JSONParser jp = new JSONParser(sr, null) - ) { - Iterator<Row> iterator = jp.iterator(); - - Assert.assertEquals("Failed to detect 1st row!", true, iterator.hasNext()); - Assert.assertEquals("Failed to match 1st row!", new Row(new Object[]{"value1", "c", "10", "10.1"}), iterator.next()); - - Assert.assertEquals("Failed to detect 2nd row!", true, iterator.hasNext()); - Assert.assertEquals("Failed to match 2nd row!", new Row(new Object[]{"value2", "c2", "102", Boolean.TRUE.toString()}), iterator.next()); - - Assert.assertEquals("Failed to detect end of rows!", false, iterator.hasNext()); - Assert.assertEquals("Failed to detect end of rows 2nd time!", false, iterator.hasNext()); - } - } -} http://git-wip-us.apache.org/repos/asf/ambari/blob/e423a65e/contrib/views/hive/src/test/java/org/apache/ambari/view/hive/resources/upload/OpenCSVParserTest.java ---------------------------------------------------------------------- diff --git a/contrib/views/hive/src/test/java/org/apache/ambari/view/hive/resources/upload/OpenCSVParserTest.java b/contrib/views/hive/src/test/java/org/apache/ambari/view/hive/resources/upload/OpenCSVParserTest.java deleted file mode 100644 index 5256b5a..0000000 --- a/contrib/views/hive/src/test/java/org/apache/ambari/view/hive/resources/upload/OpenCSVParserTest.java +++ /dev/null @@ -1,333 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * <p/> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p/> - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ambari.view.hive.resources.upload; - -import org.apache.ambari.view.hive.client.Row; -import org.apache.ambari.view.hive.resources.uploads.parsers.ParseOptions; -import org.apache.ambari.view.hive.resources.uploads.parsers.csv.opencsv.OpenCSVParser; -import org.junit.Assert; -import org.junit.Test; - -import java.io.IOException; -import java.io.StringReader; -import java.util.Iterator; - -public class OpenCSVParserTest { - - /** - * no exception in creating csvParser with emtpy stream - * @throws IOException - */ - @Test - public void testEmptyStream() throws Exception { - String csv = ""; - - try( - StringReader sr = new StringReader(csv); - OpenCSVParser jp = new OpenCSVParser(sr, new ParseOptions()); - ) { - Assert.assertEquals("There should not be any rows.",false, jp.iterator().hasNext()); - } - } - - /** - * in case of csv an empty line is still considered as row - * @throws IOException - */ - @Test - public void testEmptyRow() throws Exception { - String csv = " "; - - try( - StringReader sr = new StringReader(csv); - OpenCSVParser jp = new OpenCSVParser(sr, new ParseOptions()); - ) { - Iterator<Row> iterator = jp.iterator(); - - Assert.assertEquals("Iterator should be Empty", true, iterator.hasNext()); - Assert.assertArrayEquals("Row should not be empty",new Object[]{" "},iterator.next().getRow()); - } - } - - @Test - public void testParse1Row() throws Exception { - String csv = "value1,c,10,10.1"; - - try( - StringReader sr = new StringReader(csv); - OpenCSVParser jp = new OpenCSVParser(sr, new ParseOptions()); - ) { - Iterator<Row> iterator = jp.iterator(); - - Assert.assertEquals("Iterator Empty!", true, iterator.hasNext()); - Row row = iterator.next(); - Row expected = new Row(new Object[]{"value1", "c", "10", "10.1"}); - Assert.assertEquals("Row not equal!", expected, row); - - Assert.assertEquals("Should report no more rows!", false, iterator.hasNext()); - } - } - - @Test - public void testParseMultipleRow() throws Exception { - - String csv = "value1,c,10,10.1\n" + - "value2,c2,102,true"; - - try( - StringReader sr = new StringReader(csv); - OpenCSVParser jp = new OpenCSVParser(sr, new ParseOptions()); - ) { - - Iterator<Row> iterator = jp.iterator(); - - Assert.assertEquals("Failed to detect 1st row!", true, iterator.hasNext()); - Assert.assertEquals("Failed to match 1st row!", new Row(new Object[]{"value1", "c", "10", "10.1"}), iterator.next()); - - Assert.assertEquals("Failed to detect 2nd row!", true, iterator.hasNext()); - Assert.assertEquals("Failed to match 2nd row!", new Row(new Object[]{"value2", "c2", "102", Boolean.TRUE.toString()}), iterator.next()); - - Assert.assertEquals("Failed to detect end of rows!", false, iterator.hasNext()); - Assert.assertEquals("Failed to detect end of rows 2nd time!", false, iterator.hasNext()); - } - } - - @Test - public void testQuotedAndEscapedEndline() throws Exception { - - String csv = "\"row1-\ncol1\",1,1.1\n\"row2-\\\ncol1\",2,2.2\n"; - ParseOptions po = new ParseOptions(); - - try( - StringReader sr = new StringReader(csv); - OpenCSVParser jp = new OpenCSVParser(sr, po); - ) { - - Iterator<Row> iterator = jp.iterator(); - - Row row = new Row(new Object[]{"row1-\ncol1", "1", "1.1"}); - Assert.assertEquals("Failed to detect 1st row!", true, iterator.hasNext()); - Assert.assertEquals("Failed to match 1st row!", row, iterator.next()); - - Row row2 = new Row(new Object[]{"row2-\ncol1", "2", "2.2"}); - Assert.assertEquals("Failed to detect 1st row!", true, iterator.hasNext()); - Assert.assertEquals("Failed to match 1st row!", row2, iterator.next()); - - } - } - - @Test - public void testQuotedDoubleQuote() throws Exception { - - String csv = "\"aaa\",\"b\"\"bb\",\"ccc\""; - ParseOptions po = new ParseOptions(); - - try( - StringReader sr = new StringReader(csv); - OpenCSVParser jp = new OpenCSVParser(sr, po); - ) { - - Iterator<Row> iterator = jp.iterator(); - - Row row = new Row(new Object[]{"aaa", "b\"bb", "ccc"}); - Assert.assertEquals("Failed to detect 1st row!", true, iterator.hasNext()); - Assert.assertEquals("Failed to match 1st row!", row, iterator.next()); - } - } - - @Test - public void testEscapedDoubleQuote() throws Exception { - - String csv = "\"aaa\",\"b\\\"bb\",\"ccc\""; - ParseOptions po = new ParseOptions(); - - try( - StringReader sr = new StringReader(csv); - OpenCSVParser jp = new OpenCSVParser(sr, po); - ) { - - Iterator<Row> iterator = jp.iterator(); - - Row row = new Row(new Object[]{"aaa", "b\"bb", "ccc"}); - Assert.assertEquals("Failed to detect 1st row!", true, iterator.hasNext()); - Assert.assertEquals("Failed to match 1st row!", row, iterator.next()); - } - } - - - @Test - public void testSpecialEscape() throws Exception { - - String csv = "\"aaa\",\"b$\"bb\",\"ccc\""; - ParseOptions po = new ParseOptions(); - po.setOption(ParseOptions.OPTIONS_CSV_ESCAPE_CHAR,'$'); - - try( - StringReader sr = new StringReader(csv); - OpenCSVParser jp = new OpenCSVParser(sr, po); - ) { - - Iterator<Row> iterator = jp.iterator(); - - Row row = new Row(new Object[]{"aaa", "b\"bb", "ccc"}); - Assert.assertEquals("Failed to detect 1st row!", true, iterator.hasNext()); - Assert.assertEquals("Failed to match 1st row!", row, iterator.next()); - } - } - - @Test - public void testMultipleEscape() throws Exception { - - String csv = "BBAABBKMAABB"; - ParseOptions po = new ParseOptions(); - po.setOption(ParseOptions.OPTIONS_CSV_ESCAPE_CHAR,'B'); - po.setOption(ParseOptions.OPTIONS_CSV_DELIMITER,'M'); - - try( - StringReader sr = new StringReader(csv); - OpenCSVParser jp = new OpenCSVParser(sr, po); - ) { - Iterator<Row> iterator = jp.iterator(); - - Row row = new Row(new Object[]{"AABK", "AAB"}); - Assert.assertEquals("Failed to detect 1st row!", true, iterator.hasNext()); - Assert.assertEquals("Failed to match 1st row!", row, iterator.next()); - } - } - - @Test - public void testSpecialEscapedEscape() throws Exception { - - String csv = "aaa,b$$bb,ccc"; - ParseOptions po = new ParseOptions(); - po.setOption(ParseOptions.OPTIONS_CSV_ESCAPE_CHAR,'$'); - - try( - StringReader sr = new StringReader(csv); - OpenCSVParser jp = new OpenCSVParser(sr, po); - ) { - - Iterator<Row> iterator = jp.iterator(); - - Row row = new Row(new Object[]{"aaa", "b$bb", "ccc"}); - Assert.assertEquals("Failed to detect 1st row!", true, iterator.hasNext()); - Assert.assertEquals("Failed to match 1st row!", row, iterator.next()); - } - } - - - @Test - public void testSpecialUnEscapedEscape() throws Exception { - - String csv = "aaa,b$bb,ccc"; - ParseOptions po = new ParseOptions(); - po.setOption(ParseOptions.OPTIONS_CSV_ESCAPE_CHAR,'$'); - - try( - StringReader sr = new StringReader(csv); - OpenCSVParser jp = new OpenCSVParser(sr, po); - ) { - - Iterator<Row> iterator = jp.iterator(); - - Row row = new Row(new Object[]{"aaa", "bbb", "ccc"}); - Assert.assertEquals("Failed to detect 1st row!", true, iterator.hasNext()); - Assert.assertEquals("Failed to match 1st row!", row, iterator.next()); - } - } - - @Test - public void test001Escape() throws Exception { - - String csv = "aaa,b\001\"bb,ccc"; - ParseOptions po = new ParseOptions(); - po.setOption(ParseOptions.OPTIONS_CSV_ESCAPE_CHAR,'\001'); - - try( - StringReader sr = new StringReader(csv); - OpenCSVParser jp = new OpenCSVParser(sr, po); - ) { - - Iterator<Row> iterator = jp.iterator(); - Row row = new Row(new Object[]{"aaa", "b\"bb", "ccc"}); - Assert.assertEquals("Failed to detect 1st row!", true, iterator.hasNext()); - Assert.assertEquals("Failed to match 1st row!", row, iterator.next()); } - } - - @Test - public void testSpecialQuote() throws Exception { - - String csv = "\001aaa\001,\001b\001\001bb\001,\001ccc\001"; - ParseOptions po = new ParseOptions(); - po.setOption(ParseOptions.OPTIONS_CSV_QUOTE,'\001'); - - try( - StringReader sr = new StringReader(csv); - OpenCSVParser jp = new OpenCSVParser(sr, po); - ) { - - Iterator<Row> iterator = jp.iterator(); - Row row = new Row(new Object[]{"aaa", "b\001bb", "ccc"}); - Assert.assertEquals("Failed to detect 1st row!", true, iterator.hasNext()); - Assert.assertEquals("Failed to match 1st row!", row, iterator.next()); - } - } - - @Test - public void testSpaceAsDelimiterAndQuoted() throws Exception { - - String csv = "aaa \"b bb\" ccc\naaa2 bbb2 \"c cc2\""; - ParseOptions po = new ParseOptions(); -// po.setOption(ParseOptions.OPTIONS_CSV_ESCAPE_CHAR,'\001'); - po.setOption(ParseOptions.OPTIONS_CSV_DELIMITER,' '); - - try( - StringReader sr = new StringReader(csv); - OpenCSVParser jp = new OpenCSVParser(sr, po); - ) { - - Iterator<Row> iterator = jp.iterator(); - Row row = new Row(new Object[]{"aaa", "b bb", "ccc"}); - Assert.assertEquals("Failed to detect 1st row!", true, iterator.hasNext()); - Assert.assertEquals("Failed to match 1st row!", row, iterator.next()); - - Row row2 = new Row(new Object[]{"aaa2", "bbb2", "c cc2"}); - Assert.assertEquals("Failed to detect 1st row!", true, iterator.hasNext()); - Assert.assertEquals("Failed to match 1st row!", row2, iterator.next()); - } - } - - @Test - public void testFailedDelimiterEscaped() throws Exception { - - String csv = "aaa,b\\,bb,ccc"; - ParseOptions po = new ParseOptions(); - - try( - StringReader sr = new StringReader(csv); - OpenCSVParser jp = new OpenCSVParser(sr, po); - ) { - - Iterator<Row> iterator = jp.iterator(); - Row row = new Row(new Object[]{"aaa", "b","bb", "ccc"}); // different from Common CSVParser - Assert.assertEquals("Failed to detect 1st row!", true, iterator.hasNext()); - Assert.assertEquals("Failed to match 1st row!", row, iterator.next()); - } - } -}
