Search in sources :

Example 76 with Tenant

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());
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) UserRealm(org.wso2.carbon.user.api.UserRealm) RealmService(org.wso2.carbon.user.core.service.RealmService) UserStoreManager(org.wso2.carbon.user.core.UserStoreManager) TreeMap(java.util.TreeMap) TenantManager(org.wso2.carbon.user.core.tenant.TenantManager) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 77 with Tenant

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;
}
Also used : UserStoreException(org.wso2.carbon.user.api.UserStoreException) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 78 with Tenant

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();
        }
    }
}
Also used : Resource(org.wso2.carbon.registry.api.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) JSONObject(org.json.simple.JSONObject) UserStoreException(org.wso2.carbon.user.api.UserStoreException) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject) ParseException(org.json.simple.parser.ParseException) RegistryService(org.wso2.carbon.registry.core.service.RegistryService)

Example 79 with Tenant

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");
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIAdmin(org.wso2.carbon.apimgt.api.APIAdmin) Schema(org.everit.json.schema.Schema) APIUtil(org.wso2.carbon.apimgt.impl.utils.APIUtil) Test(org.junit.Test) BeforeTest(org.testng.annotations.BeforeTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 80 with Tenant

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");
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIAdmin(org.wso2.carbon.apimgt.api.APIAdmin) Schema(org.everit.json.schema.Schema) APIUtil(org.wso2.carbon.apimgt.impl.utils.APIUtil) Test(org.junit.Test) BeforeTest(org.testng.annotations.BeforeTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)180 UserStoreException (org.wso2.carbon.user.api.UserStoreException)88 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)83 ArrayList (java.util.ArrayList)79 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)70 PreparedStatement (java.sql.PreparedStatement)51 SQLException (java.sql.SQLException)50 IOException (java.io.IOException)49 Connection (java.sql.Connection)49 HashMap (java.util.HashMap)44 ResultSet (java.sql.ResultSet)43 JSONObject (org.json.simple.JSONObject)41 Resource (org.wso2.carbon.registry.core.Resource)40 Registry (org.wso2.carbon.registry.core.Registry)38 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)34 API (org.wso2.carbon.apimgt.api.model.API)34 Test (org.junit.Test)33 File (java.io.File)32 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)32 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)30