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