Skip to content

Commit 2222b01

Browse files
authored
[RHCLOUD-37354] add bulk resources for Notifications GW (RedHatInsights#3242)
* [RHCLOUD-37354] add bulk resources for Notifications GW * Sonar fixes
1 parent 9e917f7 commit 2222b01

File tree

5 files changed

+142
-1
lines changed

5 files changed

+142
-1
lines changed

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

+12
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
import jakarta.validation.constraints.NotNull;
1818
import jakarta.ws.rs.NotFoundException;
1919

20+
import java.util.ArrayList;
2021
import java.util.Collection;
22+
import java.util.HashMap;
2123
import java.util.List;
24+
import java.util.Map;
2225
import java.util.Set;
2326
import java.util.UUID;
2427
import java.util.function.Supplier;
@@ -187,7 +190,16 @@ public EventType getEventType(String bundleName, String applicationName, String
187190
} catch (NoResultException noResultException) {
188191
return null;
189192
}
193+
}
194+
195+
public Map<String, Map<String, List<String>>> getBaetList() {
196+
final String query = "SELECT application.bundle.name, application.name, name FROM EventType";
197+
final List<String[]> flatBaet = entityManager.createQuery(query, String[].class)
198+
.getResultList();
190199

200+
final Map<String, Map<String, List<String>>> mapBaet = new HashMap<>();
201+
flatBaet.stream().forEach(baet -> mapBaet.computeIfAbsent(baet[0], k -> new HashMap<>()).computeIfAbsent(baet[1], k -> new ArrayList<>()).add(baet[2]));
202+
return mapBaet;
191203
}
192204

193205
public List<EventType> getEventTypes(UUID appId) {

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

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import jakarta.persistence.EntityManager;
88
import jakarta.persistence.NoResultException;
99
import jakarta.transaction.Transactional;
10+
import java.util.List;
1011
import java.util.Optional;
1112
import java.util.UUID;
1213

@@ -44,6 +45,13 @@ public Optional<X509Certificate> findCertificate(String bundle, String applicati
4445
}
4546
}
4647

48+
public List<X509Certificate> findCertificates() {
49+
final String query = "FROM X509Certificate";
50+
return this.entityManager
51+
.createQuery(query, X509Certificate.class)
52+
.getResultList();
53+
}
54+
4755
@Transactional
4856
public boolean updateCertificate(UUID id, X509Certificate gatewayCertificate) {
4957
String hql = "UPDATE X509Certificate SET subjectDn = :subjectDn, sourceEnvironment = :sourceEnvironment WHERE id = :id";

backend/src/main/java/com/redhat/cloud/notifications/routers/internal/ValidationResource.java

+16
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
2424
import org.eclipse.microprofile.openapi.annotations.responses.APIResponses;
2525
import org.jboss.resteasy.reactive.RestQuery;
26+
import java.util.List;
27+
import java.util.Map;
2628
import java.util.Optional;
2729

2830
import static com.redhat.cloud.notifications.Constants.API_INTERNAL;
@@ -61,6 +63,13 @@ public Response validate(@RestQuery String bundle, @RestQuery String application
6163
}
6264
}
6365

66+
@GET
67+
@Path("/baet_list")
68+
@Produces(APPLICATION_JSON)
69+
public Map<String, Map<String, List<String>>> listBaet() {
70+
return applicationRepository.getBaetList();
71+
}
72+
6473
@POST
6574
@Consumes(APPLICATION_JSON)
6675
@APIResponses({
@@ -137,4 +146,11 @@ public X509Certificate validateCertificateAccordingBundleAndApp(@RestQuery Strin
137146
return gatewayCertificate.get();
138147
}
139148
}
149+
150+
@GET
151+
@Path("/certificates")
152+
@Produces(APPLICATION_JSON)
153+
public List<X509Certificate> getCertificates() {
154+
return x509CertificateRepository.findCertificates();
155+
}
140156
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.redhat.cloud.notifications.routers.internal;
2+
3+
import com.redhat.cloud.notifications.TestHelpers;
4+
import com.redhat.cloud.notifications.TestLifecycleManager;
5+
import com.redhat.cloud.notifications.db.DbIsolatedTest;
6+
import com.redhat.cloud.notifications.models.EventType;
7+
import io.quarkus.test.common.QuarkusTestResource;
8+
import io.quarkus.test.junit.QuarkusTest;
9+
import io.restassured.common.mapper.TypeRef;
10+
import io.restassured.http.Header;
11+
import org.apache.commons.lang3.RandomStringUtils;
12+
import org.eclipse.microprofile.config.inject.ConfigProperty;
13+
import org.junit.jupiter.api.Test;
14+
import java.util.List;
15+
import java.util.Map;
16+
import java.util.UUID;
17+
18+
import static com.redhat.cloud.notifications.CrudTestHelpers.createApp;
19+
import static com.redhat.cloud.notifications.CrudTestHelpers.createBundle;
20+
import static com.redhat.cloud.notifications.CrudTestHelpers.createEventType;
21+
import static io.restassured.RestAssured.given;
22+
import static io.restassured.http.ContentType.JSON;
23+
import static jakarta.ws.rs.core.Response.Status.OK;
24+
import static org.junit.jupiter.api.Assertions.assertEquals;
25+
import static org.junit.jupiter.api.Assertions.assertTrue;
26+
27+
28+
@QuarkusTest
29+
@QuarkusTestResource(TestLifecycleManager.class)
30+
class ValidationResourceTest extends DbIsolatedTest {
31+
32+
33+
@ConfigProperty(name = "internal.admin-role")
34+
String adminRole;
35+
36+
@Test
37+
void testGetBaetList() {
38+
Header adminIdentity = TestHelpers.createTurnpikeIdentityHeader("user", adminRole);
39+
40+
final String bundleName = "bundle-name-baet1";
41+
final String applicationName = "application-name-baet1";
42+
String bundleId = createBundle(adminIdentity, bundleName, RandomStringUtils.secure().nextAlphanumeric(10), OK.getStatusCode()).get();
43+
String applicationId = createApp(adminIdentity, bundleId, applicationName, RandomStringUtils.secure().nextAlphanumeric(10), null, OK.getStatusCode()).get();
44+
EventType eventType = new EventType();
45+
eventType.setDescription(RandomStringUtils.secure().nextAlphanumeric(10));
46+
eventType.setName("event1");
47+
eventType.setDisplayName("Event 1");
48+
eventType.setApplicationId(UUID.fromString(applicationId));
49+
createEventType(adminIdentity, eventType, OK.getStatusCode());
50+
51+
eventType.setName("event2");
52+
eventType.setDisplayName("Event 2");
53+
createEventType(adminIdentity, eventType, OK.getStatusCode());
54+
55+
Map<String, Map<String, List<String>>> baetMap = given()
56+
.header(adminIdentity)
57+
.contentType(JSON)
58+
.when()
59+
.get("/internal/validation/baet_list")
60+
.then()
61+
.statusCode(OK.getStatusCode()).extract().as(new TypeRef<>() { });
62+
63+
assertEquals(2, baetMap.size());
64+
assertEquals(1, baetMap.get("rhel").size());
65+
assertEquals(1, baetMap.get("rhel").get("policies").size());
66+
assertTrue(baetMap.get("rhel").get("policies").contains("policy-triggered"));
67+
68+
assertEquals(1, baetMap.get(bundleName).size());
69+
assertEquals(2, baetMap.get(bundleName).get(applicationName).size());
70+
assertTrue(baetMap.get(bundleName).get(applicationName).contains("event1"));
71+
assertTrue(baetMap.get(bundleName).get(applicationName).contains("event2"));
72+
}
73+
}

backend/src/test/java/com/redhat/cloud/notifications/routers/internal/X509CertificatesResourceTest.java

+33-1
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
import com.redhat.cloud.notifications.models.X509Certificate;
88
import io.quarkus.test.common.QuarkusTestResource;
99
import io.quarkus.test.junit.QuarkusTest;
10+
import io.restassured.common.mapper.TypeRef;
1011
import io.restassured.http.Header;
1112
import io.vertx.core.json.JsonObject;
1213
import jakarta.ws.rs.core.Response;
1314
import org.eclipse.microprofile.config.inject.ConfigProperty;
1415
import org.junit.jupiter.api.Test;
16+
import java.util.List;
1517
import java.util.UUID;
1618

1719
import static com.redhat.cloud.notifications.CrudTestHelpers.createApp;
@@ -44,6 +46,9 @@ void testCrudX509Certificate() {
4446

4547
checkGatewayCertificate(intenralUserIdentity, bundleName, applicationName, "certificate data", FORBIDDEN);
4648

49+
List<X509Certificate> certificates = getCertificates(adminIdentity);
50+
assertEquals(0, certificates.size());
51+
4752
X509Certificate x509Certificate = new X509Certificate();
4853
x509Certificate.setSubjectDn("certificate data");
4954
x509Certificate.setBundle(bundleName);
@@ -62,6 +67,9 @@ void testCrudX509Certificate() {
6267
String certificateId = createdCertificateId.getString("id");
6368
assertNotNull(certificateId);
6469

70+
certificates = getCertificates(adminIdentity);
71+
assertEquals(1, certificates.size());
72+
6573
x509Certificate = checkGatewayCertificate(intenralUserIdentity, bundleName, applicationName, "certificate data", OK);
6674
assertEquals("stage", x509Certificate.getSourceEnvironment());
6775

@@ -95,6 +103,18 @@ void testCrudX509Certificate() {
95103
.statusCode(OK.getStatusCode());
96104

97105
checkGatewayCertificate(intenralUserIdentity, bundleName, applicationName, "certificate data updated", FORBIDDEN);
106+
107+
given()
108+
.header(adminIdentity)
109+
.contentType(JSON)
110+
.pathParam("certificateId", x509Certificate.getId())
111+
.when()
112+
.delete("/internal/x509Certificates/{certificateId}")
113+
.then()
114+
.statusCode(OK.getStatusCode());
115+
116+
certificates = getCertificates(adminIdentity);
117+
assertEquals(0, certificates.size());
98118
}
99119

100120
@Test
@@ -146,9 +166,21 @@ private static X509Certificate checkGatewayCertificate(Header adminIdentity, Str
146166

147167
if (Response.Status.OK == status) {
148168
JsonObject jsonApp = new JsonObject(responseBody);
149-
return jsonApp.mapTo(X509Certificate.class);
169+
X509Certificate certificate = jsonApp.mapTo(X509Certificate.class);
170+
certificate.setId(UUID.fromString(jsonApp.getString("id")));
171+
return certificate;
150172
}
151173
return null;
152174
}
153175

176+
private static List<X509Certificate> getCertificates(Header adminIdentity) {
177+
return given()
178+
.header(adminIdentity)
179+
.contentType(JSON)
180+
.when()
181+
.get("/internal/validation/certificates")
182+
.then()
183+
.statusCode(OK.getStatusCode()).extract().as(new TypeRef<>() { });
184+
}
185+
154186
}

0 commit comments

Comments
 (0)