use of com.redhat.cloud.notifications.models.Application in project notifications-backend by RedHatInsights.
the class UserConfigResource method getSettingsValueForUser.
/**
* Pulls the user settings values of an user across all the know applications of a bundle
*/
private SettingsValues getSettingsValueForUser(String account, String username, String bundleName) {
if (bundleName == null || bundleName.equals("")) {
throw new BadRequestException("bundleName must have a value");
}
SettingsValues settingsValues = new SettingsValues();
Bundle bundle = bundleRepository.getBundle(bundleName);
if (bundle == null) {
throw new BadRequestException("Unknown bundleName: " + bundleName);
} else {
BundleSettingsValue bundleSettingsValue = new BundleSettingsValue();
bundleSettingsValue.displayName = bundle.getDisplayName();
settingsValues.bundles.put(bundle.getName(), bundleSettingsValue);
for (Application application : applicationRepository.getApplications(bundle.getName())) {
ApplicationSettingsValue applicationSettingsValue = new ApplicationSettingsValue();
applicationSettingsValue.displayName = application.getDisplayName();
settingsValues.bundles.get(bundle.getName()).applications.put(application.getName(), applicationSettingsValue);
for (EmailSubscriptionType emailSubscriptionType : EmailSubscriptionType.values()) {
// TODO NOTIF-450 How do we deal with a failure here? What kind of response should be sent to the UI when the engine is down?
boolean supported = templateEngineClient.isSubscriptionTypeSupported(bundle.getName(), application.getName(), emailSubscriptionType);
if (supported) {
settingsValues.bundles.get(bundle.getName()).applications.get(application.getName()).notifications.put(emailSubscriptionType, false);
}
}
}
List<EmailSubscription> emailSubscriptions = emailSubscriptionRepository.getEmailSubscriptionsForUser(account, username);
for (EmailSubscription emailSubscription : emailSubscriptions) {
if (settingsValues.bundles.containsKey(emailSubscription.getApplication().getBundle().getName())) {
BundleSettingsValue bundleSettings = settingsValues.bundles.get(emailSubscription.getApplication().getBundle().getName());
if (bundleSettings.applications.containsKey(emailSubscription.getApplication().getName())) {
ApplicationSettingsValue applicationSettingsValue = bundleSettings.applications.get(emailSubscription.getApplication().getName());
if (applicationSettingsValue.notifications.containsKey(emailSubscription.getType())) {
bundleSettings.applications.get(emailSubscription.getApplication().getName()).notifications.put(emailSubscription.getType(), true);
}
}
}
}
return settingsValues;
}
}
use of com.redhat.cloud.notifications.models.Application in project notifications-backend by RedHatInsights.
the class InternalPermissionResource method addAccess.
@POST
@Path("/")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public InternalRoleAccess addAccess(@Valid AddAccessRequest addAccessRequest) {
InternalRoleAccess access = new InternalRoleAccess();
Application application = applicationRepository.getApplication(addAccessRequest.applicationId);
access.setApplicationId(addAccessRequest.applicationId);
access.setRole(addAccessRequest.role);
access.setApplication(application);
return internalRoleAccessRepository.addAccess(access);
}
use of com.redhat.cloud.notifications.models.Application in project notifications-backend by RedHatInsights.
the class CrudTestHelpers method updateApp.
public static void updateApp(Header identity, String bundleId, String appId, String name, String displayName, int expectedStatusCode) {
Application app = buildApp(bundleId, name, displayName);
updateApp(identity, appId, app, expectedStatusCode);
}
use of com.redhat.cloud.notifications.models.Application in project notifications-backend by RedHatInsights.
the class ApplicationRepository method getEventTypes.
public List<EventType> getEventTypes(UUID appId) {
String query = "FROM EventType WHERE application.id = :appId";
Application app = entityManager.find(Application.class, appId);
if (app == null) {
throw new NotFoundException();
} else {
List<EventType> eventTypes = entityManager.createQuery(query, EventType.class).setParameter("appId", appId).getResultList();
for (EventType eventType : eventTypes) {
eventType.filterOutApplication();
}
return eventTypes;
}
}
use of com.redhat.cloud.notifications.models.Application 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