Skip to content

Commit 5341513

Browse files
author
Dennis Labordus
committed
Fixed Resource test with security.
Signed-off-by: Dennis Labordus <[email protected]>
1 parent 3a4ce9f commit 5341513

File tree

6 files changed

+285
-259
lines changed

6 files changed

+285
-259
lines changed

app/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ SPDX-License-Identifier: Apache-2.0
102102
<artifactId>rest-assured</artifactId>
103103
<scope>test</scope>
104104
</dependency>
105+
<dependency>
106+
<groupId>io.quarkus</groupId>
107+
<artifactId>quarkus-test-security-oidc</artifactId>
108+
<scope>test</scope>
109+
</dependency>
105110
<dependency>
106111
<groupId>io.quarkus</groupId>
107112
<artifactId>quarkus-jacoco</artifactId>

app/src/main/java/org/lfenergy/compas/scl/data/rest/Constants.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,9 @@ private Constants() {
1010
public static final String TYPE_PATH_PARAM = "type";
1111
public static final String ID_PATH_PARAM = "id";
1212
public static final String VERSION_PATH_PARAM = "version";
13+
14+
public static final String READ_ROLE = "Read";
15+
public static final String CREATE_ROLE = "Create";
16+
public static final String UPDATE_ROLE = "Update";
17+
public static final String DELETE_ROLE = "Delete";
1318
}

app/src/main/java/org/lfenergy/compas/scl/data/rest/v1/CompasCommonResource.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
import java.util.Arrays;
1616
import java.util.stream.Collectors;
1717

18+
import static org.lfenergy.compas.scl.data.rest.Constants.READ_ROLE;
19+
1820
@Path("/common/v1/")
1921
public class CompasCommonResource {
2022
@GET
2123
@Path("/type/list")
22-
@RolesAllowed("Read")
24+
@RolesAllowed(READ_ROLE)
2325
@Produces(MediaType.APPLICATION_XML)
2426
public TypeListResponse list() {
2527
var response = new TypeListResponse();

app/src/main/java/org/lfenergy/compas/scl/data/rest/v1/CompasSclDataResource.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public CompasSclDataResource(CompasSclDataService compasSclDataService) {
3333
}
3434

3535
@POST
36-
@RolesAllowed("Create")
36+
@RolesAllowed(CREATE_ROLE)
3737
@Consumes(MediaType.APPLICATION_XML)
3838
@Produces(MediaType.APPLICATION_XML)
3939
public CreateResponse create(@PathParam(TYPE_PATH_PARAM) SclType type,
@@ -45,7 +45,7 @@ public CreateResponse create(@PathParam(TYPE_PATH_PARAM) SclType type,
4545

4646
@GET
4747
@Path("/list")
48-
@RolesAllowed("Read")
48+
@RolesAllowed(READ_ROLE)
4949
@Produces(MediaType.APPLICATION_XML)
5050
public ListResponse list(@PathParam(TYPE_PATH_PARAM) SclType type) {
5151
var response = new ListResponse();
@@ -55,7 +55,7 @@ public ListResponse list(@PathParam(TYPE_PATH_PARAM) SclType type) {
5555

5656
@GET
5757
@Path("/{" + ID_PATH_PARAM + "}/versions")
58-
@RolesAllowed("Read")
58+
@RolesAllowed(READ_ROLE)
5959
@Produces(MediaType.APPLICATION_XML)
6060
public VersionsResponse listVersionsByUUID(@PathParam(TYPE_PATH_PARAM) SclType type,
6161
@PathParam(ID_PATH_PARAM) UUID id) {
@@ -66,7 +66,7 @@ public VersionsResponse listVersionsByUUID(@PathParam(TYPE_PATH_PARAM) SclType t
6666

6767
@GET
6868
@Path("/{" + ID_PATH_PARAM + "}")
69-
@RolesAllowed("Read")
69+
@RolesAllowed(READ_ROLE)
7070
@Produces(MediaType.APPLICATION_XML)
7171
public GetResponse findByUUID(@PathParam(TYPE_PATH_PARAM) SclType type,
7272
@PathParam(ID_PATH_PARAM) UUID id) {
@@ -77,7 +77,7 @@ public GetResponse findByUUID(@PathParam(TYPE_PATH_PARAM) SclType type,
7777

7878
@GET
7979
@Path("/{" + ID_PATH_PARAM + "}/{" + VERSION_PATH_PARAM + "}")
80-
@RolesAllowed("Read")
80+
@RolesAllowed(READ_ROLE)
8181
@Produces(MediaType.APPLICATION_XML)
8282
public GetResponse findByUUIDAndVersion(@PathParam(TYPE_PATH_PARAM) SclType type,
8383
@PathParam(ID_PATH_PARAM) UUID id,
@@ -89,7 +89,7 @@ public GetResponse findByUUIDAndVersion(@PathParam(TYPE_PATH_PARAM) SclType type
8989

9090
@GET
9191
@Path("/{" + ID_PATH_PARAM + "}/scl")
92-
@RolesAllowed("Read")
92+
@RolesAllowed(READ_ROLE)
9393
@Produces(MediaType.APPLICATION_XML)
9494
public String findRawSCLByUUID(@PathParam(TYPE_PATH_PARAM) SclType type,
9595
@PathParam(ID_PATH_PARAM) UUID id) {
@@ -99,7 +99,7 @@ public String findRawSCLByUUID(@PathParam(TYPE_PATH_PARAM) SclType type,
9999

100100
@GET
101101
@Path("/{" + ID_PATH_PARAM + "}/{" + VERSION_PATH_PARAM + "}/scl")
102-
@RolesAllowed("Read")
102+
@RolesAllowed(READ_ROLE)
103103
@Produces(MediaType.APPLICATION_XML)
104104
public String findRawSCLByUUIDAndVersion(@PathParam(TYPE_PATH_PARAM) SclType type,
105105
@PathParam(ID_PATH_PARAM) UUID id,
@@ -110,7 +110,7 @@ public String findRawSCLByUUIDAndVersion(@PathParam(TYPE_PATH_PARAM) SclType typ
110110

111111
@PUT
112112
@Path("/{" + ID_PATH_PARAM + "}")
113-
@RolesAllowed("Update")
113+
@RolesAllowed(UPDATE_ROLE)
114114
@Consumes(MediaType.APPLICATION_XML)
115115
@Produces(MediaType.APPLICATION_XML)
116116
public void update(@PathParam(TYPE_PATH_PARAM) SclType type,
@@ -121,7 +121,7 @@ public void update(@PathParam(TYPE_PATH_PARAM) SclType type,
121121

122122
@DELETE
123123
@Path("/{" + ID_PATH_PARAM + "}")
124-
@RolesAllowed("Delete")
124+
@RolesAllowed(DELETE_ROLE)
125125
@Produces(MediaType.APPLICATION_XML)
126126
public void deleteAll(@PathParam(TYPE_PATH_PARAM) SclType type,
127127
@PathParam(ID_PATH_PARAM) UUID id) {
@@ -130,7 +130,7 @@ public void deleteAll(@PathParam(TYPE_PATH_PARAM) SclType type,
130130

131131
@DELETE
132132
@Path("/{" + ID_PATH_PARAM + "}/{" + VERSION_PATH_PARAM + "}")
133-
@RolesAllowed("Delete")
133+
@RolesAllowed(DELETE_ROLE)
134134
@Produces(MediaType.APPLICATION_XML)
135135
public void deleteVersion(@PathParam(TYPE_PATH_PARAM) SclType type,
136136
@PathParam(ID_PATH_PARAM) UUID id,

app/src/test/java/org/lfenergy/compas/scl/data/rest/v1/CompasCommonResourceTest.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,28 @@
55

66
import io.quarkus.test.common.http.TestHTTPEndpoint;
77
import io.quarkus.test.junit.QuarkusTest;
8+
import io.quarkus.test.security.TestSecurity;
89
import org.junit.jupiter.api.Test;
910
import org.lfenergy.compas.scl.data.model.SclType;
1011

1112
import static io.restassured.RestAssured.given;
1213
import static org.junit.jupiter.api.Assertions.assertEquals;
14+
import static org.lfenergy.compas.scl.data.rest.Constants.READ_ROLE;
1315

1416
@QuarkusTest
1517
@TestHTTPEndpoint(CompasCommonResource.class)
1618
class CompasCommonResourceTest {
17-
// @Test
18-
// void list_WhenCalled_ThenItemResponseRetrieved() {
19-
// var response = given()
20-
// .when().get("/type/list")
21-
// .then()
22-
// .statusCode(200)
23-
// .extract()
24-
// .response();
25-
//
26-
// var xmlPath = response.xmlPath();
27-
// assertEquals(SclType.values().length, xmlPath.getList("TypeListResponse.Type").size());
28-
// }
19+
@Test
20+
@TestSecurity(user = "test-user", roles = {READ_ROLE})
21+
void list_WhenCalled_ThenItemResponseRetrieved() {
22+
var response = given()
23+
.when().get("/type/list")
24+
.then()
25+
.statusCode(200)
26+
.extract()
27+
.response();
28+
29+
var xmlPath = response.xmlPath();
30+
assertEquals(SclType.values().length, xmlPath.getList("TypeListResponse.Type").size());
31+
}
2932
}

0 commit comments

Comments
 (0)