mirror of https://github.com/halo-dev/halo
refactor: policy rule constructor to avoid null values (#2131)
* refactor: policy rule constructor to avoid null values * fix: unit test casepull/2138/head
parent
2c057f4fe1
commit
19db04c430
|
@ -1,8 +1,6 @@
|
||||||
package run.halo.app.identity.authorization;
|
package run.halo.app.identity.authorization;
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* PolicyRule holds information that describes a policy rule, but does not contain information
|
* PolicyRule holds information that describes a policy rule, but does not contain information
|
||||||
|
@ -12,8 +10,6 @@ import lombok.NoArgsConstructor;
|
||||||
* @since 2.0.0
|
* @since 2.0.0
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
|
||||||
@NoArgsConstructor
|
|
||||||
public class PolicyRule {
|
public class PolicyRule {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -53,6 +49,22 @@ public class PolicyRule {
|
||||||
*/
|
*/
|
||||||
String[] verbs;
|
String[] verbs;
|
||||||
|
|
||||||
|
public PolicyRule(String[] apiGroups, String[] resources, String[] resourceNames,
|
||||||
|
String[] nonResourceURLs, String[] verbs) {
|
||||||
|
this.apiGroups = nullElseEmpty(apiGroups);
|
||||||
|
this.resources = nullElseEmpty(resources);
|
||||||
|
this.resourceNames = nullElseEmpty(resourceNames);
|
||||||
|
this.nonResourceURLs = nullElseEmpty(nonResourceURLs);
|
||||||
|
this.verbs = nullElseEmpty(verbs);
|
||||||
|
}
|
||||||
|
|
||||||
|
String[] nullElseEmpty(String... items) {
|
||||||
|
if (items == null) {
|
||||||
|
return new String[] {};
|
||||||
|
}
|
||||||
|
return items;
|
||||||
|
}
|
||||||
|
|
||||||
public static class Builder {
|
public static class Builder {
|
||||||
String[] apiGroups;
|
String[] apiGroups;
|
||||||
String[] resources;
|
String[] resources;
|
||||||
|
@ -85,21 +97,8 @@ public class PolicyRule {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
String[] nullElseEmpty(String... items) {
|
|
||||||
if (items == null) {
|
|
||||||
return new String[] {};
|
|
||||||
}
|
|
||||||
return items;
|
|
||||||
}
|
|
||||||
|
|
||||||
public PolicyRule build() {
|
public PolicyRule build() {
|
||||||
return new PolicyRule(
|
return new PolicyRule(apiGroups, resources, resourceNames, nonResourceURLs, verbs);
|
||||||
nullElseEmpty(apiGroups),
|
|
||||||
nullElseEmpty(resources),
|
|
||||||
nullElseEmpty(resourceNames),
|
|
||||||
nullElseEmpty(nonResourceURLs),
|
|
||||||
nullElseEmpty(verbs)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,72 @@
|
||||||
|
package run.halo.app.identity.authorization;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import run.halo.app.infra.utils.JsonUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests for {@link PolicyRule}.
|
||||||
|
*
|
||||||
|
* @author guqing
|
||||||
|
* @since 2.0.0
|
||||||
|
*/
|
||||||
|
class PolicyRuleTest {
|
||||||
|
private ObjectMapper objectMapper;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
objectMapper = JsonUtils.DEFAULT_JSON_MAPPER;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void constructPolicyRule() throws JsonProcessingException {
|
||||||
|
PolicyRule policyRule = new PolicyRule(null, null, null, null, null);
|
||||||
|
assertThat(policyRule).isNotNull();
|
||||||
|
JsonNode policyRuleJson = objectMapper.valueToTree(policyRule);
|
||||||
|
assertThat(policyRuleJson).isEqualTo(objectMapper.readTree("""
|
||||||
|
{"apiGroups":[],"resources":[],"resourceNames":[],"nonResourceURLs":[],"verbs":[]}
|
||||||
|
"""));
|
||||||
|
|
||||||
|
PolicyRule policyByBuilder = new PolicyRule.Builder().build();
|
||||||
|
JsonNode policyByBuilderJson = objectMapper.valueToTree(policyByBuilder);
|
||||||
|
assertThat(policyByBuilderJson).isEqualTo(objectMapper.readTree("""
|
||||||
|
{"apiGroups":[],"resources":[],"resourceNames":[],"nonResourceURLs":[],"verbs":[]}
|
||||||
|
"""));
|
||||||
|
|
||||||
|
PolicyRule policyNonNull = new PolicyRule.Builder()
|
||||||
|
.apiGroups("group")
|
||||||
|
.resources("resource-1", "resource-2")
|
||||||
|
.resourceNames("resourceName")
|
||||||
|
.nonResourceURLs("non resource url")
|
||||||
|
.verbs("verbs")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
JsonNode expected = objectMapper.readTree("""
|
||||||
|
{
|
||||||
|
"apiGroups": [
|
||||||
|
"group"
|
||||||
|
],
|
||||||
|
"resources": [
|
||||||
|
"resource-1",
|
||||||
|
"resource-2"
|
||||||
|
],
|
||||||
|
"resourceNames": [
|
||||||
|
"resourceName"
|
||||||
|
],
|
||||||
|
"nonResourceURLs": [
|
||||||
|
"non resource url"
|
||||||
|
],
|
||||||
|
"verbs": [
|
||||||
|
"verbs"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
JsonNode policyNonNullJson = objectMapper.valueToTree(policyNonNull);
|
||||||
|
assertThat(policyNonNullJson).isEqualTo(expected);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue