You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by rc...@apache.org on 2023/10/18 02:09:21 UTC

[james-project] branch master updated: JAMES-3828 ICALAttributeDTO: support missing uid, method and dtstamp fields

This is an automated email from the ASF dual-hosted git repository.

rcordier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git


The following commit(s) were added to refs/heads/master by this push:
     new a06a1a362e JAMES-3828 ICALAttributeDTO: support missing uid, method and dtstamp fields
a06a1a362e is described below

commit a06a1a362eca615e250b48434746dc96cbf2eac8
Author: Quan Tran <hq...@linagora.com>
AuthorDate: Tue Oct 17 14:35:18 2023 +0700

    JAMES-3828 ICALAttributeDTO: support missing uid, method and dtstamp fields
    
    Issue was that if any of these field is missing, James would abort event processing, preventing Calendar from recovering and ingesting the event.
---
 .../transport/mailets/model/ICALAttributeDTO.java  |  23 ++--
 .../transport/mailets/ICALToJsonAttributeTest.java | 120 ++++++++++++++++++++-
 .../mailets/model/ICALAttributeDTOTest.java        |  49 +++++----
 3 files changed, 152 insertions(+), 40 deletions(-)

diff --git a/mailet/icalendar/src/main/java/org/apache/james/transport/mailets/model/ICALAttributeDTO.java b/mailet/icalendar/src/main/java/org/apache/james/transport/mailets/model/ICALAttributeDTO.java
index e594e4e1a0..37cb232a5f 100644
--- a/mailet/icalendar/src/main/java/org/apache/james/transport/mailets/model/ICALAttributeDTO.java
+++ b/mailet/icalendar/src/main/java/org/apache/james/transport/mailets/model/ICALAttributeDTO.java
@@ -47,17 +47,14 @@ public class ICALAttributeDTO {
             Optional<String> dtstamp = optionalOf(vevent.getDateStamp());
 
             Preconditions.checkNotNull(ical);
-            Preconditions.checkState(uid.isPresent(), "uid is a compulsory property of an ICAL object");
-            Preconditions.checkState(method.isPresent(), "method is a compulsory property of an ICAL object");
-            Preconditions.checkState(dtstamp.isPresent(), "dtstamp is a compulsory property of an ICAL object");
 
             return sender -> recipient -> replyTo ->
                     new ICALAttributeDTO(
                             ical,
-                            uid.get(), sender.asString(),
+                            uid, sender.asString(),
                             recipient.asString(),
                             replyTo.asString(),
-                            dtstamp.get(), method.get(), sequence,
+                            dtstamp, method, sequence,
                             recurrenceId);
         }
 
@@ -89,14 +86,14 @@ public class ICALAttributeDTO {
     private final String sender;
     private final String recipient;
     private final String replyTo;
-    private final String uid;
-    private final String dtstamp;
-    private final String method;
+    private final Optional<String> uid;
+    private final Optional<String> dtstamp;
+    private final Optional<String> method;
     private final Optional<String> sequence;
     private final Optional<String> recurrenceId;
 
-    private ICALAttributeDTO(String ical, String uid, String sender, String recipient, String replyTo, String dtstamp,
-                             String method, Optional<String> sequence, Optional<String> recurrenceId) {
+    private ICALAttributeDTO(String ical, Optional<String> uid, String sender, String recipient, String replyTo, Optional<String> dtstamp,
+                             Optional<String> method, Optional<String> sequence, Optional<String> recurrenceId) {
         this.ical = ical;
         this.sender = sender;
         this.recipient = recipient;
@@ -124,15 +121,15 @@ public class ICALAttributeDTO {
         return replyTo;
     }
 
-    public String getUid() {
+    public Optional<String> getUid() {
         return uid;
     }
 
-    public String getDtstamp() {
+    public Optional<String> getDtstamp() {
         return dtstamp;
     }
 
-    public String getMethod() {
+    public Optional<String> getMethod() {
         return method;
     }
 
diff --git a/mailet/icalendar/src/test/java/org/apache/james/transport/mailets/ICALToJsonAttributeTest.java b/mailet/icalendar/src/test/java/org/apache/james/transport/mailets/ICALToJsonAttributeTest.java
index f85a3de513..5fe45e8f01 100644
--- a/mailet/icalendar/src/test/java/org/apache/james/transport/mailets/ICALToJsonAttributeTest.java
+++ b/mailet/icalendar/src/test/java/org/apache/james/transport/mailets/ICALToJsonAttributeTest.java
@@ -24,7 +24,6 @@ import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatThrownBy;
 
 import java.io.ByteArrayInputStream;
-import java.io.Serializable;
 import java.nio.charset.StandardCharsets;
 import java.util.List;
 import java.util.Map;
@@ -487,7 +486,7 @@ public class ICALToJsonAttributeTest {
         testee.init(FakeMailetConfig.builder().build());
 
         AttributeValue<byte[]> ics = AttributeValue.of(ClassLoaderUtils.getSystemResourceAsByteArray("ics/meeting.ics"));
-        AttributeValue<byte[]> ics2 = AttributeValue.of(ClassLoaderUtils.getSystemResourceAsByteArray("ics/meeting_without_uid.ics"));
+        AttributeValue<byte[]> ics2 = AttributeValue.of(ClassLoaderUtils.getSystemResourceAsByteArray("ics/no_event.ics"));
         AttributeValue<Object> calendar = AttributeValue.ofUnserializable(new CalendarBuilder().build(new ByteArrayInputStream(ics.getValue())));
         AttributeValue<Object> calendar2 = AttributeValue.ofUnserializable(new CalendarBuilder().build(new ByteArrayInputStream(ics2.getValue())));
         Map<String, AttributeValue<?>> icals = ImmutableMap.of("key", calendar, "key2", calendar2);
@@ -522,6 +521,123 @@ public class ICALToJsonAttributeTest {
                 });
     }
 
+    @Test
+    void serviceShouldNotFailUponMissingUid() throws Exception {
+        testee.init(FakeMailetConfig.builder().build());
+
+        AttributeValue<byte[]> ics2 = AttributeValue.of(ClassLoaderUtils.getSystemResourceAsByteArray("ics/meeting_without_uid.ics"));
+        AttributeValue<Object> calendar2 = AttributeValue.ofUnserializable(new CalendarBuilder().build(new ByteArrayInputStream(ics2.getValue())));
+        Map<String, AttributeValue<?>> icals = ImmutableMap.of("key2", calendar2);
+        Map<String, AttributeValue<?>> rawIcals = ImmutableMap.of("key2", ics2);
+        MailAddress recipient = MailAddressFixture.OTHER_AT_JAMES;
+        Mail mail = FakeMail.builder()
+            .name("mail")
+            .sender(SENDER)
+            .recipient(recipient)
+            .attribute(new Attribute(ICALToJsonAttribute.DEFAULT_SOURCE, AttributeValue.of(icals)))
+            .attribute(new Attribute(ICALToJsonAttribute.DEFAULT_RAW_SOURCE, AttributeValue.ofAny(rawIcals)))
+            .build();
+
+        testee.service(mail);
+
+        assertThat(AttributeUtils.getValueAndCastFromMail(mail, ICALToJsonAttribute.DEFAULT_DESTINATION, MAP_STRING_BYTES_CLASS))
+            .isPresent()
+            .hasValueSatisfying(jsons -> {
+                assertThat(jsons).hasSize(1);
+                List<String> actual = toSortedValueList(jsons);
+
+                assertThatJson(actual.get(0)).isEqualTo("{" +
+                    "\"ical\": \"" + toJsonValue(ics2.getValue()) + "\"," +
+                    "\"sender\": \"" + SENDER.asString() + "\"," +
+                    "\"recipient\": \"" + recipient.asString() + "\"," +
+                    "\"replyTo\": \"" + SENDER.asString() + "\"," +
+                    "\"uid\": null," +
+                    "\"sequence\": \"0\"," +
+                    "\"dtstamp\": \"20170103T103250Z\"," +
+                    "\"method\": \"REQUEST\"," +
+                    "\"recurrence-id\": null" +
+                    "}");
+            });
+    }
+
+    @Test
+    void serviceShouldNotFailUponMissingMethod() throws Exception {
+        testee.init(FakeMailetConfig.builder().build());
+
+        AttributeValue<byte[]> ics2 = AttributeValue.of(ClassLoaderUtils.getSystemResourceAsByteArray("ics/meeting_without_method.ics"));
+        AttributeValue<Object> calendar2 = AttributeValue.ofUnserializable(new CalendarBuilder().build(new ByteArrayInputStream(ics2.getValue())));
+        Map<String, AttributeValue<?>> icals = ImmutableMap.of("key2", calendar2);
+        Map<String, AttributeValue<?>> rawIcals = ImmutableMap.of("key2", ics2);
+        MailAddress recipient = MailAddressFixture.OTHER_AT_JAMES;
+        Mail mail = FakeMail.builder()
+            .name("mail")
+            .sender(SENDER)
+            .recipient(recipient)
+            .attribute(new Attribute(ICALToJsonAttribute.DEFAULT_SOURCE, AttributeValue.of(icals)))
+            .attribute(new Attribute(ICALToJsonAttribute.DEFAULT_RAW_SOURCE, AttributeValue.ofAny(rawIcals)))
+            .build();
+
+        testee.service(mail);
+
+        assertThat(AttributeUtils.getValueAndCastFromMail(mail, ICALToJsonAttribute.DEFAULT_DESTINATION, MAP_STRING_BYTES_CLASS))
+            .isPresent()
+            .hasValueSatisfying(jsons -> {
+                assertThat(jsons).hasSize(1);
+                List<String> actual = toSortedValueList(jsons);
+
+                assertThatJson(actual.get(0)).isEqualTo("{" +
+                    "\"ical\": \"" + toJsonValue(ics2.getValue()) + "\"," +
+                    "\"sender\": \"" + SENDER.asString() + "\"," +
+                    "\"recipient\": \"" + recipient.asString() + "\"," +
+                    "\"replyTo\": \"" + SENDER.asString() + "\"," +
+                    "\"uid\": \"f1514f44bf39311568d64072ac247c17656ceafde3b4b3eba961c8c5184cdc6ee047feb2aab16e43439a608f28671ab7c10e754c301b1e32001ad51dd20eac2fc7af20abf4093bbe\"," +
+                    "\"sequence\": \"0\"," +
+                    "\"dtstamp\": \"20170103T103250Z\"," +
+                    "\"method\": null," +
+                    "\"recurrence-id\": null" +
+                    "}");
+            });
+    }
+
+    @Test
+    void serviceShouldNotFailUponMissingDtStamp() throws Exception {
+        testee.init(FakeMailetConfig.builder().build());
+
+        AttributeValue<byte[]> ics2 = AttributeValue.of(ClassLoaderUtils.getSystemResourceAsByteArray("ics/meeting_without_dtstamp.ics"));
+        AttributeValue<Object> calendar2 = AttributeValue.ofUnserializable(new CalendarBuilder().build(new ByteArrayInputStream(ics2.getValue())));
+        Map<String, AttributeValue<?>> icals = ImmutableMap.of("key2", calendar2);
+        Map<String, AttributeValue<?>> rawIcals = ImmutableMap.of("key2", ics2);
+        MailAddress recipient = MailAddressFixture.OTHER_AT_JAMES;
+        Mail mail = FakeMail.builder()
+            .name("mail")
+            .sender(SENDER)
+            .recipient(recipient)
+            .attribute(new Attribute(ICALToJsonAttribute.DEFAULT_SOURCE, AttributeValue.of(icals)))
+            .attribute(new Attribute(ICALToJsonAttribute.DEFAULT_RAW_SOURCE, AttributeValue.ofAny(rawIcals)))
+            .build();
+
+        testee.service(mail);
+
+        assertThat(AttributeUtils.getValueAndCastFromMail(mail, ICALToJsonAttribute.DEFAULT_DESTINATION, MAP_STRING_BYTES_CLASS))
+            .isPresent()
+            .hasValueSatisfying(jsons -> {
+                assertThat(jsons).hasSize(1);
+                List<String> actual = toSortedValueList(jsons);
+
+                assertThatJson(actual.get(0)).isEqualTo("{" +
+                    "\"ical\": \"" + toJsonValue(ics2.getValue()) + "\"," +
+                    "\"sender\": \"" + SENDER.asString() + "\"," +
+                    "\"recipient\": \"" + recipient.asString() + "\"," +
+                    "\"replyTo\": \"" + SENDER.asString() + "\"," +
+                    "\"uid\": \"f1514f44bf39311568d640727cff54e819573448d09d2e5677987ff29caa01a9e047feb2aab16e43439a608f28671ab7c10e754ce92be513f8e04ae9ff15e65a9819cf285a6962bc\"," +
+                    "\"sequence\": \"0\"," +
+                    "\"dtstamp\": null," +
+                    "\"method\": \"REQUEST\"," +
+                    "\"recurrence-id\": null" +
+                    "}");
+            });
+    }
+
     @Test
     void serviceShouldFilterNonExistingKeys() throws Exception {
         testee.init(FakeMailetConfig.builder().build());
diff --git a/mailet/icalendar/src/test/java/org/apache/james/transport/mailets/model/ICALAttributeDTOTest.java b/mailet/icalendar/src/test/java/org/apache/james/transport/mailets/model/ICALAttributeDTOTest.java
index 3f577d9e4c..cab15c36e2 100644
--- a/mailet/icalendar/src/test/java/org/apache/james/transport/mailets/model/ICALAttributeDTOTest.java
+++ b/mailet/icalendar/src/test/java/org/apache/james/transport/mailets/model/ICALAttributeDTOTest.java
@@ -20,7 +20,6 @@
 package org.apache.james.transport.mailets.model;
 
 import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatThrownBy;
 
 import java.io.ByteArrayInputStream;
 
@@ -78,51 +77,51 @@ class ICALAttributeDTOTest {
     }
 
     @Test
-    void buildShouldThrowOnCalendarWithoutDtstamp() throws Exception {
-        byte[] ics = ClassLoaderUtils.getSystemResourceAsByteArray("ics/meeting_without_dtstamp.ics");
+    void dtoShouldNotFailUponMissingUid() throws Exception {
+        byte[] ics = ClassLoaderUtils.getSystemResourceAsByteArray("ics/meeting_without_uid.ics");
         Calendar calendar = new CalendarBuilder().build(new ByteArrayInputStream(ics));
 
         MailAddress recipient = MailAddressFixture.ANY_AT_JAMES;
         MailAddress sender = MailAddressFixture.OTHER_AT_JAMES;
+        ICALAttributeDTO ical = ICALAttributeDTO.builder()
+            .from(calendar, ics)
+            .sender(sender)
+            .recipient(recipient)
+            .replyTo(sender);
 
-        assertThatThrownBy(() -> ICALAttributeDTO.builder()
-                .from(calendar, ics)
-                .sender(sender)
-                .recipient(recipient)
-                .replyTo(sender))
-            .isInstanceOf(IllegalStateException.class);
+        assertThat(ical.getUid()).isEmpty();
     }
 
     @Test
-    void buildShouldThrowOnCalendarWithoutUid() throws Exception {
-        byte[] ics = ClassLoaderUtils.getSystemResourceAsByteArray("ics/meeting_without_uid.ics");
+    void dtoShouldNotFailUponMissingMethod() throws Exception {
+        byte[] ics = ClassLoaderUtils.getSystemResourceAsByteArray("ics/meeting_without_method.ics");
         Calendar calendar = new CalendarBuilder().build(new ByteArrayInputStream(ics));
 
         MailAddress recipient = MailAddressFixture.ANY_AT_JAMES;
         MailAddress sender = MailAddressFixture.OTHER_AT_JAMES;
+        ICALAttributeDTO ical = ICALAttributeDTO.builder()
+            .from(calendar, ics)
+            .sender(sender)
+            .recipient(recipient)
+            .replyTo(sender);
 
-        assertThatThrownBy(() -> ICALAttributeDTO.builder()
-                .from(calendar, ics)
-                .sender(sender)
-                .recipient(recipient)
-                .replyTo(sender))
-            .isInstanceOf(IllegalStateException.class);
+        assertThat(ical.getMethod()).isEmpty();
     }
 
     @Test
-    void buildShouldThrowOnCalendarWithoutMethod() throws Exception {
-        byte[] ics = ClassLoaderUtils.getSystemResourceAsByteArray("ics/meeting_without_method.ics");
+    void dtoShouldNotFailUponMissingDtStamp() throws Exception {
+        byte[] ics = ClassLoaderUtils.getSystemResourceAsByteArray("ics/meeting_without_dtstamp.ics");
         Calendar calendar = new CalendarBuilder().build(new ByteArrayInputStream(ics));
 
         MailAddress recipient = MailAddressFixture.ANY_AT_JAMES;
         MailAddress sender = MailAddressFixture.OTHER_AT_JAMES;
+        ICALAttributeDTO ical = ICALAttributeDTO.builder()
+            .from(calendar, ics)
+            .sender(sender)
+            .recipient(recipient)
+            .replyTo(sender);
 
-        assertThatThrownBy(() -> ICALAttributeDTO.builder()
-                .from(calendar, ics)
-                .sender(sender)
-                .recipient(recipient)
-                .replyTo(sender))
-            .isInstanceOf(IllegalStateException.class);
+        assertThat(ical.getDtstamp()).isEmpty();
     }
 
     @Test


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org