private UpdatedRepresentation createUpdatedDocumentPartFromUpdatedFlags(Mailbox mailbox, UpdatedFlags updatedFlags) { try { return new UpdatedRepresentation( indexIdFor(mailbox, updatedFlags.getUid()), messageToElasticSearchJson.getUpdatedJsonMessagePart( updatedFlags.getNewFlags(), updatedFlags.getModSeq())); } catch (JsonProcessingException e) { throw new RuntimeException("Error while creating updatedDocumentParts", e); } }
public Optional<BulkResponse> update(List<UpdatedRepresentation> updatedDocumentParts) { try { Preconditions.checkNotNull(updatedDocumentParts); BulkRequestBuilder bulkRequestBuilder = client.prepareBulk(); updatedDocumentParts.forEach(updatedDocumentPart -> bulkRequestBuilder.add( client.prepareUpdate( aliasName.getValue(), typeName.getValue(), updatedDocumentPart.getId()) .setDoc(updatedDocumentPart.getUpdatedDocumentPart()))); return Optional.of(bulkRequestBuilder.get()); } catch (ValidationException e) { LOGGER.warn("Error while updating index", e); return Optional.empty(); } }
@Test public void updateShouldWork() throws Exception { //Given Mailbox mailbox = mock(Mailbox.class); Flags flags = new Flags(); UpdatedFlags updatedFlags = UpdatedFlags.builder() .uid(MESSAGE_UID) .modSeq(MODSEQ) .oldFlags(flags) .newFlags(flags) .build(); when(mailbox.getMailboxId()) .thenReturn(MAILBOX_ID); when(messageToElasticSearchJson.getUpdatedJsonMessagePart(any(Flags.class), any(Long.class))) .thenReturn("json updated content"); //When testee.update(session, mailbox, Lists.newArrayList(updatedFlags)); //Then ImmutableList<UpdatedRepresentation> expectedUpdatedRepresentations = ImmutableList.of(new UpdatedRepresentation(ELASTIC_SEARCH_ID, "json updated content")); verify(elasticSearchIndexer).update(expectedUpdatedRepresentations); }
@Test public void updateShouldNotPropagateExceptionWhenExceptionOccurs() throws Exception { //Given Mailbox mailbox = mock(Mailbox.class); Flags flags = new Flags(); UpdatedFlags updatedFlags = UpdatedFlags.builder() .uid(MESSAGE_UID) .modSeq(MODSEQ) .oldFlags(flags) .newFlags(flags) .build(); when(mailbox.getMailboxId()) .thenReturn(MAILBOX_ID); ImmutableList<UpdatedRepresentation> expectedUpdatedRepresentations = ImmutableList.of(new UpdatedRepresentation(ELASTIC_SEARCH_ID, "json updated content")); when(elasticSearchIndexer.update(expectedUpdatedRepresentations)) .thenThrow(new ElasticsearchException("")); //When testee.update(session, mailbox, Lists.newArrayList(updatedFlags)); //Then //No exception }