use of org.wso2.carbon.apimgt.api.APIProvider in project carbon-apimgt by wso2.
the class APIProviderImplTest method testCreateNewAPIVersion_Exception.
@Test(expected = APIManagementException.class)
public void testCreateNewAPIVersion_Exception() throws RegistryException, UserStoreException, APIManagementException, IOException, DuplicateAPIException, APIPersistenceException, XMLStreamException {
// Create Original API
APIIdentifier apiId = new APIIdentifier("admin", "API1", "1.0.0");
API api = new API(apiId);
api.setContext("/test");
api.setVisibility("Public");
api.setStatus(APIConstants.CREATED);
String newVersion = "1.0.0";
// Create new API object
APIIdentifier newApiId = new APIIdentifier("admin", "API1", "1.0.1");
final API newApi = new API(newApiId);
newApi.setStatus(APIConstants.CREATED);
newApi.setContext("/test");
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);
GenericArtifact artifactNew = Mockito.mock(GenericArtifact.class);
Mockito.when(APIUtil.createAPIArtifactContent(artifact, newApi)).thenReturn(artifactNew);
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);
String targetPath = APIConstants.API_LOCATION + RegistryConstants.PATH_SEPARATOR + api.getId().getProviderName() + RegistryConstants.PATH_SEPARATOR + api.getId().getApiName() + RegistryConstants.PATH_SEPARATOR + newVersion + APIConstants.API_RESOURCE_NAME;
String apiSourcePath = "API1/1.0.0/";
PowerMockito.when(APIUtil.getAPIPath(apiId)).thenReturn(apiSourcePath);
Mockito.when(apiProvider.registry.resourceExists(targetPath)).thenThrow(RegistryException.class);
apiProvider.createNewAPIVersion(api.getUuid(), newVersion, false, "carbon.super");
}
use of org.wso2.carbon.apimgt.api.APIProvider in project carbon-apimgt by wso2.
the class APIProviderImplTest method testGetCustomApiInSequences.
@Test
public void testGetCustomApiInSequences() 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.getCustomApiInSequences(apiId);
Assert.assertEquals(1, sequenceList.size());
// OMException when building OMElement
PowerMockito.when(APIUtil.buildOMElement(any(InputStream.class))).thenThrow(new OMException());
apiProvider.getCustomApiInSequences(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_IN))).thenThrow(org.wso2.carbon.registry.api.RegistryException.class);
String msg = "Error while processing the " + APIConstants.API_CUSTOM_SEQUENCE_TYPE_IN + " sequences of " + apiId + " in the registry";
try {
apiProvider.getCustomApiInSequences(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.getCustomApiInSequences(apiId);
} catch (APIManagementException e) {
Assert.assertEquals(msg1, e.getMessage());
}
}
use of org.wso2.carbon.apimgt.api.APIProvider in project carbon-apimgt by wso2.
the class APIProviderImplTest method testCreateNewAPIVersion.
@Test
public void testCreateNewAPIVersion() throws Exception {
// Create Original API
APIIdentifier apiId = new APIIdentifier("admin", "API1", "1.0.0");
API api = new API(apiId);
api.setContext("/test");
api.setVisibility("Public");
api.setStatus(APIConstants.CREATED);
api.setWsdlUrl("https://localhost:9443/services/echo?wsdl");
api.setOrganization("carbon.super");
long time = System.currentTimeMillis();
String newVersion = "1.0.1";
// Create new API object
APIIdentifier newApiId = new APIIdentifier("admin", "API1", "1.0.1");
final API newApi = new API(newApiId);
newApi.setStatus(APIConstants.CREATED);
newApi.setContext("/test");
newApi.setWsdlUrl("/registry/resource/_system/governance/apimgt/applicationdata/wsdls/admin--API11.0.0.wsdl");
// Create Documentation List
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(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);
GenericArtifact artifactNew = Mockito.mock(GenericArtifact.class);
Mockito.when(APIUtil.createAPIArtifactContent(artifact, newApi)).thenReturn(artifactNew);
PublisherAPI publisherAPI = Mockito.mock(PublisherAPI.class);
Mockito.when(publisherAPI.getVersionTimestamp()).thenReturn(String.valueOf(time));
PowerMockito.when(apiPersistenceInstance.addAPI(any(Organization.class), any(PublisherAPI.class))).thenReturn(publisherAPI);
API returnedAPI = apiProvider.addAPI(api);
Assert.assertTrue(StringUtils.isNotEmpty(returnedAPI.getVersionTimestamp()));
String targetPath = APIConstants.API_LOCATION + RegistryConstants.PATH_SEPARATOR + api.getId().getProviderName() + RegistryConstants.PATH_SEPARATOR + api.getId().getApiName() + RegistryConstants.PATH_SEPARATOR + newVersion + APIConstants.API_RESOURCE_NAME;
String apiSourcePath = APIConstants.API_LOCATION + RegistryConstants.PATH_SEPARATOR + apiId.getProviderName() + RegistryConstants.PATH_SEPARATOR + apiId.getApiName() + RegistryConstants.PATH_SEPARATOR + apiId.getVersion() + APIConstants.API_RESOURCE_NAME;
PowerMockito.when(APIUtil.getAPIPath(apiId)).thenReturn(apiSourcePath);
String apiSourceUUID = "87ty543-899hyt";
Mockito.when(apiProvider.registry.resourceExists(targetPath)).thenReturn(false);
Mockito.doNothing().when(apiProvider.registry).beginTransaction();
Mockito.doNothing().when(apiProvider.registry).commitTransaction();
Resource apiSourceArtifact = Mockito.mock(Resource.class);
Mockito.when(apiProvider.registry.get(apiSourcePath)).thenReturn(apiSourceArtifact);
// Mocking Old API retrieval
Mockito.when(apiSourceArtifact.getUUID()).thenReturn(apiSourceUUID);
Mockito.when(artifact.getAttribute(APIConstants.API_OVERVIEW_STATUS)).thenReturn("PUBLISHED");
Mockito.when(artifact.getAttribute(APIConstants.API_OVERVIEW_CONTEXT)).thenReturn("test");
Mockito.when(artifact.getAttribute(APIConstants.API_OVERVIEW_CONTEXT_TEMPLATE)).thenReturn("test/{version}");
Mockito.when(artifact.getAttribute(APIConstants.API_OVERVIEW_WEBSOCKET)).thenReturn("false");
Mockito.when(artifact.getAttribute(APIConstants.API_OVERVIEW_VISIBLE_ROLES)).thenReturn("admin, subscriber");
Mockito.when(artifactManager.getGenericArtifact(apiSourceUUID)).thenReturn(artifact);
// Mocking thumbnail
String thumbUrl = APIConstants.API_IMAGE_LOCATION + RegistryConstants.PATH_SEPARATOR + api.getId().getProviderName() + RegistryConstants.PATH_SEPARATOR + api.getId().getApiName() + RegistryConstants.PATH_SEPARATOR + api.getId().getVersion() + RegistryConstants.PATH_SEPARATOR + APIConstants.API_ICON_IMAGE;
Resource image = Mockito.mock(Resource.class);
Mockito.when(apiProvider.registry.get(thumbUrl)).thenReturn(image);
Mockito.when(apiProvider.registry.resourceExists(thumbUrl)).thenReturn(true);
// Mocking In sequence retrieval
String inSeqFilePath = "API1/1.0.0/in";
PowerMockito.when(APIUtil.getSequencePath(api.getId(), "in")).thenReturn(inSeqFilePath);
Mockito.when(apiProvider.registry.resourceExists(inSeqFilePath)).thenReturn(true);
Collection inSeqCollection = Mockito.mock(Collection.class);
Mockito.when(apiProvider.registry.get(inSeqFilePath)).thenReturn(inSeqCollection);
String[] inSeqChildPaths = { "path1" };
Mockito.when(inSeqCollection.getChildren()).thenReturn(inSeqChildPaths);
Mockito.when(apiProvider.registry.get(inSeqChildPaths[0])).thenReturn(apiSourceArtifact);
InputStream responseStream = IOUtils.toInputStream("<sequence name=\"in-seq\"></sequence>", "UTF-8");
OMElement seqElment = buildOMElement(responseStream);
PowerMockito.when(APIUtil.buildOMElement(responseStream)).thenReturn(seqElment);
Mockito.when(apiSourceArtifact.getContentStream()).thenReturn(responseStream);
// Mocking Out sequence retrieval
Resource apiSourceArtifact1 = Mockito.mock(Resource.class);
String outSeqFilePath = "API1/1.0.0/out";
PowerMockito.when(APIUtil.getSequencePath(api.getId(), "out")).thenReturn(outSeqFilePath);
Mockito.when(apiProvider.registry.resourceExists(outSeqFilePath)).thenReturn(true);
Collection outSeqCollection = Mockito.mock(Collection.class);
Mockito.when(apiProvider.registry.get(outSeqFilePath)).thenReturn(outSeqCollection);
String[] outSeqChildPaths = { "path2" };
Mockito.when(outSeqCollection.getChildren()).thenReturn(outSeqChildPaths);
Mockito.when(apiProvider.registry.get(outSeqChildPaths[0])).thenReturn(apiSourceArtifact1);
InputStream responseStream2 = IOUtils.toInputStream("<sequence name=\"in-seq\"></sequence>", "UTF-8");
OMElement seqElment2 = buildOMElement(responseStream2);
PowerMockito.when(APIUtil.buildOMElement(responseStream2)).thenReturn(seqElment2);
Mockito.when(apiSourceArtifact1.getContentStream()).thenReturn(responseStream2);
// Mock Adding new API artifact with new version
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
apiProvider.createAPI(newApi);
return null;
}
}).when(artifactManager).addGenericArtifact(artifact);
Mockito.doNothing().when(artifact).attachLifecycle(APIConstants.API_LIFE_CYCLE);
PowerMockito.when(APIUtil.getAPIProviderPath(api.getId())).thenReturn("/dummy/provider/path");
Mockito.doNothing().when(apiProvider.registry).addAssociation("/dummy/provider/path", targetPath, APIConstants.PROVIDER_ASSOCIATION);
PowerMockito.when(GovernanceUtils.getArtifactPath(apiProvider.registry, artifact.getId())).thenReturn(artifactPath);
PowerMockito.doNothing().when(APIUtil.class);
String[] roles = { "admin", "subscriber" };
APIUtil.setResourcePermissions("admin", "Public", roles, artifactPath);
// Mock no tags case
Mockito.when(apiProvider.registry.getTags(apiSourcePath)).thenReturn(null);
// Mock WSDL retrieval
String wsdlUrl = APIUtil.getWSDLDefinitionFilePath(api.getId().getApiName(), api.getId().getVersion(), api.getId().getProviderName());
PowerMockito.when(apiProvider.registry.resourceExists(wsdlUrl)).thenReturn(true);
// Mock new API retrieval
String newApiPath = "API1/1.0.1/";
PowerMockito.when(APIUtil.getAPIPath(newApi.getId())).thenReturn(newApiPath);
String newApiUUID = "87ty543-899hy23";
GenericArtifact newArtifact = Mockito.mock(GenericArtifact.class);
Resource newApiResource = Mockito.mock(Resource.class);
Mockito.when(newApiResource.getUUID()).thenReturn(newApiUUID);
Mockito.when(apiProvider.registry.get(newApiPath)).thenReturn(newApiResource);
Mockito.when(artifactManager.getGenericArtifact(newApiUUID)).thenReturn(newArtifact);
PowerMockito.when(APIUtil.getAPI(newArtifact, apiProvider.registry, api.getId(), "test")).thenReturn(newApi);
// Swagger resource
String resourcePath = APIUtil.getOpenAPIDefinitionFilePath(api.getId().getApiName(), api.getId().getVersion(), api.getId().getProviderName());
Mockito.when(apiProvider.registry.resourceExists(resourcePath + APIConstants.API_OAS_DEFINITION_RESOURCE_NAME)).thenReturn(true);
PowerMockito.mockStatic(OASParserUtil.class);
Mockito.when(OASParserUtil.getAPIDefinition(apiId, apiProvider.registry)).thenReturn("{\"info\": {\"swagger\":\"data\"}}");
Mockito.doNothing().when(artifactManager).updateGenericArtifact(artifact);
// WSDL
String newWsdlResourcePath = APIUtil.getWSDLDefinitionFilePath(newApi.getId().getApiName(), newApi.getId().getVersion(), newApi.getId().getProviderName());
PowerMockito.when(apiProvider.registry.copy(resourcePath, newWsdlResourcePath)).thenReturn(newWsdlResourcePath);
// Mock Config system registry
PowerMockito.when(tenantManager.getTenantId(Matchers.anyString())).thenReturn(-1234);
AuthorizationManager authManager = Mockito.mock(AuthorizationManager.class);
UserRealm userRealm = Mockito.mock(UserRealm.class);
PowerMockito.when(realmService.getTenantUserRealm(-1234)).thenReturn(userRealm);
PowerMockito.when(userRealm.getAuthorizationManager()).thenReturn(authManager);
}
use of org.wso2.carbon.apimgt.api.APIProvider in project carbon-apimgt by wso2.
the class APIProviderImplTest method prepareForGetAPIsByProvider.
/**
* This method can be used when invoking getAPIsByProvider()
*/
private void prepareForGetAPIsByProvider(GenericArtifactManager artifactManager, APIProviderImplWrapper apiProvider, String providerId, API api1, API api2) throws APIManagementException, RegistryException {
String providerPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + providerId;
// Mocking API1 association
Association association1 = Mockito.mock(Association.class);
String apiPath1 = "/API1/1.0.0";
Resource resource1 = Mockito.mock(Resource.class);
String apiArtifactId1 = "76897689";
Mockito.when(apiProvider.registry.resourceExists(apiPath1)).thenReturn(true);
Mockito.when(association1.getDestinationPath()).thenReturn(apiPath1);
Mockito.when(apiProvider.registry.get(apiPath1)).thenReturn(resource1);
Mockito.when(resource1.getUUID()).thenReturn(apiArtifactId1);
GenericArtifact apiArtifact1 = Mockito.mock(GenericArtifact.class);
Mockito.when(artifactManager.getGenericArtifact(apiArtifactId1)).thenReturn(apiArtifact1);
Mockito.when(APIUtil.getAPI(apiArtifact1, apiProvider.registry)).thenReturn(api1);
// Mocking API2 association
Association association2 = Mockito.mock(Association.class);
String apiPath2 = "/API2/1.0.0";
Resource resource2 = Mockito.mock(Resource.class);
String apiArtifactId2 = "76897622";
Mockito.when(apiProvider.registry.resourceExists(apiPath2)).thenReturn(true);
Mockito.when(association2.getDestinationPath()).thenReturn(apiPath2);
Mockito.when(apiProvider.registry.get(apiPath2)).thenReturn(resource2);
Mockito.when(resource2.getUUID()).thenReturn(apiArtifactId2);
GenericArtifact apiArtifact2 = Mockito.mock(GenericArtifact.class);
Mockito.when(artifactManager.getGenericArtifact(apiArtifactId2)).thenReturn(apiArtifact2);
Mockito.when(APIUtil.getAPI(apiArtifact2, apiProvider.registry)).thenReturn(api2);
Association[] associactions = { association1, association2 };
Mockito.when(apiProvider.registry.getAssociations(providerPath, APIConstants.PROVIDER_ASSOCIATION)).thenReturn(associactions);
}
use of org.wso2.carbon.apimgt.api.APIProvider in project carbon-apimgt by wso2.
the class APIProviderImplTest method testGetSecurityAuditAttributesFromAllConfigs.
/**
* This method tests the retrieval of API Security Audit Properties when both the tenant and global
* configs contain values
* @throws APIManagementException
* @throws RegistryException
*/
@Test
public void testGetSecurityAuditAttributesFromAllConfigs() throws APIManagementException, RegistryException {
// Mock values from global config
String username = "admin";
String apiToken = "2780f0ca-3423-435f-0e9f-a634e0do65915";
String collectionId = "8750f8ca-34f9-4baf-8b9f-c6854ed06534";
String global = "true";
// Mock retrieving the tenant domain
PowerMockito.mockStatic(MultitenantUtils.class);
Mockito.when(MultitenantUtils.getTenantDomain(username)).thenReturn("carbon.super");
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
// Create a mock APIManagerConfiguration Object for retrieving properties from the deployment.toml
APIManagerConfiguration apiManagerConfiguration = PowerMockito.mock(APIManagerConfiguration.class);
ServiceReferenceHolder serviceReferenceHolder = PowerMockito.mock(ServiceReferenceHolder.class);
APIManagerConfigurationService apiManagerConfigurationService = PowerMockito.mock(APIManagerConfigurationService.class);
PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
Mockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
// Mock the properties read from the deployment.toml
Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.API_SECURITY_AUDIT_API_TOKEN)).thenReturn(apiToken);
Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.API_SECURITY_AUDIT_CID)).thenReturn(collectionId);
Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.API_SECURITY_AUDIT_GLOBAL)).thenReturn(global);
// Mock values from tenant config
final int tenantId = -1234;
String apiToken1 = "1234f0ca-9879-112f-0e8f-a098e0do12456";
String collectionId1 = "467f8ca-40f8-4baf-8b0f-c6854ed04653";
boolean overrideGlobal = true;
// Sample JSONObject for mocking
JSONObject jsonObject = new JSONObject();
jsonObject.put("apiToken", apiToken1);
jsonObject.put("collectionId", collectionId1);
jsonObject.put("overrideGlobal", overrideGlobal);
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);
PowerMockito.mockStatic(APIUtil.class);
Mockito.when(APIUtil.getSecurityAuditAttributesFromRegistry(superTenantDomain)).thenReturn(jsonObject);
// Test the object to be returned when overrideGlobal is true
JSONObject jsonObject1 = apiProvider.getSecurityAuditAttributesFromConfig("admin");
Assert.assertEquals(jsonObject1.get(APIConstants.SECURITY_AUDIT_API_TOKEN), apiToken1);
Assert.assertEquals(jsonObject1.get(APIConstants.SECURITY_AUDIT_COLLECTION_ID), collectionId1);
// Test the object to be returned when overrideGlobal is false
jsonObject.put("overrideGlobal", false);
JSONObject jsonObject2 = apiProvider.getSecurityAuditAttributesFromConfig("admin");
Assert.assertEquals(jsonObject2.get(APIConstants.SECURITY_AUDIT_API_TOKEN), apiToken);
Assert.assertEquals(jsonObject2.get(APIConstants.SECURITY_AUDIT_COLLECTION_ID), collectionId);
}
Aggregations