|
| 1 | +/* |
| 2 | + * Licensed to Metamarkets Group Inc. (Metamarkets) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. Metamarkets licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. 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, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package io.druid.indexer; |
| 21 | + |
| 22 | +import com.fasterxml.jackson.core.JsonParser; |
| 23 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 24 | +import com.fasterxml.jackson.core.JsonToken; |
| 25 | +import com.fasterxml.jackson.databind.DeserializationContext; |
| 26 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 27 | +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; |
| 28 | +import com.fasterxml.jackson.databind.module.SimpleModule; |
| 29 | +import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; |
| 30 | +import io.druid.java.util.common.DateTimes; |
| 31 | +import org.joda.time.DateTime; |
| 32 | +import org.junit.Assert; |
| 33 | +import org.junit.Test; |
| 34 | + |
| 35 | +import java.io.IOException; |
| 36 | + |
| 37 | +public class TaskStatusPlusTest |
| 38 | +{ |
| 39 | + @Test |
| 40 | + public void testSerde() throws IOException |
| 41 | + { |
| 42 | + final ObjectMapper mapper = new ObjectMapper(); |
| 43 | + mapper.registerModule( |
| 44 | + new SimpleModule() |
| 45 | + .addDeserializer(DateTime.class, new DateTimeDeserializer()) |
| 46 | + .addSerializer(DateTime.class, ToStringSerializer.instance) |
| 47 | + ); |
| 48 | + final TaskStatusPlus status = new TaskStatusPlus( |
| 49 | + "testId", |
| 50 | + DateTimes.nowUtc(), |
| 51 | + DateTimes.nowUtc(), |
| 52 | + TaskState.RUNNING, |
| 53 | + 1000L, |
| 54 | + TaskLocation.create("testHost", 1010, -1) |
| 55 | + ); |
| 56 | + final String json = mapper.writeValueAsString(status); |
| 57 | + Assert.assertEquals(status, mapper.readValue(json, TaskStatusPlus.class)); |
| 58 | + } |
| 59 | + |
| 60 | + // Copied from io.druid.jackson.JodaStuff |
| 61 | + private static class DateTimeDeserializer extends StdDeserializer<DateTime> |
| 62 | + { |
| 63 | + public DateTimeDeserializer() |
| 64 | + { |
| 65 | + super(DateTime.class); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public DateTime deserialize(JsonParser jp, DeserializationContext ctxt) |
| 70 | + throws IOException, JsonProcessingException |
| 71 | + { |
| 72 | + JsonToken t = jp.getCurrentToken(); |
| 73 | + if (t == JsonToken.VALUE_NUMBER_INT) { |
| 74 | + return DateTimes.utc(jp.getLongValue()); |
| 75 | + } |
| 76 | + if (t == JsonToken.VALUE_STRING) { |
| 77 | + String str = jp.getText().trim(); |
| 78 | + if (str.length() == 0) { // [JACKSON-360] |
| 79 | + return null; |
| 80 | + } |
| 81 | + // make sure to preserve time zone information when parsing timestamps |
| 82 | + return DateTimes.ISO_DATE_OR_TIME_WITH_OFFSET.parse(str); |
| 83 | + } |
| 84 | + throw ctxt.mappingException(getValueClass()); |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments