ServerPositioningSource implements PositioningSource { @NonNull @VisibleForTesting MoPubClientPositioning parseJsonResponse(@Nullable String json) throws JSONException { if (json == null || json.equals("")) { throw new JSONException("Empty response"); } JSONObject jsonObject = new JSONObject(json); String error = jsonObject.optString("error", null); if (error != null) { throw new JSONException(error); } JSONArray fixed = jsonObject.optJSONArray(FIXED_KEY); JSONObject repeating = jsonObject.optJSONObject(REPEATING_KEY); MoPubClientPositioning positioning = new MoPubClientPositioning(); if (fixed == null && repeating == null) { throw new JSONException("Must contain fixed or repeating positions"); } if (fixed != null) { parseFixedJson(fixed, positioning); } if (repeating != null) { parseRepeatingJson(repeating, positioning); } return positioning; } ServerPositioningSource(@NonNull final Context context); @VisibleForTesting  ServerPositioningSource(@NonNull final Context context,
            @NonNull final DownloadTaskProvider downloadTaskProvider); @Override void loadPositions(@NonNull String adUnitId, @NonNull PositioningListener listener);  }
@Test public void parseJsonResponse_noFixedPositions_shouldReturnEmptyPositioning() throws JSONException { MoPubClientPositioning positioning = subject.parseJsonResponse( "{fixed: []}"); assertThat(positioning.getFixedPositions()).isEmpty(); assertThat(positioning.getRepeatingInterval()).isEqualTo(MoPubClientPositioning.NO_REPEAT); }
@Test public void parseJsonResponse_oneFixedPosition_shouldReturnValidPositioning() throws JSONException { MoPubClientPositioning positioning = subject.parseJsonResponse( "{fixed: [{position: 2}]}"); assertThat(positioning.getFixedPositions()).containsOnly(2); assertThat(positioning.getRepeatingInterval()).isEqualTo(MoPubClientPositioning.NO_REPEAT); }
@Test public void parseJsonResponse_twoFixedPositions_shouldReturnValidPositioning() throws JSONException { MoPubClientPositioning positioning = subject.parseJsonResponse( "{fixed: [{position: 1}, {position: 8}]}"); assertThat(positioning.getFixedPositions()).containsExactly(1, 8); assertThat(positioning.getRepeatingInterval()).isEqualTo(MoPubClientPositioning.NO_REPEAT); }
@Test public void parseJsonResponse_twoFixedPositions_shouldIgnoreNonZeroSection() throws JSONException { MoPubClientPositioning positioning = subject.parseJsonResponse( "{fixed: [{section: 0, position: 5}, {section: 1, position: 8}]}"); assertThat(positioning.getFixedPositions()).containsOnly(5); assertThat(positioning.getRepeatingInterval()).isEqualTo(MoPubClientPositioning.NO_REPEAT); }
@Test public void parseJsonResponse_repeatingInterval_shouldReturnValidPositioning() throws JSONException { MoPubClientPositioning positioning = subject.parseJsonResponse( "{repeating: {interval: 2}}"); assertThat(positioning.getFixedPositions()).isEmpty(); assertThat(positioning.getRepeatingInterval()).isEqualTo(2); }
@Test public void parseJsonResponse_fixedAndRepeating_shouldReturnValidPositioning() throws JSONException { MoPubClientPositioning positioning = subject.parseJsonResponse( "{fixed: [{position: 0}, {position: 1}], repeating: {interval: 2}}"); assertThat(positioning.getFixedPositions()).containsExactly(0, 1); assertThat(positioning.getRepeatingInterval()).isEqualTo(2); }