Search in sources :

Example 16 with Analyzer

use of org.wso2.carbon.apimgt.core.api.Analyzer in project carbon-apimgt by wso2.

the class XMLAnalyzerTestCase method testMaxAttributeLength.

@Test(expectedExceptions = APIMThreatAnalyzerException.class)
public void testMaxAttributeLength() throws Exception {
    init();
    String xmlString = "<root attribute1111111111='someValue111111111' attribute2='1'></root>";
    XMLAnalyzer analyzer = new XMLAnalyzer();
    xmlConfig.setMaxAttributeLength(1);
    xmlConfig.setMaxAttributeCount(1);
    analyzer.configure(xmlConfig);
    analyzer.analyze(xmlString, "/foo");
}
Also used : XMLAnalyzer(org.wso2.carbon.apimgt.ballerina.threatprotection.analyzer.XMLAnalyzer) BeforeTest(org.testng.annotations.BeforeTest) Test(org.testng.annotations.Test)

Example 17 with Analyzer

use of org.wso2.carbon.apimgt.core.api.Analyzer in project carbon-apimgt by wso2.

the class ConfigureJsonAnalyzer method execute.

@Override
public BValue[] execute(Context context) {
    String event = getStringArgument(context, 0);
    // configure json analyzer
    BStruct jsonInfo = ((BStruct) getRefArgument(context, 0));
    if (jsonInfo != null) {
        String jsonPolicyId = jsonInfo.getStringField(0);
        switch(event) {
            case THREAT_PROTECTION_POLICY_ADD:
            case THREAT_PROTECTION_POLICY_UPDATE:
                String name = jsonInfo.getStringField(1);
                int propertyCount = (int) jsonInfo.getIntField(0);
                int stringLength = (int) jsonInfo.getIntField(1);
                int arrayElementCount = (int) jsonInfo.getIntField(2);
                int keyLength = (int) jsonInfo.getIntField(3);
                int maxJSONDepth = (int) jsonInfo.getIntField(4);
                JSONConfig jsonConfig = new JSONConfig();
                jsonConfig.setName(name);
                jsonConfig.setMaxPropertyCount(propertyCount);
                jsonConfig.setMaxStringLength(stringLength);
                jsonConfig.setMaxArrayElementCount(arrayElementCount);
                jsonConfig.setMaxKeyLength(keyLength);
                jsonConfig.setMaxJsonDepth(maxJSONDepth);
                // put into ConfigurationHolder
                ConfigurationHolder.addJsonConfig(jsonPolicyId, jsonConfig);
                break;
            case THREAT_PROTECTION_POLICY_DELETE:
                ConfigurationHolder.removeJsonConfig(jsonPolicyId);
                break;
            default:
                log.warn("Unknown event type for Threat Protection Policy. Event: " + event);
                break;
        }
    }
    return getBValues(new BBoolean(true));
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BBoolean(org.ballerinalang.model.values.BBoolean) JSONConfig(org.wso2.carbon.apimgt.ballerina.threatprotection.configurations.JSONConfig)

Example 18 with Analyzer

use of org.wso2.carbon.apimgt.core.api.Analyzer in project carbon-apimgt by wso2.

the class APIManagerFactory method getAnalyzer.

/**
 * Get Analyzer for a particular user
 *
 * @param username The username of user who's requesting the object
 * @return APIPublisher object
 * @throws APIManagementException if error occurred while initializing Analytics object
 */
public Analyzer getAnalyzer(String username) throws APIManagementException {
    Analyzer analyzer = analyzers.get(username);
    if (analyzer == null) {
        synchronized (username.intern()) {
            analyzer = analyzers.get(username);
            if (analyzer != null) {
                return analyzer;
            }
            analyzer = newAnalyzer(username);
            analyzers.put(username, analyzer);
        }
    }
    return analyzer;
}
Also used : Analyzer(org.wso2.carbon.apimgt.core.api.Analyzer)

Example 19 with Analyzer

use of org.wso2.carbon.apimgt.core.api.Analyzer in project carbon-apimgt by wso2.

the class AnalyzerImplTestCase method testGetAPIInfo.

@Test(description = "Get API Info test")
public void testGetAPIInfo() throws APIManagementException {
    AnalyticsDAO analyticsDAO = Mockito.mock(AnalyticsDAO.class);
    APIInfo apiInfo = new APIInfo();
    List<APIInfo> apiInfos = new ArrayList<>();
    apiInfos.add(apiInfo);
    Analyzer analyzer = getAnalyzerImpl(analyticsDAO);
    when(analyticsDAO.getAPIInfo(Instant.parse(FROM_TIMESTAMP), Instant.parse(TO_TIMESTAMP), null)).thenReturn(apiInfos);
    List<APIInfo> apiInfoResult = analyzer.getAPIInfo(Instant.parse(FROM_TIMESTAMP), Instant.parse(TO_TIMESTAMP), null);
    Assert.assertNotNull(apiInfoResult);
    verify(analyticsDAO, Mockito.times(1)).getAPIInfo(Instant.parse(FROM_TIMESTAMP), Instant.parse(TO_TIMESTAMP), null);
    // Error path
    Mockito.when(analyticsDAO.getAPIInfo(Instant.parse(FROM_TIMESTAMP), Instant.parse(TO_TIMESTAMP), null)).thenThrow(APIMgtDAOException.class);
    try {
        analyzer.getAPIInfo(Instant.parse(FROM_TIMESTAMP), Instant.parse(TO_TIMESTAMP), null);
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Error occurred while fetching API information");
    }
}
Also used : AnalyticsDAO(org.wso2.carbon.apimgt.core.dao.AnalyticsDAO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) APIInfo(org.wso2.carbon.apimgt.core.models.analytics.APIInfo) ArrayList(java.util.ArrayList) Analyzer(org.wso2.carbon.apimgt.core.api.Analyzer) Test(org.testng.annotations.Test)

Example 20 with Analyzer

use of org.wso2.carbon.apimgt.core.api.Analyzer in project carbon-apimgt by wso2.

the class AnalyzerImplTestCase method testGetAPICount.

@Test(description = "Get API count test")
public void testGetAPICount() throws APIManagementException {
    AnalyticsDAO analyticsDAO = Mockito.mock(AnalyticsDAO.class);
    APICount apiCount1 = new APICount();
    APICount apiCount2 = new APICount();
    List<APICount> apiCountList = new ArrayList<>();
    apiCountList.add(apiCount1);
    apiCountList.add(apiCount2);
    Analyzer analyzer = getAnalyzerImpl(analyticsDAO);
    when(analyticsDAO.getAPICount(Instant.parse(FROM_TIMESTAMP), Instant.parse(TO_TIMESTAMP), null)).thenReturn(apiCountList);
    List<APICount> apiCountListFromDB = analyzer.getAPICount(Instant.parse(FROM_TIMESTAMP), Instant.parse(TO_TIMESTAMP), null);
    Assert.assertNotNull(apiCountListFromDB);
    verify(analyticsDAO, Mockito.times(1)).getAPICount(Instant.parse(FROM_TIMESTAMP), Instant.parse(TO_TIMESTAMP), null);
    // Error path
    Mockito.when(analyticsDAO.getAPICount(Instant.parse(FROM_TIMESTAMP), Instant.parse(TO_TIMESTAMP), null)).thenThrow(APIMgtDAOException.class);
    try {
        analyzer.getAPICount(Instant.parse(FROM_TIMESTAMP), Instant.parse(TO_TIMESTAMP), null);
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Error occurred while fetching API count information");
    }
}
Also used : APICount(org.wso2.carbon.apimgt.core.models.analytics.APICount) AnalyticsDAO(org.wso2.carbon.apimgt.core.dao.AnalyticsDAO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ArrayList(java.util.ArrayList) Analyzer(org.wso2.carbon.apimgt.core.api.Analyzer) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)27 JSONConfig (org.wso2.carbon.apimgt.ballerina.threatprotection.configurations.JSONConfig)15 JSONAnalyzer (org.wso2.carbon.apimgt.ballerina.threatprotection.analyzer.JSONAnalyzer)14 Analyzer (org.wso2.carbon.apimgt.core.api.Analyzer)13 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)12 BeforeTest (org.testng.annotations.BeforeTest)7 XMLAnalyzer (org.wso2.carbon.apimgt.ballerina.threatprotection.analyzer.XMLAnalyzer)7 ArrayList (java.util.ArrayList)6 AnalyticsDAO (org.wso2.carbon.apimgt.core.dao.AnalyticsDAO)6 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)6 ZoneId (java.time.ZoneId)5 APISubscriptionCount (org.wso2.carbon.apimgt.core.models.analytics.APISubscriptionCount)3 BBoolean (org.ballerinalang.model.values.BBoolean)2 APIMThreatAnalyzer (org.wso2.carbon.apimgt.ballerina.threatprotection.analyzer.APIMThreatAnalyzer)2 XMLConfig (org.wso2.carbon.apimgt.ballerina.threatprotection.configurations.XMLConfig)2 APICount (org.wso2.carbon.apimgt.core.models.analytics.APICount)2 APIInfo (org.wso2.carbon.apimgt.core.models.analytics.APIInfo)2 ApplicationCount (org.wso2.carbon.apimgt.core.models.analytics.ApplicationCount)2 SubscriptionCount (org.wso2.carbon.apimgt.core.models.analytics.SubscriptionCount)2 SubscriptionInfo (org.wso2.carbon.apimgt.core.models.analytics.SubscriptionInfo)2