fix npe thrown from getEntry() and getStatus() SQLMetadataStorageActionHandler due to a non-exist entryId

This commit is contained in:
Bingkun Guo 2015-06-26 10:40:56 -05:00
parent 0c85c8c60a
commit e4e18b17b5
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
{ {
return Optional.fromNullable( byte[] res = handle.createQuery(
jsonMapper.<EntryType>readValue(
handle.createQuery(
String.format("SELECT payload FROM %s WHERE id = :id", entryTable) String.format("SELECT payload FROM %s WHERE id = :id", entryTable)
) )
.bind("id", entryId) .bind("id", entryId)
.map(ByteArrayMapper.FIRST) .map(ByteArrayMapper.FIRST)
.first(), .first();
entryType
) return Optional.fromNullable(
res == null ? null : jsonMapper.<EntryType>readValue(res, 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
{ {
return Optional.fromNullable( byte[] res = handle.createQuery(
jsonMapper.<StatusType>readValue(
handle.createQuery(
String.format("SELECT status_payload FROM %s WHERE id = :id", entryTable) String.format("SELECT status_payload FROM %s WHERE id = :id", entryTable)
) )
.bind("id", entryId) .bind("id", entryId)
.map(ByteArrayMapper.FIRST) .map(ByteArrayMapper.FIRST)
.first(), .first();
statusType
) return Optional.fromNullable(
res == null ? null : jsonMapper.<StatusType>readValue(res, 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)