DocumentMetadataAvroTruncator implements AvroTruncator<DocumentMetadata> { @Override public DocumentMetadata truncate(DocumentMetadata source) { if (Objects.nonNull(source.getAbstract$())) { source.setAbstract$(abstractTruncator.apply(source.getAbstract$())); } if (Objects.nonNull(source.getTitle())) { source.setTitle(titleTruncator.apply(source.getTitle())); } if (Objects.nonNull(source.getAuthors())) { source.setAuthors(authorsTruncator.apply(source.getAuthors())); } if (Objects.nonNull(source.getKeywords())) { source.setKeywords(keywordsTruncator.apply(source.getKeywords())); } return source; } private  DocumentMetadataAvroTruncator(int maxAbstractLength,
                                          int maxTitleLength,
                                          int maxAuthorsSize,
                                          int maxAuthorFullnameLength,
                                          int maxKeywordsSize,
                                          int maxKeywordLength); @Override DocumentMetadata truncate(DocumentMetadata source); static Builder newBuilder();  }
@Test public void shouldNotTruncateAbstractWhenItIsNull() { DocumentMetadata source = createDocumentMetadata(); DocumentMetadata result = documentMetadataAvroTruncator.truncate(source); assertEquals(source, result); verify(abstractTruncator, never()).apply(any()); }
@Test public void shouldTruncateAbstractWhenItIsNotNull() { DocumentMetadata source = createDocumentMetadata(); source.setAbstract$("abstract"); when(abstractTruncator.apply("abstract")).thenReturn("truncated"); DocumentMetadata result = documentMetadataAvroTruncator.truncate(source); assertEquals("truncated", result.getAbstract$()); }
@Test public void shouldNotTruncateTitleWhenItIsNull() { DocumentMetadata source = createDocumentMetadata(); DocumentMetadata result = documentMetadataAvroTruncator.truncate(source); assertEquals(source, result); verify(titleTruncator, never()).apply(any()); }
@Test public void shouldTruncateTitleWhenItIsNotNull() { DocumentMetadata source = createDocumentMetadata(); source.setTitle("title"); when(titleTruncator.apply("title")).thenReturn("truncated"); DocumentMetadata result = documentMetadataAvroTruncator.truncate(source); assertEquals("truncated", result.getTitle()); }
@Test public void shouldNotTruncateAuthorsWhenItIsNull() { DocumentMetadata source = createDocumentMetadata(); DocumentMetadata result = documentMetadataAvroTruncator.truncate(source); assertEquals(source, result); verify(authorsTruncator, never()).apply(any()); }
@Test public void shouldTruncateAuthorsWhenItIsNotNull() { DocumentMetadata source = createDocumentMetadata(); List<Author> authors = Collections.singletonList(mock(Author.class)); source.setAuthors(authors); List<Author> truncated = Collections.singletonList(mock(Author.class)); when(authorsTruncator.apply(authors)).thenReturn(truncated); DocumentMetadata result = documentMetadataAvroTruncator.truncate(source); assertEquals(truncated, result.getAuthors()); }
@Test public void shouldNotTruncateKeywordsWhenItIsNull() { DocumentMetadata source = createDocumentMetadata(); DocumentMetadata result = documentMetadataAvroTruncator.truncate(source); assertEquals(source, result); verify(keywordsTruncator, never()).apply(any()); }
@Test public void shouldTruncateKeywordsWhenItIsNotNull() { DocumentMetadata source = createDocumentMetadata(); List<CharSequence> keywords = Collections.singletonList("keyword"); source.setKeywords(keywords); List<CharSequence> truncated = Collections.singletonList("truncated"); when(keywordsTruncator.apply(keywords)).thenReturn(truncated); DocumentMetadata result = documentMetadataAvroTruncator.truncate(source); assertEquals(truncated, result.getKeywords()); }