### Question:
DefaultMetaStoreEventCompactor { public List<MetaStoreEvent> compact(List<MetaStoreEvent> tableEvents) { LinkedList<MetaStoreEvent> finalEvents = new LinkedList<>(); for (MetaStoreEvent e : tableEvents) { switch (e.getEventType()) { case DROP_TABLE: processDropTable(finalEvents, e); break; case DROP_PARTITION: mergeOrAdd(finalEvents, e, evt -> evt.isDropEvent()); break; case CREATE_TABLE: case ADD_PARTITION: case ALTER_PARTITION: case INSERT: mergeOrAdd(finalEvents, e, evt -> !evt.isDropEvent()); break; default: log.debug("Unknown event type {}: adding to list of event", e.getEventType()); finalEvents.add(e); break; } } return finalEvents; } DefaultMetaStoreEventCompactor();   DefaultMetaStoreEventCompactor(EventMerger eventMerger); List<MetaStoreEvent> compact(List<MetaStoreEvent> tableEvents);  }### Answer:
@Test public void shouldReturnSameEventsIfEventsCannotBeMerged() { List<MetaStoreEvent> events = asList(mockEvent(DROP_PARTITION), mockEvent(DROP_TABLE), mockEvent(CREATE_TABLE)); List<MetaStoreEvent> compactEvents = compactor.compact(events); assertThat(compactEvents).isEqualTo(events); }
@Test public void mergeEventsIfEventsCanBeMerged() { MetaStoreEvent eventA = mockEvent(DROP_PARTITION); MetaStoreEvent eventB = mockEvent(DROP_TABLE); MetaStoreEvent eventC = mockEvent(CREATE_TABLE); MetaStoreEvent eventD = mockEvent(ADD_PARTITION); MetaStoreEvent eventE = mockEvent(ADD_PARTITION); when(merger.canMerge(eventC, eventD)).thenReturn(true); MetaStoreEvent eventCD = mockEvent(CREATE_TABLE); when(merger.merge(eventC, eventD)).thenReturn(eventCD); when(merger.canMerge(eventCD, eventE)).thenReturn(true); MetaStoreEvent eventCDE = mockEvent(CREATE_TABLE); when(merger.merge(eventCD, eventE)).thenReturn(eventCDE); List<MetaStoreEvent> compactEvents = compactor.compact(asList(eventA, eventB, eventC, eventD, eventE)); assertThat(compactEvents).isEqualTo(asList(eventA, eventB, eventCDE)); }
@Test public void mergeDiscardPreviousEventsIfADropTableIsFoundInBetween() { MetaStoreEvent eventA = mockEvent(CREATE_TABLE); MetaStoreEvent eventB = mockEvent(ADD_PARTITION); MetaStoreEvent eventC = mockEvent(DROP_TABLE); MetaStoreEvent eventD = mockEvent(CREATE_TABLE); MetaStoreEvent eventE = mockEvent(ADD_PARTITION); when(merger.canMerge(eventA, eventB)).thenReturn(true); MetaStoreEvent eventAB = mockEvent(CREATE_TABLE); when(merger.merge(eventA, eventB)).thenReturn(eventAB); when(merger.canMerge(eventD, eventE)).thenReturn(true); MetaStoreEvent eventDE = mockEvent(CREATE_TABLE); when(merger.merge(eventD, eventE)).thenReturn(eventDE); List<MetaStoreEvent> compactEvents = compactor.compact(asList(eventA, eventB, eventC, eventD, eventE)); assertThat(compactEvents).isEqualTo(asList(eventC, eventDE)); }