### Question:
CmsPsuPiisServiceInternal implements CmsPsuPiisService { @Override @Transactional public boolean revokeConsent(@NotNull PsuIdData psuIdData, @NotNull String consentId, @NotNull String instanceId) { Optional<ConsentEntity> piisConsentEntity = consentJpaRepository.findOne(piisConsentEntitySpecification.byConsentIdAndInstanceId(consentId, instanceId)) .filter(con -> isPsuIdDataContentEquals(con, psuIdData) && !con.getConsentStatus().isFinalisedStatus()); if (piisConsentEntity.isPresent()) { revokeConsent(piisConsentEntity.get()); return true; } log.info("Consent ID [{}], Instance ID: [{}]. Revoke consent failed, because given psuData is not equals stored psuData, or consent status is finalised, or consent not found", consentId, instanceId); return false; }  @Override @NotNull Optional<CmsPiisConsent> getConsent(@NotNull PsuIdData psuIdData, @NotNull String consentId, @NotNull String instanceId); @Override @NotNull List<CmsPiisConsent> getConsentsForPsu(@NotNull PsuIdData psuIdData, @NotNull String instanceId); @Override @Transactional boolean revokeConsent(@NotNull PsuIdData psuIdData, @NotNull String consentId, @NotNull String instanceId);  }### Answer:
@Test void revokeConsent() { when(piisConsentEntitySpecification.byConsentIdAndInstanceId(EXTERNAL_CONSENT_ID, DEFAULT_SERVICE_INSTANCE_ID)) .thenReturn((root, criteriaQuery, criteriaBuilder) -> null); ConsentEntity consentEntity = buildPiisConsentEntity(); when(consentJpaRepository.findOne(any(Specification.class))).thenReturn(Optional.of(consentEntity)); when(psuDataMapper.mapToPsuIdData(psuData)).thenReturn(psuIdData); boolean revokeConsent = cmsPsuPiisServiceInternal.revokeConsent(psuIdData, EXTERNAL_CONSENT_ID, DEFAULT_SERVICE_INSTANCE_ID); assertTrue(revokeConsent); verify(piisConsentEntitySpecification).byConsentIdAndInstanceId(EXTERNAL_CONSENT_ID, DEFAULT_SERVICE_INSTANCE_ID); }
@Test void revokeConsent_fail_wrongPsu() { when(piisConsentEntitySpecification.byConsentIdAndInstanceId(EXTERNAL_CONSENT_ID, DEFAULT_SERVICE_INSTANCE_ID)) .thenReturn((root, criteriaQuery, criteriaBuilder) -> null); ConsentEntity consentEntity = buildPiisConsentEntity(); when(consentJpaRepository.findOne(any(Specification.class))).thenReturn(Optional.of(consentEntity)); when(psuDataMapper.mapToPsuIdData(psuData)).thenReturn(psuIdData); boolean revokeConsent = cmsPsuPiisServiceInternal.revokeConsent(psuIdDataNotExist, EXTERNAL_CONSENT_ID, DEFAULT_SERVICE_INSTANCE_ID); assertFalse(revokeConsent); verify(consentJpaRepository, never()).save(any()); }
@Test void revokeConsent_fail_statusFinalised() { when(piisConsentEntitySpecification.byConsentIdAndInstanceId(EXTERNAL_CONSENT_ID_FINALISED, DEFAULT_SERVICE_INSTANCE_ID)) .thenReturn((root, criteriaQuery, criteriaBuilder) -> null); ConsentEntity consentEntityWithFinalisedStatus = buildFinalisedPiisConsentEntity(); when(consentJpaRepository.findOne(any(Specification.class))).thenReturn(Optional.of(consentEntityWithFinalisedStatus)); when(psuDataMapper.mapToPsuIdData(psuData)).thenReturn(psuIdData); boolean revokeConsent = cmsPsuPiisServiceInternal.revokeConsent(psuIdData, EXTERNAL_CONSENT_ID_FINALISED, DEFAULT_SERVICE_INSTANCE_ID); assertFalse(revokeConsent); verify(consentJpaRepository, never()).save(any()); }