PolicyServiceImpl implements PolicyService { @Override @Transactional(readOnly = true) public PolicyContainerDto getPolicies(String policyId, String wildcard) { assertPolicyId(policyId); if (wildcard == null) { final Policy policy = policyRepository.findOne(policyId); assertPolicyNotNull(policy, policyId); final PolicyDto policyDto = modelMapper .map(policy, PolicyDto.class); return new PolicyContainerDto(policyDto); } else { wildcard = "".equals(wildcard) ? DEFAULT_WILDCARD : wildcard; final String policyIdLike = policyId.replace(wildcard, LIKE); final List<Policy> policies = policyRepository .findAllByIdLike(policyIdLike); assertPoliciesNotEmpty(policies, policyId, wildcard); final List<PolicyDto> policyDtos = policies.stream() .map(p -> modelMapper.map(p, PolicyDto.class)) .collect(toList()); return new PolicyContainerDto(policyDtos); } }  @Override @Transactional PolicyMetadataContainerDto addPolicies(
			PolicyContentContainerDto policyContentContainerDto, boolean force); @Override @Transactional void deletePolicy(String policyId); @Override @Transactional(readOnly = true) PolicyMetadataContainerDto getAllPolicyMetadata(); @Override @Transactional(readOnly = true) PolicyContainerDto getPolicies(String policyId, String wildcard); @Override @Transactional(readOnly = true) PolicyDto getPoliciesCombinedAsPolicySet(String policyId,
			String wildcard, String policySetId, String policyCombiningAlgId); @Override Map<String, String> getPolicyCombiningAlgIds(); @Override @Transactional PolicyMetadataDto updatePolicy(
			PolicyContentDto updatePolicyRequestDto, String policyId);  }
@Test public void testGetPolicies_No_Wildcard() { final String policyId = "policyId"; final String wildcard = null; final Policy policy = mock(Policy.class); final PolicyDto getPolicyResponseDto = mock(PolicyDto.class); when(policyRepository.findOne(policyId)).thenReturn(policy); when(modelMapper.map(policy, PolicyDto.class)).thenReturn( getPolicyResponseDto); final PolicyContainerDto response = sut.getPolicies(policyId, wildcard); assertEquals(getPolicyResponseDto, response.getPolicies().get(0)); }
@Test public void testGetPolicies_No_Wildcard_Throws_PolicyNotFoundException() { final String policyId = "policyId"; final String wildcard = null; thrown.expect(PolicyNotFoundException.class); when(policyRepository.findOne(policyId)).thenReturn(null); sut.getPolicies(policyId, wildcard); }
@Test public void testGetPolicies_Throws_PolicyIdNotFoundException() { final String policyId = ""; final String wildcard = null; thrown.expect(PolicyIdNotFoundException.class); sut.getPolicies(policyId, wildcard); }
@Test public void testGetPolicies_Wildcard_Used() { final String policyId = "policy*"; final String wildcard = "*"; final String policyIdLike = "policy%"; final Policy policy1 = mock(Policy.class); final Policy policy2 = mock(Policy.class); final List<Policy> policyList = Arrays.asList(policy1, policy2); final PolicyDto getPolicyResponseDto1 = mock(PolicyDto.class); final PolicyDto getPolicyResponseDto2 = mock(PolicyDto.class); when(policyRepository.findAllByIdLike(policyIdLike)).thenReturn( policyList); when(modelMapper.map(policy1, PolicyDto.class)).thenReturn( getPolicyResponseDto1); when(modelMapper.map(policy2, PolicyDto.class)).thenReturn( getPolicyResponseDto2); final PolicyContainerDto response = sut.getPolicies(policyId, wildcard); assertTrue(response.getPolicies().contains(getPolicyResponseDto1)); assertTrue(response.getPolicies().contains(getPolicyResponseDto2)); }
@Test public void testGetPolicies_Wildcard_Used_Throws_PolicyNotFoundException() { final String policyId = "policy*"; final String wildcard = "*"; final String policyIdLike = "policy%"; thrown.expect(PolicyNotFoundException.class); final List<Policy> policyList = Collections.emptyList(); when(policyRepository.findAllByIdLike(policyIdLike)).thenReturn( policyList); sut.getPolicies(policyId, wildcard); }