use of org.wso2.carbon.registry.core.session.UserRegistry in project carbon-apimgt by wso2.
the class APIProviderImplTest method testGetCustomFaultSequences.
@Test
public void testGetCustomFaultSequences() 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(apiId);
Assert.assertNotNull(sequenceList);
Assert.assertEquals(2, sequenceList.size());
Assert.assertTrue(sequenceList.contains("fault-seq"));
Assert.assertTrue(sequenceList.contains("custom-fault-seq"));
// OMException when building OMElement
PowerMockito.when(APIUtil.buildOMElement(any(InputStream.class))).thenThrow(new OMException());
apiProvider.getCustomFaultSequences(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(APIConstants.API_CUSTOM_FAULTSEQUENCE_LOCATION)).thenThrow(org.wso2.carbon.registry.api.RegistryException.class);
String msg = "Error while processing the " + APIConstants.API_CUSTOM_SEQUENCE_TYPE_FAULT + " sequences of " + apiId + " in the registry";
try {
apiProvider.getCustomFaultSequences(apiId);
} catch (APIManagementException e) {
Assert.assertEquals(msg, e.getMessage());
}
// Registry Exception
PowerMockito.when(registryService.getGovernanceSystemRegistry(Matchers.anyInt())).thenThrow(RegistryException.class);
try {
apiProvider.getCustomFaultSequences(apiId);
} catch (APIManagementException e) {
Assert.assertEquals("Error while retrieving registry for tenant -1", e.getMessage());
}
}
use of org.wso2.carbon.registry.core.session.UserRegistry in project carbon-apimgt by wso2.
the class APIProviderImplTest method testGetProvider.
@Test
public void testGetProvider() throws APIManagementException, RegistryException {
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
Mockito.when(APIUtil.getMountedPath((RegistryContext) Mockito.anyObject(), Mockito.anyString())).thenReturn("testPath");
UserRegistry userReg = Mockito.mock(UserRegistry.class);
Resource resource = Mockito.mock(Resource.class);
Mockito.when(apiProvider.registry.get("testPath/providers/testProvider")).thenReturn(resource);
Mockito.when(resource.getUUID()).thenReturn("testID");
PowerMockito.when(APIUtil.getArtifactManager(userReg, APIConstants.API_KEY)).thenReturn(artifactManager);
GenericArtifact providerArtifact = Mockito.mock(GenericArtifact.class);
Mockito.when(artifactManager.getGenericArtifact("testID")).thenReturn(providerArtifact);
Provider provider = Mockito.mock(Provider.class);
Mockito.when(APIUtil.getProvider(providerArtifact)).thenReturn(provider);
Assert.assertNotNull(apiProvider.getProvider("testProvider"));
}
use of org.wso2.carbon.registry.core.session.UserRegistry in project carbon-apimgt by wso2.
the class APIProviderImplTest method testGetSecurityAuditAttributesFromTenantConfig.
/**
* This method tests the retrieval of API Security Audit Properties from the Tenant Config
* (i.e. tenant-conf.json)
* @throws RegistryException
* @throws APIManagementException
*/
@Test
public void testGetSecurityAuditAttributesFromTenantConfig() throws RegistryException, APIManagementException {
// Instantiating required variables
final int tenantId = -1234;
String apiToken = "1234f0ca-9879-112f-0e8f-a098e0do12456";
String collectionId = "467f8ca-40f8-4baf-8b0f-c6854ed04653";
boolean overrideGlobal = true;
// Sample JSONObject for mocking
JSONObject jsonObject = new JSONObject();
jsonObject.put("apiToken", apiToken);
jsonObject.put("collectionId", collectionId);
jsonObject.put("overrideGlobal", overrideGlobal);
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
PowerMockito.mockStatic(ServiceReferenceHolder.class);
RegistryService registryService = Mockito.mock(RegistryService.class);
UserRegistry userRegistry = Mockito.mock(UserRegistry.class);
Resource resource = Mockito.mock(Resource.class);
Mockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
Mockito.when(serviceReferenceHolder.getRegistryService()).thenReturn(registryService);
Mockito.when(registryService.getConfigSystemRegistry(tenantId)).thenReturn(userRegistry);
PowerMockito.mockStatic(APIUtil.class);
Mockito.when(APIUtil.getSecurityAuditAttributesFromRegistry(superTenantDomain)).thenReturn(jsonObject);
// Pass the mock values to the method call
JSONObject jsonObject1 = apiProvider.getSecurityAuditAttributesFromConfig("admin");
// Compare the API Token and Collection ID returned from the method call
Assert.assertEquals(jsonObject1.get(APIConstants.SECURITY_AUDIT_API_TOKEN), apiToken);
Assert.assertEquals(jsonObject1.get(APIConstants.SECURITY_AUDIT_COLLECTION_ID), collectionId);
}
use of org.wso2.carbon.registry.core.session.UserRegistry in project carbon-apimgt by wso2.
the class APIProviderImplTest method testGetCustomInSequences1.
@Test
public void testGetCustomInSequences1() throws Exception {
APIIdentifier apiId = new APIIdentifier("admin", "API1", "1.0.1");
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
mockSequences(APIConstants.API_CUSTOM_INSEQUENCE_LOCATION, APIConstants.API_CUSTOM_SEQUENCE_TYPE_IN, apiId);
List<String> sequenceList = apiProvider.getCustomInSequences();
Assert.assertNotNull(sequenceList);
Assert.assertEquals(1, sequenceList.size());
// OMException when building OMElement
PowerMockito.when(APIUtil.buildOMElement(any(InputStream.class))).thenThrow(new OMException());
apiProvider.getCustomOutSequences(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(APIConstants.API_CUSTOM_INSEQUENCE_LOCATION)).thenThrow(org.wso2.carbon.registry.api.RegistryException.class);
String msg = "Error while processing the in in the registry";
try {
apiProvider.getCustomInSequences();
} 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.getCustomInSequences();
} catch (APIManagementException e) {
Assert.assertEquals(msg1, e.getMessage());
}
}
use of org.wso2.carbon.registry.core.session.UserRegistry in project carbon-apimgt by wso2.
the class APIProviderImplTest method testAddFileToDocumentation.
/**
* This method tests adding file to documentation method.
* @throws Exception
*
* @throws GovernanceException Governance Exception.
*/
@Test
public void testAddFileToDocumentation() throws Exception {
String apiUUID = "xxxxxxxx";
String docUUID = "yyyyyyyy";
APIIdentifier identifier = new APIIdentifier("admin-AT-carbon.super", "API1", "1.0.0");
Set<String> environments = new HashSet<String>();
Set<URITemplate> uriTemplates = new HashSet<URITemplate>();
Tier tier = new Tier("Gold");
Map<String, Tier> tiers = new TreeMap<>();
tiers.put("Gold", tier);
URITemplate uriTemplate1 = new URITemplate();
uriTemplate1.setHTTPVerb("POST");
uriTemplate1.setAuthType("Application");
uriTemplate1.setUriTemplate("/add");
uriTemplate1.setThrottlingTier("Gold");
uriTemplates.add(uriTemplate1);
final API api = new API(identifier);
api.setStatus(APIConstants.CREATED);
api.setVisibility("public");
api.setAccessControl("all");
api.setTransports("http,https");
api.setContext("/test");
api.setEnvironments(environments);
api.setUriTemplates(uriTemplates);
api.setOrganization("carbon.super");
List<Documentation> documentationList = getDocumentationList();
final APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apiPersistenceInstance, apimgtDAO, scopesDAO, documentationList, null);
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);
Mockito.when(APIUtil.getTiers(APIConstants.TIER_RESOURCE_TYPE, "carbon.super")).thenReturn(tiers);
Mockito.when(artifactManager.newGovernanceArtifact(any(QName.class))).thenReturn(artifact);
Mockito.when(APIUtil.createAPIArtifactContent(artifact, api)).thenReturn(artifact);
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);
String fileName = "test.txt";
String contentType = "application/force-download";
Documentation doc = new Documentation(DocumentationType.HOWTO, fileName);
doc.setSourceType(DocumentSourceType.FILE);
PowerMockito.when(APIUtil.getDocumentationFilePath(api.getId(), fileName)).thenReturn("filePath");
InputStream inputStream = Mockito.mock(InputStream.class);
// apiProvider.addFileToDocumentation(api.getId(), doc, fileName, inputStream, contentType);
DocumentationContent content = new DocumentationContent();
ResourceFile resourceFile = new ResourceFile(inputStream, contentType);
content.setResourceFile(resourceFile);
apiProvider.addDocumentationContent(apiUUID, docUUID, "carbon.super", content);
}
Aggregations