18
18
import com .fasterxml .jackson .databind .ObjectMapper ;
19
19
import dev .openfga .sdk .api .configuration .*;
20
20
import dev .openfga .sdk .api .model .*;
21
+ import java .io .IOException ;
22
+ import java .nio .file .Files ;
23
+ import java .nio .file .Paths ;
21
24
import java .util .List ;
22
25
import java .util .Map ;
26
+ import org .junit .jupiter .api .BeforeAll ;
23
27
import org .junit .jupiter .api .BeforeEach ;
24
28
import org .junit .jupiter .api .Test ;
29
+ import org .junit .jupiter .api .TestInstance ;
30
+ import org .junit .jupiter .api .TestInstance .Lifecycle ;
25
31
32
+ @ TestInstance (Lifecycle .PER_CLASS )
26
33
public class OpenFgaClientIntegrationTest {
27
34
private static final ObjectMapper mapper = new ObjectMapper ().findAndRegisterModules ();
28
- private static final String DEFAULT_AUTH_MODEL =
29
- "{\" schema_version\" :\" 1.1\" ,\" type_definitions\" :[{\" type\" :\" user\" },{\" type\" :\" document\" ,\" relations\" :{\" reader\" :{\" this\" :{}},\" writer\" :{\" this\" :{}},\" owner\" :{\" this\" :{}}},\" metadata\" :{\" relations\" :{\" reader\" :{\" directly_related_user_types\" :[{\" type\" :\" user\" }]},\" writer\" :{\" directly_related_user_types\" :[{\" type\" :\" user\" }]},\" owner\" :{\" directly_related_user_types\" :[{\" type\" :\" user\" }]}}}}]}" ;
30
35
private static final String DEFAULT_USER = "user:81684243-9356-4421-8fbf-a4f8d36aa31b" ;
31
36
private static final String DEFAULT_DOC = "document:2021-budget" ;
32
- private static final ClientTupleKeyWithoutCondition DEFAULT_TUPLE_KEY = new ClientTupleKeyWithoutCondition ()
37
+ private static final ClientTupleKeyWithoutCondition DEFAULT_TUPLE_KEY_NO_CONDITION =
38
+ new ClientTupleKeyWithoutCondition ()
39
+ .user (DEFAULT_USER )
40
+ .relation ("reader" )
41
+ ._object (DEFAULT_DOC );
42
+ private static final ClientTupleKey DEFAULT_TUPLE_KEY = new ClientTupleKeyWithoutCondition ()
33
43
.user (DEFAULT_USER )
34
44
.relation ("reader" )
35
- ._object (DEFAULT_DOC );
36
- private static final ClientRelationshipCondition DEFAULT_CONDITION =
37
- new ClientRelationshipCondition ().name ("condition" ).context (Map .of ("some" , "context" ));
45
+ ._object (DEFAULT_DOC )
46
+ .condition (null ); // TODO: Add integ tests with conditions
38
47
private static final ClientAssertion DEFAULT_ASSERTION = new ClientAssertion ()
39
48
.user (DEFAULT_USER )
40
49
.relation ("reader" )
41
50
._object (DEFAULT_DOC )
42
51
.expectation (true );
52
+ private String authModelJson ;
43
53
44
54
private OpenFgaClient fga ;
45
55
56
+ @ BeforeAll
57
+ public void loadAuthModelJson () throws IOException {
58
+ authModelJson = Files .readString (Paths .get ("src" , "test-integration" , "resources" , "auth-model.json" ));
59
+ }
60
+
46
61
@ BeforeEach
47
62
public void initializeApi () throws Exception {
48
63
System .setProperty ("HttpRequestAttempt.debug-logging" , "enable" );
@@ -136,7 +151,7 @@ public void readAuthModel() throws Exception {
136
151
assertEquals (authModelId , response .getAuthorizationModel ().getId ());
137
152
String typeDefsJson = mapper .writeValueAsString (authModel .getTypeDefinitions ());
138
153
assertEquals (
139
- "[{\" type\" :\" user\" ,\" relations\" :{},\" metadata\" :null},{\" type\" :\" document\" ,\" relations\" :{\" owner\" :{\" this\" :{},\" computedUserset\" :null,\" tupleToUserset\" :null,\" union\" :null,\" intersection\" :null,\" difference\" :null},\" reader\" :{\" this\" :{},\" computedUserset\" :null,\" tupleToUserset\" :null,\" union\" :null,\" intersection\" :null,\" difference\" :null},\" writer\" :{\" this\" :{},\" computedUserset\" :null,\" tupleToUserset\" :null,\" union\" :null,\" intersection\" :null,\" difference\" :null}},\" metadata\" :{\" relations\" :{\" owner\" :{\" directly_related_user_types\" :[{\" type\" :\" user\" ,\" relation\" :null,\" wildcard\" :null,\" condition\" :null}]},\" reader\" :{\" directly_related_user_types\" :[{\" type\" :\" user\" ,\" relation\" :null,\" wildcard\" :null,\" condition\" :null}]},\" writer\" :{\" directly_related_user_types\" :[{\" type\" :\" user\" ,\" relation\" :null,\" wildcard\" :null,\" condition\" :null}]}}}}]" ,
154
+ "[{\" type\" :\" user\" ,\" relations\" :{},\" metadata\" :null},{\" type\" :\" document\" ,\" relations\" :{\" owner\" :{\" this\" :{},\" computedUserset\" :null,\" tupleToUserset\" :null,\" union\" :null,\" intersection\" :null,\" difference\" :null},\" reader\" :{\" this\" :{},\" computedUserset\" :null,\" tupleToUserset\" :null,\" union\" :null,\" intersection\" :null,\" difference\" :null},\" writer\" :{\" this\" :{},\" computedUserset\" :null,\" tupleToUserset\" :null,\" union\" :null,\" intersection\" :null,\" difference\" :null}},\" metadata\" :{\" relations\" :{\" conditional_reader\" :{\" directly_related_user_types\" :[{\" type\" :\" user\" ,\" relation\" :null,\" wildcard\" :null,\" condition\" :\" name_starts_with_a\" }]},\" owner\" :{\" directly_related_user_types\" :[{\" type\" :\" user\" ,\" relation\" :null,\" wildcard\" :null,\" condition\" :\" \" }]},\" reader\" :{\" directly_related_user_types\" :[{\" type\" :\" user\" ,\" relation\" :null,\" wildcard\" :null,\" condition\" :\" \" }]},\" writer\" :{\" directly_related_user_types\" :[{\" type\" :\" user\" ,\" relation\" :null,\" wildcard\" :null,\" condition\" :\" \" }]}}}}]" ,
140
155
typeDefsJson );
141
156
}
142
157
@@ -164,7 +179,7 @@ public void readAuthModels() throws Exception {
164
179
String typeDefsJson = mapper .writeValueAsString (authModel .getTypeDefinitions ());
165
180
166
181
assertEquals (
167
- "[{\" type\" :\" user\" ,\" relations\" :{},\" metadata\" :null},{\" type\" :\" document\" ,\" relations\" :{\" owner\" :{\" this\" :{},\" computedUserset\" :null,\" tupleToUserset\" :null,\" union\" :null,\" intersection\" :null,\" difference\" :null},\" reader\" :{\" this\" :{},\" computedUserset\" :null,\" tupleToUserset\" :null,\" union\" :null,\" intersection\" :null,\" difference\" :null},\" writer\" :{\" this\" :{},\" computedUserset\" :null,\" tupleToUserset\" :null,\" union\" :null,\" intersection\" :null,\" difference\" :null}},\" metadata\" :{\" relations\" :{\" owner\" :{\" directly_related_user_types\" :[{\" type\" :\" user\" ,\" relation\" :null,\" wildcard\" :null,\" condition\" :null}]},\" reader\" :{\" directly_related_user_types\" :[{\" type\" :\" user\" ,\" relation\" :null,\" wildcard\" :null,\" condition\" :null}]},\" writer\" :{\" directly_related_user_types\" :[{\" type\" :\" user\" ,\" relation\" :null,\" wildcard\" :null,\" condition\" :null}]}}}}]" ,
182
+ "[{\" type\" :\" user\" ,\" relations\" :{},\" metadata\" :null},{\" type\" :\" document\" ,\" relations\" :{\" owner\" :{\" this\" :{},\" computedUserset\" :null,\" tupleToUserset\" :null,\" union\" :null,\" intersection\" :null,\" difference\" :null},\" reader\" :{\" this\" :{},\" computedUserset\" :null,\" tupleToUserset\" :null,\" union\" :null,\" intersection\" :null,\" difference\" :null},\" writer\" :{\" this\" :{},\" computedUserset\" :null,\" tupleToUserset\" :null,\" union\" :null,\" intersection\" :null,\" difference\" :null}},\" metadata\" :{\" relations\" :{\" conditional_reader\" :{\" directly_related_user_types\" :[{\" type\" :\" user\" ,\" relation\" :null,\" wildcard\" :null,\" condition\" :\" name_starts_with_a\" }]},\" owner\" :{\" directly_related_user_types\" :[{\" type\" :\" user\" ,\" relation\" :null,\" wildcard\" :null,\" condition\" :\" \" }]},\" reader\" :{\" directly_related_user_types\" :[{\" type\" :\" user\" ,\" relation\" :null,\" wildcard\" :null,\" condition\" :\" \" }]},\" writer\" :{\" directly_related_user_types\" :[{\" type\" :\" user\" ,\" relation\" :null,\" wildcard\" :null,\" condition\" :\" \" }]}}}}]" ,
168
183
typeDefsJson );
169
184
} catch (JsonProcessingException ex ) {
170
185
assertNull (ex );
@@ -178,8 +193,7 @@ public void writeAuthModel() throws Exception {
178
193
String storeName = thisTestName ();
179
194
String storeId = createStore (storeName );
180
195
fga .setStoreId (storeId );
181
- WriteAuthorizationModelRequest request =
182
- mapper .readValue (DEFAULT_AUTH_MODEL , WriteAuthorizationModelRequest .class );
196
+ WriteAuthorizationModelRequest request = mapper .readValue (authModelJson , WriteAuthorizationModelRequest .class );
183
197
184
198
// When
185
199
WriteAuthorizationModelResponse response =
@@ -200,8 +214,7 @@ public void write_and_read() throws Exception {
200
214
String authModelId = writeAuthModel (storeId );
201
215
fga .setAuthorizationModelId (authModelId );
202
216
203
- ClientWriteRequest writeRequest =
204
- new ClientWriteRequest ().writes (List .of (DEFAULT_TUPLE_KEY .condition (DEFAULT_CONDITION )));
217
+ ClientWriteRequest writeRequest = new ClientWriteRequest ().writes (List .of (DEFAULT_TUPLE_KEY ));
205
218
ClientReadRequest readRequest =
206
219
new ClientReadRequest ().user (DEFAULT_USER )._object (DEFAULT_DOC );
207
220
@@ -226,8 +239,7 @@ public void write_and_check() throws Exception {
226
239
fga .setStoreId (storeId );
227
240
String authModelId = writeAuthModel (storeId );
228
241
fga .setAuthorizationModelId (authModelId );
229
- ClientWriteRequest writeRequest =
230
- new ClientWriteRequest ().writes (List .of (DEFAULT_TUPLE_KEY .condition (DEFAULT_CONDITION )));
242
+ ClientWriteRequest writeRequest = new ClientWriteRequest ().writes (List .of (DEFAULT_TUPLE_KEY ));
231
243
ClientCheckRequest checkRequest =
232
244
new ClientCheckRequest ().user (DEFAULT_USER ).relation ("reader" )._object (DEFAULT_DOC );
233
245
@@ -248,8 +260,7 @@ public void write_and_expand() throws Exception {
248
260
fga .setStoreId (storeId );
249
261
String authModelId = writeAuthModel (storeId );
250
262
fga .setAuthorizationModelId (authModelId );
251
- ClientWriteRequest writeRequest =
252
- new ClientWriteRequest ().writes (List .of (DEFAULT_TUPLE_KEY .condition (DEFAULT_CONDITION )));
263
+ ClientWriteRequest writeRequest = new ClientWriteRequest ().writes (List .of (DEFAULT_TUPLE_KEY ));
253
264
ClientExpandRequest expandRequest =
254
265
new ClientExpandRequest ()._object (DEFAULT_DOC ).relation ("reader" );
255
266
@@ -279,8 +290,7 @@ public void write_and_listObjects() throws Exception {
279
290
fga .setStoreId (storeId );
280
291
String authModelId = writeAuthModel (storeId );
281
292
fga .setAuthorizationModelId (authModelId );
282
- ClientWriteRequest writeRequest =
283
- new ClientWriteRequest ().writes (List .of (DEFAULT_TUPLE_KEY .condition (DEFAULT_CONDITION )));
293
+ ClientWriteRequest writeRequest = new ClientWriteRequest ().writes (List .of (DEFAULT_TUPLE_KEY ));
284
294
ClientListObjectsRequest listObjectsRequest = new ClientListObjectsRequest ()
285
295
.user (DEFAULT_USER )
286
296
.relation ("reader" )
@@ -335,10 +345,8 @@ private String createStore(String storeName) throws Exception {
335
345
*/
336
346
private String writeAuthModel (String storeId ) throws Exception {
337
347
fga .setStoreId (storeId );
338
- WriteAuthorizationModelRequest request =
339
- mapper .readValue (DEFAULT_AUTH_MODEL , WriteAuthorizationModelRequest .class );
340
- WriteAuthorizationModelResponse response =
341
- fga .writeAuthorizationModel (request ).get ();
348
+ var request = mapper .readValue (authModelJson , WriteAuthorizationModelRequest .class );
349
+ var response = fga .writeAuthorizationModel (request ).get ();
342
350
return response .getAuthorizationModelId ();
343
351
}
344
352
0 commit comments