Improve code quality 92/98492/1
authorbogumil_zebek <bogumil.zebek@nokia.com>
Mon, 18 Nov 2019 08:05:30 +0000 (09:05 +0100)
committerZebek Bogumil <bogumil.zebek@nokia.com>
Mon, 18 Nov 2019 08:05:30 +0000 (09:05 +0100)
Remove sonar violations.

Issue-ID: AAI-2710
Signed-off-by: Zebek Bogumil <bogumil.zebek@nokia.com>
Change-Id: I557581b72b527f9d38dd5f9f7099085a8b639c88

sparkybe-onap-service/src/main/java/org/onap/aai/sparky/sync/entity/ObjectIdCollection.java
sparkybe-onap-service/src/main/java/org/onap/aai/sparky/util/ErrorUtil.java
sparkybe-onap-service/src/main/java/org/onap/aai/sparky/util/JsonXmlConverter.java
sparkybe-onap-service/src/main/java/org/onap/aai/sparky/util/NodeUtils.java
sparkybe-onap-service/src/test/java/org/onap/aai/sparky/sync/entity/ObjectIdCollectionTest.java [new file with mode: 0644]
sparkybe-onap-service/src/test/java/org/onap/aai/sparky/util/ErrorUtilTest.java [new file with mode: 0644]
sparkybe-onap-service/src/test/java/org/onap/aai/sparky/util/JsonXmlConverterTest.java [new file with mode: 0644]

index 7c91cd2..8e9d2f5 100644 (file)
@@ -29,8 +29,8 @@ import java.util.concurrent.ConcurrentHashMap;
  */
 public class ObjectIdCollection {
 
-  protected ConcurrentHashMap<String, String> importedObjectIds =
-      new ConcurrentHashMap<String, String>();
+  private ConcurrentHashMap<String, String> importedObjectIds =
+      new ConcurrentHashMap<>();
 
   public Collection<String> getImportedObjectIds() {
     return importedObjectIds.values();
@@ -58,11 +58,7 @@ public class ObjectIdCollection {
     if (items == null) {
       return;
     }
-
-    items.stream().forEach((item) -> {
-      importedObjectIds.putIfAbsent(item, item);
-    });
-
+    items.forEach(it -> importedObjectIds.putIfAbsent(it, it));
   }
 
   /**
index 0bf00a3..df24a7f 100644 (file)
@@ -25,6 +25,8 @@ package org.onap.aai.sparky.util;
  */
 public class ErrorUtil {
 
+  private ErrorUtil() {}
+
   /**
    * Extract stack trace elements.
    *
@@ -46,8 +48,8 @@ public class ErrorUtil {
 
       int numFramesToExtract = Math.min(maxNumberOfElementsToCapture, stackTraceElements.length);
 
-      for (int x = 0; x < numFramesToExtract; x++) {
-        sb.append(stackTraceElements[x]).append("\n");
+      for (int index = 0; index < numFramesToExtract; index++) {
+        sb.append(stackTraceElements[index]).append("\n");
       }
 
     }
index e42aa8d..e3de990 100644 (file)
@@ -30,6 +30,8 @@ import org.json.XML;
  */
 public class JsonXmlConverter {
 
+  private JsonXmlConverter() {}
+
   /**
    * Checks if is valid json.
    *
@@ -56,10 +58,9 @@ public class JsonXmlConverter {
    * @param jsonText the json text
    * @return the string
    */
-  public static String convertJsontoXml(String jsonText) {
+  public static String convertJsonToXml(String jsonText) {
     JSONObject jsonObj = new JSONObject(jsonText);
-    String xmlText = XML.toString(jsonObj);
-    return xmlText;
+    return XML.toString(jsonObj);
   }
 
   /**
@@ -68,7 +69,7 @@ public class JsonXmlConverter {
    * @param xmlText the xml text
    * @return the string
    */
-  public static String convertXmltoJson(String xmlText) {
+  public static String convertXmlToJson(String xmlText) {
     JSONObject jsonObj = XML.toJSONObject(xmlText);
     return jsonObj.toString();
   }
index 8772afa..25aa8b4 100644 (file)
@@ -623,7 +623,7 @@ public class NodeUtils {
       return null;
     }
 
-    return JsonXmlConverter.convertJsontoXml(jsonOutput);
+    return JsonXmlConverter.convertJsonToXml(jsonOutput);
 
   }
 
diff --git a/sparkybe-onap-service/src/test/java/org/onap/aai/sparky/sync/entity/ObjectIdCollectionTest.java b/sparkybe-onap-service/src/test/java/org/onap/aai/sparky/sync/entity/ObjectIdCollectionTest.java
new file mode 100644 (file)
index 0000000..210e0c9
--- /dev/null
@@ -0,0 +1,81 @@
+/**
+ * ============LICENSE_START=======================================================
+ * org.onap.aai
+ * ================================================================================
+ * Copyright © 2019 Nokia 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.aai.sparky.sync.entity;
+
+import com.google.common.collect.Lists;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+
+public class ObjectIdCollectionTest {
+
+    private ObjectIdCollection objectIdCollection;
+
+    @Before
+    public void setUp(){
+        objectIdCollection = new ObjectIdCollection();
+    }
+
+    @Test
+    public void shouldBePossibleToStoreObjectId(){
+        // when
+        objectIdCollection.addObjectId("1");
+        objectIdCollection.addObjectId("2");
+
+        // then
+        assertEquals(2, objectIdCollection.getSize());
+        assertTrue(objectIdCollection.getImportedObjectIds().contains("1"));
+        assertTrue(objectIdCollection.getImportedObjectIds().contains("2"));
+    }
+
+    @Test
+    public void shouldBePossibleToStoreCollectionOfObjectIds(){
+        // given
+        List<String> objectIds = Lists.newArrayList("1","2","3");
+
+        // when
+        objectIdCollection.addAll(objectIds);
+
+        // then
+        assertEquals(3, objectIdCollection.getSize());
+        assertTrue(objectIdCollection.getImportedObjectIds().contains("1"));
+        assertTrue(objectIdCollection.getImportedObjectIds().contains("2"));
+        assertTrue(objectIdCollection.getImportedObjectIds().contains("3"));
+    }
+
+    @Test
+    public void shouldBePossibleToClearObjectIds(){
+        // given
+        List<String> objectIds = Lists.newArrayList("1","2","3");
+        objectIdCollection.addAll(objectIds);
+
+        // when
+        objectIdCollection.clear();
+
+        // then
+        assertEquals(0, objectIdCollection.getSize());
+    }
+
+}
diff --git a/sparkybe-onap-service/src/test/java/org/onap/aai/sparky/util/ErrorUtilTest.java b/sparkybe-onap-service/src/test/java/org/onap/aai/sparky/util/ErrorUtilTest.java
new file mode 100644 (file)
index 0000000..5ab7029
--- /dev/null
@@ -0,0 +1,66 @@
+/**
+ * ============LICENSE_START=======================================================
+ * org.onap.aai
+ * ================================================================================
+ * Copyright © 2019 Nokia 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.aai.sparky.util;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+public class ErrorUtilTest {
+
+    private StackTraceElement[] stackTraceElements = new StackTraceElement[]{
+            new StackTraceElement("TestClass", "methodA", "a", 2),
+            new StackTraceElement("TestClass", "methodB", "b", 3)
+    };
+
+    @Mock
+    Exception exception;
+
+    @Test
+    public void shouldReturnEmptyStringWhenStackTraceArrayIsEmpty() {
+        assertEquals("", ErrorUtil.extractStackTraceElements(1, exception));
+    }
+
+    @Test
+    public void shouldReturnNarrowedStackTraceElementsAsFormattedStringToPassedMaxNumberOfElements() {
+        // given
+        when(exception.getStackTrace()).thenReturn(stackTraceElements);
+        // when
+        final String actual = ErrorUtil.extractStackTraceElements(1, exception);
+        // then
+        assertEquals("TestClass.methodA(a:2)\n", actual);
+    }
+
+    @Test
+    public void shouldReturnFullStackTraceElementsAsFormattedStringWhenMaxNumberOfElementsIsOutOfRange() {
+        // given
+        when(exception.getStackTrace()).thenReturn(stackTraceElements);
+        // when
+        final String actual = ErrorUtil.extractStackTraceElements(5, exception);
+        // then
+        assertEquals("TestClass.methodA(a:2)\nTestClass.methodB(b:3)\n", actual);
+    }
+
+}
diff --git a/sparkybe-onap-service/src/test/java/org/onap/aai/sparky/util/JsonXmlConverterTest.java b/sparkybe-onap-service/src/test/java/org/onap/aai/sparky/util/JsonXmlConverterTest.java
new file mode 100644 (file)
index 0000000..73463bd
--- /dev/null
@@ -0,0 +1,50 @@
+/**
+ * ============LICENSE_START=======================================================
+ * org.onap.aai
+ * ================================================================================
+ * Copyright © 2019 Nokia 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.aai.sparky.util;
+
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+public class JsonXmlConverterTest {
+
+    @Test
+    public void shouldReportAnErrorWhenDataHasInvalidFormat(){
+        assertFalse(JsonXmlConverter.isValidJson("invalid json"));
+    }
+
+    @Test
+    public void shouldAcceptDataWhenDataHasValidFormat(){
+        assertTrue(JsonXmlConverter.isValidJson("{ 'number': 5}"));
+    }
+
+    @Test
+    public void shouldConvertXmlToJson() {
+        final String actual = JsonXmlConverter.convertXmlToJson("<root><number>5</number><text>message</text></root>");
+        assertEquals("{\"root\":{\"number\":5,\"text\":\"message\"}}", actual);
+    }
+
+    @Test
+    public void shouldConvertJsonToXml() {
+        final String actual = JsonXmlConverter.convertJsonToXml("{\"root\":{\"number\":5,\"text\":\"message\"}}");
+        assertEquals("<root><number>5</number><text>message</text></root>", actual);
+    }
+
+}