use of org.wso2.carbon.apimgt.api.model.APIRevision in project carbon-apimgt by wso2.
the class ApiMgtDAO method deleteAPIProductRevision.
/**
* Delete API Product revision database records
*
* @param apiRevision content of the revision
* @throws APIManagementException if an error occurs when restoring an API revision
*/
public void deleteAPIProductRevision(APIRevision apiRevision) throws APIManagementException {
try (Connection connection = APIMgtDBUtil.getConnection()) {
try {
connection.setAutoCommit(false);
// Retrieve API ID
APIProductIdentifier apiProductIdentifier = APIUtil.getAPIProductIdentifierFromUUID(apiRevision.getApiUUID());
int apiId = getAPIID(apiRevision.getApiUUID(), connection);
int tenantId = APIUtil.getTenantId(APIUtil.replaceEmailDomainBack(apiProductIdentifier.getProviderName()));
// Removing related revision entries from AM_REVISION table
PreparedStatement removeAMRevisionStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.DELETE_API_REVISION);
removeAMRevisionStatement.setString(1, apiRevision.getRevisionUUID());
removeAMRevisionStatement.executeUpdate();
// Removing related revision entries from AM_API_PRODUCT_MAPPING table
PreparedStatement removeProductMappingsStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.REMOVE_REVISION_ENTRIES_IN_AM_API_PRODUCT_MAPPING_BY_REVISION_UUID);
removeProductMappingsStatement.setInt(1, apiId);
removeProductMappingsStatement.setString(2, apiRevision.getRevisionUUID());
removeProductMappingsStatement.executeUpdate();
// Removing related revision entries from AM_API_URL_MAPPING table
PreparedStatement removeURLMappingsStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.REMOVE_PRODUCT_REVISION_ENTRIES_IN_AM_API_URL_MAPPING_BY_REVISION_UUID);
removeURLMappingsStatement.setString(1, apiRevision.getRevisionUUID());
removeURLMappingsStatement.executeUpdate();
// Removing related revision entries from AM_API_CLIENT_CERTIFICATE table
PreparedStatement removeClientCertificatesStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.REMOVE_REVISION_ENTRIES_IN_AM_API_CLIENT_CERTIFICATE_BY_REVISION_UUID);
removeClientCertificatesStatement.setInt(1, apiId);
removeClientCertificatesStatement.setString(2, apiRevision.getRevisionUUID());
removeClientCertificatesStatement.executeUpdate();
// Removing related revision entries from AM_GRAPHQL_COMPLEXITY table
PreparedStatement removeGraphQLComplexityStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.REMOVE_REVISION_ENTRIES_IN_AM_GRAPHQL_COMPLEXITY_BY_REVISION_UUID);
removeGraphQLComplexityStatement.setInt(1, apiId);
removeGraphQLComplexityStatement.setString(2, apiRevision.getRevisionUUID());
removeGraphQLComplexityStatement.executeUpdate();
// Removing related revision entries for operation policies
deleteAllAPISpecificOperationPoliciesByAPIUUID(connection, apiRevision.getApiUUID(), apiRevision.getRevisionUUID());
connection.commit();
} catch (SQLException e) {
connection.rollback();
handleException("Failed to delete API Revision entry of API Product UUID " + apiRevision.getApiUUID(), e);
}
} catch (SQLException e) {
handleException("Failed to delete API Revision entry of API Product UUID " + apiRevision.getApiUUID(), e);
}
}
use of org.wso2.carbon.apimgt.api.model.APIRevision in project carbon-apimgt by wso2.
the class ApiMgtDAO method getAPIInfoByUUID.
/**
* Retrieve basic information about the given API by the UUID quering only from AM_API
*
* @param apiId UUID of the API
* @return basic information about the API
* @throws APIManagementException error while getting the API information from AM_API
*/
public APIInfo getAPIInfoByUUID(String apiId) throws APIManagementException {
try (Connection connection = APIMgtDBUtil.getConnection()) {
APIRevision apiRevision = getRevisionByRevisionUUID(connection, apiId);
String sql = SQLConstants.RETRIEVE_API_INFO_FROM_UUID;
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
if (apiRevision != null) {
preparedStatement.setString(1, apiRevision.getApiUUID());
} else {
preparedStatement.setString(1, apiId);
}
try (ResultSet resultSet = preparedStatement.executeQuery()) {
if (resultSet.next()) {
APIInfo.Builder apiInfoBuilder = new APIInfo.Builder();
apiInfoBuilder = apiInfoBuilder.id(resultSet.getString("API_UUID")).name(resultSet.getString("API_NAME")).version(resultSet.getString("API_VERSION")).provider(resultSet.getString("API_PROVIDER")).context(resultSet.getString("CONTEXT")).contextTemplate(resultSet.getString("CONTEXT_TEMPLATE")).status(APIUtil.getApiStatus(resultSet.getString("STATUS"))).apiType(resultSet.getString("API_TYPE")).createdBy(resultSet.getString("CREATED_BY")).createdTime(resultSet.getString("CREATED_TIME")).updatedBy(resultSet.getString("UPDATED_BY")).updatedTime(resultSet.getString("UPDATED_TIME")).revisionsCreated(resultSet.getInt("REVISIONS_CREATED")).isRevision(apiRevision != null);
if (apiRevision != null) {
apiInfoBuilder = apiInfoBuilder.apiTier(getAPILevelTier(connection, apiRevision.getApiUUID(), apiId));
} else {
apiInfoBuilder = apiInfoBuilder.apiTier(resultSet.getString("API_TIER"));
}
return apiInfoBuilder.build();
}
}
}
} catch (SQLException e) {
throw new APIManagementException("Error while retrieving apimgt connection", e, ExceptionCodes.INTERNAL_ERROR);
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.APIRevision in project carbon-apimgt by wso2.
the class ApiMgtDAO method setAPIProductFromDB.
/**
* Get product Id from the product name and the provider.
*
* @param product product identifier
* @throws APIManagementException exception
*/
public void setAPIProductFromDB(APIProduct product) throws APIManagementException {
APIProductIdentifier apiProductIdentifier = product.getId();
String currentApiUuid;
APIRevision apiRevision = ApiMgtDAO.getInstance().checkAPIUUIDIsARevisionUUID(product.getUuid());
if (apiRevision != null && apiRevision.getApiUUID() != null) {
currentApiUuid = apiRevision.getApiUUID();
} else {
currentApiUuid = product.getUuid();
}
try (Connection connection = APIMgtDBUtil.getConnection();
PreparedStatement prepStmt = connection.prepareStatement(SQLConstants.GET_API_PRODUCT_SQL)) {
prepStmt.setString(1, currentApiUuid);
try (ResultSet rs = prepStmt.executeQuery()) {
if (rs.next()) {
product.setProductId(rs.getInt("API_ID"));
product.setProductLevelPolicy(rs.getString("API_TIER"));
} else {
String msg = "Unable to find the API Product : " + apiProductIdentifier.getName() + "-" + APIUtil.replaceEmailDomainBack(apiProductIdentifier.getProviderName()) + "-" + apiProductIdentifier.getVersion() + " in the database";
throw new APIMgtResourceNotFoundException(msg);
}
}
} catch (SQLException e) {
handleException("Error while locating API Product: " + apiProductIdentifier.getName() + "-" + APIUtil.replaceEmailDomainBack(apiProductIdentifier.getProviderName()) + "-" + apiProductIdentifier.getVersion() + " from the database", e);
}
}
use of org.wso2.carbon.apimgt.api.model.APIRevision in project carbon-apimgt by wso2.
the class APIProviderImplTest method testRestoreAPIRevision.
/**
* This method tests restoring an API Revision to Working Copy
*
* @throws APIManagementException
*/
@Test
public void testRestoreAPIRevision() throws APIManagementException, APIPersistenceException {
ImportExportAPI importExportAPI = Mockito.mock(ImportExportAPI.class);
ArtifactSaver artifactSaver = Mockito.mock(ArtifactSaver.class);
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apiPersistenceInstance, apimgtDAO, importExportAPI, gatewayArtifactsMgtDAO, artifactSaver);
APIIdentifier apiId = new APIIdentifier("admin", "PizzaShackAPI", "1.0.0", "63e1e37e-a5b8-4be6-86a5-d6ae0749f131");
API api = new API(apiId);
api.setContext("/test");
api.setStatus(APIConstants.CREATED);
String apiPath = "/apimgt/applicationdata/provider/admin/PizzaShackAPI/1.0.0/api";
APIRevision apiRevision = new APIRevision();
apiRevision.setApiUUID("63e1e37e-a5b8-4be6-86a5-d6ae0749f131");
apiRevision.setDescription("test description revision 1");
Mockito.when(apimgtDAO.getRevisionCountByAPI(Mockito.anyString())).thenReturn(0);
Mockito.when(apimgtDAO.getMostRecentRevisionId(Mockito.anyString())).thenReturn(0);
Mockito.when(APIUtil.getAPIIdentifierFromUUID(Mockito.anyString())).thenReturn(apiId);
Mockito.when(APIUtil.getAPIPath(apiId)).thenReturn(apiPath);
Mockito.when(APIUtil.getTenantConfig(Mockito.anyString())).thenReturn(new JSONObject());
PowerMockito.when(apiPersistenceInstance.addAPIRevision(any(Organization.class), Mockito.anyString(), Mockito.anyInt())).thenReturn("b55e0fc3-9829-4432-b99e-02056dc91838");
try {
apiProvider.addAPIRevision(apiRevision, superTenantDomain);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
Mockito.when(apimgtDAO.getRevisionByRevisionUUID(Mockito.anyString())).thenReturn(apiRevision);
PowerMockito.doNothing().when(apiPersistenceInstance).restoreAPIRevision(any(Organization.class), Mockito.anyString(), Mockito.anyString(), Mockito.anyInt());
try {
apiProvider.restoreAPIRevision("63e1e37e-a5b8-4be6-86a5-d6ae0749f131", "b55e0fc3-9829-4432-b99e-02056dc91838", superTenantDomain);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
use of org.wso2.carbon.apimgt.api.model.APIRevision in project carbon-apimgt by wso2.
the class APIProviderImplTest method testGetAPIRevision.
/**
* This method tests adding a new API Revision and then retrieving API Revision by Revision UUID
*
* @throws APIManagementException
*/
@Test
public void testGetAPIRevision() throws APIManagementException, APIPersistenceException {
ImportExportAPI importExportAPI = Mockito.mock(ImportExportAPI.class);
ArtifactSaver artifactSaver = Mockito.mock(ArtifactSaver.class);
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apiPersistenceInstance, apimgtDAO, importExportAPI, gatewayArtifactsMgtDAO, artifactSaver);
APIIdentifier apiId = new APIIdentifier("admin", "PizzaShackAPI", "1.0.0", "63e1e37e-a5b8-4be6-86a5-d6ae0749f131");
API api = new API(apiId);
api.setContext("/test");
api.setStatus(APIConstants.CREATED);
String apiPath = "/apimgt/applicationdata/provider/admin/PizzaShackAPI/1.0.0/api";
APIRevision apiRevision = new APIRevision();
apiRevision.setApiUUID("63e1e37e-a5b8-4be6-86a5-d6ae0749f131");
apiRevision.setDescription("test description revision 1");
Mockito.when(apimgtDAO.getRevisionCountByAPI(Mockito.anyString())).thenReturn(0);
Mockito.when(apimgtDAO.getMostRecentRevisionId(Mockito.anyString())).thenReturn(0);
Mockito.when(APIUtil.getAPIIdentifierFromUUID(Mockito.anyString())).thenReturn(apiId);
Mockito.when(APIUtil.getAPIPath(apiId)).thenReturn(apiPath);
PowerMockito.when(apiPersistenceInstance.addAPIRevision(any(Organization.class), Mockito.anyString(), Mockito.anyInt())).thenReturn("b55e0fc3-9829-4432-b99e-02056dc91838");
Mockito.when(APIUtil.getTenantConfig(Mockito.anyString())).thenReturn(new JSONObject());
try {
apiProvider.addAPIRevision(apiRevision, superTenantDomain);
apiProvider.getAPIRevision("b55e0fc3-9829-4432-b99e-02056dc91838");
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
Aggregations