use of org.wso2.carbon.apimgt.core.models.policy.Condition in project carbon-apimgt by wso2.
the class PolicyDAOImpl method updateBlockConditionStateByUUID.
@Override
public boolean updateBlockConditionStateByUUID(String uuid, Boolean state) throws APIMgtDAOException {
boolean status = false;
String query = "UPDATE AM_BLOCK_CONDITIONS SET ENABLED = ? WHERE UUID = ?";
try (Connection connection = DAOUtil.getConnection();
PreparedStatement updateBlockConditionPreparedStatement = connection.prepareStatement(query)) {
connection.setAutoCommit(false);
updateBlockConditionPreparedStatement.setBoolean(1, state);
updateBlockConditionPreparedStatement.setString(2, uuid);
updateBlockConditionPreparedStatement.executeUpdate();
connection.commit();
status = true;
} catch (SQLException e) {
throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "updating block condition: " + uuid, e);
}
return status;
}
use of org.wso2.carbon.apimgt.core.models.policy.Condition in project carbon-apimgt by wso2.
the class APIMgtAdminServiceImplTestCase method testUpdateBlockConditionStateByUUID.
@Test(description = "Test updating block condition state by uuid")
public void testUpdateBlockConditionStateByUUID() 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.updateBlockConditionStateByUUID(blockConditions.getUuid(), true)).thenReturn(true);
Boolean statusTrue = adminService.updateBlockConditionStateByUUID(blockConditions.getUuid(), true);
Assert.assertTrue(statusTrue);
// Error path
// Failure updating
Mockito.when(policyDAO.updateBlockConditionStateByUUID(blockConditions.getUuid(), true)).thenReturn(false);
Boolean statusFalse = adminService.updateBlockConditionStateByUUID(blockConditions.getUuid(), true);
Assert.assertFalse(statusFalse);
// Error path
// APIMgtDAOException
Mockito.when(policyDAO.updateBlockConditionStateByUUID(blockConditions.getUuid(), true)).thenThrow(APIMgtDAOException.class);
try {
adminService.updateBlockConditionStateByUUID(blockConditions.getUuid(), true);
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Couldn't update block condition with UUID: " + blockConditions.getUuid() + ", state: " + true);
}
}
use of org.wso2.carbon.apimgt.core.models.policy.Condition in project carbon-apimgt by wso2.
the class APIMgtAdminServiceImplTestCase method testAddBlockCondition.
@Test(description = "Test adding block condition")
public void testAddBlockCondition() 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);
String uuid = adminService.addBlockCondition(blockConditions);
Assert.assertNotNull(uuid);
// Error path
Mockito.when(policyDAO.addBlockConditions(blockConditions)).thenThrow(APIMgtDAOException.class);
try {
adminService.addBlockCondition(blockConditions);
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Couldn't add block condition with condition type: " + blockConditions.getConditionType() + ", condition value: " + blockConditions.getConditionValue());
}
}
use of org.wso2.carbon.apimgt.core.models.policy.Condition in project carbon-apimgt by wso2.
the class APIMgtAdminServiceImplTestCase method testGetBlockConditionByUUID.
@Test(description = "Test getting block condition by uuid")
public void testGetBlockConditionByUUID() throws APIManagementException {
PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
APIMgtAdminServiceImpl adminService = getAPIMgtAdminServiceImpl(policyDAO);
BlockConditions blockConditions = SampleTestObjectCreator.createDefaultBlockCondition(BLOCK_CONDITION_TYPE);
Mockito.when(policyDAO.getBlockConditionByUUID(blockConditions.getUuid())).thenReturn(blockConditions);
BlockConditions blockConditionsReturned = adminService.getBlockConditionByUUID(blockConditions.getUuid());
Assert.assertEquals(blockConditionsReturned, blockConditions);
// Error path
Mockito.when(policyDAO.getBlockConditionByUUID(blockConditions.getUuid())).thenThrow(APIMgtDAOException.class);
try {
adminService.getBlockConditionByUUID(blockConditions.getUuid());
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Couldn't get block condition by UUID: " + blockConditions.getUuid());
}
}
use of org.wso2.carbon.apimgt.core.models.policy.Condition in project carbon-apimgt by wso2.
the class DefaultKeyManagerImplTestCase method testGetNewAccessTokenErrorCases.
@Test
public void testGetNewAccessTokenErrorCases() throws Exception {
DCRMServiceStub dcrmServiceStub = Mockito.mock(DCRMServiceStub.class);
OAuth2ServiceStubs oAuth2ServiceStub = Mockito.mock(OAuth2ServiceStubs.class);
OAuth2ServiceStubs.TokenServiceStub tokenStub = Mockito.mock(OAuth2ServiceStubs.TokenServiceStub.class);
ScopeRegistration scopeRegistration = Mockito.mock(ScopeRegistration.class);
DefaultKeyManagerImpl kmImpl = new DefaultKeyManagerImpl(dcrmServiceStub, oAuth2ServiceStub, scopeRegistration);
// error case - tokenRequest is null
try {
kmImpl.getNewAccessToken(null);
Assert.fail("Exception was expected, but wasn't thrown");
} catch (KeyManagementException ex) {
Assert.assertTrue(ex.getMessage().equals("No information available to generate Token. " + "AccessTokenRequest is null"));
}
// error case - invalid grant type
final String invalidGrantType = "invalid_grant";
AccessTokenRequest tokenRequest = createKeyManagerTokenRequest(consumerKey, consumerSecret, invalidGrantType, null, null, null, -2L, null, null, null, null);
try {
kmImpl.getNewAccessToken(tokenRequest);
Assert.fail("Exception was expected, but wasn't thrown");
} catch (KeyManagementException ex) {
Assert.assertTrue(ex.getMessage().contains("Invalid access token request. Unsupported grant type: " + invalidGrantType));
}
// error case - response is null (mock condition (validity period) is different)
tokenRequest = createKeyManagerTokenRequest(consumerKey, consumerSecret, KeyManagerConstants.REFRESH_GRANT_TYPE, null, null, null, -1L, null, null, "xxx-refresh-token-xxx", null);
Mockito.when(oAuth2ServiceStub.getTokenServiceStub()).thenReturn(tokenStub);
Mockito.when(oAuth2ServiceStub.getTokenServiceStub().generateRefreshGrantAccessToken(tokenRequest.getRefreshToken(), tokenRequest.getScopes(), tokenRequest.getValidityPeriod(), tokenRequest.getClientId(), tokenRequest.getClientSecret())).thenReturn(null);
try {
kmImpl.getNewAccessToken(tokenRequest);
Assert.fail("Exception was expected, but wasn't thrown");
} catch (KeyManagementException ex) {
Assert.assertTrue(ex.getMessage().equals("Error occurred while generating an access token. " + "Response is null"));
}
// error case - token response non-200
// //request to key manager
tokenRequest = createKeyManagerTokenRequest(consumerKey, consumerSecret, KeyManagerConstants.REFRESH_GRANT_TYPE, null, null, null, 7200L, null, null, "xxx-refresh-token-xxx", null);
final int errorCode = 500;
Response errorResponse = Response.builder().status(errorCode).headers(new HashMap<>()).body("backend error occurred", Util.UTF_8).build();
Mockito.when(oAuth2ServiceStub.getTokenServiceStub()).thenReturn(tokenStub);
Mockito.when(oAuth2ServiceStub.getTokenServiceStub().generateRefreshGrantAccessToken(tokenRequest.getRefreshToken(), tokenRequest.getScopes(), tokenRequest.getValidityPeriod(), tokenRequest.getClientId(), tokenRequest.getClientSecret())).thenReturn(errorResponse);
try {
kmImpl.getNewAccessToken(tokenRequest);
Assert.fail("Exception was expected, but wasn't thrown");
} catch (KeyManagementException ex) {
Assert.assertTrue(ex.getMessage().startsWith("Token generation request failed. HTTP error code: " + errorCode));
}
}
Aggregations