use of org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO in project carbon-apimgt by wso2.
the class WorkflowUtils method completeStateChangeWorkflow.
/**
* Complete the lifecycle state change workflow
*
* @param workflowDTO Workflow DTO object
* @throws WorkflowException Exception when completing the workflow
*/
protected static void completeStateChangeWorkflow(WorkflowDTO workflowDTO) throws WorkflowException {
String externalWorkflowRef = workflowDTO.getExternalWorkflowReference();
try {
ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();
Workflow workflow = apiMgtDAO.getworkflowReferenceByExternalWorkflowReference(externalWorkflowRef);
String apiName = workflow.getMetadata("ApiName");
String action = workflow.getMetadata("Action");
String providerName = workflow.getMetadata("ApiProvider");
String version = workflow.getMetadata("ApiVersion");
String invoker = workflow.getMetadata("Invoker");
String currentStatus = workflow.getMetadata("CurrentState");
int tenantId = workflowDTO.getTenantId();
try {
// tenant flow is already started from the rest api service impl. no need to start from here
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(invoker);
Registry registry = ServiceReferenceHolder.getInstance().getRegistryService().getGovernanceUserRegistry(invoker, tenantId);
APIIdentifier apiIdentifier = new APIIdentifier(providerName, apiName, version);
GenericArtifact apiArtifact = APIUtil.getAPIArtifact(apiIdentifier, registry);
if (WorkflowStatus.APPROVED.equals(workflowDTO.getStatus())) {
String targetStatus;
apiArtifact.invokeAction(action, APIConstants.API_LIFE_CYCLE);
targetStatus = apiArtifact.getLifecycleState();
if (!currentStatus.equals(targetStatus)) {
apiMgtDAO.recordAPILifeCycleEvent(apiArtifact.getId(), currentStatus.toUpperCase(), targetStatus.toUpperCase(), invoker, tenantId);
}
if (log.isDebugEnabled()) {
String logMessage = "API Status changed successfully. API Name: " + apiIdentifier.getApiName() + ", API " + "Version " + apiIdentifier.getVersion() + ", New Status : " + targetStatus;
log.debug(logMessage);
}
}
} catch (RegistryException e) {
String errorMsg = "Could not complete api state change workflow";
log.error(errorMsg, e);
throw new WorkflowException(errorMsg, e);
}
} catch (APIManagementException e) {
String errorMsg = "Could not complete api state change workflow";
log.error(errorMsg, e);
}
}
use of org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO in project carbon-apimgt by wso2.
the class APIProviderImplTest method testDeleteAPIProductWorkflowTask.
@Test
public void testDeleteAPIProductWorkflowTask() 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 productStateChangeWorkflowExecutor = Mockito.mock(WorkflowExecutor.class);
Mockito.when(wfe.getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_API_PRODUCT_STATE)).thenReturn(productStateChangeWorkflowExecutor);
WorkflowDTO workflowDTO = Mockito.mock(WorkflowDTO.class);
Mockito.when(apimgtDAO.retrieveWorkflowFromInternalReference(Integer.toString(1111), WorkflowConstants.WF_TYPE_AM_API_PRODUCT_STATE)).thenReturn(workflowDTO);
APIProductIdentifier identifier = new APIProductIdentifier("admin", "APIProduct", "1.0.0", apiUUID);
apiProvider.deleteWorkflowTask(identifier);
Mockito.verify(apimgtDAO, Mockito.times(1)).getAPIID(apiUUID);
}
use of org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO in project carbon-apimgt by wso2.
the class APIProviderImplTest method testUpdateAPIforStateChange_ToRetired.
@Test
public void testUpdateAPIforStateChange_ToRetired() throws RegistryException, UserStoreException, APIManagementException, FaultGatewaysException, WorkflowException, APIPersistenceException, XMLStreamException {
APIIdentifier apiId = new APIIdentifier("admin", "API1", "1.0.0");
Set<String> environments = new HashSet<String>();
environments.add("Production");
environments.add("Sandbox");
API api = new API(apiId);
api.setContext("/test");
api.setStatus(APIConstants.CREATED);
api.setAsDefaultVersion(true);
api.setEnvironments(environments);
api.setOrganization("carbon.super");
Mockito.when(apimgtDAO.getPublishedDefaultVersion(apiId)).thenReturn("1.0.0");
Map<String, String> failedGWEnv = new HashMap<String, String>();
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apiPersistenceInstance, apimgtDAO, scopesDAO, null, null);
Mockito.when(artifactManager.newGovernanceArtifact(any(QName.class))).thenReturn(artifact);
Mockito.when(APIUtil.createAPIArtifactContent(artifact, api)).thenReturn(artifact);
RegistryService registryService = Mockito.mock(RegistryService.class);
UserRegistry userRegistry = Mockito.mock(UserRegistry.class);
ServiceReferenceHolder serviceReferenceHolder = TestUtils.getServiceReferenceHolder();
RealmService realmService = Mockito.mock(RealmService.class);
TenantManager tenantManager = Mockito.mock(TenantManager.class);
PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
Mockito.when(serviceReferenceHolder.getRegistryService()).thenReturn(registryService);
Mockito.when(registryService.getConfigSystemRegistry(Mockito.anyInt())).thenReturn(userRegistry);
Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
PublisherAPI publisherAPI = Mockito.mock(PublisherAPI.class);
PowerMockito.when(apiPersistenceInstance.addAPI(any(Organization.class), any(PublisherAPI.class))).thenReturn(publisherAPI);
apiProvider.addAPI(api);
// Mock Updating API
Resource apiSourceArtifact = Mockito.mock(Resource.class);
Mockito.when(apiSourceArtifact.getUUID()).thenReturn("12640983654");
String apiSourcePath = "path";
PowerMockito.when(APIUtil.getAPIPath(api.getId())).thenReturn(apiSourcePath);
PowerMockito.when(apiProvider.registry.get(apiSourcePath)).thenReturn(apiSourceArtifact);
Mockito.when(artifact.getAttribute(APIConstants.API_OVERVIEW_STATUS)).thenReturn("CREATED");
Mockito.when(artifactManager.getGenericArtifact(apiSourceArtifact.getUUID())).thenReturn(artifact);
PowerMockito.when(APIUtil.createAPIArtifactContent(artifact, api)).thenReturn(artifact);
Mockito.when(artifact.getId()).thenReturn("12640983654");
PowerMockito.when(GovernanceUtils.getArtifactPath(apiProvider.registry, "12640983654")).thenReturn(apiSourcePath);
Mockito.doNothing().when(artifactManager).updateGenericArtifact(artifact);
WorkflowExecutorFactory wfe = PowerMockito.mock(WorkflowExecutorFactory.class);
Mockito.when(WorkflowExecutorFactory.getInstance()).thenReturn(wfe);
boolean status = apiProvider.updateAPIforStateChange(api, APIConstants.CREATED, APIConstants.RETIRED);
Assert.assertEquals(2, api.getEnvironments().size());
Assert.assertEquals(true, status);
}
use of org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO in project carbon-apimgt by wso2.
the class APIProviderImplTest method testGetCustomApiOutSequences.
@Test
public void testGetCustomApiOutSequences() throws Exception {
APIIdentifier apiId = new APIIdentifier("admin", "API1", "1.0.1");
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
mockSequences(APIConstants.API_CUSTOM_OUTSEQUENCE_LOCATION, APIConstants.API_CUSTOM_SEQUENCE_TYPE_OUT, apiId);
List<String> sequenceList = apiProvider.getCustomApiOutSequences(apiId);
Assert.assertNotNull(sequenceList);
Assert.assertEquals(1, sequenceList.size());
// OMException when building OMElement
PowerMockito.when(APIUtil.buildOMElement(any(InputStream.class))).thenThrow(new OMException());
apiProvider.getCustomApiOutSequences(apiId);
// org.wso2.carbon.registry.api.RegistryException
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(APIUtil.getSequencePath(apiId, APIConstants.API_CUSTOM_SEQUENCE_TYPE_OUT))).thenThrow(org.wso2.carbon.registry.api.RegistryException.class);
String msg = "Error while processing the " + APIConstants.API_CUSTOM_SEQUENCE_TYPE_OUT + " sequences of " + apiId + " in the registry";
try {
apiProvider.getCustomApiOutSequences(apiId);
} catch (APIManagementException e) {
Assert.assertEquals(msg, e.getMessage());
}
// Registry Exception
PowerMockito.when(registryService.getGovernanceSystemRegistry(Matchers.anyInt())).thenThrow(RegistryException.class);
String msg1 = "Error while retrieving registry for tenant -1";
try {
apiProvider.getCustomApiOutSequences(apiId);
} catch (APIManagementException e) {
Assert.assertEquals(msg1, e.getMessage());
}
}
use of org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO in project carbon-apimgt by wso2.
the class APIProviderImplTest method testSearchAPIs_NoProviderId.
@Test
public void testSearchAPIs_NoProviderId() throws APIManagementException, RegistryException {
API api1 = new API(new APIIdentifier("admin", "API1", "1.0.1"));
api1.setContext("api1context");
api1.setStatus(APIConstants.PUBLISHED);
api1.setDescription("API 1 Desciption");
Set<URITemplate> uriTemplates = new HashSet<URITemplate>();
URITemplate uriTemplate1 = new URITemplate();
uriTemplate1.setHTTPVerb("POST");
uriTemplate1.setAuthType("Application");
uriTemplate1.setUriTemplate("/add");
uriTemplate1.setThrottlingTier("Gold");
uriTemplates.add(uriTemplate1);
api1.setUriTemplates(uriTemplates);
API api2 = new API(new APIIdentifier("admin", "API2", "1.0.0"));
api2.setContext("api2context");
api2.setStatus(APIConstants.CREATED);
api2.setDescription("API 2 Desciption");
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
GenericArtifact genericArtifact1 = Mockito.mock(GenericArtifact.class);
GenericArtifact genericArtifact2 = Mockito.mock(GenericArtifact.class);
Mockito.when(genericArtifact1.getAttribute(APIConstants.API_OVERVIEW_NAME)).thenReturn("API1");
Mockito.when(genericArtifact1.getAttribute(APIConstants.API_OVERVIEW_VERSION)).thenReturn("1.0.1");
Mockito.when(genericArtifact1.getAttribute(APIConstants.API_OVERVIEW_CONTEXT)).thenReturn("api1context");
Mockito.when(genericArtifact1.getAttribute(APIConstants.API_OVERVIEW_DESCRIPTION)).thenReturn("API 1 Desciption");
Mockito.when(genericArtifact1.getAttribute(APIConstants.API_OVERVIEW_PROVIDER)).thenReturn("admin");
Mockito.when(genericArtifact1.getAttribute(APIConstants.API_OVERVIEW_STATUS)).thenReturn("Published");
Mockito.when(genericArtifact2.getAttribute(APIConstants.API_OVERVIEW_NAME)).thenReturn("API2");
Mockito.when(genericArtifact2.getAttribute(APIConstants.API_OVERVIEW_VERSION)).thenReturn("1.0.0");
Mockito.when(genericArtifact2.getAttribute(APIConstants.API_OVERVIEW_CONTEXT)).thenReturn("api2context");
Mockito.when(genericArtifact2.getAttribute(APIConstants.API_OVERVIEW_DESCRIPTION)).thenReturn("API 2 Desciption");
Mockito.when(genericArtifact2.getAttribute(APIConstants.API_OVERVIEW_PROVIDER)).thenReturn("admin");
Mockito.when(genericArtifact2.getAttribute(APIConstants.API_OVERVIEW_STATUS)).thenReturn("Created");
Mockito.when(APIUtil.getAPI(genericArtifact1, apiProvider.registry)).thenReturn(api1);
Mockito.when(APIUtil.getAPI(genericArtifact2, apiProvider.registry)).thenReturn(api2);
Mockito.when(APIUtil.getAPI(genericArtifact1)).thenReturn(api1);
Mockito.when(APIUtil.getAPI(genericArtifact2)).thenReturn(api2);
GenericArtifact[] genericArtifacts = { genericArtifact1, genericArtifact2 };
Mockito.when(artifactManager.getAllGenericArtifacts()).thenReturn(genericArtifacts);
// Search by Name matching both APIs
List<API> foundApiList0 = apiProvider.searchAPIs("API", "Name", null);
Assert.assertNotNull(foundApiList0);
Assert.assertEquals(2, foundApiList0.size());
// Search by exact name
List<API> foundApiList1 = apiProvider.searchAPIs("API1", "Name", null);
Assert.assertNotNull(foundApiList1);
Assert.assertEquals(1, foundApiList1.size());
// Search by exact provider
List<API> foundApiList2 = apiProvider.searchAPIs("admin", "Provider", null);
Assert.assertNotNull(foundApiList2);
Assert.assertEquals(2, foundApiList2.size());
// Search by exact version
List<API> foundApiList3 = apiProvider.searchAPIs("1.0.0", "Version", null);
Assert.assertNotNull(foundApiList3);
Assert.assertEquals(1, foundApiList3.size());
// Search by exact context
List<API> foundApiList4 = apiProvider.searchAPIs("api1context", "Context", null);
Assert.assertNotNull(foundApiList4);
Assert.assertEquals(1, foundApiList4.size());
// Search by exact context
List<API> foundApiList5 = apiProvider.searchAPIs("api2context", "Context", null);
Assert.assertNotNull(foundApiList5);
Assert.assertEquals(1, foundApiList5.size());
// Search by wrong context
List<API> foundApiList6 = apiProvider.searchAPIs("test", "Context", null);
Assert.assertNotNull(foundApiList6);
Assert.assertEquals(0, foundApiList6.size());
// Search by status
List<API> foundApiList7 = apiProvider.searchAPIs("Published", "Status", null);
Assert.assertNotNull(foundApiList7);
Assert.assertEquals(1, foundApiList7.size());
// Search by Description
List<API> foundApiList8 = apiProvider.searchAPIs("API 1 Desciption", "Description", null);
Assert.assertNotNull(foundApiList8);
Assert.assertEquals(1, foundApiList8.size());
// Search by Description
List<API> foundApiList9 = apiProvider.searchAPIs("API", "Description", null);
Assert.assertNotNull(foundApiList9);
Assert.assertEquals(2, foundApiList9.size());
}
Aggregations