UpdateEntryPointSubscriber implements Consumer<UpdateEntryPointEvent> { @Override public void accept(UpdateEntryPointEvent updateEntryPointEvent) { EntryPointKey entryPointKey = updateEntryPointEvent.getKey(); try { if (!this.stateManager.lock(entryPointKey)) { throw new IllegalStateException("Could not acquire lock for update entry point event " + entryPointKey); } EntryPoint existingEntryPoint = Optional.ofNullable(stateManager.getPendingConfiguration(entryPointKey) .orElseGet(() -> stateManager.getCommittingConfiguration(entryPointKey) .orElseGet(() -> stateManager.getCurrentConfiguration(entryPointKey) .orElse(null)))) .orElseThrow(() -> new IllegalStateException("Trying to update a missing entry point for key " + entryPointKey)); EntryPoint mergedEntryPoint = existingEntryPoint.mergeWithUpdate(updateEntryPointEvent.getUpdatedEntryPoint()); Optional<EntryPoint> preparedEntryPoint = stateManager.prepare(entryPointKey, mergedEntryPoint); if (!preparedEntryPoint.isPresent()) { throw new IllegalStateException("Unable to prepare entry point update for key " + entryPointKey); } updateEntryPointEvent.onSuccess(new UpdateEntryPointResponse(updateEntryPointEvent.getCorrelationId(), entryPointKey, preparedEntryPoint.get())); } catch (Exception exception) { updateEntryPointEvent.onError(exception); } finally { this.stateManager.release(entryPointKey); } } UpdateEntryPointSubscriber(EntryPointStateManager stateManager); @Override void accept(UpdateEntryPointEvent updateEntryPointEvent);  }
@Test public void should_update_configuration_when_entry_point_has_pending_configuration() { EntryPoint pending = mock(EntryPoint.class); EntryPoint expected = mock(EntryPoint.class); when(pending.mergeWithUpdate(updatedEntryPoint)).thenReturn(expected); when(stateManager.lock(entryPointKey)).thenReturn(true); when(stateManager.getCommittingConfiguration(entryPointKey)).thenReturn(Optional.empty()); when(stateManager.getPendingConfiguration(entryPointKey)).thenReturn(Optional.of(pending)); when(stateManager.getCurrentConfiguration(entryPointKey)).thenReturn(Optional.empty()); when(stateManager.prepare(entryPointKey, expected)).thenReturn(Optional.of(expected)); updateEntryPointSubscriber.accept(event); verify(stateManager).prepare(entryPointKey, expected); ArgumentCaptor<UpdateEntryPointResponse> captor = ArgumentCaptor.forClass(UpdateEntryPointResponse.class); verify(event).onSuccess(captor.capture()); }
@Test public void should_update_configuration_when_entry_point_has_committing_configuration() { EntryPoint committing = mock(EntryPoint.class); EntryPoint expected = mock(EntryPoint.class); when(committing.mergeWithUpdate(updatedEntryPoint)).thenReturn(expected); when(stateManager.lock(entryPointKey)).thenReturn(true); when(stateManager.getCommittingConfiguration(entryPointKey)).thenReturn(Optional.of(committing)); when(stateManager.getPendingConfiguration(entryPointKey)).thenReturn(Optional.empty()); when(stateManager.getCurrentConfiguration(entryPointKey)).thenReturn(Optional.empty()); when(stateManager.prepare(entryPointKey, expected)).thenReturn(Optional.of(expected)); updateEntryPointSubscriber.accept(event); verify(stateManager).prepare(entryPointKey, expected); ArgumentCaptor<UpdateEntryPointResponse> captor = ArgumentCaptor.forClass(UpdateEntryPointResponse.class); verify(event).onSuccess(captor.capture()); }
@Test public void should_update_configuration_when_entry_point_has_no_pending_and_no_committing_configuration() { EntryPoint current = mock(EntryPoint.class); EntryPoint expected = mock(EntryPoint.class); when(current.mergeWithUpdate(updatedEntryPoint)).thenReturn(expected); when(stateManager.lock(entryPointKey)).thenReturn(true); when(stateManager.getCommittingConfiguration(entryPointKey)).thenReturn(Optional.empty()); when(stateManager.getPendingConfiguration(entryPointKey)).thenReturn(Optional.empty()); when(stateManager.getCurrentConfiguration(entryPointKey)).thenReturn(Optional.of(current)); when(stateManager.prepare(entryPointKey, expected)).thenReturn(Optional.of(expected)); updateEntryPointSubscriber.accept(event); verify(stateManager).prepare(entryPointKey, expected); }
@Test public void should_call_on_error_when_entry_point_could_not_be_updated() { EntryPoint current = mock(EntryPoint.class); EntryPoint expected = mock(EntryPoint.class); when(stateManager.lock(entryPointKey)).thenReturn(true); when(stateManager.getCommittingConfiguration(entryPointKey)).thenReturn(Optional.empty()); when(stateManager.getPendingConfiguration(entryPointKey)).thenReturn(Optional.empty()); when(stateManager.getCurrentConfiguration(entryPointKey)).thenReturn(Optional.of(current)); when(current.mergeWithUpdate(updatedEntryPoint)).thenReturn(expected); when(stateManager.prepare(entryPointKey, expected)).thenReturn(Optional.empty()); updateEntryPointSubscriber.accept(event); verify(event).onError(any(IllegalStateException.class)); }
@Test public void should_call_on_error_when_entry_point_has_no_configuration_at_all() { when(stateManager.lock(entryPointKey)).thenReturn(true); when(stateManager.getCommittingConfiguration(entryPointKey)).thenReturn(Optional.empty()); when(stateManager.getPendingConfiguration(entryPointKey)).thenReturn(Optional.empty()); when(stateManager.getCurrentConfiguration(entryPointKey)).thenReturn(Optional.empty()); updateEntryPointSubscriber.accept(event); verify(stateManager, never()).prepare(any(), any()); verify(event).onError(any(IllegalStateException.class)); }
@Test public void should_call_on_error_when_lock_can_not_be_acquired() { when(stateManager.lock(entryPointKey)).thenReturn(false); updateEntryPointSubscriber.accept(event); verify(stateManager, never()).prepare(any(), any()); verify(event).onError(any(IllegalStateException.class)); }