use of com.redhat.cloud.notifications.models.BehaviorGroup in project notifications-backend by RedHatInsights.
the class CrudTestHelpers method buildDefaultBehaviorGroup.
private static BehaviorGroup buildDefaultBehaviorGroup(String displayName, String bundleId) {
BehaviorGroup bg = new BehaviorGroup();
bg.setDisplayName(displayName);
bg.setBundleId(UUID.fromString(bundleId));
return bg;
}
use of com.redhat.cloud.notifications.models.BehaviorGroup in project notifications-backend by RedHatInsights.
the class LifecycleITest method addEventTypeBehavior.
@Transactional
void addEventTypeBehavior(UUID eventTypeId, UUID behaviorGroupId) {
EventType eventType = entityManager.find(EventType.class, eventTypeId);
BehaviorGroup behaviorGroup = entityManager.find(BehaviorGroup.class, behaviorGroupId);
EventTypeBehavior behavior = new EventTypeBehavior();
behavior.setId(new EventTypeBehaviorId());
behavior.setEventType(eventType);
behavior.setBehaviorGroup(behaviorGroup);
entityManager.persist(behavior);
}
use of com.redhat.cloud.notifications.models.BehaviorGroup in project notifications-backend by RedHatInsights.
the class LifecycleITest method test.
@Test
void test() {
final String accountId = "tenant";
final String orgId = "org-id";
final String username = "user";
setupEmailMock(accountId, orgId, username);
// First, we need a bundle, an app and an event type. Let's create them!
Bundle bundle = resourceHelpers.createBundle(BUNDLE_NAME);
Application app = resourceHelpers.createApp(bundle.getId(), APP_NAME);
EventType eventType = resourceHelpers.createEventType(app.getId(), EVENT_TYPE_NAME);
// We also need behavior groups.
BehaviorGroup behaviorGroup1 = createBehaviorGroup(accountId, bundle);
BehaviorGroup behaviorGroup2 = createBehaviorGroup(accountId, bundle);
BehaviorGroup defaultBehaviorGroup = createBehaviorGroup(null, bundle);
// We need actions for our behavior groups.
Endpoint endpoint1 = createWebhookEndpoint(accountId, SECRET_TOKEN);
Endpoint endpoint2 = createWebhookEndpoint(accountId, SECRET_TOKEN);
Endpoint endpoint3 = createWebhookEndpoint(accountId, "wrong-secret-token");
// We'll start with a first behavior group actions configuration. This will slightly change later in the test.
addBehaviorGroupAction(behaviorGroup1.getId(), endpoint1.getId());
addBehaviorGroupAction(behaviorGroup1.getId(), endpoint2.getId());
addBehaviorGroupAction(behaviorGroup2.getId(), endpoint3.getId());
// Adding an email endpoint to the default behavior group
addDefaultBehaviorGroupAction(defaultBehaviorGroup);
// Let's push a first message! It should not trigger any webhook call since we didn't link the event type with any behavior group.
pushMessage(0, 0, 0, 0);
// Now we'll link the event type with one behavior group.
addEventTypeBehavior(eventType.getId(), behaviorGroup1.getId());
// Get the account canonical email endpoint
Endpoint emailEndpoint = statelessSessionFactory.withSession(statelessSession -> {
return getAccountCanonicalEmailEndpoint(accountId);
});
// Pushing a new message should trigger two webhook calls.
pushMessage(2, 0, 0, 0);
// Let's check the notifications history.
retry(() -> checkEndpointHistory(endpoint1, 1, true));
retry(() -> checkEndpointHistory(endpoint2, 1, true));
retry(() -> checkEndpointHistory(emailEndpoint, 0, true));
// We'll link the event type with the default behavior group
addEventTypeBehavior(eventType.getId(), defaultBehaviorGroup.getId());
// We'll link an additional behavior group to the event type.
addEventTypeBehavior(eventType.getId(), behaviorGroup2.getId());
// Pushing a new message should trigger three webhook calls and 1 emails - email is not sent as the user is not subscribed
pushMessage(3, 1, 0, 0);
// Let's check the notifications history again.
retry(() -> checkEndpointHistory(endpoint1, 2, true));
retry(() -> checkEndpointHistory(endpoint2, 2, true));
retry(() -> checkEndpointHistory(endpoint3, 1, false));
retry(() -> checkEndpointHistory(emailEndpoint, 0, true));
// Lets subscribe the user to the email preferences
subscribeUserPreferences(accountId, username, app.getId());
// Pushing a new message should trigger three webhook calls and 1 email
pushMessage(3, 1, 1, 0);
// Let's check the notifications history again.
retry(() -> checkEndpointHistory(endpoint1, 3, true));
retry(() -> checkEndpointHistory(endpoint2, 3, true));
retry(() -> checkEndpointHistory(endpoint3, 2, false));
retry(() -> checkEndpointHistory(emailEndpoint, 1, true));
/*
* Let's change the behavior group actions configuration by adding an action to the second behavior group.
* Endpoint 2 is now an action for both behavior groups, but it should not be notified twice on each message because we don't want duplicate notifications.
*/
addBehaviorGroupAction(behaviorGroup2.getId(), endpoint2.getId());
// Pushing a new message should trigger three webhook calls.
pushMessage(3, 1, 1, 0);
// Let's check the notifications history again.
retry(() -> checkEndpointHistory(endpoint1, 4, true));
retry(() -> checkEndpointHistory(endpoint2, 4, true));
retry(() -> checkEndpointHistory(endpoint3, 3, false));
retry(() -> checkEndpointHistory(emailEndpoint, 2, true));
/*
* What happens if we unlink the event type from the behavior groups?
* Pushing a new message should not trigger any webhook call.
*/
// Unlinking user behavior group
clearEventTypeBehaviors(eventType);
pushMessage(0, 0, 0, 0);
// The notifications history should be exactly the same than last time.
retry(() -> checkEndpointHistory(endpoint1, 4, true));
retry(() -> checkEndpointHistory(endpoint2, 4, true));
retry(() -> checkEndpointHistory(endpoint3, 3, false));
retry(() -> checkEndpointHistory(emailEndpoint, 2, true));
// Linking the default behavior group again
addEventTypeBehavior(eventType.getId(), defaultBehaviorGroup.getId());
pushMessage(0, 1, 1, 0);
// Deleting the default behavior group should unlink it
deleteBehaviorGroup(defaultBehaviorGroup);
pushMessage(0, 0, 0, 0);
// We'll finish with a bundle removal.
deleteBundle(bundle);
}
Aggregations