Search in sources :

Example 1 with DefaultLogSerializer

use of com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer in project mobile-center-sdk-android by Microsoft.

the class MobileCenter method instanceConfigure.

/**
     * Internal SDK configuration.
     *
     * @param application application context.
     * @param appSecret   a unique and secret key used to identify the application.
     * @return true if configuration was successful, false otherwise.
     */
/* UncaughtExceptionHandler is used by PowerMock but lint does not detect it. */
@SuppressLint("VisibleForTests")
private synchronized boolean instanceConfigure(Application application, String appSecret) {
    /* Load some global constants. */
    Constants.loadFromContext(application);
    /* Enable a default log level for debuggable applications. */
    if (!mLogLevelConfigured && Constants.APPLICATION_DEBUGGABLE) {
        MobileCenterLog.setLogLevel(Log.WARN);
    }
    /* Parse and store parameters. */
    if (mApplication != null) {
        MobileCenterLog.warn(LOG_TAG, "Mobile Center may only be configured once");
        return false;
    } else if (application == null) {
        MobileCenterLog.error(LOG_TAG, "application may not be null");
    } else if (appSecret == null || appSecret.isEmpty()) {
        MobileCenterLog.error(LOG_TAG, "appSecret may not be null or empty");
    } else {
        /* Store state. */
        mApplication = application;
        mAppSecret = appSecret;
        /* If parameters are valid, init context related resources. */
        StorageHelper.initialize(application);
        /* Remember state to avoid double call PreferencesStorage. */
        boolean enabled = isInstanceEnabled();
        /* Init uncaught exception handler. */
        mUncaughtExceptionHandler = new UncaughtExceptionHandler();
        if (enabled)
            mUncaughtExceptionHandler.register();
        mServices = new HashSet<>();
        /* Init channel. */
        mLogSerializer = new DefaultLogSerializer();
        mLogSerializer.addLogFactory(StartServiceLog.TYPE, new StartServiceLogFactory());
        mLogSerializer.addLogFactory(CustomPropertiesLog.TYPE, new CustomPropertiesLogFactory());
        mChannel = new DefaultChannel(application, appSecret, mLogSerializer);
        mChannel.setEnabled(enabled);
        mChannel.addGroup(CORE_GROUP, DEFAULT_TRIGGER_COUNT, DEFAULT_TRIGGER_INTERVAL, DEFAULT_TRIGGER_MAX_PARALLEL_REQUESTS, null);
        if (mLogUrl != null)
            mChannel.setLogUrl(mLogUrl);
        MobileCenterLog.logAssert(LOG_TAG, "Mobile Center SDK configured successfully.");
        return true;
    }
    MobileCenterLog.logAssert(LOG_TAG, "Mobile Center SDK configuration failed.");
    return false;
}
Also used : StartServiceLogFactory(com.microsoft.azure.mobile.ingestion.models.json.StartServiceLogFactory) CustomPropertiesLogFactory(com.microsoft.azure.mobile.ingestion.models.json.CustomPropertiesLogFactory) DefaultChannel(com.microsoft.azure.mobile.channel.DefaultChannel) DefaultLogSerializer(com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer) SuppressLint(android.annotation.SuppressLint)

Example 2 with DefaultLogSerializer

use of com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer in project mobile-center-sdk-android by Microsoft.

the class DatabasePersistenceAndroidTest method deleteLogsForGroup.

@Test
public void deleteLogsForGroup() throws PersistenceException, IOException {
    /* Initialize database persistence. */
    DatabasePersistence persistence = new DatabasePersistence("test-persistence", "deleteLogsForGroup", 1);
    /* Set a mock log serializer. */
    LogSerializer logSerializer = new DefaultLogSerializer();
    logSerializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory());
    persistence.setLogSerializer(logSerializer);
    try {
        /* Generate a log and persist. */
        Log log1 = AndroidTestUtils.generateMockLog();
        Log log2 = AndroidTestUtils.generateMockLog();
        Log log3 = AndroidTestUtils.generateMockLog();
        Log log4 = AndroidTestUtils.generateMockLog();
        persistence.putLog("test-p1", log1);
        persistence.putLog("test-p1", log2);
        persistence.putLog("test-p2", log3);
        persistence.putLog("test-p3", log4);
        /* Get a log from persistence. */
        List<Log> outputLogs = new ArrayList<>();
        String id1 = persistence.getLogs("test-p1", 5, outputLogs);
        String id2 = persistence.getLogs("test-p2", 5, outputLogs);
        assertNotNull(id1);
        assertNotNull(id2);
        /* Delete. */
        persistence.deleteLogs("test-p1");
        persistence.deleteLogs("test-p3");
        /* Try another get for verification. */
        outputLogs.clear();
        persistence.getLogs("test-p3", 5, outputLogs);
        /* Verify. */
        Map<String, List<Long>> pendingGroups = persistence.mPendingDbIdentifiersGroups;
        assertNull(pendingGroups.get("test-p1" + id1));
        assertEquals(1, pendingGroups.get("test-p2" + id2).size());
        assertEquals(1, pendingGroups.size());
        assertEquals(0, outputLogs.size());
        assertEquals(1, persistence.mDatabaseStorage.size());
        /* Verify one log still persists in the database. */
        persistence.clearPendingLogState();
        outputLogs.clear();
        persistence.getLogs("test-p2", 5, outputLogs);
        assertEquals(1, outputLogs.size());
        assertEquals(log3, outputLogs.get(0));
        /* Count for groups. */
        assertEquals(0, persistence.countLogs("test-p1"));
        assertEquals(1, persistence.countLogs("test-p2"));
        assertEquals(0, persistence.countLogs("test-p3"));
    } finally {
        /* Close. */
        //noinspection ThrowFromFinallyBlock
        persistence.close();
    }
}
Also used : MockLogFactory(com.microsoft.azure.mobile.ingestion.models.json.MockLogFactory) Log(com.microsoft.azure.mobile.ingestion.models.Log) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) DefaultLogSerializer(com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.azure.mobile.ingestion.models.json.LogSerializer) Mockito.anyString(org.mockito.Mockito.anyString) DefaultLogSerializer(com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer) MediumTest(android.support.test.filters.MediumTest) Test(org.junit.Test)

Example 3 with DefaultLogSerializer

use of com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer in project mobile-center-sdk-android by Microsoft.

the class DatabasePersistenceAndroidTest method deleteLogs.

@Test
public void deleteLogs() throws PersistenceException, IOException {
    /* Initialize database persistence. */
    DatabasePersistence persistence = new DatabasePersistence("test-persistence", "deleteLogs", 1);
    /* Set a mock log serializer. */
    LogSerializer logSerializer = new DefaultLogSerializer();
    logSerializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory());
    persistence.setLogSerializer(logSerializer);
    try {
        /* Generate a log and persist. */
        Log log1 = AndroidTestUtils.generateMockLog();
        Log log2 = AndroidTestUtils.generateMockLog();
        Log log3 = AndroidTestUtils.generateMockLog();
        Log log4 = AndroidTestUtils.generateMockLog();
        persistence.putLog("test-p1", log1);
        persistence.putLog("test-p1", log2);
        persistence.putLog("test-p2", log3);
        persistence.putLog("test-p3", log4);
        assertEquals(2, persistence.countLogs("test-p1"));
        assertEquals(1, persistence.countLogs("test-p2"));
        assertEquals(1, persistence.countLogs("test-p3"));
        /* Get a log from persistence. */
        List<Log> outputLogs1 = new ArrayList<>();
        List<Log> outputLogs2 = new ArrayList<>();
        List<Log> outputLogs3 = new ArrayList<>();
        String id = persistence.getLogs("test-p1", 5, outputLogs1);
        persistence.getLogs("test-p2", 5, outputLogs2);
        persistence.getLogs("test-p3", 5, outputLogs3);
        /* Verify. */
        assertNotNull(id);
        assertNotEquals("", id);
        assertEquals(2, outputLogs1.size());
        assertEquals(1, outputLogs2.size());
        assertEquals(1, outputLogs3.size());
        /* Delete. */
        persistence.deleteLogs("", id);
        /* Access DatabaseStorage directly to verify the deletions. */
        DatabaseScanner scanner1 = persistence.mDatabaseStorage.getScanner(DatabasePersistence.COLUMN_GROUP, "test-p1");
        DatabaseScanner scanner2 = persistence.mDatabaseStorage.getScanner(DatabasePersistence.COLUMN_GROUP, "test-p2");
        DatabaseScanner scanner3 = persistence.mDatabaseStorage.getScanner(DatabasePersistence.COLUMN_GROUP, "test-p3");
        //noinspection TryFinallyCanBeTryWithResources
        try {
            /* Verify. */
            assertEquals(2, getIteratorSize(scanner1.iterator()));
            assertEquals(1, getIteratorSize(scanner2.iterator()));
            assertEquals(1, getIteratorSize(scanner3.iterator()));
        } finally {
            /* Close. */
            scanner1.close();
            scanner2.close();
            scanner3.close();
        }
        /* Delete. */
        persistence.deleteLogs("test-p1", id);
        /* Access DatabaseStorage directly to verify the deletions. */
        DatabaseScanner scanner4 = persistence.mDatabaseStorage.getScanner(DatabasePersistence.COLUMN_GROUP, "test-p1");
        //noinspection TryFinallyCanBeTryWithResources
        try {
            /* Verify. */
            assertEquals(0, getIteratorSize(scanner4.iterator()));
        } finally {
            /* Close. */
            scanner4.close();
        }
        /* Count logs after delete. */
        assertEquals(0, persistence.countLogs("test-p1"));
        assertEquals(1, persistence.countLogs("test-p2"));
        assertEquals(1, persistence.countLogs("test-p3"));
    } finally {
        /* Close. */
        //noinspection ThrowFromFinallyBlock
        persistence.close();
    }
}
Also used : MockLogFactory(com.microsoft.azure.mobile.ingestion.models.json.MockLogFactory) Log(com.microsoft.azure.mobile.ingestion.models.Log) DatabaseScanner(com.microsoft.azure.mobile.utils.storage.StorageHelper.DatabaseStorage.DatabaseScanner) ArrayList(java.util.ArrayList) DefaultLogSerializer(com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.azure.mobile.ingestion.models.json.LogSerializer) Mockito.anyString(org.mockito.Mockito.anyString) DefaultLogSerializer(com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer) MediumTest(android.support.test.filters.MediumTest) Test(org.junit.Test)

Example 4 with DefaultLogSerializer

use of com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer in project mobile-center-sdk-android by Microsoft.

the class DatabasePersistenceAndroidTest method putLog.

@Test
public void putLog() throws PersistenceException, IOException {
    /* Initialize database persistence. */
    DatabasePersistence persistence = new DatabasePersistence("test-persistence", "putLog", 1);
    /* Set a mock log serializer. */
    LogSerializer logSerializer = new DefaultLogSerializer();
    logSerializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory());
    persistence.setLogSerializer(logSerializer);
    try {
        /* Initial count is 0. */
        assertEquals(0, persistence.countLogs("test-p1"));
        /* Generate a log and persist. */
        Log log = AndroidTestUtils.generateMockLog();
        persistence.putLog("test-p1", log);
        /* Count logs. */
        assertEquals(1, persistence.countLogs("test-p1"));
        /* Get a log from persistence. */
        List<Log> outputLogs = new ArrayList<>();
        persistence.getLogs("test-p1", 1, outputLogs);
        assertEquals(1, outputLogs.size());
        assertEquals(log, outputLogs.get(0));
        assertEquals(1, persistence.countLogs("test-p1"));
    } finally {
        /* Close. */
        //noinspection ThrowFromFinallyBlock
        persistence.close();
    }
}
Also used : MockLogFactory(com.microsoft.azure.mobile.ingestion.models.json.MockLogFactory) Log(com.microsoft.azure.mobile.ingestion.models.Log) ArrayList(java.util.ArrayList) DefaultLogSerializer(com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.azure.mobile.ingestion.models.json.LogSerializer) DefaultLogSerializer(com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer) MediumTest(android.support.test.filters.MediumTest) Test(org.junit.Test)

Example 5 with DefaultLogSerializer

use of com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer in project mobile-center-sdk-android by Microsoft.

the class DatabasePersistenceAndroidTest method getLogsException.

@Test
public void getLogsException() throws PersistenceException, IOException, JSONException {
    /* Initialize database persistence. */
    DatabasePersistence persistence = new DatabasePersistence("test-persistence", "getLogs", 1);
    /* Set a mock log serializer. */
    LogSerializer logSerializer = spy(new DefaultLogSerializer());
    /* Throw a JSON exception for the first call. */
    doThrow(new JSONException("JSON exception")).doReturn(AndroidTestUtils.generateMockLog()).doThrow(new JSONException("JSON exception")).doReturn(AndroidTestUtils.generateMockLog()).when(logSerializer).deserializeLog(anyString());
    persistence.setLogSerializer(logSerializer);
    try {
        /* Test constants. */
        int numberOfLogs = 4;
        /* Generate a log and persist. */
        Log[] logs = new Log[numberOfLogs];
        for (int i = 0; i < logs.length; i++) logs[i] = AndroidTestUtils.generateMockLog();
        /* Put. */
        for (Log log : logs) persistence.putLog("test", log);
        /* Get. */
        List<Log> outputLogs = new ArrayList<>();
        persistence.getLogs("test", 10, outputLogs);
        assertEquals(numberOfLogs / 2, outputLogs.size());
        assertEquals(2, persistence.mDatabaseStorage.size());
    } finally {
        /* Close. */
        //noinspection ThrowFromFinallyBlock
        persistence.close();
    }
}
Also used : Log(com.microsoft.azure.mobile.ingestion.models.Log) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) DefaultLogSerializer(com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.azure.mobile.ingestion.models.json.LogSerializer) DefaultLogSerializer(com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer) SuppressLint(android.annotation.SuppressLint) MediumTest(android.support.test.filters.MediumTest) Test(org.junit.Test)

Aggregations

DefaultLogSerializer (com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer)11 LogSerializer (com.microsoft.azure.mobile.ingestion.models.json.LogSerializer)10 Test (org.junit.Test)10 Log (com.microsoft.azure.mobile.ingestion.models.Log)8 ArrayList (java.util.ArrayList)8 MediumTest (android.support.test.filters.MediumTest)6 MockLogFactory (com.microsoft.azure.mobile.ingestion.models.json.MockLogFactory)5 SuppressLint (android.annotation.SuppressLint)3 LogContainer (com.microsoft.azure.mobile.ingestion.models.LogContainer)2 UUID (java.util.UUID)2 JSONException (org.json.JSONException)2 Mockito.anyString (org.mockito.Mockito.anyString)2 EventLog (com.microsoft.azure.mobile.analytics.ingestion.models.EventLog)1 PageLog (com.microsoft.azure.mobile.analytics.ingestion.models.PageLog)1 StartSessionLog (com.microsoft.azure.mobile.analytics.ingestion.models.StartSessionLog)1 EventLogFactory (com.microsoft.azure.mobile.analytics.ingestion.models.json.EventLogFactory)1 PageLogFactory (com.microsoft.azure.mobile.analytics.ingestion.models.json.PageLogFactory)1 StartSessionLogFactory (com.microsoft.azure.mobile.analytics.ingestion.models.json.StartSessionLogFactory)1 DefaultChannel (com.microsoft.azure.mobile.channel.DefaultChannel)1 Device (com.microsoft.azure.mobile.ingestion.models.Device)1