|
| 1 | +/* |
| 2 | + * DISCLAIMER |
| 3 | + * |
| 4 | + * Copyright 2016 ArangoDB GmbH, Cologne, Germany |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + * |
| 18 | + * Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | + */ |
| 20 | + |
| 21 | +package com.arangodb.serde; |
| 22 | + |
| 23 | + |
| 24 | +import com.arangodb.*; |
| 25 | +import com.arangodb.config.ConfigUtils; |
| 26 | +import com.arangodb.internal.serde.InternalSerde; |
| 27 | +import com.arangodb.model.DocumentCreateOptions; |
| 28 | +import com.arangodb.serde.jackson.JacksonSerde; |
| 29 | +import com.fasterxml.jackson.core.JsonGenerator; |
| 30 | +import com.fasterxml.jackson.core.JsonParser; |
| 31 | +import com.fasterxml.jackson.databind.*; |
| 32 | +import com.fasterxml.jackson.databind.annotation.JsonSerialize; |
| 33 | +import com.fasterxml.jackson.databind.module.SimpleModule; |
| 34 | +import org.junit.jupiter.api.AfterAll; |
| 35 | +import org.junit.jupiter.api.BeforeAll; |
| 36 | +import org.junit.jupiter.api.Test; |
| 37 | + |
| 38 | +import java.io.IOException; |
| 39 | +import java.math.BigInteger; |
| 40 | +import java.util.Collections; |
| 41 | +import java.util.HashMap; |
| 42 | +import java.util.Map; |
| 43 | +import java.util.UUID; |
| 44 | +import java.util.concurrent.ExecutionException; |
| 45 | + |
| 46 | +import static com.fasterxml.jackson.databind.DeserializationFeature.USE_BIG_INTEGER_FOR_INTS; |
| 47 | +import static com.fasterxml.jackson.databind.SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED; |
| 48 | +import static org.assertj.core.api.Assertions.assertThat; |
| 49 | + |
| 50 | + |
| 51 | +/** |
| 52 | + * @author Michele Rastelli |
| 53 | + */ |
| 54 | +class CustomSerdeAsyncTest { |
| 55 | + |
| 56 | + private static final String COLLECTION_NAME = "collection"; |
| 57 | + private static final String PERSON_SERIALIZER_ADDED_PREFIX = "MyNameIs"; |
| 58 | + private static final String PERSON_DESERIALIZER_ADDED_PREFIX = "Hello"; |
| 59 | + |
| 60 | + private static ArangoDBAsync arangoDB; |
| 61 | + private static ArangoDatabaseAsync db; |
| 62 | + private static ArangoCollectionAsync collection; |
| 63 | + |
| 64 | + @BeforeAll |
| 65 | + static void init() throws ExecutionException, InterruptedException { |
| 66 | + JacksonSerde serde = JacksonSerde.of(ContentType.JSON) |
| 67 | + .configure((mapper) -> { |
| 68 | + mapper.configure(WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED, true); |
| 69 | + mapper.configure(USE_BIG_INTEGER_FOR_INTS, true); |
| 70 | + SimpleModule module = new SimpleModule("PersonModule"); |
| 71 | + module.addDeserializer(Person.class, new PersonDeserializer()); |
| 72 | + mapper.registerModule(module); |
| 73 | + }); |
| 74 | + arangoDB = new ArangoDB.Builder() |
| 75 | + .loadProperties(ConfigUtils.loadConfig()) |
| 76 | + .serde(serde) |
| 77 | + .protocol(Protocol.HTTP_JSON) |
| 78 | + .build() |
| 79 | + .async(); |
| 80 | + |
| 81 | + db = arangoDB.db("custom-serde-test"); |
| 82 | + if (!db.exists().get()) { |
| 83 | + db.create().get(); |
| 84 | + } |
| 85 | + |
| 86 | + collection = db.collection(COLLECTION_NAME); |
| 87 | + if (!collection.exists().get()) { |
| 88 | + collection.create().get(); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @AfterAll |
| 93 | + static void shutdown() throws ExecutionException, InterruptedException { |
| 94 | + if (db.exists().get()) |
| 95 | + db.drop().get(); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + void customPersonDeserializer() throws ExecutionException, InterruptedException { |
| 100 | + Person person = new Person(); |
| 101 | + person.name = "Joe"; |
| 102 | + Person result = collection.insertDocument( |
| 103 | + person, |
| 104 | + new DocumentCreateOptions().returnNew(true) |
| 105 | + ).get().getNew(); |
| 106 | + assertThat(result.name).isEqualTo(PERSON_DESERIALIZER_ADDED_PREFIX + PERSON_SERIALIZER_ADDED_PREFIX + person.name); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + void manualCustomPersonDeserializer() { |
| 111 | + Person person = new Person(); |
| 112 | + person.name = "Joe"; |
| 113 | + InternalSerde serialization = arangoDB.getSerde(); |
| 114 | + byte[] serialized = serialization.serializeUserData(person); |
| 115 | + Person deserializedPerson = serialization.deserializeUserData(serialized, Person.class); |
| 116 | + assertThat(deserializedPerson.name).isEqualTo(PERSON_DESERIALIZER_ADDED_PREFIX + PERSON_SERIALIZER_ADDED_PREFIX + person.name); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + void aqlSerialization() throws ExecutionException, InterruptedException { |
| 121 | + String key = "test-" + UUID.randomUUID(); |
| 122 | + |
| 123 | + Map<String, Object> doc = new HashMap<>(); |
| 124 | + doc.put("_key", key); |
| 125 | + doc.put("arr", Collections.singletonList("hello")); |
| 126 | + doc.put("int", 10); |
| 127 | + |
| 128 | + HashMap<String, Object> params = new HashMap<>(); |
| 129 | + params.put("doc", doc); |
| 130 | + params.put("@collection", COLLECTION_NAME); |
| 131 | + |
| 132 | + Map<String, Object> result = db.query( |
| 133 | + "INSERT @doc INTO @@collection RETURN NEW", |
| 134 | + Map.class, |
| 135 | + params |
| 136 | + ).get().getResult().get(0); |
| 137 | + |
| 138 | + assertThat(result.get("arr")).isInstanceOf(String.class); |
| 139 | + assertThat(result.get("arr")).isEqualTo("hello"); |
| 140 | + assertThat(result.get("int")).isInstanceOf(BigInteger.class); |
| 141 | + assertThat(result.get("int")).isEqualTo(BigInteger.valueOf(10)); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + void aqlDeserialization() throws ExecutionException, InterruptedException { |
| 146 | + String key = "test-" + UUID.randomUUID(); |
| 147 | + |
| 148 | + Map<String, Object> doc = new HashMap<>(); |
| 149 | + doc.put("_key", key); |
| 150 | + doc.put("arr", Collections.singletonList("hello")); |
| 151 | + doc.put("int", 10); |
| 152 | + |
| 153 | + collection.insertDocument(doc); |
| 154 | + |
| 155 | + final Map<String, Object> result = db.query( |
| 156 | + "RETURN DOCUMENT(@docId)", |
| 157 | + Map.class, |
| 158 | + Collections.singletonMap("docId", COLLECTION_NAME + "/" + key) |
| 159 | + ).get().getResult().get(0); |
| 160 | + |
| 161 | + assertThat(result.get("arr")).isInstanceOf(String.class); |
| 162 | + assertThat(result.get("arr")).isEqualTo("hello"); |
| 163 | + assertThat(result.get("int")).isInstanceOf(BigInteger.class); |
| 164 | + assertThat(result.get("int")).isEqualTo(BigInteger.valueOf(10)); |
| 165 | + } |
| 166 | + |
| 167 | + @Test |
| 168 | + void insertDocument() throws ExecutionException, InterruptedException { |
| 169 | + String key = "test-" + UUID.randomUUID(); |
| 170 | + |
| 171 | + Map<String, Object> doc = new HashMap<>(); |
| 172 | + doc.put("_key", key); |
| 173 | + doc.put("arr", Collections.singletonList("hello")); |
| 174 | + doc.put("int", 10); |
| 175 | + |
| 176 | + Map<String, Object> result = collection.insertDocument( |
| 177 | + doc, |
| 178 | + new DocumentCreateOptions().returnNew(true) |
| 179 | + ).get().getNew(); |
| 180 | + |
| 181 | + assertThat(result.get("arr")).isInstanceOf(String.class); |
| 182 | + assertThat(result.get("arr")).isEqualTo("hello"); |
| 183 | + assertThat(result.get("int")).isInstanceOf(BigInteger.class); |
| 184 | + assertThat(result.get("int")).isEqualTo(BigInteger.valueOf(10)); |
| 185 | + } |
| 186 | + |
| 187 | + @Test |
| 188 | + void getDocument() throws ExecutionException, InterruptedException { |
| 189 | + String key = "test-" + UUID.randomUUID(); |
| 190 | + |
| 191 | + Map<String, Object> doc = new HashMap<>(); |
| 192 | + doc.put("_key", key); |
| 193 | + doc.put("arr", Collections.singletonList("hello")); |
| 194 | + doc.put("int", 10); |
| 195 | + |
| 196 | + collection.insertDocument(doc).get(); |
| 197 | + |
| 198 | + final Map<String, Object> result = db.collection(COLLECTION_NAME).getDocument( |
| 199 | + key, |
| 200 | + Map.class, |
| 201 | + null).get(); |
| 202 | + |
| 203 | + assertThat(result.get("arr")).isInstanceOf(String.class); |
| 204 | + assertThat(result.get("arr")).isEqualTo("hello"); |
| 205 | + assertThat(result.get("int")).isInstanceOf(BigInteger.class); |
| 206 | + assertThat(result.get("int")).isEqualTo(BigInteger.valueOf(10)); |
| 207 | + } |
| 208 | + |
| 209 | + @Test |
| 210 | + void parseNullString() { |
| 211 | + final String json = arangoDB.getSerde().deserializeUserData(arangoDB.getSerde().serializeUserData(null), |
| 212 | + String.class); |
| 213 | + assertThat(json).isNull(); |
| 214 | + } |
| 215 | + |
| 216 | + static class PersonSerializer extends JsonSerializer<Person> { |
| 217 | + @Override |
| 218 | + public void serialize(Person value, JsonGenerator gen, SerializerProvider serializers) throws IOException { |
| 219 | + gen.writeStartObject(); |
| 220 | + gen.writeFieldName("name"); |
| 221 | + gen.writeString(PERSON_SERIALIZER_ADDED_PREFIX + value.name); |
| 222 | + gen.writeEndObject(); |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + static class PersonDeserializer extends JsonDeserializer<Person> { |
| 227 | + @Override |
| 228 | + public Person deserialize(JsonParser parser, DeserializationContext ctx) throws IOException { |
| 229 | + Person person = new Person(); |
| 230 | + JsonNode rootNode = parser.getCodec().readTree(parser); |
| 231 | + JsonNode nameNode = rootNode.get("name"); |
| 232 | + if (nameNode != null && nameNode.isTextual()) { |
| 233 | + person.name = PERSON_DESERIALIZER_ADDED_PREFIX + nameNode.asText(); |
| 234 | + } |
| 235 | + return person; |
| 236 | + } |
| 237 | + } |
| 238 | + |
| 239 | + @JsonSerialize(using = PersonSerializer.class) |
| 240 | + static class Person { |
| 241 | + String name; |
| 242 | + } |
| 243 | + |
| 244 | +} |
0 commit comments