|
1 | 1 | /**
|
2 | 2 | *
|
3 |
| - * Copyright 2020, Optimizely and contributors |
| 3 | + * Copyright 2021, Optimizely and contributors |
4 | 4 | *
|
5 | 5 | * Licensed under the Apache License, Version 2.0 (the "License");
|
6 | 6 | * you may not use this file except in compliance with the License.
|
@@ -495,6 +495,112 @@ public void decide_doNotSendEvent_withOption() {
|
495 | 495 | OptimizelyDecision decision = user.decide(flagKey, Arrays.asList(OptimizelyDecideOption.DISABLE_DECISION_EVENT));
|
496 | 496 |
|
497 | 497 | assertEquals(decision.getVariationKey(), "variation_with_traffic");
|
| 498 | + |
| 499 | + // impression event not expected here |
| 500 | + } |
| 501 | + |
| 502 | + @Test |
| 503 | + public void decide_sendEvent_featureTest_withSendFlagDecisionsOn() { |
| 504 | + optimizely = new Optimizely.Builder() |
| 505 | + .withDatafile(datafile) |
| 506 | + .withEventProcessor(new ForwardingEventProcessor(eventHandler, null)) |
| 507 | + .build(); |
| 508 | + |
| 509 | + Map<String, Object> attributes = Collections.singletonMap("gender", "f"); |
| 510 | + OptimizelyUserContext user = optimizely.createUserContext(userId, attributes); |
| 511 | + |
| 512 | + optimizely.addDecisionNotificationHandler( |
| 513 | + decisionNotification -> { |
| 514 | + Assert.assertEquals(decisionNotification.getDecisionInfo().get(DECISION_EVENT_DISPATCHED), true); |
| 515 | + isListenerCalled = true; |
| 516 | + }); |
| 517 | + |
| 518 | + String flagKey = "feature_2"; |
| 519 | + String experimentId = "10420810910"; |
| 520 | + String variationId = "10418551353"; |
| 521 | + isListenerCalled = false; |
| 522 | + user.decide(flagKey); |
| 523 | + assertTrue(isListenerCalled); |
| 524 | + |
| 525 | + eventHandler.expectImpression(experimentId, variationId, userId, attributes); |
| 526 | + } |
| 527 | + |
| 528 | + @Test |
| 529 | + public void decide_sendEvent_rollout_withSendFlagDecisionsOn() { |
| 530 | + optimizely = new Optimizely.Builder() |
| 531 | + .withDatafile(datafile) |
| 532 | + .withEventProcessor(new ForwardingEventProcessor(eventHandler, null)) |
| 533 | + .build(); |
| 534 | + |
| 535 | + Map<String, Object> attributes = Collections.singletonMap("gender", "f"); |
| 536 | + OptimizelyUserContext user = optimizely.createUserContext(userId, attributes); |
| 537 | + |
| 538 | + optimizely.addDecisionNotificationHandler( |
| 539 | + decisionNotification -> { |
| 540 | + Assert.assertEquals(decisionNotification.getDecisionInfo().get(DECISION_EVENT_DISPATCHED), true); |
| 541 | + isListenerCalled = true; |
| 542 | + }); |
| 543 | + |
| 544 | + String flagKey = "feature_3"; |
| 545 | + String experimentId = null; |
| 546 | + String variationId = null; |
| 547 | + isListenerCalled = false; |
| 548 | + user.decide(flagKey); |
| 549 | + assertTrue(isListenerCalled); |
| 550 | + |
| 551 | + eventHandler.expectImpression(null, "", userId, attributes); |
| 552 | + } |
| 553 | + |
| 554 | + @Test |
| 555 | + public void decide_sendEvent_featureTest_withSendFlagDecisionsOff() { |
| 556 | + String datafileWithSendFlagDecisionsOff = datafile.replace("\"sendFlagDecisions\": true", "\"sendFlagDecisions\": false"); |
| 557 | + optimizely = new Optimizely.Builder() |
| 558 | + .withDatafile(datafileWithSendFlagDecisionsOff) |
| 559 | + .withEventProcessor(new ForwardingEventProcessor(eventHandler, null)) |
| 560 | + .build(); |
| 561 | + |
| 562 | + Map<String, Object> attributes = Collections.singletonMap("gender", "f"); |
| 563 | + OptimizelyUserContext user = optimizely.createUserContext(userId, attributes); |
| 564 | + |
| 565 | + optimizely.addDecisionNotificationHandler( |
| 566 | + decisionNotification -> { |
| 567 | + Assert.assertEquals(decisionNotification.getDecisionInfo().get(DECISION_EVENT_DISPATCHED), true); |
| 568 | + isListenerCalled = true; |
| 569 | + }); |
| 570 | + |
| 571 | + String flagKey = "feature_2"; |
| 572 | + String experimentId = "10420810910"; |
| 573 | + String variationId = "10418551353"; |
| 574 | + isListenerCalled = false; |
| 575 | + user.decide(flagKey); |
| 576 | + assertTrue(isListenerCalled); |
| 577 | + |
| 578 | + eventHandler.expectImpression(experimentId, variationId, userId, attributes); |
| 579 | + } |
| 580 | + |
| 581 | + @Test |
| 582 | + public void decide_sendEvent_rollout_withSendFlagDecisionsOff() { |
| 583 | + String datafileWithSendFlagDecisionsOff = datafile.replace("\"sendFlagDecisions\": true", "\"sendFlagDecisions\": false"); |
| 584 | + optimizely = new Optimizely.Builder() |
| 585 | + .withDatafile(datafileWithSendFlagDecisionsOff) |
| 586 | + .withEventProcessor(new ForwardingEventProcessor(eventHandler, null)) |
| 587 | + .build(); |
| 588 | + |
| 589 | + Map<String, Object> attributes = Collections.singletonMap("gender", "f"); |
| 590 | + OptimizelyUserContext user = optimizely.createUserContext(userId, attributes); |
| 591 | + |
| 592 | + optimizely.addDecisionNotificationHandler( |
| 593 | + decisionNotification -> { |
| 594 | + Assert.assertEquals(decisionNotification.getDecisionInfo().get(DECISION_EVENT_DISPATCHED), false); |
| 595 | + isListenerCalled = true; |
| 596 | + }); |
| 597 | + |
| 598 | + String flagKey = "feature_3"; |
| 599 | + isListenerCalled = false; |
| 600 | + user.decide(flagKey); |
| 601 | + assertTrue(isListenerCalled); |
| 602 | + |
| 603 | + // impression event not expected here |
498 | 604 | }
|
499 | 605 |
|
500 | 606 | // notifications
|
|
0 commit comments