use of org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO in project carbon-apimgt by wso2.
the class APIProviderImplTest method testCheckIfAPIExists.
@Test
public void testCheckIfAPIExists() throws APIManagementException, UserStoreException, RegistryException, XMLStreamException {
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
APIIdentifier apiId = new APIIdentifier("admin", "API1", "1.0.1");
Mockito.when(APIUtil.getAPIPath(apiId)).thenReturn("testPath");
PowerMockito.mockStatic(MultitenantUtils.class);
PowerMockito.when(MultitenantUtils.getTenantDomain(Mockito.anyString())).thenReturn("abc.org");
// Mock Config system registry
ServiceReferenceHolder sh = TestUtils.getServiceReferenceHolder();
RegistryService registryService = Mockito.mock(RegistryService.class);
PowerMockito.when(sh.getRegistryService()).thenReturn(registryService);
UserRegistry systemReg = Mockito.mock(UserRegistry.class);
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(Matchers.anyString())).thenReturn(-1234);
PowerMockito.when(sh.getRegistryService()).thenReturn(registryService);
PowerMockito.when(registryService.getGovernanceSystemRegistry(-1234)).thenReturn(systemReg);
Mockito.when(systemReg.resourceExists("testPath")).thenReturn(true);
Assert.assertEquals(true, apiProvider.checkIfAPIExists(apiId));
PowerMockito.when(MultitenantUtils.getTenantDomain(Mockito.anyString())).thenReturn("carbon.super");
apiProvider.tenantDomain = "carbon.super1";
PowerMockito.when(registryService.getGovernanceUserRegistry("admin", -1234)).thenReturn(systemReg);
Assert.assertEquals(true, apiProvider.checkIfAPIExists(apiId));
apiProvider.tenantDomain = null;
apiProvider.registry = systemReg;
Assert.assertEquals(true, apiProvider.checkIfAPIExists(apiId));
}
use of org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO in project carbon-apimgt by wso2.
the class APIProviderImplTest method testAddAPINameWithIllegalCharacters.
@Test
public void testAddAPINameWithIllegalCharacters() throws APIManagementException, GovernanceException {
APIIdentifier apiId = new APIIdentifier("admin", "API2&", "1.0.2");
API api = new API(apiId);
api.setContext("/test");
api.setStatus(APIConstants.CREATED);
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
Mockito.when(artifactManager.newGovernanceArtifact(any(QName.class))).thenReturn(artifact);
Mockito.when(APIUtil.createAPIArtifactContent(artifact, api)).thenReturn(artifact);
try {
apiProvider.addAPI(api);
Assert.fail("Exception was expected, but wasn't thrown");
} catch (APIManagementException e) {
Assert.assertTrue(e.getMessage().contains("API Name contains one or more illegal characters"));
}
}
use of org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO in project carbon-apimgt by wso2.
the class AbstractAPIManagerTestCase method testAddSubscriber.
@Test
public void testAddSubscriber() throws APIManagementException, org.wso2.carbon.user.api.UserStoreException {
int tenantId = -1234;
AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(null, null, null, tenantManager, apiMgtDAO);
Mockito.when(tenantManager.getTenantId(Mockito.anyString())).thenThrow(UserStoreException.class).thenReturn(tenantId);
PowerMockito.mockStatic(APIUtil.class);
SortedMap<String, String> claimValues = new TreeMap<String, String>();
claimValues.put("admin@wso2.om", APIConstants.EMAIL_CLAIM);
PowerMockito.when(APIUtil.getClaims(API_PROVIDER, tenantId, DEFAULT_DIALECT_URI)).thenReturn(claimValues);
try {
abstractAPIManager.addSubscriber(API_PROVIDER, SAMPLE_RESOURCE_ID);
Assert.fail("User store exception not thrown for error scenario");
} catch (APIManagementException e) {
Assert.assertTrue(e.getMessage().contains("Error while adding the subscriber"));
}
Mockito.doThrow(APIManagementException.class).doNothing().when(apiMgtDAO).addSubscriber((Subscriber) Mockito.any(), Mockito.anyString());
try {
abstractAPIManager.addSubscriber(API_PROVIDER, SAMPLE_RESOURCE_ID);
Assert.fail("APIM exception not thrown for error scenario");
} catch (APIManagementException e) {
Assert.assertTrue(e.getMessage().contains("Error while adding the subscriber"));
}
PowerMockito.mockStatic(APIUtil.class);
PowerMockito.when(APIUtil.isEnabledUnlimitedTier()).thenReturn(true, false);
Mockito.doNothing().when(apiMgtDAO).addSubscriber((Subscriber) Mockito.any(), Mockito.anyString());
abstractAPIManager.addSubscriber(API_PROVIDER, SAMPLE_RESOURCE_ID);
List<Tier> tierValues = new ArrayList<Tier>();
tierValues.add(new Tier("Gold"));
tierValues.add(new Tier("Silver"));
Map<String, Tier> tierMap = new HashMap<String, Tier>();
tierMap.put("Gold", new Tier("Gold"));
tierMap.put("Silver", new Tier("Silver"));
PowerMockito.when(APIUtil.getTiers(Mockito.anyInt(), Mockito.anyString())).thenReturn(tierMap);
PowerMockito.when(APIUtil.sortTiers(Mockito.anySet())).thenReturn(tierValues);
abstractAPIManager.addSubscriber(API_PROVIDER, SAMPLE_RESOURCE_ID);
Mockito.verify(apiMgtDAO, Mockito.times(3)).addSubscriber((Subscriber) Mockito.any(), Mockito.anyString());
}
use of org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO in project carbon-apimgt by wso2.
the class AbstractAPIManagerTestCase method testIsAPIAvailable.
@Test
public void testIsAPIAvailable() throws APIManagementException {
APIIdentifier apiIdentifier = getAPIIdentifier(SAMPLE_API_NAME, API_PROVIDER, SAMPLE_API_VERSION);
String organization = "org1";
String path = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + apiIdentifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + apiIdentifier.getApiName() + RegistryConstants.PATH_SEPARATOR + apiIdentifier.getVersion();
AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(apiMgtDAO);
Mockito.when(apiMgtDAO.getUUIDFromIdentifier(apiIdentifier, organization)).thenReturn("xxxxx");
Assert.assertTrue(abstractAPIManager.isAPIAvailable(apiIdentifier, organization));
}
use of org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO in project carbon-apimgt by wso2.
the class AbstractAPIManagerTestCase method init.
@Before
public void init() {
System.setProperty(CARBON_HOME, "");
privilegedCarbonContext = Mockito.mock(PrivilegedCarbonContext.class);
PowerMockito.mockStatic(PrivilegedCarbonContext.class);
PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(privilegedCarbonContext);
PowerMockito.mockStatic(GovernanceUtils.class);
paginationContext = Mockito.mock(PaginationContext.class);
PowerMockito.mockStatic(PaginationContext.class);
PowerMockito.when(PaginationContext.getInstance()).thenReturn(paginationContext);
apiMgtDAO = Mockito.mock(ApiMgtDAO.class);
scopesDAO = Mockito.mock(ScopesDAO.class);
registry = Mockito.mock(Registry.class);
genericArtifactManager = Mockito.mock(GenericArtifactManager.class);
registryService = Mockito.mock(RegistryService.class);
tenantManager = Mockito.mock(TenantManager.class);
graphQLSchemaDefinition = Mockito.mock(GraphQLSchemaDefinition.class);
keyManager = Mockito.mock(KeyManager.class);
apiPersistenceInstance = Mockito.mock(APIPersistence.class);
PowerMockito.mockStatic(KeyManagerHolder.class);
KeyManagerDto keyManagerDto = new KeyManagerDto();
keyManagerDto.setName("default");
keyManagerDto.setKeyManager(keyManager);
keyManagerDto.setIssuer("https://localhost");
Map<String, KeyManagerDto> tenantKeyManagerDtoMap = new HashMap<>();
tenantKeyManagerDtoMap.put("default", keyManagerDto);
PowerMockito.when(KeyManagerHolder.getTenantKeyManagers("carbon.super")).thenReturn(tenantKeyManagerDtoMap);
}
Aggregations