Merge pull request #1468 from guobingkun/fix_npe_for_storage_action_handler

Fix npe thrown from SQLMetadataStorageActionHandler
This commit is contained in:
Himanshu 2015-06-29 09:57:51 -05:00
commit 8172bdff1c
2 changed files with 30 additions and 18 deletions

View File

@ -165,16 +165,15 @@ public class SQLMetadataStorageActionHandler<EntryType, StatusType, LogType, Loc
@Override @Override
public Optional<EntryType> withHandle(Handle handle) throws Exception public Optional<EntryType> withHandle(Handle handle) throws Exception
{ {
byte[] res = handle.createQuery(
String.format("SELECT payload FROM %s WHERE id = :id", entryTable)
)
.bind("id", entryId)
.map(ByteArrayMapper.FIRST)
.first();
return Optional.fromNullable( return Optional.fromNullable(
jsonMapper.<EntryType>readValue( res == null ? null : jsonMapper.<EntryType>readValue(res, entryType)
handle.createQuery(
String.format("SELECT payload FROM %s WHERE id = :id", entryTable)
)
.bind("id", entryId)
.map(ByteArrayMapper.FIRST)
.first(),
entryType
)
); );
} }
} }
@ -190,16 +189,15 @@ public class SQLMetadataStorageActionHandler<EntryType, StatusType, LogType, Loc
@Override @Override
public Optional<StatusType> withHandle(Handle handle) throws Exception public Optional<StatusType> withHandle(Handle handle) throws Exception
{ {
byte[] res = handle.createQuery(
String.format("SELECT status_payload FROM %s WHERE id = :id", entryTable)
)
.bind("id", entryId)
.map(ByteArrayMapper.FIRST)
.first();
return Optional.fromNullable( return Optional.fromNullable(
jsonMapper.<StatusType>readValue( res == null ? null : jsonMapper.<StatusType>readValue(res, statusType)
handle.createQuery(
String.format("SELECT status_payload FROM %s WHERE id = :id", entryTable)
)
.bind("id", entryId)
.map(ByteArrayMapper.FIRST)
.first(),
statusType
)
); );
} }
} }

View File

@ -118,8 +118,12 @@ public class SQLMetadataStorageActionHandlerTest
handler.getEntry(entryId) handler.getEntry(entryId)
); );
Assert.assertEquals(Optional.absent(), handler.getEntry("non_exist_entry"));
Assert.assertEquals(Optional.absent(), handler.getStatus(entryId)); Assert.assertEquals(Optional.absent(), handler.getStatus(entryId));
Assert.assertEquals(Optional.absent(), handler.getStatus("non_exist_entry"));
Assert.assertTrue(handler.setStatus(entryId, true, status1)); Assert.assertTrue(handler.setStatus(entryId, true, status1));
Assert.assertEquals( Assert.assertEquals(
@ -179,6 +183,11 @@ public class SQLMetadataStorageActionHandlerTest
handler.insert(entryId, new DateTime("2014-01-01"), "test", entry, true, status); handler.insert(entryId, new DateTime("2014-01-01"), "test", entry, true, status);
Assert.assertEquals(
ImmutableList.of(),
handler.getLogs("non_exist_entry")
);
Assert.assertEquals( Assert.assertEquals(
ImmutableMap.of(), ImmutableMap.of(),
handler.getLocks(entryId) handler.getLocks(entryId)
@ -206,6 +215,11 @@ public class SQLMetadataStorageActionHandlerTest
handler.insert(entryId, new DateTime("2014-01-01"), "test", entry, true, status); handler.insert(entryId, new DateTime("2014-01-01"), "test", entry, true, status);
Assert.assertEquals(
ImmutableMap.<Long, Map<String, Integer>>of(),
handler.getLocks("non_exist_entry")
);
Assert.assertEquals( Assert.assertEquals(
ImmutableMap.<Long, Map<String, Integer>>of(), ImmutableMap.<Long, Map<String, Integer>>of(),
handler.getLocks(entryId) handler.getLocks(entryId)