### Question:
DirectSolrInputDocumentWriter implements SolrInputDocumentWriter { @Override public void add(int shard, Map<String, SolrInputDocument> inputDocumentMap) throws SolrServerException, IOException { Collection<SolrInputDocument> inputDocuments = inputDocumentMap.values(); try { solrServer.add(inputDocuments); indexAddMeter.mark(inputDocuments.size()); } catch (SolrException e) { if (isDocumentIssue(e)) { retryAddsIndividually(inputDocuments); } else { solrAddErrorMeter.mark(inputDocuments.size()); throw e; } } catch (SolrServerException sse) { solrAddErrorMeter.mark(inputDocuments.size()); throw sse; } } DirectSolrInputDocumentWriter(String indexName, SolrClient solrServer); @Override void add(int shard, Map<String, SolrInputDocument> inputDocumentMap); @Override void deleteById(int shard, List<String> idsToDelete); @Override void deleteByQuery(String deleteQuery); @Override void close();  }### Answer:
@Test public void testAdd_NormalCase() throws SolrServerException, IOException { SolrInputDocument inputDocA = mock(SolrInputDocument.class); SolrInputDocument inputDocB = mock(SolrInputDocument.class); Map<String, SolrInputDocument> toAdd = ImmutableSortedMap.of("idA", inputDocA, "idB", inputDocB); solrWriter.add(-1, toAdd); verify(solrServer).add(toAdd.values()); }
@Test(expected = IOException.class) public void testAdd_IOException() throws SolrServerException, IOException { SolrInputDocument inputDoc = mock(SolrInputDocument.class); Map<String, SolrInputDocument> inputDocMap = ImmutableMap.of("idA", inputDoc); when(solrServer.add(inputDocMap.values())).thenThrow(new IOException()); solrWriter.add(-1, inputDocMap); }
@Test(expected = SolrException.class) public void testAdd_SolrExceptionCausedByIOException() throws SolrServerException, IOException { SolrInputDocument inputDoc = mock(SolrInputDocument.class); Map<String,SolrInputDocument> inputDocMap = ImmutableMap.of("idA", inputDoc); when(solrServer.add(inputDocMap.values())) .thenThrow(new SolrException(ErrorCode.SERVER_ERROR, new IOException())); solrWriter.add(-1, inputDocMap); }
@Test public void testAdd_BadRequest() throws SolrServerException, IOException { SolrInputDocument inputDoc = mock(SolrInputDocument.class); Map<String,SolrInputDocument> inputDocumentMap = ImmutableMap.of("idA", inputDoc); when(solrServer.add(ImmutableList.of(inputDoc))).thenThrow( new SolrException(ErrorCode.BAD_REQUEST, "should be swallowed and logged")); solrWriter.add(-1, inputDocumentMap); }
@Test public void testAdd_RetryIndividually() throws SolrServerException, IOException { SolrInputDocument badInputDoc = mock(SolrInputDocument.class); SolrInputDocument goodInputDoc = mock(SolrInputDocument.class); Map<String, SolrInputDocument> inputDocumentMap = ImmutableSortedMap.of("bad", badInputDoc, "good", goodInputDoc); when(solrServer.add(inputDocumentMap.values())) .thenThrow(new SolrException(ErrorCode.BAD_REQUEST, "bad document")); when(solrServer.add(badInputDoc)).thenThrow(new SolrException(ErrorCode.BAD_REQUEST, "bad document")); solrWriter.add(-1, inputDocumentMap); verify(solrServer).add(goodInputDoc); }