<commit_before>package org.infobip.mobile.messaging.geo.tools;

import android.annotation.SuppressLint;
import android.content.Context;
import android.preference.PreferenceManager;

import org.infobip.mobile.messaging.Message;
import org.infobip.mobile.messaging.MobileMessaging;
import org.infobip.mobile.messaging.MobileMessagingCore;
import org.infobip.mobile.messaging.MobileMessagingProperty;
import org.infobip.mobile.messaging.android.MobileMessagingBaseTestCase;
import org.infobip.mobile.messaging.dal.sqlite.DatabaseHelper;
import org.infobip.mobile.messaging.dal.sqlite.SqliteDatabaseProvider;
import org.infobip.mobile.messaging.geo.Area;
import org.infobip.mobile.messaging.geo.Geo;
import org.infobip.mobile.messaging.geo.GeoEventSettings;
import org.infobip.mobile.messaging.geo.GeoEventType;
import org.infobip.mobile.messaging.geo.GeoLatLng;
import org.infobip.mobile.messaging.geo.geofencing.GeofencingHelper;
import org.infobip.mobile.messaging.geo.mapper.GeoDataMapper;
import org.infobip.mobile.messaging.geo.platform.GeoBroadcaster;
import org.infobip.mobile.messaging.geo.report.GeoReport;
import org.infobip.mobile.messaging.geo.storage.TestMessageStore;
import org.infobip.mobile.messaging.logging.MobileMessagingLogger;
import org.infobip.mobile.messaging.mobile.MobileApiResourceProvider;
import org.infobip.mobile.messaging.notification.NotificationHandler;
import org.infobip.mobile.messaging.platform.Broadcaster;
import org.infobip.mobile.messaging.platform.Time;
import org.infobip.mobile.messaging.platform.TimeProvider;
import org.infobip.mobile.messaging.storage.MessageStore;
import org.infobip.mobile.messaging.util.PreferenceHelper;
import org.junit.After;
import org.junit.Before;
import org.mockito.Mockito;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

/**
 * @author sslavin
 * @since 10/03/2017.
 */

public abstract class MobileMessagingTestCase extends MobileMessagingBaseTestCase {

    protected static MessageStore geoStore;
    private static GeofencingHelper geofencingHelper;
    protected DatabaseHelper databaseHelper;
    protected SqliteDatabaseProvider databaseProvider;
    protected GeoBroadcaster geoBroadcaster;
    protected Broadcaster coreBroadcaster;
    protected TestTimeProvider time;
    protected MobileMessagingCore mobileMessagingCore;
    protected MobileMessaging mobileMessaging;
    protected NotificationHandler notificationHandler;

    protected static class TestTimeProvider implements TimeProvider {

        long delta = 0;
        boolean overwritten = false;

        public void forward(long time, TimeUnit unit) {
            delta += unit.toMillis(time);
        }

        public void backward(long time, TimeUnit unit) {
            delta -= unit.toMillis(time);
        }

        public void reset() {
            overwritten = false;
            delta = 0;
        }

        public void set(long time) {
            overwritten = true;
            delta = time;
        }

        @Override
        public long now() {
            if (overwritten) {
                return delta;
            } else {
                return System.currentTimeMillis() + delta;
            }
        }
    }

    protected void enableMessageStoreForReceivedMessages() {
        PreferenceHelper.saveClass(context, MobileMessagingProperty.MESSAGE_STORE_CLASS, TestMessageStore.class);
    }

    @SuppressLint("ApplySharedPref")
    @Before
    public void setUp() throws Exception {
        super.setUp();

        PreferenceManager.getDefaultSharedPreferences(context).edit().clear().commit();

        PreferenceHelper.saveString(context, MobileMessagingProperty.API_URI, "http://127.0.0.1:" + debugServer.getListeningPort() + "/");
        PreferenceHelper.saveString(context, MobileMessagingProperty.APPLICATION_CODE, "TestApplicationCode");
        PreferenceHelper.saveString(context, MobileMessagingProperty.INFOBIP_REGISTRATION_ID, "TestDeviceInstanceId");
        PreferenceHelper.saveString(context, MobileMessagingProperty.GCM_REGISTRATION_ID, "TestRegistrationId");
        PreferenceHelper.saveBoolean(context, MobileMessagingProperty.GCM_REGISTRATION_ID_REPORTED, true);

        MobileMessagingLogger.enforce();

        time = new TestTimeProvider();
        Time.reset(time);

        notificationHandler = Mockito.mock(NotificationHandler.class);
        coreBroadcaster = Mockito.mock(Broadcaster.class);
        mobileMessagingCore = MobileMessagingTestable.create(context, coreBroadcaster);
        mobileMessaging = mobileMessagingCore;
        geofencingHelper = new GeofencingHelper(context);

        MobileApiResourceProvider.INSTANCE.resetMobileApi();

        geoBroadcaster = Mockito.mock(GeoBroadcaster.class);
        geofencingHelper.removeUnreportedGeoEvents();

        databaseHelper = MobileMessagingCore.getDatabaseHelper(context);
        databaseProvider = MobileMessagingCore.getDatabaseProvider(context);
        geoStore = geofencingHelper.getMessageStoreForGeo();
    }

    @After
    public void tearDown() throws Exception {
        super.tearDown();

        time.reset();
        databaseProvider.deleteDatabase();
    }

    /**
     * Generates messages with provided id
     *
     * @param saveToStorage set to true to save messages to message store
     * @param messageId     message id for a message
     * @param campaignId    id of a campaign for a message
     * @param areas         list of areas to save to message
     * @return new message
     */
    protected static Message createMessage(Context context, String messageId, String campaignId, boolean saveToStorage, Area... areas) {
        return createMessage(context, messageId, saveToStorage, new Geo(0.0, 0.0, null, null, null, campaignId, Arrays.asList(areas), new ArrayList<GeoEventSettings>(), 0, null));
    }

    /**
     * Generates messages with provided id
     *
     * @param saveToStorage set to true to save messages to message store
     * @param messageId     message id for a message
     * @param campaignId    id of a campaign for a message
     * @param saveToStorage true if the message should be saved to storage
     * @param contentUrl    URL for the rich data
     * @param areas         list of areas to save to message
     * @return new message
     */
    protected static Message createMessage(Context context, String messageId, String campaignId, boolean saveToStorage, String contentUrl, Area... areas) {
        return createMessage(context, messageId, saveToStorage, new Geo(0.0, 0.0, null, null, null, campaignId, Arrays.asList(areas), new ArrayList<GeoEventSettings>(), 0, contentUrl));
    }

    /**
     * Generates messages with provided ids and geo campaign object
     *
     * @param saveToStorage set to true to save messages to message store
     * @param messageId     message id for a message
     * @param geo           geo campaign object
     * @return new message
     */
    protected static Message createMessage(Context context, String messageId, boolean saveToStorage, Geo... geo) {
        Message message = new Message();
        message.setMessageId(messageId);
        message.setSentTimestamp(0);

        boolean isGeo = geo.length > 0 && geo[0] != null && geo[0].getAreasList() != null && !geo[0].getAreasList().isEmpty();
        if (isGeo) {
            message.setInternalData(GeoDataMapper.geoToInternalData(geo[0]));
        }

        if (saveToStorage) {
            geoStore.save(context, message);
        }
        return message;
    }
<commit_msg>Geo test adjustment for new schema<commit_after>package org.infobip.mobile.messaging.geo.tools;

import android.annotation.SuppressLint;
import android.content.Context;
import android.preference.PreferenceManager;

import org.infobip.mobile.messaging.Message;
import org.infobip.mobile.messaging.MobileMessaging;
import org.infobip.mobile.messaging.MobileMessagingCore;
import org.infobip.mobile.messaging.MobileMessagingProperty;
import org.infobip.mobile.messaging.android.MobileMessagingBaseTestCase;
import org.infobip.mobile.messaging.api.support.http.serialization.JsonSerializer;
import org.infobip.mobile.messaging.dal.json.InternalDataMapper;
import org.infobip.mobile.messaging.dal.sqlite.DatabaseHelper;
import org.infobip.mobile.messaging.dal.sqlite.SqliteDatabaseProvider;
import org.infobip.mobile.messaging.geo.Area;
import org.infobip.mobile.messaging.geo.Geo;
import org.infobip.mobile.messaging.geo.GeoEventSettings;
import org.infobip.mobile.messaging.geo.GeoEventType;
import org.infobip.mobile.messaging.geo.GeoLatLng;
import org.infobip.mobile.messaging.geo.geofencing.GeofencingHelper;
import org.infobip.mobile.messaging.geo.mapper.GeoDataMapper;
import org.infobip.mobile.messaging.geo.platform.GeoBroadcaster;
import org.infobip.mobile.messaging.geo.report.GeoReport;
import org.infobip.mobile.messaging.geo.storage.TestMessageStore;
import org.infobip.mobile.messaging.logging.MobileMessagingLogger;
import org.infobip.mobile.messaging.mobile.MobileApiResourceProvider;
import org.infobip.mobile.messaging.notification.NotificationHandler;
import org.infobip.mobile.messaging.platform.Broadcaster;
import org.infobip.mobile.messaging.platform.Time;
import org.infobip.mobile.messaging.platform.TimeProvider;
import org.infobip.mobile.messaging.storage.MessageStore;
import org.infobip.mobile.messaging.util.PreferenceHelper;
import org.junit.After;
import org.junit.Before;
import org.mockito.Mockito;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

/**
 * @author sslavin
 * @since 10/03/2017.
 */

public abstract class MobileMessagingTestCase extends MobileMessagingBaseTestCase {

    protected static MessageStore geoStore;
    private static GeofencingHelper geofencingHelper;
    protected DatabaseHelper databaseHelper;
    protected SqliteDatabaseProvider databaseProvider;
    protected GeoBroadcaster geoBroadcaster;
    protected Broadcaster coreBroadcaster;
    protected TestTimeProvider time;
    protected MobileMessagingCore mobileMessagingCore;
    protected MobileMessaging mobileMessaging;
    protected NotificationHandler notificationHandler;

    protected static class TestTimeProvider implements TimeProvider {

        long delta = 0;
        boolean overwritten = false;

        public void forward(long time, TimeUnit unit) {
            delta += unit.toMillis(time);
        }

        public void backward(long time, TimeUnit unit) {
            delta -= unit.toMillis(time);
        }

        public void reset() {
            overwritten = false;
            delta = 0;
        }

        public void set(long time) {
            overwritten = true;
            delta = time;
        }

        @Override
        public long now() {
            if (overwritten) {
                return delta;
            } else {
                return System.currentTimeMillis() + delta;
            }
        }
    }

    protected void enableMessageStoreForReceivedMessages() {
        PreferenceHelper.saveClass(context, MobileMessagingProperty.MESSAGE_STORE_CLASS, TestMessageStore.class);
    }

    @SuppressLint("ApplySharedPref")
    @Before
    public void setUp() throws Exception {
        super.setUp();

        PreferenceManager.getDefaultSharedPreferences(context).edit().clear().commit();

        PreferenceHelper.saveString(context, MobileMessagingProperty.API_URI, "http://127.0.0.1:" + debugServer.getListeningPort() + "/");
        PreferenceHelper.saveString(context, MobileMessagingProperty.APPLICATION_CODE, "TestApplicationCode");
        PreferenceHelper.saveString(context, MobileMessagingProperty.INFOBIP_REGISTRATION_ID, "TestDeviceInstanceId");
        PreferenceHelper.saveString(context, MobileMessagingProperty.GCM_REGISTRATION_ID, "TestRegistrationId");
        PreferenceHelper.saveBoolean(context, MobileMessagingProperty.GCM_REGISTRATION_ID_REPORTED, true);

        MobileMessagingLogger.enforce();

        time = new TestTimeProvider();
        Time.reset(time);

        notificationHandler = Mockito.mock(NotificationHandler.class);
        coreBroadcaster = Mockito.mock(Broadcaster.class);
        mobileMessagingCore = MobileMessagingTestable.create(context, coreBroadcaster);
        mobileMessaging = mobileMessagingCore;
        geofencingHelper = new GeofencingHelper(context);

        MobileApiResourceProvider.INSTANCE.resetMobileApi();

        geoBroadcaster = Mockito.mock(GeoBroadcaster.class);
        geofencingHelper.removeUnreportedGeoEvents();

        databaseHelper = MobileMessagingCore.getDatabaseHelper(context);
        databaseProvider = MobileMessagingCore.getDatabaseProvider(context);
        geoStore = geofencingHelper.getMessageStoreForGeo();
    }

    @After
    public void tearDown() throws Exception {
        super.tearDown();

        time.reset();
        databaseProvider.deleteDatabase();
    }

    /**
     * Generates messages with provided id
     *
     * @param saveToStorage set to true to save messages to message store
     * @param messageId     message id for a message
     * @param campaignId    id of a campaign for a message
     * @param areas         list of areas to save to message
     * @return new message
     */
    protected static Message createMessage(Context context, String messageId, String campaignId, boolean saveToStorage, Area... areas) {
        return createMessage(context, messageId, saveToStorage, new Geo(0.0, 0.0, null, null, null, campaignId, Arrays.asList(areas), new ArrayList<GeoEventSettings>(), 0, null));
    }

    /**
     * Generates messages with provided id
     *
     * @param saveToStorage set to true to save messages to message store
     * @param messageId     message id for a message
     * @param campaignId    id of a campaign for a message
     * @param saveToStorage true if the message should be saved to storage
     * @param contentUrl    URL for the rich data
     * @param areas         list of areas to save to message
     * @return new message
     */
    protected static Message createMessage(Context context, String messageId, String campaignId, boolean saveToStorage, String contentUrl, Area... areas) {
        return createMessage(context, messageId, saveToStorage, new Geo(0.0, 0.0, null, null, null, campaignId, Arrays.asList(areas), new ArrayList<GeoEventSettings>(), 0, contentUrl));
    }

    /**
     * Generates messages with provided ids and geo campaign object
     *
     * @param saveToStorage set to true to save messages to message store
     * @param messageId     message id for a message
     * @param geo           geo campaign object
     * @return new message
     */
    protected static Message createMessage(Context context, String messageId, boolean saveToStorage, Geo... geo) {
        Message message = new Message();
        message.setMessageId(messageId);
        message.setSentTimestamp(0);

        boolean isGeo = geo.length > 0 && geo[0] != null && geo[0].getAreasList() != null && !geo[0].getAreasList().isEmpty();
        if (isGeo) {
            JsonSerializer serializer = new JsonSerializer();
            message.setInternalData(GeoDataMapper.geoToInternalData(geo[0]));
            message.setContentUrl(InternalDataMapper.getInternalDataContentUrl(serializer.serialize(geo[0])));
        }

        if (saveToStorage) {
            geoStore.save(context, message);
        }
        return message;
    }

{'fraction_non_alphanumeric': 0.08705212779718019, 'fraction_numerical': 0.004203854611305135, 'mean_word_length': 3.525314603453322, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3529855', 'n_tokens_mistral': 4395, 'n_tokens_neox': 4163, 'n_words': 954}