PolicyServiceImpl implements PolicyService { @Override @Transactional public PolicyMetadataContainerDto addPolicies( PolicyContentContainerDto policyContentContainerDto, boolean force) { final List<PolicyDto> requestPolicyDtos = policyContentContainerDto .getPolicies().stream().map(MappingUtils::toPolicyDto) .collect(toList()); assertUniqueIds(requestPolicyDtos); if (force == false) { assertNotExistsInPolicyRepository(requestPolicyDtos, policyRepository::findOne); } final List<Policy> policies = requestPolicyDtos.stream() .map(PolicyValidationUtils::validateAndReturn) .map(policyDto -> modelMapper.map(policyDto, Policy.class)) .collect(toList()); final List<PolicyMetadataDto> responsePolicyDtos = policies .stream() .map(policy -> modelMapper.map(policy, PolicyMetadataDto.class)) .collect(toList()); policyRepository.save(policies); return new PolicyMetadataContainerDto(responsePolicyDtos); }  @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 testAddPolicies_Forced() throws UnsupportedEncodingException { final boolean force = true; final PolicyContentDto addPolicyRequestDto1 = new PolicyContentDto(); addPolicyRequestDto1.setPolicy(POLICY_STRING1.getBytes("UTF-8")); final PolicyContentDto addPolicyRequestDto2 = new PolicyContentDto(); addPolicyRequestDto2.setPolicy(POLICY_STRING2.getBytes("UTF-8")); final PolicyContentContainerDto addPolicyRequestContainerDto = new PolicyContentContainerDto( addPolicyRequestDto1, addPolicyRequestDto2); final Policy policy1 = mock(Policy.class); when(policy1.getId()).thenReturn(POLICY_ID1); final Policy policy2 = mock(Policy.class); when(policy2.getId()).thenReturn(POLICY_ID2); when(modelMapper.map(addPolicyRequestDto1, Policy.class)).thenReturn( policy1); when(modelMapper.map(addPolicyRequestDto2, Policy.class)).thenReturn( policy2); final PolicyMetadataDto addPolicyResponseDto1 = mock(PolicyMetadataDto.class); final PolicyMetadataDto addPolicyResponseDto2 = mock(PolicyMetadataDto.class); when(modelMapper.map(policy1, PolicyMetadataDto.class)).thenReturn( addPolicyResponseDto1); when(modelMapper.map(policy2, PolicyMetadataDto.class)).thenReturn( addPolicyResponseDto2); sut.addPolicies(addPolicyRequestContainerDto, force); verify(policyRepository, times(1)).save(anyListOf(Policy.class)); }
@Test public void testAddPolicies_Forced_One_Already_Exists() throws UnsupportedEncodingException { final boolean force = true; final PolicyContentDto addPolicyRequestDto1 = new PolicyContentDto(); addPolicyRequestDto1.setPolicy(POLICY_STRING1.getBytes("UTF-8")); final PolicyContentDto addPolicyRequestDto2 = new PolicyContentDto(); addPolicyRequestDto2.setPolicy(POLICY_STRING2.getBytes("UTF-8")); final PolicyContentContainerDto addPolicyRequestContainerDto = new PolicyContentContainerDto( addPolicyRequestDto1, addPolicyRequestDto2); final Policy policy1 = mock(Policy.class); when(policy1.getId()).thenReturn(POLICY_ID1); final Policy policy2 = mock(Policy.class); when(policy2.getId()).thenReturn(POLICY_ID2); when(modelMapper.map(addPolicyRequestDto1, Policy.class)).thenReturn( policy1); when(modelMapper.map(addPolicyRequestDto2, Policy.class)).thenReturn( policy2); final PolicyMetadataDto addPolicyResponseDto1 = mock(PolicyMetadataDto.class); final PolicyMetadataDto addPolicyResponseDto2 = mock(PolicyMetadataDto.class); when(modelMapper.map(policy1, PolicyMetadataDto.class)).thenReturn( addPolicyResponseDto1); when(modelMapper.map(policy2, PolicyMetadataDto.class)).thenReturn( addPolicyResponseDto2); when(policyRepository.findOne(POLICY_ID1)).thenReturn(new Policy()); sut.addPolicies(addPolicyRequestContainerDto, force); verify(policyRepository, times(1)).save(anyListOf(Policy.class)); }
@Test public void testAddPolicies_Not_Forced() throws UnsupportedEncodingException { final boolean force = false; final PolicyContentDto addPolicyRequestDto1 = new PolicyContentDto(); addPolicyRequestDto1.setPolicy(POLICY_STRING1.getBytes("UTF-8")); final PolicyContentDto addPolicyRequestDto2 = new PolicyContentDto(); addPolicyRequestDto2.setPolicy(POLICY_STRING2.getBytes("UTF-8")); final PolicyContentContainerDto addPolicyRequestContainerDto = new PolicyContentContainerDto( addPolicyRequestDto1, addPolicyRequestDto2); final Policy policy1 = mock(Policy.class); when(policy1.getId()).thenReturn(POLICY_ID1); final Policy policy2 = mock(Policy.class); when(policy2.getId()).thenReturn(POLICY_ID2); when(modelMapper.map(addPolicyRequestDto1, Policy.class)).thenReturn( policy1); when(modelMapper.map(addPolicyRequestDto2, Policy.class)).thenReturn( policy2); final PolicyMetadataDto addPolicyResponseDto1 = mock(PolicyMetadataDto.class); final PolicyMetadataDto addPolicyResponseDto2 = mock(PolicyMetadataDto.class); when(modelMapper.map(policy1, PolicyMetadataDto.class)).thenReturn( addPolicyResponseDto1); when(modelMapper.map(policy2, PolicyMetadataDto.class)).thenReturn( addPolicyResponseDto2); sut.addPolicies(addPolicyRequestContainerDto, force); verify(policyRepository, times(1)).save(anyListOf(Policy.class)); }
@Test public void testAddPolicies_Not_Forced_Throws_PolicyAlreadyExistsException() throws UnsupportedEncodingException { thrown.expect(PolicyAlreadyExistsException.class); final boolean force = false; final PolicyContentDto addPolicyRequestDto1 = new PolicyContentDto(); addPolicyRequestDto1.setPolicy(POLICY_STRING1.getBytes("UTF-8")); final PolicyContentDto addPolicyRequestDto2 = new PolicyContentDto(); addPolicyRequestDto2.setPolicy(POLICY_STRING2.getBytes("UTF-8")); final PolicyContentContainerDto addPolicyRequestContainerDto = new PolicyContentContainerDto( addPolicyRequestDto1, addPolicyRequestDto2); final Policy policy1 = mock(Policy.class); when(policy1.getId()).thenReturn(POLICY_ID1); final Policy policy2 = mock(Policy.class); when(policy2.getId()).thenReturn(POLICY_ID2); when(policyRepository.findOne(POLICY_ID1)).thenReturn(new Policy()); when(modelMapper.map(addPolicyRequestDto1, Policy.class)).thenReturn( policy1); when(modelMapper.map(addPolicyRequestDto2, Policy.class)).thenReturn( policy2); final PolicyMetadataDto addPolicyResponseDto1 = mock(PolicyMetadataDto.class); final PolicyMetadataDto addPolicyResponseDto2 = mock(PolicyMetadataDto.class); when(modelMapper.map(policy1, PolicyMetadataDto.class)).thenReturn( addPolicyResponseDto1); when(modelMapper.map(policy2, PolicyMetadataDto.class)).thenReturn( addPolicyResponseDto2); sut.addPolicies(addPolicyRequestContainerDto, force); verify(policyRepository, times(1)).save(anyListOf(Policy.class)); }
@Test public void testAddPolicies_Throws_ConflictingRequestException() throws UnsupportedEncodingException { thrown.expect(ConflictingRequestException.class); final PolicyContentDto addPolicyRequestDto = new PolicyContentDto(); addPolicyRequestDto.setPolicy(POLICY_STRING1.getBytes("UTF-8")); final PolicyContentContainerDto addPolicyRequestContainerDto = new PolicyContentContainerDto( addPolicyRequestDto, addPolicyRequestDto); sut.addPolicies(addPolicyRequestContainerDto, false); }