use of org.wso2.carbon.apimgt.core.models.policy.APIPolicy in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testUpdateAPIWithContextChange.
@Test(description = "Test UpdateAPI with context changed")
public void testUpdateAPIWithContextChange() throws APIManagementException {
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
APIGateway gateway = Mockito.mock(APIGateway.class);
IdentityProvider identityProvider = Mockito.mock(IdentityProvider.class);
PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.api, APIMgtConstants.DEFAULT_API_POLICY)).thenReturn(new APIPolicy(APIMgtConstants.DEFAULT_API_POLICY));
Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, GOLD_TIER)).thenReturn(new SubscriptionPolicy(GOLD_TIER));
Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, SILVER_TIER)).thenReturn(new SubscriptionPolicy(SILVER_TIER));
Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, BRONZE_TIER)).thenReturn(new SubscriptionPolicy(BRONZE_TIER));
APIPublisherImpl apiPublisher = getApiPublisherImpl(identityProvider, apiDAO, apiLifecycleManager, gatewaySourceGenerator, gateway, policyDAO);
API.APIBuilder api = SampleTestObjectCreator.createDefaultAPI();
String uuid = api.getId();
Mockito.when(apiDAO.getAPI(uuid)).thenReturn(api.build());
Mockito.when(identityProvider.getRoleId(ADMIN_ROLE)).thenReturn(ADMIN_ROLE_ID);
Mockito.when(identityProvider.getRoleId(DEVELOPER_ROLE)).thenReturn(DEVELOPER_ROLE_ID);
Mockito.when(apiDAO.getApiSwaggerDefinition(api.getId())).thenReturn(SampleTestObjectCreator.apiDefinition);
apiPublisher.updateAPI(api.context("test"));
Mockito.verify(apiDAO, Mockito.times(1)).updateAPI(uuid, api.context("test").build());
}
use of org.wso2.carbon.apimgt.core.models.policy.APIPolicy in project carbon-apimgt by wso2.
the class APIMgtAdminServiceImplTestCase method testGetApiPolicyByUuid.
@Test(description = "Test getting API policy by UUID")
public void testGetApiPolicyByUuid() throws APIManagementException {
PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
APIMgtAdminServiceImpl adminService = getAPIMgtAdminServiceImpl(policyDAO);
APIPolicy apiPolicy = SampleTestObjectCreator.createDefaultAPIPolicy();
Mockito.when(policyDAO.getApiPolicyByUuid(apiPolicy.getUuid())).thenReturn(apiPolicy);
adminService.getApiPolicyByUuid(apiPolicy.getUuid());
Mockito.verify(policyDAO, Mockito.times(1)).getApiPolicyByUuid(apiPolicy.getUuid());
// Error path
Mockito.when(policyDAO.getApiPolicyByUuid(apiPolicy.getUuid())).thenThrow(APIMgtDAOException.class);
try {
adminService.getApiPolicyByUuid(apiPolicy.getUuid());
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Couldn't retrieve API policy with id: " + apiPolicy.getUuid());
}
}
use of org.wso2.carbon.apimgt.core.models.policy.APIPolicy in project carbon-apimgt by wso2.
the class APIMgtAdminServiceImplTestCase method testDeletePolicy.
@Test(description = "Test delete policy")
public void testDeletePolicy() throws APIManagementException {
PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
APIGateway apiGateway = Mockito.mock(APIGateway.class);
APIMgtAdminServiceImpl adminService = getAPIMgtAdminServiceImpl(policyDAO, apiGateway);
APIPolicy apiPolicy = SampleTestObjectCreator.createDefaultAPIPolicy();
Mockito.when(policyDAO.getPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.api, apiPolicy.getPolicyName())).thenReturn(apiPolicy);
adminService.deletePolicy(apiPolicy.getPolicyName(), APIMgtAdminService.PolicyLevel.api);
Mockito.verify(policyDAO, Mockito.times(1)).getPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.api, apiPolicy.getPolicyName());
Mockito.verify(policyDAO, Mockito.times(1)).deletePolicy(APIMgtAdminService.PolicyLevel.api, apiPolicy.getPolicyName());
// Error path
Mockito.doThrow(APIMgtDAOException.class).when(policyDAO).deletePolicy(APIMgtAdminService.PolicyLevel.api, apiPolicy.getPolicyName());
try {
adminService.deletePolicy(apiPolicy.getPolicyName(), APIMgtAdminService.PolicyLevel.api);
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Couldn't delete policy with name: " + apiPolicy.getPolicyName() + ", level: " + APIMgtAdminService.PolicyLevel.api);
}
}
use of org.wso2.carbon.apimgt.core.models.policy.APIPolicy in project carbon-apimgt by wso2.
the class APIMgtAdminServiceImplTestCase method testGetApiPolicy.
@Test(description = "Test getting API policy")
public void testGetApiPolicy() throws APIManagementException {
PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
APIMgtAdminServiceImpl adminService = getAPIMgtAdminServiceImpl(policyDAO);
APIPolicy apiPolicy = SampleTestObjectCreator.createDefaultAPIPolicy();
Mockito.when(policyDAO.getApiPolicy(apiPolicy.getPolicyName())).thenReturn(apiPolicy);
adminService.getApiPolicy(apiPolicy.getPolicyName());
Mockito.verify(policyDAO, Mockito.times(1)).getApiPolicy(apiPolicy.getPolicyName());
// Error path
Mockito.when(policyDAO.getApiPolicy(apiPolicy.getPolicyName())).thenThrow(APIMgtDAOException.class);
try {
adminService.getApiPolicy(apiPolicy.getPolicyName());
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Couldn't retrieve API policy with name: " + apiPolicy.getPolicyName());
}
}
use of org.wso2.carbon.apimgt.core.models.policy.APIPolicy in project carbon-apimgt by wso2.
the class PolicyExportManager method createArchiveFromExecutionPlans.
public String createArchiveFromExecutionPlans(String exportedPoliciesDirName, String archiveDir, String archiveName) throws APIManagementException {
try {
// retrieve all policies under each policy level
List<APIPolicy> apiPolicies = apiMgtAdminService.getApiPolicies();
List<ApplicationPolicy> applicationPolicies = apiMgtAdminService.getApplicationPolicies();
List<SubscriptionPolicy> subscriptionPolicies = apiMgtAdminService.getSubscriptionPolicies();
List<CustomPolicy> customPolicies = apiMgtAdminService.getCustomRules();
// write all execution Plans/Siddhi Apps to exportPoliciesDirName directory
String dirLocation = exportedPoliciesDirName + File.separator + EXPORT_POLICIES;
APIFileUtils.createDirectory(dirLocation);
if (!apiPolicies.isEmpty()) {
for (Map<String, String> map : getApiPolicySiddhiApps(apiPolicies)) {
prepareFile(dirLocation, map);
}
}
if (!applicationPolicies.isEmpty()) {
prepareFile(dirLocation, getAppPolicySiddhiApps(applicationPolicies));
}
if (!subscriptionPolicies.isEmpty()) {
prepareFile(dirLocation, getSubscriptionPolicySiddhiApps(subscriptionPolicies));
}
if (!customPolicies.isEmpty()) {
prepareFile(dirLocation, getCustomPolicySiddhiApps(customPolicies));
}
// create archive and get the archive location
String zippedFilePath = createArchiveFromPolicies(exportedPoliciesDirName, archiveDir, archiveName);
APIFileUtils.deleteDirectory(exportedPoliciesDirName);
return zippedFilePath;
} catch (APIManagementException e) {
String errorMessage = "Error while exporting policies";
log.error(errorMessage, e);
throw new APIManagementException(errorMessage, e);
}
}
Aggregations