### Question:
IdAddingSolrUpdateWriter implements SolrUpdateWriter { @Override public void add(SolrInputDocument solrDocument) { String docId = documentId; SolrInputField uniqueKeySolrField = solrDocument.getField(uniqueKeyField); if (uniqueKeySolrField == null) { if (idUsed) { throw new IllegalStateException("Document id '" + documentId + "' has already been used by this record"); } solrDocument.addField(uniqueKeyField, documentId); idUsed = true; } else { docId = uniqueKeySolrField.getValue().toString(); } if (tableNameField != null) { solrDocument.addField(tableNameField, tableName); } updateCollector.add(docId, solrDocument); } IdAddingSolrUpdateWriter(String uniqueKeyField, String documentId, String tableNameField, String tableName,
            SolrUpdateCollector updateCollector); @Override void add(SolrInputDocument solrDocument); @Override void deleteById(String documentId); @Override void deleteByQuery(String query);  }### Answer:
@Test public void testAdd_AddId() { IdAddingSolrUpdateWriter updateWriter = new IdAddingSolrUpdateWriter(UNIQUE_KEY_FIELD, DOCUMENT_ID, null, TABLE_NAME, updateCollector); updateWriter.add(solrDoc); verify(solrDoc).addField(UNIQUE_KEY_FIELD, DOCUMENT_ID); verify(updateCollector).add(DOCUMENT_ID, solrDoc); }
@Test public void testAdd_IdAlreadyPresent() { IdAddingSolrUpdateWriter updateWriter = new IdAddingSolrUpdateWriter( UNIQUE_KEY_FIELD, DOCUMENT_ID, null, TABLE_NAME, updateCollector); SolrInputField solrIdField = new SolrInputField(DOCUMENT_ID); solrIdField.setValue(DOCUMENT_ID, 1.0f); when(solrDoc.getField(UNIQUE_KEY_FIELD)).thenReturn(solrIdField); updateWriter.add(solrDoc); verify(updateCollector).add(DOCUMENT_ID, solrDoc); }
@Test(expected = IllegalStateException.class) public void testAdd_MultipleDocumentsForOneId() { IdAddingSolrUpdateWriter updateWriter = new IdAddingSolrUpdateWriter( UNIQUE_KEY_FIELD, DOCUMENT_ID, null, TABLE_NAME, updateCollector); updateWriter.add(solrDoc); updateWriter.add(solrDoc); }
@Test public void testAdd_MultipleDocumentsWithTheirOwnIds() { String idA = DOCUMENT_ID + "A"; String idB = DOCUMENT_ID + "B"; IdAddingSolrUpdateWriter updateWriter = new IdAddingSolrUpdateWriter( UNIQUE_KEY_FIELD, DOCUMENT_ID, null, TABLE_NAME, updateCollector); SolrInputDocument docA = mock(SolrInputDocument.class); SolrInputDocument docB = mock(SolrInputDocument.class); SolrInputField keyFieldA = new SolrInputField(UNIQUE_KEY_FIELD); keyFieldA.setValue(idA, 1.0f); SolrInputField keyFieldB = new SolrInputField(UNIQUE_KEY_FIELD); keyFieldB.setValue(idB, 1.0f); when(docA.getField(UNIQUE_KEY_FIELD)).thenReturn(keyFieldA); when(docB.getField(UNIQUE_KEY_FIELD)).thenReturn(keyFieldB); updateWriter.add(docA); updateWriter.add(docB); verify(updateCollector).add(idA, docA); verify(updateCollector).add(idB, docB); }
@Test public void testAdd_IncludeTableName() { IdAddingSolrUpdateWriter updateWriter = new IdAddingSolrUpdateWriter( UNIQUE_KEY_FIELD, DOCUMENT_ID, "tableNameField", TABLE_NAME, updateCollector); updateWriter.add(solrDoc); verify(solrDoc).addField(UNIQUE_KEY_FIELD, DOCUMENT_ID); verify(solrDoc).addField("tableNameField", TABLE_NAME); verify(updateCollector).add(DOCUMENT_ID, solrDoc); }