WorkflowStateUtil { public static WorkflowState patchWorkflowState( final Optional<WorkflowState> originalWorkflowState, final WorkflowState patch) { return originalWorkflowState.map( o -> { WorkflowState.Builder builder = o.toBuilder() .enabled(patch.enabled().orElse(o.enabled().orElse(false))); patch.nextNaturalTrigger().ifPresent(builder::nextNaturalTrigger); return builder.build(); } ).orElse(patch); } private  WorkflowStateUtil(); static WorkflowState patchWorkflowState(
      final Optional<WorkflowState> originalWorkflowState,
      final WorkflowState patch);  }
@Test public void patchAnEmptyStateReturnsPatch() { WorkflowState patchedState = patchWorkflowState(Optional.empty(), FULLY_POPULATED_STATE); assertThat(patchedState, equalTo(FULLY_POPULATED_STATE)); }
@Test public void patchStateWithAnEmptyPatchReturnsOriginal() { WorkflowState patchedState = patchWorkflowState( Optional.of(FULLY_POPULATED_STATE), WorkflowState.empty()); assertThat(patchedState, equalTo(FULLY_POPULATED_STATE)); }
@Test public void patchEnabledFieldReturnsPatchedOriginal() { WorkflowState patch = WorkflowState.patchEnabled(false); WorkflowState patchedState = patchWorkflowState( Optional.of(FULLY_POPULATED_STATE), patch); assertThat(patchedState.enabled(), equalTo(patch.enabled())); assertThat(patchedState.nextNaturalTrigger().toString(), equalTo(FULLY_POPULATED_STATE.nextNaturalTrigger().toString())); assertThat(patchedState.nextNaturalOffsetTrigger().toString(), equalTo(FULLY_POPULATED_STATE.nextNaturalOffsetTrigger().toString())); }
@Test public void nonPopulatedNorPatchedEnabledShouldBeFalseAfterPatch() { WorkflowState patch = WorkflowState.empty(); WorkflowState patchedState = patchWorkflowState( Optional.of(WorkflowState.patchEnabled(false)), patch); assertThat(patchedState, equalTo(WorkflowState.patchEnabled(false))); }