DatabasePersistence extends Persistence { @Override public long putLog(@NonNull Log log, @NonNull String group, @IntRange(from = Flags.NORMAL, to = Flags.CRITICAL) int flags) throws PersistenceException { try { AppCenterLog.debug(LOG_TAG, "Storing a log to the Persistence database for log type " + log.getType() + " with flags=" + flags); String payload = getLogSerializer().serializeLog(log); ContentValues contentValues; int payloadSize = payload.getBytes("UTF-8").length; boolean isLargePayload = payloadSize >= PAYLOAD_MAX_SIZE; String targetKey; String targetToken; if (log instanceof CommonSchemaLog) { if (isLargePayload) { throw new PersistenceException("Log is larger than " + PAYLOAD_MAX_SIZE + " bytes, cannot send to OneCollector."); } targetToken = log.getTransmissionTargetTokens().iterator().next(); targetKey = PartAUtils.getTargetKey(targetToken); targetToken = CryptoUtils.getInstance(mContext).encrypt(targetToken); } else { targetKey = null; targetToken = null; } long maxSize = mDatabaseManager.getMaxSize(); if (maxSize == -1) { throw new PersistenceException("Failed to store a log to the Persistence database."); } if (!isLargePayload && maxSize <= payloadSize) { throw new PersistenceException("Log is too large (" + payloadSize + " bytes) to store in database. " + "Current maximum database size is " + maxSize + " bytes."); } contentValues = getContentValues(group, isLargePayload ? null : payload, targetToken, log.getType(), targetKey, Flags.getPersistenceFlag(flags, false)); long databaseId = mDatabaseManager.put(contentValues, COLUMN_PRIORITY); if (databaseId == -1) { throw new PersistenceException("Failed to store a log to the Persistence database for log type " + log.getType() + "."); } AppCenterLog.debug(LOG_TAG, "Stored a log to the Persistence database for log type " + log.getType() + " with databaseId=" + databaseId); if (isLargePayload) { AppCenterLog.debug(LOG_TAG, "Payload is larger than what SQLite supports, storing payload in a separate file."); File directory = getLargePayloadGroupDirectory(group); directory.mkdir(); File payloadFile = getLargePayloadFile(directory, databaseId); try { FileManager.write(payloadFile, payload); } catch (IOException e) { mDatabaseManager.delete(databaseId); throw e; } AppCenterLog.debug(LOG_TAG, "Payload written to " + payloadFile); } return databaseId; } catch (JSONException e) { throw new PersistenceException("Cannot convert to JSON string.", e); } catch (IOException e) { throw new PersistenceException("Cannot save large payload in a file.", e); } } DatabasePersistence(Context context);   DatabasePersistence(Context context, int version, @SuppressWarnings("SameParameterValue") final ContentValues schema); @Override boolean setMaxStorageSize(long maxStorageSizeInBytes); @Override long putLog(@NonNull Log log, @NonNull String group, @IntRange(from = Flags.NORMAL, to = Flags.CRITICAL) int flags); @Override void deleteLogs(@NonNull String group, @NonNull String id); @Override void deleteLogs(String group); @Override int countLogs(@NonNull String group); @Override @Nullable String getLogs(@NonNull String group, @NonNull Collection<String> pausedTargetKeys, @IntRange(from = 0) int limit, @NonNull List<Log> outLogs); @Override void clearPendingLogState(); @Override void close();  }
@Test(expected = PersistenceException.class) public void putLogWithJSONException() throws Exception { DatabaseManager databaseManager = mock(DatabaseManager.class); whenNew(DatabaseManager.class).withAnyArguments().thenReturn(databaseManager); DatabasePersistence persistence = new DatabasePersistence(mock(Context.class)); LogSerializer logSerializer = mock(LogSerializer.class); when(logSerializer.serializeLog(any(Log.class))).thenThrow(new JSONException("JSON exception")); persistence.setLogSerializer(logSerializer); persistence.putLog(mock(Log.class), "test-p1", NORMAL); }
@Test(expected = PersistenceException.class) public void putLogWithOpenDatabaseException() throws Exception { DatabaseManager databaseManager = mock(DatabaseManager.class); whenNew(DatabaseManager.class).withAnyArguments().thenReturn(databaseManager); when(databaseManager.getMaxSize()).thenReturn(-1L); DatabasePersistence persistence = new DatabasePersistence(mock(Context.class)); LogSerializer logSerializer = mock(LogSerializer.class); when(logSerializer.serializeLog(any(Log.class))).thenReturn("mock"); persistence.setLogSerializer(logSerializer); persistence.putLog(mock(Log.class), "test-p1", NORMAL); }