1   /*
2    * Copyright 2018 LINE Corporation
3    *
4    * LINE Corporation licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package com.linecorp.centraldogma.internal.jsonpatch;
18  
19  import java.io.IOException;
20  
21  import com.fasterxml.jackson.annotation.JsonCreator;
22  import com.fasterxml.jackson.annotation.JsonProperty;
23  import com.fasterxml.jackson.core.JsonGenerator;
24  import com.fasterxml.jackson.core.JsonPointer;
25  import com.fasterxml.jackson.databind.JsonNode;
26  import com.fasterxml.jackson.databind.SerializerProvider;
27  import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
28  
29  public final class TestAbsenceOperation extends JsonPatchOperation {
30      @JsonCreator
31      public TestAbsenceOperation(@JsonProperty("path") final JsonPointer path) {
32          super("testAbsence", path);
33      }
34  
35      @Override
36      JsonNode apply(JsonNode node) {
37          final JsonNode found = node.at(path);
38          if (!found.isMissingNode()) {
39              throw new JsonPatchException("existent path: " + path);
40          }
41          return node;
42      }
43  
44      @Override
45      public void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException {
46          gen.writeStartObject();
47          gen.writeStringField("op", op);
48          gen.writeStringField("path", path.toString());
49          gen.writeEndObject();
50      }
51  
52      @Override
53      public void serializeWithType(JsonGenerator gen, SerializerProvider serializers, TypeSerializer typeSer)
54              throws IOException {
55          serialize(gen, serializers);
56      }
57  
58      @Override
59      public String toString() {
60          return "op: " + op + "; path: \"" + path + '"';
61      }
62  }