JAMES-1717 Add interfaces for storing users vacations
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/30e7a476 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/30e7a476 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/30e7a476 Branch: refs/heads/master Commit: 30e7a476cf5c22bf4a4271f874a143364756f56c Parents: 5619dbc Author: Benoit Tellier <[email protected]> Authored: Tue Apr 5 18:20:08 2016 +0700 Committer: Benoit Tellier <[email protected]> Committed: Fri Apr 22 15:28:59 2016 +0700 ---------------------------------------------------------------------- .../james/jmap/api/vacation/AccountId.java | 58 +++++++++ .../james/jmap/api/vacation/Vacation.java | 126 +++++++++++++++++++ .../jmap/api/vacation/VacationRepository.java | 32 +++++ .../AbstractVacationRepositoryTest.java | 81 ++++++++++++ .../james/jmap/api/vacation/AccountIdTest.java | 45 +++++++ .../james/jmap/model/VacationResponse.java | 2 +- 6 files changed, 343 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/30e7a476/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/vacation/AccountId.java ---------------------------------------------------------------------- diff --git a/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/vacation/AccountId.java b/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/vacation/AccountId.java new file mode 100644 index 0000000..b1885dd --- /dev/null +++ b/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/vacation/AccountId.java @@ -0,0 +1,58 @@ +/**************************************************************** + * 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.jmap.api.vacation; + +import java.util.Objects; + +import com.google.common.base.Preconditions; +import com.google.common.base.Strings; + +public class AccountId { + + public static AccountId fromString(String identifier) { + Preconditions.checkArgument(!Strings.isNullOrEmpty(identifier), "AccountId identifier should not be null or empty"); + return new AccountId(identifier); + } + + private final String identifier; + + private AccountId(String identifier) { + this.identifier = identifier; + } + + public String getIdentifier() { + return identifier; + } + + @Override + public boolean equals(Object o) { + if (o == null || getClass() != o.getClass()) { + return false; + } + + AccountId accountId = (AccountId) o; + return Objects.equals(this.identifier, accountId.identifier); + } + + @Override + public int hashCode() { + return Objects.hash(identifier); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/30e7a476/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/vacation/Vacation.java ---------------------------------------------------------------------- diff --git a/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/vacation/Vacation.java b/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/vacation/Vacation.java new file mode 100644 index 0000000..5957d37 --- /dev/null +++ b/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/vacation/Vacation.java @@ -0,0 +1,126 @@ +/**************************************************************** + * 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.jmap.api.vacation; + +import java.time.ZonedDateTime; +import java.util.Objects; +import java.util.Optional; + +import com.google.common.base.Preconditions; + +public class Vacation { + + public static final String ID = "singleton"; + public static final boolean DEFAULT_DISABLED = false; + + public static Builder builder() { + return new Builder(); + } + + public static class Builder { + private Optional<Boolean> isEnabled = Optional.empty(); + private Optional<ZonedDateTime> fromDate = Optional.empty(); + private Optional<ZonedDateTime> toDate = Optional.empty(); + private String textBody = ""; + + public Builder enabled(boolean enabled) { + isEnabled = Optional.of(enabled); + return this; + } + + public Builder fromDate(Optional<ZonedDateTime> fromDate) { + Preconditions.checkNotNull(fromDate); + this.fromDate = fromDate; + return this; + } + + public Builder toDate(Optional<ZonedDateTime> toDate) { + Preconditions.checkNotNull(toDate); + this.toDate = toDate; + return this; + } + + public Builder textBody(String textBody) { + this.textBody = textBody; + return this; + } + + public Builder copy(Vacation vacation) { + this.textBody = vacation.getTextBody(); + this.fromDate = vacation.getFromDate(); + this.toDate = vacation.getToDate(); + this.isEnabled = Optional.of(vacation.isEnabled()); + return this; + } + + public Vacation build() { + Preconditions.checkNotNull(textBody); + return new Vacation(isEnabled.orElse(DEFAULT_DISABLED), fromDate, toDate, textBody); + } + } + + private final boolean isEnabled; + private final Optional<ZonedDateTime> fromDate; + private final Optional<ZonedDateTime> toDate; + private final String textBody; + + private Vacation(boolean isEnabled, Optional<ZonedDateTime> fromDate, Optional<ZonedDateTime> toDate, String textBody) { + this.isEnabled = isEnabled; + this.fromDate = fromDate; + this.toDate = toDate; + this.textBody = textBody; + } + + + public boolean isEnabled() { + return isEnabled; + } + + public Optional<ZonedDateTime> getFromDate() { + return fromDate; + } + + public Optional<ZonedDateTime> getToDate() { + return toDate; + } + + public String getTextBody() { + return textBody; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Vacation vacation = (Vacation) o; + + return Objects.equals(this.isEnabled, vacation.isEnabled) && + Objects.equals(this.fromDate, vacation.fromDate) && + Objects.equals(this.toDate, vacation.toDate) && + Objects.equals(this.textBody, vacation.textBody); + } + + @Override + public int hashCode() { + return Objects.hash(isEnabled, fromDate, toDate, textBody); + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/30e7a476/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/vacation/VacationRepository.java ---------------------------------------------------------------------- diff --git a/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/vacation/VacationRepository.java b/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/vacation/VacationRepository.java new file mode 100644 index 0000000..02ff1c6 --- /dev/null +++ b/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/vacation/VacationRepository.java @@ -0,0 +1,32 @@ +/**************************************************************** + * 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.jmap.api.vacation; + +import java.util.concurrent.CompletableFuture; + +public interface VacationRepository { + + Vacation DEFAULT_VACATION = Vacation.builder().enabled(false).build(); + + CompletableFuture<Void> modifyVacation(AccountId accountId, Vacation vacation); + + CompletableFuture<Vacation> retrieveVacation(AccountId accountId); + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/30e7a476/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/vacation/AbstractVacationRepositoryTest.java ---------------------------------------------------------------------- diff --git a/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/vacation/AbstractVacationRepositoryTest.java b/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/vacation/AbstractVacationRepositoryTest.java new file mode 100644 index 0000000..ba15584 --- /dev/null +++ b/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/vacation/AbstractVacationRepositoryTest.java @@ -0,0 +1,81 @@ +/**************************************************************** + * 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.jmap.api.vacation; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.time.ZonedDateTime; +import java.util.Optional; + +import org.junit.Before; +import org.junit.Test; + +public abstract class AbstractVacationRepositoryTest { + + public static final AccountId ACCOUNT_ID = AccountId.fromString("identifier"); + public static final ZonedDateTime ZONED_DATE_TIME = ZonedDateTime.parse("2016-04-03T02:01+07:00[Asia/Vientiane]"); + public static final Vacation VACATION_1 = Vacation.builder().enabled(true).build(); + public static final Vacation VACATION_2 = Vacation.builder().fromDate(Optional.of(ZONED_DATE_TIME)).enabled(true).build(); + + private VacationRepository vacationRepository; + + protected abstract VacationRepository createVacationRepository(); + + @Before + public void setUp() { + vacationRepository = createVacationRepository(); + } + + @Test + public void retrieveVacationShouldReturnDefaultValueByDefault() { + assertThat(vacationRepository.retrieveVacation(ACCOUNT_ID).join()).isEqualTo(VacationRepository.DEFAULT_VACATION); + } + + @Test + public void modifyVacationShouldWork() { + vacationRepository.modifyVacation(ACCOUNT_ID, VACATION_1).join(); + + assertThat(vacationRepository.retrieveVacation(ACCOUNT_ID).join()).isEqualTo(VACATION_1); + } + + @Test + public void modifyVacationShouldReplacePreviousValue() { + vacationRepository.modifyVacation(ACCOUNT_ID, VACATION_1).join(); + vacationRepository.modifyVacation(ACCOUNT_ID, VACATION_2).join(); + + assertThat(vacationRepository.retrieveVacation(ACCOUNT_ID).join()).isEqualTo(VACATION_2); + } + + @Test(expected = NullPointerException.class) + public void retrieveVacationShouldThrowOnNullAccountId() { + vacationRepository.retrieveVacation(null); + } + + @Test(expected = NullPointerException.class) + public void modifyVacationShouldThrowOnNullAccountId() { + vacationRepository.modifyVacation(null, VACATION_1); + } + + @Test(expected = NullPointerException.class) + public void modifyVacationShouldThrowOnNullVacation() { + vacationRepository.modifyVacation(ACCOUNT_ID, null); + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/30e7a476/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/vacation/AccountIdTest.java ---------------------------------------------------------------------- diff --git a/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/vacation/AccountIdTest.java b/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/vacation/AccountIdTest.java new file mode 100644 index 0000000..4362273 --- /dev/null +++ b/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/vacation/AccountIdTest.java @@ -0,0 +1,45 @@ +/**************************************************************** + * 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.jmap.api.vacation; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Test; + +public class AccountIdTest { + + public static final String IDENTIFIER = "id"; + + @Test(expected = IllegalArgumentException.class) + public void createShouldThrowOnNullIdentifier() { + AccountId.fromString(null); + } + + @Test(expected = IllegalArgumentException.class) + public void createShouldThrowOnEmptyIdentifier() { + AccountId.fromString(""); + } + + @Test + public void createShouldWork() { + assertThat(AccountId.fromString(IDENTIFIER).getIdentifier()).isEqualTo(IDENTIFIER); + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/30e7a476/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/VacationResponse.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/VacationResponse.java b/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/VacationResponse.java index 4af3c5f..d81607e 100644 --- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/VacationResponse.java +++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/VacationResponse.java @@ -72,7 +72,7 @@ public class VacationResponse { } public Builder fromVacation(Vacation vacation) { - this.id = vacation.getId(); + this.id = Vacation.ID; this.isEnabled = vacation.isEnabled(); this.fromDate = vacation.getFromDate(); this.toDate = vacation.getToDate(); --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
