VWOData { public void saveGoal(@NonNull String goalIdentifier, Double value) { for (Campaign campaign : mCampaigns) { for (Goal goal : campaign.getGoals()) { if (goal.getIdentifier().equals(goalIdentifier)) { String campaignData = mVWO.getVwoPreference().getString(VWOPersistData.CAMPAIGN_KEY + campaign.getId()); if (campaignData != null && !campaignData.equals("")) { try { JSONObject jsonObject = new JSONObject(campaignData); VWOPersistData vwoPersistData = new VWOPersistData(jsonObject); if (!vwoPersistData.isGoalExists(goal.getId())) { vwoPersistData.addGoal(goal.getId()); vwoPersistData.saveCampaign(mVWO.getVwoPreference()); String goalUrl; if(value != null) { goalUrl = mVWO.getVwoUrlBuilder().getGoalUrl(campaign.getId(), campaign.getVariation().getId(), goal.getId(), value); } else { goalUrl = mVWO.getVwoUrlBuilder().getGoalUrl(campaign.getId(), campaign.getVariation().getId(), goal.getId()); } GoalEntry goalEntry = new GoalEntry(goalUrl, campaign.getId(), campaign.getVariation().getId(), goal.getId()); mVWO.getMessageQueue().add(goalEntry); } else { VWOLog.w(VWOLog.CAMPAIGN_LOGS, "Duplicate goal identifier: " + goalIdentifier, true); } } catch (Exception exception) { VWOLog.w(VWOLog.CAMPAIGN_LOGS, "Unable to generate goal data object", exception, true); } } } } } } 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 saveGoalTest() throws IOException, JSONException, InterruptedException { vwo.getMessageQueue().removeAll(); final HashMap<String, String> savedCampaignMap = new HashMap<>(); String campaignData = "{\"campaignId\":14,\"variationId\":2,\"goals\":[]}"; VWOPreference vwoPreference = Mockito.mock(VWOPreference.class); Mockito.when(vwoPreference.isPartOfCampaign(anyString())).thenReturn(true); 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(vwoPreference.getString(ArgumentMatchers.eq("campaign_14"))).thenReturn(campaignData); Mockito.when(vwo.getVwoPreference()).thenReturn(vwoPreference); String data = TestUtils.readJsonFile(getClass(), "com/vwo/mobile/data/campaigns.json"); VWOData vwoData = new VWOData(vwo); vwoData.parseData(new JSONArray(data)); vwoData.saveGoal("goal", null); synchronized (lock) { lock.wait(1000); } Assert.assertEquals("{\"campaignId\":14,\"variationId\":2,\"goals\":[349]}", savedCampaignMap.get("campaign_14")); Assert.assertEquals("https: }