Search in sources :

Example 6 with AnalyticsDAO

use of org.wso2.carbon.apimgt.core.dao.AnalyticsDAO in project carbon-apimgt by wso2.

the class AnalyticsDAOImplIT method testGetAPIList.

@Test
public void testGetAPIList() throws Exception {
    Instant fromTimeStamp = Instant.ofEpochMilli(System.currentTimeMillis());
    API testAPI1 = TestUtil.addCustomAPI("Name1", "1.0.0", "sample1");
    API testAPI2 = TestUtil.addCustomAPI("Name2", "1.0.0", "sample2");
    Instant toTimeStamp = Instant.ofEpochMilli(System.currentTimeMillis() + DELAY_TIME);
    AnalyticsDAO analyticsDAO = DAOFactory.getAnalyticsDAO();
    List<APIInfo> apiInfoList = analyticsDAO.getAPIInfo(fromTimeStamp, toTimeStamp, null);
    Assert.assertEquals(apiInfoList.size(), 2);
    APIInfo apiInfo1 = apiInfoList.get(0);
    APIInfo apiInfo2 = apiInfoList.get(1);
    API result1 = new API.APIBuilder(apiInfo1.getProvider(), apiInfo1.getName(), apiInfo1.getVersion()).build();
    API result2 = new API.APIBuilder(apiInfo2.getProvider(), apiInfo2.getName(), apiInfo2.getVersion()).build();
    Assert.assertTrue(TestUtil.testAPIEqualsLazy(testAPI1, result1));
    Assert.assertTrue(TestUtil.testAPIEqualsLazy(testAPI2, result2));
}
Also used : AnalyticsDAO(org.wso2.carbon.apimgt.core.dao.AnalyticsDAO) Instant(java.time.Instant) APIInfo(org.wso2.carbon.apimgt.core.models.analytics.APIInfo) API(org.wso2.carbon.apimgt.core.models.API) Test(org.testng.annotations.Test)

Example 7 with AnalyticsDAO

use of org.wso2.carbon.apimgt.core.dao.AnalyticsDAO in project carbon-apimgt by wso2.

the class AnalyzerImplTestCase method testGetSubscrptionInfo.

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

Example 8 with AnalyticsDAO

use of org.wso2.carbon.apimgt.core.dao.AnalyticsDAO 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 9 with AnalyticsDAO

use of org.wso2.carbon.apimgt.core.dao.AnalyticsDAO in project carbon-apimgt by wso2.

the class AnalyzerImplTestCase method testGetApplicationCount.

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

Example 10 with AnalyticsDAO

use of org.wso2.carbon.apimgt.core.dao.AnalyticsDAO in project carbon-apimgt by wso2.

the class AnalyticsDAOImpl method getAPIInfo.

/**
 * @see AnalyticsDAO#getAPIInfo(Instant, Instant, String)
 */
@Override
@SuppressFBWarnings("SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING")
public List<APIInfo> getAPIInfo(Instant fromTimestamp, Instant toTimestamp, String createdBy) throws APIMgtDAOException {
    final String query;
    if (StringUtils.isNotEmpty(createdBy)) {
        query = "SELECT UUID,PROVIDER,NAME,CONTEXT,VERSION,CREATED_TIME,CURRENT_LC_STATUS, LC_WORKFLOW_STATUS  " + "FROM AM_API " + "WHERE CREATED_TIME BETWEEN ? AND ? " + "AND CREATED_BY = ? " + "ORDER BY CREATED_TIME ASC";
    } else {
        query = "SELECT UUID,PROVIDER,NAME,CONTEXT,VERSION,CREATED_TIME,CURRENT_LC_STATUS, LC_WORKFLOW_STATUS  " + "FROM AM_API " + "WHERE CREATED_TIME BETWEEN ? AND ? " + "ORDER BY CREATED_TIME ASC";
    }
    List<APIInfo> apiInfoList = 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()) {
                APIInfo apiInfo = new APIInfo();
                apiInfo.setId(rs.getString("UUID"));
                apiInfo.setProvider(rs.getString("PROVIDER"));
                apiInfo.setName(rs.getString("NAME"));
                apiInfo.setContext(rs.getString("CONTEXT"));
                apiInfo.setVersion(rs.getString("VERSION"));
                apiInfo.setCreatedTime(rs.getTimestamp("CREATED_TIME").getTime());
                apiInfo.setLifeCycleStatus(rs.getString("CURRENT_LC_STATUS"));
                apiInfo.setWorkflowStatus(rs.getString("LC_WORKFLOW_STATUS"));
                apiInfoList.add(apiInfo);
            }
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException("Error while creating database connection/prepared-statement", e);
    }
    return apiInfoList;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) APIInfo(org.wso2.carbon.apimgt.core.models.analytics.APIInfo) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

AnalyticsDAO (org.wso2.carbon.apimgt.core.dao.AnalyticsDAO)14 ArrayList (java.util.ArrayList)12 Test (org.testng.annotations.Test)12 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)8 Connection (java.sql.Connection)7 SQLException (java.sql.SQLException)7 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)6 PreparedStatement (java.sql.PreparedStatement)6 ResultSet (java.sql.ResultSet)6 Instant (java.time.Instant)6 Analyzer (org.wso2.carbon.apimgt.core.api.Analyzer)6 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)6 APISubscriptionCount (org.wso2.carbon.apimgt.core.models.analytics.APISubscriptionCount)6 API (org.wso2.carbon.apimgt.core.models.API)4 Application (org.wso2.carbon.apimgt.core.models.Application)3 APICount (org.wso2.carbon.apimgt.core.models.analytics.APICount)3 APIInfo (org.wso2.carbon.apimgt.core.models.analytics.APIInfo)3 ApplicationCount (org.wso2.carbon.apimgt.core.models.analytics.ApplicationCount)3 SubscriptionCount (org.wso2.carbon.apimgt.core.models.analytics.SubscriptionCount)3 SubscriptionInfo (org.wso2.carbon.apimgt.core.models.analytics.SubscriptionInfo)3