Skip to content

Commit f5e7994

Browse files
authored
Add policy evaluation details related classes (#703)
`PolicyViolationErrorInfo` is missing `@JsonIgnoreProperties(ignoreUnknown = true)` annotation and `ObjectMapper` wasn't configured for ignoring unknown fields. This caused an exception when Azure returned `evaluationDetails` in it's response, making impossible to handle the error gracefully. In this commit: - Configured `ObjectMapper` with `DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES` to avoid issues when a new field is introduced on Azure side - created `EvaluationDetails` and subsequent classes to parse the response - added `toString` implementation for related and newly created classes for easier logging - added unit test to verify the parsing
1 parent 8fcf7e0 commit f5e7994

File tree

5 files changed

+341
-10
lines changed

5 files changed

+341
-10
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for
4+
* license information.
5+
*/
6+
7+
package com.microsoft.azure;
8+
9+
import java.util.List;
10+
11+
public class EvaluationDetails {
12+
13+
private List<ExpressionEvaluationDetails> evaluatedExpressions;
14+
15+
public List<ExpressionEvaluationDetails> getEvaluatedExpressions() {
16+
return evaluatedExpressions;
17+
}
18+
19+
@Override
20+
public String toString() {
21+
return "EvaluationDetails{"
22+
+ "evaluatedExpressions=" + evaluatedExpressions
23+
+ '}';
24+
}
25+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for
4+
* license information.
5+
*/
6+
7+
package com.microsoft.azure;
8+
9+
import java.util.List;
10+
11+
public class ExpressionEvaluationDetails {
12+
13+
private String expression;
14+
15+
private String expressionValue;
16+
17+
private String operator;
18+
19+
private String path;
20+
21+
private String result;
22+
23+
private List<String> targetValue;
24+
25+
public String getExpression() {
26+
return expression;
27+
}
28+
29+
public String getExpressionValue() {
30+
return expressionValue;
31+
}
32+
33+
public String getOperator() {
34+
return operator;
35+
}
36+
37+
public String getPath() {
38+
return path;
39+
}
40+
41+
public String getResult() {
42+
return result;
43+
}
44+
45+
public List<String> getTargetValue() {
46+
return targetValue;
47+
}
48+
49+
@Override
50+
public String toString() {
51+
return "ExpressionEvaluationDetails{"
52+
+ "expression='" + expression + '\''
53+
+ ", expressionValue='" + expressionValue + '\''
54+
+ ", operator='" + operator + '\''
55+
+ ", path='" + path + '\''
56+
+ ", result='" + result + '\''
57+
+ ", targetValue=" + targetValue
58+
+ '}';
59+
}
60+
}

azure-client-runtime/src/main/java/com/microsoft/azure/PolicyViolation.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.io.IOException;
1010

1111
import com.fasterxml.jackson.core.JsonParseException;
12+
import com.fasterxml.jackson.databind.DeserializationFeature;
1213
import com.fasterxml.jackson.databind.JsonMappingException;
1314
import com.fasterxml.jackson.databind.MapperFeature;
1415
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -22,22 +23,24 @@ public class PolicyViolation extends TypedErrorInfo {
2223
* Policy violation error details.
2324
*/
2425
private PolicyViolationErrorInfo policyErrorInfo;
25-
26+
2627
/**
2728
* Initializes a new instance of PolicyViolation.
28-
* @param type the error type
29+
*
30+
* @param type the error type
2931
* @param policyErrorInfo the error details
30-
* @throws JsonParseException if the policyErrorInfo has invalid content.
31-
* @throws JsonMappingException if the policyErrorInfo's JSON does not match the expected schema.
32-
* @throws IOException if an IO error occurs.
32+
* @throws JsonParseException if the policyErrorInfo has invalid content.
33+
* @throws JsonMappingException if the policyErrorInfo's JSON does not match the expected schema.
34+
* @throws IOException if an IO error occurs.
3335
*/
3436
public PolicyViolation(String type, ObjectNode policyErrorInfo) throws JsonParseException, JsonMappingException, IOException {
3537
super(type, policyErrorInfo);
36-
ObjectMapper objectMapper = new ObjectMapper();
37-
objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
38+
ObjectMapper objectMapper = new ObjectMapper()
39+
.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true)
40+
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
3841
this.policyErrorInfo = objectMapper.readValue(policyErrorInfo.toString(), PolicyViolationErrorInfo.class);
3942
}
40-
43+
4144
/**
4245
* @return the policy violation error details.
4346
*/

azure-client-runtime/src/main/java/com/microsoft/azure/PolicyViolationErrorInfo.java

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ public class PolicyViolationErrorInfo {
7878
* The policy set definition display name.
7979
*/
8080
private String policySetDefinitionDisplayName;
81-
81+
82+
private EvaluationDetails evaluationDetails;
83+
8284
/**
8385
* @return the policy definition id.
8486
*/
@@ -170,17 +172,48 @@ public String getPolicySetDefinitionDisplayName() {
170172
return policySetDefinitionDisplayName;
171173
}
172174

175+
public EvaluationDetails getEvaluationDetails() {
176+
return evaluationDetails;
177+
}
178+
179+
@Override
180+
public String toString() {
181+
return "PolicyViolationErrorInfo{"
182+
+ "policyDefinitionId='" + policyDefinitionId + '\''
183+
+ ", policySetDefinitionId='" + policySetDefinitionId + '\''
184+
+ ", policyDefinitionReferenceId='" + policyDefinitionReferenceId + '\''
185+
+ ", policySetDefinitionName='" + policySetDefinitionName + '\''
186+
+ ", policyDefinitionName='" + policyDefinitionName + '\''
187+
+ ", policyDefinitionEffect='" + policyDefinitionEffect + '\''
188+
+ ", policyAssignmentId='" + policyAssignmentId + '\''
189+
+ ", policyAssignmentName='" + policyAssignmentName + '\''
190+
+ ", policyAssignmentDisplayName='" + policyAssignmentDisplayName + '\''
191+
+ ", policyAssignmentScope='" + policyAssignmentScope + '\''
192+
+ ", policyAssignmentParameters=" + policyAssignmentParameters
193+
+ ", policyDefinitionDisplayName='" + policyDefinitionDisplayName + '\''
194+
+ ", policySetDefinitionDisplayName='" + policySetDefinitionDisplayName + '\''
195+
+ ", evaluationDetails=" + evaluationDetails
196+
+ '}';
197+
}
198+
173199
/**
174200
* An instance of this class provides policy parameter value.
175201
*/
176202
public static class PolicyParameter {
177203
private JsonNode value;
178-
204+
179205
/**
180206
* @return the parameter value.
181207
*/
182208
public JsonNode getValue() {
183209
return value;
184210
}
211+
212+
@Override
213+
public String toString() {
214+
return "PolicyParameter{"
215+
+ "value=" + value
216+
+ '}';
217+
}
185218
}
186219
}

0 commit comments

Comments
 (0)