use of org.wso2.carbon.apimgt.api.model.APIIdentifier in project carbon-apimgt by wso2.
the class AbstractAPIManagerTestCase method testGetDocumentationContent.
@Test
public void testGetDocumentationContent() throws APIManagementException, org.wso2.carbon.user.api.UserStoreException, RegistryException {
int tenantId = -1234;
UserRegistry registry = Mockito.mock(UserRegistry.class);
AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapperExtended(genericArtifactManager, registryService, registry, tenantManager);
Mockito.when(tenantManager.getTenantId(Mockito.anyString())).thenThrow(UserStoreException.class).thenReturn(tenantId);
APIIdentifier identifier = getAPIIdentifier(SAMPLE_API_NAME, API_PROVIDER, SAMPLE_API_VERSION);
String docName = "doc1";
try {
abstractAPIManager.getDocumentationContent(identifier, docName);
Assert.fail("User store exception not thrown for error scenario");
} catch (APIManagementException e) {
Assert.assertTrue(e.getMessage().contains("Failed to get document content found for documentation:"));
}
Mockito.when(registryService.getGovernanceSystemRegistry(Mockito.anyInt())).thenThrow(RegistryException.class).thenReturn(registry);
try {
abstractAPIManager.getDocumentationContent(identifier, docName);
Assert.fail("Registry exception not thrown for error scenario");
} catch (APIManagementException e) {
Assert.assertTrue(e.getMessage().contains("No document content found for documentation:"));
}
Mockito.when(registry.resourceExists(Mockito.anyString())).thenReturn(false, true, true);
String docContent = abstractAPIManager.getDocumentationContent(identifier, docName);
Assert.assertNull(docContent);
String docObject = "samlple doc content";
Resource resource = new ResourceImpl();
resource.setContent(docObject.getBytes(StandardCharsets.UTF_8));
Mockito.when(registry.get(Mockito.anyString())).thenReturn(resource);
docContent = abstractAPIManager.getDocumentationContent(identifier, docName);
Assert.assertEquals(docContent, docObject);
abstractAPIManager = new AbstractAPIManagerWrapper(genericArtifactManager, registryService, registry, tenantManager);
docContent = abstractAPIManager.getDocumentationContent(identifier, docName);
Assert.assertEquals(docContent, docObject);
Mockito.when(registryService.getGovernanceUserRegistry(Mockito.anyString(), Mockito.anyInt())).thenReturn(registry);
abstractAPIManager.tenantDomain = SAMPLE_TENANT_DOMAIN_1;
docContent = abstractAPIManager.getDocumentationContent(identifier, docName);
Assert.assertEquals(docContent, docObject);
}
use of org.wso2.carbon.apimgt.api.model.APIIdentifier in project carbon-apimgt by wso2.
the class APIProviderImplTest method testGetCustomFaultSequences1.
@Test
public void testGetCustomFaultSequences1() throws Exception {
APIIdentifier apiId = new APIIdentifier("admin", "API1", "1.0.1");
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
mockSequences(APIConstants.API_CUSTOM_FAULTSEQUENCE_LOCATION, APIConstants.API_CUSTOM_SEQUENCE_TYPE_FAULT, apiId);
List<String> sequenceList = apiProvider.getCustomFaultSequences();
Assert.assertNotNull(sequenceList);
Assert.assertEquals(1, sequenceList.size());
// OMException when building OMElement
PowerMockito.when(APIUtil.buildOMElement(any(InputStream.class))).thenThrow(new OMException());
apiProvider.getCustomFaultSequences();
// 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(APIConstants.API_CUSTOM_FAULTSEQUENCE_LOCATION)).thenThrow(org.wso2.carbon.registry.api.RegistryException.class);
String msg = "Error while processing the fault in the registry";
try {
apiProvider.getCustomFaultSequences();
} 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.getCustomFaultSequences();
} 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 AbstractAPIManagerTestCase method testGetAsyncApiDefinition.
@Test
public void testGetAsyncApiDefinition() throws Exception {
Organization org = Mockito.mock(Organization.class);
PowerMockito.whenNew(Organization.class).withArguments(SAMPLE_TENANT_DOMAIN, null).thenReturn(org);
AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(apiPersistenceInstance);
APIIdentifier identifier = getAPIIdentifier(SAMPLE_API_NAME, API_PROVIDER, SAMPLE_API_VERSION);
identifier.setUuid(SAMPLE_RESOURCE_ID);
PowerMockito.mockStatic(OASParserUtil.class);
String asyncDefinition = "Sample Async Definition";
PowerMockito.when(apiPersistenceInstance.getAsyncDefinition(org, SAMPLE_RESOURCE_ID)).thenReturn(asyncDefinition);
Assert.assertEquals(abstractAPIManager.getAsyncAPIDefinition(SAMPLE_RESOURCE_ID, SAMPLE_TENANT_DOMAIN), asyncDefinition);
}
use of org.wso2.carbon.apimgt.api.model.APIIdentifier in project carbon-apimgt by wso2.
the class AbstractAPIManagerTestCase method testGetAllDocumentationWithLoggedUser.
@Test
public void testGetAllDocumentationWithLoggedUser() throws APIManagementException, org.wso2.carbon.user.api.UserStoreException, RegistryException {
int tenantId = -1234;
UserRegistry registry = Mockito.mock(UserRegistry.class);
AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(genericArtifactManager, registryService, registry, tenantManager);
Mockito.when(tenantManager.getTenantId(SAMPLE_TENANT_DOMAIN)).thenThrow(UserStoreException.class).thenReturn(tenantId);
APIIdentifier identifier = getAPIIdentifier(SAMPLE_API_NAME, API_PROVIDER, SAMPLE_API_VERSION);
String loggedInUser = "admin";
abstractAPIManager.registry = registry;
GenericArtifact genericArtifact = getGenericArtifact(SAMPLE_API_NAME, API_PROVIDER, SAMPLE_API_VERSION, "sample");
genericArtifact.setAttribute(APIConstants.DOC_TYPE, "Other");
genericArtifact.setAttribute(APIConstants.DOC_SOURCE_TYPE, "URL");
String apiDocPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getApiName() + RegistryConstants.PATH_SEPARATOR + identifier.getVersion() + RegistryConstants.PATH_SEPARATOR + APIConstants.DOC_DIR + RegistryConstants.PATH_SEPARATOR;
PowerMockito.mockStatic(APIUtil.class);
PowerMockito.when(APIUtil.getAPIOrAPIProductDocPath(identifier)).thenReturn(apiDocPath);
Resource resource1 = new ResourceImpl();
resource1.setUUID(SAMPLE_RESOURCE_ID);
Mockito.when(genericArtifact.getPath()).thenReturn("test");
String docName = "sample";
Documentation documentation = new Documentation(DocumentationType.HOWTO, docName);
PowerMockito.when(APIUtil.getDocumentation(genericArtifact)).thenReturn(documentation);
Mockito.when(registry.resourceExists(apiDocPath)).thenReturn(true);
try {
abstractAPIManager.getAllDocumentation(identifier, loggedInUser);
Assert.fail("Registry exception not thrown for error scenario");
} catch (APIManagementException e) {
Assert.assertTrue(e.getMessage().contains("Failed to get documentations for api"));
}
Resource resource2 = new ResourceImpl();
resource2.setUUID(SAMPLE_RESOURCE_ID);
Mockito.when(genericArtifactManager.getGenericArtifact(SAMPLE_RESOURCE_ID)).thenReturn(genericArtifact);
String documentationName = "doc1";
Collection documentCollection = new CollectionImpl();
documentCollection.setChildren(new String[] { apiDocPath + documentationName, apiDocPath + APIConstants.INLINE_DOCUMENT_CONTENT_DIR });
Mockito.when(registry.get(apiDocPath)).thenReturn(documentCollection);
Mockito.when(registry.get(apiDocPath + documentationName)).thenReturn(resource2);
PowerMockito.when(APIUtil.getDocumentation(genericArtifact, loggedInUser)).thenReturn(documentation);
List<Documentation> documentationList = abstractAPIManager.getAllDocumentation(identifier, loggedInUser);
Assert.assertNotNull(documentationList);
Assert.assertEquals(documentationList.size(), 1);
String contentPath = APIConstants.API_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getApiName() + RegistryConstants.PATH_SEPARATOR + identifier.getVersion() + RegistryConstants.PATH_SEPARATOR + APIConstants.DOC_DIR + RegistryConstants.PATH_SEPARATOR + RegistryConstants.PATH_SEPARATOR + documentationName;
genericArtifact.setAttribute(APIConstants.DOC_SOURCE_TYPE, "In line");
genericArtifact.setAttribute(APIConstants.DOC_NAME, documentationName);
ResourceDO resourceDO = new ResourceDO();
resourceDO.setLastUpdatedOn(12344567);
Resource resource3 = new ResourceImpl(contentPath, resourceDO);
Mockito.when(registry.get(contentPath)).thenReturn(resource3);
documentationList = abstractAPIManager.getAllDocumentation(identifier, loggedInUser);
Assert.assertNotNull(documentationList);
Assert.assertEquals(documentationList.size(), 1);
}
use of org.wso2.carbon.apimgt.api.model.APIIdentifier in project carbon-apimgt by wso2.
the class AbstractAPIManagerTestCase method testGetIcon.
@Test
public void testGetIcon() throws APIManagementException, org.wso2.carbon.user.api.UserStoreException, RegistryException {
APIIdentifier identifier = new APIIdentifier(SAMPLE_API_NAME, API_PROVIDER, SAMPLE_API_VERSION);
int tenantId = -1234;
UserRegistry registry = Mockito.mock(UserRegistry.class);
AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapperExtended(genericArtifactManager, registryService, registry, tenantManager);
Mockito.when(tenantManager.getTenantId(Mockito.anyString())).thenThrow(UserStoreException.class).thenReturn(tenantId);
try {
abstractAPIManager.getIcon(identifier);
Assert.fail("User store exception not thrown for error scenario");
} catch (APIManagementException e) {
Assert.assertTrue(e.getMessage().contains("Error while loading API icon of API"));
}
Mockito.when(registryService.getGovernanceSystemRegistry(Mockito.anyInt())).thenThrow(RegistryException.class).thenReturn(registry);
try {
abstractAPIManager.getIcon(identifier);
Assert.fail("User store exception not thrown for error scenario");
} catch (APIManagementException e) {
Assert.assertTrue(e.getMessage().contains("Error while loading API icon of API"));
}
Assert.assertNull(abstractAPIManager.getIcon(identifier));
AbstractAPIManager abstractAPIManager1 = new AbstractAPIManagerWrapper(genericArtifactManager, registryService, registry, tenantManager);
Mockito.when(registryService.getGovernanceUserRegistry(Mockito.anyString(), Mockito.anyInt())).thenReturn(registry);
Assert.assertNull(abstractAPIManager1.getIcon(identifier));
abstractAPIManager1.tenantDomain = SAMPLE_TENANT_DOMAIN_1;
Mockito.when(registry.resourceExists(Mockito.anyString())).thenReturn(true);
Resource resource = new ResourceImpl();
resource.setContent("sample conetent");
resource.setMediaType("api");
Mockito.when(registry.get(Mockito.anyString())).thenReturn(resource);
Assert.assertTrue(abstractAPIManager1.getIcon(identifier).getContentType().equals("api"));
}
Aggregations