From ea262e6da52fd4da0733f02998f87aebaf502ddb Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Wed, 12 Jun 2019 14:32:25 -0400 Subject: Apply simple sonar fixes Note: A number of these were identified, by SonarLint, in the Test classes, which are not typically scanned by Sonar. Removed unnecessary imports. Removed unneeded "throws Xxx". Replaced lambda with method references. Replaced duplicate strings with constants. Replaced try-fail-catch with assert-j methods to eliminate sonar complaints about duplicate failure messages. Added missing @Override annotations. Use map.computeIfAbsent() where appropriate. Also fixed some minor checkstyle issues. Removed unneeded "volatile" declarations. Replaced some if-else constructs with "?:" construct, per sonar. Replaced Object.wait() with CountDownLatch.await(); according to sonar (and javadocs), Object.wait() can return due to "spurious wakeups". Fixed issue whereby CryptoUtilsTest wouldn't run in my Eclipse. Change-Id: Ib6b71ed65662cfd6209400dac57ed69279bf29ec Issue-ID: POLICY-1791 Signed-off-by: Jim Hahn --- .../common/gson/JacksonExclusionStrategyTest.java | 1 - .../common/gson/JacksonFieldAdapterFactoryTest.java | 2 -- .../common/gson/JacksonMethodAdapterFactoryTest.java | 2 -- .../policy/common/gson/internal/AdapterTest.java | 17 +++++++++-------- .../gson/internal/AnyGetterSerializerTest.java | 5 ++--- .../gson/internal/AnySetterDeserializerTest.java | 1 - .../policy/common/gson/internal/ClassWalkerTest.java | 20 +++++++++++--------- .../common/gson/internal/FieldDeserializerTest.java | 1 - .../common/gson/internal/FieldSerializerTest.java | 1 - .../common/gson/internal/JacksonTypeAdapterTest.java | 2 +- .../onap/policy/common/gson/internal/LifterTest.java | 1 - 11 files changed, 23 insertions(+), 30 deletions(-) (limited to 'gson/src/test/java/org') diff --git a/gson/src/test/java/org/onap/policy/common/gson/JacksonExclusionStrategyTest.java b/gson/src/test/java/org/onap/policy/common/gson/JacksonExclusionStrategyTest.java index 4b5473c5..3ce16964 100644 --- a/gson/src/test/java/org/onap/policy/common/gson/JacksonExclusionStrategyTest.java +++ b/gson/src/test/java/org/onap/policy/common/gson/JacksonExclusionStrategyTest.java @@ -33,7 +33,6 @@ import java.util.LinkedList; import java.util.TreeMap; import org.junit.BeforeClass; import org.junit.Test; -import org.onap.policy.common.gson.JacksonExclusionStrategy; public class JacksonExclusionStrategyTest { diff --git a/gson/src/test/java/org/onap/policy/common/gson/JacksonFieldAdapterFactoryTest.java b/gson/src/test/java/org/onap/policy/common/gson/JacksonFieldAdapterFactoryTest.java index 9d4b4388..bbeb1e26 100644 --- a/gson/src/test/java/org/onap/policy/common/gson/JacksonFieldAdapterFactoryTest.java +++ b/gson/src/test/java/org/onap/policy/common/gson/JacksonFieldAdapterFactoryTest.java @@ -32,8 +32,6 @@ import com.google.gson.reflect.TypeToken; import java.util.ArrayList; import java.util.List; import org.junit.Test; -import org.onap.policy.common.gson.JacksonExclusionStrategy; -import org.onap.policy.common.gson.JacksonFieldAdapterFactory; import org.onap.policy.common.gson.annotation.GsonJsonIgnore; import org.onap.policy.common.gson.annotation.GsonJsonProperty; diff --git a/gson/src/test/java/org/onap/policy/common/gson/JacksonMethodAdapterFactoryTest.java b/gson/src/test/java/org/onap/policy/common/gson/JacksonMethodAdapterFactoryTest.java index 47cf4b3b..6377420d 100644 --- a/gson/src/test/java/org/onap/policy/common/gson/JacksonMethodAdapterFactoryTest.java +++ b/gson/src/test/java/org/onap/policy/common/gson/JacksonMethodAdapterFactoryTest.java @@ -33,8 +33,6 @@ import com.google.gson.reflect.TypeToken; import java.util.Map; import java.util.TreeMap; import org.junit.Test; -import org.onap.policy.common.gson.JacksonExclusionStrategy; -import org.onap.policy.common.gson.JacksonMethodAdapterFactory; import org.onap.policy.common.gson.annotation.GsonJsonAnyGetter; import org.onap.policy.common.gson.annotation.GsonJsonAnySetter; import org.onap.policy.common.gson.annotation.GsonJsonIgnore; diff --git a/gson/src/test/java/org/onap/policy/common/gson/internal/AdapterTest.java b/gson/src/test/java/org/onap/policy/common/gson/internal/AdapterTest.java index 04f37164..33160007 100644 --- a/gson/src/test/java/org/onap/policy/common/gson/internal/AdapterTest.java +++ b/gson/src/test/java/org/onap/policy/common/gson/internal/AdapterTest.java @@ -34,11 +34,12 @@ import java.util.List; import org.junit.Test; import org.onap.policy.common.gson.JacksonExclusionStrategy; import org.onap.policy.common.gson.annotation.GsonJsonProperty; -import org.onap.policy.common.gson.internal.Adapter; import org.onap.policy.common.gson.internal.DataAdapterFactory.Data; import org.onap.policy.common.gson.internal.DataAdapterFactory.DerivedData; public class AdapterTest { + private static final String EMPTY_ALIAS = "emptyAlias"; + private static final String GET_VALUE = ".getValue"; private static final String GET_VALUE_NAME = "getValue"; private static final String VALUE_NAME = "value"; private static final String MY_NAME = AdapterTest.class.getName(); @@ -171,9 +172,9 @@ public class AdapterTest { adapter = new Adapter(gson, mget(GET_VALUE_NAME), String.class); assertEquals(VALUE_NAME, adapter.getPropName()); - assertEquals(MY_NAME + ".getValue", adapter.getFullName()); + assertEquals(MY_NAME + GET_VALUE, adapter.getFullName()); - assertEquals("hello: " + MY_NAME + ".getValue", adapter.makeError("hello: ")); + assertEquals("hello: " + MY_NAME + GET_VALUE, adapter.makeError("hello: ")); // test setter @@ -201,7 +202,7 @@ public class AdapterTest { @Test public void testDetmPropName() { - assertEquals("emptyAlias", Adapter.detmPropName(field("emptyAlias"))); + assertEquals(EMPTY_ALIAS, Adapter.detmPropName(field(EMPTY_ALIAS))); assertEquals("name-with-alias", Adapter.detmPropName(field("nameWithAlias"))); assertEquals("unaliased", Adapter.detmPropName(field("unaliased"))); assertEquals(null, Adapter.detmPropName(field("$invalidFieldName"))); @@ -209,7 +210,7 @@ public class AdapterTest { @Test public void testDetmGetterPropName() { - assertEquals("emptyAlias", Adapter.detmGetterPropName(mget("getEmptyAlias"))); + assertEquals(EMPTY_ALIAS, Adapter.detmGetterPropName(mget("getEmptyAlias"))); assertEquals("get-with-alias", Adapter.detmGetterPropName(mget("getWithAlias"))); assertEquals("plain", Adapter.detmGetterPropName(mget("getPlain"))); assertEquals("primBool", Adapter.detmGetterPropName(mget("isPrimBool"))); @@ -222,7 +223,7 @@ public class AdapterTest { @Test public void testDetmSetterPropName() { - assertEquals("emptyAlias", Adapter.detmSetterPropName(mset("setEmptyAlias"))); + assertEquals(EMPTY_ALIAS, Adapter.detmSetterPropName(mset("setEmptyAlias"))); assertEquals("set-with-alias", Adapter.detmSetterPropName(mset("setWithAlias"))); assertEquals("plain", Adapter.detmSetterPropName(mset("setPlain"))); assertEquals(null, Adapter.detmSetterPropName(mset("noSet"))); @@ -236,8 +237,8 @@ public class AdapterTest { } @Test - public void testGetQualifiedNameMethod() throws Exception { - assertEquals(MY_NAME + ".getValue", Adapter.getQualifiedName(mget(GET_VALUE_NAME))); + public void testGetQualifiedNameMethod() { + assertEquals(MY_NAME + GET_VALUE, Adapter.getQualifiedName(mget(GET_VALUE_NAME))); } /** diff --git a/gson/src/test/java/org/onap/policy/common/gson/internal/AnyGetterSerializerTest.java b/gson/src/test/java/org/onap/policy/common/gson/internal/AnyGetterSerializerTest.java index 75128420..f68e3e3e 100644 --- a/gson/src/test/java/org/onap/policy/common/gson/internal/AnyGetterSerializerTest.java +++ b/gson/src/test/java/org/onap/policy/common/gson/internal/AnyGetterSerializerTest.java @@ -36,7 +36,6 @@ import java.util.Set; import org.junit.Before; import org.junit.Test; import org.onap.policy.common.gson.JacksonExclusionStrategy; -import org.onap.policy.common.gson.internal.AnyGetterSerializer; import org.onap.policy.common.gson.internal.DataAdapterFactory.Data; public class AnyGetterSerializerTest { @@ -61,7 +60,7 @@ public class AnyGetterSerializerTest { } @Test - public void testAddToTree_testCopyLiftedItems() throws Exception { + public void testAddToTree_testCopyLiftedItems() { JsonObject tree = new JsonObject(); tree.addProperty("hello", "world"); @@ -84,7 +83,7 @@ public class AnyGetterSerializerTest { } @Test - public void testAddToTree_NullMap() throws Exception { + public void testAddToTree_NullMap() { JsonObject tree = new JsonObject(); tree.addProperty("hello", "world"); diff --git a/gson/src/test/java/org/onap/policy/common/gson/internal/AnySetterDeserializerTest.java b/gson/src/test/java/org/onap/policy/common/gson/internal/AnySetterDeserializerTest.java index b180605e..c03e3695 100644 --- a/gson/src/test/java/org/onap/policy/common/gson/internal/AnySetterDeserializerTest.java +++ b/gson/src/test/java/org/onap/policy/common/gson/internal/AnySetterDeserializerTest.java @@ -36,7 +36,6 @@ import java.util.TreeMap; import org.junit.Before; import org.junit.Test; import org.onap.policy.common.gson.JacksonExclusionStrategy; -import org.onap.policy.common.gson.internal.AnySetterDeserializer; import org.onap.policy.common.gson.internal.DataAdapterFactory.Data; public class AnySetterDeserializerTest { diff --git a/gson/src/test/java/org/onap/policy/common/gson/internal/ClassWalkerTest.java b/gson/src/test/java/org/onap/policy/common/gson/internal/ClassWalkerTest.java index 1a15be09..6af4ae4f 100644 --- a/gson/src/test/java/org/onap/policy/common/gson/internal/ClassWalkerTest.java +++ b/gson/src/test/java/org/onap/policy/common/gson/internal/ClassWalkerTest.java @@ -42,11 +42,11 @@ import org.onap.policy.common.gson.annotation.GsonJsonAnyGetter; import org.onap.policy.common.gson.annotation.GsonJsonAnySetter; import org.onap.policy.common.gson.annotation.GsonJsonIgnore; import org.onap.policy.common.gson.annotation.GsonJsonProperty; -import org.onap.policy.common.gson.internal.Adapter; -import org.onap.policy.common.gson.internal.ClassWalker; public class ClassWalkerTest { + private static final String SET_OVERRIDE = ".setOverride"; + private MyWalker walker; /** @@ -63,12 +63,12 @@ public class ClassWalkerTest { assertEquals("[Intfc1, Intfc2, Intfc1, Intfc3, Bottom, DerivedFromBottom]", walker.classes.toString()); - List inFields = walker.getInProps(Field.class).stream().map(field -> field.getName()) + List inFields = walker.getInProps(Field.class).stream().map(Field::getName) .collect(Collectors.toList()); Collections.sort(inFields); assertEquals("[exposedField, overriddenValue, transField]", inFields.toString()); - List outFields = walker.getInProps(Field.class).stream().map(field -> field.getName()) + List outFields = walker.getInProps(Field.class).stream().map(Field::getName) .collect(Collectors.toList()); Collections.sort(outFields); assertEquals("[exposedField, overriddenValue, transField]", outFields.toString()); @@ -125,7 +125,7 @@ public class ClassWalkerTest { assertNotNull(walker.getAnyGetter()); assertEquals("getTheMap", walker.getAnyGetter().getName()); - List getters = walker.getOutProps(Method.class).stream().map(method -> method.getName()) + List getters = walker.getOutProps(Method.class).stream().map(Method::getName) .collect(Collectors.toList()); Collections.sort(getters); assertEquals("[getId, getOnlyOut, getValue]", getters.toString()); @@ -133,7 +133,7 @@ public class ClassWalkerTest { assertNotNull(walker.getAnySetter()); assertEquals("setMapValue", walker.getAnySetter().getName()); - List setters = walker.getInProps(Method.class).stream().map(method -> method.getName()) + List setters = walker.getInProps(Method.class).stream().map(Method::getName) .collect(Collectors.toList()); Collections.sort(setters); assertEquals("[setId, setOnlyIn, setValue]", setters.toString()); @@ -146,17 +146,17 @@ public class ClassWalkerTest { // setter with too few parameters assertThatThrownBy(() -> walker.walkClassHierarchy(AnySetterTooFewParams.class)) .isInstanceOf(JsonParseException.class).hasMessage(ClassWalker.ANY_SETTER_MISMATCH_ERR - + AnySetterTooFewParams.class.getName() + ".setOverride"); + + AnySetterTooFewParams.class.getName() + SET_OVERRIDE); // setter with too many parameters assertThatThrownBy(() -> walker.walkClassHierarchy(AnySetterTooManyParams.class)) .isInstanceOf(JsonParseException.class).hasMessage(ClassWalker.ANY_SETTER_MISMATCH_ERR - + AnySetterTooManyParams.class.getName() + ".setOverride"); + + AnySetterTooManyParams.class.getName() + SET_OVERRIDE); // setter with invalid parameter type assertThatThrownBy(() -> walker.walkClassHierarchy(AnySetterInvalidParam.class)) .isInstanceOf(JsonParseException.class).hasMessage(ClassWalker.ANY_SETTER_TYPE_ERR - + AnySetterInvalidParam.class.getName() + ".setOverride"); + + AnySetterInvalidParam.class.getName() + SET_OVERRIDE); } @Test @@ -317,10 +317,12 @@ public class ClassWalkerTest { private String value; + @Override public String getValue() { return value; } + @Override public void setValue(String value) { this.value = value; } diff --git a/gson/src/test/java/org/onap/policy/common/gson/internal/FieldDeserializerTest.java b/gson/src/test/java/org/onap/policy/common/gson/internal/FieldDeserializerTest.java index 62bab231..509ddb79 100644 --- a/gson/src/test/java/org/onap/policy/common/gson/internal/FieldDeserializerTest.java +++ b/gson/src/test/java/org/onap/policy/common/gson/internal/FieldDeserializerTest.java @@ -34,7 +34,6 @@ import java.util.List; import org.junit.Test; import org.onap.policy.common.gson.JacksonExclusionStrategy; import org.onap.policy.common.gson.internal.DataAdapterFactory.Data; -import org.onap.policy.common.gson.internal.FieldDeserializer; public class FieldDeserializerTest { private static final String TEXT_FIELD_NAME = "text"; diff --git a/gson/src/test/java/org/onap/policy/common/gson/internal/FieldSerializerTest.java b/gson/src/test/java/org/onap/policy/common/gson/internal/FieldSerializerTest.java index 18167fc0..cc5ef928 100644 --- a/gson/src/test/java/org/onap/policy/common/gson/internal/FieldSerializerTest.java +++ b/gson/src/test/java/org/onap/policy/common/gson/internal/FieldSerializerTest.java @@ -33,7 +33,6 @@ import java.util.List; import org.junit.Test; import org.onap.policy.common.gson.JacksonExclusionStrategy; import org.onap.policy.common.gson.internal.DataAdapterFactory.Data; -import org.onap.policy.common.gson.internal.FieldSerializer; public class FieldSerializerTest { private static final String TEXT_FIELD_NAME = "text"; diff --git a/gson/src/test/java/org/onap/policy/common/gson/internal/JacksonTypeAdapterTest.java b/gson/src/test/java/org/onap/policy/common/gson/internal/JacksonTypeAdapterTest.java index a75fe17e..6be4e590 100644 --- a/gson/src/test/java/org/onap/policy/common/gson/internal/JacksonTypeAdapterTest.java +++ b/gson/src/test/java/org/onap/policy/common/gson/internal/JacksonTypeAdapterTest.java @@ -135,7 +135,7 @@ public class JacksonTypeAdapterTest { String data = stringAdapter.read(new JsonReader(new StringReader("'read text'".replace('\'', '"')))); - assertEquals("read text", data.toString()); + assertEquals("read text", data); } private static class Data { diff --git a/gson/src/test/java/org/onap/policy/common/gson/internal/LifterTest.java b/gson/src/test/java/org/onap/policy/common/gson/internal/LifterTest.java index 55a3b674..e0b9eb30 100644 --- a/gson/src/test/java/org/onap/policy/common/gson/internal/LifterTest.java +++ b/gson/src/test/java/org/onap/policy/common/gson/internal/LifterTest.java @@ -28,7 +28,6 @@ import java.util.Arrays; import java.util.HashSet; import java.util.Set; import org.junit.Test; -import org.onap.policy.common.gson.internal.Lifter; public class LifterTest { -- cgit 1.2.3-korg