Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
Merge pull request #17 from 1and1/FixBrokenJsonResponse
Browse files Browse the repository at this point in the history
Fixes broken json return message on latest-revision-since.
  • Loading branch information
netdevfighter authored May 18, 2017
2 parents 24e98d6 + 5407ca0 commit 4a5f070
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.oneandone</groupId>
<artifactId>go-maven-poller</artifactId>
<version>1.2.0</version>
<version>1.2.1</version>
<url>https://github.com/1and1/go-maven-poller</url>

<licenses>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class JsonUtil {

private static final DateTimeFormatter dateTomeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX");
private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

/**
* This method serializes the specified object into its equivalent Json representation.
Expand All @@ -21,7 +21,7 @@ public class JsonUtil {
*/
public static String toJsonString(final Object object) {
final GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(ZonedDateTime.class, new ZonedDateTimeConverter(dateTomeFormatter));
gsonBuilder.registerTypeAdapter(ZonedDateTime.class, new ZonedDateTimeConverter(dateTimeFormatter));
return gsonBuilder.create().toJson(object);
}

Expand All @@ -36,7 +36,7 @@ public static String toJsonString(final Object object) {
*/
public static <T> T fromJsonString(final String json, final Class<T> type) {
final GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(ZonedDateTime.class, new ZonedDateTimeConverter(dateTomeFormatter));
gsonBuilder.registerTypeAdapter(ZonedDateTime.class, new ZonedDateTimeConverter(dateTimeFormatter));
return gsonBuilder.create().fromJson(json, type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.google.gson.*;

import java.lang.reflect.Type;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

Expand All @@ -18,7 +20,7 @@ class ZonedDateTimeConverter implements JsonSerializer<ZonedDateTime>, JsonDeser
/** Format specifier. */
private final DateTimeFormatter dateTimeFormatter;

public ZonedDateTimeConverter(final DateTimeFormatter dateTimeFormatter) {
ZonedDateTimeConverter(final DateTimeFormatter dateTimeFormatter) {
Preconditions.checkArgument(dateTimeFormatter != null, "dateTimeFormatter is null");
this.dateTimeFormatter = dateTimeFormatter;
}
Expand All @@ -33,7 +35,8 @@ public ZonedDateTime deserialize(JsonElement jsonElement, Type type, JsonDeseria
if (jsonElement.getAsString() == null || jsonElement.getAsString().isEmpty()) {
return null;
}

return ZonedDateTime.parse(jsonElement.getAsString(), dateTimeFormatter);
//No timezone is provided in message -> need to add the default
final LocalDateTime withoutTimezone = LocalDateTime.parse(jsonElement.getAsString(), dateTimeFormatter);
return ZonedDateTime.of(withoutTimezone, ZoneId.systemDefault());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import com.oneandone.go.plugin.maven.util.JsonUtil;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class ConfigurationMessageTest {

@Test
public void testDeserialization() throws Exception {
public void testDeserializationSerialization() throws Exception {
final ConfigurationMessage configurationMessage = JsonUtil.fromJsonString(
"{\n" +
" \"repository-configuration\": {\n" +
Expand Down Expand Up @@ -39,7 +40,9 @@ public void testDeserialization() throws Exception {

assertNotNull(configurationMessage);
assertNotNull(configurationMessage.getPreviousRevision());
assertEquals("0.0.6-SNAPSHOT", configurationMessage.getPreviousRevision().getRevision());
assertNotNull(configurationMessage.getPreviousRevision().getTimestamp());
assertEquals("\"2015-04-09T11:08:52.209Z\"", JsonUtil.toJsonString(configurationMessage.getPreviousRevision().getTimestamp()));
}

}

0 comments on commit 4a5f070

Please sign in to comment.