PolicyProvisioner implements ApplicationListener<ContextStartedEvent>, JsonMapper { @Override public void onApplicationEvent(ContextStartedEvent contextStartedEvent) { policyDefinitions() .filter(definition -> !pdpPolicyRepository.findByNameAndLatestRevision(definition.getName(), true).isPresent()) .forEach(definition -> { String policyXml = policyTemplateEngine.createPolicyXml(definition); PdpPolicy pdpPolicy = new PdpPolicy(policyXml, definition.getName(), true, "provisioned", "surfnet", "system", true, definition.getType()); pdpPolicyRepository.save(pdpPolicy); }); } @Autowired  PolicyProvisioner(@Value("${provision.policies.directory}") String path, PdpPolicyRepository pdpPolicyRepository); @Override void onApplicationEvent(ContextStartedEvent contextStartedEvent);  }
@Test public void onApplicationEvent() throws Exception { when(pdpPolicyRepository.findByNameAndLatestRevision("test-policy", true)).thenReturn(Optional.empty()); ArgumentCaptor<PdpPolicy> argument = ArgumentCaptor.forClass(PdpPolicy.class); subject.onApplicationEvent(null); verify(pdpPolicyRepository).save(argument.capture()); PdpPolicy policy = argument.getValue(); assertEquals("urn:surfconext:xacml:policy:id:test_policy", policy.getPolicyId()); assertTrue(policy.getPolicyXml() .contains("DataType=\"http: "urn:collab:group:surfteams.nl:nl:surfnet:diensten:team_name" + "</AttributeValue>")); }
@Test(expected = RuntimeException.class) public void pathDoesNotExists() throws Exception { subject = new PolicyProvisioner("does-not-exists", pdpPolicyRepository); subject.onApplicationEvent(null); }