|
| 1 | +/* |
| 2 | + * Copyright 2021 The Dapr Authors |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * Unless required by applicable law or agreed to in writing, software |
| 8 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | + * See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | + |
| 14 | +package io.dapr.serializer; |
| 15 | + |
| 16 | +import com.fasterxml.jackson.annotation.JsonInclude; |
| 17 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 18 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | +import com.fasterxml.jackson.databind.SerializationFeature; |
| 20 | +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; |
| 21 | +import com.google.common.base.Objects; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import java.io.Serializable; |
| 26 | +import java.time.ZoneId; |
| 27 | +import java.time.ZonedDateTime; |
| 28 | + |
| 29 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 30 | +import static org.junit.jupiter.api.Assertions.fail; |
| 31 | + |
| 32 | +public class CustomObjectSerializerTest { |
| 33 | + |
| 34 | + private static final DaprObjectSerializer SERIALIZER = |
| 35 | + ObjectSerializerFactory.createJacksonSerializer(new ObjectMapper() |
| 36 | + .registerModule(new JavaTimeModule()) |
| 37 | + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) |
| 38 | + .setSerializationInclusion(JsonInclude.Include.NON_NULL) |
| 39 | + .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)); |
| 40 | + |
| 41 | + public static class ObjectForTesting implements Serializable { |
| 42 | + private ZonedDateTime time; |
| 43 | + |
| 44 | + public ZonedDateTime getTime() { |
| 45 | + return time; |
| 46 | + } |
| 47 | + |
| 48 | + public void setTime(ZonedDateTime time) { |
| 49 | + this.time = time; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public boolean equals(Object o) { |
| 54 | + if (o == null || getClass() != o.getClass()) { |
| 55 | + return false; |
| 56 | + } |
| 57 | + ObjectForTesting that = (ObjectForTesting) o; |
| 58 | + return Objects.equal(time, that.time); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public int hashCode() { |
| 63 | + return Objects.hashCode(time); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void serializeObjectForTesting() { |
| 69 | + ObjectForTesting obj = new ObjectForTesting(); |
| 70 | + obj.setTime(ZonedDateTime.of(1900, 1, 1, 1, 1, 0, 0, ZoneId.of("UTC"))); |
| 71 | + String expectedResult = |
| 72 | + "{\"time\":\"1900-01-01T01:01:00Z\"}"; |
| 73 | + |
| 74 | + String serializedValue; |
| 75 | + try { |
| 76 | + serializedValue = new String(SERIALIZER.serialize(obj)); |
| 77 | + assertEquals(expectedResult, serializedValue, |
| 78 | + "FOUND:[[" + serializedValue + "]] \n but was EXPECTING: [[" + expectedResult + "]]"); |
| 79 | + } catch (IOException exception) { |
| 80 | + fail(exception.getMessage()); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments