Search in sources :

Example 1 with Patch

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);
    }
}
Also used : TypeReference(com.fasterxml.jackson.core.type.TypeReference) IOException(java.io.IOException) Patch(com.enonic.kubernetes.operator.api.mutation.Patch) AdmissionReview(io.fabric8.kubernetes.api.model.admission.v1.AdmissionReview)

Example 2 with Patch

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;
}
Also used : StringReader(java.io.StringReader) JsonValue(javax.json.JsonValue) JsonPatchBuilder(javax.json.JsonPatchBuilder) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject) KubernetesResource(io.fabric8.kubernetes.api.model.KubernetesResource) Patch(com.enonic.kubernetes.operator.api.mutation.Patch) JsonStructure(javax.json.JsonStructure)

Aggregations

Patch (com.enonic.kubernetes.operator.api.mutation.Patch)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 KubernetesResource (io.fabric8.kubernetes.api.model.KubernetesResource)1 AdmissionReview (io.fabric8.kubernetes.api.model.admission.v1.AdmissionReview)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 JsonObject (javax.json.JsonObject)1 JsonPatchBuilder (javax.json.JsonPatchBuilder)1 JsonReader (javax.json.JsonReader)1 JsonStructure (javax.json.JsonStructure)1 JsonValue (javax.json.JsonValue)1