use of org.wso2.carbon.apimgt.common.gateway.dto.QueryAnalyzerResponseDTO in project carbon-apimgt by wso2.
the class QueryMutationAnalyzer method analyseQueryMutationComplexity.
/**
* This method analyses the query complexity
*
* @param messageContext message context of the request
* @param payload payload of the request
* @return true, if query complexity does not exceed the maximum or false, if query complexity exceeds the maximum
*/
public boolean analyseQueryMutationComplexity(MessageContext messageContext, String payload) {
FieldComplexityCalculator fieldComplexityCalculator = null;
try {
fieldComplexityCalculator = new FieldComplexityCalculatorImpl((String) messageContext.getProperty(APIConstants.GRAPHQL_ACCESS_CONTROL_POLICY));
} catch (ParseException e) {
String errorMessage = "Policy definition parsing failed. ";
handleFailure(GraphQLConstants.GRAPHQL_INVALID_QUERY, messageContext, errorMessage, errorMessage);
}
int maxQueryComplexity = getMaxQueryComplexity(messageContext);
QueryAnalyzerResponseDTO responseDTO = analyseQueryComplexity(maxQueryComplexity, payload, fieldComplexityCalculator);
if (!responseDTO.isSuccess() && !responseDTO.getErrorList().isEmpty()) {
handleFailure(GraphQLConstants.GRAPHQL_QUERY_TOO_COMPLEX, messageContext, GraphQLConstants.GRAPHQL_QUERY_TOO_COMPLEX_MESSAGE, responseDTO.getErrorList().toString());
log.error(responseDTO.getErrorList().toString());
return false;
}
return true;
}
use of org.wso2.carbon.apimgt.common.gateway.dto.QueryAnalyzerResponseDTO in project carbon-apimgt by wso2.
the class QueryAnalyzerTestCase method testAnalyzeQueryDepthForSubscriptionsSuccess.
@Test
public void testAnalyzeQueryDepthForSubscriptionsSuccess() {
int maxComplexity = 4;
String payload = "subscription {\n" + " liftStatusChange {\n" + " name\n" + " }\n" + "}\n";
QueryAnalyzerResponseDTO queryAnalyzerResponseDTO = queryAnalyzer.analyseQueryComplexity(maxComplexity, payload, fieldComplexityCalculator);
Assert.assertTrue(queryAnalyzerResponseDTO.isSuccess());
Assert.assertTrue(queryAnalyzerResponseDTO.getErrorList().isEmpty());
maxComplexity = 0;
queryAnalyzerResponseDTO = queryAnalyzer.analyseQueryComplexity(maxComplexity, payload, fieldComplexityCalculator);
Assert.assertTrue(queryAnalyzerResponseDTO.isSuccess());
Assert.assertTrue(queryAnalyzerResponseDTO.getErrorList().isEmpty());
}
use of org.wso2.carbon.apimgt.common.gateway.dto.QueryAnalyzerResponseDTO in project carbon-apimgt by wso2.
the class QueryAnalyzerTestCase method testAnalyseQueryDepthForSubscriptionsError.
@Test
public void testAnalyseQueryDepthForSubscriptionsError() {
int maxDepth = 1;
String payload = "subscription {\n" + " liftStatusChange {\n" + " name\n" + " id\n" + " }\n" + "}\n";
QueryAnalyzerResponseDTO queryAnalyzerResponseDTO = queryAnalyzer.analyseQueryDepth(maxDepth, payload);
Assert.assertFalse(queryAnalyzerResponseDTO.isSuccess());
Assert.assertTrue(queryAnalyzerResponseDTO.getErrorList().toString().contains("maximum query depth exceeded 2 > 1"));
}
use of org.wso2.carbon.apimgt.common.gateway.dto.QueryAnalyzerResponseDTO in project carbon-apimgt by wso2.
the class QueryAnalyzerTestCase method testAnalyzeQueryDepthForSubscriptionsError.
@Test
public void testAnalyzeQueryDepthForSubscriptionsError() {
int maxComplexity = 4;
String payload = "subscription {\n" + " liftStatusChange {\n" + " name\n" + " id\n" + " night\n" + " }\n" + "}\n";
QueryAnalyzerResponseDTO queryAnalyzerResponseDTO = queryAnalyzer.analyseQueryComplexity(maxComplexity, payload, fieldComplexityCalculator);
Assert.assertFalse(queryAnalyzerResponseDTO.isSuccess());
Assert.assertTrue(queryAnalyzerResponseDTO.getErrorList().toString().contains("maximum query complexity exceeded"));
}
use of org.wso2.carbon.apimgt.common.gateway.dto.QueryAnalyzerResponseDTO in project carbon-apimgt by wso2.
the class QueryAnalyzerTestCase method testAnalyseQueryDepthForSubscriptionsSuccess.
@Test
public void testAnalyseQueryDepthForSubscriptionsSuccess() {
int maxDepth = 3;
String payload = "subscription {\n" + " liftStatusChange {\n" + " id\n" + " }\n" + "}\n";
QueryAnalyzerResponseDTO queryAnalyzerResponseDTO = queryAnalyzer.analyseQueryDepth(maxDepth, payload);
Assert.assertTrue(queryAnalyzerResponseDTO.isSuccess());
Assert.assertTrue(queryAnalyzerResponseDTO.getErrorList().isEmpty());
maxDepth = 0;
queryAnalyzerResponseDTO = queryAnalyzer.analyseQueryDepth(maxDepth, payload);
Assert.assertTrue(queryAnalyzerResponseDTO.isSuccess());
Assert.assertTrue(queryAnalyzerResponseDTO.getErrorList().isEmpty());
}
Aggregations