1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.adservices.data.measurement; 18 19 import static java.util.function.Predicate.not; 20 21 import android.database.Cursor; 22 import android.database.sqlite.SQLiteDatabase; 23 import android.net.Uri; 24 25 import com.android.adservices.common.WebUtil; 26 import com.android.adservices.service.measurement.Attribution; 27 import com.android.adservices.service.measurement.EventReport; 28 import com.android.adservices.service.measurement.KeyValueData; 29 import com.android.adservices.service.measurement.Source; 30 import com.android.adservices.service.measurement.Trigger; 31 import com.android.adservices.service.measurement.aggregation.AggregateEncryptionKey; 32 import com.android.adservices.service.measurement.aggregation.AggregateReport; 33 import com.android.adservices.service.measurement.registration.AsyncRedirect; 34 import com.android.adservices.service.measurement.registration.AsyncRegistration; 35 import com.android.adservices.service.measurement.reporting.DebugReport; 36 import com.android.adservices.service.measurement.util.UnsignedLong; 37 38 import org.json.JSONArray; 39 import org.json.JSONException; 40 import org.json.JSONObject; 41 42 import java.util.ArrayList; 43 import java.util.Arrays; 44 import java.util.Collections; 45 import java.util.Comparator; 46 import java.util.List; 47 import java.util.Locale; 48 import java.util.concurrent.TimeUnit; 49 import java.util.stream.Collectors; 50 51 /** 52 * Class for providing test data for measurement tests. 53 */ 54 public class DbState { 55 56 private static final String DEFAULT_REGISTRATION_URI = 57 WebUtil.validUrl("https://test.example.test"); 58 List<Source> mSourceList; 59 List<SourceDestination> mSourceDestinationList; 60 List<Trigger> mTriggerList; 61 List<EventReport> mEventReportList; 62 List<Attribution> mAttrRateLimitList; 63 List<AggregateEncryptionKey> mAggregateEncryptionKeyList; 64 List<AggregateReport> mAggregateReportList; 65 List<DebugReport> mDebugReportList; 66 List<AsyncRegistration> mAsyncRegistrationList; 67 List<KeyValueData> mKeyValueDataList; 68 DbState()69 public DbState() { 70 mSourceList = new ArrayList<>(); 71 mSourceDestinationList = new ArrayList<>(); 72 mTriggerList = new ArrayList<>(); 73 mEventReportList = new ArrayList<>(); 74 mAttrRateLimitList = new ArrayList<>(); 75 mAggregateEncryptionKeyList = new ArrayList<>(); 76 mAggregateReportList = new ArrayList<>(); 77 mDebugReportList = new ArrayList<>(); 78 mAsyncRegistrationList = new ArrayList<>(); 79 mKeyValueDataList = new ArrayList<>(); 80 } 81 DbState(JSONObject testInput)82 public DbState(JSONObject testInput) throws JSONException { 83 this(); 84 85 // Sources 86 if (testInput.has("sources")) { 87 JSONArray sources = testInput.getJSONArray("sources"); 88 for (int i = 0; i < sources.length(); i++) { 89 JSONObject sJSON = sources.getJSONObject(i); 90 Source source = getSourceFrom(sJSON); 91 mSourceList.add(source); 92 } 93 } 94 95 // SourceDestinations 96 if (testInput.has("source_destinations")) { 97 JSONArray sourceDestinations = testInput.getJSONArray("source_destinations"); 98 for (int i = 0; i < sourceDestinations.length(); i++) { 99 JSONObject sdJSON = sourceDestinations.getJSONObject(i); 100 SourceDestination sourceDestination = getSourceDestinationFrom(sdJSON); 101 mSourceDestinationList.add(sourceDestination); 102 } 103 } 104 105 // Triggers 106 if (testInput.has("triggers")) { 107 JSONArray triggers = testInput.getJSONArray("triggers"); 108 for (int i = 0; i < triggers.length(); i++) { 109 JSONObject tJSON = triggers.getJSONObject(i); 110 Trigger trigger = getTriggerFrom(tJSON); 111 mTriggerList.add(trigger); 112 } 113 } 114 115 // EventReports 116 if (testInput.has("event_reports")) { 117 JSONArray eventReports = testInput.getJSONArray("event_reports"); 118 for (int i = 0; i < eventReports.length(); i++) { 119 JSONObject rJSON = eventReports.getJSONObject(i); 120 EventReport eventReport = getEventReportFrom(rJSON); 121 mEventReportList.add(eventReport); 122 } 123 } 124 125 // Attributions 126 if (testInput.has("attributions")) { 127 JSONArray attrs = testInput.getJSONArray("attributions"); 128 for (int i = 0; i < attrs.length(); i++) { 129 JSONObject attrJSON = attrs.getJSONObject(i); 130 Attribution attrRateLimit = getAttributionFrom(attrJSON); 131 mAttrRateLimitList.add(attrRateLimit); 132 } 133 } 134 135 // AggregateEncryptionKeys 136 if (testInput.has("aggregate_encryption_keys")) { 137 JSONArray keys = testInput.getJSONArray("aggregate_encryption_keys"); 138 for (int i = 0; i < keys.length(); i++) { 139 JSONObject keyJSON = keys.getJSONObject(i); 140 AggregateEncryptionKey key = getAggregateEncryptionKeyFrom(keyJSON); 141 mAggregateEncryptionKeyList.add(key); 142 } 143 } 144 145 if (testInput.has("aggregate_reports")) { 146 // AggregateReports 147 JSONArray aggregateReports = testInput.getJSONArray("aggregate_reports"); 148 for (int i = 0; i < aggregateReports.length(); i++) { 149 JSONObject rJSON = aggregateReports.getJSONObject(i); 150 AggregateReport aggregateReport = getAggregateReportFrom(rJSON); 151 mAggregateReportList.add(aggregateReport); 152 } 153 } 154 155 if (testInput.has("debug_reports")) { 156 // DebugReports 157 JSONArray debugReports = testInput.getJSONArray("debug_reports"); 158 for (int i = 0; i < debugReports.length(); i++) { 159 JSONObject rJSON = debugReports.getJSONObject(i); 160 DebugReport debugReport = getDebugReportFrom(rJSON); 161 mDebugReportList.add(debugReport); 162 } 163 } 164 165 if (testInput.has("async_registrations")) { 166 // AsyncRegistrations 167 JSONArray asyncRegistrations = testInput.getJSONArray("async_registrations"); 168 for (int i = 0; i < asyncRegistrations.length(); i++) { 169 JSONObject aJSON = asyncRegistrations.getJSONObject(i); 170 AsyncRegistration asyncRegistration = getAsyncRegistrationFrom(aJSON); 171 mAsyncRegistrationList.add(asyncRegistration); 172 } 173 } 174 175 if (testInput.has("key_values")) { 176 // KeyValues 177 JSONArray keyValues = testInput.getJSONArray("key_values"); 178 for (int i = 0; i < keyValues.length(); i++) { 179 JSONObject aJSON = keyValues.getJSONObject(i); 180 KeyValueData keyValueData = getKeyValueDataFrom(aJSON); 181 mKeyValueDataList.add(keyValueData); 182 } 183 } 184 } 185 DbState(SQLiteDatabase readerDB)186 public DbState(SQLiteDatabase readerDB) { 187 this(); 188 189 // Read Source table 190 Cursor sourceCursor = readerDB.query(MeasurementTables.SourceContract.TABLE, 191 null, null, null, null, null, MeasurementTables.SourceContract.ID); 192 while (sourceCursor.moveToNext()) { 193 mSourceList.add(SqliteObjectMapper.constructSourceFromCursor(sourceCursor)); 194 } 195 sourceCursor.close(); 196 197 // Read SourceDestination table 198 Cursor destCursor = readerDB.query(MeasurementTables.SourceDestination.TABLE, 199 null, null, null, null, null, null); 200 while (destCursor.moveToNext()) { 201 mSourceDestinationList.add(getSourceDestinationFrom(destCursor)); 202 } 203 destCursor.close(); 204 205 // Read Trigger table 206 Cursor triggerCursor = readerDB.query(MeasurementTables.TriggerContract.TABLE, 207 null, null, null, null, null, MeasurementTables.TriggerContract.ID); 208 while (triggerCursor.moveToNext()) { 209 mTriggerList.add(SqliteObjectMapper.constructTriggerFromCursor(triggerCursor)); 210 } 211 triggerCursor.close(); 212 213 // Read EventReport table 214 Cursor eventReportCursor = 215 readerDB.query( 216 MeasurementTables.EventReportContract.TABLE, 217 null, 218 null, 219 null, 220 null, 221 null, 222 MeasurementTables.EventReportContract.ID); 223 while (eventReportCursor.moveToNext()) { 224 mEventReportList.add( 225 SqliteObjectMapper.constructEventReportFromCursor(eventReportCursor)); 226 } 227 eventReportCursor.close(); 228 229 // Read Attribution table 230 Cursor attrCursor = readerDB.query(MeasurementTables.AttributionContract.TABLE, 231 null, null, null, null, null, MeasurementTables.AttributionContract.ID); 232 while (attrCursor.moveToNext()) { 233 mAttrRateLimitList.add(getAttributionFrom(attrCursor)); 234 } 235 attrCursor.close(); 236 237 // Read AggregateReport table 238 Cursor aggregateReportCursor = 239 readerDB.query( 240 MeasurementTables.AggregateReport.TABLE, 241 null, 242 null, 243 null, 244 null, 245 null, 246 MeasurementTables.AggregateReport.ID); 247 while (aggregateReportCursor.moveToNext()) { 248 mAggregateReportList.add( 249 SqliteObjectMapper.constructAggregateReport(aggregateReportCursor)); 250 } 251 aggregateReportCursor.close(); 252 253 // Read AggregateEncryptionKey table 254 Cursor keyCursor = readerDB.query(MeasurementTables.AggregateEncryptionKey.TABLE, 255 null, null, null, null, null, MeasurementTables.AggregateEncryptionKey.ID); 256 while (keyCursor.moveToNext()) { 257 mAggregateEncryptionKeyList.add( 258 SqliteObjectMapper.constructAggregateEncryptionKeyFromCursor(keyCursor)); 259 } 260 keyCursor.close(); 261 262 // Read DebugReport table 263 Cursor debugReportCursor = 264 readerDB.query( 265 MeasurementTables.DebugReportContract.TABLE, 266 null, 267 null, 268 null, 269 null, 270 null, 271 MeasurementTables.DebugReportContract.ID); 272 while (debugReportCursor.moveToNext()) { 273 mDebugReportList.add( 274 SqliteObjectMapper.constructDebugReportFromCursor(debugReportCursor)); 275 } 276 debugReportCursor.close(); 277 278 // Read AsyncRegistration table 279 Cursor asyncRegistrationCursor = 280 readerDB.query( 281 MeasurementTables.AsyncRegistrationContract.TABLE, 282 null, 283 null, 284 null, 285 null, 286 null, 287 null); 288 while (asyncRegistrationCursor.moveToNext()) { 289 mAsyncRegistrationList.add( 290 SqliteObjectMapper.constructAsyncRegistration(asyncRegistrationCursor)); 291 } 292 asyncRegistrationCursor.close(); 293 294 // Read KeyValueData table 295 Cursor keyValueDataCursor = 296 readerDB.query( 297 MeasurementTables.KeyValueDataContract.TABLE, 298 new String[] { 299 MeasurementTables.KeyValueDataContract.KEY, 300 MeasurementTables.KeyValueDataContract.VALUE, 301 MeasurementTables.KeyValueDataContract.DATA_TYPE 302 }, 303 null, 304 null, 305 null, 306 null, 307 MeasurementTables.KeyValueDataContract.KEY); 308 while (keyValueDataCursor.moveToNext()) { 309 KeyValueData.Builder builder = new KeyValueData.Builder(); 310 builder.setKey(keyValueDataCursor.getString(0)); 311 builder.setValue(keyValueDataCursor.getString(1)); 312 builder.setDataType(KeyValueData.DataType.valueOf(keyValueDataCursor.getString(2))); 313 mKeyValueDataList.add(builder.build()); 314 } 315 keyValueDataCursor.close(); 316 } 317 sortAll()318 public void sortAll() { 319 mSourceList.sort( 320 Comparator.comparing(Source::getEventTime) 321 .thenComparing(Source::getPriority)); 322 323 mSourceDestinationList.sort( 324 Comparator.comparing(SourceDestination::getSourceId) 325 .thenComparing(SourceDestination::getDestinationType) 326 .thenComparing(SourceDestination::getDestination)); 327 328 mTriggerList.sort(Comparator.comparing(Trigger::getTriggerTime)); 329 330 mEventReportList.sort( 331 Comparator.comparing(EventReport::getReportTime) 332 .thenComparing(EventReport::getTriggerTime)); 333 334 mAttrRateLimitList.sort( 335 Comparator.comparing(Attribution::getScope) 336 .thenComparing(Attribution::getTriggerId)); 337 338 mAggregateEncryptionKeyList.sort( 339 Comparator.comparing(AggregateEncryptionKey::getKeyId)); 340 341 mAggregateReportList.sort( 342 Comparator.comparing(AggregateReport::getScheduledReportTime) 343 .thenComparing(AggregateReport::getSourceRegistrationTime)); 344 345 mDebugReportList.sort(Comparator.comparing(DebugReport::getId)); 346 347 mAsyncRegistrationList.sort(Comparator.comparing(AsyncRegistration::getRequestTime)); 348 349 mKeyValueDataList.sort(Comparator.comparing(KeyValueData::getKey)); 350 } 351 getAggregateEncryptionKeyList()352 public List<AggregateEncryptionKey> getAggregateEncryptionKeyList() { 353 return mAggregateEncryptionKeyList; 354 } 355 getSourceFrom(JSONObject sJSON)356 private Source getSourceFrom(JSONObject sJSON) throws JSONException { 357 Source.Builder builder = 358 new Source.Builder() 359 .setId(sJSON.getString("id")) 360 .setEventId(new UnsignedLong(sJSON.getString("eventId"))) 361 .setSourceType( 362 Source.SourceType.valueOf( 363 sJSON.getString("sourceType").toUpperCase(Locale.ENGLISH))) 364 .setPublisher(Uri.parse(sJSON.getString("publisher"))) 365 .setPublisherType(sJSON.optInt("publisherType")) 366 .setAggregateSource(sJSON.optString("aggregationKeys", null)) 367 .setAggregateContributions(sJSON.optInt("aggregateContributions")) 368 .setEnrollmentId(sJSON.getString("enrollmentId")) 369 .setEventTime(sJSON.getLong("eventTime")) 370 .setExpiryTime(sJSON.getLong("expiryTime")) 371 .setEventReportWindow(sJSON.getLong("eventReportWindow")) 372 .setAggregatableReportWindow(sJSON.optLong("aggregatableReportWindow")) 373 .setPriority(sJSON.getLong("priority")) 374 .setStatus(sJSON.getInt("status")) 375 .setRegistrant(Uri.parse(sJSON.getString("registrant"))) 376 .setInstallAttributionWindow( 377 sJSON.optLong( 378 "installAttributionWindow", TimeUnit.DAYS.toMillis(30))) 379 .setInstallCooldownWindow(sJSON.optLong("installCooldownWindow", 0)) 380 .setInstallAttributed(sJSON.optBoolean("installAttributed", false)) 381 .setAttributionMode( 382 sJSON.optInt("attribution_mode", Source.AttributionMode.TRUTHFULLY)) 383 .setFilterDataString(sJSON.optString("filterData", null)) 384 .setRegistrationOrigin(getRegistrationOrigin(sJSON)) 385 .setTriggerSpecsString(sJSON.optString("triggerSpecs", null)) 386 .setEventAttributionStatus(sJSON.optString("eventAttributionStatus", null)); 387 388 if (sJSON.opt("aggregateReportDedupKeys") != null) { 389 builder.setAggregateReportDedupKeys( 390 commaSeparatedUnsignedStringsToList( 391 sJSON.getString("aggregateReportDedupKeys"))); 392 } 393 394 return builder.build(); 395 } 396 commaSeparatedUnsignedStringsToList(String values)397 private static List<UnsignedLong> commaSeparatedUnsignedStringsToList(String values) { 398 if (values == null) { 399 return Collections.emptyList(); 400 } 401 return Arrays.stream(values.split(",")) 402 .map(String::trim) 403 .filter(not(String::isEmpty)) 404 .map(UnsignedLong::new) 405 .collect(Collectors.toList()); 406 } 407 getSourceDestinationFrom(JSONObject sdJSON)408 private SourceDestination getSourceDestinationFrom(JSONObject sdJSON) throws JSONException { 409 return new SourceDestination.Builder() 410 .setSourceId(sdJSON.getString("sourceId")) 411 .setDestination(sdJSON.getString("destination")) 412 .setDestinationType(sdJSON.getInt("destinationType")) 413 .build(); 414 } 415 getTriggerFrom(JSONObject tJSON)416 private Trigger getTriggerFrom(JSONObject tJSON) throws JSONException { 417 return new Trigger.Builder() 418 .setId(tJSON.getString("id")) 419 .setAttributionDestination(Uri.parse(tJSON.getString("attributionDestination"))) 420 .setDestinationType(tJSON.optInt("destinationType")) 421 .setEnrollmentId(tJSON.getString("enrollmentId")) 422 .setEventTriggers(tJSON.getString("eventTriggers")) 423 .setAggregateTriggerData(tJSON.optString("aggregatableTriggerData", null)) 424 .setAggregateValuesString(tJSON.optString("aggregatableValues", null)) 425 .setTriggerTime(tJSON.getLong("triggerTime")) 426 .setStatus(tJSON.getInt("status")) 427 .setRegistrant(Uri.parse(tJSON.getString("registrant"))) 428 .setFilters(tJSON.optString("filters", null)) 429 .setNotFilters(tJSON.optString("not_filters", null)) 430 .setRegistrationOrigin(getRegistrationOrigin(tJSON)) 431 .setAggregatableSourceRegistrationTimeConfig( 432 Trigger.SourceRegistrationTimeConfig.valueOf( 433 tJSON.getString("aggregatableSourceRegistrationTimeConfig"))) 434 .setTriggerContextId(tJSON.optString("triggerContextId", null)) 435 .build(); 436 } 437 getEventReportFrom(JSONObject rJSON)438 private EventReport getEventReportFrom(JSONObject rJSON) throws JSONException { 439 return new EventReport.Builder() 440 .setId(rJSON.getString("id")) 441 .setSourceEventId(new UnsignedLong(rJSON.getString("sourceEventId"))) 442 .setAttributionDestinations( 443 SqliteObjectMapper.destinationsStringToList( 444 rJSON.getString("attributionDestination"))) 445 .setEnrollmentId(rJSON.getString("enrollmentId")) 446 .setTriggerData(new UnsignedLong(rJSON.getString("triggerData"))) 447 .setTriggerTime(rJSON.getLong("triggerTime")) 448 .setReportTime(rJSON.getLong("reportTime")) 449 .setTriggerPriority(rJSON.getLong("triggerPriority")) 450 .setStatus(rJSON.getInt("status")) 451 .setRandomizedTriggerRate(rJSON.optDouble("randomizedTriggerRate", 0.0D)) 452 .setSourceType( 453 Source.SourceType.valueOf( 454 rJSON.getString("sourceType").toUpperCase(Locale.ENGLISH))) 455 .setSourceId(rJSON.optString("sourceId", null)) 456 .setTriggerId(rJSON.optString("triggerId", null)) 457 .setRegistrationOrigin(getRegistrationOrigin(rJSON)) 458 .build(); 459 } 460 getAttributionFrom(JSONObject attrJSON)461 private Attribution getAttributionFrom(JSONObject attrJSON) 462 throws JSONException { 463 return new Attribution.Builder() 464 .setId(attrJSON.getString("id")) 465 .setScope(attrJSON.optInt("scope", 0)) 466 .setSourceSite(attrJSON.getString("sourceSite")) 467 .setSourceOrigin(attrJSON.getString("sourceOrigin")) 468 .setDestinationSite(attrJSON.getString("destinationSite")) 469 .setDestinationOrigin(attrJSON.getString("destinationOrigin")) 470 .setEnrollmentId(attrJSON.getString("enrollmentId")) 471 .setTriggerTime(attrJSON.getLong("triggerTime")) 472 .setRegistrant(attrJSON.getString("registrant")) 473 .setSourceId(attrJSON.optString("sourceId", null)) 474 .setTriggerId(attrJSON.optString("triggerId", null)) 475 .build(); 476 } 477 getAggregateEncryptionKeyFrom(JSONObject keyJSON)478 private AggregateEncryptionKey getAggregateEncryptionKeyFrom(JSONObject keyJSON) 479 throws JSONException { 480 return new AggregateEncryptionKey.Builder() 481 .setId(keyJSON.getString("id")) 482 .setKeyId(keyJSON.getString("keyId")) 483 .setPublicKey(keyJSON.getString("publicKey")) 484 .setExpiry(keyJSON.getLong("expiry")) 485 .setAggregationCoordinatorOrigin( 486 WebUtil.validUri(keyJSON.getString("aggregation_coordinator_origin"))) 487 .build(); 488 } 489 getAttributionFrom(Cursor cursor)490 private Attribution getAttributionFrom(Cursor cursor) { 491 return new Attribution.Builder() 492 .setId( 493 cursor.getString( 494 cursor.getColumnIndex(MeasurementTables.AttributionContract.ID))) 495 .setSourceSite( 496 cursor.getString( 497 cursor.getColumnIndex( 498 MeasurementTables.AttributionContract.SOURCE_SITE))) 499 .setScope( 500 cursor.getInt( 501 cursor.getColumnIndex( 502 MeasurementTables.AttributionContract.SCOPE))) 503 .setSourceOrigin( 504 cursor.getString( 505 cursor.getColumnIndex( 506 MeasurementTables.AttributionContract.SOURCE_ORIGIN))) 507 .setDestinationSite( 508 cursor.getString( 509 cursor.getColumnIndex( 510 MeasurementTables.AttributionContract.DESTINATION_SITE))) 511 .setDestinationOrigin( 512 cursor.getString( 513 cursor.getColumnIndex( 514 MeasurementTables.AttributionContract.DESTINATION_ORIGIN))) 515 .setEnrollmentId( 516 cursor.getString( 517 cursor.getColumnIndex( 518 MeasurementTables.AttributionContract.ENROLLMENT_ID))) 519 .setTriggerTime( 520 cursor.getLong( 521 cursor.getColumnIndex( 522 MeasurementTables.AttributionContract.TRIGGER_TIME))) 523 .setRegistrant( 524 cursor.getString( 525 cursor.getColumnIndex( 526 MeasurementTables.AttributionContract.REGISTRANT))) 527 .setSourceId( 528 cursor.getString( 529 cursor.getColumnIndex( 530 MeasurementTables.AttributionContract.SOURCE_ID))) 531 .setTriggerId( 532 cursor.getString( 533 cursor.getColumnIndex( 534 MeasurementTables.AttributionContract.TRIGGER_ID))) 535 .build(); 536 } 537 getSourceDestinationFrom(Cursor cursor)538 private SourceDestination getSourceDestinationFrom(Cursor cursor) { 539 return new SourceDestination.Builder() 540 .setSourceId( 541 cursor.getString( 542 cursor.getColumnIndex( 543 MeasurementTables.SourceDestination.SOURCE_ID))) 544 .setDestination( 545 cursor.getString( 546 cursor.getColumnIndex( 547 MeasurementTables.SourceDestination.DESTINATION))) 548 .setDestinationType( 549 cursor.getInt( 550 cursor.getColumnIndex( 551 MeasurementTables.SourceDestination.DESTINATION_TYPE))) 552 .build(); 553 } 554 getAggregateReportFrom(JSONObject rJSON)555 private AggregateReport getAggregateReportFrom(JSONObject rJSON) 556 throws JSONException { 557 AggregateReport.Builder builder = 558 new AggregateReport.Builder() 559 .setId(rJSON.getString("id")) 560 .setPublisher(Uri.parse(rJSON.getString("publisher"))) 561 .setAttributionDestination( 562 Uri.parse(rJSON.getString("attributionDestination"))) 563 .setSourceRegistrationTime(rJSON.getLong("sourceRegistrationTime")) 564 .setScheduledReportTime(rJSON.getLong("scheduledReportTime")) 565 .setEnrollmentId(rJSON.getString("enrollmentId")) 566 .setDebugCleartextPayload(rJSON.getString("debugCleartextPayload")) 567 .setStatus(rJSON.getInt("status")) 568 .setApiVersion(rJSON.optString("apiVersion", null)) 569 .setSourceId(rJSON.optString("sourceId", null)) 570 .setTriggerId(rJSON.optString("triggerId", null)) 571 .setRegistrationOrigin(getRegistrationOrigin(rJSON)) 572 .setAggregationCoordinatorOrigin( 573 Uri.parse( 574 rJSON.optString( 575 "aggregation_coordinator_origin", 576 "https://test.test"))) 577 .setIsFakeReport(rJSON.optBoolean("isFakeReport", false)) 578 .setApi(rJSON.optString("api", null)) 579 .setTriggerContextId(rJSON.optString("triggerContextId", null)); 580 581 if (rJSON.opt("dedupKey") != null) { 582 builder.setDedupKey(new UnsignedLong(rJSON.getString("dedupKey"))); 583 } 584 585 return builder.build(); 586 } 587 getDebugReportFrom(JSONObject rJSON)588 private DebugReport getDebugReportFrom(JSONObject rJSON) throws JSONException { 589 DebugReport.Builder builder = 590 new DebugReport.Builder() 591 .setId(rJSON.getString("id")) 592 .setType(rJSON.getString("type")) 593 .setBody(rJSON.getString("body")); 594 if (rJSON.has("registrant")) { 595 builder.setRegistrant(Uri.parse(rJSON.getString("registrant"))); 596 } 597 return builder.build(); 598 } 599 getAsyncRegistrationFrom(JSONObject aJSON)600 private AsyncRegistration getAsyncRegistrationFrom(JSONObject aJSON) throws JSONException { 601 return new AsyncRegistration.Builder() 602 .setRegistrationId(aJSON.getString("registrationId")) 603 .setRegistrationUri(Uri.parse(aJSON.getString("registrationUri"))) 604 .setTopOrigin(Uri.parse(aJSON.getString("topOrigin"))) 605 .setRegistrant(Uri.parse(aJSON.getString("registrant"))) 606 .setOsDestination(Uri.parse(aJSON.getString("osDestination"))) 607 .setRequestTime(aJSON.getLong("requestTime")) 608 .setAdIdPermission(aJSON.getBoolean("adIdPermission")) 609 .setId(aJSON.getString("id")) 610 .setType( 611 AsyncRegistration.RegistrationType.values()[ 612 aJSON.getInt("registrationType")]) 613 .setPlatformAdId(aJSON.getString("platformAdId")) 614 .setDebugKeyAllowed(aJSON.getBoolean("debugKeyAllowed")) 615 .setRetryCount(aJSON.getInt("retryCount")) 616 .setVerifiedDestination(Uri.parse(aJSON.getString("verifiedDestination"))) 617 .setWebDestination(Uri.parse(aJSON.getString("webDestination"))) 618 .setSourceType(Source.SourceType.values()[aJSON.getInt("sourceType")]) 619 .setRedirectBehavior( 620 AsyncRedirect.RedirectBehavior.valueOf(aJSON.getString("redirectBehavior"))) 621 .build(); 622 } 623 getKeyValueDataFrom(JSONObject kJSON)624 private KeyValueData getKeyValueDataFrom(JSONObject kJSON) throws JSONException { 625 return new KeyValueData.Builder() 626 .setKey(kJSON.getString("key")) 627 .setDataType(KeyValueData.DataType.valueOf(kJSON.getString("dataType"))) 628 .setValue(kJSON.getString("value")) 629 .build(); 630 } 631 getRegistrationOrigin(JSONObject json)632 private Uri getRegistrationOrigin(JSONObject json) throws JSONException { 633 return Uri.parse( 634 json.isNull("registration_origin") 635 ? DEFAULT_REGISTRATION_URI 636 : json.get("registration_origin").toString()); 637 } 638 } 639