### Question:
SamlMessageSenderHandler { public SamlMessage generateAuthnResponseFromHub(SessionId sessionId, String principalIpAddressAsSeenByHub) { AuthnResponseFromHubContainerDto authnResponseFromHub = sessionProxy.getAuthnResponseFromHub(sessionId); Response samlResponse = responseTransformer.apply(authnResponseFromHub.getSamlResponse()); validateAndLogSamlResponseSignature(samlResponse); SamlMessage samlMessage = new SamlMessage( authnResponseFromHub.getSamlResponse(), SamlMessageType.SAML_RESPONSE, authnResponseFromHub.getRelayState(), authnResponseFromHub.getPostEndpoint().toString(), Optional.empty() ); externalCommunicationEventLogger.logResponseFromHub(samlResponse.getID(), sessionId, authnResponseFromHub.getPostEndpoint(), principalIpAddressAsSeenByHub); return samlMessage; } @Inject  SamlMessageSenderHandler(
            StringToOpenSamlObjectTransformer<Response> responseTransformer,
            StringToOpenSamlObjectTransformer<AuthnRequest> authnRequestTransformer,
            SamlMessageSignatureValidator samlMessageSignatureValidator,
            ExternalCommunicationEventLogger externalCommunicationEventLogger,
            ProtectiveMonitoringLogger protectiveMonitoringLogger,
            SessionProxy sessionProxy); SamlMessage generateAuthnResponseFromHub(SessionId sessionId, String principalIpAddressAsSeenByHub); SamlMessage generateErrorResponseFromHub(final SessionId sessionId, String principalIpAddressAsSeenByHub); SamlMessage generateAuthnRequestFromHub(SessionId sessionId, String principalIpAddress);  }### Answer:
@Test public void generateAuthnResponseFromHub_shouldAddExternalCommunicationEvent() throws Exception { SessionId sessionId = SessionId.createNewSessionId(); String expectedSamlMessageId = UUID.randomUUID().toString(); Response openSamlResponse = setUpAuthnResponseFromHub(sessionId, expectedSamlMessageId); SamlMessage authnResponse = samlMessageSenderHandler.generateAuthnResponseFromHub(sessionId, principalIpAddressAsSeenByHub); assertThat(authnResponse.getSamlMessage()).isEqualTo(samlRequest); assertThat(authnResponse.getPostEndpoint()).isEqualTo(postEndPoint.toString()); assertThat(authnResponse.getRegistration()).isNotPresent(); assertThat(authnResponse.getRelayState().isPresent()).isTrue(); assertThat(authnResponse.getRelayState().get()).isEqualTo(relayState.get()); assertThat(authnResponse.getSamlMessageType()).isEqualTo(SamlMessageType.SAML_RESPONSE); verify(externalCommunicationEventLogger).logResponseFromHub(expectedSamlMessageId, sessionId, postEndPoint, principalIpAddressAsSeenByHub); verify(protectiveMonitoringLogger).logAuthnResponse(openSamlResponse, Direction.OUTBOUND, SignatureStatus.VALID_SIGNATURE); }
@Test(expected = SamlTransformationErrorException.class) public void generateAuthResponseFromHub_shouldThrowSamlTransformationException() throws MarshallingException, SignatureException { SessionId sessionId = SessionId.createNewSessionId(); String expectedSamlMessageId = UUID.randomUUID().toString(); Response openSamlResponse = setUpAuthnResponseFromHub(sessionId, expectedSamlMessageId); when(samlMessageSignatureValidator.validate(openSamlResponse, SPSSODescriptor.DEFAULT_ELEMENT_NAME)).thenReturn(SamlValidationResponse.anInvalidResponse(new SamlValidationSpecification("bad", true))); samlMessageSenderHandler.generateAuthnResponseFromHub(sessionId, principalIpAddressAsSeenByHub); }