Search in sources :

Example 21 with Bundle

use of com.redhat.cloud.notifications.models.Bundle in project notifications-backend by RedHatInsights.

the class BehaviorGroupRepository method findByBundleId.

public List<BehaviorGroup> findByBundleId(String accountId, UUID bundleId) {
    Bundle bundle = entityManager.find(Bundle.class, bundleId);
    if (bundle == null) {
        throw new NotFoundException("Bundle not found");
    }
    List<BehaviorGroup> behaviorGroups = entityManager.createNamedQuery("findByBundleId", BehaviorGroup.class).setParameter("accountId", accountId).setParameter("bundleId", bundleId).getResultList();
    for (BehaviorGroup behaviorGroup : behaviorGroups) {
        behaviorGroup.filterOutBundle();
    }
    return behaviorGroups;
}
Also used : Bundle(com.redhat.cloud.notifications.models.Bundle) BehaviorGroup(com.redhat.cloud.notifications.models.BehaviorGroup) NotFoundException(javax.ws.rs.NotFoundException)

Example 22 with Bundle

use of com.redhat.cloud.notifications.models.Bundle in project notifications-backend by RedHatInsights.

the class BundleRepository method getApplications.

public List<Application> getApplications(UUID id) {
    String query = "FROM Application WHERE bundle.id = :id";
    Bundle bundle = entityManager.find(Bundle.class, id);
    if (bundle == null) {
        throw new NotFoundException();
    } else {
        return entityManager.createQuery(query, Application.class).setParameter("id", id).getResultList();
    }
}
Also used : Bundle(com.redhat.cloud.notifications.models.Bundle) NotFoundException(javax.ws.rs.NotFoundException)

Example 23 with Bundle

use of com.redhat.cloud.notifications.models.Bundle in project notifications-backend by RedHatInsights.

the class ApplicationRepository method createApp.

@Transactional
public Application createApp(Application app) {
    Bundle bundle = entityManager.find(Bundle.class, app.getBundleId());
    if (bundle == null) {
        throw new NotFoundException();
    } else {
        app.setBundle(bundle);
        entityManager.persist(app);
        return app;
    }
}
Also used : Bundle(com.redhat.cloud.notifications.models.Bundle) NotFoundException(javax.ws.rs.NotFoundException) Transactional(javax.transaction.Transactional)

Example 24 with Bundle

use of com.redhat.cloud.notifications.models.Bundle 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);
}
Also used : Endpoint(com.redhat.cloud.notifications.models.Endpoint) EventType(com.redhat.cloud.notifications.models.EventType) Bundle(com.redhat.cloud.notifications.models.Bundle) BehaviorGroup(com.redhat.cloud.notifications.models.BehaviorGroup) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Application(com.redhat.cloud.notifications.models.Application) Test(org.junit.jupiter.api.Test) QuarkusTest(io.quarkus.test.junit.QuarkusTest)

Example 25 with Bundle

use of com.redhat.cloud.notifications.models.Bundle in project notifications-backend by RedHatInsights.

the class EmailTemplateMigrationServiceTest method testMigration.

@Test
void testMigration() {
    /*
         * Bundle: rhel
         */
    Bundle rhel = findBundle("rhel");
    // App: advisor
    Application advisor = resourceHelpers.createApp(rhel.getId(), "advisor");
    EventType newRecommendation = resourceHelpers.createEventType(advisor.getId(), "new-recommendation");
    EventType resolvedRecommendation = resourceHelpers.createEventType(advisor.getId(), "resolved-recommendation");
    EventType deactivatedRecommendation = resourceHelpers.createEventType(advisor.getId(), "deactivated-recommendation");
    // App: compliance
    Application compliance = resourceHelpers.createApp(rhel.getId(), "compliance");
    EventType complianceBelowThreshold = resourceHelpers.createEventType(compliance.getId(), "compliance-below-threshold");
    EventType reportUploadFailed = resourceHelpers.createEventType(compliance.getId(), "report-upload-failed");
    // App: drift
    Application drift = resourceHelpers.createApp(rhel.getId(), "drift");
    EventType driftBaselineDetected = resourceHelpers.createEventType(drift.getId(), "drift-baseline-detected");
    // App: edge-management
    Application edgeManagement = resourceHelpers.createApp(rhel.getId(), "edge-management");
    EventType imageCreation = resourceHelpers.createEventType(edgeManagement.getId(), "image-creation");
    EventType updateDevices = resourceHelpers.createEventType(edgeManagement.getId(), "update-devices");
    // App: patch
    Application patch = resourceHelpers.createApp(rhel.getId(), "patch");
    EventType newAdvisories = resourceHelpers.createEventType(patch.getId(), "new-advisory");
    // App: policies
    Application policies = findApp("rhel", "policies");
    EventType policyTriggered = findEventType("rhel", "policies", "policy-triggered");
    // App: vulnerability
    Application vulnerability = resourceHelpers.createApp(rhel.getId(), "vulnerability");
    EventType newCveCvss = resourceHelpers.createEventType(vulnerability.getId(), "new-cve-cvss");
    EventType newCveSeverity = resourceHelpers.createEventType(vulnerability.getId(), "new-cve-severity");
    EventType newCveSecurityRule = resourceHelpers.createEventType(vulnerability.getId(), "new-cve-security-rule");
    EventType anyCveKnownExploit = resourceHelpers.createEventType(vulnerability.getId(), "any-cve-known-exploit");
    /*
         * Bundle: openshift
         */
    Bundle openshift = resourceHelpers.createBundle("openshift");
    // App: advisor
    Application advisorOpenshift = resourceHelpers.createApp(openshift.getId(), "advisor");
    EventType newRecommendationOpenshift = resourceHelpers.createEventType(advisorOpenshift.getId(), "new-recommendation");
    /*
         * Bundle: application-services
         */
    Bundle applicationServices = resourceHelpers.createBundle("application-services");
    // App: rhosak
    Application rhosak = resourceHelpers.createApp(applicationServices.getId(), "rhosak");
    EventType scheduledUpgrade = resourceHelpers.createEventType(rhosak.getId(), "scheduled-upgrade");
    EventType disruption = resourceHelpers.createEventType(rhosak.getId(), "disruption");
    EventType instanceCreated = resourceHelpers.createEventType(rhosak.getId(), "instance-created");
    EventType instanceDeleted = resourceHelpers.createEventType(rhosak.getId(), "instance-deleted");
    EventType actionRequired = resourceHelpers.createEventType(rhosak.getId(), "action-required");
    /*
         * Bundle: ansible
         */
    Bundle ansible = resourceHelpers.createBundle("ansible");
    // App: reports
    Application reports = resourceHelpers.createApp(ansible.getId(), "reports");
    EventType reportAvailable = resourceHelpers.createEventType(reports.getId(), "report-available");
    /*
         * Bundle: console
         */
    Bundle console = findBundle("console");
    // App: notifications
    Application notifications = findApp("console", "notifications");
    EventType failedIntegration = resourceHelpers.createEventType(notifications.getId(), "failed-integration");
    // App: sources
    Application sources = resourceHelpers.createApp(console.getId(), "sources");
    EventType availabilityStatus = resourceHelpers.createEventType(sources.getId(), "availability-status");
    // App: rbac
    Application rbac = resourceHelpers.createApp(console.getId(), "rbac");
    EventType rhNewRoleAvailable = resourceHelpers.createEventType(rbac.getId(), "rh-new-role-available");
    EventType rhPlatformDefaultRoleUpdated = resourceHelpers.createEventType(rbac.getId(), "rh-platform-default-role-updated");
    EventType rhNonPlatformDefaultRoleUpdated = resourceHelpers.createEventType(rbac.getId(), "rh-non-platform-default-role-updated");
    EventType customRoleCreated = resourceHelpers.createEventType(rbac.getId(), "custom-role-created");
    EventType customRoleUpdated = resourceHelpers.createEventType(rbac.getId(), "custom-role-updated");
    EventType customRoleDeleted = resourceHelpers.createEventType(rbac.getId(), "custom-role-deleted");
    EventType rhNewRoleAddedToDefaultAccess = resourceHelpers.createEventType(rbac.getId(), "rh-new-role-added-to-default-access");
    EventType rhRoleRemovedFromDefaultAccess = resourceHelpers.createEventType(rbac.getId(), "rh-role-removed-from-default-access");
    EventType customDefaultAccessUpdated = resourceHelpers.createEventType(rbac.getId(), "custom-default-access-updated");
    EventType groupCreated = resourceHelpers.createEventType(rbac.getId(), "group-created");
    EventType groupUpdated = resourceHelpers.createEventType(rbac.getId(), "group-updated");
    EventType groupDeleted = resourceHelpers.createEventType(rbac.getId(), "group-deleted");
    EventType platformDefaultGroupTurnedIntoCustom = resourceHelpers.createEventType(rbac.getId(), "platform-default-group-turned-into-custom");
    clearDbTemplates();
    given().basePath(API_INTERNAL).when().put("/template-engine/migrate").then().statusCode(200).contentType(JSON);
    statelessSessionFactory.withSession(statelessSession -> {
        /*
             * Bundle: rhel
             */
        // App: advisor
        findAndCompileInstantEmailTemplate(newRecommendation.getId());
        findAndCompileInstantEmailTemplate(resolvedRecommendation.getId());
        findAndCompileInstantEmailTemplate(deactivatedRecommendation.getId());
        assertTrue(templateRepository.findAggregationEmailTemplate(rhel.getName(), advisor.getName(), DAILY).isEmpty());
        // App: compliance
        findAndCompileInstantEmailTemplate(complianceBelowThreshold.getId());
        findAndCompileInstantEmailTemplate(reportUploadFailed.getId());
        findAndCompileAggregationEmailTemplate(rhel.getName(), compliance.getName(), DAILY);
        // App: drift
        findAndCompileInstantEmailTemplate(driftBaselineDetected.getId());
        findAndCompileAggregationEmailTemplate(rhel.getName(), drift.getName(), DAILY);
        // App: edge-management
        findAndCompileInstantEmailTemplate(imageCreation.getId());
        findAndCompileInstantEmailTemplate(updateDevices.getId());
        // App: patch
        findAndCompileInstantEmailTemplate(newAdvisories.getId());
        findAndCompileAggregationEmailTemplate(rhel.getName(), patch.getName(), DAILY);
        // App: policies
        findAndCompileInstantEmailTemplate(policyTriggered.getId());
        findAndCompileAggregationEmailTemplate(rhel.getName(), policies.getName(), DAILY);
        // App: vulnerability
        findAndCompileInstantEmailTemplate(newCveCvss.getId());
        findAndCompileInstantEmailTemplate(newCveSecurityRule.getId());
        findAndCompileInstantEmailTemplate(newCveSeverity.getId());
        findAndCompileInstantEmailTemplate(anyCveKnownExploit.getId());
        findAndCompileAggregationEmailTemplate(rhel.getName(), vulnerability.getName(), DAILY);
        /*
             * Bundle: openshift
             */
        // App: advisor
        findAndCompileInstantEmailTemplate(newRecommendationOpenshift.getId());
        assertTrue(templateRepository.findAggregationEmailTemplate(openshift.getName(), advisorOpenshift.getName(), DAILY).isEmpty());
        /*
             * Bundle: application-services
             */
        // App: rhosak
        findAndCompileInstantEmailTemplate(scheduledUpgrade.getId());
        findAndCompileInstantEmailTemplate(disruption.getId());
        findAndCompileInstantEmailTemplate(instanceCreated.getId());
        findAndCompileInstantEmailTemplate(instanceDeleted.getId());
        findAndCompileInstantEmailTemplate(actionRequired.getId());
        findAndCompileAggregationEmailTemplate(applicationServices.getName(), rhosak.getName(), DAILY);
        /*
             * Bundle: ansible
             */
        // App: reports
        findAndCompileInstantEmailTemplate(reportAvailable.getId());
        assertTrue(templateRepository.findAggregationEmailTemplate(ansible.getName(), reports.getName(), DAILY).isEmpty());
        /*
             * Bundle: console
             */
        // App: notifications
        findAndCompileInstantEmailTemplate(failedIntegration.getId());
        assertTrue(templateRepository.findAggregationEmailTemplate(console.getName(), notifications.getName(), DAILY).isEmpty());
        // App: sources
        findAndCompileInstantEmailTemplate(availabilityStatus.getId());
        assertTrue(templateRepository.findAggregationEmailTemplate(console.getName(), sources.getName(), DAILY).isEmpty());
        // App: rbac
        findAndCompileInstantEmailTemplate(rhNewRoleAvailable.getId());
        findAndCompileInstantEmailTemplate(rhPlatformDefaultRoleUpdated.getId());
        findAndCompileInstantEmailTemplate(rhNonPlatformDefaultRoleUpdated.getId());
        findAndCompileInstantEmailTemplate(customRoleCreated.getId());
        findAndCompileInstantEmailTemplate(customRoleUpdated.getId());
        findAndCompileInstantEmailTemplate(customRoleDeleted.getId());
        findAndCompileInstantEmailTemplate(rhNewRoleAddedToDefaultAccess.getId());
        findAndCompileInstantEmailTemplate(rhRoleRemovedFromDefaultAccess.getId());
        findAndCompileInstantEmailTemplate(customDefaultAccessUpdated.getId());
        findAndCompileInstantEmailTemplate(groupCreated.getId());
        findAndCompileInstantEmailTemplate(groupUpdated.getId());
        findAndCompileInstantEmailTemplate(groupDeleted.getId());
        findAndCompileInstantEmailTemplate(platformDefaultGroupTurnedIntoCustom.getId());
        assertTrue(templateRepository.findAggregationEmailTemplate(console.getName(), rbac.getName(), DAILY).isEmpty());
    });
    clearDbTemplates();
}
Also used : EventType(com.redhat.cloud.notifications.models.EventType) Bundle(com.redhat.cloud.notifications.models.Bundle) Application(com.redhat.cloud.notifications.models.Application) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Aggregations

Bundle (com.redhat.cloud.notifications.models.Bundle)28 Application (com.redhat.cloud.notifications.models.Application)13 QuarkusTest (io.quarkus.test.junit.QuarkusTest)13 Test (org.junit.jupiter.api.Test)13 BehaviorGroup (com.redhat.cloud.notifications.models.BehaviorGroup)12 DbIsolatedTest (com.redhat.cloud.notifications.db.DbIsolatedTest)11 EventType (com.redhat.cloud.notifications.models.EventType)11 Endpoint (com.redhat.cloud.notifications.models.Endpoint)6 Transactional (javax.transaction.Transactional)5 NotFoundException (javax.ws.rs.NotFoundException)4 BadRequestException (javax.ws.rs.BadRequestException)3 Event (com.redhat.cloud.notifications.models.Event)2 EmailSubscription (com.redhat.cloud.notifications.models.EmailSubscription)1 EmailSubscriptionType (com.redhat.cloud.notifications.models.EmailSubscriptionType)1 NotificationHistory (com.redhat.cloud.notifications.models.NotificationHistory)1 EventLogEntry (com.redhat.cloud.notifications.routers.models.EventLogEntry)1 SettingsValues (com.redhat.cloud.notifications.routers.models.SettingsValues)1 ApplicationSettingsValue (com.redhat.cloud.notifications.routers.models.SettingsValues.ApplicationSettingsValue)1 BundleSettingsValue (com.redhat.cloud.notifications.routers.models.SettingsValues.BundleSettingsValue)1 Header (io.restassured.http.Header)1