diff options
author | sebdet <sebastien.determe@intl.att.com> | 2019-03-02 00:17:46 +0100 |
---|---|---|
committer | sebdet <sebastien.determe@intl.att.com> | 2019-03-02 02:00:43 +0100 |
commit | 75c0f1dd0b0b9700a3f235611131fdc500e10eb0 (patch) | |
tree | 8a09aac5249bd3ea6218a2c8f640c41975218390 /src/main/java/org/onap | |
parent | c2bcf2a333a66d401c6afe0589c117f0986a5bd2 (diff) |
Improve tests
Improve tests and equals/hashcode of each model objects + fix Instant
bug of LoopLog
Issue-ID: CLAMP-300
Change-Id: If081f07864ba73449f8c6bcf8e23ede138204082
Signed-off-by: sebdet <sebastien.determe@intl.att.com>
Diffstat (limited to 'src/main/java/org/onap')
8 files changed, 215 insertions, 9 deletions
diff --git a/src/main/java/org/onap/clamp/clds/util/JsonUtils.java b/src/main/java/org/onap/clamp/clds/util/JsonUtils.java index f59864ac0..6182598bf 100644 --- a/src/main/java/org/onap/clamp/clds/util/JsonUtils.java +++ b/src/main/java/org/onap/clamp/clds/util/JsonUtils.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP CLAMP * ================================================================================ - * Copyright (C) 2018 AT&T Intellectual Property. All rights + * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights * reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -32,6 +32,7 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; +import java.time.Instant; import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -43,6 +44,8 @@ import java.util.stream.StreamSupport; import org.onap.clamp.clds.model.properties.AbstractModelElement; import org.onap.clamp.clds.service.SecureServicePermission; import org.onap.clamp.clds.service.SecureServicePermissionDeserializer; +import org.onap.clamp.dao.model.gson.converter.InstantDeserializer; +import org.onap.clamp.dao.model.gson.converter.InstantSerializer; /** * This class is used to access the GSON with restricted type access. @@ -56,12 +59,17 @@ public class JsonUtils { public static final Gson GSON = new GsonBuilder() .registerTypeAdapter(SecureServicePermission.class, new SecureServicePermissionDeserializer()).create(); + public static final Gson GSON_JPA_MODEL = new GsonBuilder() + .registerTypeAdapter(Instant.class, new InstantSerializer()) + .registerTypeAdapter(Instant.class, new InstantDeserializer()).setPrettyPrinting() + .excludeFieldsWithoutExposeAnnotation().create(); + private JsonUtils() { } /** - * Return the value field of the json node element that has a name field equals - * to the given name. + * typeAdapter Return the value field of the json node element that has a name + * field equals to the given name. */ public static String getStringValueByName(JsonElement jsonElement, String name) { String value = extractJsonValueFromElement(jsonElement, name).map(JsonUtils::extractStringValueFromElement) diff --git a/src/main/java/org/onap/clamp/dao/LoopLogRepository.java b/src/main/java/org/onap/clamp/dao/LoopLogRepository.java index 5f983b2aa..199a25e87 100644 --- a/src/main/java/org/onap/clamp/dao/LoopLogRepository.java +++ b/src/main/java/org/onap/clamp/dao/LoopLogRepository.java @@ -28,6 +28,6 @@ import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; @Repository -public interface LoopLogRepository extends CrudRepository<LoopLog, String> { +public interface LoopLogRepository extends CrudRepository<LoopLog, Long> { } diff --git a/src/main/java/org/onap/clamp/dao/model/Loop.java b/src/main/java/org/onap/clamp/dao/model/Loop.java index 3473b54ff..c5946c9db 100644 --- a/src/main/java/org/onap/clamp/dao/model/Loop.java +++ b/src/main/java/org/onap/clamp/dao/model/Loop.java @@ -196,4 +196,30 @@ public class Loop implements Serializable { loopLogs.add(log); log.setLoop(this); } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((name == null) ? 0 : name.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Loop other = (Loop) obj; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + return true; + } + } diff --git a/src/main/java/org/onap/clamp/dao/model/LoopLog.java b/src/main/java/org/onap/clamp/dao/model/LoopLog.java index 8f7fd68b2..ba88bbee4 100644 --- a/src/main/java/org/onap/clamp/dao/model/LoopLog.java +++ b/src/main/java/org/onap/clamp/dao/model/LoopLog.java @@ -27,6 +27,7 @@ import com.google.gson.annotations.Expose; import java.io.Serializable; import java.time.Instant; +import java.time.temporal.ChronoUnit; import javax.persistence.Column; import javax.persistence.Entity; @@ -40,6 +41,14 @@ import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; +/** + * + * This class holds the logs created by the Clamp Backend. The Instant is always + * rounded to the nearest second as the nano seconds can't be stored in the + * database. The logs can be therefore exposed to the UI or the client doing + * some GET Loop on the backend. + * + */ @Entity @Table(name = "loop_logs") public class LoopLog implements Serializable { @@ -51,7 +60,7 @@ public class LoopLog implements Serializable { @Expose @Id @GeneratedValue(strategy = GenerationType.AUTO) - private long id; + private Long id; @Expose @Column(name = "log_type", nullable = false) @@ -68,13 +77,13 @@ public class LoopLog implements Serializable { @Expose @Column(name = "log_instant", nullable = false) - private Instant logInstant = Instant.now(); + private Instant logInstant = Instant.now().truncatedTo(ChronoUnit.SECONDS); - public long getId() { + public Long getId() { return id; } - public void setId(long id) { + public void setId(Long id) { this.id = id; } @@ -107,7 +116,32 @@ public class LoopLog implements Serializable { } public void setLogInstant(Instant logInstant) { - this.logInstant = logInstant; + this.logInstant = logInstant.truncatedTo(ChronoUnit.SECONDS); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((id == null) ? 0 : id.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + LoopLog other = (LoopLog) obj; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + return true; } } diff --git a/src/main/java/org/onap/clamp/dao/model/MicroServicePolicy.java b/src/main/java/org/onap/clamp/dao/model/MicroServicePolicy.java index 7fa4a55d9..6014d8d56 100644 --- a/src/main/java/org/onap/clamp/dao/model/MicroServicePolicy.java +++ b/src/main/java/org/onap/clamp/dao/model/MicroServicePolicy.java @@ -123,4 +123,29 @@ public class MicroServicePolicy implements Serializable { this.usedByLoops = usedBy; } + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((name == null) ? 0 : name.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + MicroServicePolicy other = (MicroServicePolicy) obj; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + return true; + } + } diff --git a/src/main/java/org/onap/clamp/dao/model/OperationalPolicy.java b/src/main/java/org/onap/clamp/dao/model/OperationalPolicy.java index d66fd9408..23f757416 100644 --- a/src/main/java/org/onap/clamp/dao/model/OperationalPolicy.java +++ b/src/main/java/org/onap/clamp/dao/model/OperationalPolicy.java @@ -87,4 +87,29 @@ public class OperationalPolicy implements Serializable { this.configurationsJson = configurationsJson; } + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((name == null) ? 0 : name.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + OperationalPolicy other = (OperationalPolicy) obj; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + return true; + } + } diff --git a/src/main/java/org/onap/clamp/dao/model/gson/converter/InstantDeserializer.java b/src/main/java/org/onap/clamp/dao/model/gson/converter/InstantDeserializer.java new file mode 100644 index 000000000..2d63e55fa --- /dev/null +++ b/src/main/java/org/onap/clamp/dao/model/gson/converter/InstantDeserializer.java @@ -0,0 +1,47 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2019 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.clamp.dao.model.gson.converter; + +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; + +import java.lang.reflect.Type; +import java.time.Instant; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; +import java.time.format.FormatStyle; +import java.util.Locale; + +public class InstantDeserializer implements JsonDeserializer<Instant> { + + DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withLocale(Locale.US) + .withZone(ZoneId.systemDefault()); + + @Override + public Instant deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) { + return Instant.parse(json.getAsString()); + + } +}
\ No newline at end of file diff --git a/src/main/java/org/onap/clamp/dao/model/gson/converter/InstantSerializer.java b/src/main/java/org/onap/clamp/dao/model/gson/converter/InstantSerializer.java new file mode 100644 index 000000000..cdb439e46 --- /dev/null +++ b/src/main/java/org/onap/clamp/dao/model/gson/converter/InstantSerializer.java @@ -0,0 +1,41 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2019 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.clamp.dao.model.gson.converter; + +import com.google.gson.JsonElement; +import com.google.gson.JsonPrimitive; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; + +import java.lang.reflect.Type; +import java.time.Instant; +import java.time.format.DateTimeFormatter; + +public class InstantSerializer implements JsonSerializer<Instant> { + + @Override + public JsonElement serialize(Instant src, Type typeOfSrc, JsonSerializationContext context) { + return new JsonPrimitive(DateTimeFormatter.ISO_INSTANT.format(src)); + } +}
\ No newline at end of file |