Improve code quality 61/96661/2
authorBogumil Zebek <bogumil.zebek@nokia.com>
Mon, 7 Oct 2019 06:11:34 +0000 (08:11 +0200)
committerZebek Bogumil <bogumil.zebek@nokia.com>
Mon, 21 Oct 2019 10:16:06 +0000 (12:16 +0200)
Issue-ID: CCSDK-1808
Signed-off-by: Zebek Bogumil <bogumil.zebek@nokia.com>
Change-Id: I004557c67b3440c41c00b6bf1a13e723f3ded0e4

sdnr/wt/devicemanager/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/aaiconnector/impl/URLParamEncoder.java
sdnr/wt/devicemanager/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/base/netconf/container/AllPm.java
sdnr/wt/devicemanager/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/aaiconnector/impl/URLParamEncoderTest.java [new file with mode: 0644]
sdnr/wt/devicemanager/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/base/netconf/container/AllPmTest.java [new file with mode: 0644]

index 2e8b44f..4decc24 100644 (file)
@@ -19,13 +19,16 @@ package org.onap.ccsdk.features.sdnr.wt.devicemanager.aaiconnector.impl;
 
 class URLParamEncoder {
 
+    private URLParamEncoder() {
+    }
+
+    private static final String UNSAFE_CHARSET = " %$&+,/:;=?@<>#%";
+
     public static String encode(String input) {
         StringBuilder resultStr = new StringBuilder();
         for (char ch : input.toCharArray()) {
             if (isUnsafe(ch)) {
-                resultStr.append('%');
-                resultStr.append(toHex(ch / 16));
-                resultStr.append(toHex(ch % 16));
+                resultStr.append(escape(ch));
             } else {
                 resultStr.append(ch);
             }
@@ -33,14 +36,18 @@ class URLParamEncoder {
         return resultStr.toString();
     }
 
-    private static char toHex(int ch) {
-        return (char) (ch < 10 ? '0' + ch : 'A' + ch - 10);
-    }
-
     private static boolean isUnsafe(char ch) {
-        if (ch > 128 || ch < 0) {
+        if (ch > 128) {
             return true;
         }
-        return " %$&+,/:;=?@<>#%".indexOf(ch) >= 0;
+        return UNSAFE_CHARSET.indexOf(ch) >= 0;
+    }
+
+    private static String escape(char ch){
+        return String.format("%c%c%c", '%', toHex(ch / 16), toHex(ch % 16));
+    }
+
+    private static char toHex(int ch) {
+        return (char) (ch < 10 ? '0' + ch : 'A' + ch - 10);
     }
 }
index 196493c..5c7cd03 100644 (file)
@@ -1,4 +1,4 @@
-/*******************************************************************************
+/**
  * ============LICENSE_START========================================================================
  * ONAP : ccsdk feature sdnr wt
  * =================================================================================================
@@ -6,28 +6,28 @@
  * =================================================================================================
  * 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
- *
+ * <p>
  * http://www.apache.org/licenses/LICENSE-2.0
- *
+ * <p>
  * 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.ccsdk.features.sdnr.wt.devicemanager.base.netconf.container;
 
-import java.util.ArrayList;
-import java.util.List;
 import org.onap.ccsdk.features.sdnr.wt.devicemanager.performancemanager.impl.database.types.EsHistoricalPerformance15Minutes;
 import org.onap.ccsdk.features.sdnr.wt.devicemanager.performancemanager.impl.database.types.EsHistoricalPerformance24Hours;
 
-public class AllPm {
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
 
-    private final static AllPm EMPTY = new AllPm();
+public class AllPm {
 
     private final List<EsHistoricalPerformance15Minutes> pm15 = new ArrayList<>();
     private final List<EsHistoricalPerformance24Hours> pm24 = new ArrayList<>();
@@ -41,19 +41,19 @@ public class AllPm {
     }
 
     public List<EsHistoricalPerformance15Minutes> getPm15() {
-        return pm15;
+        return Collections.unmodifiableList(pm15);
     }
 
     public List<EsHistoricalPerformance24Hours> getPm24() {
-        return pm24;
+        return Collections.unmodifiableList(pm24);
     }
 
-    public Object size() {
-        return pm15.size()+pm24.size();
+    public int size() {
+        return pm15.size() + pm24.size();
     }
 
     public static AllPm getEmpty() {
-        return EMPTY;
+        return new AllPm();
     }
 
 }
diff --git a/sdnr/wt/devicemanager/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/aaiconnector/impl/URLParamEncoderTest.java b/sdnr/wt/devicemanager/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/aaiconnector/impl/URLParamEncoderTest.java
new file mode 100644 (file)
index 0000000..cc4a436
--- /dev/null
@@ -0,0 +1,38 @@
+/*******************************************************************************
+ * ============LICENSE_START=======================================================
+ * ONAP : ccsdk feature sdnr wt
+ *  ================================================================================
+ * Copyright (C) 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.ccsdk.features.sdnr.wt.devicemanager.aaiconnector.impl;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+
+public class URLParamEncoderTest {
+
+    @Test
+    public void shouldEncodeStringsToFormatAcceptableByURL(){
+        assertEquals("test", URLParamEncoder.encode("test"));
+        assertEquals("test%20str", URLParamEncoder.encode("test str"));
+        assertEquals("test%23%24str", URLParamEncoder.encode("test#$str"));
+        assertEquals("test%20%25%24%26%2B%2C%2F%3A%3B%3D%3F%40%3C%3E%23%25str", URLParamEncoder.encode("test %$&+,/:;=?@<>#%str"));
+
+    }
+}
diff --git a/sdnr/wt/devicemanager/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/base/netconf/container/AllPmTest.java b/sdnr/wt/devicemanager/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/base/netconf/container/AllPmTest.java
new file mode 100644 (file)
index 0000000..538dcd4
--- /dev/null
@@ -0,0 +1,104 @@
+/*******************************************************************************
+ * ============LICENSE_START=======================================================
+ * ONAP : ccsdk feature sdnr wt
+ *  ================================================================================
+ * Copyright (C) 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.ccsdk.features.sdnr.wt.devicemanager.base.netconf.container;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.onap.ccsdk.features.sdnr.wt.devicemanager.performancemanager.impl.database.types.EsHistoricalPerformance15Minutes;
+import org.onap.ccsdk.features.sdnr.wt.devicemanager.performancemanager.impl.database.types.EsHistoricalPerformance24Hours;
+
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.mock;
+
+
+public class AllPmTest {
+
+    private AllPm allPm;
+
+    @Before
+    public void setUp(){
+        allPm = AllPm.getEmpty();
+    }
+
+    @Test
+    public void shouldCreateEmptyInstance() {
+        assertEquals(0, allPm.size());
+    }
+
+
+    @Test
+    public void shouldBePossibleToAdd15MinutesPerformanceMeasurements() {
+        // given
+        final EsHistoricalPerformance15Minutes esHistoricalPerformance15Minutes_1 = mock(EsHistoricalPerformance15Minutes.class);
+        final EsHistoricalPerformance15Minutes esHistoricalPerformance15Minutes_2 = mock(EsHistoricalPerformance15Minutes.class);
+
+        allPm.add(esHistoricalPerformance15Minutes_1);
+        allPm.add(esHistoricalPerformance15Minutes_2);
+
+
+        // when
+        final List<EsHistoricalPerformance15Minutes> pm15size = allPm.getPm15();
+        final List<EsHistoricalPerformance24Hours> pm24size = allPm.getPm24();
+
+        // then
+        assertEquals(2, pm15size.size());
+        assertEquals(0, pm24size.size());
+    }
+
+    @Test
+    public void shouldBePossibleToAdd24HoursPerformanceMeasurements() {
+        // given
+        final EsHistoricalPerformance24Hours esHistoricalPerformance24Hours_1 = mock(EsHistoricalPerformance24Hours.class);
+        final EsHistoricalPerformance24Hours esHistoricalPerformance24Hours_2 = mock(EsHistoricalPerformance24Hours.class);
+
+        allPm.add(esHistoricalPerformance24Hours_1);
+        allPm.add(esHistoricalPerformance24Hours_2);
+
+
+        // when
+        final List<EsHistoricalPerformance15Minutes> pm15size = allPm.getPm15();
+        final List<EsHistoricalPerformance24Hours> pm24size = allPm.getPm24();
+
+        // then
+        assertEquals(0, pm15size.size());
+        assertEquals(2, pm24size.size());
+    }
+
+    @Test
+    public void shouldBePossibleToAddPerformanceMeasurements() {
+        // given
+        final EsHistoricalPerformance15Minutes esHistoricalPerformance15Minutes = mock(EsHistoricalPerformance15Minutes.class);
+        final EsHistoricalPerformance24Hours esHistoricalPerformance24Hours = mock(EsHistoricalPerformance24Hours.class);
+
+        allPm.add(esHistoricalPerformance15Minutes);
+        allPm.add(esHistoricalPerformance24Hours);
+
+        // when
+        final int size = allPm.size();
+
+        // then
+        assertEquals(2, size);
+    }
+
+
+}