Search in sources :

Example 96 with Group

use of org.wso2.charon3.core.objects.Group in project carbon-apimgt by wso2.

the class SampleTestObjectCreator method createDefaultCustomPolicy.

/**
 * Create a custom policy.
 *
 * @return CustomPolicy object
 */
protected static CustomPolicy createDefaultCustomPolicy() {
    CustomPolicy customPolicy = new CustomPolicy(SAMPLE_CUSTOM_RULE);
    customPolicy.setKeyTemplate("$userId");
    String siddhiQuery = "FROM RequestStream SELECT userId, ( userId == 'admin@carbon.super' ) AS isEligible , " + "str:concat('admin@carbon.super','') as throttleKey INSERT INTO EligibilityStream;" + "FROM EligibilityStream[isEligible==true]#throttler:timeBatch(1 min) SELECT throttleKey, " + "(count(userId) >= 5 as isThrottled, expiryTimeStamp group by throttleKey INSERT ALL EVENTS into " + "ResultStream;";
    customPolicy.setSiddhiQuery(siddhiQuery);
    customPolicy.setDescription("Sample custom policy");
    return customPolicy;
}
Also used : CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy)

Example 97 with Group

use of org.wso2.charon3.core.objects.Group in project carbon-apimgt by wso2.

the class AnalyticsDAOImpl method getAPICount.

/**
 * @see AnalyticsDAO#getAPICount(Instant, Instant, String)
 */
@Override
@SuppressFBWarnings("SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING")
public List<APICount> getAPICount(Instant fromTime, Instant toTime, String createdBy) throws APIMgtDAOException {
    final String query;
    if (StringUtils.isNotEmpty(createdBy)) {
        query = "SELECT COUNT(UUID) AS count, CREATED_TIME AS time " + "FROM AM_API " + "WHERE (CREATED_TIME BETWEEN ? AND ?) " + "AND CREATED_BY = ? " + "GROUP BY CREATED_TIME " + "ORDER BY CREATED_TIME ASC";
    } else {
        query = "SELECT COUNT(UUID) AS count, CREATED_TIME AS time " + "FROM AM_API " + "WHERE (CREATED_TIME BETWEEN ? AND ?) " + "GROUP BY CREATED_TIME " + "ORDER BY CREATED_TIME ASC";
    }
    List<APICount> apiInfoList = new ArrayList<>();
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(query)) {
        statement.setTimestamp(1, Timestamp.from(fromTime));
        statement.setTimestamp(2, Timestamp.from(toTime));
        if (StringUtils.isNotEmpty(createdBy)) {
            statement.setString(3, createdBy);
        }
        log.debug("Executing query: {} ", query);
        statement.execute();
        try (ResultSet rs = statement.getResultSet()) {
            long count = 0;
            while (rs.next()) {
                APICount apiCount = new APICount();
                count += rs.getLong("count");
                apiCount.setTimestamp(rs.getTimestamp("time").getTime());
                apiCount.setCount(count);
                apiInfoList.add(apiCount);
            }
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException("Error while creating database connection/prepared-statement", e);
    }
    return apiInfoList;
}
Also used : APICount(org.wso2.carbon.apimgt.core.models.analytics.APICount) APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 98 with Group

use of org.wso2.charon3.core.objects.Group in project carbon-apimgt by wso2.

the class AnalyticsDAOImpl method getSubscriptionCount.

/**
 * @see AnalyticsDAO#getSubscriptionCount(Instant, Instant, String)
 */
@Override
@SuppressFBWarnings("SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING")
public List<SubscriptionCount> getSubscriptionCount(Instant fromTimestamp, Instant toTimestamp, String createdBy) throws APIMgtDAOException {
    final String query;
    if (StringUtils.isNotEmpty(createdBy)) {
        query = "SELECT COUNT(subs.UUID) AS COUNT, subs.CREATED_TIME AS time " + "FROM AM_SUBSCRIPTION subs, AM_API  api  " + "WHERE (subs.CREATED_TIME BETWEEN ? AND ?) " + "AND  subs.api_id=api.uuid " + "AND subs.CREATED_BY= ? " + "GROUP BY subs.CREATED_TIME ORDER BY subs.CREATED_TIME ASC";
    } else {
        query = "SELECT COUNT(subs.UUID) AS COUNT, subs.CREATED_TIME AS time " + "FROM AM_SUBSCRIPTION subs, AM_API  api  " + "WHERE (subs.CREATED_TIME BETWEEN ? AND ?) " + "AND  subs.api_id=api.uuid " + "GROUP BY subs.CREATED_TIME ORDER BY subs.CREATED_TIME ASC";
    }
    List<SubscriptionCount> subscriptionCountList = new ArrayList<>();
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(query)) {
        statement.setTimestamp(1, Timestamp.from(fromTimestamp));
        statement.setTimestamp(2, Timestamp.from(toTimestamp));
        if (StringUtils.isNotEmpty(createdBy)) {
            statement.setString(3, createdBy);
        }
        log.debug("Executing query: {} ", query);
        statement.execute();
        try (ResultSet rs = statement.getResultSet()) {
            while (rs.next()) {
                SubscriptionCount subscriptionCount = new SubscriptionCount();
                subscriptionCount.setTimestamp(rs.getTimestamp("TIME").getTime());
                subscriptionCount.setCount(rs.getInt("COUNT"));
                subscriptionCountList.add(subscriptionCount);
            }
        }
    } catch (SQLException e) {
        String errorMsg = "Error while creating database connection/prepared-statement";
        throw new APIMgtDAOException(errorMsg, e);
    }
    return subscriptionCountList;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) APISubscriptionCount(org.wso2.carbon.apimgt.core.models.analytics.APISubscriptionCount) SubscriptionCount(org.wso2.carbon.apimgt.core.models.analytics.SubscriptionCount) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 99 with Group

use of org.wso2.charon3.core.objects.Group in project carbon-apimgt by wso2.

the class AnalyticsDAOImpl method getAPISubscriptionCount.

/**
 * @see AnalyticsDAO#getAPISubscriptionCount(Instant, Instant, String)
 */
@Override
@SuppressFBWarnings("SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING")
public List<APISubscriptionCount> getAPISubscriptionCount(Instant fromTime, Instant toTime, String apiId) throws APIMgtDAOException {
    final String query;
    if (StringUtils.isNotEmpty(apiId)) {
        query = "SELECT api.UUID,api.NAME,api.VERSION,api.PROVIDER,count(subs.UUID) as COUNT " + "FROM AM_SUBSCRIPTION subs,AM_API api " + "WHERE api.UUID=subs.API_ID " + "AND (subs.CREATED_TIME BETWEEN ? AND ?) " + "AND subs.SUB_STATUS = 'ACTIVE' " + "AND api.UUID=? " + "GROUP BY api.UUID,api.NAME,api.VERSION,api.PROVIDER";
    } else {
        query = "SELECT api.UUID,api.NAME,api.VERSION,api.PROVIDER,count(subs.UUID) as COUNT " + "FROM AM_SUBSCRIPTION subs,AM_API api " + "WHERE api.UUID=subs.API_ID " + "AND (subs.CREATED_TIME BETWEEN ? AND ?) " + "AND subs.SUB_STATUS = 'ACTIVE' " + "GROUP BY api.UUID,api.NAME,api.VERSION,api.PROVIDER";
    }
    List<APISubscriptionCount> apiSubscriptionCountList = new ArrayList<>();
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(query)) {
        statement.setTimestamp(1, Timestamp.from(fromTime));
        statement.setTimestamp(2, Timestamp.from(toTime));
        if (StringUtils.isNotEmpty(apiId)) {
            statement.setString(3, apiId);
        }
        log.debug("Executing query: {} ", query);
        statement.execute();
        try (ResultSet rs = statement.getResultSet()) {
            while (rs.next()) {
                APISubscriptionCount apiSubscriptionCount = new APISubscriptionCount();
                apiSubscriptionCount.setId(rs.getString("UUID"));
                apiSubscriptionCount.setName(rs.getString("NAME"));
                apiSubscriptionCount.setVersion(rs.getString("VERSION"));
                apiSubscriptionCount.setProvider(rs.getString("PROVIDER"));
                apiSubscriptionCount.setCount(rs.getInt("COUNT"));
                apiSubscriptionCountList.add(apiSubscriptionCount);
            }
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException("Error while creating database connection/prepared-statement", e);
    }
    return apiSubscriptionCountList;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APISubscriptionCount(org.wso2.carbon.apimgt.core.models.analytics.APISubscriptionCount) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 100 with Group

use of org.wso2.charon3.core.objects.Group in project carbon-apimgt by wso2.

the class ApiDAOImplIT method testGetAPIsWhenAPIHasNoPermissionsAssigned.

@Test(description = "Tests getting the APIs when the API has no permissions assigned")
public void testGetAPIsWhenAPIHasNoPermissionsAssigned() throws Exception {
    ApiDAO apiDAO = DAOFactory.getApiDAO();
    Set<String> rolesOfUser = new HashSet<>();
    // The ID here is the group ID of the provider of the API. This ID is not assigned permissions for the API
    rolesOfUser.add(ALTERNATIVE_USER_ROLE_ID);
    // But this user is not the provider of the API
    List<API> apiList = apiDAO.getAPIs(rolesOfUser, ALTERNATIVE_USER);
    Assert.assertTrue(apiList.isEmpty());
    API.APIBuilder builder = SampleTestObjectCreator.createDefaultAPI().permissionMap(null);
    API api1 = builder.build();
    testAddGetEndpoint();
    apiDAO.addAPI(api1);
    apiList = apiDAO.getAPIs(rolesOfUser, ALTERNATIVE_USER);
    List<API> expectedAPIs = new ArrayList<>();
    expectedAPIs.add(SampleTestObjectCreator.copyAPISummary(api1));
    // Since the API has no permissions assigned specifically, it is visible to every one
    Assert.assertTrue(apiList.size() == 1);
    Assert.assertTrue(APIUtils.isListsEqualIgnoreOrder(apiList, expectedAPIs, new APIComparator()), TestUtil.printDiff(apiList, expectedAPIs));
}
Also used : ArrayList(java.util.ArrayList) APIComparator(org.wso2.carbon.apimgt.core.util.APIComparator) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)128 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)97 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)94 Event (org.wso2.siddhi.core.event.Event)87 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)78 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)51 Query (org.wso2.siddhi.query.api.execution.query.Query)17 ArrayList (java.util.ArrayList)16 CharonException (org.wso2.charon3.core.exceptions.CharonException)15 SCIMResponse (org.wso2.charon3.core.protocol.SCIMResponse)13 StreamCallback (org.wso2.siddhi.core.stream.output.StreamCallback)12 HashMap (java.util.HashMap)8 BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)8 SCIMResourceTypeSchema (org.wso2.charon3.core.schema.SCIMResourceTypeSchema)8 InternalErrorException (org.wso2.charon3.core.exceptions.InternalErrorException)7 UserManager (org.wso2.charon3.core.extensions.UserManager)7 Group (org.wso2.charon3.core.objects.Group)7 ApiOperation (io.swagger.annotations.ApiOperation)6 ApiResponses (io.swagger.annotations.ApiResponses)6 Produces (javax.ws.rs.Produces)6