use of com.enonic.kubernetes.operator.api.mutation.Patch in project xp-operator by enonic.
the class TestApi method runTest.
@SuppressWarnings("unchecked")
private void runTest(File file) {
try {
TestFile test = mapper.readValue(file, TestFile.class);
if (test.disabled()) {
return;
}
AdmissionReview review = mutationApi.validate(test.admissionRequest());
List<Patch> patch = null;
if (review.getResponse().getPatch() != null) {
String patchDecoded = new String(BaseEncoding.base64().decode(review.getResponse().getPatch()));
patch = mapper.readValue(patchDecoded, new TypeReference<List<Patch>>() {
});
review = applyPatch(patch, review);
}
review = admissionApi.validate(review);
Assertions.assertEquals(test.assertException(), review.getResponse().getStatus().getMessage(), "Exception does not match");
if (review.getResponse().getStatus().getMessage() == null) {
Assertions.assertEquals(mapper.writeValueAsString(test.assertResult()), mapper.writeValueAsString(review.getRequest().getObject()), "Result does not match");
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of com.enonic.kubernetes.operator.api.mutation.Patch in project xp-operator by enonic.
the class TestApi method applyPatch.
private AdmissionReview applyPatch(final List<Patch> patches, final AdmissionReview review) throws JsonProcessingException {
JsonPatchBuilder jsonPatchBuilder = Json.createPatchBuilder();
for (Patch patch : patches) {
// Convert value into JsonValue
JsonReader jsonReader = Json.createReader(new StringReader(jsonMapper.writeValueAsString(patch)));
JsonObject p = jsonReader.readObject();
jsonReader.close();
JsonValue val = p.getValue("/value");
switch(patch.op()) {
case "add":
jsonPatchBuilder.add(patch.path(), val);
break;
case "replace":
jsonPatchBuilder.replace(patch.path(), val);
break;
case "remove":
jsonPatchBuilder.remove(patch.path());
break;
}
}
// Apply the patch
JsonReader reader = Json.createReader(new StringReader(jsonMapper.writeValueAsString(review.getRequest().getObject())));
JsonStructure json = reader.read();
json = jsonPatchBuilder.build().apply(json);
reader.close();
review.getRequest().setObject(jsonMapper.readValue(json.toString(), KubernetesResource.class));
review.setResponse(null);
return review;
}
Aggregations