Search in sources :

Example 1 with SubscriptionCount

use of org.wso2.carbon.apimgt.core.models.analytics.SubscriptionCount in project carbon-apimgt by wso2.

the class AnalyzerImplTestCase method testGetSubscriptionCount.

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

Example 2 with SubscriptionCount

use of org.wso2.carbon.apimgt.core.models.analytics.SubscriptionCount in project carbon-apimgt by wso2.

the class AnalyticsMappingUtilTestCase method fromSubscriptionCountListToDTOTest.

@Test
public void fromSubscriptionCountListToDTOTest() {
    List<SubscriptionCount> subscriptionCountList = new ArrayList<>();
    subscriptionCountList.add(SampleTestObjectCreator.createRandomSubscriptionCountObject());
    subscriptionCountList.add(SampleTestObjectCreator.createRandomSubscriptionCountObject());
    subscriptionCountList.add(SampleTestObjectCreator.createRandomSubscriptionCountObject());
    SubscriptionCountListDTO subscriptionCountListToDTO = AnalyticsMappingUtil.fromSubscriptionCountListToDTO(subscriptionCountList, ZoneOffset.UTC);
    Assert.assertEquals(subscriptionCountList.size(), subscriptionCountListToDTO.getList().size());
    for (int i = 0; i < subscriptionCountList.size(); i++) {
        Assert.assertEquals(Long.valueOf(subscriptionCountList.get(i).getCount()), Long.valueOf(subscriptionCountListToDTO.getList().get(i).getCount()));
        Assert.assertEquals(epochToISO8601DateTime(subscriptionCountList.get(i).getTimestamp(), ZoneOffset.UTC), subscriptionCountListToDTO.getList().get(i).getTime());
    }
}
Also used : SubscriptionCountListDTO(org.wso2.carbon.apimgt.rest.api.analytics.dto.SubscriptionCountListDTO) APISubscriptionCountListDTO(org.wso2.carbon.apimgt.rest.api.analytics.dto.APISubscriptionCountListDTO) ArrayList(java.util.ArrayList) APISubscriptionCount(org.wso2.carbon.apimgt.core.models.analytics.APISubscriptionCount) SubscriptionCount(org.wso2.carbon.apimgt.core.models.analytics.SubscriptionCount) Test(org.testng.annotations.Test)

Example 3 with SubscriptionCount

use of org.wso2.carbon.apimgt.core.models.analytics.SubscriptionCount 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 4 with SubscriptionCount

use of org.wso2.carbon.apimgt.core.models.analytics.SubscriptionCount in project carbon-apimgt by wso2.

the class AnalyticsDAOImplIT method testGetSubscriptionCount.

@Test
public void testGetSubscriptionCount() throws Exception {
    Instant fromTimeStamp = Instant.ofEpochMilli(System.currentTimeMillis());
    API testAPI = TestUtil.addTestAPI();
    Application testApplication = TestUtil.addTestApplication();
    TestUtil.subscribeToAPI(testAPI, testApplication);
    Instant toTimeStamp = Instant.ofEpochMilli(System.currentTimeMillis() + DELAY_TIME);
    AnalyticsDAO analyticsDAO = DAOFactory.getAnalyticsDAO();
    List<SubscriptionCount> subscriptionCount = analyticsDAO.getSubscriptionCount(fromTimeStamp, toTimeStamp, null);
    Assert.assertEquals(subscriptionCount.size(), 1);
}
Also used : AnalyticsDAO(org.wso2.carbon.apimgt.core.dao.AnalyticsDAO) Instant(java.time.Instant) API(org.wso2.carbon.apimgt.core.models.API) APISubscriptionCount(org.wso2.carbon.apimgt.core.models.analytics.APISubscriptionCount) SubscriptionCount(org.wso2.carbon.apimgt.core.models.analytics.SubscriptionCount) Application(org.wso2.carbon.apimgt.core.models.Application) Test(org.testng.annotations.Test)

Example 5 with SubscriptionCount

use of org.wso2.carbon.apimgt.core.models.analytics.SubscriptionCount in project carbon-apimgt by wso2.

the class AnalyticsDAOImplIT method testGetSubscriptionCountPerAPI.

@Test
public void testGetSubscriptionCountPerAPI() throws Exception {
    Instant fromTimeStamp = Instant.ofEpochMilli(System.currentTimeMillis());
    API testAPI = TestUtil.addTestAPI();
    API testAPI1 = TestUtil.addCustomAPI("TestAPI1", "1.0.0", "test1");
    API testAPI2 = TestUtil.addCustomAPI("TestAPI2", "1.0.0", "test2");
    Application testApplication = TestUtil.addTestApplication();
    Application testApplication2 = TestUtil.addCustomApplication("APP2", "admin");
    TestUtil.subscribeToAPI(testAPI, testApplication);
    TestUtil.subscribeToAPI(testAPI, testApplication2);
    TestUtil.subscribeToAPI(testAPI1, testApplication2);
    TestUtil.subscribeToAPI(testAPI1, testApplication);
    TestUtil.subscribeToAPI(testAPI2, testApplication2);
    Instant toTimeStamp = Instant.ofEpochMilli(System.currentTimeMillis() + DELAY_TIME);
    AnalyticsDAO analyticsDAO = DAOFactory.getAnalyticsDAO();
    List<APISubscriptionCount> subscriptionCount = analyticsDAO.getAPISubscriptionCount(fromTimeStamp, toTimeStamp, null);
    Assert.assertEquals(subscriptionCount.size(), 3);
}
Also used : AnalyticsDAO(org.wso2.carbon.apimgt.core.dao.AnalyticsDAO) APISubscriptionCount(org.wso2.carbon.apimgt.core.models.analytics.APISubscriptionCount) Instant(java.time.Instant) API(org.wso2.carbon.apimgt.core.models.API) Application(org.wso2.carbon.apimgt.core.models.Application) Test(org.testng.annotations.Test)

Aggregations

APISubscriptionCount (org.wso2.carbon.apimgt.core.models.analytics.APISubscriptionCount)7 SubscriptionCount (org.wso2.carbon.apimgt.core.models.analytics.SubscriptionCount)7 ArrayList (java.util.ArrayList)4 Test (org.testng.annotations.Test)4 AnalyticsDAO (org.wso2.carbon.apimgt.core.dao.AnalyticsDAO)3 SubscriptionCountListDTO (org.wso2.carbon.apimgt.rest.api.analytics.dto.SubscriptionCountListDTO)3 Instant (java.time.Instant)2 Analyzer (org.wso2.carbon.apimgt.core.api.Analyzer)2 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)2 API (org.wso2.carbon.apimgt.core.models.API)2 Application (org.wso2.carbon.apimgt.core.models.Application)2 APISubscriptionCountListDTO (org.wso2.carbon.apimgt.rest.api.analytics.dto.APISubscriptionCountListDTO)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 ZoneId (java.time.ZoneId)1 Random (java.util.Random)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1