Search in sources :

Example 11 with EventType

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

the class BehaviorGroupRepositoryTest method testFindBehaviorGroupsByEventTypeId.

@Test
void testFindBehaviorGroupsByEventTypeId() {
    Bundle bundle = resourceHelpers.createBundle();
    Application app = resourceHelpers.createApplication(bundle.getId());
    EventType eventType = createEventType(app);
    BehaviorGroup behaviorGroup = resourceHelpers.createBehaviorGroup(DEFAULT_ACCOUNT_ID, "displayName", bundle.getId());
    updateAndCheckEventTypeBehaviors(DEFAULT_ACCOUNT_ID, eventType.getId(), true, behaviorGroup.getId());
    List<BehaviorGroup> behaviorGroups = resourceHelpers.findBehaviorGroupsByEventTypeId(eventType.getId());
    assertEquals(1, behaviorGroups.size());
    assertEquals(behaviorGroup.getId(), behaviorGroups.get(0).getId());
}
Also used : EventType(com.redhat.cloud.notifications.models.EventType) Bundle(com.redhat.cloud.notifications.models.Bundle) BehaviorGroup(com.redhat.cloud.notifications.models.BehaviorGroup) Application(com.redhat.cloud.notifications.models.Application) QuarkusTest(io.quarkus.test.junit.QuarkusTest) DbIsolatedTest(com.redhat.cloud.notifications.db.DbIsolatedTest) Test(org.junit.jupiter.api.Test)

Example 12 with EventType

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

the class EventLogCleanerTest method testPostgresStoredProcedure.

@Test
@Transactional
void testPostgresStoredProcedure() {
    deleteAllEvents();
    EventType eventType = createEventType();
    createEvent(eventType, now().minus(Duration.ofHours(1L)));
    createEvent(eventType, now().minus(Duration.ofDays(62L)));
    assertEquals(2L, count());
    entityManager.createNativeQuery("CALL cleanEventLog()").executeUpdate();
    assertEquals(1L, count());
}
Also used : EventType(com.redhat.cloud.notifications.models.EventType) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test) Transactional(javax.transaction.Transactional)

Example 13 with EventType

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

the class ResourceHelpers method createEventType.

@Transactional
public EventType createEventType(UUID appId, String eventTypeName) {
    EventType eventType = new EventType();
    eventType.setApplication(entityManager.find(Application.class, appId));
    eventType.setApplicationId(appId);
    eventType.setName(eventTypeName);
    eventType.setDisplayName("Policies will take care of the rules");
    eventType.setDescription("Policies is super cool, you should use it");
    entityManager.persist(eventType);
    return eventType;
}
Also used : EventType(com.redhat.cloud.notifications.models.EventType) Application(com.redhat.cloud.notifications.models.Application) Transactional(javax.transaction.Transactional)

Example 14 with EventType

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

the class EmailTemplateMigrationService method createInstantEmailTemplate.

/*
     * Creates an instant email template and the underlying templates only if:
     * - the event type exists in the DB
     * - the instant email template does not already exist in the DB
     * Existing instant email templates are never updated by this migration service.
     */
private void createInstantEmailTemplate(List<String> warnings, String bundleName, String appName, List<String> eventTypeNames, String subjectTemplateName, String subjectTemplateExtension, String subjectTemplateDescription, String bodyTemplateName, String bodyTemplateExtension, String bodyTemplateDescription) {
    for (String eventTypeName : eventTypeNames) {
        Optional<EventType> eventType = findEventType(warnings, bundleName, appName, eventTypeName);
        if (eventType.isPresent()) {
            if (instantEmailTemplateExists(eventType.get())) {
                warnings.add(String.format("Instant email template found in DB for event type: %s/%s/%s", bundleName, appName, eventTypeName));
            } else {
                Template subjectTemplate = getOrCreateTemplate(warnings, subjectTemplateName, subjectTemplateExtension, subjectTemplateDescription);
                Template bodyTemplate = getOrCreateTemplate(warnings, bodyTemplateName, bodyTemplateExtension, bodyTemplateDescription);
                LOGGER.infof("Creating instant email template for event type: %s/%s/%s", bundleName, appName, eventTypeName);
                InstantEmailTemplate emailTemplate = new InstantEmailTemplate();
                emailTemplate.setEventType(eventType.get());
                emailTemplate.setSubjectTemplate(subjectTemplate);
                emailTemplate.setSubjectTemplateId(subjectTemplate.getId());
                emailTemplate.setBodyTemplate(bodyTemplate);
                emailTemplate.setBodyTemplateId(bodyTemplate.getId());
                entityManager.persist(emailTemplate);
            }
        }
    }
}
Also used : EventType(com.redhat.cloud.notifications.models.EventType) InstantEmailTemplate(com.redhat.cloud.notifications.models.InstantEmailTemplate) AggregationEmailTemplate(com.redhat.cloud.notifications.models.AggregationEmailTemplate) Template(com.redhat.cloud.notifications.models.Template) InstantEmailTemplate(com.redhat.cloud.notifications.models.InstantEmailTemplate)

Example 15 with EventType

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

the class NotificationResourceTest method testGetEventTypesAffectedByEndpoint.

@Test
void testGetEventTypesAffectedByEndpoint() {
    String tenant = "testGetEventTypesAffectedByEndpoint";
    String orgId = "testGetEventTypesAffectedByEndpointOrgId";
    Header identityHeader = initRbacMock(tenant, orgId, "user", FULL_ACCESS);
    UUID bundleId = helpers.createTestAppAndEventTypes();
    UUID behaviorGroupId1 = helpers.createBehaviorGroup(tenant, "behavior-group-1", bundleId).getId();
    UUID behaviorGroupId2 = helpers.createBehaviorGroup(tenant, "behavior-group-2", bundleId).getId();
    UUID appId = applicationRepository.getApplications(TEST_BUNDLE_NAME).stream().filter(a -> a.getName().equals(TEST_APP_NAME_2)).findFirst().get().getId();
    UUID endpointId1 = helpers.createWebhookEndpoint(tenant);
    UUID endpointId2 = helpers.createWebhookEndpoint(tenant);
    List<EventType> eventTypes = applicationRepository.getEventTypes(appId);
    // ep1 assigned to ev0; ep2 not assigned.
    behaviorGroupRepository.updateEventTypeBehaviors(tenant, eventTypes.get(0).getId(), Set.of(behaviorGroupId1));
    behaviorGroupRepository.updateBehaviorGroupActions(tenant, behaviorGroupId1, List.of(endpointId1));
    String responseBody = given().header(identityHeader).pathParam("endpointId", endpointId1.toString()).when().get("/notifications/behaviorGroups/affectedByRemovalOfEndpoint/{endpointId}").then().statusCode(200).contentType(JSON).extract().asString();
    JsonArray behaviorGroups = new JsonArray(responseBody);
    assertEquals(1, behaviorGroups.size());
    behaviorGroups.getJsonObject(0).mapTo(BehaviorGroup.class);
    assertEquals(behaviorGroupId1.toString(), behaviorGroups.getJsonObject(0).getString("id"));
    responseBody = given().header(identityHeader).pathParam("endpointId", endpointId2.toString()).when().get("/notifications/behaviorGroups/affectedByRemovalOfEndpoint/{endpointId}").then().statusCode(200).contentType(JSON).extract().asString();
    behaviorGroups = new JsonArray(responseBody);
    assertEquals(0, behaviorGroups.size());
    // ep1 assigned to event ev0; ep2 assigned to event ev1
    behaviorGroupRepository.updateEventTypeBehaviors(tenant, eventTypes.get(0).getId(), Set.of(behaviorGroupId2));
    behaviorGroupRepository.updateBehaviorGroupActions(tenant, behaviorGroupId2, List.of(endpointId2));
    responseBody = given().header(identityHeader).pathParam("endpointId", endpointId1.toString()).when().get("/notifications/behaviorGroups/affectedByRemovalOfEndpoint/{endpointId}").then().statusCode(200).contentType(JSON).extract().asString();
    behaviorGroups = new JsonArray(responseBody);
    assertEquals(1, behaviorGroups.size());
    behaviorGroups.getJsonObject(0).mapTo(BehaviorGroup.class);
    assertEquals(behaviorGroupId1.toString(), behaviorGroups.getJsonObject(0).getString("id"));
    responseBody = given().header(identityHeader).pathParam("endpointId", endpointId2.toString()).when().get("/notifications/behaviorGroups/affectedByRemovalOfEndpoint/{endpointId}").then().statusCode(200).contentType(JSON).extract().asString();
    behaviorGroups = new JsonArray(responseBody);
    assertEquals(1, behaviorGroups.size());
    behaviorGroups.getJsonObject(0).mapTo(BehaviorGroup.class);
    assertEquals(behaviorGroupId2.toString(), behaviorGroups.getJsonObject(0).getString("id"));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) ResourceHelpers(com.redhat.cloud.notifications.db.ResourceHelpers) TestLifecycleManager(com.redhat.cloud.notifications.TestLifecycleManager) FULL_ACCESS(com.redhat.cloud.notifications.MockServerConfig.RbacAccess.FULL_ACCESS) TEST_BUNDLE_NAME(com.redhat.cloud.notifications.db.ResourceHelpers.TEST_BUNDLE_NAME) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) Header(io.restassured.http.Header) MockServerConfig(com.redhat.cloud.notifications.MockServerConfig) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Inject(javax.inject.Inject) EventType(com.redhat.cloud.notifications.models.EventType) BehaviorGroup(com.redhat.cloud.notifications.models.BehaviorGroup) NO_ACCESS(com.redhat.cloud.notifications.MockServerConfig.RbacAccess.NO_ACCESS) RbacAccess(com.redhat.cloud.notifications.MockServerConfig.RbacAccess) JsonObject(io.vertx.core.json.JsonObject) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) DbIsolatedTest(com.redhat.cloud.notifications.db.DbIsolatedTest) BehaviorGroupRepository(com.redhat.cloud.notifications.db.repositories.BehaviorGroupRepository) QuarkusTestResource(io.quarkus.test.common.QuarkusTestResource) Application(com.redhat.cloud.notifications.models.Application) Json(com.redhat.cloud.notifications.Json) JSON(io.restassured.http.ContentType.JSON) ApplicationRepository(com.redhat.cloud.notifications.db.repositories.ApplicationRepository) Set(java.util.Set) Facet(com.redhat.cloud.notifications.routers.models.Facet) READ_ACCESS(com.redhat.cloud.notifications.MockServerConfig.RbacAccess.READ_ACCESS) UUID(java.util.UUID) TEST_APP_NAME_2(com.redhat.cloud.notifications.db.ResourceHelpers.TEST_APP_NAME_2) Test(org.junit.jupiter.api.Test) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) Response(io.restassured.response.Response) TestConstants(com.redhat.cloud.notifications.TestConstants) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) TestHelpers(com.redhat.cloud.notifications.TestHelpers) RestAssured.given(io.restassured.RestAssured.given) RestAssured(io.restassured.RestAssured) JsonArray(io.vertx.core.json.JsonArray) Header(io.restassured.http.Header) EventType(com.redhat.cloud.notifications.models.EventType) UUID(java.util.UUID) QuarkusTest(io.quarkus.test.junit.QuarkusTest) DbIsolatedTest(com.redhat.cloud.notifications.db.DbIsolatedTest) Test(org.junit.jupiter.api.Test)

Aggregations

EventType (com.redhat.cloud.notifications.models.EventType)36 QuarkusTest (io.quarkus.test.junit.QuarkusTest)16 Test (org.junit.jupiter.api.Test)16 Application (com.redhat.cloud.notifications.models.Application)14 Bundle (com.redhat.cloud.notifications.models.Bundle)11 Transactional (javax.transaction.Transactional)10 BehaviorGroup (com.redhat.cloud.notifications.models.BehaviorGroup)9 Action (com.redhat.cloud.notifications.ingress.Action)7 TestHelpers.serializeAction (com.redhat.cloud.notifications.TestHelpers.serializeAction)6 DbIsolatedTest (com.redhat.cloud.notifications.db.DbIsolatedTest)6 UUID (java.util.UUID)5 Event (com.redhat.cloud.notifications.models.Event)4 NotFoundException (javax.ws.rs.NotFoundException)4 AggregationEmailTemplate (com.redhat.cloud.notifications.models.AggregationEmailTemplate)3 InstantEmailTemplate (com.redhat.cloud.notifications.models.InstantEmailTemplate)3 Template (com.redhat.cloud.notifications.models.Template)3 Header (io.restassured.http.Header)3 Endpoint (com.redhat.cloud.notifications.models.Endpoint)2 JsonArray (io.vertx.core.json.JsonArray)2 JsonObject (io.vertx.core.json.JsonObject)2