diff --git a/webcal/src/main/java/com/onixbyte/icalendar/datatype/CalendarUserAddress.java b/webcal/src/main/java/com/onixbyte/icalendar/datatype/CalendarUserAddress.java index 25f7df0..3625bab 100644 --- a/webcal/src/main/java/com/onixbyte/icalendar/datatype/CalendarUserAddress.java +++ b/webcal/src/main/java/com/onixbyte/icalendar/datatype/CalendarUserAddress.java @@ -30,6 +30,14 @@ public final class CalendarUserAddress { this.value = value; } + public CalendarUserAddress(String value) { + var uri = URI.create(value); + if (!"mailto".equalsIgnoreCase(uri.getScheme())) { + throw new IllegalArgumentException("Calendar User Address (CAL-ADDRESS) only accept mailto URI."); + } + this.value = uri; + } + @Override public String toString() { return value.toString(); diff --git a/webcal/src/main/java/com/onixbyte/icalendar/property/parameter/Delegatee.java b/webcal/src/main/java/com/onixbyte/icalendar/property/parameter/Delegatee.java index bcfae0e..e60a852 100644 --- a/webcal/src/main/java/com/onixbyte/icalendar/property/parameter/Delegatee.java +++ b/webcal/src/main/java/com/onixbyte/icalendar/property/parameter/Delegatee.java @@ -22,6 +22,7 @@ import com.onixbyte.icalendar.datatype.CalendarUserAddress; import java.net.URI; import java.util.ArrayList; import java.util.List; +import java.util.function.Supplier; /** * Delegate @@ -38,6 +39,10 @@ public final class Delegatee implements PropertyParameter { this.value = value; } + public static Builder builder() { + return new Builder(); + } + public static class Builder { private List value; @@ -45,8 +50,28 @@ public final class Delegatee implements PropertyParameter { this.value = new ArrayList<>(); } - public Builder addDelegatee(URI delegateeUri) { - value.add(new CalendarUserAddress(delegateeUri)); + public Builder addDelegatee(CalendarUserAddress delegatee) { + value.add(delegatee); + return this; + } + + public Builder addDelegatee(URI delegatee) { + value.add(new CalendarUserAddress(delegatee)); + return this; + } + + public Builder addDelegatee(String delegatee) { + value.add(new CalendarUserAddress(delegatee)); + return this; + } + + public Builder addDelegatees(List delegatees) { + value.addAll(delegatees); + return this; + } + + public Builder addDelegatees(Supplier> delegatees) { + value.addAll(delegatees.get()); return this; } diff --git a/webcal/src/main/resources/logback.xml b/webcal/src/main/resources/logback.xml index 5ba9d1b..1882442 100644 --- a/webcal/src/main/resources/logback.xml +++ b/webcal/src/main/resources/logback.xml @@ -1,6 +1,6 @@ - + diff --git a/webcal/src/test/java/com/onixbyte/icalendar/test/DelegateeTest.java b/webcal/src/test/java/com/onixbyte/icalendar/test/DelegateeTest.java new file mode 100644 index 0000000..373f756 --- /dev/null +++ b/webcal/src/test/java/com/onixbyte/icalendar/test/DelegateeTest.java @@ -0,0 +1,125 @@ +/* + * Copyright (C) 2024-2024 OnixByte. + * + * 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. + */ + +package com.onixbyte.icalendar.test; + +import com.onixbyte.icalendar.datatype.CalendarUserAddress; +import com.onixbyte.icalendar.property.parameter.Delegatee; +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.Test; + +import java.net.URI; +import java.util.List; +import java.util.stream.Stream; + +@Slf4j +public class DelegateeTest { + + @Test + void testAddStringDelegatee() { + var delegatee = Delegatee.builder() + .addDelegatee("mailto:user01@example.onixbyte.com") + .build() + .resolve(); + log.info("#testAddStringDelegatee {}", delegatee); + } + + @Test + void testAddUriDelegatee() { + var delegatee = Delegatee.builder() + .addDelegatee(URI.create("mailto:user01@example.onixbyte.com")) + .build() + .resolve(); + log.info("#testAddUriDelegatee {}", delegatee); + } + + @Test + void testAddCauDelegateeWithStringConstructor() { + var delegatee = Delegatee.builder() + .addDelegatee(new CalendarUserAddress("mailto:user01@example.onixbyte.com")) + .build() + .resolve(); + log.info("#testAddCauDelegateeWithStringConstructor {}", delegatee); + } + + @Test + void testAddCauDelegateeWithUriConstructor() { + var delegatee = Delegatee.builder() + .addDelegatee(new CalendarUserAddress(URI.create("mailto:user01@example.onixbyte.com"))) + .build() + .resolve(); + log.info("#testAddCauDelegateeWithUriConstructor {}", delegatee); + } + + @Test + void testAddDelegateeSeparately() { + var delegatee = Delegatee.builder() + .addDelegatee(new CalendarUserAddress(URI.create("mailto:user01@example.onixbyte.com"))) + .addDelegatee(URI.create("mailto:user02@example.onixbyte.com")) + .addDelegatee("mailto:user03@example.onixbyte.com") + .build() + .resolve(); + log.info("#testAddDelegateeSeparately {}", delegatee); + } + + @Test + void testAddDelegateeSeparatelyWithIncorrectUri() { + try { + var delegatee = Delegatee.builder() + .addDelegatee(new CalendarUserAddress(URI.create("mailto:user01@example.onixbyte.com"))) + .addDelegatee(URI.create("mailto:user02@example.onixbyte.com")) + .addDelegatee("https://example.onixbyte.com") + .build() + .resolve(); + log.info("#testAddDelegateeSeparatelyWithIncorrectUri {}", delegatee); + } catch (IllegalArgumentException iae) { + log.error("#testAddDelegateeSeparatelyWithIncorrectUri {}", iae.getMessage()); + } + } + + @Test + void testAddDelegateesWithCau() { + try { + var delegatee = Delegatee.builder() + .addDelegatees(List.of( + new CalendarUserAddress("mailto:user01@example.onixbyte.com"), + new CalendarUserAddress("mailto:user02@example.onixbyte.com") + )) + .build() + .resolve(); + log.info("#testAddDelegateesWithCau {}", delegatee); + } catch (IllegalArgumentException iae) { + log.error("#testAddDelegateesWithCau {}", iae.getMessage()); + } + } + + @Test + void testAddDelegateesWithSupplier() { + try { + var delegatee = Delegatee.builder() + .addDelegatees(() -> Stream.of("mailto:user01@example.onixbyte.com", "mailto:user02@example.onixbyte.com") + .map(CalendarUserAddress::new) + .toList()) + .build() + .resolve(); + log.info("#testAddDelegateesWithSupplier {}", delegatee); + } catch (IllegalArgumentException iae) { + log.error("#testAddDelegateesWithSupplier {}", iae.getMessage()); + } + } + +} diff --git a/webcal/src/test/java/com/onixbyte/icalendar/test/TestCalendar.java b/webcal/src/test/java/com/onixbyte/icalendar/test/TestCalendar.java deleted file mode 100644 index a95b0fa..0000000 --- a/webcal/src/test/java/com/onixbyte/icalendar/test/TestCalendar.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (C) 2024-2024 OnixByte. - * - * 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. - */ - -package com.onixbyte.icalendar.test; - -import com.onixbyte.icalendar.component.Calendar; -import lombok.extern.slf4j.Slf4j; -import org.junit.jupiter.api.Test; - -import java.time.LocalDateTime; -import java.util.UUID; - -/** - * TestWebCalendar - * - * @author Zihlu Wang - */ -@Slf4j -public class TestCalendar { - - @Test - void testWebCalendar() { - - } - -}