Skip to content

Commit

Permalink
RHCLOUD-31596 add filter for unmuted event types to endpoint `/notifi…
Browse files Browse the repository at this point in the history
…cations/eventTypes` (#2760)

jira: RHCLOUD-31596
  • Loading branch information
jessicarod7 authored Jun 17, 2024
1 parent c9b6aac commit cf9ad74
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -246,21 +246,21 @@ public boolean deleteEventTypeById(UUID id) {
return rowCount > 0;
}

public List<EventType> getEventTypes(Query limiter, Set<UUID> appIds, UUID bundleId, String eventTypeName) {
public List<EventType> getEventTypes(Query limiter, Set<UUID> appIds, UUID bundleId, String eventTypeName, boolean excludeMutedTypes) {
if (limiter != null) {
limiter.setSortFields(EventType.SORT_FIELDS);
}

return getEventTypesQueryBuilder(appIds, bundleId, eventTypeName)
return getEventTypesQueryBuilder(appIds, bundleId, eventTypeName, excludeMutedTypes)
.join(JoinBuilder.builder().leftJoinFetch("e.application"))
.limit(limiter != null ? limiter.getLimit() : null)
.sort(limiter != null ? limiter.getSort() : null)
.build(entityManager::createQuery)
.getResultList();
}

public Long getEventTypesCount(Set<UUID> appIds, UUID bundleId, String eventTypeName) {
return getEventTypesQueryBuilder(appIds, bundleId, eventTypeName)
public Long getEventTypesCount(Set<UUID> appIds, UUID bundleId, String eventTypeName, boolean excludeMutedTypes) {
return getEventTypesQueryBuilder(appIds, bundleId, eventTypeName, excludeMutedTypes)
.buildCount(entityManager::createQuery)
.getSingleResult();
}
Expand Down Expand Up @@ -298,7 +298,9 @@ public boolean applicationBundleExists(final String applicationName, final Strin
}
}

private QueryBuilder<EventType> getEventTypesQueryBuilder(Set<UUID> appIds, UUID bundleId, String eventTypeName) {
private QueryBuilder<EventType> getEventTypesQueryBuilder(Set<UUID> appIds, UUID bundleId, String eventTypeName, boolean excludeMutedTypes) {
final String unmutedEventTypesQuery = "SELECT etb.eventType.id FROM EventTypeBehavior AS etb";

return QueryBuilder
.builder(EventType.class)
.alias("e")
Expand All @@ -308,6 +310,7 @@ private QueryBuilder<EventType> getEventTypesQueryBuilder(Set<UUID> appIds, UUID
.ifAnd(appIds != null && appIds.size() > 0, "e.application.id IN (:appIds)", "appIds", appIds)
.ifAnd(bundleId != null, "e.application.bundle.id = :bundleId", "bundleId", bundleId)
.ifAnd(eventTypeName != null, "(LOWER(e.displayName) LIKE :eventTypeName OR LOWER(e.name) LIKE :eventTypeName)", "eventTypeName", (Supplier<String>) () -> "%" + eventTypeName.toLowerCase() + "%")
.ifAnd(excludeMutedTypes, "e.id IN (" + unmutedEventTypesQuery + ")")
.and("e.visible = true")
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,14 @@ public Page<BehaviorGroup> getLinkedBehaviorGroups(
@GET
@Path("/eventTypes")
@Produces(APPLICATION_JSON)
@Operation(summary = "List all event types", description = "Lists all event types. You can filter the returned list by bundle or application name.")
@Operation(summary = "List all event types", description = "Lists all event types. You can filter the returned list by bundle, application name, or unmuted types.")
@RolesAllowed(ConsoleIdentityProvider.RBAC_READ_NOTIFICATIONS)
public Page<EventType> getEventTypes(
@Context UriInfo uriInfo, @BeanParam @Valid Query query, @QueryParam("applicationIds") Set<UUID> applicationIds, @QueryParam("bundleId") UUID bundleId,
@QueryParam("eventTypeName") String eventTypeName
@QueryParam("eventTypeName") String eventTypeName, @QueryParam("excludeMutedTypes") boolean excludeMutedTypes
) {
List<EventType> eventTypes = applicationRepository.getEventTypes(query, applicationIds, bundleId, eventTypeName);
Long count = applicationRepository.getEventTypesCount(applicationIds, bundleId, eventTypeName);
List<EventType> eventTypes = applicationRepository.getEventTypes(query, applicationIds, bundleId, eventTypeName, excludeMutedTypes);
Long count = applicationRepository.getEventTypesCount(applicationIds, bundleId, eventTypeName, excludeMutedTypes);
return new Page<>(
eventTypes,
PageLinksBuilder.build(uriInfo.getPath(), count, query.getLimit().getLimit(), query.getLimit().getOffset()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import static io.restassured.RestAssured.given;
import static io.restassured.http.ContentType.JSON;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -330,6 +331,107 @@ void testEventTypeFetchingByBundleApplicationAndEventTypeName() {
assertEquals(1, eventTypes.size());
}

@Test
void testEventTypeFetchingAndExcludeMutedTypes() {
UUID bundleId = helpers.createTestAppAndEventTypes();
List<Application> apps = applicationRepository.getApplications(TEST_BUNDLE_NAME);
Application app = apps.stream().filter(a -> a.getName().equals(TEST_APP_NAME_2)).findFirst().get();
UUID appId = app.getId();
Header identityHeader = initRbacMock(ACCOUNT_ID, ORG_ID, "user", FULL_ACCESS);
UUID behaviorGroupId1 = helpers.createBehaviorGroup(ACCOUNT_ID, ORG_ID, "behaviour-group-1", bundleId).getId();
UUID behaviorGroupId2 = helpers.createBehaviorGroup(ACCOUNT_ID, ORG_ID, "behaviour-group-2", bundleId).getId();

List<EventType> eventTypes = applicationRepository.getEventTypes(appId);
// bgroup1 assigned to ev0 and ev1, bgroup2 assigned to ev14, all other event types unassigned
List<String> unmutedEventTypeNames = List.of(String.format(TEST_EVENT_TYPE_FORMAT, 0), String.format(TEST_EVENT_TYPE_FORMAT, 1), String.format(TEST_EVENT_TYPE_FORMAT, 14));
behaviorGroupRepository.updateEventTypeBehaviors(ORG_ID, eventTypes.stream().filter(ev -> ev.getName().equals(unmutedEventTypeNames.getFirst())).findFirst().get().getId(), Set.of(behaviorGroupId1));
behaviorGroupRepository.updateEventTypeBehaviors(ORG_ID, eventTypes.stream().filter(ev -> ev.getName().equals(unmutedEventTypeNames.get(1))).findFirst().get().getId(), Set.of(behaviorGroupId1));
behaviorGroupRepository.updateEventTypeBehaviors(ORG_ID, eventTypes.stream().filter(ev -> ev.getName().equals(unmutedEventTypeNames.get(2))).findFirst().get().getId(), Set.of(behaviorGroupId2));

String response = given()
.when()
.header(identityHeader)
.queryParam("excludeMutedTypes", "true")
.get("/notifications/eventTypes")
.then()
.statusCode(200)
.contentType(JSON)
.extract().asString();

JsonObject page = new JsonObject(response);
JsonArray respEventTypes = page.getJsonArray("data");
ArrayList<String> respEventTypeNames = new ArrayList<>();
for (int i = 0; i < respEventTypes.size(); i++) {
JsonObject ev = respEventTypes.getJsonObject(i);
respEventTypeNames.add(ev.getString("name"));
}

assertTrue(unmutedEventTypeNames.containsAll(respEventTypeNames) && respEventTypeNames.containsAll(unmutedEventTypeNames));
assertFalse(respEventTypeNames.contains(String.format(TEST_EVENT_TYPE_FORMAT, 2)));
assertEquals(3, page.getJsonObject("meta").getInteger("count"));
}

@Test
void testEventTypeFetchingByBundleApplicationEventTypeNameAndExcludeMutedTypes() {
UUID bundleId = helpers.createTestAppAndEventTypes();
List<Application> apps = applicationRepository.getApplications(TEST_BUNDLE_NAME);
Application app1 = apps.stream().filter(a -> a.getName().equals(TEST_APP_NAME)).findFirst().get();
UUID appId1 = app1.getId();
Application app2 = apps.stream().filter(a -> a.getName().equals(TEST_APP_NAME_2)).findFirst().get();
UUID appId2 = app2.getId();
Header identityHeader = initRbacMock(ACCOUNT_ID, ORG_ID, "user", FULL_ACCESS);
UUID behaviorGroupId1 = helpers.createBehaviorGroup(ACCOUNT_ID, ORG_ID, "behaviour-group-1", bundleId).getId();
UUID behaviorGroupId2 = helpers.createBehaviorGroup(ACCOUNT_ID, ORG_ID, "behaviour-group-2", bundleId).getId();

// bgroup1 assigned to ev0 and ev1 on TEST_APP_NAME, bgroup2 assigned to ev1 on TEST_APP_NAME_2, all other event types unassigned
List<EventType> eventTypesApp1 = applicationRepository.getEventTypes(appId1);
behaviorGroupRepository.updateEventTypeBehaviors(ORG_ID, eventTypesApp1.getFirst().getId(), Set.of(behaviorGroupId1));
behaviorGroupRepository.updateEventTypeBehaviors(ORG_ID, eventTypesApp1.get(1).getId(), Set.of(behaviorGroupId1));
List<EventType> eventTypesApp2 = applicationRepository.getEventTypes(appId2);
behaviorGroupRepository.updateEventTypeBehaviors(ORG_ID, eventTypesApp2.get(1).getId(), Set.of(behaviorGroupId2));

Response unmutedResponse = given()
.when()
.header(identityHeader)
.queryParam("bundleId", bundleId)
.queryParam("applicationIds", appId1)
.queryParam("eventTypeName", "1")
.queryParam("excludeMutedTypes", "true")
.get("/notifications/eventTypes")
.then()
.statusCode(200)
.contentType(JSON)
.extract().response();

JsonObject unmutedPage = new JsonObject(unmutedResponse.getBody().asString());
JsonArray unmutedEventTypes = unmutedPage.getJsonArray("data");
for (int i = 0; i < unmutedEventTypes.size(); i++) {
JsonObject unmutedEv = unmutedEventTypes.getJsonObject(i);
unmutedEv.mapTo(EventType.class);
assertEquals(bundleId.toString(), unmutedEv.getJsonObject("application").getString("bundle_id"));
assertEquals(appId1.toString(), unmutedEv.getJsonObject("application").getString("id"));
assertTrue(unmutedEv.getString("display_name").contains("1") || unmutedEv.getString("name").contains("1"));
}
assertEquals(1, unmutedEventTypes.size());

Response mutedResponse = given()
.when()
.header(identityHeader)
.queryParam("bundleId", bundleId)
.queryParam("applicationIds", appId2)
.queryParam("eventTypeName", "50")
.queryParam("excludeMutedTypes", "true")
.get("notifications/eventTypes")
.then()
.statusCode(200)
.contentType(JSON)
.extract().response();

JsonObject mutedPage = new JsonObject(mutedResponse.getBody().asString());
JsonArray mutedEventTypes = mutedPage.getJsonArray("data");
assertTrue(mutedEventTypes.isEmpty());
}

@Test
void testGetEventTypesAffectedByEndpoint() {
String accountId = "testGetEventTypesAffectedByEndpoint";
Expand Down

0 comments on commit cf9ad74

Please sign in to comment.