org.wso2.carbon.registry.core.config.RegistryContext Java Examples

The following examples show how to use org.wso2.carbon.registry.core.config.RegistryContext. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BaseWebAppAuthenticatorFrameworkTest.java    From carbon-device-mgt with Apache License 2.0 6 votes vote down vote up
/**
 * To get the registry service.
 * @return RegistryService
 * @throws RegistryException Registry Exception
 */
private  RegistryService getRegistryService() throws RegistryException, UserStoreException {
    RealmService realmService = new InMemoryRealmService();
    AuthenticatorFrameworkDataHolder.getInstance().setRealmService(realmService);
    UserStoreManager userStoreManager = AuthenticatorFrameworkDataHolder.getInstance().getRealmService()
            .getTenantUserRealm(MultitenantConstants.SUPER_TENANT_ID).getUserStoreManager();
    Permission adminPermission = new Permission(PermissionUtils.ADMIN_PERMISSION_REGISTRY_PATH,
            CarbonConstants.UI_PERMISSION_ACTION);
    userStoreManager.addRole(ADMIN_ROLE + "t", new String[] { ADMIN_USER }, new Permission[] { adminPermission });
    RegistryDataHolder.getInstance().setRealmService(realmService);
    DeviceManagementDataHolder.getInstance().setRealmService(realmService);
    InputStream is = BaseWebAppAuthenticatorFrameworkTest.class.getClassLoader()
            .getResourceAsStream("carbon-home/repository/conf/registry.xml");
    RegistryContext context = RegistryContext.getBaseInstance(is, realmService);
    context.setSetup(true);
    return context.getEmbeddedRegistryService();
}
 
Example #2
Source File: DeviceAccessAuthorizationServiceTest.java    From carbon-device-mgt with Apache License 2.0 6 votes vote down vote up
private RegistryService getRegistryService() throws RegistryException, UserStoreException {
    RealmService realmService = new InMemoryRealmService();
    PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(ADMIN_USER);
    BasicDataSource dataSource = new BasicDataSource();
    String connectionUrl = "jdbc:h2:./target/databasetest/CARBON_TEST";
    dataSource.setUrl(connectionUrl);
    dataSource.setDriverClassName(DRIVER_CLASS_NAME);
    JDBCTenantManager jdbcTenantManager = new JDBCTenantManager(dataSource,
            MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
    realmService.setTenantManager(jdbcTenantManager);
    RegistryDataHolder.getInstance().setRealmService(realmService);
    DeviceManagementDataHolder.getInstance().setRealmService(realmService);
    InputStream is = this.getClass().getClassLoader().getResourceAsStream("carbon-home/repository/conf/registry.xml");
    RegistryContext context = RegistryContext.getBaseInstance(is, realmService);
    context.setSetup(true);
    return context.getEmbeddedRegistryService();
}
 
Example #3
Source File: CarbonBasedTestListener.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
/**
 * Creates the regostry service if not available.
 *
 * @param realClass
 * @return
 * @throws RegistryException
 */
private RegistryService createRegistryService(Class realClass, int tenantID, String tenantDomain) throws
        RegistryException {

    if (registryService != null) {
        return registryService;
    }

    try {
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain);
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenantID);

        RegistryContext registryContext = RegistryContext.getBaseInstance(IdentityTenantUtil.getRealmService());
        DataSource dataSource = MockInitialContextFactory
                .initializeDatasource(REG_DB_JNDI_NAME, realClass, new String[]{REG_DB_SQL_FILE});
        registryContext.setDataAccessManager(new JDBCDataAccessManager(dataSource));
        registryService = new EmbeddedRegistryService(registryContext);

        OSGiDataHolder.getInstance().setRegistryService(registryService);
        CarbonCoreDataHolder.getInstance().setRegistryService(registryService);
        PrivilegedCarbonContext.getThreadLocalCarbonContext()
                .setRegistry(RegistryType.USER_GOVERNANCE, registryService.getRegistry());

        return registryService;
    } catch (TestCreationException e) {
        log.error("Could not load registry data", e);
        throw new RegistryException("Could not load registry data", e);
    }
}
 
Example #4
Source File: Utils.java    From carbon-device-mgt with Apache License 2.0 5 votes vote down vote up
/**
 * To get the registry service.
 * @return RegistryService
 * @throws RegistryException Registry Exception
 */
public static RegistryService getRegistryService() throws RegistryException {
    RealmService realmService = new InMemoryRealmService();
    RegistryDataHolder.getInstance().setRealmService(realmService);
    DeviceManagementDataHolder.getInstance().setRealmService(realmService);
    InputStream is = Utils.class.getClassLoader().getResourceAsStream("carbon-home/repository/conf/registry.xml");
    RegistryContext context = RegistryContext.getBaseInstance(is, realmService);
    context.setSetup(true);
    return context.getEmbeddedRegistryService();
}
 
Example #5
Source File: TestUtils.java    From carbon-device-mgt with Apache License 2.0 5 votes vote down vote up
public static RegistryService getRegistryService(Class clazz) throws RegistryException {
    RealmService realmService = new InMemoryRealmService();
    RegistryDataHolder.getInstance().setRealmService(realmService);
    DeviceManagementDataHolder.getInstance().setRealmService(realmService);
    InputStream is = clazz.getClassLoader().getResourceAsStream("carbon-home/repository/conf/registry.xml");
    RegistryContext context = RegistryContext.getBaseInstance(is, realmService);
    context.setSetup(true);
    return context.getEmbeddedRegistryService();
}
 
Example #6
Source File: DeviceManagementProviderServiceTest.java    From carbon-device-mgt with Apache License 2.0 5 votes vote down vote up
private RegistryService getRegistryService() throws RegistryException {
    RealmService realmService = new InMemoryRealmService();
    RegistryDataHolder.getInstance().setRealmService(realmService);
    DeviceManagementDataHolder.getInstance().setRealmService(realmService);
    InputStream is = this.getClass().getClassLoader().getResourceAsStream("carbon-home/repository/conf/registry.xml");
    RegistryContext context = RegistryContext.getBaseInstance(is, realmService);
    context.setSetup(true);
    return context.getEmbeddedRegistryService();
}
 
Example #7
Source File: BaseDeviceManagementTest.java    From carbon-device-mgt with Apache License 2.0 5 votes vote down vote up
private RegistryService getRegistryService() throws RegistryException {
    RealmService realmService = new InMemoryRealmService();
    RegistryDataHolder.getInstance().setRealmService(realmService);
    DeviceManagementDataHolder.getInstance().setRealmService(realmService);
    InputStream is = this.getClass().getClassLoader().getResourceAsStream("carbon-home/repository/conf/registry.xml");
    RegistryContext context = RegistryContext.getBaseInstance(is, realmService);
    context.setSetup(true);
    return context.getEmbeddedRegistryService();
}
 
Example #8
Source File: NotificationManagementServiceImplTests.java    From carbon-device-mgt with Apache License 2.0 5 votes vote down vote up
private RegistryService getRegistryService() throws RegistryException {
    RealmService realmService = new InMemoryRealmService();
    RegistryDataHolder.getInstance().setRealmService(realmService);
    DeviceManagementDataHolder.getInstance().setRealmService(realmService);
    InputStream is = this.getClass().getClassLoader().getResourceAsStream("carbon-home/repository/conf/registry.xml");
    RegistryContext context = RegistryContext.getBaseInstance(is, realmService);
    context.setSetup(true);
    return context.getEmbeddedRegistryService();
}
 
Example #9
Source File: BaseAPIPublisherTest.java    From carbon-device-mgt with Apache License 2.0 5 votes vote down vote up
private RegistryService getRegistryService() throws RegistryException, UserStoreException {
    RealmService realmService = new InMemoryRealmService();
    APIPublisherDataHolder.getInstance().setRealmService(realmService);
    RegistryDataHolder.getInstance().setRealmService(realmService);
    JWTClientExtensionDataHolder.getInstance().setRealmService(realmService);
    InputStream is = this.getClass().getClassLoader().getResourceAsStream("carbon-home/repository/" +
            "conf/registry.xml");
    RegistryContext context = RegistryContext.getBaseInstance(is, realmService);
    context.setSetup(true);
    return context.getEmbeddedRegistryService();
}
 
Example #10
Source File: BasePolicyManagementDAOTest.java    From carbon-device-mgt with Apache License 2.0 5 votes vote down vote up
protected RegistryService getRegistryService() throws RegistryException {
    RealmService realmService = new InMemoryRealmService();
    RegistryDataHolder.getInstance().setRealmService(realmService);
    DeviceManagementDataHolder.getInstance().setRealmService(realmService);
    InputStream is = this.getClass().getClassLoader().getResourceAsStream(
            "carbon-home/repository/conf/registry.xml");
    RegistryContext context = RegistryContext.getBaseInstance(is, realmService);
    context.setSetup(true);
    return context.getEmbeddedRegistryService();
}
 
Example #11
Source File: BaseJWTClientManagerExtTest.java    From carbon-device-mgt with Apache License 2.0 5 votes vote down vote up
private RegistryService getRegistryService() throws RegistryException {
    RealmService realmService = new InMemoryRealmService();
    RegistryDataHolder.getInstance().setRealmService(realmService);
    JWTClientExtensionDataHolder.getInstance().setRealmService(realmService);
    InputStream is = this.getClass().getClassLoader().getResourceAsStream("carbon-home/repository/conf/registry.xml");
    RegistryContext context = RegistryContext.getBaseInstance(is, realmService);
    context.setSetup(true);
    return context.getEmbeddedRegistryService();
}
 
Example #12
Source File: APIManagerComponent.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
private void setupImagePermissions() throws APIManagementException {
    try {
        AuthorizationManager accessControlAdmin = ServiceReferenceHolder.getInstance().getRealmService().getTenantUserRealm(MultitenantConstants.SUPER_TENANT_ID).getAuthorizationManager();
        String imageLocation = APIUtil.getMountedPath(RegistryContext.getBaseInstance(), RegistryConstants.GOVERNANCE_REGISTRY_BASE_PATH) + APIConstants.API_IMAGE_LOCATION;
        if (!accessControlAdmin.isRoleAuthorized(CarbonConstants.REGISTRY_ANONNYMOUS_ROLE_NAME, imageLocation, ActionConstants.GET)) {
            // Can we get rid of this?
            accessControlAdmin.authorizeRole(CarbonConstants.REGISTRY_ANONNYMOUS_ROLE_NAME, imageLocation, ActionConstants.GET);
        }
    } catch (UserStoreException e) {
        throw new APIManagementException("Error while setting up permissions for image collection", e);
    }
}
 
Example #13
Source File: AbstractAPIManagerTestCase.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Test
public void testConstructor() throws Exception {

    ServiceReferenceHolderMockCreator holderMockCreator = new ServiceReferenceHolderMockCreator(1);
    ServiceReferenceHolderMockCreator.initContextService();
    holderMockCreator.initRegistryServiceMockCreator(false, new Object());
    RegistryAuthorizationManager registryAuthorizationManager = Mockito.mock(RegistryAuthorizationManager.class);
    Mockito.doThrow(UserStoreException.class).doNothing().when(registryAuthorizationManager)
            .authorizeRole(Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
    PowerMockito.whenNew(RegistryAuthorizationManager.class).withAnyArguments()
            .thenReturn(registryAuthorizationManager);
    PowerMockito.mockStatic(RegistryUtils.class);
    PowerMockito.when(RegistryUtils.getAbsolutePath((RegistryContext) Mockito.any(), Mockito.anyString()))
            .thenReturn("/test");
    try {
        new AbstractAPIManager(null) {
            @Override
            public String getGraphqlSchema(APIIdentifier apiId) throws APIManagementException {
                return null;
            }
        };
        Assert.fail("User store exception not thrown for error scenario");
    } catch (APIManagementException e) {
        Assert.assertTrue(e.getMessage().contains("Error while setting the permissions"));
    }

    PowerMockito.mockStatic(APIUtil.class);
    PowerMockito.doNothing().when(APIUtil.class, "loadTenantRegistry", Mockito.anyInt());
    PowerMockito.mockStatic(MultitenantUtils.class);
    PowerMockito.when(MultitenantUtils.getTenantDomain(Mockito.anyString())).thenReturn(SAMPLE_TENANT_DOMAIN_1);
    String userName = "admin";

    Mockito.verify(
            holderMockCreator.getRegistryServiceMockCreator().getMock().getConfigSystemRegistry(Mockito.anyInt()),
            Mockito.atLeastOnce());
}
 
Example #14
Source File: APIManagerComponentImagePermissionTest.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() throws Exception {
    PowerMockito.mockStatic(ServiceReferenceHolder.class);
    PowerMockito.mockStatic(APIUtil.class);
    PowerMockito.mockStatic(CarbonContext.class);
    PowerMockito.mockStatic(RegistryUtils.class);
    PowerMockito.mockStatic(APIMgtDBUtil.class);
    authManager = Mockito.mock(AuthorizationManager.class);
    serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
    realmService = Mockito.mock(RealmService.class);
    componentContext = Mockito.mock(ComponentContext.class);
    BundleContext bundleContext = Mockito.mock(BundleContext.class);
    UserRealm userRealm = Mockito.mock(UserRealm.class);
    Registry registry = Mockito.mock(Registry.class);
    CarbonContext carbonContext = Mockito.mock(CarbonContext.class);
    APIManagerConfiguration configuration = Mockito.mock(APIManagerConfiguration.class);

    Mockito.when(componentContext.getBundleContext()).thenReturn(bundleContext);
    Mockito.when(realmService.getTenantUserRealm(MultitenantConstants.SUPER_TENANT_ID)).thenReturn(userRealm);
    Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
    Mockito.when(userRealm.getAuthorizationManager()).thenReturn(authManager);
    Mockito.when(registry.resourceExists(Mockito.anyString())).thenReturn(true);
    Mockito.when(carbonContext.getRegistry(RegistryType.USER_GOVERNANCE)).thenReturn(registry);
    Mockito.doNothing().when(configuration).load(Mockito.anyString());
    Mockito.when(configuration.getFirstProperty(Mockito.anyString())).thenReturn("");
    PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    PowerMockito.when(APIUtil.getMountedPath(null, "")).thenReturn("");
    PowerMockito.when(RegistryUtils.getAbsolutePath(null, null)).thenReturn("");
    PowerMockito.when(CarbonContext.getThreadLocalCarbonContext()).thenReturn(carbonContext);
    PowerMockito.when(APIUtil.getMountedPath(Mockito.any(RegistryContext.class), Mockito.anyString()))
            .thenReturn("");
    PowerMockito.doThrow(new IndexOutOfBoundsException()).when(APIMgtDBUtil.class, "initialize");
    PowerMockito.whenNew(APIManagerConfiguration.class).withAnyArguments().thenReturn(configuration);
}