JAMES-2578 EnqueuedMailsDaoUtil is using wrong attribute converter

Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/6f6a7db6
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/6f6a7db6
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/6f6a7db6

Branch: refs/heads/master
Commit: 6f6a7db6a869c9baa06a7eb1f68e432ebe777248
Parents: 857e40b
Author: tran tien duc <dt...@linagora.com>
Authored: Wed Nov 14 11:35:01 2018 +0700
Committer: Benoit Tellier <btell...@linagora.com>
Committed: Thu Nov 15 09:07:26 2018 +0700

----------------------------------------------------------------------
 .../view/cassandra/EnqueuedMailsDaoUtil.java    | 16 +++---
 .../cassandra/EnqueuedMailsDaoUtilTest.java     | 53 ++++++++++++++++++++
 2 files changed, 60 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/6f6a7db6/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/view/cassandra/EnqueuedMailsDaoUtil.java
----------------------------------------------------------------------
diff --git 
a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/view/cassandra/EnqueuedMailsDaoUtil.java
 
b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/view/cassandra/EnqueuedMailsDaoUtil.java
index bab1ae7..a0fe44c 100644
--- 
a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/view/cassandra/EnqueuedMailsDaoUtil.java
+++ 
b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/view/cassandra/EnqueuedMailsDaoUtil.java
@@ -39,9 +39,8 @@ import static 
org.apache.james.queue.rabbitmq.view.cassandra.CassandraMailQueueV
 import static 
org.apache.james.queue.rabbitmq.view.cassandra.CassandraMailQueueViewModule.EnqueuedMailsTable.STATE;
 import static 
org.apache.james.queue.rabbitmq.view.cassandra.CassandraMailQueueViewModule.EnqueuedMailsTable.TIME_RANGE_START;
 
-import java.io.ByteArrayInputStream;
 import java.io.IOException;
-import java.io.ObjectInputStream;
+import java.io.UncheckedIOException;
 import java.nio.ByteBuffer;
 import java.nio.charset.StandardCharsets;
 import java.time.Instant;
@@ -72,6 +71,7 @@ import org.apache.mailet.PerRecipientHeaders;
 import com.datastax.driver.core.Row;
 import com.datastax.driver.core.UDTValue;
 import com.github.fge.lambdas.Throwing;
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 
@@ -132,7 +132,8 @@ public class EnqueuedMailsDaoUtil {
             .build();
     }
 
-    private static List<Attribute> toAttributes(Map<String, ByteBuffer> 
rowAttributes) {
+    @VisibleForTesting
+    static List<Attribute> toAttributes(Map<String, ByteBuffer> rowAttributes) 
{
         return rowAttributes.entrySet()
             .stream()
             .map(entry -> new Attribute(AttributeName.of(entry.getKey()), 
fromByteBuffer(entry.getValue())))
@@ -141,12 +142,9 @@ public class EnqueuedMailsDaoUtil {
 
     private static AttributeValue<?> fromByteBuffer(ByteBuffer byteBuffer) {
         try {
-            byte[] data = new byte[byteBuffer.remaining()];
-            byteBuffer.get(data);
-            ObjectInputStream objectInputStream = new ObjectInputStream(new 
ByteArrayInputStream(data));
-            return AttributeValue.fromJsonString((String) 
objectInputStream.readObject());
-        } catch (IOException | ClassNotFoundException e) {
-            throw new RuntimeException(e);
+            return 
AttributeValue.fromJsonString(StandardCharsets.UTF_8.decode(byteBuffer).toString());
+        } catch (IOException e) {
+            throw new UncheckedIOException(e);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/6f6a7db6/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/view/cassandra/EnqueuedMailsDaoUtilTest.java
----------------------------------------------------------------------
diff --git 
a/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/view/cassandra/EnqueuedMailsDaoUtilTest.java
 
b/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/view/cassandra/EnqueuedMailsDaoUtilTest.java
new file mode 100644
index 0000000..bde0c0d
--- /dev/null
+++ 
b/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/view/cassandra/EnqueuedMailsDaoUtilTest.java
@@ -0,0 +1,53 @@
+/****************************************************************
+ * 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.james.queue.rabbitmq.view.cassandra;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
+
+import org.apache.mailet.Attribute;
+import org.junit.jupiter.api.Test;
+import org.testcontainers.shaded.com.google.common.collect.ImmutableMap;
+
+class EnqueuedMailsDaoUtilTest {
+
+    @Test
+    void toAttributesShouldReturnEmptyWhenEmptyRawAttributeMap() {
+        ImmutableMap<String, ByteBuffer> attrMap = ImmutableMap.of();
+
+        assertThat(EnqueuedMailsDaoUtil.toAttributes(attrMap))
+            .isEmpty();
+    }
+
+    @Test
+    void toAttributesShouldConvertRawAttributeMap() {
+        ImmutableMap<String, ByteBuffer> attrMap = ImmutableMap
+            .of("Header-1", 
toByteBuffer("{\"serializer\":\"StringSerializer\",\"value\":\"alice\"}"));
+
+        assertThat(EnqueuedMailsDaoUtil.toAttributes(attrMap))
+            .containsExactly(Attribute.convertToAttribute("Header-1", 
"alice"));
+    }
+
+    private ByteBuffer toByteBuffer(String value) {
+        return ByteBuffer.wrap(value.getBytes(StandardCharsets.UTF_8));
+    }
+}
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org
For additional commands, e-mail: server-dev-h...@james.apache.org

Reply via email to