use of org.wso2.carbon.apimgt.keymgt.model.entity.ApplicationPolicy in project carbon-apimgt by wso2.
the class ApiMgtDAO method getApplicationPolicyByUUID.
/**
* Retrieves {@link ApplicationPolicy} with name <code>uuid</code>
*
* @param uuid uuid of the policy to retrieve from the database
* @return {@link ApplicationPolicy}
* @throws APIManagementException
*/
public ApplicationPolicy getApplicationPolicyByUUID(String uuid) throws APIManagementException {
ApplicationPolicy policy = null;
Connection connection = null;
PreparedStatement selectStatement = null;
ResultSet resultSet = null;
String sqlQuery = SQLConstants.GET_APPLICATION_POLICY_BY_UUID_SQL;
if (forceCaseInsensitiveComparisons) {
sqlQuery = SQLConstants.GET_APPLICATION_POLICY_BY_UUID_SQL;
}
try {
connection = APIMgtDBUtil.getConnection();
selectStatement = connection.prepareStatement(sqlQuery);
selectStatement.setString(1, uuid);
// Should return only single row
resultSet = selectStatement.executeQuery();
if (resultSet.next()) {
policy = new ApplicationPolicy(resultSet.getString(ThrottlePolicyConstants.COLUMN_NAME));
setCommonPolicyDetails(policy, resultSet);
}
} catch (SQLException e) {
handleException("Failed to get application policy: " + uuid, e);
} finally {
APIMgtDBUtil.closeAllConnections(selectStatement, connection, resultSet);
}
return policy;
}
use of org.wso2.carbon.apimgt.keymgt.model.entity.ApplicationPolicy in project carbon-apimgt by wso2.
the class ApiMgtDAO method getApplicationPolicies.
/**
* Get application level polices
*
* @param tenantID polices are selected only belong to specific tenantID
* @return AppilicationPolicy array list
*/
public ApplicationPolicy[] getApplicationPolicies(int tenantID) throws APIManagementException {
List<ApplicationPolicy> policies = new ArrayList<ApplicationPolicy>();
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sqlQuery = SQLConstants.GET_APP_POLICIES;
if (forceCaseInsensitiveComparisons) {
sqlQuery = SQLConstants.GET_APP_POLICIES;
}
try {
conn = APIMgtDBUtil.getConnection();
ps = conn.prepareStatement(sqlQuery);
ps.setInt(1, tenantID);
rs = ps.executeQuery();
while (rs.next()) {
ApplicationPolicy appPolicy = new ApplicationPolicy(rs.getString(ThrottlePolicyConstants.COLUMN_NAME));
setCommonPolicyDetails(appPolicy, rs);
policies.add(appPolicy);
}
} catch (SQLException e) {
handleException("Error while executing SQL", e);
} finally {
APIMgtDBUtil.closeAllConnections(ps, conn, rs);
}
return policies.toArray(new ApplicationPolicy[policies.size()]);
}
use of org.wso2.carbon.apimgt.keymgt.model.entity.ApplicationPolicy in project carbon-apimgt by wso2.
the class APIProviderImplTest method getPolicyAppLevel.
private ApplicationPolicy getPolicyAppLevel() {
ApplicationPolicy policy = new ApplicationPolicy("gold");
policy.setDescription("Description");
policy.setTenantDomain("carbon.super");
RequestCountLimit defaultLimit = new RequestCountLimit();
defaultLimit.setTimeUnit("min");
defaultLimit.setUnitTime(5);
defaultLimit.setRequestCount(1000);
QuotaPolicy defaultQuotaPolicy = new QuotaPolicy();
defaultQuotaPolicy.setLimit(defaultLimit);
defaultQuotaPolicy.setType("RequestCount");
policy.setDefaultQuotaPolicy(defaultQuotaPolicy);
return policy;
}
use of org.wso2.carbon.apimgt.keymgt.model.entity.ApplicationPolicy in project carbon-apimgt by wso2.
the class AbstractAPIManagerTestCase method testGetPolicies.
@Test
public void testGetPolicies() throws APIManagementException, org.wso2.carbon.user.api.UserStoreException, RegistryException, XMLStreamException {
APIPolicy[] policies1 = { new APIPolicy("policy1") };
ApplicationPolicy[] policies2 = { new ApplicationPolicy("policy2"), new ApplicationPolicy("policy3") };
SubscriptionPolicy[] policies3 = { new SubscriptionPolicy("policy4"), new SubscriptionPolicy("policy5"), new SubscriptionPolicy("policy6") };
GlobalPolicy[] policies4 = { new GlobalPolicy("policy7"), new GlobalPolicy("policy8"), new GlobalPolicy("policy9"), new GlobalPolicy("policy0") };
PowerMockito.mockStatic(APIUtil.class);
BDDMockito.when(APIUtil.getTenantId(Mockito.anyString())).thenReturn(-1234);
PowerMockito.when(APIUtil.replaceSystemProperty(Mockito.anyString())).thenAnswer((Answer<String>) invocation -> {
Object[] args = invocation.getArguments();
return (String) args[0];
});
AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(apiMgtDAO);
Mockito.when(apiMgtDAO.getAPIPolicies(Mockito.anyInt())).thenReturn(policies1);
Mockito.when(apiMgtDAO.getApplicationPolicies(Mockito.anyInt())).thenReturn(policies2);
Mockito.when(apiMgtDAO.getSubscriptionPolicies(Mockito.anyInt())).thenReturn(policies3);
Mockito.when(apiMgtDAO.getGlobalPolicies(Mockito.anyInt())).thenReturn(policies4);
ServiceReferenceHolder sh = mockRegistryAndUserRealm(-1234);
APIManagerConfigurationService amConfigService = Mockito.mock(APIManagerConfigurationService.class);
APIManagerConfiguration amConfig = Mockito.mock(APIManagerConfiguration.class);
ThrottleProperties throttleProperties = Mockito.mock(ThrottleProperties.class, Mockito.RETURNS_MOCKS);
PowerMockito.when(sh.getAPIManagerConfigurationService()).thenReturn(amConfigService);
PowerMockito.when(amConfigService.getAPIManagerConfiguration()).thenReturn(amConfig);
PowerMockito.when(amConfig.getThrottleProperties()).thenReturn(throttleProperties);
Assert.assertEquals(abstractAPIManager.getPolicies(API_PROVIDER, PolicyConstants.POLICY_LEVEL_API).length, 1);
Assert.assertEquals(abstractAPIManager.getPolicies(API_PROVIDER, PolicyConstants.POLICY_LEVEL_APP).length, 2);
PowerMockito.when(throttleProperties.isEnableUnlimitedTier()).thenReturn(false);
Assert.assertEquals(3, abstractAPIManager.getPolicies(API_PROVIDER, PolicyConstants.POLICY_LEVEL_SUB).length);
Assert.assertEquals(4, abstractAPIManager.getPolicies(API_PROVIDER, PolicyConstants.POLICY_LEVEL_GLOBAL).length);
Assert.assertEquals(0, abstractAPIManager.getPolicies(API_PROVIDER, "Test").length);
}
use of org.wso2.carbon.apimgt.keymgt.model.entity.ApplicationPolicy in project carbon-apimgt by wso2.
the class APIMgtDAOTest method testDeleteSubscriptionsForapiId.
@Test
public void testDeleteSubscriptionsForapiId() throws Exception {
Subscriber subscriber = new Subscriber("testCreateApplicationRegistrationEntry");
String organization = "testOrg";
subscriber.setTenantId(-1234);
subscriber.setEmail("abc@wso2.com");
subscriber.setSubscribedDate(new Date(System.currentTimeMillis()));
apiMgtDAO.addSubscriber(subscriber, null);
Policy applicationPolicy = getApplicationPolicy("testCreateApplicationRegistrationEntry");
SubscriptionPolicy subscriptionPolicy = (SubscriptionPolicy) getSubscriptionPolicy("testCreateApplicationRegistrationEntry");
subscriptionPolicy.setMonetizationPlan(APIConstants.Monetization.FIXED_RATE);
apiMgtDAO.addSubscriptionPolicy((SubscriptionPolicy) subscriptionPolicy);
applicationPolicy.setTenantId(-1234);
apiMgtDAO.addApplicationPolicy((ApplicationPolicy) applicationPolicy);
Application application = new Application("testCreateApplicationRegistrationEntry", subscriber);
application.setTier("testCreateApplicationRegistrationEntry");
application.setId(apiMgtDAO.addApplication(application, "testCreateApplicationRegistrationEntry", organization));
application.setDescription("updated description");
apiMgtDAO.updateApplication(application);
assertEquals(apiMgtDAO.getApplicationById(application.getId()).getDescription(), "updated description");
APIIdentifier apiId = new APIIdentifier("testCreateApplicationRegistrationEntry", "testCreateApplicationRegistrationEntry", "1.0.0");
API api = new API(apiId);
api.setContext("/testCreateApplicationRegistrationEntry");
api.setContextTemplate("/testCreateApplicationRegistrationEntry/{version}");
api.setVersionTimestamp(String.valueOf(System.currentTimeMillis()));
APIPolicy apiPolicy = (APIPolicy) getPolicyAPILevelPerUser("testCreateApplicationRegistrationEntry");
api.setApiLevelPolicy(apiPolicy.getPolicyName());
api.setUUID(UUID.randomUUID().toString());
api.getId().setId(apiMgtDAO.addAPI(api, -1234, organization));
apiId.setTier(subscriptionPolicy.getPolicyName());
ApiTypeWrapper apiTypeWrapper = new ApiTypeWrapper(api);
int subsId = apiMgtDAO.addSubscription(apiTypeWrapper, application, APIConstants.SubscriptionStatus.ON_HOLD, subscriber.getName());
assertTrue(apiMgtDAO.getApplicationsByTier(subscriptionPolicy.getPolicyName()).length > 0);
String subStatus = apiMgtDAO.getSubscriptionStatusById(subsId);
assertEquals(subStatus, APIConstants.SubscriptionStatus.ON_HOLD);
SubscribedAPI subscribedAPI = apiMgtDAO.getSubscriptionById(subsId);
String clientIdProduction = UUID.randomUUID().toString();
String clientIdSandbox = UUID.randomUUID().toString();
apiMgtDAO.createApplicationKeyTypeMappingForManualClients(APIConstants.API_KEY_TYPE_PRODUCTION, application.getId(), clientIdProduction, "Default", UUID.randomUUID().toString());
apiMgtDAO.createApplicationKeyTypeMappingForManualClients(APIConstants.API_KEY_TYPE_SANDBOX, application.getId(), clientIdSandbox, "Default", UUID.randomUUID().toString());
assertTrue(apiMgtDAO.getSubscriptionCount(subscriber, application.getName(), null) > 0);
OAuthApplicationInfo oAuthApplicationInfo = new OAuthApplicationInfo();
Mockito.when(keyManager.retrieveApplication(clientIdProduction)).thenReturn(oAuthApplicationInfo);
Mockito.when(keyManager.retrieveApplication(clientIdSandbox)).thenReturn(oAuthApplicationInfo);
assertTrue(apiMgtDAO.getSubscribedAPIs(organization, subscriber, null).size() > 0);
assertEquals(subscribedAPI.getSubCreatedStatus(), APIConstants.SubscriptionCreatedStatus.SUBSCRIBE);
assertEquals(subscribedAPI.getApiId(), apiId);
assertEquals(subscribedAPI.getApplication().getId(), application.getId());
SubscribedAPI subscribedAPIFromUuid = apiMgtDAO.getSubscriptionByUUID(subscribedAPI.getUUID());
assertEquals(subscribedAPIFromUuid.getSubCreatedStatus(), APIConstants.SubscriptionCreatedStatus.SUBSCRIBE);
assertEquals(subscribedAPIFromUuid.getApiId(), apiId);
assertEquals(subscribedAPIFromUuid.getApplication().getId(), application.getId());
apiMgtDAO.updateApplicationStatus(application.getId(), APIConstants.ApplicationStatus.APPLICATION_APPROVED);
String status = apiMgtDAO.getApplicationStatus("testCreateApplicationRegistrationEntry", "testCreateApplicationRegistrationEntry");
assertEquals(status, APIConstants.ApplicationStatus.APPLICATION_APPROVED);
boolean applicationExist = apiMgtDAO.isApplicationExist(application.getName(), subscriber.getName(), null, organization);
assertTrue(applicationExist);
Set<SubscribedAPI> subscribedAPIS = apiMgtDAO.getSubscribedAPIs(subscriber, application.getName(), null);
assertEquals(subscribedAPIS.size(), 1);
apiMgtDAO.updateSubscription(apiId, APIConstants.SubscriptionStatus.BLOCKED, application.getId(), organization);
subscribedAPI.setSubStatus(APIConstants.SubscriptionStatus.REJECTED);
apiMgtDAO.updateSubscription(subscribedAPI);
assertTrue(apiMgtDAO.hasSubscription(subscriptionPolicy.getPolicyName(), subscriber.getName(), PolicyConstants.POLICY_LEVEL_SUB));
assertTrue(apiMgtDAO.hasSubscription(applicationPolicy.getPolicyName(), subscriber.getName(), PolicyConstants.POLICY_LEVEL_APP));
assertTrue(apiMgtDAO.hasSubscription(apiPolicy.getPolicyName(), subscriber.getName(), PolicyConstants.POLICY_LEVEL_API));
assertTrue(apiPolicy.getPolicyName().equals(apiMgtDAO.getAPILevelTier(apiMgtDAO.getAPIID(api.getUuid()))));
apiMgtDAO.recordAPILifeCycleEvent(api.getUuid(), "CREATED", "PUBLISHED", "testCreateApplicationRegistrationEntry", -1234);
apiMgtDAO.updateDefaultAPIPublishedVersion(apiId);
apiMgtDAO.removeAllSubscriptions(api.getUuid());
assertTrue(apiMgtDAO.getAPINamesMatchingContext(api.getContext()).size() > 0);
apiMgtDAO.deleteAPI(api.getUuid());
apiMgtDAO.deleteApplication(application);
apiMgtDAO.removeThrottlePolicy(PolicyConstants.POLICY_LEVEL_APP, "testCreateApplicationRegistrationEntry", -1234);
apiMgtDAO.deleteApplicationKeyMappingByConsumerKey(clientIdProduction);
apiMgtDAO.deleteApplicationMappingByConsumerKey(clientIdSandbox);
deleteSubscriber(subscriber.getId());
}
Aggregations