VWOData { public void parseData(JSONArray data) { for (int i = 0; i < data.length(); i++) { try { switch (data.getJSONObject(i).getString(Campaign.STATUS)) { case CAMPAIGN_RUNNING: if (data.getJSONObject(i).has(Campaign.VARIATION)) { Campaign tempCampaign = new Campaign(mVWO, data.getJSONObject(i)); if (VWOPersistData.isExistingCampaign(mVWO, VWOPersistData.CAMPAIGN_KEY + tempCampaign.getId())) { mCampaigns.add(tempCampaign); VWOLog.w(VWOLog.CAMPAIGN_LOGS, "User already part of campaign \"" + tempCampaign.getName() + "\"\nAnd variation \"" + tempCampaign.getVariation().getName() + "\"", true); } else { if (tempCampaign.shouldTrackUserAutomatically()) { evaluateAndMakeUserPartOfCampaign(tempCampaign); } else { mUntrackedCampaigns.add(tempCampaign); } } } else { VWOLog.wtf(VWOLog.CAMPAIGN_LOGS, "Variation object missing: \n" + data.toString(), true); } break; case CAMPAIGN_EXCLUDED: int campaignId = data.getJSONObject(i).getInt("id"); VWOPersistData vwoPersistData = new VWOPersistData(campaignId, 0); vwoPersistData.saveCampaign(mVWO.getVwoPreference()); break; default: String campaignName; if (data.getJSONObject(i).has(Campaign.CAMPAIGN_NAME)) { campaignName = data.getJSONObject(i).getString(Campaign.CAMPAIGN_NAME); } else { campaignName = data.getJSONObject(i).getString(Campaign.ID); } VWOLog.i(VWOLog.CAMPAIGN_LOGS, "Discarding Campaign \"" + campaignName + "\", because it is not running", true); break; } } catch (JSONException exception) { VWOLog.e(VWOLog.CAMPAIGN_LOGS, "Unable to parse campaign data: \n" + data.toString(), exception, true, true); } } if (mCampaigns.size() > 0) { VWOPersistData.updateReturningUser(mVWO); } generateVariationHash(); } VWOData(VWO vwo); void parseData(JSONArray data); @Nullable Object getVariationForKey(String key); @Nullable Variation getVariationForCampaign(String testKey); void saveGoal(@NonNull String goalIdentifier, Double value); static final String CAMPAIGN_RUNNING; static final String CAMPAIGN_EXCLUDED; }
@Test public void broadcastReceiverTest() throws IOException, JSONException, InterruptedException { vwo.getMessageQueue().removeAll(); final HashMap<String, String> savedCampaignMap = new HashMap<>(); VWOPreference vwoPreference = Mockito.mock(VWOPreference.class); Mockito.doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { String argument1 = invocation.getArgument(0); String argument2 = invocation.getArgument(1); savedCampaignMap.put(argument1, argument2); return null; } }).when(vwoPreference).putString(ArgumentMatchers.anyString(), ArgumentMatchers.anyString()); Mockito.when(vwo.getVwoPreference()).thenReturn(vwoPreference); IntentFilter intentFilter = new IntentFilter(VWO.Constants.NOTIFY_USER_TRACKING_STARTED); LocalBroadcastManager.getInstance(RuntimeEnvironment.application.getApplicationContext()) .registerReceiver(myBroadcastReceiverMock, intentFilter); String data = TestUtils.readJsonFile(getClass(), "com/vwo/mobile/data/campaigns.json"); VWOData vwoData = new VWOData(vwo); vwoData.parseData(new JSONArray(data)); Intent intent = intentStack.pop(); Bundle bundle = intent.getExtras(); Assert.assertNotNull(bundle); Assert.assertEquals("14", bundle.getString(VWO.Constants.ARG_CAMPAIGN_ID)); Assert.assertEquals("Campaign 14", bundle.getString(VWO.Constants.ARG_CAMPAIGN_NAME)); Assert.assertEquals("2", bundle.getString(VWO.Constants.ARG_VARIATION_ID)); Assert.assertEquals("Variation-1", bundle.getString(VWO.Constants.ARG_VARIATION_NAME)); }