Repository: provectus/kafka-ui
Issue: BE: Fix sonar code smells
Labels: good first issue
scope/backend
status/accepted
type/chore

https://sonarcloud.io/project/issues?resolved=false&id=com.provectus%3Akafka-ui_backend

Issue comments:
blocked by #1537
@Haarolean is this issue still open to work on ?
@dshubhadeep yes
@dshubhadeep any luck?
@Haarolean will submit a PR by this weekend

Pull request review comments:

Patch that resolved the issue:
diff --git a/kafka-ui-api/src/main/java/com/provectus/kafka/ui/service/metrics/JmxMetricsRetriever.java b/kafka-ui-api/src/main/java/com/provectus/kafka/ui/service/metrics/JmxMetricsRetriever.java
index 78f5bdeced3..e7a58cbae27 100644
--- a/kafka-ui-api/src/main/java/com/provectus/kafka/ui/service/metrics/JmxMetricsRetriever.java
+++ b/kafka-ui-api/src/main/java/com/provectus/kafka/ui/service/metrics/JmxMetricsRetriever.java
@@ -72,17 +72,14 @@ private void withJmxConnector(String jmxUrl,
                                 KafkaCluster c,
                                 Consumer<JMXConnector> consumer) {
     var env = prepareJmxEnvAndSetThreadLocal(c);
-    try {
-      JMXConnector connector = null;
+    try (JMXConnector connector = JMXConnectorFactory.newJMXConnector(new JMXServiceURL(jmxUrl), env)) {
       try {
-        connector = JMXConnectorFactory.newJMXConnector(new JMXServiceURL(jmxUrl), env);
         connector.connect(env);
       } catch (Exception exception) {
         log.error("Error connecting to {}", jmxUrl, exception);
         return;
       }
       consumer.accept(connector);
-      connector.close();
     } catch (Exception e) {
       log.error("Error getting jmx metrics from {}", jmxUrl, e);
     } finally {
diff --git a/kafka-ui-api/src/main/java/com/provectus/kafka/ui/util/KafkaServicesValidation.java b/kafka-ui-api/src/main/java/com/provectus/kafka/ui/util/KafkaServicesValidation.java
index 31d90244ebe..4b8af81f851 100644
--- a/kafka-ui-api/src/main/java/com/provectus/kafka/ui/util/KafkaServicesValidation.java
+++ b/kafka-ui-api/src/main/java/com/provectus/kafka/ui/util/KafkaServicesValidation.java
@@ -45,12 +45,10 @@ private static Mono<ApplicationPropertyValidationDTO> invalid(Throwable th) {
    */
   public static Optional<String> validateTruststore(TruststoreConfig truststoreConfig) {
     if (truststoreConfig.getTruststoreLocation() != null && truststoreConfig.getTruststorePassword() != null) {
-      try {
+      try (FileInputStream fileInputStream = new FileInputStream(
+             (ResourceUtils.getFile(truststoreConfig.getTruststoreLocation())))) {
         KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
-        trustStore.load(
-            new FileInputStream((ResourceUtils.getFile(truststoreConfig.getTruststoreLocation()))),
-            truststoreConfig.getTruststorePassword().toCharArray()
-        );
+        trustStore.load(fileInputStream, truststoreConfig.getTruststorePassword().toCharArray());
         TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(
             TrustManagerFactory.getDefaultAlgorithm()
         );
diff --git a/kafka-ui-e2e-checks/src/main/java/com/provectus/kafka/ui/pages/schemas/SchemaDetails.java b/kafka-ui-e2e-checks/src/main/java/com/provectus/kafka/ui/pages/schemas/SchemaDetails.java
index 11c2d4a7ba3..38c12d35f3d 100644
--- a/kafka-ui-e2e-checks/src/main/java/com/provectus/kafka/ui/pages/schemas/SchemaDetails.java
+++ b/kafka-ui-e2e-checks/src/main/java/com/provectus/kafka/ui/pages/schemas/SchemaDetails.java
@@ -13,7 +13,7 @@ public class SchemaDetails extends BasePage {
   protected SelenideElement compatibilityField = $x("//h4[contains(text(),'Compatibility')]/../p");
   protected SelenideElement editSchemaBtn = $x("//button[contains(text(),'Edit Schema')]");
   protected SelenideElement removeBtn = $x("//*[contains(text(),'Remove')]");
-  protected SelenideElement confirmBtn = $x("//div[@role='dialog']//button[contains(text(),'Confirm')]");
+  protected SelenideElement schemaConfirmBtn = $x("//div[@role='dialog']//button[contains(text(),'Confirm')]");
   protected SelenideElement schemaTypeField = $x("//h4[contains(text(),'Type')]/../p");
   protected SelenideElement latestVersionField = $x("//h4[contains(text(),'Latest version')]/../p");
   protected SelenideElement compareVersionBtn = $x("//button[text()='Compare Versions']");
@@ -62,8 +62,8 @@ public SchemaDetails openCompareVersionMenu() {
   public SchemaDetails removeSchema() {
     clickByJavaScript(dotMenuBtn);
     removeBtn.shouldBe(Condition.enabled).click();
-    confirmBtn.shouldBe(Condition.visible).click();
-    confirmBtn.shouldBe(Condition.disappear);
+    schemaConfirmBtn.shouldBe(Condition.visible).click();
+    schemaConfirmBtn.shouldBe(Condition.disappear);
     return this;
   }
 }
diff --git a/kafka-ui-e2e-checks/src/main/java/com/provectus/kafka/ui/pages/topics/ProduceMessagePanel.java b/kafka-ui-e2e-checks/src/main/java/com/provectus/kafka/ui/pages/topics/ProduceMessagePanel.java
index c4e65c65bee..b683321bd51 100644
--- a/kafka-ui-e2e-checks/src/main/java/com/provectus/kafka/ui/pages/topics/ProduceMessagePanel.java
+++ b/kafka-ui-e2e-checks/src/main/java/com/provectus/kafka/ui/pages/topics/ProduceMessagePanel.java
@@ -14,7 +14,7 @@ public class ProduceMessagePanel extends BasePage {
   protected SelenideElement keyTextArea = $x("//div[@id='key']/textarea");
   protected SelenideElement valueTextArea = $x("//div[@id='content']/textarea");
   protected SelenideElement headersTextArea = $x("//div[@id='headers']/textarea");
-  protected SelenideElement submitBtn = headersTextArea.$x("../../../..//button[@type='submit']");
+  protected SelenideElement submitProduceMessageBtn = headersTextArea.$x("../../../..//button[@type='submit']");
   protected SelenideElement partitionDdl = $x("//ul[@name='partition']");
   protected SelenideElement keySerdeDdl = $x("//ul[@name='keySerde']");
   protected SelenideElement contentSerdeDdl = $x("//ul[@name='valueSerde']");
@@ -48,8 +48,8 @@ public ProduceMessagePanel setHeadersFld(String value) {
 
   @Step
   public ProduceMessagePanel submitProduceMessage() {
-    clickByActions(submitBtn);
-    submitBtn.shouldBe(Condition.disappear);
+    clickByActions(submitProduceMessageBtn);
+    submitProduceMessageBtn.shouldBe(Condition.disappear);
     refresh();
     return this;
   }


Test patch:
diff --git a/kafka-ui-api/src/test/java/com/provectus/kafka/ui/service/TopicsServicePaginationTest.java b/kafka-ui-api/src/test/java/com/provectus/kafka/ui/service/TopicsServicePaginationTest.java
index 8867400fee3..ac00fd9982e 100644
--- a/kafka-ui-api/src/test/java/com/provectus/kafka/ui/service/TopicsServicePaginationTest.java
+++ b/kafka-ui-api/src/test/java/com/provectus/kafka/ui/service/TopicsServicePaginationTest.java
@@ -131,7 +131,7 @@ public void shouldCalculateCorrectPageCountForNonDivisiblePageSize() {
 
     assertThat(topics.getBody().getPageCount()).isEqualTo(4);
     assertThat(topics.getBody().getTopics()).hasSize(1);
-    assertThat(topics.getBody().getTopics().get(0).getName().equals("99"));
+    assertThat(topics.getBody().getTopics().get(0).getName()).isEqualTo("99");
   }
 
   @Test