use of org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl in project carbon-apimgt by wso2.
the class APIMgtAdminServiceImplTestCase method testUpdateApiPolicy.
@Test(description = "Test update API policy")
public void testUpdateApiPolicy() throws APIManagementException {
PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
APIGateway apiGateway = Mockito.mock(APIGateway.class);
APIMgtAdminServiceImpl adminService = getAPIMgtAdminServiceImpl(policyDAO, apiGateway);
APIPolicy apiPolicy = SampleTestObjectCreator.createDefaultAPIPolicy();
adminService.updateApiPolicy(apiPolicy);
Mockito.verify(policyDAO, Mockito.times(1)).updateApiPolicy(apiPolicy);
// Error path
Mockito.doThrow(APIMgtDAOException.class).when(policyDAO).updateApiPolicy(apiPolicy);
try {
adminService.updateApiPolicy(apiPolicy);
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Couldn't update API policy for uuid: " + apiPolicy.getUuid());
}
}
use of org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl in project carbon-apimgt by wso2.
the class APIMgtAdminServiceImplTestCase method testDeleteBlockConditionByUuid.
@Test(description = "Test deleting block condition by uuid")
public void testDeleteBlockConditionByUuid() throws APIManagementException {
PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
APIGateway apiGateway = Mockito.mock(APIGateway.class);
APIMgtAdminServiceImpl adminService = getAPIMgtAdminServiceImpl(policyDAO, apiGateway);
BlockConditions blockConditions = SampleTestObjectCreator.createDefaultBlockCondition(BLOCK_CONDITION_TYPE);
Mockito.when(policyDAO.deleteBlockConditionByUuid(blockConditions.getUuid())).thenReturn(true);
Boolean statusTrue = adminService.deleteBlockConditionByUuid(blockConditions.getUuid());
Assert.assertTrue(statusTrue);
// Error path
// Failure deleting
Mockito.when(policyDAO.deleteBlockConditionByUuid(blockConditions.getUuid())).thenReturn(false);
Boolean statusFalse = adminService.deleteBlockConditionByUuid(blockConditions.getUuid());
Assert.assertFalse(statusFalse);
// Error path
// APIMgtDAOException
Mockito.when(policyDAO.deleteBlockConditionByUuid(blockConditions.getUuid())).thenThrow(APIMgtDAOException.class);
try {
adminService.deleteBlockConditionByUuid(blockConditions.getUuid());
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Couldn't delete block condition with UUID: " + blockConditions.getUuid());
}
}
use of org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl in project carbon-apimgt by wso2.
the class APIMgtAdminServiceImplTestCase method testGetAPIsByGatewayLabel.
@Test(description = "Test getting APIs by Gateway label")
public void testGetAPIsByGatewayLabel() throws APIManagementException {
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
List<String> gatewayLabels = new ArrayList<>();
gatewayLabels.add("Label1");
List<API> apiListExpected = new ArrayList<>();
API api = SampleTestObjectCreator.createDefaultAPI().labels(new ArrayList<>(gatewayLabels)).build();
apiListExpected.add(api);
Mockito.when(apiDAO.getAPIsByGatewayLabel(gatewayLabels)).thenReturn(apiListExpected);
APIMgtAdminServiceImpl adminService = getAPIMgtAdminServiceImpl(apiDAO);
List<API> apiListReturned = adminService.getAPIsByGatewayLabel(gatewayLabels);
Assert.assertEquals(apiListReturned, apiListExpected);
// When gateway labels are null
try {
adminService.getAPIsByGatewayLabel(null);
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Gateway labels cannot be null");
}
// Error path
// Error while getting the APIs
Mockito.when(apiDAO.getAPIsByGatewayLabel(gatewayLabels)).thenThrow(APIMgtDAOException.class);
try {
adminService.getAPIsByGatewayLabel(gatewayLabels);
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Error occurred while getting the API list in given gateway labels");
}
}
use of org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl in project carbon-apimgt by wso2.
the class APIMgtAdminServiceImplTestCase method testGetAllPolicies.
@Test(description = "Test getting all policies")
public void testGetAllPolicies() throws APIManagementException {
PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
APIMgtAdminServiceImpl adminService = getAPIMgtAdminServiceImpl(policyDAO);
Set<PolicyValidationData> policyValidationDataSetExpected = new HashSet<>();
PolicyValidationData policyValidationData = new PolicyValidationData(POLICY_ID, POLICY_NAME, true);
policyValidationDataSetExpected.add(policyValidationData);
Mockito.when(policyDAO.getAllPolicies()).thenReturn(policyValidationDataSetExpected);
Set<PolicyValidationData> policyValidationDataSetReturned = adminService.getAllPolicies();
Assert.assertEquals(policyValidationDataSetReturned, policyValidationDataSetExpected);
// Error path
Mockito.when(policyDAO.getAllPolicies()).thenThrow(APIMgtDAOException.class);
try {
adminService.getAllPolicies();
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Error occurred while retrieving policies");
}
}
use of org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl in project carbon-apimgt by wso2.
the class ApisApiServiceImplTestCase method apisGetApisForExceptionTestCase.
@Test
public void apisGetApisForExceptionTestCase() throws Exception {
String labels = "ZONE_ONE,ZONE_TWO";
APIMgtAdminServiceImpl apiMgtAdminService = Mockito.mock(APIMgtAdminServiceImpl.class);
PowerMockito.mockStatic(RestApiUtil.class);
PowerMockito.when(RestApiUtil.getAPIMgtAdminService()).thenReturn(apiMgtAdminService);
String apiID = UUID.randomUUID().toString();
String message = "Error while retrieving gateway config of API " + apiID;
APIManagementException apiManagementException = new APIManagementException(message, ExceptionCodes.GATEWAY_EXCEPTION);
Mockito.when(RestApiUtil.getAPIMgtAdminService()).thenThrow(apiManagementException);
ApisApiServiceImpl apisApiService = new ApisApiServiceImpl();
Response response = apisApiService.apisGet(labels, "Published", getRequest());
Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
}
Aggregations