diff options
author | Jim Hahn <jrh3@att.com> | 2020-05-15 14:46:15 -0400 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2020-05-15 14:52:15 -0400 |
commit | 5210055db2f00f42fea4717b2726dda52e1c1a59 (patch) | |
tree | 435bf700d4f5096b23126f899ab36fe3cc0de888 /models-interactions/model-impl/so | |
parent | afe5abed95c6cbfe239066e267d9c10a71f20623 (diff) |
Cannot parse finishTime in legacy SO responses
Adding the actual sample responses to the SO simulator broke the
drools-apps junit for usecases. Fixed (in theory) by updating the
legacy SO actor to properly decode the finishTime. Refactored the
new SO actor, extracting the type adapter into its own class file
so it could be shared between the new and legacy actors.
Issue-ID: POLICY-2570
Change-Id: I061b603172440b1a91da16d09b4f2a0d289dfc41
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'models-interactions/model-impl/so')
2 files changed, 73 insertions, 2 deletions
diff --git a/models-interactions/model-impl/so/src/main/java/org/onap/policy/so/util/Serialization.java b/models-interactions/model-impl/so/src/main/java/org/onap/policy/so/util/Serialization.java index 7224e665a..78506380d 100644 --- a/models-interactions/model-impl/so/src/main/java/org/onap/policy/so/util/Serialization.java +++ b/models-interactions/model-impl/so/src/main/java/org/onap/policy/so/util/Serialization.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * mso * ================================================================================ - * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2019 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -23,11 +23,14 @@ package org.onap.policy.so.util; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import java.time.LocalDateTime; public final class Serialization { public static final Gson gsonPretty = - new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create(); + new GsonBuilder().disableHtmlEscaping().setPrettyPrinting() + .registerTypeAdapter(LocalDateTime.class, new SoLocalDateTimeTypeAdapter()) + .create(); private Serialization() { // utility class with explicit private constructor diff --git a/models-interactions/model-impl/so/src/main/java/org/onap/policy/so/util/SoLocalDateTimeTypeAdapter.java b/models-interactions/model-impl/so/src/main/java/org/onap/policy/so/util/SoLocalDateTimeTypeAdapter.java new file mode 100644 index 000000000..a0e835ffb --- /dev/null +++ b/models-interactions/model-impl/so/src/main/java/org/onap/policy/so/util/SoLocalDateTimeTypeAdapter.java @@ -0,0 +1,68 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.so.util; + +import com.google.gson.JsonParseException; +import com.google.gson.TypeAdapter; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonToken; +import com.google.gson.stream.JsonWriter; +import java.io.IOException; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; + +/* + * TODO: combine the functionality of this adapter with existing LocalDateTimeTypeAdapter and eliminate this class. + */ + +/** + * GSON Type Adapter for "LocalDateTime" fields, that uses the standard RFC_1123_DATE_TIME + * formatter. + */ +public class SoLocalDateTimeTypeAdapter extends TypeAdapter<LocalDateTime> { + private static final DateTimeFormatter FORMATTER = DateTimeFormatter.RFC_1123_DATE_TIME; + + @Override + public LocalDateTime read(JsonReader in) throws IOException { + try { + if (in.peek() == JsonToken.NULL) { + in.nextNull(); + return null; + } else { + return LocalDateTime.parse(in.nextString(), FORMATTER); + } + + } catch (DateTimeParseException e) { + throw new JsonParseException("invalid date", e); + } + } + + @Override + public void write(JsonWriter out, LocalDateTime value) throws IOException { + if (value == null) { + out.nullValue(); + } else { + String text = value.format(FORMATTER); + out.value(text); + } + } +} |