### Question:
CmsConsentAuthorisationServiceInternal { public Optional<AuthorisationEntity> getAuthorisationByAuthorisationId(@NotNull String authorisationId, @NotNull String instanceId) throws AuthorisationIsExpiredException { Optional<AuthorisationEntity> authorisation = authorisationRepository.findOne(authorisationSpecification.byExternalIdAndInstanceId(authorisationId, instanceId)); if (authorisation.isPresent() && !authorisation.get().isAuthorisationNotExpired()) { log.info("Authorisation ID [{}], Instance ID: [{}]. Authorisation is expired", authorisationId, instanceId); throw new AuthorisationIsExpiredException(authorisation.get().getTppNokRedirectUri()); } return authorisation; }  Optional<AuthorisationEntity> getAuthorisationByAuthorisationId(@NotNull String authorisationId, @NotNull String instanceId); Optional<AuthorisationEntity> getAuthorisationByRedirectId(String redirectId, String instanceId); boolean updateScaStatusAndAuthenticationData(@NotNull ScaStatus status, AuthorisationEntity authorisation, AuthenticationDataHolder authenticationDataHolder); List<AuthorisationEntity> getAuthorisationsByParentExternalId(String externalId);  }### Answer:
@Test void getAuthorisationByExternalId_success() throws AuthorisationIsExpiredException { authorisationEntity.setAuthorisationExpirationTimestamp(OffsetDateTime.now().plusDays(1)); when(authorisationSpecification.byExternalIdAndInstanceId(AUTHORISATION_ID, INSTANCE_ID)) .thenReturn((root, criteriaQuery, criteriaBuilder) -> null); when(authorisationRepository.findOne(any(Specification.class))) .thenReturn(Optional.of(authorisationEntity)); Optional<AuthorisationEntity> actual = cmsConsentAuthorisationServiceInternal.getAuthorisationByAuthorisationId(AUTHORISATION_ID, INSTANCE_ID); assertTrue(actual.isPresent()); assertEquals(authorisationEntity, actual.get()); }
@Test void getAuthorisationByExternalId_notFound_empty() throws AuthorisationIsExpiredException { when(authorisationSpecification.byExternalIdAndInstanceId(AUTHORISATION_ID, INSTANCE_ID)) .thenReturn((root, criteriaQuery, criteriaBuilder) -> null); when(authorisationRepository.findOne(any(Specification.class))).thenReturn(Optional.empty()); Optional<AuthorisationEntity> actual = cmsConsentAuthorisationServiceInternal.getAuthorisationByAuthorisationId(AUTHORISATION_ID, INSTANCE_ID); assertTrue(actual.isEmpty()); }
@Test void getAuthorisationByExternalId_expired_error() { authorisationEntity.setAuthorisationExpirationTimestamp(OffsetDateTime.now().minusDays(1)); when(authorisationSpecification.byExternalIdAndInstanceId(AUTHORISATION_ID, INSTANCE_ID)) .thenReturn((root, criteriaQuery, criteriaBuilder) -> null); when(authorisationRepository.findOne(any(Specification.class))) .thenReturn(Optional.of(authorisationEntity)); assertThrows(AuthorisationIsExpiredException.class, () -> cmsConsentAuthorisationServiceInternal.getAuthorisationByAuthorisationId(AUTHORISATION_ID, INSTANCE_ID)); }