### Question:
SingleRelatedItemIndexingMessageEventHandler implements RelatedItemIndexingMessageEventHandler { @Override public void onEvent(RelatedItemIndexingMessage request, long l, boolean endOfBatch) throws Exception { if(!request.isValidMessage()) { log.debug("Invalid indexing message. Ignoring message"); return; } if(request.getRelatedItems().getNumberOfRelatedItems()==0) { log.debug("Invalid indexing message, no related products. Ignoring message"); request.setValidMessage(false); return; } try { RelatedItem[] products = indexConverter.convertFrom(request); this.count[COUNTER_POS]-=products.length; for(RelatedItem p : products) { relatedItems.add(p); } if(endOfBatch || this.count[COUNTER_POS]<1) { try { log.debug("Sending indexing requests to the storage repository"); try { storageRepository.store(locationMapper, relatedItems); } catch(Exception e) { log.warn("Exception calling storage repository for related products:{}", products, e); } } finally { this.count[COUNTER_POS] = batchSize; relatedItems.clear(); } } } finally { request.setValidMessage(false); } } SingleRelatedItemIndexingMessageEventHandler(Configuration configuration,
                                                        RelatedItemIndexingMessageConverter converter,
                                                        RelatedItemStorageRepository repository,
                                                        RelatedItemStorageLocationMapper locationMapper); @Override void onEvent(RelatedItemIndexingMessage request, long l, boolean endOfBatch); @Override void shutdown();  }### Answer:
@Test public void testSendingOneItem() { try { handler.onEvent(getMessage(),1,true); } catch(Exception e) { fail(); } assertEquals(1,repo.getNumberOfCalls()); assertEquals(3,repo.getNumberOfProductsSentForStoring()); }
@Test public void testSendingManyItemsButBelowBatchSize() { try { for(int i=0;i<7;i++) { handler.onEvent(getMessage(),i,false); } handler.onEvent(getMessage(),8,true); } catch(Exception e) { fail(); } assertEquals(1,repo.getNumberOfCalls()); assertEquals(24,repo.getNumberOfProductsSentForStoring()); }
@Test public void testSendingManyItemsExceedingBatchSize() { try { for(int i=0;i<25;i++) { handler.onEvent(getMessage(),i,false); } handler.onEvent(getMessage(),26,true); } catch(Exception e) { fail(); } assertEquals(3,repo.getNumberOfCalls()); assertEquals(78,repo.getNumberOfProductsSentForStoring()); }
@Test public void checkNoCallMadeForInvalidMessage() { RelatedItemIndexingMessage message = new RelatedItemIndexingMessage(configuration); message.setValidMessage(false); try { handler.onEvent(message,1,true); } catch (Exception e) { fail(); } assertEquals(0,repo.getNumberOfCalls()); assertEquals(0,repo.getNumberOfProductsSentForStoring()); }
@Test public void checkNoCallMadeForMessageWithNoProducts() { RelatedItemIndexingMessage message = new RelatedItemIndexingMessage(configuration); message.setValidMessage(true); message.getRelatedItems().setNumberOfRelatedItems(0); try { handler.onEvent(message,1,true); } catch (Exception e) { fail(); } assertEquals(0,repo.getNumberOfCalls()); assertEquals(0,repo.getNumberOfProductsSentForStoring()); }