From 38cf5fed407ed0aa5e7adb72c90b82bccccfe3e0 Mon Sep 17 00:00:00 2001 From: shrek2000 Date: Tue, 9 Jan 2018 16:00:28 +0200 Subject: Fail to Export VLM Timestamps had null value, invalid json caused exceptions Issue-ID: SDC-892 Change-Id: Iab117dd76cd51c3cd5fa62a59f55b18e265c58bf Signed-off-by: shrek2000 --- .../core/tools/exportinfo/ExportSerializer.java | 68 +++++++++++++--------- .../core/tools/importinfo/ImportSingleTable.java | 12 ++-- .../exportinfo/serialize/VLMExtractTest.java | 6 +- 3 files changed, 52 insertions(+), 34 deletions(-) (limited to 'openecomp-be/tools/zusammen-tools/src') diff --git a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/exportinfo/ExportSerializer.java b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/exportinfo/ExportSerializer.java index 0098f21eb2..f4710fbae5 100644 --- a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/exportinfo/ExportSerializer.java +++ b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/exportinfo/ExportSerializer.java @@ -8,6 +8,8 @@ import com.datastax.driver.core.ResultSet; import com.datastax.driver.core.Row; import com.google.gson.JsonElement; import com.google.gson.JsonParser; +import com.google.gson.JsonSyntaxException; +import org.apache.commons.lang3.StringUtils; import org.codehaus.jackson.map.ObjectMapper; import org.openecomp.core.tools.importinfo.ImportProperties; import org.openecomp.core.tools.model.ColumnDefinition; @@ -22,6 +24,7 @@ import java.nio.ByteBuffer; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Base64; +import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -32,14 +35,14 @@ import static org.openecomp.core.tools.importinfo.ImportSingleTable.dataTypesMap public class ExportSerializer { - private static final Logger logger = LoggerFactory.getLogger(ExportDataCommand.class); + private static final Logger logger = LoggerFactory.getLogger(ExportSerializer.class); private static final String ELEMENT_TABLE_NAME = "element"; private static final String ELEMENT_INFO_COLUMN_NAME = "info"; public void serializeResult(final ResultSet resultSet, final Set filteredItems, final String filteredColumn, Set vlms) { try { TableData tableData = new TableData(); - tableData.definitions = resultSet.getColumnDefinitions().asList().stream().map(column -> new ColumnDefinition(column)).collect(Collectors.toList()); + tableData.definitions = resultSet.getColumnDefinitions().asList().stream().map(ColumnDefinition::new).collect(Collectors.toList()); String table = tableData.definitions.iterator().next().getTable(); boolean isElementTable = table.equals(ELEMENT_TABLE_NAME); Iterator iterator = resultSet.iterator(); @@ -78,9 +81,9 @@ public class ExportSerializer { if (string == null) { string = ""; } - if (checkForVLM && vlms != null){ + if (checkForVLM && vlms != null) { String vlm = extractVlm(string); - if (vlm!= null) { + if (vlm != null) { vlms.add(vlm); } } @@ -94,7 +97,12 @@ public class ExportSerializer { data = Base64.getEncoder().encodeToString(bytes.array()); break; case TIMESTAMP: - data = row.getDate(i).getTime(); + Date rowDate = row.getDate(i); + if (rowDate != null) { + data = rowDate.getTime(); + } else { + data = ""; + } break; case BOOLEAN: data = row.getBool(i); @@ -109,14 +117,14 @@ public class ExportSerializer { data = row.getFloat(i); break; case SET: - Set set = row.getSet(i, Object.class); - Object joined = set.stream().map(o -> o.toString()).collect(Collectors.joining(ExportDataCommand.JOIN_DELIMITER)); + Set set = row.getSet(i, Object.class); + Object joined = set.stream().map(Object::toString).collect(Collectors.joining(ExportDataCommand.JOIN_DELIMITER)); data = Base64.getEncoder().encodeToString(joined.toString().getBytes()); break; case MAP: - Map map = row.getMap(i, Object.class, Object.class); - Set> entrySet = map.entrySet(); - Object mapAsString = entrySet.parallelStream().map(entry -> entry.getKey().toString() + ExportDataCommand.MAP_DELIMITER +entry.getValue().toString()) + Map map = row.getMap(i, Object.class, Object.class); + Set> entrySet = map.entrySet(); + Object mapAsString = entrySet.parallelStream().map(entry -> entry.getKey().toString() + ExportDataCommand.MAP_DELIMITER + entry.getValue().toString()) .collect(Collectors.joining(ExportDataCommand.MAP_DELIMITER)); data = Base64.getEncoder().encodeToString(mapAsString.toString().getBytes()); break; @@ -126,22 +134,28 @@ public class ExportSerializer { return data; } - protected String extractVlm(String injson) { - if (injson == null){ - return null; - } - JsonElement root = new JsonParser().parse(injson); - if (root == null){ - return null; - } - JsonElement properties = root.getAsJsonObject().get("properties"); - if (properties == null){ - return null; - } - JsonElement vendorId = properties.getAsJsonObject().get("vendorId"); - if (vendorId == null){ - return null; - } - return vendorId.getAsString(); + protected String extractVlm(String inJson) { + if (StringUtils.isEmpty(inJson.trim())) { + return null; + } + JsonElement root; + try { + root = new JsonParser().parse(inJson); + } catch (JsonSyntaxException e) { + Utils.logError(logger, "Failed to parse json " + inJson, e); + return null; + } + if (root == null) { + return null; + } + JsonElement properties = root.getAsJsonObject().get("properties"); + if (properties == null) { + return null; + } + JsonElement vendorId = properties.getAsJsonObject().get("vendorId"); + if (vendorId == null) { + return null; + } + return vendorId.getAsString(); } } diff --git a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/importinfo/ImportSingleTable.java b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/importinfo/ImportSingleTable.java index 8a671c5f8d..922e5c7d00 100644 --- a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/importinfo/ImportSingleTable.java +++ b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/importinfo/ImportSingleTable.java @@ -36,8 +36,8 @@ public class ImportSingleTable { private static final Logger logger = LoggerFactory.getLogger(ImportSingleTable.class); - public static final String INSERT_INTO = "INSERT INTO "; - public static final String VALUES = " VALUES "; + private static final String INSERT_INTO = "INSERT INTO "; + private static final String VALUES = " VALUES "; private static final Map statementsCache = new HashMap<>(); public void importFile(Path file) { @@ -96,7 +96,11 @@ public class ImportSingleTable { bind.setBytes(i, ByteBuffer.wrap(Base64.getDecoder().decode(rowData.getBytes()))); break; case TIMESTAMP: - bind.setDate(i, new Date(Long.parseLong(rowData))); + if (StringUtils.isEmpty(rowData)){ + bind.setSet(i, null); + } else { + bind.setDate(i, new Date(Long.parseLong(rowData))); + } break; case BOOLEAN: bind.setBool(i, Boolean.parseBoolean(rowData)); @@ -147,7 +151,7 @@ public class ImportSingleTable { ColumnDefinition def = tableData.definitions.iterator().next(); StringBuilder sb = new StringBuilder(); sb.append(INSERT_INTO).append(def.getKeyspace()).append(".").append(def.getTable()); - sb.append(tableData.definitions.stream().map(definition -> definition.getName()).collect(Collectors.joining(" , ", " ( ", " ) "))); + sb.append(tableData.definitions.stream().map(ColumnDefinition::getName).collect(Collectors.joining(" , ", " ( ", " ) "))); sb.append(VALUES).append(tableData.definitions.stream().map(definition -> "?").collect(Collectors.joining(" , ", " ( ", " ) "))).append(";"); return sb.toString(); } diff --git a/openecomp-be/tools/zusammen-tools/src/test/java/org/openecomp/core/tools/Commands/exportinfo/serialize/VLMExtractTest.java b/openecomp-be/tools/zusammen-tools/src/test/java/org/openecomp/core/tools/Commands/exportinfo/serialize/VLMExtractTest.java index a5a1ff2cbc..ed1ad2b2f4 100644 --- a/openecomp-be/tools/zusammen-tools/src/test/java/org/openecomp/core/tools/Commands/exportinfo/serialize/VLMExtractTest.java +++ b/openecomp-be/tools/zusammen-tools/src/test/java/org/openecomp/core/tools/Commands/exportinfo/serialize/VLMExtractTest.java @@ -36,12 +36,12 @@ public class VLMExtractTest { @Test(expectedExceptions = IllegalStateException.class) public void failToExtractVLMBecauseJsonIsCorrupted(){ String elemenet_info_string = "gfhhhghgh"; - new CustomExportSerializer().extractVlm(elemenet_info_string); + assertNull(new CustomExportSerializer().extractVlm(elemenet_info_string)); } private static final class CustomExportSerializer extends ExportSerializer{ - public String extractVlm(String injson) { - return super.extractVlm(injson); + public String extractVlm(String inJson) { + return super.extractVlm(inJson); } } } -- cgit 1.2.3-korg