Skip to content

Commit d0e3bad

Browse files
author
Asaf Cohen
committed
fix condition set rule api and add test
1 parent 7ff10c2 commit d0e3bad

2 files changed

Lines changed: 41 additions & 34 deletions

File tree

src/main/java/io/permit/sdk/api/ConditionSetRulesApi.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ public ConditionSetRuleRead create(ConditionSetRuleCreate rule) throws IOExcepti
231231
.post(jsonBody)
232232
);
233233

234-
return this.<ConditionSetRuleRead>callApiAndParseJson(request, ConditionSetRuleRead.class);
234+
ConditionSetRuleRead[] createdRuleArray = this.<ConditionSetRuleRead[]>callApiAndParseJson(request, ConditionSetRuleRead[].class);
235+
return createdRuleArray[0];
235236
}
236237

237238
/**

src/test/java/io/permit/sdk/endpoints/ConditionSetsE2ETest.java

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.permit.sdk.endpoints;
22

33
import com.google.gson.Gson;
4-
import com.google.gson.internal.LinkedTreeMap;
54
import io.permit.sdk.Permit;
65
import io.permit.sdk.PermitE2ETestBase;
76
import io.permit.sdk.api.PermitApiError;
@@ -12,10 +11,7 @@
1211
import org.junit.jupiter.api.Test;
1312

1413
import java.io.IOException;
15-
import java.util.Arrays;
16-
import java.util.Collections;
17-
import java.util.HashMap;
18-
import java.util.List;
14+
import java.util.*;
1915
import java.util.stream.Collectors;
2016

2117
import static org.junit.jupiter.api.Assertions.*;
@@ -33,6 +29,40 @@ public class ConditionSetsE2ETest extends PermitE2ETestBase {
3329
private static ConditionSetCreate resourcesetData;
3430
private static ConditionSetRuleCreate ruleData;
3531

32+
private static HashMap<String, Object> getUsersetConditions() {
33+
HashMap<String, Integer> ageCondition = new HashMap<String, Integer>() {{
34+
put("greater-than", 30);
35+
}};
36+
37+
HashMap<String, HashMap<String, Integer>> attributeRef = new HashMap<>();
38+
attributeRef.put("user.age", ageCondition);
39+
40+
List<HashMap<String, HashMap<String, Integer>>> andConditions = new ArrayList<>();
41+
andConditions.add(attributeRef);
42+
43+
HashMap<String, Object> conditions = new HashMap<>();
44+
conditions.put("allOf", andConditions);
45+
46+
return conditions;
47+
}
48+
49+
private static HashMap<String, Object> getResourcesetConditions() {
50+
HashMap<String, Boolean> privateCondition = new HashMap<String, Boolean>() {{
51+
put("equals", false);
52+
}};
53+
54+
HashMap<String, HashMap<String, Boolean>> attributeRef = new HashMap<>();
55+
attributeRef.put("resource.private", privateCondition);
56+
57+
List<HashMap<String, HashMap<String, Boolean>>> andConditions = new ArrayList<>();
58+
andConditions.add(attributeRef);
59+
60+
HashMap<String, Object> conditions = new HashMap<>();
61+
conditions.put("allOf", andConditions);
62+
63+
return conditions;
64+
}
65+
3666
@BeforeAll
3767
static void setup() {
3868
// resource actions
@@ -64,38 +94,14 @@ static void setup() {
6494
new ConditionSetCreate(USERSET_KEY, "Users over 30")
6595
)
6696
.withType(ConditionSetType.USERSET)
67-
.withConditions(new HashMap<String, Object>() {{
68-
put("allOf", Collections.singletonList(
69-
new HashMap<String, List<HashMap<String, HashMap<String, Integer>>>>() {{
70-
put("allOf", Collections.singletonList(
71-
new HashMap<String, HashMap<String, Integer>>() {{
72-
put("user.age", new HashMap<String, Integer>() {{
73-
put("greater-than", 30);
74-
}});
75-
}}
76-
));
77-
}}
78-
));
79-
}})
97+
.withConditions(getUsersetConditions())
8098
);
8199

82100
resourcesetData = ((
83101
new ConditionSetCreate(RESOURCESET_KEY, "Private Docs")
84102
)
85103
.withType(ConditionSetType.RESOURCESET)
86-
.withConditions(new HashMap<String, Object>() {{
87-
put("allOf", Collections.singletonList(
88-
new HashMap<String, List<HashMap<String, HashMap<String, Boolean>>>>() {{
89-
put("allOf", Collections.singletonList(
90-
new HashMap<String, HashMap<String, Boolean>>() {{
91-
put("resource.private", new HashMap<String, Boolean>() {{
92-
put("equals", false);
93-
}});
94-
}}
95-
));
96-
}}
97-
));
98-
}})
104+
.withConditions(getResourcesetConditions())
99105
);
100106

101107
ruleData = new ConditionSetRuleCreate(USERSET_KEY, DOCUMENT_KEY + ":sign", RESOURCESET_KEY);
@@ -127,7 +133,7 @@ void cleanup() {
127133
}
128134

129135
@Test
130-
void testResourcesApi() {
136+
void testConditionSetsApi() {
131137
// init the client
132138
Permit permit = new Permit(this.config);
133139
Gson gson = new Gson();
@@ -175,7 +181,7 @@ void testResourcesApi() {
175181
assertEquals(userset.name, usersetData.name);
176182
assertEquals(userset.description, usersetData.description);
177183

178-
ConditionSetRead resourceset = permit.api.conditionSets.create(resourcesetData);
184+
ConditionSetRead resourceset = permit.api.conditionSets.create(resourcesetData.withResourceId(document.id));
179185
assertNotNull(resourceset);
180186
assertNotNull(resourceset.id);
181187
assertEquals(resourceset.key, resourcesetData.key);

0 commit comments

Comments
 (0)