Search in sources :

Example 16 with Application

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

the class EndpointResource method unsubscribeEmail.

@DELETE
@Path("/email/subscription/{bundleName}/{applicationName}/{type}")
@Produces(APPLICATION_JSON)
@RolesAllowed(ConsoleIdentityProvider.RBAC_WRITE_INTEGRATIONS_ENDPOINTS)
@Transactional
public boolean unsubscribeEmail(@Context SecurityContext sec, @PathParam("bundleName") String bundleName, @PathParam("applicationName") String applicationName, @PathParam("type") EmailSubscriptionType type) {
    RhIdPrincipal principal = (RhIdPrincipal) sec.getUserPrincipal();
    Application app = applicationRepository.getApplication(bundleName, applicationName);
    if (app == null) {
        throw new NotFoundException();
    } else {
        return emailSubscriptionRepository.unsubscribe(principal.getAccount(), principal.getName(), bundleName, applicationName, type);
    }
}
Also used : RhIdPrincipal(com.redhat.cloud.notifications.auth.principal.rhid.RhIdPrincipal) NotFoundException(javax.ws.rs.NotFoundException) Application(com.redhat.cloud.notifications.models.Application) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) RolesAllowed(javax.annotation.security.RolesAllowed) Produces(javax.ws.rs.Produces) Transactional(javax.transaction.Transactional)

Example 17 with Application

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

the class DbCleaner method clean.

/**
 * Deletes all records from all database tables (except for flyway_schema_history) and restores the default records.
 * This method should be called from a method annotated with <b>both</b> {@link BeforeEach} and {@link AfterEach} in
 * all test classes that involve SQL queries to guarantee the tests isolation in terms of stored data.
 */
@Transactional
public void clean() {
    for (Class<?> entity : ENTITIES) {
        entityManager.createQuery("DELETE FROM " + entity.getSimpleName()).executeUpdate();
    }
    Bundle bundle = new Bundle(DEFAULT_BUNDLE_NAME, DEFAULT_BUNDLE_DISPLAY_NAME);
    bundleRepository.createBundle(bundle);
    Application app = new Application();
    app.setBundleId(bundle.getId());
    app.setName(DEFAULT_APP_NAME);
    app.setDisplayName(DEFAULT_APP_DISPLAY_NAME);
    applicationRepository.createApp(app);
    EventType eventType = new EventType();
    eventType.setApplicationId(app.getId());
    eventType.setName(DEFAULT_EVENT_TYPE_NAME);
    eventType.setDisplayName(DEFAULT_EVENT_TYPE_DISPLAY_NAME);
    eventType.setDescription(DEFAULT_EVENT_TYPE_DESCRIPTION);
    applicationRepository.createEventType(eventType);
    entityManager.createQuery("UPDATE CurrentStatus SET status = :status").setParameter("status", Status.UP).executeUpdate();
}
Also used : EventType(com.redhat.cloud.notifications.models.EventType) Bundle(com.redhat.cloud.notifications.models.Bundle) Application(com.redhat.cloud.notifications.models.Application) Transactional(javax.transaction.Transactional)

Example 18 with Application

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

the class ResourceHelpers method createApplication.

public Application createApplication(UUID bundleId, String name, String displayName) {
    Application app = new Application();
    app.setBundleId(bundleId);
    app.setName(name);
    app.setDisplayName(displayName);
    return applicationRepository.createApp(app);
}
Also used : Application(com.redhat.cloud.notifications.models.Application)

Example 19 with Application

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

the class BehaviorGroupRepositoryTest method testAddAndDeleteEventTypeBehavior.

@Test
void testAddAndDeleteEventTypeBehavior() {
    Bundle bundle = resourceHelpers.createBundle();
    Application app = resourceHelpers.createApplication(bundle.getId());
    EventType eventType = createEventType(app);
    BehaviorGroup behaviorGroup1 = resourceHelpers.createBehaviorGroup(DEFAULT_ACCOUNT_ID, "Behavior group 1", bundle.getId());
    BehaviorGroup behaviorGroup2 = resourceHelpers.createBehaviorGroup(DEFAULT_ACCOUNT_ID, "Behavior group 2", bundle.getId());
    BehaviorGroup behaviorGroup3 = resourceHelpers.createBehaviorGroup(DEFAULT_ACCOUNT_ID, "Behavior group 3", bundle.getId());
    updateAndCheckEventTypeBehaviors(DEFAULT_ACCOUNT_ID, eventType.getId(), true, behaviorGroup1.getId());
    updateAndCheckEventTypeBehaviors(DEFAULT_ACCOUNT_ID, eventType.getId(), true, behaviorGroup1.getId());
    updateAndCheckEventTypeBehaviors(DEFAULT_ACCOUNT_ID, eventType.getId(), true, behaviorGroup1.getId(), behaviorGroup2.getId());
    updateAndCheckEventTypeBehaviors(DEFAULT_ACCOUNT_ID, eventType.getId(), true, behaviorGroup2.getId());
    updateAndCheckEventTypeBehaviors(DEFAULT_ACCOUNT_ID, eventType.getId(), true, behaviorGroup1.getId(), behaviorGroup2.getId(), behaviorGroup3.getId());
    updateAndCheckEventTypeBehaviors(DEFAULT_ACCOUNT_ID, eventType.getId(), true);
}
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 20 with Application

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

the class UserConfigResource method saveSettings.

@POST
@Path("/notification-preference")
@Consumes(APPLICATION_JSON)
@Produces(TEXT_PLAIN)
@Operation(hidden = true)
@Transactional
public Response saveSettings(@Context SecurityContext sec, @Valid SettingsValues values) {
    final RhIdPrincipal principal = (RhIdPrincipal) sec.getUserPrincipal();
    final String account = principal.getAccount();
    final String name = principal.getName();
    final List<Boolean> subscriptionRequests = new ArrayList<>();
    values.bundles.forEach((bundleName, bundleSettingsValue) -> bundleSettingsValue.applications.forEach((applicationName, applicationSettingsValue) -> {
        Application app = applicationRepository.getApplication(bundleName, applicationName);
        applicationSettingsValue.notifications.forEach((emailSubscriptionType, subscribed) -> {
            if (subscribed) {
                if (app != null) {
                    subscriptionRequests.add(emailSubscriptionRepository.subscribe(account, name, bundleName, applicationName, emailSubscriptionType));
                }
            } else {
                subscriptionRequests.add(emailSubscriptionRepository.unsubscribe(account, name, bundleName, applicationName, emailSubscriptionType));
            }
        });
    }));
    boolean allisSuccess = subscriptionRequests.stream().allMatch(Boolean.TRUE::equals);
    Response.ResponseBuilder builder;
    if (allisSuccess) {
        builder = Response.ok();
    } else {
        // Prevent from saving
        builder = Response.serverError().entity("Storing of settings Failed.");
        builder.type("text/plain");
    }
    return builder.build();
}
Also used : PathParam(javax.ws.rs.PathParam) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Constants(com.redhat.cloud.notifications.Constants) Path(javax.ws.rs.Path) SecurityContext(javax.ws.rs.core.SecurityContext) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Valid(javax.validation.Valid) EmailSubscriptionRepository(com.redhat.cloud.notifications.db.repositories.EmailSubscriptionRepository) BundleSettingsValue(com.redhat.cloud.notifications.routers.models.SettingsValues.BundleSettingsValue) QueryParam(javax.ws.rs.QueryParam) Consumes(javax.ws.rs.Consumes) BadRequestException(javax.ws.rs.BadRequestException) APPLICATION_JSON(javax.ws.rs.core.MediaType.APPLICATION_JSON) EmailSubscriptionType(com.redhat.cloud.notifications.models.EmailSubscriptionType) UserConfigPreferences(com.redhat.cloud.notifications.routers.models.UserConfigPreferences) BundleRepository(com.redhat.cloud.notifications.db.repositories.BundleRepository) Application(com.redhat.cloud.notifications.models.Application) ApplicationRepository(com.redhat.cloud.notifications.db.repositories.ApplicationRepository) POST(javax.ws.rs.POST) Context(javax.ws.rs.core.Context) RestClient(org.eclipse.microprofile.rest.client.inject.RestClient) SettingsValues(com.redhat.cloud.notifications.routers.models.SettingsValues) Transactional(javax.transaction.Transactional) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Bundle(com.redhat.cloud.notifications.models.Bundle) EmailSubscription(com.redhat.cloud.notifications.models.EmailSubscription) SettingsValueJsonForm(com.redhat.cloud.notifications.routers.models.SettingsValueJsonForm) Operation(org.eclipse.microprofile.openapi.annotations.Operation) EntityTag(javax.ws.rs.core.EntityTag) ApplicationSettingsValue(com.redhat.cloud.notifications.routers.models.SettingsValues.ApplicationSettingsValue) List(java.util.List) Response(javax.ws.rs.core.Response) RhIdPrincipal(com.redhat.cloud.notifications.auth.principal.rhid.RhIdPrincipal) TemplateEngineClient(com.redhat.cloud.notifications.templates.TemplateEngineClient) TEXT_PLAIN(javax.ws.rs.core.MediaType.TEXT_PLAIN) Response(javax.ws.rs.core.Response) RhIdPrincipal(com.redhat.cloud.notifications.auth.principal.rhid.RhIdPrincipal) ArrayList(java.util.ArrayList) Application(com.redhat.cloud.notifications.models.Application) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) Operation(org.eclipse.microprofile.openapi.annotations.Operation) Transactional(javax.transaction.Transactional)

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