PolicyIdServiceImpl implements PolicyIdService { @Override public String generatePolicyId(ConsentDto consentDto, String mrn) { final short iterationLimit = 3; return UniqueValueGenerator .generateUniqueValue(() -> generateRandomPolicyId(consentDto, mrn), generatedValue -> consentRepository.findAllByConsentReferenceId(generatedValue) .size() == 0, iterationLimit); } PolicyIdServiceImpl(String pidDomainId, String pidDomainType,
			ConsentRepository consentRepository); @Override String generatePolicyId(ConsentDto consentDto, String mrn);  }
@Test public void testGeneratePolicyId() { when(consentRepository.findAllByConsentReferenceId(anyString())) .thenReturn(new ArrayList<Consent>(1)); ConsentDto consentDto = new ConsentDto(); Set<String> organizationalProvidersDisclosureIsMadeTo = new HashSet<String>(); organizationalProvidersDisclosureIsMadeTo.add("111"); Set<String> providersPermittedToDisclose = new HashSet<String>(); providersPermittedToDisclose.add("222"); consentDto .setOrganizationalProvidersDisclosureIsMadeTo(organizationalProvidersDisclosureIsMadeTo); consentDto .setProvidersPermittedToDisclose(providersPermittedToDisclose); String policyId = sut.generatePolicyId(consentDto, MRNMOCK); assertTrue(StringUtils.hasText(policyId)); assertTrue(policyId.startsWith(MRNMOCK)); assertTrue(policyId.contains(PIDDOMAINIDMOCK)); assertTrue(policyId.contains(PIDDOMAINTYPEMOCK)); }
@Test public void testGeneratePolicyId_Throws_UniqueValueGeneratorException() { when(consentRepository.findAllByConsentReferenceId(anyString())) .thenReturn(Arrays.asList(new Consent())); ConsentDto consentDto = new ConsentDto(); Set<String> organizationalProvidersDisclosureIsMadeTo = new HashSet<String>(); organizationalProvidersDisclosureIsMadeTo.add("111"); Set<String> providersPermittedToDisclose = new HashSet<String>(); providersPermittedToDisclose.add("222"); consentDto .setOrganizationalProvidersDisclosureIsMadeTo(organizationalProvidersDisclosureIsMadeTo); consentDto .setProvidersPermittedToDisclose(providersPermittedToDisclose); thrown.expect(UniqueValueGeneratorException.class); String policyId = sut.generatePolicyId(consentDto, MRNMOCK); assertTrue(StringUtils.hasText(policyId)); assertTrue(policyId.startsWith(MRNMOCK)); assertTrue(policyId.contains(PIDDOMAINIDMOCK)); assertTrue(policyId.contains(PIDDOMAINTYPEMOCK)); }