use of org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.ArtifactSaver in project carbon-apimgt by wso2.
the class APIGatewayManager method unDeployFromGateway.
public void unDeployFromGateway(APIProduct apiProduct, String tenantDomain, Set<API> associatedAPIs, Set<String> gatewaysToRemove) throws APIManagementException {
String apiProductUuid = apiProduct.getUuid();
APIProductIdentifier apiProductIdentifier = apiProduct.getId();
try {
if (artifactSaver != null) {
artifactSaver.removeArtifact(apiProductUuid, apiProductIdentifier.getName(), apiProductIdentifier.getVersion(), APIConstants.API_PRODUCT_REVISION, tenantDomain);
}
GatewayArtifactsMgtDAO.getInstance().deleteGatewayArtifact(apiProductUuid, APIConstants.API_PRODUCT_REVISION);
GatewayArtifactsMgtDAO.getInstance().removePublishedGatewayLabels(apiProductUuid, APIConstants.API_PRODUCT_REVISION);
} catch (ArtifactSynchronizerException e) {
throw new APIManagementException("API " + apiProductIdentifier + "couldn't get unDeployed", e);
}
if (debugEnabled) {
log.debug("Status of " + apiProductIdentifier + " has been updated to DB");
}
sendUnDeploymentEvent(apiProduct, tenantDomain, gatewaysToRemove, associatedAPIs);
}
use of org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.ArtifactSaver 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.impl.gatewayartifactsynchronizer.ArtifactSaver in project carbon-apimgt by wso2.
the class APIProviderImplTest method testGetAPIRevisions.
/**
* This method tests adding a new API Revision and then retrieving API Revisions by API UUID
*
* @throws APIManagementException
*/
@Test
public void testGetAPIRevisions() throws APIManagementException, APIPersistenceException, ArtifactSynchronizerException {
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);
apiProvider.getAPIRevisions("63e1e37e-a5b8-4be6-86a5-d6ae0749f131");
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
use of org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.ArtifactSaver 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());
}
}
use of org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.ArtifactSaver in project carbon-apimgt by wso2.
the class APIProviderImpl method addAPIRevision.
/**
* Adds a new APIRevision to an existing API
*
* @param apiRevision APIRevision
* @throws APIManagementException if failed to add APIRevision
*/
@Override
public String addAPIRevision(APIRevision apiRevision, String organization) throws APIManagementException {
int revisionCountPerAPI = apiMgtDAO.getRevisionCountByAPI(apiRevision.getApiUUID());
int maxRevisionCount = getMaxRevisionCount(organization);
if (revisionCountPerAPI >= maxRevisionCount) {
String errorMessage = "Maximum number of revisions per API has reached. " + "Need to remove stale revision to create a new Revision for API with API UUID:" + apiRevision.getApiUUID();
throw new APIManagementException(errorMessage, ExceptionCodes.from(ExceptionCodes.MAXIMUM_REVISIONS_REACHED, apiRevision.getApiUUID()));
}
int revisionId = apiMgtDAO.getMostRecentRevisionId(apiRevision.getApiUUID()) + 1;
apiRevision.setId(revisionId);
APIIdentifier apiId = APIUtil.getAPIIdentifierFromUUID(apiRevision.getApiUUID());
if (apiId == null) {
throw new APIMgtResourceNotFoundException("Couldn't retrieve existing API with API UUID: " + apiRevision.getApiUUID(), ExceptionCodes.from(ExceptionCodes.API_NOT_FOUND, apiRevision.getApiUUID()));
}
apiId.setUuid(apiRevision.getApiUUID());
String revisionUUID;
try {
revisionUUID = apiPersistenceInstance.addAPIRevision(new Organization(organization), apiId.getUUID(), revisionId);
} catch (APIPersistenceException e) {
String errorMessage = "Failed to add revision registry artifacts";
throw new APIManagementException(errorMessage, ExceptionCodes.from(ExceptionCodes.ERROR_CREATING_API_REVISION, apiRevision.getApiUUID()));
}
if (StringUtils.isEmpty(revisionUUID)) {
String errorMessage = "Failed to retrieve revision uuid";
throw new APIManagementException(errorMessage, ExceptionCodes.from(ExceptionCodes.API_REVISION_UUID_NOT_FOUND));
}
apiRevision.setRevisionUUID(revisionUUID);
apiMgtDAO.addAPIRevision(apiRevision);
if (importExportAPI != null) {
try {
File artifact = importExportAPI.exportAPI(apiRevision.getApiUUID(), revisionUUID, true, ExportFormat.JSON, false, true, organization);
// Keeping the organization as tenant domain since MG does not support organization-wise deployment
// Artifacts will be deployed in ST for all organizations
gatewayArtifactsMgtDAO.addGatewayAPIArtifactAndMetaData(apiRevision.getApiUUID(), apiId.getApiName(), apiId.getVersion(), apiRevision.getRevisionUUID(), tenantDomain, APIConstants.HTTP_PROTOCOL, artifact);
if (artifactSaver != null) {
// Keeping the organization as tenant domain since MG does not support organization-wise deployment
// Artifacts will be deployed in ST for all organizations
artifactSaver.saveArtifact(apiRevision.getApiUUID(), apiId.getApiName(), apiId.getVersion(), apiRevision.getRevisionUUID(), tenantDomain, artifact);
}
} catch (APIImportExportException | ArtifactSynchronizerException e) {
throw new APIManagementException("Error while Store the Revision Artifact", ExceptionCodes.from(ExceptionCodes.API_REVISION_UUID_NOT_FOUND));
}
}
return revisionUUID;
}
Aggregations