use of org.wso2.carbon.apimgt.core.models.analytics.APIInfo 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;
}
use of org.wso2.carbon.apimgt.core.models.analytics.APIInfo in project carbon-apimgt by wso2.
the class AnalyzerImplTestCase method testGetAPIInfo.
@Test(description = "Get API Info test")
public void testGetAPIInfo() throws APIManagementException {
AnalyticsDAO analyticsDAO = Mockito.mock(AnalyticsDAO.class);
APIInfo apiInfo = new APIInfo();
List<APIInfo> apiInfos = new ArrayList<>();
apiInfos.add(apiInfo);
Analyzer analyzer = getAnalyzerImpl(analyticsDAO);
when(analyticsDAO.getAPIInfo(Instant.parse(FROM_TIMESTAMP), Instant.parse(TO_TIMESTAMP), null)).thenReturn(apiInfos);
List<APIInfo> apiInfoResult = analyzer.getAPIInfo(Instant.parse(FROM_TIMESTAMP), Instant.parse(TO_TIMESTAMP), null);
Assert.assertNotNull(apiInfoResult);
verify(analyticsDAO, Mockito.times(1)).getAPIInfo(Instant.parse(FROM_TIMESTAMP), Instant.parse(TO_TIMESTAMP), null);
// Error path
Mockito.when(analyticsDAO.getAPIInfo(Instant.parse(FROM_TIMESTAMP), Instant.parse(TO_TIMESTAMP), null)).thenThrow(APIMgtDAOException.class);
try {
analyzer.getAPIInfo(Instant.parse(FROM_TIMESTAMP), Instant.parse(TO_TIMESTAMP), null);
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Error occurred while fetching API information");
}
}
use of org.wso2.carbon.apimgt.core.models.analytics.APIInfo in project carbon-apimgt by wso2.
the class AnalyticsMappingUtilTestCase method fromAPIInfoListToDTOTest.
@Test
public void fromAPIInfoListToDTOTest() {
List<APIInfo> apiInfoList = new ArrayList<>();
apiInfoList.add(SampleTestObjectCreator.createRandomAPIInfoObject());
apiInfoList.add(SampleTestObjectCreator.createRandomAPIInfoObject());
apiInfoList.add(SampleTestObjectCreator.createRandomAPIInfoObject());
APIInfoListDTO apiInfoListDTO = AnalyticsMappingUtil.fromAPIInfoListToDTO(apiInfoList, ZoneOffset.UTC);
Assert.assertEquals(apiInfoList.size(), apiInfoListDTO.getList().size());
for (int i = 0; i < apiInfoList.size(); i++) {
Assert.assertEquals(epochToISO8601DateTime(apiInfoList.get(i).getCreatedTime(), ZoneOffset.UTC), apiInfoListDTO.getList().get(i).getTime());
Assert.assertEquals(apiInfoList.get(i).getName(), apiInfoListDTO.getList().get(i).getName());
Assert.assertEquals(apiInfoList.get(i).getContext(), apiInfoListDTO.getList().get(i).getContext());
Assert.assertEquals(apiInfoList.get(i).getDescription(), apiInfoListDTO.getList().get(i).getDescription());
Assert.assertEquals(apiInfoList.get(i).getLifeCycleStatus(), apiInfoListDTO.getList().get(i).getLifeCycleStatus());
Assert.assertEquals(apiInfoList.get(i).getProvider(), apiInfoListDTO.getList().get(i).getProvider());
Assert.assertEquals(apiInfoList.get(i).getVersion(), apiInfoListDTO.getList().get(i).getVersion());
Assert.assertEquals(apiInfoList.get(i).getId(), apiInfoListDTO.getList().get(i).getId());
}
}
use of org.wso2.carbon.apimgt.core.models.analytics.APIInfo in project carbon-apimgt by wso2.
the class ApiApiServiceImpl method apiListGet.
/**
* Get list of API Info
*
* @param startTime Filter for start time stamp
* @param endTime Filter for end time stamp
* @param createdBy Filter for created user
* @param request MSF4J request
* @return API List
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response apiListGet(String startTime, String endTime, String createdBy, Request request) throws NotFoundException {
String username = RestApiUtil.getLoggedInUsername(request);
try {
log.debug("Retrieving API information. [From: {} To: {} Created By:{} ]", startTime, endTime, createdBy);
Analyzer analyzer = RestApiUtil.getAnalyzer(username);
ZoneId requestTimezone = RestApiUtil.getRequestTimeZone(startTime);
List<APIInfo> apiInfoList = analyzer.getAPIInfo(fromISO8601ToInstant(startTime), fromISO8601ToInstant(endTime), createdBy);
APIInfoListDTO apiInfoListDTO = AnalyticsMappingUtil.fromAPIInfoListToDTO(apiInfoList, requestTimezone);
return Response.ok().entity(apiInfoListDTO).build();
} catch (APIManagementException e) {
String errorMessage = "Error while retrieving API information";
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.carbon.apimgt.core.models.analytics.APIInfo in project carbon-apimgt by wso2.
the class AnalyticsMappingUtil method fromAPIInfoToDTO.
private static APIInfoDTO fromAPIInfoToDTO(APIInfo apiInfo, ZoneId zoneId) {
APIInfoDTO apiInfoDTO = new APIInfoDTO();
apiInfoDTO.setId(apiInfo.getId());
apiInfoDTO.setName(apiInfo.getName());
apiInfoDTO.setVersion(apiInfo.getVersion());
apiInfoDTO.setContext(apiInfo.getContext());
apiInfoDTO.setDescription(apiInfo.getDescription());
apiInfoDTO.setLifeCycleStatus(apiInfo.getLifeCycleStatus());
apiInfoDTO.setProvider(apiInfo.getProvider());
apiInfoDTO.setWorkflowStatus(apiInfo.getWorkflowStatus());
apiInfoDTO.setTime(epochToISO8601DateTime(apiInfo.getCreatedTime(), zoneId));
return apiInfoDTO;
}
Aggregations