CAMEL-11794: Refactor to make the encoding testable, add a test

Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/a1f25b5e
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/a1f25b5e
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/a1f25b5e

Branch: refs/heads/master
Commit: a1f25b5e52f6b8643f56032cb586b432bf948931
Parents: 3a92923
Author: Peter Palaga <ppal...@redhat.com>
Authored: Wed Sep 20 15:10:26 2017 +0200
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Wed Sep 20 17:17:58 2017 +0200

----------------------------------------------------------------------
 .../camel/component/consul/ConsulRegistry.java  | 63 ++++++++++++++++++--
 .../consul/ConsulRegistryUtilsTest.java         | 54 +++++++++++++++++
 2 files changed, 111 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/a1f25b5e/components/camel-consul/src/main/java/org/apache/camel/component/consul/ConsulRegistry.java
----------------------------------------------------------------------
diff --git 
a/components/camel-consul/src/main/java/org/apache/camel/component/consul/ConsulRegistry.java
 
b/components/camel-consul/src/main/java/org/apache/camel/component/consul/ConsulRegistry.java
index c910f40..cd968c2 100644
--- 
a/components/camel-consul/src/main/java/org/apache/camel/component/consul/ConsulRegistry.java
+++ 
b/components/camel-consul/src/main/java/org/apache/camel/component/consul/ConsulRegistry.java
@@ -78,8 +78,8 @@ public class ConsulRegistry implements Registry {
         kvClient = consul.keyValueClient();
         Optional<String> result = kvClient.getValueAsString(key);
         if (result.isPresent()) {
-            byte[] postDecodedValue = Base64.decodeBase64(result.get());
-            return SerializationUtils.deserialize(postDecodedValue);
+            byte[] postDecodedValue = 
ConsulRegistryUtils.decodeBase64(result.get());
+            return ConsulRegistryUtils.deserialize(postDecodedValue);
         }
         return null;
     }
@@ -188,11 +188,10 @@ public class ConsulRegistry implements Registry {
         if (lookupByName(key) != null) {
             remove(key);
         }
-        Object clone = SerializationUtils.clone((Serializable) object);
-        byte[] serializedObject = SerializationUtils.serialize((Serializable) 
clone);
+        Object clone = ConsulRegistryUtils.clone((Serializable) object);
+        byte[] serializedObject = ConsulRegistryUtils.serialize((Serializable) 
clone);
         // pre-encode due native encoding issues
-        byte[] preEncodedValue = Base64.encodeBase64(serializedObject);
-        String value = new String(preEncodedValue);
+        String value = ConsulRegistryUtils.encodeBase64(serializedObject);
         // store the actual class
         kvClient.putValue(key, value);
         // store just as a bookmark
@@ -251,4 +250,56 @@ public class ConsulRegistry implements Registry {
         return lookupByType(type);
     }
 
+    static class ConsulRegistryUtils {
+        /**
+         * Decodes using Base64.
+         *
+         * @param base64String the {@link String} to decode
+         * @return a decoded data as a byte array
+         */
+        static byte[] decodeBase64(final String base64String) {
+            return Base64.decodeBase64(base64String);
+        }
+
+        /**
+         * Encodes using Base64.
+         * 
+         * @param binaryData the data to encode
+         * @return an encoded data as a {@link String}
+         */
+        static String encodeBase64(final byte[] binaryData) {
+            final byte[] encoded = Base64.encodeBase64(binaryData);
+            return new String(encoded);
+        }
+
+        /**
+         * Deserializes an object out of the given byte array.
+         *
+         * @param bytes the byte array to deserialize from
+         * @return an {@link Object} deserialized from the given byte array
+         */
+        static Object deserialize(byte[] bytes) {
+            return SerializationUtils.deserialize(bytes);
+        }
+
+        /**
+         * A deep serialization based clone
+         *
+         * @param object the object to clone
+         * @return a deep clone
+         */
+        static Object clone(Serializable object) {
+            return SerializationUtils.clone(object);
+        }
+
+        /**
+         * Serializes the given {@code serializable} using Java Serialization
+         * @param serializable
+         * @return the serialized object as a byte array
+         */
+        static byte[] serialize(Serializable serializable) {
+            return SerializationUtils.serialize(serializable);
+        }
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/a1f25b5e/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulRegistryUtilsTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulRegistryUtilsTest.java
 
b/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulRegistryUtilsTest.java
new file mode 100644
index 0000000..1a1eac2
--- /dev/null
+++ 
b/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulRegistryUtilsTest.java
@@ -0,0 +1,54 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+package org.apache.camel.component.consul;
+
+import java.io.Serializable;
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.camel.component.consul.ConsulRegistry.ConsulRegistryUtils;
+import org.junit.Test;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
+/**
+ * @author <a href="https://github.com/ppalaga";>Peter Palaga</a>
+ */
+public class ConsulRegistryUtilsTest {
+
+    @Test
+    public void encodeDecode() {
+        final List<String> src = Arrays.asList("one", "\u0434\u0432\u0430", 
"t\u0159i");
+        final byte[] serialized = 
ConsulRegistryUtils.serialize((Serializable)src);
+        assertEquals(src, ConsulRegistryUtils.deserialize(serialized));
+        final String encoded = ConsulRegistryUtils.encodeBase64(serialized);
+        
assertEquals("rO0ABXNyABpqYXZhLnV0aWwuQXJyYXlzJEFycmF5TGlzdNmkPL7NiAbSAgABWwABYXQAE1tMamF2YS9sYW5nL09iamVjdDt4"
+            + 
"cHVyABNbTGphdmEubGFuZy5TdHJpbmc7rdJW5+kde0cCAAB4cAAAAAN0AANvbmV0AAbQtNCy0LB0AAR0xZlp",
 encoded);
+        final byte[] decoded = ConsulRegistryUtils.decodeBase64(encoded);
+        assertArrayEquals(new byte[] {
+            -84, -19, 0, 5, 115, 114, 0, 26, 106, 97, 118, 97, 46, 117, 116, 
105, 108, 46, 65, 114, 114, 97, 121,
+            115, 36, 65, 114, 114, 97, 121, 76, 105, 115, 116, -39, -92, 60, 
-66, -51, -120, 6, -46, 2, 0, 1, 91, 0,
+            1, 97, 116, 0, 19, 91, 76, 106, 97, 118, 97, 47, 108, 97, 110, 
103, 47, 79, 98, 106, 101, 99, 116, 59,
+            120, 112, 117, 114, 0, 19, 91, 76, 106, 97, 118, 97, 46, 108, 97, 
110, 103, 46, 83, 116, 114, 105, 110,
+            103, 59, -83, -46, 86, -25, -23, 29, 123, 71, 2, 0, 0, 120, 112, 
0, 0, 0, 3, 116, 0, 3, 111, 110, 101,
+            116, 0, 6, -48, -76, -48, -78, -48, -80, 116, 0, 4, 116, -59, 
-103, 105
+        }, decoded);
+        assertEquals(src, ConsulRegistryUtils.deserialize(decoded));
+    }
+
+}

Reply via email to