use of org.mule.runtime.deployment.model.api.policy.PolicyTemplate in project mule by mulesoft.
the class MuleApplicationPolicyProvider method addPolicy.
@Override
public synchronized void addPolicy(PolicyTemplateDescriptor policyTemplateDescriptor, PolicyParametrization parametrization) throws PolicyRegistrationException {
try {
checkArgument(application != null, "application was not configured on the policy provider");
Optional<RegisteredPolicyInstanceProvider> registeredPolicyInstanceProvider = registeredPolicyInstanceProviders.stream().filter(p -> p.getPolicyId().equals(parametrization.getId())).findFirst();
if (registeredPolicyInstanceProvider.isPresent()) {
throw new IllegalArgumentException(createPolicyAlreadyRegisteredError(parametrization.getId()));
}
Optional<RegisteredPolicyTemplate> registeredPolicyTemplate = registeredPolicyTemplates.stream().filter(p -> p.policyTemplate.getDescriptor().getBundleDescriptor().getArtifactId().equals(policyTemplateDescriptor.getBundleDescriptor().getArtifactId())).findAny();
if (!registeredPolicyTemplate.isPresent()) {
PolicyTemplate policyTemplate = policyTemplateFactory.createArtifact(application, policyTemplateDescriptor);
registeredPolicyTemplate = of(new RegisteredPolicyTemplate(policyTemplate));
registeredPolicyTemplates.add(registeredPolicyTemplate.get());
}
ApplicationPolicyInstance applicationPolicyInstance = policyInstanceProviderFactory.create(application, registeredPolicyTemplate.get().policyTemplate, parametrization);
applicationPolicyInstance.initialise();
registeredPolicyInstanceProviders.add(new RegisteredPolicyInstanceProvider(applicationPolicyInstance, parametrization.getId()));
registeredPolicyInstanceProviders.sort(null);
registeredPolicyTemplate.get().count++;
} catch (Exception e) {
throw new PolicyRegistrationException(createPolicyRegistrationError(parametrization.getId()), e);
}
}
use of org.mule.runtime.deployment.model.api.policy.PolicyTemplate in project mule by mulesoft.
the class DefaultApplicationPolicyInstanceTestCase method correctArtifactTypeForPolicies.
@Test
@Issue("MULE-14289")
@Ignore("MULE-14289: The discovered ArtifactConfigurationProcessor is not compatible with the provided mocks.")
public void correctArtifactTypeForPolicies() throws InitialisationException {
MuleContextListener muleContextListener = mock(MuleContextListener.class);
ArgumentCaptor<MuleContext> muleContextCaptor = ArgumentCaptor.forClass(MuleContext.class);
PolicyTemplate policyTemplate = mock(PolicyTemplate.class, RETURNS_DEEP_STUBS);
when(policyTemplate.getArtifactClassLoader().getClassLoader()).thenReturn(this.getClass().getClassLoader());
Application application = mock(Application.class, RETURNS_DEEP_STUBS);
Registry registry = application.getRegistry();
doReturn(of(mockContextWithServices())).when(registry).lookupByType(MuleContext.class);
doReturn(of(mock(ExtensionManager.class))).when(registry).lookupByName(OBJECT_EXTENSION_MANAGER);
PolicyParametrization parameterization = mock(PolicyParametrization.class, RETURNS_DEEP_STUBS);
when(parameterization.getId()).thenReturn("policyId");
DefaultApplicationPolicyInstance applicationPolicyInstance = new DefaultApplicationPolicyInstance(application, policyTemplate, parameterization, mock(ServiceRepository.class), mock(ClassLoaderRepository.class), emptyList(), mock(ExtensionModelLoaderRepository.class), muleContextListener);
applicationPolicyInstance.initialise();
verify(muleContextListener).onCreation(muleContextCaptor.capture());
assertThat(muleContextCaptor.getValue().getArtifactType(), is(POLICY));
}
use of org.mule.runtime.deployment.model.api.policy.PolicyTemplate in project mule by mulesoft.
the class DefaultPolicyTemplateFactoryTestCase method createsPolicyTemplate.
@Test
public void createsPolicyTemplate() throws Exception {
RegionClassLoader regionClassLoader = createRegionClassLoader();
PolicyTemplateClassLoaderBuilder policyTemplateClassLoaderBuilder = createPolicyTemplateClassLoaderBuilder(regionClassLoader);
MuleDeployableArtifactClassLoader policyClassLoader = mock(MuleDeployableArtifactClassLoader.class);
when(policyClassLoader.getArtifactId()).thenReturn(POLICY_ID);
when(policyTemplateClassLoaderBuilder.build()).thenReturn(policyClassLoader);
when(classLoaderBuilderFactory.createArtifactClassLoaderBuilder()).thenReturn(policyTemplateClassLoaderBuilder);
PolicyTemplate policyTemplate = policyTemplateFactory.createArtifact(createApplication(regionClassLoader), descriptor);
assertThat(policyTemplate.getArtifactClassLoader(), is(policyClassLoader));
assertThat(policyTemplate.getDescriptor(), is(descriptor));
assertThat(policyTemplate.getArtifactId(), is(POLICY_ID));
assertThat(regionClassLoader.getArtifactPluginClassLoaders().size(), equalTo(1));
}
Aggregations