### Question:
AisConsentLazyMigrationService { public ConsentEntity migrateIfNeeded(ConsentEntity consentEntity) { if (consentEntity.getData() == null) { Optional<AisConsent> obsoleteAisConsentOptional = obsoleteAisConsentJpaRepository.findByExternalId(consentEntity.getExternalId()); if (obsoleteAisConsentOptional.isPresent()) { byte[] consentData = getConsentData(obsoleteAisConsentOptional.get()); consentEntity.setData(consentData); consentJpaRepository.save(consentEntity); } } return consentEntity; }  ConsentEntity migrateIfNeeded(ConsentEntity consentEntity);  }### Answer:
@Test void migrateIfNeeded() { ConsentEntity consentEntity = new ConsentEntity(); consentEntity.setExternalId(EXTERNAL_ID); AisConsent obsoleteAisConsent = jsonReader.getObjectFromFile("json/service/migration/ais-consent.json", AisConsent.class); when(obsoleteAisConsentJpaRepository.findByExternalId(EXTERNAL_ID)) .thenReturn(Optional.of(obsoleteAisConsent)); aisConsentLazyMigrationService.migrateIfNeeded(consentEntity); verify(obsoleteAisConsentJpaRepository, times(1)).findByExternalId(EXTERNAL_ID); verify(consentJpaRepository, times(1)).save(consentEntity); assertNotNull(consentEntity.getData()); AisConsentData actual = consentDataMapper.mapToAisConsentData(consentEntity.getData()); assertEquals(AccountAccessType.ALL_ACCOUNTS, actual.getAllPsd2()); assertEquals(AccountAccessType.ALL_ACCOUNTS, actual.getAvailableAccounts()); assertEquals(AccountAccessType.ALL_ACCOUNTS_WITH_OWNER_NAME, actual.getAvailableAccountsWithBalance()); assertTrue(actual.isCombinedServiceIndicator()); }
@Test void migrateIfNeeded_consentDataExists() { ConsentEntity consentEntity = new ConsentEntity(); consentEntity.setData("data".getBytes()); aisConsentLazyMigrationService.migrateIfNeeded(consentEntity); verify(obsoleteAisConsentJpaRepository, never()).findByExternalId(any()); verify(consentJpaRepository, never()).save(any()); }