Search in sources :

Example 6 with Application

use of com.redhat.cloud.notifications.models.Application 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 7 with Application

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

the class ApplicationRepository method createEventType.

@Transactional
public EventType createEventType(EventType eventType) {
    Application app = entityManager.find(Application.class, eventType.getApplicationId());
    if (app == null) {
        throw new NotFoundException();
    } else {
        eventType.setApplication(app);
        entityManager.persist(eventType);
        eventType.filterOutApplication();
        return eventType;
    }
}
Also used : NotFoundException(javax.ws.rs.NotFoundException) Application(com.redhat.cloud.notifications.models.Application) Transactional(javax.transaction.Transactional)

Example 8 with Application

use of com.redhat.cloud.notifications.models.Application 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)

Example 9 with Application

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

the class LifecycleITest method subscribeUserPreferences.

@Transactional
void subscribeUserPreferences(String accountId, String userId, UUID appId) {
    Application application = entityManager.find(Application.class, appId);
    EmailSubscription subscription = new EmailSubscription();
    subscription.setId(new EmailSubscriptionId());
    subscription.setAccountId(accountId);
    subscription.setUserId(userId);
    subscription.setApplication(application);
    subscription.setType(INSTANT);
    entityManager.persist(subscription);
}
Also used : EmailSubscriptionId(com.redhat.cloud.notifications.models.EmailSubscriptionId) Application(com.redhat.cloud.notifications.models.Application) EmailSubscription(com.redhat.cloud.notifications.models.EmailSubscription) Transactional(javax.transaction.Transactional)

Example 10 with Application

use of com.redhat.cloud.notifications.models.Application 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)

Aggregations

Application (com.redhat.cloud.notifications.models.Application)32 Bundle (com.redhat.cloud.notifications.models.Bundle)15 EventType (com.redhat.cloud.notifications.models.EventType)14 QuarkusTest (io.quarkus.test.junit.QuarkusTest)11 Transactional (javax.transaction.Transactional)11 Test (org.junit.jupiter.api.Test)11 DbIsolatedTest (com.redhat.cloud.notifications.db.DbIsolatedTest)9 BehaviorGroup (com.redhat.cloud.notifications.models.BehaviorGroup)7 Header (io.restassured.http.Header)5 Response (io.restassured.response.Response)4 JsonArray (io.vertx.core.json.JsonArray)4 JsonObject (io.vertx.core.json.JsonObject)4 Path (javax.ws.rs.Path)4 Produces (javax.ws.rs.Produces)4 RhIdPrincipal (com.redhat.cloud.notifications.auth.principal.rhid.RhIdPrincipal)3 ApplicationRepository (com.redhat.cloud.notifications.db.repositories.ApplicationRepository)3 EmailSubscription (com.redhat.cloud.notifications.models.EmailSubscription)3 Endpoint (com.redhat.cloud.notifications.models.Endpoint)3 List (java.util.List)3 UUID (java.util.UUID)3