Search in sources :

Example 11 with APIInfo

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

Example 12 with APIInfo

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");
    }
}
Also used : AnalyticsDAO(org.wso2.carbon.apimgt.core.dao.AnalyticsDAO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) APIInfo(org.wso2.carbon.apimgt.core.models.analytics.APIInfo) ArrayList(java.util.ArrayList) Analyzer(org.wso2.carbon.apimgt.core.api.Analyzer) Test(org.testng.annotations.Test)

Example 13 with APIInfo

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());
    }
}
Also used : APIInfo(org.wso2.carbon.apimgt.core.models.analytics.APIInfo) ArrayList(java.util.ArrayList) APIInfoListDTO(org.wso2.carbon.apimgt.rest.api.analytics.dto.APIInfoListDTO) Test(org.testng.annotations.Test)

Example 14 with APIInfo

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();
    }
}
Also used : ZoneId(java.time.ZoneId) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIInfo(org.wso2.carbon.apimgt.core.models.analytics.APIInfo) Analyzer(org.wso2.carbon.apimgt.core.api.Analyzer) APIInfoListDTO(org.wso2.carbon.apimgt.rest.api.analytics.dto.APIInfoListDTO)

Example 15 with APIInfo

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;
}
Also used : APIInfoDTO(org.wso2.carbon.apimgt.rest.api.analytics.dto.APIInfoDTO)

Aggregations

ArrayList (java.util.ArrayList)9 APIInfo (org.wso2.carbon.apimgt.core.models.analytics.APIInfo)7 API (org.wso2.carbon.apimgt.core.models.API)5 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)4 Test (org.testng.annotations.Test)3 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)3 APIInfoListDTO (org.wso2.carbon.apimgt.rest.api.analytics.dto.APIInfoListDTO)3 Info (io.swagger.models.Info)2 Swagger (io.swagger.models.Swagger)2 SwaggerParser (io.swagger.parser.SwaggerParser)2 HashMap (java.util.HashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Analyzer (org.wso2.carbon.apimgt.core.api.Analyzer)2 AnalyticsDAO (org.wso2.carbon.apimgt.core.dao.AnalyticsDAO)2 APIResource (org.wso2.carbon.apimgt.core.models.APIResource)2 UriTemplate (org.wso2.carbon.apimgt.core.models.UriTemplate)2 APIInfoDTO (org.wso2.carbon.apimgt.rest.api.analytics.dto.APIInfoDTO)2 ServiceMethodInfo (org.wso2.msf4j.ServiceMethodInfo)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 Contact (io.swagger.models.Contact)1