From ab6c52439d078e81de69fbaaa88f015b7be616fc Mon Sep 17 00:00:00 2001 From: Phaneendra Kumar Divi Date: Mon, 7 Dec 2020 15:11:26 +0530 Subject: [PATCH 1/2] Fixes for the sonar cloud issues spotted in code and tests. --- .../java/ai/apptuit/metrics/jinsight/ConfigService.java | 6 +++--- .../java/ai/apptuit/metrics/jinsight/ConfigServiceTest.java | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/ai/apptuit/metrics/jinsight/ConfigService.java b/src/main/java/ai/apptuit/metrics/jinsight/ConfigService.java index 7277356..40fad8b 100644 --- a/src/main/java/ai/apptuit/metrics/jinsight/ConfigService.java +++ b/src/main/java/ai/apptuit/metrics/jinsight/ConfigService.java @@ -317,11 +317,11 @@ static int getThisJVMProcessID() throws ConfigurationException { jvm.setAccessible(true); sun.management.VMManagement mgmt = (sun.management.VMManagement) jvm.get(runtime); - java.lang.reflect.Method pid_method = + java.lang.reflect.Method pidMethod = mgmt.getClass().getDeclaredMethod("getProcessId"); - pid_method.setAccessible(true); + pidMethod.setAccessible(true); - return (Integer) pid_method.invoke(mgmt); + return (Integer) pidMethod.invoke(mgmt); } catch (NoSuchFieldException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { throw new ConfigurationException("Error fetching " + PID_TEMPLATE_VARIABLE + " of JVM", e); } diff --git a/src/test/java/ai/apptuit/metrics/jinsight/ConfigServiceTest.java b/src/test/java/ai/apptuit/metrics/jinsight/ConfigServiceTest.java index 5c92e6e..741ed21 100644 --- a/src/test/java/ai/apptuit/metrics/jinsight/ConfigServiceTest.java +++ b/src/test/java/ai/apptuit/metrics/jinsight/ConfigServiceTest.java @@ -227,7 +227,7 @@ public void testGlobalUUIDTag() throws Exception { ConfigService configService = new ConfigService(p); Map globalTags = configService.getGlobalTags(); assertEquals(globalTags.size(), 1); - assertEquals(globalTags.get("jvmid").length(), 36); + assertEquals(36, globalTags.get("jvmid").length()); } @Test @@ -236,8 +236,8 @@ public void testGlobalProcessIDTag() throws Exception { p.setProperty("global_tags", "pid:${PID}"); ConfigService configService = new ConfigService(p); Map globalTags = configService.getGlobalTags(); - assertEquals(globalTags.size(), 1); - assertEquals(globalTags.get("pid"), getThisJVMProcessID() + ""); + assertEquals(1, globalTags.size()); + assertEquals(getThisJVMProcessID() + "", globalTags.get("pid")); } @Test From d30c12cc5555a642433dfa98d46b217a742fa3b3 Mon Sep 17 00:00:00 2001 From: Phaneendra Kumar Divi Date: Mon, 7 Dec 2020 15:19:03 +0530 Subject: [PATCH 2/2] Fixed complexity of if statement to get sonarcloud build to pass. --- .../ai/apptuit/metrics/jinsight/ConfigService.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/ai/apptuit/metrics/jinsight/ConfigService.java b/src/main/java/ai/apptuit/metrics/jinsight/ConfigService.java index 40fad8b..64a9d89 100644 --- a/src/main/java/ai/apptuit/metrics/jinsight/ConfigService.java +++ b/src/main/java/ai/apptuit/metrics/jinsight/ConfigService.java @@ -291,12 +291,12 @@ private void loadGlobalTags(Properties config) throws ConfigurationException { if (tagAndValue.length == 2) { String tag = tagAndValue[0].trim(); String value = tagAndValue[1].trim(); + if (value.equalsIgnoreCase(UUID_TEMPLATE_VARIABLE)) { + value = UUID.randomUUID().toString(); + } else if (value.equalsIgnoreCase(PID_TEMPLATE_VARIABLE)) { + value = getThisJVMProcessID() + ""; + } if (tag.length() > 0 && value.length() > 0) { - if (value.equalsIgnoreCase(UUID_TEMPLATE_VARIABLE)) { - value = UUID.randomUUID().toString(); - } else if (value.equalsIgnoreCase(PID_TEMPLATE_VARIABLE)) { - value = getThisJVMProcessID() + ""; - } loadedGlobalTags.put(tag, value); continue; }