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));
}
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");
}
}
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");
}
}
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");
}
}
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;
}
Aggregations