Skip to content

Commit dcc6265

Browse files
committed
add additional tests
1 parent 6847428 commit dcc6265

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

libs/entitlement/src/test/java/org/elasticsearch/entitlement/runtime/policy/PolicyParserTests.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,28 @@ public ManyConstructorsEntitlement(String s) {}
4040
public ManyConstructorsEntitlement(int i) {}
4141
}
4242

43+
public static class ManyMethodsEntitlement implements Entitlement {
44+
@ExternalEntitlement
45+
public static ManyMethodsEntitlement create(String s) {
46+
return new ManyMethodsEntitlement();
47+
}
48+
49+
@ExternalEntitlement
50+
public static ManyMethodsEntitlement create(int i) {
51+
return new ManyMethodsEntitlement();
52+
}
53+
}
54+
55+
public static class ConstructorAndMethodEntitlement implements Entitlement {
56+
@ExternalEntitlement
57+
public static ConstructorAndMethodEntitlement create(String s) {
58+
return new ConstructorAndMethodEntitlement(s);
59+
}
60+
61+
@ExternalEntitlement
62+
public ConstructorAndMethodEntitlement(String s) {}
63+
}
64+
4365
public void testGetEntitlementTypeName() {
4466
assertEquals("create_class_loader", PolicyParser.getEntitlementTypeName(CreateClassLoaderEntitlement.class));
4567

@@ -174,4 +196,43 @@ public void testMultipleConstructorsAnnotated() throws IOException {
174196
)
175197
);
176198
}
199+
200+
public void testMultipleMethodsAnnotated() throws IOException {
201+
var parser = new PolicyParser(new ByteArrayInputStream("""
202+
entitlement-module-name:
203+
- many_methods
204+
""".getBytes(StandardCharsets.UTF_8)), "test-policy.yaml", true, Map.of("many_methods", ManyMethodsEntitlement.class));
205+
206+
var e = expectThrows(IllegalStateException.class, parser::parsePolicy);
207+
assertThat(
208+
e.getMessage(),
209+
equalTo(
210+
"entitlement class "
211+
+ "[org.elasticsearch.entitlement.runtime.policy.PolicyParserTests$ManyMethodsEntitlement]"
212+
+ " has more than one constructor and/or method annotated with ExternalEntitlement"
213+
)
214+
);
215+
}
216+
217+
public void testConstructorAndMethodAnnotated() throws IOException {
218+
var parser = new PolicyParser(
219+
new ByteArrayInputStream("""
220+
entitlement-module-name:
221+
- constructor_and_method
222+
""".getBytes(StandardCharsets.UTF_8)),
223+
"test-policy.yaml",
224+
true,
225+
Map.of("constructor_and_method", ConstructorAndMethodEntitlement.class)
226+
);
227+
228+
var e = expectThrows(IllegalStateException.class, parser::parsePolicy);
229+
assertThat(
230+
e.getMessage(),
231+
equalTo(
232+
"entitlement class "
233+
+ "[org.elasticsearch.entitlement.runtime.policy.PolicyParserTests$ConstructorAndMethodEntitlement]"
234+
+ " has more than one constructor and/or method annotated with ExternalEntitlement"
235+
)
236+
);
237+
}
177238
}

0 commit comments

Comments
 (0)