use of org.wso2.carbon.user.api.Tenant in project carbon-apimgt by wso2.
the class APIProviderImplTest method testGetSubscriberClaims.
@Test
public void testGetSubscriberClaims() throws APIManagementException, UserStoreException, XMLStreamException {
String configuredClaims = "http://wso2.org/claim1,http://wso2.org/claim2";
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
// Mock retrieving the tenant domain
PowerMockito.mockStatic(MultitenantUtils.class);
PowerMockito.mockStatic(APIUtil.class);
Mockito.when(MultitenantUtils.getTenantDomain("admin")).thenReturn("carbon.super");
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.getRealmService()).thenReturn(realmService);
Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
PowerMockito.when(tenantManager.getTenantId(Matchers.anyString())).thenReturn(-1234);
UserStoreManager userStoreManager = Mockito.mock(UserStoreManager.class);
UserRealm userRealm = Mockito.mock(UserRealm.class);
PowerMockito.when(realmService.getTenantUserRealm(-1234)).thenReturn(userRealm);
PowerMockito.when(userRealm.getUserStoreManager()).thenReturn(userStoreManager);
PowerMockito.when(userStoreManager.isExistingUser("admin")).thenReturn(true);
SortedMap<String, String> claimValues = new TreeMap<String, String>();
claimValues.put("claim1", "http://wso2.org/claim1");
claimValues.put("claim2", "http://wso2.org/claim2");
claimValues.put("claim3", "http://wso2.org/claim3");
PowerMockito.when(APIUtil.getClaims("admin", -1234, DEFAULT_DIALECT_URI)).thenReturn(claimValues);
APIManagerConfiguration configuration = Mockito.mock(APIManagerConfiguration.class);
APIManagerConfigurationService configurationService = Mockito.mock(APIManagerConfigurationService.class);
PowerMockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(configurationService);
PowerMockito.when(configurationService.getAPIManagerConfiguration()).thenReturn(configuration);
Mockito.when(configuration.getFirstProperty(APIConstants.API_PUBLISHER_SUBSCRIBER_CLAIMS)).thenReturn(configuredClaims);
Map subscriberClaims = apiProvider.getSubscriberClaims("admin");
assertNotNull(subscriberClaims);
assertEquals(configuredClaims.split(",").length, subscriberClaims.size());
}
use of org.wso2.carbon.user.api.Tenant in project carbon-apimgt by wso2.
the class SOAPOperationBindingUtils method isSOAPToRESTApi.
/**
* Checks the api is a soap to rest converted one or a soap pass through
* <p>
* Note: This method directly called from the jaggery layer
*
* @param name api name
* @param version api version
* @param provider api provider
* @return true if the api is soap to rest converted one. false if the user have a pass through
* @throws APIManagementException if an error occurs when accessing the registry
*/
public static boolean isSOAPToRESTApi(String name, String version, String provider) throws APIManagementException {
provider = (provider != null ? provider.trim() : null);
name = (name != null ? name.trim() : null);
version = (version != null ? version.trim() : null);
boolean isTenantFlowStarted = false;
try {
String tenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(provider));
if (tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
isTenantFlowStarted = true;
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
}
RegistryService registryService = ServiceReferenceHolder.getInstance().getRegistryService();
int tenantId;
UserRegistry registry;
try {
tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomain);
APIUtil.loadTenantRegistry(tenantId);
registry = registryService.getGovernanceSystemRegistry(tenantId);
String resourcePath = APIConstants.API_LOCATION + RegistryConstants.PATH_SEPARATOR + APIUtil.replaceEmailDomain(provider) + RegistryConstants.PATH_SEPARATOR + name + RegistryConstants.PATH_SEPARATOR + version + RegistryConstants.PATH_SEPARATOR + SOAPToRESTConstants.SOAP_TO_REST_RESOURCE;
if (log.isDebugEnabled()) {
log.debug("Resource path to the soap to rest converted sequence: " + resourcePath);
}
return registry.resourceExists(resourcePath);
} catch (RegistryException e) {
handleException("Error when create registry instance", e);
} catch (UserStoreException e) {
handleException("Error while reading tenant information", e);
}
} finally {
if (isTenantFlowStarted) {
PrivilegedCarbonContext.endTenantFlow();
}
}
return false;
}
use of org.wso2.carbon.user.api.Tenant in project carbon-apimgt by wso2.
the class SequenceUtils method updateRestToSoapConvertedSequences.
/**
* Updates the api sequences where user will be able to edits the generated sequences
* <p>
* Note: this method is directly invoked from the jaggery layer
*
* @param name api name
* @param version api version
* @param provider api provider
* @param seqType to identify the sequence is whether in/out sequence
* @param content sequence content
* @throws APIManagementException throws exceptions on unsuccessful persistence in registry or json parsing of the content
*/
public static void updateRestToSoapConvertedSequences(String name, String version, String provider, String seqType, String content) throws APIManagementException {
if (provider != null) {
provider = APIUtil.replaceEmailDomain(provider);
}
provider = (provider != null ? provider.trim() : null);
name = (name != null ? name.trim() : null);
version = (version != null ? version.trim() : null);
boolean isTenantFlowStarted = false;
JSONParser jsonParser = new JSONParser();
try {
String tenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(provider));
if (tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
isTenantFlowStarted = true;
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
}
RegistryService registryService = ServiceReferenceHolder.getInstance().getRegistryService();
int tenantId;
UserRegistry registry;
tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomain);
APIUtil.loadTenantRegistry(tenantId);
registry = registryService.getGovernanceSystemRegistry(tenantId);
JSONObject sequences = (JSONObject) jsonParser.parse(content);
for (Object sequence : sequences.keySet()) {
String sequenceContent = (String) ((JSONObject) sequences.get(sequence)).get("content");
String resourcePath = APIConstants.API_LOCATION + RegistryConstants.PATH_SEPARATOR + APIUtil.replaceEmailDomain(provider) + RegistryConstants.PATH_SEPARATOR + name + RegistryConstants.PATH_SEPARATOR + version + RegistryConstants.PATH_SEPARATOR + SOAPToRESTConstants.SOAP_TO_REST_RESOURCE + RegistryConstants.PATH_SEPARATOR + seqType + RegistryConstants.PATH_SEPARATOR + sequence + ".xml";
Resource regResource;
if (registry.resourceExists(resourcePath)) {
regResource = registry.get(resourcePath);
regResource.setContent(sequenceContent);
if (log.isDebugEnabled()) {
log.debug("Api sequence content for " + resourcePath + " is: " + sequenceContent);
}
regResource.setMediaType("text/xml");
registry.put(resourcePath, regResource);
}
}
} catch (ParseException e) {
handleException("Error occurred while parsing the sequence json", e);
} catch (UserStoreException e) {
handleException("Error while reading tenant information", e);
} catch (RegistryException e) {
handleException("Error when create registry instance", e);
} catch (org.wso2.carbon.registry.api.RegistryException e) {
handleException("Error while creating registry resource", e);
} finally {
if (isTenantFlowStarted) {
PrivilegedCarbonContext.endTenantFlow();
}
}
}
use of org.wso2.carbon.user.api.Tenant in project carbon-apimgt by wso2.
the class APIAdminImplTest method updateTenantConfigNegative2.
// invalid json
@Test
public void updateTenantConfigNegative2() throws Exception {
String tenantConf = "{\"hello\"";
PowerMockito.doNothing().when(APIUtil.class, "validateRestAPIScopes", tenantConf);
APIAdmin apiAdmin = new APIAdminImpl();
Schema schema = Mockito.mock(Schema.class);
PowerMockito.when(APIUtil.class, "retrieveTenantConfigJsonSchema").thenReturn(schema);
Mockito.doNothing().when(apimConfigService).updateTenantConfig("abc.com", tenantConf);
try {
apiAdmin.updateTenantConfig("abc.com", tenantConf);
Assert.fail("Method successfully invoked");
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "tenant-config validation failure");
}
}
use of org.wso2.carbon.user.api.Tenant in project carbon-apimgt by wso2.
the class APIAdminImplTest method updateTenantConfigNegative3.
// valid json element missing
@Test
public void updateTenantConfigNegative3() throws Exception {
String tenantConf = "{\"hello\":\"world\"}";
PowerMockito.doNothing().when(APIUtil.class, "validateRestAPIScopes", tenantConf);
APIAdmin apiAdmin = new APIAdminImpl();
Schema schema = Mockito.mock(Schema.class);
PowerMockito.when(APIUtil.class, "retrieveTenantConfigJsonSchema").thenReturn(schema);
Mockito.doThrow(ValidationException.class).when(schema).validate(Mockito.any());
Mockito.doNothing().when(apimConfigService).updateTenantConfig("abc.com", tenantConf);
try {
apiAdmin.updateTenantConfig("abc.com", tenantConf);
Assert.fail("Method successfully invoked");
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "tenant-config validation failure");
}
}
Aggregations