Search in sources :

Example 6 with EventType

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

the class CrudTestHelpers method buildEventType.

public static EventType buildEventType(String appId, String name, String displayName, String description) {
    EventType eventType = new EventType();
    if (appId != null) {
        eventType.setApplicationId(UUID.fromString(appId));
    }
    eventType.setName(name);
    eventType.setDisplayName(displayName);
    eventType.setDescription(description);
    return eventType;
}
Also used : EventType(com.redhat.cloud.notifications.models.EventType)

Example 7 with EventType

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

the class EphemeralDataInitializer method persist.

@Transactional
void persist(EphemeralData data) {
    if (data.bundles != null) {
        for (com.redhat.cloud.notifications.ephemeral.Bundle b : data.bundles) {
            String selectBundleQuery = "FROM Bundle WHERE name = :bundleName";
            Bundle bundle;
            try {
                bundle = entityManager.createQuery(selectBundleQuery, Bundle.class).setParameter("bundleName", b.name).getSingleResult();
                LOGGER.infof("Bundle with name '%s' already exists, it won't be created from the ephemeral data", b.name);
            } catch (NoResultException e) {
                bundle = new Bundle();
                bundle.setName(b.name);
                bundle.setDisplayName(b.displayName);
                entityManager.persist(bundle);
            }
            if (b.applications != null) {
                for (com.redhat.cloud.notifications.ephemeral.Application a : b.applications) {
                    String selectAppQuery = "FROM Application WHERE bundle.name = :bundleName AND name = :appName";
                    Application app;
                    try {
                        app = entityManager.createQuery(selectAppQuery, Application.class).setParameter("bundleName", b.name).setParameter("appName", a.name).getSingleResult();
                        LOGGER.infof("Application with name '%s' already exists, it won't be created from the ephemeral data", a.name);
                    } catch (NoResultException e) {
                        app = new Application();
                        app.setName(a.name);
                        app.setDisplayName(a.displayName);
                        app.setBundle(bundle);
                        app.setBundleId(bundle.getId());
                        entityManager.persist(app);
                    }
                    if (a.eventTypes != null) {
                        for (com.redhat.cloud.notifications.ephemeral.EventType et : a.eventTypes) {
                            String selectEventTypeQuery = "FROM EventType WHERE application.bundle.name = :bundleName AND application.name = :appName AND name = :eventTypeName";
                            EventType eventType;
                            try {
                                entityManager.createQuery(selectEventTypeQuery, EventType.class).setParameter("bundleName", b.name).setParameter("appName", a.name).setParameter("eventTypeName", et.name).getSingleResult();
                                LOGGER.infof("Event type with name '%s' already exists, it won't be created from the ephemeral data", et.name);
                            } catch (NoResultException e) {
                                eventType = new EventType();
                                eventType.setName(et.name);
                                eventType.setDisplayName(et.displayName);
                                eventType.setDescription(et.description);
                                eventType.setApplication(app);
                                eventType.setApplicationId(app.getId());
                                entityManager.persist(eventType);
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : EventType(com.redhat.cloud.notifications.models.EventType) Bundle(com.redhat.cloud.notifications.models.Bundle) NoResultException(javax.persistence.NoResultException) Application(com.redhat.cloud.notifications.models.Application) Transactional(javax.transaction.Transactional)

Example 8 with EventType

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

the class EventConsumerTest method testInvalidMessageId.

@Test
void testInvalidMessageId() {
    EventType eventType = mockGetEventTypeAndCreateEvent();
    Action action = buildValidAction();
    String payload = serializeAction(action);
    Message<String> message = buildMessageWithId("I am not a valid UUID!".getBytes(UTF_8), payload);
    inMemoryConnector.source(INGRESS_CHANNEL).send(message);
    micrometerAssertionHelper.awaitAndAssertTimerIncrement(CONSUMED_TIMER_NAME, 1);
    assertEquals(1L, registry.timer(CONSUMED_TIMER_NAME, "bundle", action.getBundle(), "application", action.getApplication()).count());
    micrometerAssertionHelper.assertCounterIncrement(MESSAGE_ID_INVALID_COUNTER_NAME, 1);
    assertNoCounterIncrement(REJECTED_COUNTER_NAME, PROCESSING_ERROR_COUNTER_NAME, PROCESSING_EXCEPTION_COUNTER_NAME, DUPLICATE_COUNTER_NAME, MESSAGE_ID_VALID_COUNTER_NAME, MESSAGE_ID_MISSING_COUNTER_NAME);
    verifyExactlyOneProcessing(eventType, payload, action);
    verify(kafkaMessageDeduplicator, times(1)).registerMessageId(null);
}
Also used : TestHelpers.serializeAction(com.redhat.cloud.notifications.TestHelpers.serializeAction) Action(com.redhat.cloud.notifications.ingress.Action) EventType(com.redhat.cloud.notifications.models.EventType) Test(org.junit.jupiter.api.Test) QuarkusTest(io.quarkus.test.junit.QuarkusTest)

Example 9 with EventType

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

the class EventConsumerTest method testProcessingErrorWithoutMessageId.

@Test
void testProcessingErrorWithoutMessageId() {
    EventType eventType = mockGetEventTypeAndCreateEvent();
    mockProcessingFailure();
    Action action = buildValidAction();
    String payload = serializeAction(action);
    inMemoryConnector.source(INGRESS_CHANNEL).send(payload);
    micrometerAssertionHelper.awaitAndAssertTimerIncrement(CONSUMED_TIMER_NAME, 1);
    assertEquals(1L, registry.timer(CONSUMED_TIMER_NAME, "bundle", action.getBundle(), "application", action.getApplication()).count());
    micrometerAssertionHelper.assertCounterIncrement(MESSAGE_ID_MISSING_COUNTER_NAME, 1);
    micrometerAssertionHelper.assertCounterIncrement(PROCESSING_ERROR_COUNTER_NAME, 1);
    assertNoCounterIncrement(REJECTED_COUNTER_NAME, DUPLICATE_COUNTER_NAME, MESSAGE_ID_VALID_COUNTER_NAME, MESSAGE_ID_INVALID_COUNTER_NAME);
    verifyExactlyOneProcessing(eventType, payload, action);
    verify(kafkaMessageDeduplicator, times(1)).registerMessageId(null);
}
Also used : TestHelpers.serializeAction(com.redhat.cloud.notifications.TestHelpers.serializeAction) Action(com.redhat.cloud.notifications.ingress.Action) EventType(com.redhat.cloud.notifications.models.EventType) Test(org.junit.jupiter.api.Test) QuarkusTest(io.quarkus.test.junit.QuarkusTest)

Example 10 with EventType

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

the class EventConsumerTest method mockGetEventTypeAndCreateEvent.

private EventType mockGetEventTypeAndCreateEvent() {
    Bundle bundle = new Bundle();
    bundle.setDisplayName("Bundle");
    Application app = new Application();
    app.setDisplayName("Application");
    app.setBundle(bundle);
    EventType eventType = new EventType();
    eventType.setDisplayName("Event type");
    eventType.setApplication(app);
    when(eventTypeRepository.getEventType(eq(BUNDLE), eq(APP), eq(EVENT_TYPE))).thenReturn(eventType);
    when(eventRepository.create(any(Event.class))).thenAnswer(invocation -> invocation.getArgument(0));
    return eventType;
}
Also used : EventType(com.redhat.cloud.notifications.models.EventType) Bundle(com.redhat.cloud.notifications.models.Bundle) Event(com.redhat.cloud.notifications.models.Event) Application(com.redhat.cloud.notifications.models.Application)

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