From 986e26be5dceea2d07403714f15500e912f4ed92 Mon Sep 17 00:00:00 2001 From: Joe Edwards Date: Fri, 7 Jun 2024 16:17:16 +0100 Subject: [PATCH 1/3] PYIC-6711: Add capability to record audit events on journey events --- .../ProcessJourneyEventHandler.java | 80 ++++++++++++------- .../statemachine/StateMachine.java | 22 +++-- .../statemachine/TransitionResult.java | 14 ++++ .../statemachine/events/BasicEvent.java | 9 ++- .../statemachine/events/Event.java | 3 +- .../events/ExitNestedJourneyEvent.java | 3 +- .../statemachine/states/BasicState.java | 9 +-- .../states/JourneyChangeState.java | 4 +- .../states/NestedJourneyInvokeState.java | 19 +++-- .../statemachine/states/State.java | 3 +- .../stepresponses/CriStepResponse.java | 1 - .../stepresponses/StepResponse.java | 2 - .../journey-maps/new-p2-identity.yaml | 3 +- .../test/initial-journey-selection.yaml | 23 +++--- .../ProcessJourneyEventHandlerTest.java | 29 +++++-- .../statemachine/StateMachineTest.java | 44 ++++++---- .../statemachine/events/BasicEventTest.java | 16 ++-- .../events/ExitNestedJourneyEventTest.java | 7 +- .../statemachine/states/BasicStateTest.java | 19 +---- .../states/NestedJourneyInvokeStateTest.java | 27 ++++--- .../stepresponses/CriStepResponseTest.java | 2 +- 21 files changed, 206 insertions(+), 133 deletions(-) create mode 100644 lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/TransitionResult.java diff --git a/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/ProcessJourneyEventHandler.java b/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/ProcessJourneyEventHandler.java index 9857bd5ccd..989176ae8e 100644 --- a/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/ProcessJourneyEventHandler.java +++ b/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/ProcessJourneyEventHandler.java @@ -15,6 +15,7 @@ import uk.gov.di.ipv.core.library.auditing.AuditEventUser; import uk.gov.di.ipv.core.library.auditing.extension.AuditExtensionMitigationType; import uk.gov.di.ipv.core.library.auditing.extension.AuditExtensionSubjourneyType; +import uk.gov.di.ipv.core.library.auditing.extension.AuditExtensions; import uk.gov.di.ipv.core.library.auditing.restricted.AuditRestrictedDeviceInformation; import uk.gov.di.ipv.core.library.config.ConfigurationVariable; import uk.gov.di.ipv.core.library.domain.CoiSubjourneyType; @@ -48,11 +49,11 @@ import java.io.IOException; import java.time.Instant; -import java.util.Arrays; import java.util.EnumMap; import java.util.List; import java.util.Map; +import static com.amazonaws.util.CollectionUtils.isNullOrEmpty; import static uk.gov.di.ipv.core.library.config.ConfigurationVariable.BACKEND_SESSION_TIMEOUT; import static uk.gov.di.ipv.core.library.domain.CoiSubjourneyType.isCoiSubjourneyEvent; import static uk.gov.di.ipv.core.library.domain.IpvJourneyTypes.SESSION_TIMEOUT; @@ -168,22 +169,6 @@ public Map handleRequest(JourneyRequest journeyRequest, Context auditEventUser, stepResponse.getMitigationStart(), deviceInformation); } - String journeyAuditEvent = stepResponse.getAuditEvent(); - if (journeyAuditEvent != null) { - boolean eventTypeExists = - Arrays.stream(AuditEventTypes.values()) - .anyMatch(eventType -> eventType.name().equals(journeyAuditEvent)); - if (!eventTypeExists) { - LOGGER.error( - LogHelper.buildLogMessage("Invalid audit event type provided") - .with(LOG_JOURNEY_EVENT.getFieldName(), journeyAuditEvent)); - throw new JourneyEngineException( - "Invalid audit event type provided, failed to execute journey engine step."); - } - AuditEventTypes auditEventType = AuditEventTypes.valueOf(journeyAuditEvent); - sendJourneyAuditEvent(auditEventType, auditEventUser, deviceInformation); - } - return stepResponse.value(); } catch (HttpResponseExceptionWithErrorBody e) { return StepFunctionHelpers.generateErrorOutputMap( @@ -214,7 +199,13 @@ private StepResponse executeJourneyEvent( var initialJourneyType = ipvSessionItem.getJourneyType(); try { - var newState = executeStateTransition(ipvSessionItem, journeyEvent, currentPage); + var newState = + executeStateTransition( + ipvSessionItem, + journeyEvent, + currentPage, + auditEventUser, + deviceInformation); while (newState instanceof JourneyChangeState journeyChangeState) { LOGGER.info( @@ -229,7 +220,13 @@ private StepResponse executeJourneyEvent( ipvSessionItem.setUserState(journeyChangeState.getInitialState()); sendSubJourneyStartAuditEvent( auditEventUser, journeyChangeState.getJourneyType(), deviceInformation); - newState = executeStateTransition(ipvSessionItem, NEXT_EVENT, null); + newState = + executeStateTransition( + ipvSessionItem, + NEXT_EVENT, + null, + auditEventUser, + deviceInformation); } var basicState = (BasicState) newState; @@ -269,8 +266,13 @@ private StepResponse executeJourneyEvent( @Tracing private State executeStateTransition( - IpvSessionItem ipvSessionItem, String journeyEvent, String currentPage) - throws StateMachineNotFoundException, UnknownEventException, UnknownStateException { + IpvSessionItem ipvSessionItem, + String journeyEvent, + String currentPage, + AuditEventUser auditEventUser, + String deviceInformation) + throws StateMachineNotFoundException, SqsException, UnknownEventException, + UnknownStateException { StateMachine stateMachine = stateMachines.get(ipvSessionItem.getJourneyType()); if (stateMachine == null) { throw new StateMachineNotFoundException( @@ -284,11 +286,21 @@ private State executeStateTransition( "Found state machine for journey type: %s", ipvSessionItem.getJourneyType().name()))); - return stateMachine.transition( - ipvSessionItem.getUserState(), - journeyEvent, - new JourneyContext(configService), - currentPage); + var result = + stateMachine.transition( + ipvSessionItem.getUserState(), + journeyEvent, + new JourneyContext(configService), + currentPage); + + if (!isNullOrEmpty(result.auditEvents())) { + for (var auditEventType : result.auditEvents()) { + sendJourneyAuditEvent( + auditEventType, result.auditContext(), auditEventUser, deviceInformation); + } + } + + return result.state(); } @Tracing @@ -373,17 +385,29 @@ private void sendMitigationStartAuditEvent( } private void sendJourneyAuditEvent( - AuditEventTypes auditEventType, AuditEventUser auditEventUser, String deviceInformation) + AuditEventTypes auditEventType, + Map auditContext, + AuditEventUser auditEventUser, + String deviceInformation) throws SqsException { - auditService.sendAuditEvent( new AuditEvent( auditEventType, configService.getSsmParameter(ConfigurationVariable.COMPONENT_ID), auditEventUser, + getAuditExtensions(auditEventType, auditContext), new AuditRestrictedDeviceInformation(deviceInformation))); } + private AuditExtensions getAuditExtensions( + AuditEventTypes auditEventType, Map auditContext) { + return switch (auditEventType) { + case IPV_MITIGATION_START -> new AuditExtensionMitigationType( + auditContext.get("mitigationType")); + default -> null; + }; + } + private void sendSubJourneyStartAuditEvent( AuditEventUser auditEventUser, IpvJourneyTypes journeyType, String deviceInformation) throws SqsException { diff --git a/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/StateMachine.java b/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/StateMachine.java index 8a5628af84..c45ea4f86e 100644 --- a/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/StateMachine.java +++ b/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/StateMachine.java @@ -15,13 +15,15 @@ public class StateMachine { public static final String DELIMITER = "/"; + private static final String ATTEMPT_RECOVERY_EVENT = "attempt-recovery"; + private final Map states; public StateMachine(StateMachineInitializer initializer) throws IOException { this.states = initializer.initialize(); } - public State transition( + public TransitionResult transition( String startState, String event, JourneyContext journeyContext, String currentPage) throws UnknownEventException, UnknownStateException { var state = states.get(startState.split(DELIMITER)[0]); @@ -31,9 +33,10 @@ public State transition( String.format("Unknown state provided to state machine: %s", startState)); } + // Check page event is allowed if (currentPage != null && state instanceof BasicState basicState) { if (isPageOrCriStateAndOutOfSync(basicState, currentPage)) { - return state; + return new TransitionResult(state); } else if (basicState.getResponse() instanceof ProcessStepResponse) { throw new UnknownStateException( String.format( @@ -42,12 +45,19 @@ public State transition( } } - State newState = state.transition(event, startState, journeyContext); - if (newState instanceof NestedJourneyInvokeState) { - return newState.transition(event, startState, journeyContext); + // Special recovery event + if (ATTEMPT_RECOVERY_EVENT.equals(event)) { + return new TransitionResult(state); + } + + var result = state.transition(event, startState, journeyContext); + + // Resolve nested journey + if (result.state() instanceof NestedJourneyInvokeState) { + return result.state().transition(event, startState, journeyContext); } - return newState; + return result; } private boolean isPageOrCriStateAndOutOfSync(BasicState basicState, String currentPage) { diff --git a/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/TransitionResult.java b/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/TransitionResult.java new file mode 100644 index 0000000000..ee16260da3 --- /dev/null +++ b/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/TransitionResult.java @@ -0,0 +1,14 @@ +package uk.gov.di.ipv.core.processjourneyevent.statemachine; + +import uk.gov.di.ipv.core.library.auditing.AuditEventTypes; +import uk.gov.di.ipv.core.processjourneyevent.statemachine.states.State; + +import java.util.List; +import java.util.Map; + +public record TransitionResult( + State state, List auditEvents, Map auditContext) { + public TransitionResult(State state) { + this(state, null, null); + } +} diff --git a/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/events/BasicEvent.java b/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/events/BasicEvent.java index c6537cdef6..9d34bb04c6 100644 --- a/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/events/BasicEvent.java +++ b/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/events/BasicEvent.java @@ -3,13 +3,16 @@ import lombok.Data; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import uk.gov.di.ipv.core.library.auditing.AuditEventTypes; import uk.gov.di.ipv.core.library.domain.IpvJourneyTypes; +import uk.gov.di.ipv.core.processjourneyevent.statemachine.TransitionResult; import uk.gov.di.ipv.core.processjourneyevent.statemachine.exceptions.UnknownEventException; import uk.gov.di.ipv.core.processjourneyevent.statemachine.states.JourneyChangeState; import uk.gov.di.ipv.core.processjourneyevent.statemachine.states.State; import uk.gov.di.ipv.core.processjourneyevent.statemachine.stepresponses.JourneyContext; import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; import java.util.Optional; @@ -22,8 +25,10 @@ public class BasicEvent implements Event { private State targetStateObj; private LinkedHashMap checkIfDisabled; private LinkedHashMap checkFeatureFlag; + private List auditEvents; + private LinkedHashMap auditContext; - public State resolve(JourneyContext journeyContext) throws UnknownEventException { + public TransitionResult resolve(JourneyContext journeyContext) throws UnknownEventException { if (checkIfDisabled != null) { Optional firstDisabledCri = checkIfDisabled.keySet().stream() @@ -50,7 +55,7 @@ public State resolve(JourneyContext journeyContext) throws UnknownEventException return checkFeatureFlag.get(featureFlagValue).resolve(journeyContext); } } - return targetStateObj; + return new TransitionResult(targetStateObj, auditEvents, auditContext); } @Override diff --git a/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/events/Event.java b/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/events/Event.java index e9fae86147..ea4ad0ce76 100644 --- a/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/events/Event.java +++ b/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/events/Event.java @@ -2,6 +2,7 @@ import com.fasterxml.jackson.annotation.JsonSubTypes; import com.fasterxml.jackson.annotation.JsonTypeInfo; +import uk.gov.di.ipv.core.processjourneyevent.statemachine.TransitionResult; import uk.gov.di.ipv.core.processjourneyevent.statemachine.exceptions.UnknownEventException; import uk.gov.di.ipv.core.processjourneyevent.statemachine.states.State; import uk.gov.di.ipv.core.processjourneyevent.statemachine.stepresponses.JourneyContext; @@ -14,7 +15,7 @@ @JsonSubTypes.Type(value = ExitNestedJourneyEvent.class) }) public interface Event { - State resolve(JourneyContext journeyContext) throws UnknownEventException; + TransitionResult resolve(JourneyContext journeyContext) throws UnknownEventException; void initialize(String name, Map states); } diff --git a/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/events/ExitNestedJourneyEvent.java b/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/events/ExitNestedJourneyEvent.java index 0577a929a1..01733edb54 100644 --- a/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/events/ExitNestedJourneyEvent.java +++ b/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/events/ExitNestedJourneyEvent.java @@ -1,6 +1,7 @@ package uk.gov.di.ipv.core.processjourneyevent.statemachine.events; import lombok.Data; +import uk.gov.di.ipv.core.processjourneyevent.statemachine.TransitionResult; import uk.gov.di.ipv.core.processjourneyevent.statemachine.exceptions.UnknownEventException; import uk.gov.di.ipv.core.processjourneyevent.statemachine.states.State; import uk.gov.di.ipv.core.processjourneyevent.statemachine.stepresponses.JourneyContext; @@ -14,7 +15,7 @@ public class ExitNestedJourneyEvent implements Event { private Map nestedJourneyExitEvents; @Override - public State resolve(JourneyContext journeyContext) throws UnknownEventException { + public TransitionResult resolve(JourneyContext journeyContext) throws UnknownEventException { Event event = nestedJourneyExitEvents.get(exitEventToEmit); if (event == null) { throw new UnknownEventException("Event '%s' not found in nested journey's exit events"); diff --git a/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/states/BasicState.java b/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/states/BasicState.java index 33335461f5..c5a17bb032 100644 --- a/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/states/BasicState.java +++ b/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/states/BasicState.java @@ -5,6 +5,7 @@ import lombok.NoArgsConstructor; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import uk.gov.di.ipv.core.processjourneyevent.statemachine.TransitionResult; import uk.gov.di.ipv.core.processjourneyevent.statemachine.events.Event; import uk.gov.di.ipv.core.processjourneyevent.statemachine.exceptions.UnknownEventException; import uk.gov.di.ipv.core.processjourneyevent.statemachine.stepresponses.JourneyContext; @@ -19,7 +20,6 @@ @AllArgsConstructor public class BasicState implements State { private static final Logger LOGGER = LogManager.getLogger(); - private static final String ATTEMPT_RECOVERY_EVENT = "attempt-recovery"; private String name; private String parent; private BasicState parentObj; @@ -27,12 +27,9 @@ public class BasicState implements State { private Map events = new HashMap<>(); @Override - public State transition(String eventName, String startState, JourneyContext journeyContext) + public TransitionResult transition( + String eventName, String startState, JourneyContext journeyContext) throws UnknownEventException { - if (ATTEMPT_RECOVERY_EVENT.equals(eventName)) { - return this; - } - return getEvent(eventName) .orElseThrow( () -> diff --git a/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/states/JourneyChangeState.java b/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/states/JourneyChangeState.java index 5e94fb2bf2..7c1bb3e96c 100644 --- a/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/states/JourneyChangeState.java +++ b/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/states/JourneyChangeState.java @@ -3,6 +3,7 @@ import lombok.AllArgsConstructor; import lombok.Data; import uk.gov.di.ipv.core.library.domain.IpvJourneyTypes; +import uk.gov.di.ipv.core.processjourneyevent.statemachine.TransitionResult; import uk.gov.di.ipv.core.processjourneyevent.statemachine.exceptions.UnknownEventException; import uk.gov.di.ipv.core.processjourneyevent.statemachine.exceptions.UnknownStateException; import uk.gov.di.ipv.core.processjourneyevent.statemachine.stepresponses.JourneyContext; @@ -14,7 +15,8 @@ public class JourneyChangeState implements State { private String initialState; @Override - public State transition(String eventName, String startState, JourneyContext journeyContext) + public TransitionResult transition( + String eventName, String startState, JourneyContext journeyContext) throws UnknownEventException, UnknownStateException { throw new IllegalStateException("Cannot transition from JourneyChangeState"); } diff --git a/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/states/NestedJourneyInvokeState.java b/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/states/NestedJourneyInvokeState.java index e9a07557b4..92af96e6a8 100644 --- a/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/states/NestedJourneyInvokeState.java +++ b/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/states/NestedJourneyInvokeState.java @@ -5,6 +5,7 @@ import lombok.NoArgsConstructor; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import uk.gov.di.ipv.core.processjourneyevent.statemachine.TransitionResult; import uk.gov.di.ipv.core.processjourneyevent.statemachine.events.Event; import uk.gov.di.ipv.core.processjourneyevent.statemachine.exceptions.UnknownEventException; import uk.gov.di.ipv.core.processjourneyevent.statemachine.exceptions.UnknownStateException; @@ -26,11 +27,13 @@ public class NestedJourneyInvokeState implements State { private Map exitEvents; private String name; - public State transition(String eventName, String startState, JourneyContext journeyContext) + @Override + public TransitionResult transition( + String eventName, String startState, JourneyContext journeyContext) throws UnknownEventException, UnknownStateException { Queue stateNameParts = getStateNameParts(startState); - State nextState; + TransitionResult result; if (stateNameParts.size() == 1) { // We've not descended into the nested-states yet Event event = nestedJourneyDefinition.getEntryEvents().get(eventName); if (event == null) { @@ -39,7 +42,7 @@ public State transition(String eventName, String startState, JourneyContext jour "Unknown entry event '%s' for '%s' state nested journey definition", eventName, name)); } - nextState = event.resolve(journeyContext); + result = event.resolve(journeyContext); } else { stateNameParts.remove(); State currentNestedState = @@ -50,17 +53,17 @@ public State transition(String eventName, String startState, JourneyContext jour "State '%s' not found in nested journey definition for `%s`", stateNameParts.peek(), name)); } - nextState = + result = currentNestedState.transition( eventName, String.join(DELIMITER, stateNameParts), journeyContext); } - if (nextState instanceof NestedJourneyInvokeState) { - return nextState.transition( - eventName, String.join(DELIMITER, stateNameParts), journeyContext); + if (result.state() instanceof NestedJourneyInvokeState) { + return result.state() + .transition(eventName, String.join(DELIMITER, stateNameParts), journeyContext); } - return nextState; + return result; } private Queue getStateNameParts(String stateName) { diff --git a/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/states/State.java b/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/states/State.java index ebb4e7f8cd..bdac858296 100644 --- a/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/states/State.java +++ b/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/states/State.java @@ -2,6 +2,7 @@ import com.fasterxml.jackson.annotation.JsonSubTypes; import com.fasterxml.jackson.annotation.JsonTypeInfo; +import uk.gov.di.ipv.core.processjourneyevent.statemachine.TransitionResult; import uk.gov.di.ipv.core.processjourneyevent.statemachine.exceptions.UnknownEventException; import uk.gov.di.ipv.core.processjourneyevent.statemachine.exceptions.UnknownStateException; import uk.gov.di.ipv.core.processjourneyevent.statemachine.stepresponses.JourneyContext; @@ -12,6 +13,6 @@ @JsonSubTypes.Type(value = NestedJourneyInvokeState.class), }) public interface State { - State transition(String eventName, String startState, JourneyContext journeyContext) + TransitionResult transition(String eventName, String startState, JourneyContext journeyContext) throws UnknownEventException, UnknownStateException; } diff --git a/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/CriStepResponse.java b/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/CriStepResponse.java index 7cc53b963c..93e7815c5d 100644 --- a/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/CriStepResponse.java +++ b/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/CriStepResponse.java @@ -34,7 +34,6 @@ public class CriStepResponse implements StepResponse { private String context; private EvidenceRequest evidenceRequest; private String mitigationStart; - private String auditEvent; public Map value() { try { diff --git a/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/StepResponse.java b/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/StepResponse.java index ec7b6ed3b2..53a7b288a4 100644 --- a/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/StepResponse.java +++ b/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/StepResponse.java @@ -16,6 +16,4 @@ public interface StepResponse { Map value(); String getMitigationStart(); - - String getAuditEvent(); } diff --git a/lambdas/process-journey-event/src/main/resources/statemachine/journey-maps/new-p2-identity.yaml b/lambdas/process-journey-event/src/main/resources/statemachine/journey-maps/new-p2-identity.yaml index 0a3152ff9d..eff6433905 100644 --- a/lambdas/process-journey-event/src/main/resources/statemachine/journey-maps/new-p2-identity.yaml +++ b/lambdas/process-journey-event/src/main/resources/statemachine/journey-maps/new-p2-identity.yaml @@ -207,6 +207,8 @@ states: events: next: targetState: CRI_CLAIMED_IDENTITY_M2B + auditEvents: + - IPV_NO_PHOTO_ID_JOURNEY_START end: targetState: PYI_ESCAPE_M2B @@ -713,7 +715,6 @@ states: response: type: cri criId: claimedIdentity - auditEvent: IPV_NO_PHOTO_ID_JOURNEY_START context: bank_account parent: CRI_STATE events: diff --git a/lambdas/process-journey-event/src/main/resources/statemachine/test/initial-journey-selection.yaml b/lambdas/process-journey-event/src/main/resources/statemachine/test/initial-journey-selection.yaml index 0be9b531ce..88c4aa4799 100644 --- a/lambdas/process-journey-event/src/main/resources/statemachine/test/initial-journey-selection.yaml +++ b/lambdas/process-journey-event/src/main/resources/statemachine/test/initial-journey-selection.yaml @@ -36,9 +36,16 @@ states: testWithMitigationStart: targetState: PAGE_STATE_AT_START_OF_MITIGATION testWithAuditEvent: - targetState: PAGE_STATE_AT_START_OF_NO_PHOTO_ID - testWithWrongAuditEvent: - targetState: WRONG_AUDIT_EVENT_STATE + targetState: ERROR_STATE + auditEvents: + - IPV_NO_PHOTO_ID_JOURNEY_START + testWithAuditEventContext: + targetState: ERROR_STATE + auditEvents: + - IPV_NO_PHOTO_ID_JOURNEY_START + - IPV_MITIGATION_START + auditContext: + mitigationType: test-mitigation testJourneyStep: targetJourney: TECHNICAL_ERROR targetState: ERROR @@ -88,16 +95,6 @@ states: response: type: page pageId: page-id-for-some-page - auditEvent: IPV_NO_PHOTO_ID_JOURNEY_START - events: - enterNestedJourneyAtStateOne: - targetState: NESTED_JOURNEY_INVOKE_STATE - - WRONG_AUDIT_EVENT_STATE: - response: - type: page - pageId: page-id-for-some-page - auditEvent: WRONG_AUDIT_EVENT_TYPE events: enterNestedJourneyAtStateOne: targetState: NESTED_JOURNEY_INVOKE_STATE diff --git a/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/ProcessJourneyEventHandlerTest.java b/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/ProcessJourneyEventHandlerTest.java index b8e6d8f885..4614efc560 100644 --- a/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/ProcessJourneyEventHandlerTest.java +++ b/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/ProcessJourneyEventHandlerTest.java @@ -38,6 +38,7 @@ import static org.junit.jupiter.api.Assertions.assertNull; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static uk.gov.di.ipv.core.library.config.ConfigurationVariable.BACKEND_SESSION_TIMEOUT; @@ -514,21 +515,35 @@ void shouldSendAuditEventWhenThereIsAuditEventInJourneyMap() throws Exception { } @Test - void shouldReturnErrorWhenWrongAuditEvenTypeProvidedInJourneyMap() throws Exception { + void shouldSendMultipleAuditEventsWithContext() throws Exception { var input = JourneyRequest.builder() .ipAddress(TEST_IP) - .journey("testWithWrongAuditEvent") + .journey("testWithAuditEventContext") .ipvSessionId(TEST_IP) .build(); mockIpvSessionItemAndTimeout("CRI_STATE"); - Map output = - getProcessJourneyStepHandler().handleRequest(input, mockContext); + getProcessJourneyStepHandler(StateMachineInitializerMode.TEST) + .handleRequest(input, mockContext); - assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, output.get(STATUS_CODE)); - assertEquals(ErrorResponse.FAILED_JOURNEY_ENGINE_STEP.getCode(), output.get(CODE)); - assertEquals(ErrorResponse.FAILED_JOURNEY_ENGINE_STEP.getMessage(), output.get(MESSAGE)); + verify(mockAuditService, times(2)).sendAuditEvent(auditEventCaptor.capture()); + var capturedAuditEvents = auditEventCaptor.getAllValues(); + assertEquals(capturedAuditEvents.size(), 2); + + var firstEvent = capturedAuditEvents.get(0); + assertEquals(AuditEventTypes.IPV_NO_PHOTO_ID_JOURNEY_START, firstEvent.getEventName()); + assertEquals("core", firstEvent.getComponentId()); + assertEquals("testuserid", firstEvent.getUser().getUserId()); + assertEquals("testjourneyid", firstEvent.getUser().getGovukSigninJourneyId()); + + var secondEvent = capturedAuditEvents.get(1); + assertEquals(AuditEventTypes.IPV_MITIGATION_START, secondEvent.getEventName()); + assertEquals("core", secondEvent.getComponentId()); + assertEquals("testuserid", secondEvent.getUser().getUserId()); + assertEquals("testjourneyid", secondEvent.getUser().getGovukSigninJourneyId()); + assertEquals( + new AuditExtensionMitigationType("test-mitigation"), secondEvent.getExtensions()); } private void mockIpvSessionItemAndTimeout(String userState) { diff --git a/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/StateMachineTest.java b/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/StateMachineTest.java index aaa78c47ab..4792e3f41d 100644 --- a/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/StateMachineTest.java +++ b/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/StateMachineTest.java @@ -21,11 +21,11 @@ class StateMachineTest { @Test void transitionShouldReturnAppropriateState() throws Exception { - State expectedEndState = new BasicState(); + var expectedResult = new TransitionResult(new BasicState()); State startingState = mock(BasicState.class); when(startingState.transition("event", "START_STATE", JOURNEY_CONTEXT)) - .thenReturn(expectedEndState); + .thenReturn(expectedResult); StateMachineInitializer mockStateMachineInitializer = mock(StateMachineInitializer.class); when(mockStateMachineInitializer.initialize()) @@ -33,10 +33,9 @@ void transitionShouldReturnAppropriateState() throws Exception { StateMachine stateMachine = new StateMachine(mockStateMachineInitializer); - State transitionedState = - stateMachine.transition("START_STATE", "event", JOURNEY_CONTEXT, null); + var actualResult = stateMachine.transition("START_STATE", "event", JOURNEY_CONTEXT, null); - assertEquals(expectedEndState, transitionedState); + assertEquals(expectedResult, actualResult); } @Test @@ -54,14 +53,14 @@ void transitionShouldThrowIfGivenAnUnknownState() throws Exception { @Test void transitionShouldTransitionIntoNestedJourneyInvokeState() throws Exception { - State expectedNestedEndState = new BasicState(); + var expectedResult = new TransitionResult(new BasicState()); State nestedJourneyInvokeState = mock(NestedJourneyInvokeState.class); when(nestedJourneyInvokeState.transition("event", "START_STATE", JOURNEY_CONTEXT)) - .thenReturn(expectedNestedEndState); + .thenReturn(expectedResult); State startingState = mock(BasicState.class); when(startingState.transition("event", "START_STATE", JOURNEY_CONTEXT)) - .thenReturn(nestedJourneyInvokeState); + .thenReturn(new TransitionResult(nestedJourneyInvokeState)); StateMachineInitializer mockStateMachineInitializer = mock(StateMachineInitializer.class); when(mockStateMachineInitializer.initialize()) @@ -69,19 +68,18 @@ void transitionShouldTransitionIntoNestedJourneyInvokeState() throws Exception { StateMachine stateMachine = new StateMachine(mockStateMachineInitializer); - State transitionState = - stateMachine.transition("START_STATE", "event", JOURNEY_CONTEXT, null); + var actualResult = stateMachine.transition("START_STATE", "event", JOURNEY_CONTEXT, null); - assertEquals(expectedNestedEndState, transitionState); + assertEquals(expectedResult, actualResult); } @Test void transitionShouldHandleNestedStateName() throws Exception { - State expectedEndState = new BasicState(); + var expectedResult = new TransitionResult(new BasicState()); State startingState = mock(NestedJourneyInvokeState.class); when(startingState.transition("event", "START_STATE/NESTED_JOURNEY", JOURNEY_CONTEXT)) - .thenReturn(expectedEndState); + .thenReturn(expectedResult); StateMachineInitializer mockStateMachineInitializer = mock(StateMachineInitializer.class); when(mockStateMachineInitializer.initialize()) @@ -89,10 +87,26 @@ void transitionShouldHandleNestedStateName() throws Exception { StateMachine stateMachine = new StateMachine(mockStateMachineInitializer); - State transitionedState = + var actualResult = stateMachine.transition( "START_STATE/NESTED_JOURNEY", "event", JOURNEY_CONTEXT, null); - assertEquals(expectedEndState, transitionedState); + assertEquals(expectedResult, actualResult); + } + + @Test + void transitionShouldReturnSameStateIfAttemptedRecovery() throws Exception { + State startingState = mock(BasicState.class); + + StateMachineInitializer mockStateMachineInitializer = mock(StateMachineInitializer.class); + when(mockStateMachineInitializer.initialize()) + .thenReturn(Map.of("START_STATE", startingState)); + + StateMachine stateMachine = new StateMachine(mockStateMachineInitializer); + + var actualResult = + stateMachine.transition("START_STATE", "attempt-recovery", JOURNEY_CONTEXT, null); + + assertEquals(startingState, actualResult.state()); } } diff --git a/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/events/BasicEventTest.java b/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/events/BasicEventTest.java index c1dc69549d..4ef64456be 100644 --- a/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/events/BasicEventTest.java +++ b/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/events/BasicEventTest.java @@ -7,8 +7,8 @@ import org.mockito.junit.jupiter.MockitoExtension; import uk.gov.di.ipv.core.library.config.CoreFeatureFlag; import uk.gov.di.ipv.core.library.service.ConfigService; +import uk.gov.di.ipv.core.processjourneyevent.statemachine.TransitionResult; import uk.gov.di.ipv.core.processjourneyevent.statemachine.states.BasicState; -import uk.gov.di.ipv.core.processjourneyevent.statemachine.states.State; import uk.gov.di.ipv.core.processjourneyevent.statemachine.stepresponses.JourneyContext; import java.util.LinkedHashMap; @@ -24,11 +24,11 @@ class BasicEventTest { @Test void resolveShouldReturnAState() throws Exception { - BasicState targetState = new BasicState(); + var expectedResult = new TransitionResult(new BasicState()); BasicEvent basicEvent = new BasicEvent(); - basicEvent.setTargetStateObj(targetState); + basicEvent.setTargetStateObj(expectedResult.state()); - assertEquals(targetState, basicEvent.resolve(journeyContext)); + assertEquals(expectedResult, basicEvent.resolve(journeyContext)); } @Test @@ -47,9 +47,9 @@ void resolveShouldReturnAlternativeStateIfACheckedCriIsDisabled() throws Excepti checkIfDisabled.put("aDisabledCri", alternativeEvent); basicEventWithCheckIfDisabledConfigured.setCheckIfDisabled(checkIfDisabled); - State resolve = basicEventWithCheckIfDisabledConfigured.resolve(journeyContext); + var result = basicEventWithCheckIfDisabledConfigured.resolve(journeyContext); - assertEquals(alternativeTargetState, resolve); + assertEquals(alternativeTargetState, result.state()); } @Test @@ -66,9 +66,9 @@ void resolveShouldReturnAlternativeStateIfACheckedFeatureFlagIsSet() throws Exce CoreFeatureFlag.UNUSED_PLACEHOLDER.getName(), eventWithCheckFeatureFlagConfigured); defaultEvent.setCheckFeatureFlag(checkFeatureFlag); - State resolve = defaultEvent.resolve(journeyContext); + var result = defaultEvent.resolve(journeyContext); - assertEquals(featureFlagTargetState, resolve); + assertEquals(featureFlagTargetState, result.state()); } @Test diff --git a/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/events/ExitNestedJourneyEventTest.java b/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/events/ExitNestedJourneyEventTest.java index a86d0ded82..6e0235ff19 100644 --- a/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/events/ExitNestedJourneyEventTest.java +++ b/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/events/ExitNestedJourneyEventTest.java @@ -2,6 +2,7 @@ import org.junit.jupiter.api.Test; import uk.gov.di.ipv.core.library.service.ConfigService; +import uk.gov.di.ipv.core.processjourneyevent.statemachine.TransitionResult; import uk.gov.di.ipv.core.processjourneyevent.statemachine.exceptions.UnknownEventException; import uk.gov.di.ipv.core.processjourneyevent.statemachine.states.BasicState; import uk.gov.di.ipv.core.processjourneyevent.statemachine.states.State; @@ -21,16 +22,16 @@ class ExitNestedJourneyEventTest { @Test void resolveShouldResolveEventFromNestedJourneyExitEvents() throws UnknownEventException { - BasicState expectedState = new BasicState(); + var expectedResult = new TransitionResult(new BasicState()); BasicEvent nestedJourneyExitEvent = mock(BasicEvent.class); - when(nestedJourneyExitEvent.resolve(any(JourneyContext.class))).thenReturn(expectedState); + when(nestedJourneyExitEvent.resolve(any(JourneyContext.class))).thenReturn(expectedResult); ExitNestedJourneyEvent exitNestedJourneyEvent = new ExitNestedJourneyEvent(); exitNestedJourneyEvent.setExitEventToEmit("exiting"); exitNestedJourneyEvent.setNestedJourneyExitEvents( Map.of("exiting", nestedJourneyExitEvent)); - assertEquals(expectedState, exitNestedJourneyEvent.resolve(JOURNEY_CONTEXT)); + assertEquals(expectedResult, exitNestedJourneyEvent.resolve(JOURNEY_CONTEXT)); } @Test diff --git a/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/states/BasicStateTest.java b/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/states/BasicStateTest.java index ee0749de42..d881ede0c7 100644 --- a/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/states/BasicStateTest.java +++ b/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/states/BasicStateTest.java @@ -14,7 +14,6 @@ import java.util.Map; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; @ExtendWith(MockitoExtension.class) @@ -33,11 +32,9 @@ void transitionShouldReturnAStateWithAResponse() throws Exception { currentToTargetEvent.setTargetStateObj(targetState); currentState.setEvents(Map.of("next", currentToTargetEvent)); - BasicState transitionedState = - (BasicState) currentState.transition("next", "startState", journeyContext); + var result = currentState.transition("next", "startState", journeyContext); - assertEquals(targetState, transitionedState); - assertEquals(stepResponse, transitionedState.getResponse()); + assertEquals(targetState, result.state()); } @Test @@ -52,17 +49,9 @@ void transitionShouldUseEventsFromParentState() throws Exception { BasicState currentState = new BasicState(); currentState.setParentObj(parentState); - State transitionedState = - currentState.transition("parent-event", "startState", journeyContext); + var result = currentState.transition("parent-event", "startState", journeyContext); - assertEquals(parentEventTargetState, transitionedState); - } - - @Test - void transitionShouldReturnThisIfAttemptRecoveryEventReceived() throws Exception { - State state = new BasicState(); - - assertSame(state, state.transition("attempt-recovery", "startState", journeyContext)); + assertEquals(parentEventTargetState, result.state()); } @Test diff --git a/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/states/NestedJourneyInvokeStateTest.java b/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/states/NestedJourneyInvokeStateTest.java index 6176d82e44..d018bf3227 100644 --- a/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/states/NestedJourneyInvokeStateTest.java +++ b/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/states/NestedJourneyInvokeStateTest.java @@ -2,6 +2,7 @@ import org.junit.jupiter.api.Test; import uk.gov.di.ipv.core.library.service.ConfigService; +import uk.gov.di.ipv.core.processjourneyevent.statemachine.TransitionResult; import uk.gov.di.ipv.core.processjourneyevent.statemachine.events.BasicEvent; import uk.gov.di.ipv.core.processjourneyevent.statemachine.exceptions.UnknownEventException; import uk.gov.di.ipv.core.processjourneyevent.statemachine.exceptions.UnknownStateException; @@ -22,9 +23,9 @@ class NestedJourneyInvokeStateTest { @Test void transitionShouldUseEntryEventsWhenStartStateHasOnePart() throws Exception { - BasicState expectedEndState = new BasicState(); + var expectedResult = new TransitionResult(new BasicState()); BasicEvent basicEvent = mock(BasicEvent.class); - when(basicEvent.resolve(any(JourneyContext.class))).thenReturn(expectedEndState); + when(basicEvent.resolve(any(JourneyContext.class))).thenReturn(expectedResult); NestedJourneyDefinition nestedJourneyDefinition = new NestedJourneyDefinition(); nestedJourneyDefinition.setEntryEvents(Map.of("next", basicEvent)); @@ -32,10 +33,10 @@ void transitionShouldUseEntryEventsWhenStartStateHasOnePart() throws Exception { NestedJourneyInvokeState nestedJourneyInvokeState = new NestedJourneyInvokeState(); nestedJourneyInvokeState.setNestedJourneyDefinition(nestedJourneyDefinition); - State transitionedToState = + var actualResult = nestedJourneyInvokeState.transition("next", "INVOKE_STATE", JOURNEY_CONTEXT); - assertEquals(expectedEndState, transitionedToState); + assertEquals(expectedResult, actualResult); } @Test @@ -45,19 +46,19 @@ void transitionShouldReturnStateFromNestedJourneyDefinitionIfStartStateHasMultip BasicState currentNestedState = mock(BasicState.class); nestedJourneyDefinition.setNestedJourneyStates(Map.of("NESTED_STATE", currentNestedState)); - BasicState expectedEndState = new BasicState(); + var expectedResult = new TransitionResult(new BasicState()); when(currentNestedState.transition( eq("next"), eq("NESTED_STATE"), any(JourneyContext.class))) - .thenReturn(expectedEndState); + .thenReturn(expectedResult); NestedJourneyInvokeState nestedJourneyInvokeState = new NestedJourneyInvokeState(); nestedJourneyInvokeState.setNestedJourneyDefinition(nestedJourneyDefinition); - State transitionedToState = + var actualResult = nestedJourneyInvokeState.transition( "next", "INVOKE_STATE/NESTED_STATE", JOURNEY_CONTEXT); - assertEquals(expectedEndState, transitionedToState); + assertEquals(expectedResult, actualResult); } @Test @@ -71,19 +72,19 @@ void transitionShouldHandleANestedNestedJourneyInvokeState() throws Exception { NestedJourneyInvokeState nestedNestedJourneyInvokeState = mock(NestedJourneyInvokeState.class); - BasicState expectedEndState = new BasicState(); + var expectedResult = new TransitionResult(new BasicState()); when(nestedNestedJourneyInvokeState.transition( eq("next"), eq("NESTED_STATE"), any(JourneyContext.class))) - .thenReturn(expectedEndState); + .thenReturn(expectedResult); when(currentNestedState.transition( eq("next"), eq("NESTED_STATE"), any(JourneyContext.class))) - .thenReturn(nestedNestedJourneyInvokeState); + .thenReturn(new TransitionResult(nestedNestedJourneyInvokeState)); - State transitionedToState = + var actualResult = nestedJourneyInvokeState.transition( "next", "INVOKE_STATE/NESTED_STATE", JOURNEY_CONTEXT); - assertEquals(expectedEndState, transitionedToState); + assertEquals(expectedResult, actualResult); } @Test diff --git a/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/CriStepResponseTest.java b/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/CriStepResponseTest.java index 61f4ba8e2b..5376728982 100644 --- a/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/CriStepResponseTest.java +++ b/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/CriStepResponseTest.java @@ -16,7 +16,7 @@ class CriStepResponseTest { @MethodSource("journeyUriParameters") void valueReturnsExpectedJourneyResponse( String criId, String context, EvidenceRequest evidenceRequest, String expectedJourney) { - CriStepResponse response = new CriStepResponse(criId, context, evidenceRequest, null, null); + CriStepResponse response = new CriStepResponse(criId, context, evidenceRequest, null); assertEquals( Map.of("journey", expectedJourney), response.value(), From 9771125c30edc90454c5fc43316ad6fc213ec07a Mon Sep 17 00:00:00 2001 From: Joe Edwards Date: Fri, 7 Jun 2024 16:36:16 +0100 Subject: [PATCH 2/3] PYIC-6711: Use new mechanism for mitigation start events --- .../ProcessJourneyEventHandler.java | 20 +----- .../stepresponses/CriStepResponse.java | 1 - .../stepresponses/ErrorStepResponse.java | 2 - .../stepresponses/PageStepResponse.java | 2 - .../stepresponses/ProcessStepResponse.java | 2 - .../stepresponses/StepResponse.java | 2 - .../journey-maps/new-p2-identity.yaml | 65 ++++++++++++++++--- .../test/initial-journey-selection.yaml | 11 ---- .../ProcessJourneyEventHandlerTest.java | 26 -------- .../statemachine/states/BasicStateTest.java | 2 +- .../stepresponses/CriStepResponseTest.java | 2 +- .../stepresponses/ErrorStepResponseTest.java | 3 +- .../stepresponses/PageStepResponseTest.java | 2 +- .../ProcessStepResponseTest.java | 5 +- 14 files changed, 62 insertions(+), 83 deletions(-) diff --git a/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/ProcessJourneyEventHandler.java b/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/ProcessJourneyEventHandler.java index 989176ae8e..5c5b7e7acd 100644 --- a/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/ProcessJourneyEventHandler.java +++ b/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/ProcessJourneyEventHandler.java @@ -70,7 +70,7 @@ public class ProcessJourneyEventHandler private static final String NEXT_EVENT = "next"; private static final String END_SESSION_EVENT = "build-client-oauth-response"; private static final StepResponse END_SESSION_RESPONSE = - new ProcessStepResponse(END_SESSION_EVENT, null, null, null); + new ProcessStepResponse(END_SESSION_EVENT, null); private final IpvSessionService ipvSessionService; private final AuditService auditService; private final ConfigService configService; @@ -164,11 +164,6 @@ public Map handleRequest(JourneyRequest journeyRequest, Context ipvSessionService.updateIpvSession(ipvSessionItem); - if (stepResponse.getMitigationStart() != null) { - sendMitigationStartAuditEvent( - auditEventUser, stepResponse.getMitigationStart(), deviceInformation); - } - return stepResponse.value(); } catch (HttpResponseExceptionWithErrorBody e) { return StepFunctionHelpers.generateErrorOutputMap( @@ -371,19 +366,6 @@ private Map loadStateMachines( return stateMachinesMap; } - private void sendMitigationStartAuditEvent( - AuditEventUser auditEventUser, String mitigationType, String deviceInformation) - throws SqsException { - - auditService.sendAuditEvent( - new AuditEvent( - AuditEventTypes.IPV_MITIGATION_START, - configService.getSsmParameter(ConfigurationVariable.COMPONENT_ID), - auditEventUser, - new AuditExtensionMitigationType(mitigationType), - new AuditRestrictedDeviceInformation(deviceInformation))); - } - private void sendJourneyAuditEvent( AuditEventTypes auditEventType, Map auditContext, diff --git a/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/CriStepResponse.java b/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/CriStepResponse.java index 93e7815c5d..42b1a0b7e3 100644 --- a/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/CriStepResponse.java +++ b/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/CriStepResponse.java @@ -33,7 +33,6 @@ public class CriStepResponse implements StepResponse { private String criId; private String context; private EvidenceRequest evidenceRequest; - private String mitigationStart; public Map value() { try { diff --git a/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/ErrorStepResponse.java b/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/ErrorStepResponse.java index 074df57891..6b155f7d5d 100644 --- a/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/ErrorStepResponse.java +++ b/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/ErrorStepResponse.java @@ -13,8 +13,6 @@ public class ErrorStepResponse implements StepResponse { private static final String ERROR = "error"; private String pageId; private String statusCode; - private String mitigationStart; - private String auditEvent; public Map value() { return Map.of("type", ERROR, "page", pageId, "statusCode", Integer.parseInt(statusCode)); diff --git a/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/PageStepResponse.java b/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/PageStepResponse.java index e4ba85dd67..d27d1c6d60 100644 --- a/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/PageStepResponse.java +++ b/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/PageStepResponse.java @@ -14,8 +14,6 @@ public class PageStepResponse implements StepResponse { private String pageId; private String context; - private String mitigationStart; - private String auditEvent; public Map value() { Map response = new HashMap<>(); diff --git a/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/ProcessStepResponse.java b/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/ProcessStepResponse.java index c10f87d646..58c3559408 100644 --- a/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/ProcessStepResponse.java +++ b/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/ProcessStepResponse.java @@ -16,8 +16,6 @@ public class ProcessStepResponse implements StepResponse { private static final String JOURNEY_TEMPLATE = "/journey/%s"; private String lambda; private Map lambdaInput; - private String mitigationStart; - private String auditEvent; @Override public Map value() { diff --git a/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/StepResponse.java b/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/StepResponse.java index 53a7b288a4..054aa30558 100644 --- a/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/StepResponse.java +++ b/lambdas/process-journey-event/src/main/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/StepResponse.java @@ -14,6 +14,4 @@ }) public interface StepResponse { Map value(); - - String getMitigationStart(); } diff --git a/lambdas/process-journey-event/src/main/resources/statemachine/journey-maps/new-p2-identity.yaml b/lambdas/process-journey-event/src/main/resources/statemachine/journey-maps/new-p2-identity.yaml index eff6433905..898b261611 100644 --- a/lambdas/process-journey-event/src/main/resources/statemachine/journey-maps/new-p2-identity.yaml +++ b/lambdas/process-journey-event/src/main/resources/statemachine/journey-maps/new-p2-identity.yaml @@ -15,6 +15,10 @@ states: events: next: targetState: MITIGATION_01_IDENTITY_START_PAGE + auditEvents: + - IPV_MITIGATION_START + auditContext: + mitigationType: enhanced-verification ALTERNATE_DOC_PASSPORT: events: @@ -517,9 +521,17 @@ states: targetState: EVALUATE_GPG45_SCORES enhanced-verification: targetState: MITIGATION_02_OPTIONS_WITH_F2F + auditEvents: + - IPV_MITIGATION_START + auditContext: + mitigationType: enhanced-verification checkIfDisabled: f2f: targetState: MITIGATION_02_OPTIONS + auditEvents: + - IPV_MITIGATION_START + auditContext: + mitigationType: enhanced-verification # DCMAW journey (J1) POST_DCMAW_SUCCESS_PAGE: @@ -555,6 +567,10 @@ states: targetState: MULTIPLE_DOC_CHECK_PAGE alternate-doc-invalid-passport: targetState: MITIGATION_05_OPTIONS + auditEvents: + - IPV_MITIGATION_START + auditContext: + mitigationType: invalid-passport PROVE_ANOTHER_WAY_J2: response: @@ -600,6 +616,10 @@ states: targetState: MULTIPLE_DOC_CHECK_PAGE alternate-doc-invalid-dl: targetState: MITIGATION_03_OPTIONS + auditEvents: + - IPV_MITIGATION_START + auditContext: + mitigationType: invalid-dl PROVE_ANOTHER_WAY_J3: response: @@ -690,9 +710,17 @@ states: targetState: EVALUATE_GPG45_SCORES enhanced-verification: targetState: MITIGATION_02_OPTIONS_WITH_F2F + auditEvents: + - IPV_MITIGATION_START + auditContext: + mitigationType: enhanced-verification checkIfDisabled: f2f: targetState: MITIGATION_02_OPTIONS + auditEvents: + - IPV_MITIGATION_START + auditContext: + mitigationType: enhanced-verification CRI_HMRC_KBV_M2B: response: @@ -706,9 +734,17 @@ states: targetState: EVALUATE_GPG45_SCORES enhanced-verification: targetState: MITIGATION_02_OPTIONS_WITH_F2F_M2B + auditEvents: + - IPV_MITIGATION_START + auditContext: + mitigationType: enhanced-verification checkIfDisabled: f2f: targetState: MITIGATION_02_OPTIONS + auditEvents: + - IPV_MITIGATION_START + auditContext: + mitigationType: enhanced-verification # No photo id journey (M2B) CRI_CLAIMED_IDENTITY_M2B: @@ -794,10 +830,17 @@ states: checkIfDisabled: f2f: targetState: MITIGATION_02_OPTIONS + auditEvents: + - IPV_MITIGATION_START + auditContext: + mitigationType: enhanced-verification + auditEvents: + - IPV_MITIGATION_START + auditContext: + mitigationType: enhanced-verification MITIGATION_KBV_FAIL_M2B: response: - mitigationStart: enhanced-verification type: page pageId: no-photo-id-security-questions-find-another-way events: @@ -927,7 +970,6 @@ states: response: type: page pageId: page-ipv-identity-document-start - mitigationStart: enhanced-verification events: appTriage: targetState: MITIGATION_01_CRI_DCMAW @@ -1054,7 +1096,6 @@ states: response: type: page pageId: pyi-suggest-other-options-no-f2f - mitigationStart: enhanced-verification events: appTriage: targetState: CRI_DCMAW_PYI_ESCAPE @@ -1152,7 +1193,6 @@ states: response: type: page pageId: pyi-suggest-other-options - mitigationStart: enhanced-verification events: f2f: targetState: CRI_F2F @@ -1201,7 +1241,6 @@ states: type: page pageId: pyi-suggest-other-options context: no-photo-id - mitigationStart: enhanced-verification events: f2f: targetState: CRI_F2F @@ -1246,7 +1285,6 @@ states: response: type: page pageId: pyi-driving-licence-no-match-another-way - mitigationStart: invalid-dl events: next: targetState: MITIGATION_PP_CRI_UK_PASSPORT @@ -1262,12 +1300,15 @@ states: events: next: targetState: MITIGATION_04_IDENTITY_START_PAGE + auditEvents: + - IPV_MITIGATION_START + auditContext: + mitigationType: invalid-dl MITIGATION_04_IDENTITY_START_PAGE: response: type: page pageId: pyi-continue-with-passport - mitigationStart: invalid-dl events: next: targetState: MITIGATION_PP_CRI_UK_PASSPORT @@ -1317,7 +1358,6 @@ states: response: type: page pageId: pyi-passport-no-match-another-way - mitigationStart: invalid-passport events: next: targetState: MITIGATION_DL_CRI_DRIVING_LICENCE @@ -1333,12 +1373,15 @@ states: events: next: targetState: MITIGATION_06_IDENTITY_START_PAGE + auditEvents: + - IPV_MITIGATION_START + auditContext: + mitigationType: invalid-passport MITIGATION_06_IDENTITY_START_PAGE: response: type: page pageId: pyi-continue-with-driving-licence - mitigationStart: invalid-passport events: next: targetState: MITIGATION_DL_CRI_DRIVING_LICENCE @@ -1451,5 +1494,9 @@ states: events: next: targetState: MITIGATION_01_IDENTITY_START_PAGE + auditEvents: + - IPV_MITIGATION_START + auditContext: + mitigationType: enhanced-verification end: targetState: RETURN_TO_RP diff --git a/lambdas/process-journey-event/src/main/resources/statemachine/test/initial-journey-selection.yaml b/lambdas/process-journey-event/src/main/resources/statemachine/test/initial-journey-selection.yaml index 88c4aa4799..90f5b44277 100644 --- a/lambdas/process-journey-event/src/main/resources/statemachine/test/initial-journey-selection.yaml +++ b/lambdas/process-journey-event/src/main/resources/statemachine/test/initial-journey-selection.yaml @@ -33,8 +33,6 @@ states: targetState: CRI_STATE_WITH_EVIDENCE_REQUEST testWithContextAndEvidenceRequest: targetState: CRI_STATE_WITH_CONTEXT_AND_EVIDENCE_REQUEST - testWithMitigationStart: - targetState: PAGE_STATE_AT_START_OF_MITIGATION testWithAuditEvent: targetState: ERROR_STATE auditEvents: @@ -82,15 +80,6 @@ states: enterNestedJourneyAtStateOne: targetState: NESTED_JOURNEY_INVOKE_STATE - PAGE_STATE_AT_START_OF_MITIGATION: - response: - type: page - pageId: page-id-for-some-page - mitigationStart: a-mitigation-type - events: - enterNestedJourneyAtStateOne: - targetState: NESTED_JOURNEY_INVOKE_STATE - PAGE_STATE_AT_START_OF_NO_PHOTO_ID: response: type: page diff --git a/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/ProcessJourneyEventHandlerTest.java b/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/ProcessJourneyEventHandlerTest.java index 4614efc560..1708fa63df 100644 --- a/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/ProcessJourneyEventHandlerTest.java +++ b/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/ProcessJourneyEventHandlerTest.java @@ -465,32 +465,6 @@ void shouldFollowJourneyChanges() throws Exception { assertEquals("testjourneyid", capturedAuditEvent.getUser().getGovukSigninJourneyId()); } - @Test - void shouldSendAuditEventForMitigationStart() throws Exception { - var input = - JourneyRequest.builder() - .ipAddress(TEST_IP) - .journey("testWithMitigationStart") - .ipvSessionId(TEST_IP) - .build(); - mockIpvSessionItemAndTimeout("CRI_STATE"); - - getProcessJourneyStepHandler(StateMachineInitializerMode.TEST) - .handleRequest(input, mockContext); - - verify(mockAuditService).sendAuditEvent(auditEventCaptor.capture()); - AuditEvent capturedAuditEvent = auditEventCaptor.getValue(); - - assertEquals(AuditEventTypes.IPV_MITIGATION_START, capturedAuditEvent.getEventName()); - assertEquals("core", capturedAuditEvent.getComponentId()); - assertEquals("testuserid", capturedAuditEvent.getUser().getUserId()); - assertEquals("testjourneyid", capturedAuditEvent.getUser().getGovukSigninJourneyId()); - assertEquals( - "a-mitigation-type", - ((AuditExtensionMitigationType) capturedAuditEvent.getExtensions()) - .mitigationType()); - } - @Test void shouldSendAuditEventWhenThereIsAuditEventInJourneyMap() throws Exception { var input = diff --git a/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/states/BasicStateTest.java b/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/states/BasicStateTest.java index d881ede0c7..5fa334253d 100644 --- a/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/states/BasicStateTest.java +++ b/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/states/BasicStateTest.java @@ -24,7 +24,7 @@ class BasicStateTest { @Test void transitionShouldReturnAStateWithAResponse() throws Exception { BasicState targetState = new BasicState(); - PageStepResponse stepResponse = new PageStepResponse("stepId", "context", null, null); + PageStepResponse stepResponse = new PageStepResponse("stepId", "context"); targetState.setResponse(stepResponse); BasicState currentState = new BasicState(); diff --git a/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/CriStepResponseTest.java b/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/CriStepResponseTest.java index 5376728982..b38f00f9d0 100644 --- a/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/CriStepResponseTest.java +++ b/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/CriStepResponseTest.java @@ -16,7 +16,7 @@ class CriStepResponseTest { @MethodSource("journeyUriParameters") void valueReturnsExpectedJourneyResponse( String criId, String context, EvidenceRequest evidenceRequest, String expectedJourney) { - CriStepResponse response = new CriStepResponse(criId, context, evidenceRequest, null); + CriStepResponse response = new CriStepResponse(criId, context, evidenceRequest); assertEquals( Map.of("journey", expectedJourney), response.value(), diff --git a/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/ErrorStepResponseTest.java b/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/ErrorStepResponseTest.java index d7ee9a0014..f77fc2dae3 100644 --- a/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/ErrorStepResponseTest.java +++ b/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/ErrorStepResponseTest.java @@ -8,8 +8,7 @@ public class ErrorStepResponseTest { - public static final ErrorStepResponse ERROR_RESPONSE = - new ErrorStepResponse("aPageId", "500", null, null); + public static final ErrorStepResponse ERROR_RESPONSE = new ErrorStepResponse("aPageId", "500"); @Test void valueReturnsCorrectResponse() { diff --git a/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/PageStepResponseTest.java b/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/PageStepResponseTest.java index 77c78b0811..ae56991bb1 100644 --- a/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/PageStepResponseTest.java +++ b/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/PageStepResponseTest.java @@ -9,7 +9,7 @@ public class PageStepResponseTest { public static final PageStepResponse PAGE_RESPONSE = - new PageStepResponse("aPageId", "testContext", "mitigationType", "startNoPhotoId"); + new PageStepResponse("aPageId", "testContext"); @Test void valueReturnsCorrectPageResponse() { diff --git a/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/ProcessStepResponseTest.java b/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/ProcessStepResponseTest.java index a32f4271ef..c73b250fab 100644 --- a/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/ProcessStepResponseTest.java +++ b/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/statemachine/stepresponses/ProcessStepResponseTest.java @@ -12,10 +12,7 @@ class ProcessStepResponseTest { void valueReturnsCorrectJourneyResponse() { ProcessStepResponse processStepResponse = new ProcessStepResponse( - "a-process-lambda", - Map.of("input1", "Windom Earle", "input2", 315), - "mitigationType", - null); + "a-process-lambda", Map.of("input1", "Windom Earle", "input2", 315)); Map expectedValue = Map.of( From 779eab4c0489cfe4ff603a9f28d3a2fd9f0b2a21 Mon Sep 17 00:00:00 2001 From: Joe Edwards Date: Mon, 10 Jun 2024 10:17:49 +0100 Subject: [PATCH 3/3] PYIC-6711: fix expect args --- .../processjourneyevent/ProcessJourneyEventHandlerTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/ProcessJourneyEventHandlerTest.java b/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/ProcessJourneyEventHandlerTest.java index 1708fa63df..df30c83a1b 100644 --- a/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/ProcessJourneyEventHandlerTest.java +++ b/lambdas/process-journey-event/src/test/java/uk/gov/di/ipv/core/processjourneyevent/ProcessJourneyEventHandlerTest.java @@ -503,7 +503,7 @@ void shouldSendMultipleAuditEventsWithContext() throws Exception { verify(mockAuditService, times(2)).sendAuditEvent(auditEventCaptor.capture()); var capturedAuditEvents = auditEventCaptor.getAllValues(); - assertEquals(capturedAuditEvents.size(), 2); + assertEquals(2, capturedAuditEvents.size()); var firstEvent = capturedAuditEvents.get(0); assertEquals(AuditEventTypes.IPV_NO_PHOTO_ID_JOURNEY_START, firstEvent.getEventName());