1
1
package io .quarkus .it .keycloak ;
2
2
3
- import java .io .IOException ;
4
3
import java .util .ArrayList ;
5
4
import java .util .Arrays ;
6
5
import java .util .Collections ;
7
6
import java .util .List ;
8
7
import java .util .Map ;
9
8
10
- import org .keycloak .representations .AccessTokenResponse ;
11
9
import org .keycloak .representations .idm .ClientRepresentation ;
12
10
import org .keycloak .representations .idm .CredentialRepresentation ;
13
11
import org .keycloak .representations .idm .RealmRepresentation ;
14
12
import org .keycloak .representations .idm .RoleRepresentation ;
15
13
import org .keycloak .representations .idm .RolesRepresentation ;
16
14
import org .keycloak .representations .idm .UserRepresentation ;
17
- import org .keycloak .util .JsonSerialization ;
18
15
16
+ import io .quarkus .test .common .DevServicesContext ;
19
17
import io .quarkus .test .common .QuarkusTestResourceLifecycleManager ;
20
- import io .restassured . RestAssured ;
18
+ import io .quarkus . test . keycloak . client . KeycloakTestClient ;
21
19
22
- public class KeycloakRealmResourceManager implements QuarkusTestResourceLifecycleManager {
20
+ public class KeycloakRealmResourceManager implements QuarkusTestResourceLifecycleManager , DevServicesContext . ContextAware {
23
21
24
- private static final String KEYCLOAK_SERVER_URL = System .getProperty ("keycloak.url" , "http://localhost:8180" );
25
22
private static final String KEYCLOAK_REALM = "quarkus" ;
26
23
24
+ final KeycloakTestClient client = new KeycloakTestClient ();
25
+
27
26
@ Override
28
27
public Map <String , String > start () {
29
28
@@ -38,33 +37,10 @@ public Map<String, String> start() {
38
37
realm .getUsers ().add (createUser ("bob" , "user" ));
39
38
realm .getUsers ().add (createUser ("john" , "tester" ));
40
39
41
- try {
42
- RestAssured
43
- .given ()
44
- .auth ().oauth2 (getAdminAccessToken ())
45
- .contentType ("application/json" )
46
- .body (JsonSerialization .writeValueAsBytes (realm ))
47
- .when ()
48
- .post (KEYCLOAK_SERVER_URL + "/admin/realms" ).then ()
49
- .statusCode (201 );
50
- } catch (IOException e ) {
51
- throw new RuntimeException (e );
52
- }
40
+ client .createRealm (realm );
53
41
return Collections .emptyMap ();
54
42
}
55
43
56
- private static String getAdminAccessToken () {
57
- return RestAssured
58
- .given ()
59
- .param ("grant_type" , "password" )
60
- .param ("username" , "admin" )
61
- .param ("password" , "admin" )
62
- .param ("client_id" , "admin-cli" )
63
- .when ()
64
- .post (KEYCLOAK_SERVER_URL + "/realms/master/protocol/openid-connect/token" )
65
- .as (AccessTokenResponse .class ).getToken ();
66
- }
67
-
68
44
private static RealmRepresentation createRealm (String name ) {
69
45
RealmRepresentation realm = new RealmRepresentation ();
70
46
@@ -126,23 +102,11 @@ private static UserRepresentation createUser(String username, String... realmRol
126
102
@ Override
127
103
public void stop () {
128
104
129
- RestAssured
130
- .given ()
131
- .auth ().oauth2 (getAdminAccessToken ())
132
- .when ()
133
- .delete (KEYCLOAK_SERVER_URL + "/admin/realms/" + KEYCLOAK_REALM ).then ().statusCode (204 );
105
+ client .deleteRealm (KEYCLOAK_REALM );
134
106
}
135
107
136
- public static String getAccessToken (String userName ) {
137
- return RestAssured
138
- .given ()
139
- .param ("grant_type" , "password" )
140
- .param ("username" , userName )
141
- .param ("password" , userName )
142
- .param ("client_id" , "quarkus-app" )
143
- .param ("client_secret" , "secret" )
144
- .when ()
145
- .post (KEYCLOAK_SERVER_URL + "/realms/" + KEYCLOAK_REALM + "/protocol/openid-connect/token" )
146
- .as (AccessTokenResponse .class ).getToken ();
108
+ @ Override
109
+ public void setIntegrationTestContext (DevServicesContext context ) {
110
+ client .setIntegrationTestContext (context );
147
111
}
148
112
}
0 commit comments