Skip to content
This repository has been archived by the owner on Mar 31, 2022. It is now read-only.

Commit

Permalink
feat(policy): Enable / disable a policy rule
Browse files Browse the repository at this point in the history
  • Loading branch information
brasseld authored and NicolasGeraud committed Sep 4, 2016
1 parent 0094a22 commit 91fd555
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ public Rule deserialize(JsonParser jp, DeserializationContext ctxt) throws IOExc
rule.setDescription(subNode.asText());
}
break;
case "enabled":
if (subNode != null) {
rule.setEnabled(subNode.asBoolean(true));
}
break;
default:
// We are in the case of a policy
Policy policy = new Policy();

JsonNode enabledNode = subNode.get("enabled");
if (enabledNode != null && enabledNode.isBoolean()) {
policy.setEnabled(enabledNode.asBoolean());
}

policy.setName(field);
policy.setConfiguration(subNode.toString());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public void serialize(Rule rule, JsonGenerator jgen, SerializerProvider provider
jgen.writeStringField("description", rule.getDescription());
}

jgen.writeBooleanField("enabled", rule.isEnabled());

jgen.writeEndObject();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,19 @@ public void definition_pathwithpolicies() throws Exception {
public void definition_pathwithpolicies_disabled() throws Exception {
Api api = load("/io/gravitee/definition/jackson/api-defaultpath.json", Api.class);
Map<String, Path> paths = api.getPaths();
List<Rule> rules= paths.get("/*").getRules();
List<Rule> rules = paths.get("/*").getRules();

Policy policy = rules.iterator().next().getPolicy();
Rule accessControlRule = rules.get(0);
Policy policy = accessControlRule.getPolicy();
Assert.assertNotNull(policy);

Assert.assertEquals("access-control", policy.getName());
Assert.assertFalse(policy.isEnabled());
Assert.assertFalse(accessControlRule.isEnabled());

Rule corsRule = rules.get(1);
policy = corsRule.getPolicy();
Assert.assertNotNull(policy);
Assert.assertEquals("cors", policy.getName());
Assert.assertTrue(corsRule.isEnabled());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
"/*": [
{
"methods": ["GET", "POST"],
"enabled": false,
"access-control": {
"enabled": false
}
},
{
"methods": ["GET", "POST"],
"enabled": true,
"cors": {
"enabled": false
}
},
{
Expand Down
10 changes: 0 additions & 10 deletions model/src/main/java/io/gravitee/definition/model/Policy.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
*/
public class Policy {

private boolean enabled = true;

private String name;

private String configuration;
Expand All @@ -45,14 +43,6 @@ public void setName(String name) {
this.name = name;
}

public boolean isEnabled() {
return enabled;
}

public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
14 changes: 12 additions & 2 deletions model/src/main/java/io/gravitee/definition/model/Rule.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import java.util.List;

/**
* @author David BRASSELY (brasseld at gmail.com)
* @author Gravitee.io Team
* @author David BRASSELY (david at graviteesource.com)
* @author GraviteeSource Team
*/
public class Rule {

Expand All @@ -32,6 +32,8 @@ public class Rule {

private String description;

private boolean enabled = true;

public List<HttpMethod> getMethods() {
return methods;
}
Expand All @@ -55,4 +57,12 @@ public String getDescription() {
public void setDescription(String description) {
this.description = description;
}

public boolean isEnabled() {
return enabled;
}

public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}

0 comments on commit 91fd555

Please sign in to comment.