use of org.wso2.carbon.governance.api.generic.GenericArtifactManager in project carbon-apimgt by wso2.
the class AbstractAPIManagerTestCase method testGetDocumentation.
@Test
public void testGetDocumentation() throws APIManagementException, RegistryException {
Registry registry = Mockito.mock(UserRegistry.class);
AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(genericArtifactManager, registry, null);
APIIdentifier identifier = getAPIIdentifier(SAMPLE_API_NAME, API_PROVIDER, SAMPLE_API_VERSION);
String documentationName = "doc1";
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 + documentationName;
GenericArtifact genericArtifact = getGenericArtifact(SAMPLE_API_NAME, API_PROVIDER, SAMPLE_API_VERSION, "sample");
String docType = "other";
genericArtifact.setAttribute(APIConstants.DOC_TYPE, docType);
genericArtifact.setAttribute(APIConstants.DOC_SOURCE_TYPE, "URL");
Resource resource = new ResourceImpl();
resource.setUUID(SAMPLE_RESOURCE_ID);
Mockito.when(registry.get(contentPath)).thenThrow(RegistryException.class).thenReturn(resource);
Mockito.when(genericArtifactManager.getGenericArtifact(SAMPLE_RESOURCE_ID)).thenReturn(genericArtifact);
try {
abstractAPIManager.getDocumentation(identifier, DocumentationType.OTHER, documentationName);
Assert.fail("Registry exception not thrown for error scenario");
} catch (APIManagementException e) {
Assert.assertTrue(e.getMessage().contains("Failed to get documentation details"));
}
Assert.assertTrue(abstractAPIManager.getDocumentation(identifier, DocumentationType.OTHER, documentationName).getId().equals(genericArtifact.getId()));
}
use of org.wso2.carbon.governance.api.generic.GenericArtifactManager in project carbon-apimgt by wso2.
the class AbstractAPIManagerTestCase method testGetAllDocumentation.
@Test
public void testGetAllDocumentation() throws APIManagementException, RegistryException {
Registry registry = Mockito.mock(UserRegistry.class);
AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(genericArtifactManager, null, registry, null, apiMgtDAO);
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");
APIIdentifier identifier = getAPIIdentifier(SAMPLE_API_NAME, API_PROVIDER, SAMPLE_API_VERSION);
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)).thenThrow(RegistryException.class).thenReturn(true);
Mockito.when(apiMgtDAO.checkAPIUUIDIsARevisionUUID(Mockito.anyString())).thenReturn(null);
try {
abstractAPIManager.getAllDocumentation(identifier);
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)).thenReturn(documentation);
List<Documentation> documentationList = abstractAPIManager.getAllDocumentation(identifier);
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);
Assert.assertNotNull(documentationList);
Assert.assertEquals(documentationList.size(), 1);
}
use of org.wso2.carbon.governance.api.generic.GenericArtifactManager in project carbon-apimgt by wso2.
the class APIConsumerImplTest method testGetAllPaginatedAPIsByStatus.
@Test
public void testGetAllPaginatedAPIsByStatus() throws Exception {
Registry userRegistry = Mockito.mock(Registry.class);
APIConsumerImpl apiConsumer = new APIConsumerImplWrapper(userRegistry, apiMgtDAO);
System.setProperty(CARBON_HOME, "");
APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
Mockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
Mockito.when(apiManagerConfiguration.getFirstProperty(Mockito.anyString())).thenReturn("10", "20");
PowerMockito.mockStatic(GovernanceUtils.class);
PowerMockito.mockStatic(GovernanceUtils.class);
PowerMockito.doNothing().when(APIUtil.class, "loadTenantRegistry", Mockito.anyInt());
GenericArtifactManager artifactManager = Mockito.mock(GenericArtifactManager.class);
PowerMockito.when(APIUtil.isAllowDisplayMultipleVersions()).thenReturn(false, true);
PowerMockito.when(APIUtil.getArtifactManager((UserRegistry) (Mockito.anyObject()), Mockito.anyString())).thenReturn(artifactManager);
GenericArtifact artifact = Mockito.mock(GenericArtifact.class);
GenericArtifact[] genericArtifacts = new GenericArtifact[] { artifact };
Mockito.when(artifactManager.findGenericArtifacts(Mockito.anyMap())).thenReturn(genericArtifacts);
APIIdentifier apiId1 = new APIIdentifier("admin", "API1", "1.0.0");
API api = new API(apiId1);
Mockito.when(APIUtil.getAPI(artifact)).thenReturn(api);
String artifactPath = "artifact/path";
PowerMockito.when(GovernanceUtils.getArtifactPath(userRegistry, artifact.getId())).thenReturn(artifactPath);
Tag tag = new Tag();
Tag[] tags = new Tag[] { tag };
Mockito.when(userRegistry.getTags(artifactPath)).thenReturn(tags);
assertNotNull(apiConsumer.getAllPaginatedAPIsByStatus(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, 0, 10, "testStatus", false));
assertNotNull(apiConsumer.getAllPaginatedAPIsByStatus(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, 0, 10, "testStatus", true));
// artifact manager null path
PowerMockito.when(APIUtil.getArtifactManager((UserRegistry) (Mockito.anyObject()), Mockito.anyString())).thenReturn(null);
assertNotNull(apiConsumer.getAllPaginatedAPIsByStatus(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, 0, 10, "testStatus", true));
// generic artifact null path
PowerMockito.when(APIUtil.getArtifactManager((UserRegistry) (Mockito.anyObject()), Mockito.anyString())).thenReturn(artifactManager);
Mockito.when(artifactManager.findGenericArtifacts(Mockito.anyMap())).thenReturn(null);
assertNotNull(apiConsumer.getAllPaginatedAPIsByStatus(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, 0, 10, "testStatus", true));
}
use of org.wso2.carbon.governance.api.generic.GenericArtifactManager in project carbon-apimgt by wso2.
the class APIConsumerImplTest method testGetAllPaginatedPublishedAPIs.
@Test
public void testGetAllPaginatedPublishedAPIs() throws Exception {
APIConsumerImpl apiConsumer = new APIConsumerImplWrapper();
PowerMockito.doNothing().when(APIUtil.class, "loadTenantRegistry", Mockito.anyInt());
PowerMockito.when(APIUtil.isAllowDisplayMultipleVersions()).thenReturn(false, true);
System.setProperty(CARBON_HOME, "");
GenericArtifactManager artifactManager = Mockito.mock(GenericArtifactManager.class);
PowerMockito.when(APIUtil.getArtifactManager(apiConsumer.registry, APIConstants.API_KEY)).thenReturn(artifactManager);
GenericArtifact artifact = Mockito.mock(GenericArtifact.class);
GenericArtifact[] genericArtifacts = new GenericArtifact[] { artifact };
Mockito.when(artifactManager.findGenericArtifacts(Mockito.anyMap())).thenReturn(genericArtifacts);
APIIdentifier apiId1 = new APIIdentifier("admin", "API1", "1.0.0");
API api = new API(apiId1);
Mockito.when(APIUtil.getAPI(artifact)).thenReturn(api);
assertNotNull(apiConsumer.getAllPaginatedPublishedAPIs(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, 0, 10));
assertNotNull(apiConsumer.getAllPaginatedPublishedAPIs(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, 0, 10));
// artifact manager null path
PowerMockito.when(APIUtil.getArtifactManager((UserRegistry) (Mockito.anyObject()), Mockito.anyString())).thenReturn(null);
assertNotNull(apiConsumer.getAllPaginatedPublishedAPIs(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, 0, 10));
// generic artifact null path
PowerMockito.when(APIUtil.getArtifactManager((UserRegistry) (Mockito.anyObject()), Mockito.anyString())).thenReturn(artifactManager);
Mockito.when(artifactManager.findGenericArtifacts(Mockito.anyMap())).thenReturn(null);
assertNotNull(apiConsumer.getAllPaginatedPublishedAPIs(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, 0, 10));
}
use of org.wso2.carbon.governance.api.generic.GenericArtifactManager in project carbon-apimgt by wso2.
the class APIConsumerImplTest method testGetPublishedAPIsByProvider2.
@Test
public void testGetPublishedAPIsByProvider2() throws APIManagementException, RegistryException, org.wso2.carbon.user.core.UserStoreException {
APIConsumerImpl apiConsumer = new APIConsumerImplWrapper(userRegistry, apiMgtDAO);
String providerId = "2";
API api = new API(new APIIdentifier(API_PROVIDER, SAMPLE_API_NAME, SAMPLE_API_VERSION));
API api1 = new API(new APIIdentifier(API_PROVIDER, SAMPLE_API_NAME, "2.0.0"));
PowerMockito.mockStatic(APIUtil.class);
PowerMockito.when(APIUtil.isAllowDisplayMultipleVersions()).thenReturn(true, false);
PowerMockito.when(APIUtil.isAllowDisplayAPIsWithMultipleStatus()).thenReturn(true, false);
PowerMockito.when(APIUtil.getArtifactManager(userRegistry, APIConstants.API_KEY)).thenReturn(genericArtifactManager);
PowerMockito.when(APIUtil.getMountedPath(Mockito.any(), Mockito.anyString())).thenReturn("system/governance");
PowerMockito.when(APIUtil.getAPI(Mockito.any())).thenReturn(api);
PowerMockito.when(APIUtil.replaceEmailDomainBack(Mockito.anyString())).thenReturn(providerId);
PowerMockito.when(APIUtil.getLcStateFromArtifact((GovernanceArtifact) Mockito.any())).thenReturn(APIConstants.PUBLISHED);
GenericArtifact genericArtifact1 = new GenericArtifactImpl(new QName("local"), "artifact1");
GenericArtifact genericArtifact2 = new GenericArtifactImpl(new QName("local"), "artifact2");
GenericArtifact[] genericArtifacts = new GenericArtifact[] { genericArtifact1, genericArtifact2 };
Mockito.when(genericArtifactManager.findGenericArtifacts((Map<String, List<String>>) Mockito.any())).thenReturn(genericArtifacts);
PowerMockito.mockStatic(GovernanceUtils.class);
PowerMockito.when(GovernanceUtils.getArtifactPath(Mockito.any(), Mockito.anyString())).thenReturn("/path1");
PowerMockito.when(RegistryUtils.getAbsolutePath(Mockito.any(), Mockito.anyString())).thenReturn("/path1");
Association association = new Association();
association.setDestinationPath("/destPath1");
Association association2 = new Association();
association2.setDestinationPath("/destPath2");
Association[] associations = new Association[] { association, association2 };
Mockito.when(userRegistry.getAssociations(Mockito.anyString(), Mockito.anyString())).thenThrow(RegistryException.class).thenReturn(associations);
try {
apiConsumer.getPublishedAPIsByProvider("1", "test_user", 5, API_PROVIDER, "");
Assert.fail("Registry exception not thrown for error scenario");
} catch (APIManagementException e) {
Assert.assertTrue(e.getMessage().contains("Failed to get Published APIs for provider :"));
}
Assert.assertEquals(0, apiConsumer.getPublishedAPIsByProvider("1", "test_user", 2, API_PROVIDER, "").size());
Mockito.when(authorizationManager.isUserAuthorized(Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenThrow(UserStoreException.class).thenThrow(org.wso2.carbon.user.core.UserStoreException.class).thenReturn(true);
try {
apiConsumer.getPublishedAPIsByProvider("1", "test_user", 5, API_PROVIDER, "");
Assert.fail("User store exception not thrown for error scenario");
} catch (APIManagementException e) {
Assert.assertTrue(e.getMessage().contains("Failed to get Published APIs for provider :"));
}
try {
apiConsumer.getPublishedAPIsByProvider("1", "test_user", 5, API_PROVIDER, "");
Assert.fail("User store exception not thrown for error scenario");
} catch (APIManagementException e) {
Assert.assertTrue(e.getMessage().contains("Failed to get Published APIs for provider :"));
}
Resource resource = new ResourceImpl();
resource.setUUID(UUID.randomUUID().toString());
Mockito.when(userRegistry.get(Mockito.anyString())).thenReturn(resource);
GenericArtifact genericArtifact = Mockito.mock(GenericArtifactImpl.class);
Mockito.when(genericArtifact.getLifecycleState(Mockito.anyString())).thenReturn(APIConstants.PUBLISHED);
Mockito.when(genericArtifactManager.getGenericArtifact(Mockito.anyString())).thenReturn(genericArtifact);
Assert.assertEquals(1, apiConsumer.getPublishedAPIsByProvider("1", "test_user", 1, API_PROVIDER, "").size());
PowerMockito.when(APIUtil.getAPI((GenericArtifact) Mockito.any())).thenReturn(api, api1);
Assert.assertEquals(1, apiConsumer.getPublishedAPIsByProvider("1", "test_user", 5, API_PROVIDER, "").size());
PowerMockito.when(APIUtil.isAllowDisplayAPIsWithMultipleStatus()).thenReturn(true);
Set<API> apiSet = apiConsumer.getPublishedAPIsByProvider("1", "test_user", 5, API_PROVIDER, "");
Assert.assertEquals(1, apiSet.size());
Assert.assertTrue(apiSet.contains(api1));
String apiOwner = "Smith";
PowerMockito.when(APIUtil.replaceEmailDomainBack(apiOwner)).thenReturn(apiOwner);
api.setApiOwner("John");
PowerMockito.when(APIUtil.isAllowDisplayMultipleVersions()).thenReturn(true);
Assert.assertEquals(1, apiConsumer.getPublishedAPIsByProvider(apiOwner, "test_user", 1, apiOwner, "").size());
Assert.assertEquals(0, apiConsumer.getPublishedAPIsByProvider("1", "test_user", 1, apiOwner, "").size());
}
Aggregations