Skip to content

Commit a3fbcc2

Browse files
authored
Implement drawer integration cleanup API (RedHatInsights#3205)
1 parent f7f8142 commit a3fbcc2

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

backend/src/main/java/com/redhat/cloud/notifications/db/repositories/DrawerNotificationRepository.java

+8
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,12 @@ private String getOrderBy(Query.Sort sort) {
147147
}
148148
}
149149

150+
@Transactional
151+
public void cleanupIntegrations(int limit) {
152+
String deleteQuery = "delete from endpoints where id in (select id from endpoints where endpoint_type_v2 = 'DRAWER' " +
153+
"and org_id is not null and not exists (select 1 from endpoint_event_type where endpoint_id = id) limit :limit)";
154+
entityManager.createNativeQuery(deleteQuery)
155+
.setParameter("limit", limit)
156+
.executeUpdate();
157+
}
150158
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.redhat.cloud.notifications.routers.internal.cleanup;
2+
3+
import com.redhat.cloud.notifications.auth.ConsoleIdentityProvider;
4+
import com.redhat.cloud.notifications.db.repositories.DrawerNotificationRepository;
5+
import com.redhat.cloud.notifications.models.Environment;
6+
import jakarta.annotation.security.RolesAllowed;
7+
import jakarta.inject.Inject;
8+
import jakarta.ws.rs.Consumes;
9+
import jakarta.ws.rs.POST;
10+
import jakarta.ws.rs.Path;
11+
import jakarta.ws.rs.core.MediaType;
12+
import jakarta.ws.rs.core.Response;
13+
14+
import static com.redhat.cloud.notifications.Constants.API_INTERNAL;
15+
16+
@RolesAllowed(ConsoleIdentityProvider.RBAC_INTERNAL_ADMIN)
17+
@Path(API_INTERNAL + "/drawer_cleanup")
18+
public class DrawerIntegrationCleanUpResource {
19+
20+
@Inject
21+
DrawerNotificationRepository drawerNotificationRepository;
22+
23+
@Inject
24+
Environment environment;
25+
26+
@POST
27+
@Path("/")
28+
@Consumes(MediaType.APPLICATION_JSON)
29+
public Response cleanUp(int limit) {
30+
if (environment.isStage() || environment.isLocal()) {
31+
drawerNotificationRepository.cleanupIntegrations(limit);
32+
return Response.ok().build();
33+
}
34+
return Response.status(Response.Status.FORBIDDEN).build();
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.redhat.cloud.notifications.routers.internal.cleanup;
2+
3+
import com.redhat.cloud.notifications.Constants;
4+
import com.redhat.cloud.notifications.TestHelpers;
5+
import com.redhat.cloud.notifications.TestLifecycleManager;
6+
import com.redhat.cloud.notifications.db.DbIsolatedTest;
7+
import com.redhat.cloud.notifications.db.ResourceHelpers;
8+
import com.redhat.cloud.notifications.models.Endpoint;
9+
import com.redhat.cloud.notifications.models.EndpointType;
10+
import io.quarkus.test.common.QuarkusTestResource;
11+
import io.quarkus.test.junit.QuarkusTest;
12+
import jakarta.inject.Inject;
13+
import jakarta.persistence.EntityManager;
14+
import org.eclipse.microprofile.config.inject.ConfigProperty;
15+
import org.junit.jupiter.api.Test;
16+
import java.util.List;
17+
18+
import static io.restassured.RestAssured.given;
19+
import static io.restassured.http.ContentType.JSON;
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
22+
@QuarkusTest
23+
@QuarkusTestResource(TestLifecycleManager.class)
24+
class DrawerIntegrationCleanUpResourceTest extends DbIsolatedTest {
25+
26+
@ConfigProperty(name = "internal.admin-role")
27+
String adminRole;
28+
29+
@Inject
30+
ResourceHelpers resourceHelpers;
31+
32+
@Inject
33+
EntityManager entityManager;
34+
35+
@Test
36+
void testDrawerIntegrationDelete() {
37+
38+
for (int i = 0; i < 15; i++) {
39+
resourceHelpers.createEndpoint("123456", "123456", EndpointType.DRAWER);
40+
}
41+
String query = "SELECT e FROM Endpoint e WHERE e.orgId = :orgId";
42+
List<Endpoint> endpoints = entityManager.createQuery(query, Endpoint.class)
43+
.setParameter("orgId", 123456)
44+
.getResultList();
45+
46+
assertEquals(15, endpoints.size());
47+
48+
given()
49+
.basePath(Constants.API_INTERNAL)
50+
.header(TestHelpers.createTurnpikeIdentityHeader("user", this.adminRole))
51+
.when()
52+
.contentType(JSON)
53+
.body(10)
54+
.post("/drawer_cleanup")
55+
.then()
56+
.statusCode(200);
57+
58+
endpoints = entityManager.createQuery(query, Endpoint.class)
59+
.setParameter("orgId", 123456)
60+
.getResultList();
61+
assertEquals(5, endpoints.size());
62+
63+
given()
64+
.basePath(Constants.API_INTERNAL)
65+
.header(TestHelpers.createTurnpikeIdentityHeader("user", this.adminRole))
66+
.when()
67+
.contentType(JSON)
68+
.body(10)
69+
.post("/drawer_cleanup")
70+
.then()
71+
.statusCode(200);
72+
73+
endpoints = entityManager.createQuery(query, Endpoint.class)
74+
.setParameter("orgId", 123456)
75+
.getResultList();
76+
assertEquals(0, endpoints.size());
77+
}
78+
}

0 commit comments

Comments
 (0)