SetMailboxesMethod implements Method { @Override public Stream<JmapResponse> processToStream(JmapRequest request, MethodCallId methodCallId, MailboxSession mailboxSession) { Preconditions.checkNotNull(request); Preconditions.checkNotNull(methodCallId); Preconditions.checkNotNull(mailboxSession); Preconditions.checkArgument(request instanceof SetMailboxesRequest); SetMailboxesRequest setMailboxesRequest = (SetMailboxesRequest) request; return MDCBuilder.create() .addContext(MDCBuilder.ACTION, "SET_MAILBOXES") .addContext("create", setMailboxesRequest.getCreate()) .addContext("update", setMailboxesRequest.getUpdate()) .addContext("destroy", setMailboxesRequest.getDestroy()) .wrapArround( () -> metricFactory.decorateSupplierWithTimerMetricLogP99(JMAP_PREFIX + METHOD_NAME.getName(), () -> Stream.of( JmapResponse.builder().methodCallId(methodCallId) .response(setMailboxesResponse(setMailboxesRequest, mailboxSession)) .responseName(RESPONSE_NAME) .build()))) .get(); } @Inject  SetMailboxesMethod(Set<SetMailboxesProcessor> processors, MetricFactory metricFactory); @Override Request.Name requestHandled(); @Override Class<? extends JmapRequest> requestType(); @Override Stream<JmapResponse> processToStream(JmapRequest request, MethodCallId methodCallId, MailboxSession mailboxSession);  }
@Test public void processShouldThrowWhenNullJmapRequest() { MailboxSession session = mock(MailboxSession.class); JmapRequest nullJmapRequest = null; assertThatThrownBy(() -> new SetMailboxesMethod(NO_PROCESSOR, TIME_METRIC_FACTORY).processToStream(nullJmapRequest, MethodCallId.of("methodCallId"), session)) .isInstanceOf(NullPointerException.class); }
@Test public void processShouldThrowWhenNullMethodCallId() { MailboxSession session = mock(MailboxSession.class); JmapRequest jmapRequest = mock(JmapRequest.class); MethodCallId nullMethodCallId = null; assertThatThrownBy(() -> new SetMailboxesMethod(NO_PROCESSOR, TIME_METRIC_FACTORY).processToStream(jmapRequest, nullMethodCallId, session)) .isInstanceOf(NullPointerException.class); }
@Test public void processShouldThrowWhenNullMailboxSession() { MailboxSession nullMailboxSession = null; JmapRequest jmapRequest = mock(JmapRequest.class); assertThatThrownBy(() -> new SetMailboxesMethod(NO_PROCESSOR, TIME_METRIC_FACTORY).processToStream(jmapRequest, MethodCallId.of("methodCallId"), nullMailboxSession)) .isInstanceOf(NullPointerException.class); }
@Test public void processShouldThrowWhenJmapRequestTypeMismatch() { MailboxSession session = mock(MailboxSession.class); JmapRequest getMailboxesRequest = GetMailboxesRequest.builder().build(); assertThatThrownBy(() -> new SetMailboxesMethod(NO_PROCESSOR, TIME_METRIC_FACTORY).processToStream(getMailboxesRequest, MethodCallId.of("methodCallId"), session)) .isInstanceOf(IllegalArgumentException.class); }
@Test public void processShouldCallCreatorProcessorWhenCreationRequest() { MailboxCreationId creationId = MailboxCreationId.of("create-id01"); MailboxCreateRequest fooFolder = MailboxCreateRequest.builder().name("fooFolder").build(); SetMailboxesRequest creationRequest = SetMailboxesRequest.builder().create(creationId, fooFolder).build(); Mailbox createdfooFolder = Mailbox.builder().name("fooFolder").id(InMemoryId.of(123)).build(); SetMailboxesResponse creationResponse = SetMailboxesResponse.builder().created(creationId, createdfooFolder).build(); JmapResponse jmapResponse = JmapResponse.builder() .response(creationResponse) .methodCallId(MethodCallId.of("methodCallId")) .responseName(SetMailboxesMethod.RESPONSE_NAME) .build(); MailboxSession session = mock(MailboxSession.class); SetMailboxesProcessor creatorProcessor = mock(SetMailboxesProcessor.class); when(creatorProcessor.process(creationRequest, session)).thenReturn(creationResponse); Stream<JmapResponse> actual = new SetMailboxesMethod(ImmutableSet.of(creatorProcessor), TIME_METRIC_FACTORY) .processToStream(creationRequest, MethodCallId.of("methodCallId"), session); assertThat(actual).contains(jmapResponse); }
@Test public void processShouldCallDestructorProcessorWhenCreationRequest() { ImmutableList<MailboxId> deletions = ImmutableList.of(InMemoryId.of(1)); SetMailboxesRequest destructionRequest = SetMailboxesRequest.builder().destroy(deletions).build(); SetMailboxesResponse destructionResponse = SetMailboxesResponse.builder().destroyed(deletions).build(); JmapResponse jmapResponse = JmapResponse.builder() .response(destructionResponse) .methodCallId(MethodCallId.of("methodCallId")) .responseName(SetMailboxesMethod.RESPONSE_NAME) .build(); MailboxSession session = mock(MailboxSession.class); SetMailboxesProcessor destructorProcessor = mock(SetMailboxesProcessor.class); when(destructorProcessor.process(destructionRequest, session)).thenReturn(destructionResponse); Stream<JmapResponse> actual = new SetMailboxesMethod(ImmutableSet.of(destructorProcessor), TIME_METRIC_FACTORY) .processToStream(destructionRequest, MethodCallId.of("methodCallId"), session); assertThat(actual).contains(jmapResponse); }