### Question:
SetContentTypeFilter implements IndexDocumentFilter { @Override public void filter(DocumentIndexingPackage dip) throws IndexingException { ContentObject contentObj = dip.getContentObject(); if (!(contentObj instanceof WorkObject) && !(contentObj instanceof FileObject)) { return; } FileObject fileObj = getFileObject(dip); BinaryObject binObj; if (fileObj == null) { return; } binObj = fileObj.getOriginalFile(); String filepath = binObj.getFilename(); String mimetype = binObj.getMimetype(); log.debug("The binary has filepath {} and mimetype {}", filepath, mimetype); ArrayList<String> contentTypes = new ArrayList<>(); extractContentType(filepath, mimetype, contentTypes); dip.getDocument().setContentType(contentTypes); } SetContentTypeFilter(); @Override void filter(DocumentIndexingPackage dip);  }### Answer:
@Test public void testGetContentTypeFromWorkObject() throws Exception { when(dip.getContentObject()).thenReturn(workObj); when(workObj.getPrimaryObject()).thenReturn(fileObj); when(binObj.getFilename()).thenReturn("primary.xml"); when(binObj.getMimetype()).thenReturn("application/xml"); filter.filter(dip); verify(idb).setContentType(listCaptor.capture()); assertEquals("^text,Text", listCaptor.getValue().get(0)); assertEquals("/text^xml,xml", listCaptor.getValue().get(1)); }
@Test public void testGetContentTypeFromFileObject() throws Exception { when(dip.getContentObject()).thenReturn(fileObj); when(fileObj.getOriginalFile()).thenReturn(binObj); when(binObj.getFilename()).thenReturn("data.csv"); when(binObj.getMimetype()).thenReturn("ext.csv"); filter.filter(dip); verify(idb).setContentType(listCaptor.capture()); assertEquals("^dataset,Dataset", listCaptor.getValue().get(0)); assertEquals("/dataset^csv,csv", listCaptor.getValue().get(1)); }
@Test public void testExtensionNotFoundInMapping() throws Exception { when(dip.getContentObject()).thenReturn(fileObj); when(fileObj.getOriginalFile()).thenReturn(binObj); when(binObj.getFilename()).thenReturn("image.x3f"); when(binObj.getMimetype()).thenReturn("some_wacky_type"); filter.filter(dip); verify(idb).setContentType(listCaptor.capture()); assertEquals("^unknown,Unknown", listCaptor.getValue().get(0)); assertEquals("/unknown^x3f,x3f", listCaptor.getValue().get(1)); }
@Test public void testNotWorkAndNotFileObject() throws Exception { when(dip.getContentObject()).thenReturn(folderObj); filter.filter(dip); verify(idb, never()).setContentType(anyListOf(String.class)); }
@Test public void testWorkWithoutPrimaryObject() throws Exception { when(dip.getContentObject()).thenReturn(workObj); when(workObj.getPrimaryObject()).thenReturn(null); filter.filter(dip); verify(idb, never()).setContentType(anyListOf(String.class)); }
@Test public void testGetPlainTextContentType() throws Exception { when(dip.getContentObject()).thenReturn(fileObj); when(fileObj.getOriginalFile()).thenReturn(binObj); when(binObj.getFilename()).thenReturn("file.txt"); when(binObj.getMimetype()).thenReturn("text/plain"); filter.filter(dip); verify(idb).setContentType(listCaptor.capture()); assertEquals("^text,Text", listCaptor.getValue().get(0)); assertEquals("/text^txt,txt", listCaptor.getValue().get(1)); }