Skip to content

Commit

Permalink
added a count method for webhooks (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
xgp authored Jan 7, 2025
1 parent 615cf82 commit a7d71cb
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/main/java/io/phasetwo/keycloak/model/WebhookProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ default Stream<WebhookModel> getWebhooksStream(RealmModel realm) {
return getWebhooksStream(realm, null, null);
}

Long getWebhooksCount(RealmModel realm);

boolean removeWebhook(RealmModel realm, String id);

void removeWebhooks(RealmModel realm);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ public Stream<WebhookModel> getWebhooksStream(
return query.getResultStream().map(e -> new WebhookAdapter(session, realm, em, e));
}

@Override
public Long getWebhooksCount(RealmModel realm) {
TypedQuery<Long> query = em.createNamedQuery("countWebhooksByRealmId", Long.class);
query.setParameter("realmId", realm.getId());
return query.getSingleResult();
}

@Override
public boolean removeWebhook(RealmModel realm, String id) {
WebhookEntity e = em.find(WebhookEntity.class, id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
@NamedQuery(
name = "getWebhooksByRealmId",
query = "SELECT w FROM WebhookEntity w WHERE w.realmId = :realmId"),
@NamedQuery(
name = "countWebhooksByRealmId",
query = "SELECT count(w) FROM WebhookEntity w WHERE w.realmId = :realmId"),
@NamedQuery(
name = "removeAllWebhooks",
query = "DELETE FROM WebhookEntity w WHERE w.realmId = :realmId")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ public Stream<WebhookRepresentation> getWebhooks(
return webhooks.getWebhooksStream(realm, firstResult, maxResults).map(w -> toRepresentation(w));
}

@GET
@Path("count")
@Produces(MediaType.APPLICATION_JSON)
public Long countWebhooks(@QueryParam("search") String searchQuery) {
log.debugf("countWebhooks %s %s", realm.getName(), searchQuery);
permissions.realm().requireViewEvents();
return webhooks.getWebhooksCount(realm);
}

private WebhookRepresentation toRepresentation(WebhookModel w) {
WebhookRepresentation webhook = new WebhookRepresentation();
webhook.setId(w.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ public void testAddGetWebhook() throws Exception {
.auth(keycloak.tokenManager().getAccessTokenString())
.asResponse();
assertThat(response.getStatus(), is(200));
/*
String r = response.asString();
log.infof("got webhook response %s", r);
*/

WebhookRepresentation rep = response.asJson(new TypeReference<WebhookRepresentation>() {});
assertNotNull(rep);
assertTrue(rep.isEnabled());
Expand All @@ -68,11 +65,29 @@ public void testAddGetWebhook() throws Exception {
assertThat(rep.getUrl(), is(url));
assertNull(rep.getSecret());

response =
LegacySimpleHttp.doGet(baseUrl() + "/count", httpClient)
.auth(keycloak.tokenManager().getAccessTokenString())
.asResponse();
assertThat(response.getStatus(), is(200));
Long cnt = response.asJson(new TypeReference<Long>() {});
assertNotNull(cnt);
assertThat(cnt, is(1l));

response =
LegacySimpleHttp.doDelete(baseUrl() + "/" + urlencode(id), httpClient)
.auth(keycloak.tokenManager().getAccessTokenString())
.asResponse();
assertThat(response.getStatus(), is(204));

response =
LegacySimpleHttp.doGet(baseUrl() + "/count", httpClient)
.auth(keycloak.tokenManager().getAccessTokenString())
.asResponse();
assertThat(response.getStatus(), is(200));
cnt = response.asJson(new TypeReference<Long>() {});
assertNotNull(cnt);
assertThat(cnt, is(0l));
}

/** https://github.com/p2-inc/keycloak-events/issues/42 */
Expand Down

0 comments on commit a7d71cb

Please sign in to comment.