use of org.wso2.carbon.apimgt.api.model.APIIdentifier in project carbon-apimgt by wso2.
the class ApiMgtDAO method addExternalAPIStoresDetails.
/**
* Store external APIStore details to which APIs successfully published
*
* @param uuid API uuid
* @param apiStoreSet APIStores set
* @return added/failed
* @throws APIManagementException
*/
public boolean addExternalAPIStoresDetails(String uuid, Set<APIStore> apiStoreSet) throws APIManagementException {
Connection conn = null;
PreparedStatement ps = null;
boolean state = false;
try {
conn = APIMgtDBUtil.getConnection();
conn.setAutoCommit(false);
// This query to add external APIStores to database table
String sqlQuery = SQLConstants.ADD_EXTERNAL_API_STORE_SQL;
// Get API Id
int apiIdentifier;
apiIdentifier = getAPIID(uuid, conn);
if (apiIdentifier == -1) {
String msg = "Could not load API record for API with uuid: " + uuid;
log.error(msg);
}
ps = conn.prepareStatement(sqlQuery);
for (Object storeObject : apiStoreSet) {
APIStore store = (APIStore) storeObject;
ps.setInt(1, apiIdentifier);
ps.setString(2, store.getName());
ps.setString(3, store.getDisplayName());
ps.setString(4, store.getEndpoint());
ps.setString(5, store.getType());
ps.setTimestamp(6, new Timestamp(System.currentTimeMillis()));
ps.addBatch();
}
ps.executeBatch();
conn.commit();
state = true;
} catch (SQLException e) {
if (conn != null) {
try {
conn.rollback();
} catch (SQLException e1) {
log.error("Failed to rollback storing external apistore details ", e1);
}
}
log.error("Failed to store external apistore details", e);
state = false;
} catch (APIManagementException e) {
log.error("Failed to store external apistore details", e);
state = false;
} finally {
APIMgtDBUtil.closeAllConnections(ps, conn, null);
}
return state;
}
use of org.wso2.carbon.apimgt.api.model.APIIdentifier in project carbon-apimgt by wso2.
the class ApiMgtDAO method removeSubscription.
/**
* Removes the subscription entry from AM_SUBSCRIPTIONS for identifier.
*
* @param identifier Identifier
* @param applicationId ID of the application which has the subscription
* @throws APIManagementException
*/
public void removeSubscription(Identifier identifier, int applicationId) throws APIManagementException {
Connection conn = null;
ResultSet resultSet = null;
PreparedStatement ps = null;
int id = -1;
String uuid;
try {
conn = APIMgtDBUtil.getConnection();
conn.setAutoCommit(false);
String subscriptionUUIDQuery = SQLConstants.GET_SUBSCRIPTION_UUID_SQL;
if (identifier.getId() > 0) {
id = identifier.getId();
} else if (identifier instanceof APIIdentifier) {
String apiUuid;
if (identifier.getUUID() != null) {
apiUuid = identifier.getUUID();
} else {
apiUuid = getUUIDFromIdentifier((APIIdentifier) identifier);
}
id = getAPIID(apiUuid, conn);
} else if (identifier instanceof APIProductIdentifier) {
id = ((APIProductIdentifier) identifier).getProductId();
}
ps = conn.prepareStatement(subscriptionUUIDQuery);
ps.setInt(1, id);
ps.setInt(2, applicationId);
resultSet = ps.executeQuery();
if (resultSet.next()) {
uuid = resultSet.getString("UUID");
SubscribedAPI subscribedAPI = new SubscribedAPI(uuid);
removeSubscription(subscribedAPI, conn);
} else {
throw new APIManagementException("UUID does not exist for the given apiId:" + id + " and " + "application id:" + applicationId);
}
conn.commit();
} catch (SQLException e) {
if (conn != null) {
try {
conn.rollback();
} catch (SQLException ex) {
log.error("Failed to rollback the add subscription ", ex);
}
}
handleException("Failed to add subscriber data ", e);
} finally {
APIMgtDBUtil.closeAllConnections(ps, conn, resultSet);
}
}
use of org.wso2.carbon.apimgt.api.model.APIIdentifier in project carbon-apimgt by wso2.
the class APIProviderImplTest method mockSequencesMultiple.
private void mockSequencesMultiple(String seqLoc, String apiSeqLoc, APIIdentifier apiId) throws Exception {
ServiceReferenceHolder sh = PowerMockito.mock(ServiceReferenceHolder.class);
PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(sh);
RegistryService registryService = Mockito.mock(RegistryService.class);
PowerMockito.when(sh.getRegistryService()).thenReturn(registryService);
UserRegistry registry = Mockito.mock(UserRegistry.class);
PowerMockito.when(registryService.getGovernanceSystemRegistry(Matchers.anyInt())).thenReturn(registry);
Mockito.when(registry.resourceExists(seqLoc)).thenReturn(true);
Collection seqCollection = Mockito.mock(Collection.class);
Mockito.when(registry.get(seqLoc)).thenReturn(seqCollection);
String[] seqChildPaths = { "path1", "path2" };
Mockito.when(seqCollection.getChildren()).thenReturn(seqChildPaths);
Resource sequence = Mockito.mock(Resource.class);
Mockito.when(registry.get(seqChildPaths[0])).thenReturn(sequence);
InputStream responseStream = IOUtils.toInputStream("<sequence name=\"pqr\"></sequence>", "UTF-8");
Resource sequence2 = Mockito.mock(Resource.class);
Mockito.when(registry.get(seqChildPaths[1])).thenReturn(sequence2);
InputStream responseStream2 = IOUtils.toInputStream("<sequence name=\"abc\"></sequence>", "UTF-8");
OMElement seqElment = buildOMElement(responseStream);
OMElement seqElment2 = buildOMElement(responseStream2);
PowerMockito.when(APIUtil.buildOMElement(responseStream)).thenReturn(seqElment);
PowerMockito.when(APIUtil.buildOMElement(responseStream2)).thenReturn(seqElment2);
Mockito.when(sequence.getContentStream()).thenReturn(responseStream);
Mockito.when(sequence2.getContentStream()).thenReturn(responseStream2);
}
use of org.wso2.carbon.apimgt.api.model.APIIdentifier in project carbon-apimgt by wso2.
the class APIProviderImplTest method testDeleteWorkflowTask.
@Test
public void testDeleteWorkflowTask() throws APIManagementException, WorkflowException {
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
Mockito.when(apimgtDAO.getAPIID(apiUUID)).thenReturn(1111);
WorkflowExecutorFactory wfe = PowerMockito.mock(WorkflowExecutorFactory.class);
Mockito.when(WorkflowExecutorFactory.getInstance()).thenReturn(wfe);
WorkflowExecutor apiStateChangeWFExecutor = Mockito.mock(WorkflowExecutor.class);
Mockito.when(wfe.getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_API_STATE)).thenReturn(apiStateChangeWFExecutor);
WorkflowDTO workflowDTO = Mockito.mock(WorkflowDTO.class);
Mockito.when(apimgtDAO.retrieveWorkflowFromInternalReference(Integer.toString(1111), WorkflowConstants.WF_TYPE_AM_API_STATE)).thenReturn(workflowDTO);
APIIdentifier identifier = new APIIdentifier("admin", "API1", "1.0.0", apiUUID);
apiProvider.deleteWorkflowTask(identifier);
Mockito.verify(apimgtDAO, Mockito.times(1)).getAPIID(apiUUID);
}
use of org.wso2.carbon.apimgt.api.model.APIIdentifier 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());
}
}
Aggregations