### Question:
PiisConsentLazyMigrationService { public ConsentEntity migrateIfNeeded(ConsentEntity consentEntity) { if (consentEntity.getData() == null) { Optional<PiisConsentEntity> obsoletePiisConsentOptional = obsoletePiisConsentJpaRepository.findByExternalId(consentEntity.getExternalId()); if (obsoletePiisConsentOptional.isPresent()) { byte[] consentData = getConsentData(obsoletePiisConsentOptional.get()); consentEntity.setData(consentData); consentJpaRepository.save(consentEntity); } } return consentEntity; }  ConsentEntity migrateIfNeeded(ConsentEntity consentEntity); List<ConsentEntity> migrateIfNeeded(List<ConsentEntity> consentEntities);  }### Answer:
@Test void migrateIfNeeded() { ConsentEntity consentEntity = new ConsentEntity(); consentEntity.setExternalId(EXTERNAL_ID); PiisConsentEntity obsoletePiisConsent = jsonReader.getObjectFromFile("json/service/migration/piis-consent-entity.json", PiisConsentEntity.class); when(obsoletePiisConsentJpaRepository.findByExternalId(EXTERNAL_ID)) .thenReturn(Optional.of(obsoletePiisConsent)); piisConsentLazyMigrationService.migrateIfNeeded(consentEntity); verify(obsoletePiisConsentJpaRepository, times(1)).findByExternalId(EXTERNAL_ID); verify(consentJpaRepository, times(1)).save(consentEntity); assertNotNull(consentEntity.getData()); PiisConsentData actual = consentDataMapper.mapToPiisConsentData(consentEntity.getData()); PiisConsentData expected = jsonReader.getObjectFromFile("json/service/migration/piis-consent.json", PiisConsentData.class); assertEquals(expected, actual); }
@Test void migrateIfNeeded_consentDataExists() { ConsentEntity consentEntity = new ConsentEntity(); consentEntity.setData("data".getBytes()); piisConsentLazyMigrationService.migrateIfNeeded(consentEntity); verify(obsoletePiisConsentJpaRepository, never()).findByExternalId(any()); verify(consentJpaRepository, never()).save(any()); }