Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow operator and violation when creating policy resource #373

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,17 @@ public Response createPolicy(Policy jsonPolicy) {
try (QueryManager qm = new QueryManager()) {
Policy policy = qm.getPolicy(StringUtils.trimToNull(jsonPolicy.getName()));
if (policy == null) {
Policy.Operator operator = jsonPolicy.getOperator();
if (operator == null) {
operator = Policy.Operator.ANY;
}
Policy.ViolationState violationState = jsonPolicy.getViolationState();
if (violationState == null) {
violationState = Policy.ViolationState.INFO;
}
policy = qm.createPolicy(
StringUtils.trimToNull(jsonPolicy.getName()),
Policy.Operator.ANY, Policy.ViolationState.INFO);
operator, violationState);
return Response.status(Response.Status.CREATED).entity(policy).build();
} else {
return Response.status(Response.Status.CONFLICT).entity("A policy with the specified name already exists.").build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,50 @@ public void createPolicyTest() {
assertThat(json.getBoolean("includeChildren")).isEqualTo(false);
}

@Test
public void createPolicySpecifyOperatorAndViolationStateTest() {
final Policy policy = new Policy();
policy.setName("policy");
policy.setOperator(Policy.Operator.ALL);
policy.setViolationState(Policy.ViolationState.FAIL);

final Response response = target(V1_POLICY)
.request()
.header(X_API_KEY, apiKey)
.put(Entity.entity(policy, MediaType.APPLICATION_JSON));

assertThat(response.getStatus()).isEqualTo(201);

final JsonObject json = parseJsonObject(response);
assertThat(json).isNotNull();
assertThat(json.getString("name")).isEqualTo("policy");
assertThat(json.getString("operator")).isEqualTo("ALL");
assertThat(json.getString("violationState")).isEqualTo("FAIL");
assertThat(UuidUtil.isValidUUID(json.getString("uuid")));
assertThat(json.getBoolean("includeChildren")).isEqualTo(false);
}

@Test
public void createPolicyUseDefaultValueTest() {
final Policy policy = new Policy();
policy.setName("policy");

final Response response = target(V1_POLICY)
.request()
.header(X_API_KEY, apiKey)
.put(Entity.entity(policy, MediaType.APPLICATION_JSON));

assertThat(response.getStatus()).isEqualTo(201);

final JsonObject json = parseJsonObject(response);
assertThat(json).isNotNull();
assertThat(json.getString("name")).isEqualTo("policy");
assertThat(json.getString("operator")).isEqualTo("ANY");
assertThat(json.getString("violationState")).isEqualTo("INFO");
assertThat(UuidUtil.isValidUUID(json.getString("uuid")));
assertThat(json.getBoolean("includeChildren")).isEqualTo(false);
}

@Test
public void updatePolicyTest() {
final Policy policy = qm.createPolicy("policy", Policy.Operator.ANY, Policy.ViolationState.INFO);
Expand Down