### Question:
DocumentFieldValueMarshaller extends AbstractFieldValueMarshaller<Document, DocumentData, DocumentFieldDefinition> { @Override public DocumentData toFlatValue() { if (originalValue == null) { return null; } String templateId = (String) context.getAttributes().get(SERVER_TEMPLATE_ID); return fromDocument(originalValue, templateId); } @Inject  DocumentFieldValueMarshaller(UploadedDocumentStorage documentStorage); @Override Class<DocumentFieldDefinition> getSupportedField(); @Override Supplier<FieldValueMarshaller<Document, DocumentData, DocumentFieldDefinition>> newInstanceSupplier(); @Override DocumentData toFlatValue(); @Override Document toRawValue(DocumentData documentData); static final String SERVER_TEMPLATE_ID; }### Answer:
@Test public void testNull2FlatValue() { marshaller.init(null, field, form, context); DocumentData documentData = marshaller.toFlatValue(); assertNull("DocumentData must be null!", documentData); }
@Test public void testDocument2FlatValueEmptyLinkPattern() { Document doc = spy(new DocumentImpl(DOCUMENT_ID, "docName", 1024, new Date())); marshaller.init(doc, field, form, context); DocumentData documentData = marshaller.toFlatValue(); verify(doc).getLink(); assertNotNull(documentData); assertEquals(doc.getName(), documentData.getFileName()); assertEquals(doc.getSize(), documentData.getSize()); assertEquals("", documentData.getLink()); }
@Test public void testDocument2FlatValue() { Document doc = spy(new DocumentImpl(DOCUMENT_ID, "docName", 1024, new Date())); Map result = new HashMap(); result.put(DocumentFieldValueMarshaller.SERVER_TEMPLATE_ID, SERVER_TEMPLATE_ID); when(context.getAttributes()).thenReturn(result); marshaller.init(doc, field, form, context); DocumentData documentData = marshaller.toFlatValue(); verify(doc, never()).getLink(); assertNotNull(documentData); assertEquals(doc.getName(), documentData.getFileName()); assertEquals(doc.getSize(), documentData.getSize()); assertEquals(EXPECTED_DOWNLOAD_LINK, documentData.getLink()); }