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

View File

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