james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [09/13] james-project git commit: JAMES-1717 Implement SetVacationResponseMethod
Date Fri, 22 Apr 2016 08:33:17 GMT
JAMES-1717 Implement SetVacationResponseMethod


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

Branch: refs/heads/master
Commit: 6e72afa88132872ff0daea7485d0375b24516fb3
Parents: d690b55
Author: Benoit Tellier <btellier@linagora.com>
Authored: Fri Apr 8 09:28:19 2016 +0700
Committer: Benoit Tellier <btellier@linagora.com>
Committed: Fri Apr 22 15:29:29 2016 +0700

----------------------------------------------------------------------
 .../apache/james/utils/ExtendedServerProbe.java |   2 +
 .../apache/james/utils/GuiceServerProbe.java    |   5 +
 .../integration/SetVacationResponseTest.java    | 188 ++++++++++++++++-
 .../jmap/methods/SetVacationResponseMethod.java |  81 ++++++-
 .../james/jmap/model/SetVacationRequest.java    |   7 +
 .../james/jmap/model/VacationResponse.java      |   6 +
 .../methods/SetVacationResponseMethodTest.java  | 210 ++++++++++++++++++-
 7 files changed, 485 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/6e72afa8/server/container/guice/guice-common/src/main/java/org/apache/james/utils/ExtendedServerProbe.java
----------------------------------------------------------------------
diff --git a/server/container/guice/guice-common/src/main/java/org/apache/james/utils/ExtendedServerProbe.java
b/server/container/guice/guice-common/src/main/java/org/apache/james/utils/ExtendedServerProbe.java
index d380a2b..dc066c8 100644
--- a/server/container/guice/guice-common/src/main/java/org/apache/james/utils/ExtendedServerProbe.java
+++ b/server/container/guice/guice-common/src/main/java/org/apache/james/utils/ExtendedServerProbe.java
@@ -41,4 +41,6 @@ public interface ExtendedServerProbe<Id extends MailboxId> extends
ServerProbe {
     Mailbox<Id> getMailbox(String namespace, String user, String name);
 
     void modifyVacation(AccountId accountId, Vacation vacation);
+
+    Vacation retrieveVacation(AccountId accountId);
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/6e72afa8/server/container/guice/guice-common/src/main/java/org/apache/james/utils/GuiceServerProbe.java
----------------------------------------------------------------------
diff --git a/server/container/guice/guice-common/src/main/java/org/apache/james/utils/GuiceServerProbe.java
b/server/container/guice/guice-common/src/main/java/org/apache/james/utils/GuiceServerProbe.java
index fa5e48a..fb33a94 100644
--- a/server/container/guice/guice-common/src/main/java/org/apache/james/utils/GuiceServerProbe.java
+++ b/server/container/guice/guice-common/src/main/java/org/apache/james/utils/GuiceServerProbe.java
@@ -355,4 +355,9 @@ public class GuiceServerProbe<Id extends MailboxId> implements ExtendedServerPro
     public void modifyVacation(AccountId accountId, Vacation vacation) {
         vacationRepository.modifyVacation(accountId, vacation).join();
     }
+
+    @Override
+    public Vacation retrieveVacation(AccountId accountId) {
+        return vacationRepository.retrieveVacation(accountId).join();
+    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/6e72afa8/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SetVacationResponseTest.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SetVacationResponseTest.java
b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SetVacationResponseTest.java
index 5a77c55..0b7e935 100644
--- a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SetVacationResponseTest.java
+++ b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SetVacationResponseTest.java
@@ -22,11 +22,17 @@ package org.apache.james.jmap.methods.integration;
 import static com.jayway.restassured.RestAssured.given;
 import static com.jayway.restassured.config.EncoderConfig.encoderConfig;
 import static com.jayway.restassured.config.RestAssuredConfig.newConfig;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.hamcrest.Matchers.containsString;
 import static org.hamcrest.Matchers.equalTo;
 
+import java.time.ZonedDateTime;
+
 import org.apache.james.GuiceJamesServer;
 import org.apache.james.jmap.JmapAuthentication;
 import org.apache.james.jmap.api.access.AccessToken;
+import org.apache.james.jmap.api.vacation.AccountId;
+import org.apache.james.jmap.api.vacation.Vacation;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -70,14 +76,180 @@ public abstract class SetVacationResponseTest {
     }
 
     @Test
-    public void setVacationResponseIsNotImplementedYet() {
-        String bodyRequest = "[[\"setVacationResponse\", " +
-            "{\"accountId\": \"1\", " +
-            "\"update\":{\"idVacation\" : {" +
-                "\"id\": \"1\"," +
-                "\"isEnabled\": \"true\"," +
-                "\"textBody\": \"Message explaining my wonderful vacations\"" +
-            "}}}, \"#0\"]]";
+    public void setVacationResponseShouldReturnErrorOnMalformedRequestStructure() {
+        String bodyRequest = "[[" +
+            "\"setVacationResponse\", " +
+            "{" +
+                "\"update\":{" +
+                    "\"idVacation\" : {" +
+                        "\"id\": \"1\"," +
+                        "\"isEnabled\": \"true\"," +
+                        "\"textBody\": \"Message explaining my wonderful vacations\"" +
+                    "}" +
+                "}" +
+            "}, " +
+            "\"#0\"" +
+            "]]";
+
+        given()
+            .accept(ContentType.JSON)
+            .contentType(ContentType.JSON)
+            .header("Authorization", accessToken.serialize())
+            .body(bodyRequest)
+        .when()
+            .post("/jmap")
+        .then()
+            .statusCode(200)
+            .body(NAME, equalTo("error"))
+            .body(ARGUMENTS + ".type", equalTo("invalidArguments"))
+            .body(ARGUMENTS + ".description", equalTo("update field should just contain one
entry with key \"singleton\""));
+    }
+
+    @Test
+    public void setVacationResponseShouldContainAnErrorWhenInvalidId() {
+        String bodyRequest = "[[" +
+            "\"setVacationResponse\", " +
+            "{" +
+                "\"update\":{" +
+                    "\"singleton\" : {" +
+                        "\"id\": \"1\"," +
+                        "\"isEnabled\": \"true\"," +
+                        "\"textBody\": \"Message explaining my wonderful vacations\"" +
+                    "}" +
+                "}" +
+            "}, " +
+            "\"#0\"" +
+            "]]";
+
+        given()
+            .accept(ContentType.JSON)
+            .contentType(ContentType.JSON)
+            .header("Authorization", accessToken.serialize())
+            .body(bodyRequest)
+        .when()
+            .post("/jmap")
+        .then()
+            .statusCode(200)
+            .body(NAME, equalTo("vacationResponseSet"))
+            .body(ARGUMENTS + ".notUpdated.singleton.type", equalTo("invalidArguments"))
+            .body(ARGUMENTS + ".notUpdated.singleton.description", equalTo("There is one
VacationResponse object per account, with id set to \"singleton\" and not to 1"));
+    }
+
+    @Test
+    public void setVacationResponseShouldReturnCorrectAnswerUponValidVacationResponse() {
+        String bodyRequest = "[[" +
+            "\"setVacationResponse\", " +
+            "{" +
+                "\"update\":{" +
+                    "\"singleton\" : {" +
+                        "\"id\": \"singleton\"," +
+                        "\"isEnabled\": \"true\"," +
+                        "\"textBody\": \"Message explaining my wonderful vacations\"," +
+                        "\"fromDate\":\"2014-09-30T14:10:00Z[GMT]\"," +
+                        "\"toDate\":\"2014-10-30T14:10:00Z[GMT]\"" +
+                    "}" +
+                "}" +
+            "}, " +
+            "\"#0\"" +
+            "]]";
+
+        given()
+            .accept(ContentType.JSON)
+            .contentType(ContentType.JSON)
+            .header("Authorization", accessToken.serialize())
+            .body(bodyRequest)
+        .when()
+            .post("/jmap")
+        .then()
+            .statusCode(200)
+            .body(NAME, equalTo("vacationResponseSet"))
+            .body(ARGUMENTS + ".updated[0]", equalTo("singleton"));
+
+        Vacation vacation = jmapServer.serverProbe().retrieveVacation(AccountId.fromString(USER));
+        assertThat(vacation.getTextBody()).isEqualTo("Message explaining my wonderful vacations");
+        assertThat(vacation.isEnabled()).isTrue();
+        assertThat(vacation.getFromDate()).contains(ZonedDateTime.parse("2014-09-30T14:10:00Z[GMT]"));
+        assertThat(vacation.getToDate()).contains(ZonedDateTime.parse("2014-10-30T14:10:00Z[GMT]"));
+    }
+
+    @Test
+    public void nullTextBodyShouldBeRejected() {
+        String bodyRequest = "[[" +
+            "\"setVacationResponse\", " +
+            "{" +
+                "\"update\":{" +
+                    "\"singleton\" : {" +
+                        "\"id\": \"singleton\"," +
+                        "\"isEnabled\": \"true\"," +
+                        "\"textBody\": null" +
+                    "}" +
+                "}" +
+            "}, " +
+            "\"#0\"" +
+            "]]";
+
+        given()
+            .accept(ContentType.JSON)
+            .contentType(ContentType.JSON)
+            .header("Authorization", accessToken.serialize())
+            .body(bodyRequest)
+        .when()
+            .post("/jmap")
+        .then()
+            .statusCode(200)
+            .body(NAME, equalTo("error"))
+            .body(ARGUMENTS + ".type", equalTo("invalidArguments"))
+            .body(ARGUMENTS + ".description", containsString("textBody property of vacationResponse
object should not be null"));
+    }
+
+    @Test
+    public void noTextBodyShouldBeRejected() {
+        String bodyRequest = "[[" +
+            "\"setVacationResponse\", " +
+            "{" +
+                "\"update\":{" +
+                    "\"singleton\" : {" +
+                        "\"id\": \"singleton\"," +
+                        "\"isEnabled\": \"true\"" +
+                    "}" +
+                "}" +
+            "}, " +
+            "\"#0\"" +
+            "]]";
+
+        given()
+            .accept(ContentType.JSON)
+            .contentType(ContentType.JSON)
+            .header("Authorization", accessToken.serialize())
+            .body(bodyRequest)
+        .when()
+            .post("/jmap")
+        .then()
+            .statusCode(200)
+            .body(NAME, equalTo("error"))
+            .body(ARGUMENTS + ".type", equalTo("invalidArguments"))
+            .body(ARGUMENTS + ".description", containsString("textBody property of vacationResponse
object should not be null"));
+    }
+
+    @Test
+    public void accountIdIsNotSupported() {
+        String bodyRequest = "[[" +
+            "\"setVacationResponse\", " +
+            "{" +
+                "\"accountId\": \"1\"," +
+                "\"update\":{" +
+                    "\"singleton\" : {" +
+                        "\"id\": \"singleton\"," +
+                        "\"isEnabled\": \"true\"," +
+                        "\"textBody\": \"Message explaining my wonderful vacations\"," +
+                        "\"fromDate\":\"2014-09-30T14:10:00Z\"," +
+                        "\"toDate\":\"2014-10-30T14:10:00Z\"" +
+                    "}" +
+                "}" +
+            "}, " +
+            "\"#0\"" +
+            "]]";
+
         given()
             .accept(ContentType.JSON)
             .contentType(ContentType.JSON)

http://git-wip-us.apache.org/repos/asf/james-project/blob/6e72afa8/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetVacationResponseMethod.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetVacationResponseMethod.java
b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetVacationResponseMethod.java
index fde83d2..dde98f5 100644
--- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetVacationResponseMethod.java
+++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetVacationResponseMethod.java
@@ -21,14 +21,35 @@ package org.apache.james.jmap.methods;
 
 import java.util.stream.Stream;
 
-import org.apache.commons.lang.NotImplementedException;
+import javax.inject.Inject;
+
+import org.apache.james.jmap.api.vacation.AccountId;
+import org.apache.james.jmap.api.vacation.Vacation;
+import org.apache.james.jmap.api.vacation.VacationRepository;
 import org.apache.james.jmap.model.ClientId;
+import org.apache.james.jmap.model.SetError;
 import org.apache.james.jmap.model.SetVacationRequest;
+import org.apache.james.jmap.model.SetVacationResponse;
+import org.apache.james.jmap.model.VacationResponse;
 import org.apache.james.mailbox.MailboxSession;
 
+import com.google.common.base.Preconditions;
+
 public class SetVacationResponseMethod implements Method {
 
     public static final Request.Name METHOD_NAME = Request.name("setVacationResponse");
+    public static final Response.Name RESPONSE_NAME = Response.name("vacationResponseSet");
+    public static final String INVALID_ARGUMENTS = "invalidArguments";
+    public static final String ERROR_MESSAGE_BASE = "There is one VacationResponse object
per account, with id set to \"singleton\" and not to ";
+    public static final String INVALID_ARGUMENTS1 = "invalidArguments";
+    public static final String INVALID_ARGUMENT_DESCRIPTION = "update field should just contain
one entry with key \"singleton\"";
+
+    private final VacationRepository vacationRepository;
+
+    @Inject
+    public SetVacationResponseMethod(VacationRepository vacationRepository) {
+        this.vacationRepository = vacationRepository;
+    }
 
     @Override
     public Request.Name requestHandled() {
@@ -42,6 +63,62 @@ public class SetVacationResponseMethod implements Method {
 
     @Override
     public Stream<JmapResponse> process(JmapRequest request, ClientId clientId, MailboxSession
mailboxSession) {
-        throw new NotImplementedException();
+        Preconditions.checkNotNull(request);
+        Preconditions.checkNotNull(clientId);
+        Preconditions.checkNotNull(mailboxSession);
+        Preconditions.checkArgument(request instanceof SetVacationRequest);
+        SetVacationRequest setVacationRequest = (SetVacationRequest) request;
+
+        if (!setVacationRequest.isValid()) {
+            return Stream.of(JmapResponse
+                .builder()
+                .clientId(clientId)
+                .error(ErrorResponse.builder()
+                    .type(INVALID_ARGUMENTS1)
+                    .description(INVALID_ARGUMENT_DESCRIPTION)
+                    .build())
+                .build());
+        }
+
+        return process(clientId,
+            AccountId.fromString(mailboxSession.getUser().getUserName()),
+            setVacationRequest.getUpdate().get(Vacation.ID));
     }
+
+
+
+    private Stream<JmapResponse> process(ClientId clientId, AccountId accountId, VacationResponse
vacationResponse) {
+        if (vacationResponse.isValid()) {
+            vacationRepository.modifyVacation(accountId, convertToVacation(vacationResponse)).join();
+            return Stream.of(JmapResponse.builder()
+                .clientId(clientId)
+                .responseName(RESPONSE_NAME)
+                .response(SetVacationResponse.builder()
+                    .updatedId(Vacation.ID)
+                    .build())
+                .build());
+        } else {
+            return Stream.of(JmapResponse.builder()
+                .clientId(clientId)
+                .responseName(RESPONSE_NAME)
+                .response(SetVacationResponse.builder()
+                    .notUpdated(Vacation.ID,
+                        SetError.builder()
+                            .type(INVALID_ARGUMENTS)
+                            .description(ERROR_MESSAGE_BASE + vacationResponse.getId())
+                            .build())
+                    .build())
+                .build());
+        }
+    }
+
+    public Vacation convertToVacation(VacationResponse vacationResponse) {
+        return Vacation.builder()
+            .enabled(vacationResponse.isEnabled())
+            .fromDate(vacationResponse.getFromDate())
+            .toDate(vacationResponse.getToDate())
+            .textBody(vacationResponse.getTextBody())
+            .build();
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/6e72afa8/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/SetVacationRequest.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/SetVacationRequest.java
b/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/SetVacationRequest.java
index 1ae21ef..cbf3bca 100644
--- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/SetVacationRequest.java
+++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/SetVacationRequest.java
@@ -22,8 +22,10 @@ package org.apache.james.jmap.model;
 import java.util.Map;
 
 import org.apache.commons.lang.NotImplementedException;
+import org.apache.james.jmap.api.vacation.Vacation;
 import org.apache.james.jmap.methods.JmapRequest;
 
+import com.fasterxml.jackson.annotation.JsonIgnore;
 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
 import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
 import com.google.common.collect.ImmutableMap;
@@ -70,4 +72,9 @@ public class SetVacationRequest implements JmapRequest {
     public Map<String, VacationResponse> getUpdate() {
         return update;
     }
+
+    @JsonIgnore
+    public boolean isValid() {
+        return update.entrySet().size() == 1 && update.containsKey(Vacation.ID);
+    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/6e72afa8/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 d81607e..593b29a 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
@@ -25,6 +25,7 @@ import java.util.Optional;
 
 import org.apache.james.jmap.api.vacation.Vacation;
 
+import com.fasterxml.jackson.annotation.JsonIgnore;
 import com.fasterxml.jackson.annotation.JsonProperty;
 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
 import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
@@ -121,6 +122,11 @@ public class VacationResponse {
         return textBody;
     }
 
+    @JsonIgnore
+    public boolean isValid() {
+        return id.equals(Vacation.ID);
+    }
+
     @Override
     public boolean equals(Object o) {
         if (o == null || getClass() != o.getClass()) {

http://git-wip-us.apache.org/repos/asf/james-project/blob/6e72afa8/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetVacationResponseMethodTest.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetVacationResponseMethodTest.java
b/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetVacationResponseMethodTest.java
index 61c4237..5244ccd 100644
--- a/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetVacationResponseMethodTest.java
+++ b/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetVacationResponseMethodTest.java
@@ -19,14 +19,216 @@
 
 package org.apache.james.jmap.methods;
 
-import org.apache.commons.lang.NotImplementedException;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.when;
+
+import java.util.List;
+import java.util.Locale;
+import java.util.concurrent.CompletableFuture;
+import java.util.stream.Stream;
+
+import org.apache.james.jmap.api.vacation.AccountId;
+import org.apache.james.jmap.api.vacation.Vacation;
+import org.apache.james.jmap.api.vacation.VacationRepository;
+import org.apache.james.jmap.model.ClientId;
+import org.apache.james.jmap.model.GetMailboxesRequest;
+import org.apache.james.jmap.model.SetError;
+import org.apache.james.jmap.model.SetMailboxesRequest;
+import org.apache.james.jmap.model.SetVacationRequest;
+import org.apache.james.jmap.model.SetVacationResponse;
+import org.apache.james.jmap.model.VacationResponse;
+import org.apache.james.mailbox.MailboxSession;
+import org.junit.Before;
 import org.junit.Test;
 
+import com.google.common.collect.ImmutableMap;
+
 public class SetVacationResponseMethodTest {
 
-    @Test(expected = NotImplementedException.class)
-    public void setVacationResponseMethodIsNotImplemented() {
-        new SetVacationResponseMethod().process(null, null, null);
+    public static final String WRONG_ID = "WrongId";
+    public static final String TEXT_BODY = "Text body";
+    public static final String USERNAME = "username";
+    public static final MailboxSession.User USER = new MailboxSession.User() {
+        @Override
+        public String getUserName() {
+            return USERNAME;
+        }
+
+        @Override
+        public String getPassword() {
+            return null;
+        }
+
+        @Override
+        public List<Locale> getLocalePreferences() {
+            return null;
+        }
+    };
+
+    private SetVacationResponseMethod testee;
+    private VacationRepository vacationRepository;
+    private ClientId clientId;
+    private MailboxSession mailboxSession;
+
+    @Before
+    public void setUp() {
+        clientId = mock(ClientId.class);
+        mailboxSession = mock(MailboxSession.class);
+        vacationRepository = mock(VacationRepository.class);
+        testee = new SetVacationResponseMethod(vacationRepository);
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void processShouldThrowOnNullRequest() {
+        testee.process(null, mock(ClientId.class), mock(MailboxSession.class));
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void processShouldThrowOnNullClientId() {
+        testee.process(mock(SetMailboxesRequest.class), null, mock(MailboxSession.class));
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void processShouldThrowOnNullMailboxSession() {
+        testee.process(mock(SetMailboxesRequest.class), mock(ClientId.class), null);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void processShouldThrowOnWrongRequestType() {
+        testee.process(mock(GetMailboxesRequest.class), mock(ClientId.class), mock(MailboxSession.class));
+    }
+
+    @Test
+    public void processShouldThrowOnEmptyMap() {
+        SetVacationRequest setVacationRequest = SetVacationRequest.builder()
+            .update(ImmutableMap.of())
+            .build();
+
+        Stream<JmapResponse> result = testee.process(setVacationRequest, clientId,
mock(MailboxSession.class));
+
+        JmapResponse expected = JmapResponse.builder()
+            .clientId(clientId)
+            .error(ErrorResponse.builder()
+                .type(SetVacationResponseMethod.INVALID_ARGUMENTS)
+                .description(SetVacationResponseMethod.INVALID_ARGUMENT_DESCRIPTION)
+                .build())
+            .build();
+        assertThat(result).containsExactly(expected);
+        verifyNoMoreInteractions(vacationRepository);
+    }
+
+    @Test
+    public void processShouldThrowIfWrongMapId() {
+        SetVacationRequest setVacationRequest = SetVacationRequest.builder()
+            .update(ImmutableMap.of(WRONG_ID, VacationResponse.builder()
+                .id(Vacation.ID)
+                .enabled(false)
+                .textBody(TEXT_BODY)
+                .build()))
+            .build();
+
+        Stream<JmapResponse> result = testee.process(setVacationRequest, clientId,
mock(MailboxSession.class));
+
+        JmapResponse expected = JmapResponse.builder()
+            .clientId(clientId)
+            .error(ErrorResponse.builder()
+                .type(SetVacationResponseMethod.INVALID_ARGUMENTS)
+                .description(SetVacationResponseMethod.INVALID_ARGUMENT_DESCRIPTION)
+                .build())
+            .build();
+        assertThat(result).containsExactly(expected);
+        verifyNoMoreInteractions(vacationRepository);
+    }
+
+    @Test
+    public void processShouldThrowIfMapSizeNotOne() {
+        SetVacationRequest setVacationRequest = SetVacationRequest.builder()
+            .update(ImmutableMap.of(Vacation.ID, VacationResponse.builder()
+                    .id(Vacation.ID)
+                    .enabled(false)
+                    .textBody(TEXT_BODY)
+                    .build(),
+                WRONG_ID, VacationResponse.builder()
+                    .id(Vacation.ID)
+                    .enabled(false)
+                    .textBody(TEXT_BODY)
+                    .build()))
+            .build();
+
+        Stream<JmapResponse> result = testee.process(setVacationRequest, clientId,
mock(MailboxSession.class));
+
+        JmapResponse expected = JmapResponse.builder()
+            .clientId(clientId)
+            .error(ErrorResponse.builder()
+                .type(SetVacationResponseMethod.INVALID_ARGUMENTS)
+                .description(SetVacationResponseMethod.INVALID_ARGUMENT_DESCRIPTION)
+                .build())
+            .build();
+        assertThat(result).containsExactly(expected);
+        verifyNoMoreInteractions(vacationRepository);
+    }
+
+    @Test
+    public void processShouldUpdateRepositoryUponValidRequest() {
+        SetVacationRequest setVacationRequest = SetVacationRequest.builder()
+            .update(ImmutableMap.of(Vacation.ID, VacationResponse.builder()
+                    .id(Vacation.ID)
+                    .enabled(false)
+                    .textBody(TEXT_BODY)
+                    .build()))
+            .build();
+        Vacation vacation = Vacation.builder()
+            .enabled(false)
+            .textBody(TEXT_BODY)
+            .build();
+        AccountId accountId = AccountId.fromString(USERNAME);
+
+        when(mailboxSession.getUser()).thenReturn(USER);
+        when(vacationRepository.modifyVacation(accountId, vacation)).thenReturn(CompletableFuture.completedFuture(null));
+
+        Stream<JmapResponse> result = testee.process(setVacationRequest, clientId,
mailboxSession);
+
+        JmapResponse expected = JmapResponse.builder()
+            .clientId(clientId)
+            .responseName(SetVacationResponseMethod.RESPONSE_NAME)
+            .response(SetVacationResponse.builder()
+                .updatedId(Vacation.ID)
+                .build())
+            .build();
+        assertThat(result).containsExactly(expected);
+
+        verify(vacationRepository).modifyVacation(accountId, vacation);
+        verifyNoMoreInteractions(vacationRepository);
+    }
+
+    @Test
+    public void processShouldReturnErrorIfWrongIdIsUsedInsideVacationResponse() {
+        SetVacationRequest setVacationRequest = SetVacationRequest.builder()
+            .update(ImmutableMap.of(Vacation.ID, VacationResponse.builder()
+                .id(WRONG_ID)
+                .textBody(TEXT_BODY)
+                .enabled(false)
+                .build()))
+            .build();
+        when(mailboxSession.getUser()).thenReturn(USER);
+
+        Stream<JmapResponse> result = testee.process(setVacationRequest, clientId,
mailboxSession);
+
+        JmapResponse expected = JmapResponse.builder()
+            .clientId(clientId)
+            .responseName(SetVacationResponseMethod.RESPONSE_NAME)
+            .response(SetVacationResponse.builder()
+                .notUpdated(Vacation.ID, SetError.builder()
+                    .type(SetVacationResponseMethod.INVALID_ARGUMENTS)
+                    .description(SetVacationResponseMethod.ERROR_MESSAGE_BASE + WRONG_ID)
+                    .build())
+                .build())
+            .build();
+        assertThat(result).containsExactly(expected);
+        verifyNoMoreInteractions(vacationRepository);
     }
 
 }


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


Mime
View raw message