use of org.wso2.carbon.apimgt.api.model.APIIdentifier 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.api.model.APIIdentifier 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());
}
use of org.wso2.carbon.apimgt.api.model.APIIdentifier in project carbon-apimgt by wso2.
the class APIProviderImplTest method testAddDocumentationContent.
@Test
public void testAddDocumentationContent() throws Exception {
APIIdentifier apiId = new APIIdentifier("admin", "API1", "1.0.0");
API api = new API(apiId);
String docName = "HowTo";
Documentation doc = new Documentation(DocumentationType.HOWTO, docName);
doc.setVisibility(DocumentVisibility.API_LEVEL);
String docPath = "/apimgt/applicationdata/provider/admin/API1/1.0.0/documentation/contents";
String documentationPath = docPath + docName;
String contentPath = docPath + APIConstants.INLINE_DOCUMENT_CONTENT_DIR + RegistryConstants.PATH_SEPARATOR + docName;
Mockito.when(APIUtil.getAPIDocPath(apiId)).thenReturn(docPath);
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
Resource docResource = Mockito.mock(Resource.class);
Mockito.when(docResource.getUUID()).thenReturn("678ghk");
Mockito.when(apiProvider.registry.get(documentationPath)).thenReturn(docResource);
GenericArtifact docArtifact = Mockito.mock(GenericArtifact.class);
PowerMockito.whenNew(GenericArtifactManager.class).withAnyArguments().thenReturn(artifactManager);
Mockito.when(artifactManager.getGenericArtifact("678ghk")).thenReturn(docArtifact);
Mockito.when(APIUtil.getDocumentation(docArtifact)).thenReturn(doc);
Resource docContent = Mockito.mock(Resource.class);
Mockito.when(apiProvider.registry.resourceExists(contentPath)).thenReturn(true, false);
Mockito.when(apiProvider.registry.get(contentPath)).thenReturn(docContent);
Mockito.when(apiProvider.registry.newResource()).thenReturn(docContent);
apiProvider.addDocumentationContent(api, docName, "content");
RegistryAuthorizationManager registryAuthorizationManager = Mockito.mock(RegistryAuthorizationManager.class);
PowerMockito.whenNew(RegistryAuthorizationManager.class).withAnyArguments().thenReturn(registryAuthorizationManager);
apiProvider.tenantDomain = "carbon.super";
doc.setVisibility(DocumentVisibility.OWNER_ONLY);
apiProvider.addDocumentationContent(api, docName, "content");
doc.setVisibility(DocumentVisibility.PRIVATE);
apiProvider.addDocumentationContent(api, docName, "content");
Mockito.doThrow(RegistryException.class).when(apiProvider.registry).put(Matchers.anyString(), any(Resource.class));
try {
apiProvider.addDocumentationContent(api, docName, "content");
} catch (APIManagementException e) {
String msg = "Failed to add the documentation content of : " + docName + " of API :" + apiId.getApiName();
Assert.assertEquals(msg, e.getMessage());
}
}
use of org.wso2.carbon.apimgt.api.model.APIIdentifier 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.APIIdentifier in project carbon-apimgt by wso2.
the class APIProviderImplTest method testGetAPILifeCycleData.
@Test
public void testGetAPILifeCycleData() throws Exception {
APIIdentifier identifier = new APIIdentifier("admin-AT-carbon.super", "API1", "1.0.0");
String path = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getApiName() + RegistryConstants.PATH_SEPARATOR + identifier.getVersion();
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
Resource apiSourceArtifact = Mockito.mock(Resource.class);
Mockito.when(apiSourceArtifact.getUUID()).thenReturn("12640983654");
PowerMockito.when(APIUtil.getAPIPath(identifier)).thenReturn(path);
PowerMockito.when(apiProvider.registry.get(path)).thenReturn(apiSourceArtifact);
Mockito.when(artifactManager.getGenericArtifact(apiSourceArtifact.getUUID())).thenReturn(artifact);
Mockito.when(artifact.getLifecycleState()).thenReturn("Created");
LifecycleBean bean = getLCBean();
Mockito.when(LifecycleBeanPopulator.getLifecycleBean(path, (UserRegistry) apiProvider.registry, apiProvider.configRegistry)).thenReturn(bean);
Map<String, Object> lcData = apiProvider.getAPILifeCycleData(identifier);
List checkListItems = (List) lcData.get(APIConstants.LC_CHECK_ITEMS);
Assert.assertEquals(2, checkListItems.size());
if (checkListItems.get(0) instanceof CheckListItem) {
CheckListItem checkListItem = (CheckListItem) checkListItems.get(0);
Assert.assertTrue((APIConstants.RESUBSCRIBE_CHECK_LIST_ITEM).equals(checkListItem.getName()) || (APIConstants.DEPRECATE_CHECK_LIST_ITEM).equals(checkListItem.getName()));
if ((APIConstants.RESUBSCRIBE_CHECK_LIST_ITEM).equals(checkListItem.getName())) {
Assert.assertEquals("1", checkListItem.getOrder());
} else {
Assert.assertEquals("0", checkListItem.getOrder());
}
Assert.assertEquals("Created", checkListItem.getLifeCycleStatus());
}
if (checkListItems.get(1) instanceof CheckListItem) {
CheckListItem checkListItem = (CheckListItem) checkListItems.get(1);
Assert.assertTrue((APIConstants.RESUBSCRIBE_CHECK_LIST_ITEM).equals(checkListItem.getName()) || (APIConstants.DEPRECATE_CHECK_LIST_ITEM).equals(checkListItem.getName()));
if ((APIConstants.RESUBSCRIBE_CHECK_LIST_ITEM).equals(checkListItem.getName())) {
Assert.assertEquals("1", checkListItem.getOrder());
} else {
Assert.assertEquals("0", checkListItem.getOrder());
}
Assert.assertEquals("Created", checkListItem.getLifeCycleStatus());
}
}
Aggregations