use of org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionPolicyList in project carbon-apimgt by wso2.
the class PolicyUtilTest method testDeployAllPolicies.
@Test
public void testDeployAllPolicies() throws ExecutionPlanConfigurationException, ThrottlePolicyDeployerException, ExecutionPlanDependencyValidationException {
ExecutionPlanConfigurationException executionPlanConfigurationException = Mockito.mock(ExecutionPlanConfigurationException.class);
Mockito.when(eventProcessorService.getActiveExecutionPlan(Mockito.anyString())).thenThrow(executionPlanConfigurationException);
Map<String, ExecutionPlanConfiguration> executionPlanConfigurationMap = new HashMap<>();
ExecutionPlanConfiguration executionPlanConfiguration = Mockito.mock(ExecutionPlanConfiguration.class);
executionPlanConfigurationMap.put("sample_policy", executionPlanConfiguration);
Mockito.when(eventProcessorService.getAllActiveExecutionConfigurations()).thenReturn(executionPlanConfigurationMap);
SubscriptionPolicyList subscriptionPolicyList = new SubscriptionPolicyList();
SubscriptionPolicy subscriptionPolicy = TestUtil.getPolicySubLevel();
List<SubscriptionPolicy> subscriptionPolicies = new ArrayList<>();
subscriptionPolicies.add(subscriptionPolicy);
subscriptionPolicyList.setList(subscriptionPolicies);
Mockito.when(policyRetriever.getAllSubscriptionPolicies()).thenReturn(subscriptionPolicyList);
ApplicationPolicyList applicationPolicyList = new ApplicationPolicyList();
ApplicationPolicy applicationPolicy = TestUtil.getPolicyAppLevel();
List<ApplicationPolicy> applicationPolicies = new ArrayList<>();
applicationPolicies.add(applicationPolicy);
applicationPolicyList.setList(applicationPolicies);
Mockito.when(policyRetriever.getAllApplicationPolicies()).thenReturn(applicationPolicyList);
ApiPolicyList apiPolicyList = new ApiPolicyList();
ApiPolicy apiPolicy = TestUtil.getPolicyAPILevel();
List<ApiPolicy> apiPolicies = new ArrayList<>();
apiPolicies.add(apiPolicy);
apiPolicyList.setList(apiPolicies);
Mockito.when(policyRetriever.getAllApiPolicies()).thenReturn(apiPolicyList);
GlobalPolicyList globalPolicyList = new GlobalPolicyList();
GlobalPolicy globalPolicy = TestUtil.getPolicyGlobalLevel();
List<GlobalPolicy> globalPolicies = new ArrayList<>();
globalPolicies.add(globalPolicy);
globalPolicyList.setList(globalPolicies);
Mockito.when(policyRetriever.getAllGlobalPolicies()).thenReturn(globalPolicyList);
PolicyUtil.deployAllPolicies();
Mockito.verify(eventProcessorService, Mockito.times(5)).deployExecutionPlan(Mockito.anyString());
}
use of org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionPolicyList in project carbon-apimgt by wso2.
the class PolicyUtil method deployAllPolicies.
/**
* Deploy all the throttle policies retrieved from the database in the Traffic Manager.
*/
public static void deployAllPolicies() {
// Undeploy all existing policies
undeployAllPolicies();
PolicyRetriever policyRetriever = new PolicyRetriever();
try {
// Deploy all the policies retrieved from the database
SubscriptionPolicyList subscriptionPolicies = policyRetriever.getAllSubscriptionPolicies();
for (SubscriptionPolicy subscriptionPolicy : subscriptionPolicies.getList()) {
if (!(APIConstants.UNLIMITED_TIER.equalsIgnoreCase(subscriptionPolicy.getName()) || APIConstants.DEFAULT_SUB_POLICY_ASYNC_UNLIMITED.equalsIgnoreCase(subscriptionPolicy.getName()) || APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_UNLIMITED.equalsIgnoreCase(subscriptionPolicy.getName()))) {
deployPolicy(subscriptionPolicy, null);
}
}
ApplicationPolicyList applicationPolicies = policyRetriever.getAllApplicationPolicies();
for (ApplicationPolicy applicationPolicy : applicationPolicies.getList()) {
if (!APIConstants.UNLIMITED_TIER.equalsIgnoreCase(applicationPolicy.getName())) {
deployPolicy(applicationPolicy, null);
}
}
ApiPolicyList apiPolicies = policyRetriever.getAllApiPolicies();
for (ApiPolicy apiPolicy : apiPolicies.getList()) {
if (!APIConstants.UNLIMITED_TIER.equalsIgnoreCase(apiPolicy.getName())) {
deployPolicy(apiPolicy, null);
}
}
GlobalPolicyList globalPolicies = policyRetriever.getAllGlobalPolicies();
for (GlobalPolicy globalPolicy : globalPolicies.getList()) {
deployPolicy(globalPolicy, null);
}
} catch (ThrottlePolicyDeployerException e) {
log.error("Error in retrieving throttle policies", e);
}
}
use of org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionPolicyList in project carbon-apimgt by wso2.
the class PolicyRetriever method getSubscriptionPolicy.
/**
* Get a subscription policy given the name.
*
* @param policyName policy name
* @param tenantDomain tenant domain
* @return subscription policy
* @throws ThrottlePolicyDeployerException if failure occurs
*/
public SubscriptionPolicy getSubscriptionPolicy(String policyName, String tenantDomain) throws ThrottlePolicyDeployerException {
String path = APIConstants.SubscriptionValidationResources.SUBSCRIPTION_POLICIES + "?policyName=" + policyName;
SubscriptionPolicyList subscriptionPolicyList = getPolicies(path, tenantDomain, SubscriptionPolicyList.class);
if (subscriptionPolicyList.getList() != null && !subscriptionPolicyList.getList().isEmpty()) {
return subscriptionPolicyList.getList().get(0);
}
return null;
}
use of org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionPolicyList in project carbon-apimgt by wso2.
the class SubscriptionDataLoaderImpl method getSubscriptionPolicy.
@Override
public SubscriptionPolicy getSubscriptionPolicy(String policyName, String tenantDomain) throws DataLoadingException {
String endPoint = APIConstants.SubscriptionValidationResources.SUBSCRIPTION_POLICIES + "?policyName=" + policyName;
SubscriptionPolicy subscriptionPolicy = new SubscriptionPolicy();
if (log.isDebugEnabled()) {
log.debug("getSubscriptionPolicy for " + policyName + " for tenant " + tenantDomain);
}
String responseString;
try {
responseString = invokeService(endPoint, tenantDomain);
} catch (IOException e) {
String msg = "Error while executing the http client " + endPoint;
log.error(msg, e);
throw new DataLoadingException(msg, e);
}
if (responseString != null && !responseString.isEmpty()) {
SubscriptionPolicyList list = new Gson().fromJson(responseString, SubscriptionPolicyList.class);
if (list.getList() != null && !list.getList().isEmpty()) {
subscriptionPolicy = list.getList().get(0);
}
}
return subscriptionPolicy;
}
Aggregations