use of org.wso2.carbon.apimgt.impl.dao.ScopesDAO in project carbon-apimgt by wso2.
the class APIProviderImplTest method testGetAllPaginatedAPIs.
@Test
public void testGetAllPaginatedAPIs() throws RegistryException, UserStoreException, APIManagementException, XMLStreamException {
APIIdentifier apiId1 = new APIIdentifier("admin", "API1", "1.0.0");
API api1 = new API(apiId1);
api1.setContext("/test");
APIIdentifier apiId2 = new APIIdentifier("admin", "API2", "1.0.0");
API api2 = new API(apiId2);
api2.setContext("/test1");
PaginationContext paginationCtx = Mockito.mock(PaginationContext.class);
PowerMockito.when(PaginationContext.getInstance()).thenReturn(paginationCtx);
Mockito.when(paginationCtx.getLength()).thenReturn(2);
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
ServiceReferenceHolder sh = TestUtils.getServiceReferenceHolder();
RegistryService registryService = Mockito.mock(RegistryService.class);
PowerMockito.when(sh.getRegistryService()).thenReturn(registryService);
UserRegistry userReg = Mockito.mock(UserRegistry.class);
PowerMockito.when(registryService.getGovernanceUserRegistry(CarbonConstants.REGISTRY_ANONNYMOUS_USERNAME, -1234)).thenReturn(userReg);
PowerMockito.when(APIUtil.getArtifactManager(userReg, APIConstants.API_KEY)).thenReturn(artifactManager);
APIManagerConfigurationService amConfigService = Mockito.mock(APIManagerConfigurationService.class);
APIManagerConfiguration amConfig = Mockito.mock(APIManagerConfiguration.class);
PowerMockito.when(sh.getAPIManagerConfigurationService()).thenReturn(amConfigService);
PowerMockito.when(amConfigService.getAPIManagerConfiguration()).thenReturn(amConfig);
PowerMockito.when(amConfig.getFirstProperty(APIConstants.API_PUBLISHER_APIS_PER_PAGE)).thenReturn("2");
RealmService realmService = Mockito.mock(RealmService.class);
TenantManager tm = Mockito.mock(TenantManager.class);
PowerMockito.when(sh.getRealmService()).thenReturn(realmService);
PowerMockito.when(realmService.getTenantManager()).thenReturn(tm);
PowerMockito.when(tm.getTenantId("carbon.super")).thenReturn(-1234);
GenericArtifact genericArtifact1 = Mockito.mock(GenericArtifact.class);
GenericArtifact genericArtifact2 = Mockito.mock(GenericArtifact.class);
Mockito.when(APIUtil.getAPI(genericArtifact1)).thenReturn(api1);
Mockito.when(APIUtil.getAPI(genericArtifact2)).thenReturn(api2);
List<GovernanceArtifact> governanceArtifacts = new ArrayList<GovernanceArtifact>();
governanceArtifacts.add(genericArtifact1);
governanceArtifacts.add(genericArtifact2);
List<GovernanceArtifact> governanceArtifacts1 = new ArrayList<GovernanceArtifact>();
PowerMockito.when(GovernanceUtils.findGovernanceArtifacts(Mockito.anyMap(), any(Registry.class), Mockito.anyString())).thenReturn(governanceArtifacts, governanceArtifacts1);
Map<String, Object> result = apiProvider.getAllPaginatedAPIs("carbon.super", 0, 10);
List<API> apiList = (List<API>) result.get("apis");
Assert.assertEquals(2, apiList.size());
Assert.assertEquals("API1", apiList.get(0).getId().getApiName());
Assert.assertEquals("API2", apiList.get(1).getId().getApiName());
Assert.assertEquals(2, result.get("totalLength"));
// No APIs available
Map<String, Object> result1 = apiProvider.getAllPaginatedAPIs("carbon.super", 0, 10);
List<API> apiList1 = (List<API>) result1.get("apis");
Assert.assertEquals(0, apiList1.size());
// Registry Exception while retrieving artifacts
Mockito.when(artifactManager.findGenericArtifacts(Matchers.anyMap())).thenThrow(RegistryException.class);
try {
apiProvider.getAllPaginatedAPIs("carbon.super", 0, 10);
} catch (APIManagementException e) {
Assert.assertEquals("Failed to get all APIs", e.getMessage());
}
}
use of org.wso2.carbon.apimgt.impl.dao.ScopesDAO in project carbon-apimgt by wso2.
the class APIProviderImplTest method testGetCustomOutSequences1.
@Test
public void testGetCustomOutSequences1() 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.getCustomOutSequences();
Assert.assertNotNull(sequenceList);
Assert.assertEquals(1, sequenceList.size());
// OMException when building OMElement
PowerMockito.when(APIUtil.buildOMElement(any(InputStream.class))).thenThrow(new OMException());
apiProvider.getCustomOutSequences();
// 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_OUTSEQUENCE_LOCATION)).thenThrow(org.wso2.carbon.registry.api.RegistryException.class);
String msg = "Error while processing the out in the registry";
try {
apiProvider.getCustomOutSequences();
} 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.getCustomOutSequences();
} catch (APIManagementException e) {
Assert.assertEquals(msg1, e.getMessage());
}
}
use of org.wso2.carbon.apimgt.impl.dao.ScopesDAO in project carbon-apimgt by wso2.
the class APIProviderImplTest method testRemoveDocumentation1.
@Test
public void testRemoveDocumentation1() throws APIManagementException, RegistryException {
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apiPersistenceInstance, apimgtDAO, scopesDAO, null, null);
APIIdentifier apiId = new APIIdentifier("admin", "API1", "1.0.1");
PowerMockito.when(APIUtil.getArtifactManager(apiProvider.registry, APIConstants.DOCUMENTATION_KEY)).thenReturn(artifactManager);
GenericArtifact artifact = Mockito.mock(GenericArtifact.class);
Mockito.when(artifactManager.getGenericArtifact("testId")).thenReturn(artifact);
Mockito.when(artifact.getPath()).thenReturn("docPath");
Mockito.when(artifact.getAttribute(APIConstants.DOC_FILE_PATH)).thenReturn("docFilePath");
Association association = Mockito.mock(Association.class);
Association[] associations = new Association[] { association };
Mockito.when(apiProvider.registry.getAssociations("docPath", APIConstants.DOCUMENTATION_KEY)).thenReturn(associations);
apiProvider.removeDocumentation(apiId, "testId", "carbon.super");
Mockito.verify(apiProvider.registry);
}
use of org.wso2.carbon.apimgt.impl.dao.ScopesDAO in project carbon-apimgt by wso2.
the class APIProviderImplTest method testGetExternalAPIStores.
@Test
public void testGetExternalAPIStores() throws APIManagementException {
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
APIIdentifier apiId = new APIIdentifier("admin", "API1", "1.0.1");
String uuid = UUID.randomUUID().toString();
PowerMockito.when(APIUtil.isAPIsPublishToExternalAPIStores(-1)).thenReturn(true, false);
Set<APIStore> apiStores = new HashSet<APIStore>();
APIStore apiStore = new APIStore();
apiStore.setDisplayName("testName");
apiStore.setName("testStoreName");
apiStores.add(apiStore);
APIStore apiStore1 = new APIStore();
apiStore1.setDisplayName("testName1");
apiStore1.setName("testStoreName1");
apiStores.add(apiStore1);
Mockito.when(apimgtDAO.getExternalAPIStoresDetails(uuid)).thenReturn(apiStores);
Assert.assertNotNull(apiProvider.getExternalAPIStores(uuid));
// return null
Assert.assertNull(apiProvider.getExternalAPIStores(uuid));
}
use of org.wso2.carbon.apimgt.impl.dao.ScopesDAO in project carbon-apimgt by wso2.
the class APIProviderImplTest method testUpdateAPI_InPublishedState.
@Test
public void testUpdateAPI_InPublishedState() throws Exception {
APIIdentifier identifier = new APIIdentifier("admin-AT-carbon.super", "API1", "1.0.0");
Set<String> environments = new HashSet<String>();
environments.add("PRODUCTION");
Set<String> newEnvironments = new HashSet<String>();
newEnvironments.add("SANDBOX");
Set<URITemplate> uriTemplates = new HashSet<URITemplate>();
Set<URITemplate> newUriTemplates = new HashSet<URITemplate>();
URITemplate uriTemplate1 = new URITemplate();
uriTemplate1.setHTTPVerb("POST");
uriTemplate1.setAuthType("Application");
uriTemplate1.setUriTemplate("/add");
uriTemplate1.setThrottlingTier("Gold");
uriTemplates.add(uriTemplate1);
URITemplate uriTemplate2 = new URITemplate();
uriTemplate2.setHTTPVerb("PUT");
uriTemplate2.setAuthType("Application");
uriTemplate2.setUriTemplate("/update");
uriTemplate2.setThrottlingTier("Gold");
newUriTemplates.add(uriTemplate1);
newUriTemplates.add(uriTemplate2);
final API api = new API(identifier);
api.setStatus(APIConstants.PUBLISHED);
api.setVisibility("private");
api.setVisibleRoles("admin");
api.setAccessControl("all");
api.setTransports("http,https");
api.setContext("/test");
api.setEnvironments(newEnvironments);
api.setUriTemplates(newUriTemplates);
api.setOrganization("carbon.super");
API oldApi = new API(identifier);
oldApi.setStatus(APIConstants.PUBLISHED);
oldApi.setVisibility("public");
oldApi.setAccessControl("all");
oldApi.setContext("/test");
oldApi.setEnvironments(environments);
oldApi.setOrganization("carbon.super");
api.setUriTemplates(uriTemplates);
JSONObject jsonObject = new JSONObject();
jsonObject.put("test", "new_test");
api.setAdditionalProperties(jsonObject);
api.addProperty("secured", "false");
Tier tier = new Tier("Gold");
Map<String, Tier> tiers = new TreeMap<>();
tiers.put("Gold", tier);
Mockito.when(APIUtil.getTiers(APIConstants.TIER_RESOURCE_TYPE, "carbon.super")).thenReturn(tiers);
List<Documentation> documentationList = getDocumentationList();
Documentation documentation = documentationList.get(1);
Mockito.when(APIUtil.getAPIDocPath(api.getId())).thenReturn(documentation.getFilePath());
APIProviderImplWrapper apiProviderImplWrapper = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
Resource docResource = Mockito.mock(Resource.class);
Mockito.when(docResource.getUUID()).thenReturn(documentation.getId());
Mockito.when(apiProviderImplWrapper.registry.get(documentation.getFilePath())).thenReturn(docResource);
GenericArtifact docArtifact = Mockito.mock(GenericArtifact.class);
Mockito.when(artifactManager.getGenericArtifact(documentation.getId())).thenReturn(docArtifact);
Mockito.when(APIUtil.getDocumentation(docArtifact)).thenReturn(documentation);
Mockito.when(docArtifact.getPath()).thenReturn(artifactPath);
PowerMockito.doNothing().when(APIUtil.class, "clearResourcePermissions", Mockito.any(), Mockito.any(), Mockito.anyInt());
String[] roles = { "admin", "subscriber" };
APIUtil.setResourcePermissions("admin", "Public", roles, artifactPath);
Mockito.when(docArtifact.getAttribute(APIConstants.DOC_FILE_PATH)).thenReturn("docFilePath");
final APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apiPersistenceInstance, apimgtDAO, scopesDAO, documentationList, null);
Mockito.when(artifactManager.newGovernanceArtifact(any(QName.class))).thenReturn(artifact);
Mockito.when(APIUtil.createAPIArtifactContent(artifact, oldApi)).thenReturn(artifact);
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(oldApi);
RegistryAuthorizationManager registryAuthorizationManager = Mockito.mock(RegistryAuthorizationManager.class);
PowerMockito.whenNew(RegistryAuthorizationManager.class).withAnyArguments().thenReturn(registryAuthorizationManager);
// mock has permission
Resource apiSourceArtifact = Mockito.mock(Resource.class);
Mockito.when(apiSourceArtifact.getUUID()).thenReturn("12640983654");
String apiSourcePath = "path";
PowerMockito.when(APIUtil.getAPIPath(api.getId())).thenReturn(apiSourcePath);
PowerMockito.when(APIUtil.getAPIPath(oldApi.getId())).thenReturn(apiSourcePath);
PowerMockito.when(apiProvider.registry.get(apiSourcePath)).thenReturn(apiSourceArtifact);
// API Status is CREATED and user has permission
Mockito.when(artifact.getAttribute(APIConstants.API_OVERVIEW_STATUS)).thenReturn("PUBLISHED");
Mockito.when(artifact.getAttribute(APIConstants.API_OVERVIEW_ENDPOINT_USERNAME)).thenReturn("user1");
Mockito.when(artifact.getAttribute(APIConstants.API_OVERVIEW_ENDPOINT_PASSWORD)).thenReturn("password");
Mockito.when(artifactManager.getGenericArtifact(apiSourceArtifact.getUUID())).thenReturn(artifact);
PowerMockito.when(APIUtil.hasPermission(null, APIConstants.Permissions.API_PUBLISH)).thenReturn(true);
PowerMockito.when(APIUtil.hasPermission(null, APIConstants.Permissions.API_CREATE)).thenReturn(true);
Mockito.when(apimgtDAO.getDefaultVersion(identifier)).thenReturn("1.0.0");
Mockito.when(apimgtDAO.getPublishedDefaultVersion(identifier)).thenReturn("1.0.0");
// updateDefaultAPIInRegistry
String defaultAPIPath = APIConstants.API_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getApiName() + RegistryConstants.PATH_SEPARATOR + identifier.getVersion() + APIConstants.API_RESOURCE_NAME;
Resource defaultAPISourceArtifact = Mockito.mock(Resource.class);
String defaultAPIUUID = "12640983600";
Mockito.when(defaultAPISourceArtifact.getUUID()).thenReturn(defaultAPIUUID);
Mockito.when(apiProvider.registry.get(defaultAPIPath)).thenReturn(defaultAPISourceArtifact);
GenericArtifact defaultAPIArtifact = Mockito.mock(GenericArtifact.class);
Mockito.when(artifactManager.getGenericArtifact(defaultAPIUUID)).thenReturn(defaultAPIArtifact);
Mockito.doNothing().when(artifactManager).updateGenericArtifact(defaultAPIArtifact);
TestUtils.mockAPIMConfiguration(APIConstants.API_GATEWAY_TYPE, APIConstants.API_GATEWAY_TYPE_SYNAPSE, -1234);
// updateApiArtifact
PowerMockito.when(APIUtil.createAPIArtifactContent(artifact, api)).thenReturn(artifact);
Mockito.when(artifact.getId()).thenReturn("12640983654");
PowerMockito.when(GovernanceUtils.getArtifactPath(apiProvider.registry, "12640983654")).thenReturn(apiSourcePath);
// Mock Updating API
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
apiProvider.createAPI(api);
return null;
}
}).when(artifactManager).updateGenericArtifact(artifact);
// Mocking API already not published and published
PowerMockito.mockStatic(OASParserUtil.class);
Mockito.when(OASParserUtil.getAPIDefinition(api.getId(), apiProvider.registry)).thenReturn("{\"info\": {\"swagger\":\"data\"}}");
APIManagerConfiguration config = ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService().getAPIManagerConfiguration();
GatewayArtifactSynchronizerProperties synchronizerProperties = new GatewayArtifactSynchronizerProperties();
Mockito.when(config.getGatewayArtifactSynchronizerProperties()).thenReturn(synchronizerProperties);
PowerMockito.when(apiPersistenceInstance.getPublisherAPI(any(Organization.class), any(String.class))).thenReturn(publisherAPI);
apiProvider.updateAPI(api, oldApi);
Assert.assertEquals(1, api.getEnvironments().size());
Assert.assertEquals(true, api.getEnvironments().contains("SANDBOX"));
// Previous updateAPI() call enabled API security. Therefore need to set it as false for the second test
api.setEndpointSecured(false);
apiProvider.updateAPI(api, oldApi);
Assert.assertEquals(1, api.getEnvironments().size());
Assert.assertEquals(true, api.getEnvironments().contains("SANDBOX"));
// Test WSDL endpoint API
api.setEndpointConfig(EP_CONFIG_WSDL);
PowerMockito.when(APIUtil.isValidWSDLURL(WSDL_URL, true)).thenReturn(true);
PowerMockito.when(APIUtil.createWSDL(apiProvider.registry, api)).thenReturn("wsdl_path");
apiProvider.updateAPI(api, oldApi);
Assert.assertEquals(1, api.getEnvironments().size());
Assert.assertEquals(true, api.getEnvironments().contains("SANDBOX"));
Assert.assertEquals("Additional properties that are set are not retrieved new_test", "new_test", api.getAdditionalProperties().get("test"));
Assert.assertEquals("Additional properties that are set are not retrieved new_test", "false", api.getAdditionalProperties().get("secured"));
}
Aggregations