Search in sources :

Example 41 with UserRegistry

use of org.wso2.carbon.registry.core.session.UserRegistry in project carbon-apimgt by wso2.

the class APIUtilTest method testGetMediationSequenceUuidCustomSequence.

@Test
public void testGetMediationSequenceUuidCustomSequence() throws Exception {
    APIIdentifier apiIdentifier = Mockito.mock(APIIdentifier.class);
    ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
    RegistryService registryService = Mockito.mock(RegistryService.class);
    UserRegistry registry = Mockito.mock(UserRegistry.class);
    PowerMockito.mockStatic(ServiceReferenceHolder.class);
    Mockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    Mockito.when(serviceReferenceHolder.getRegistryService()).thenReturn(registryService);
    Mockito.when(registryService.getGovernanceSystemRegistry(eq(1))).thenReturn(registry);
    Collection collection = Mockito.mock(Collection.class);
    String artifactPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + apiIdentifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + apiIdentifier.getApiName() + RegistryConstants.PATH_SEPARATOR + apiIdentifier.getVersion();
    String path = artifactPath + RegistryConstants.PATH_SEPARATOR + "custom" + RegistryConstants.PATH_SEPARATOR;
    Mockito.when(registry.get(eq(path))).thenReturn(collection);
    String[] childPaths = { "test" };
    Mockito.when(collection.getChildren()).thenReturn(childPaths);
    String expectedUUID = UUID.randomUUID().toString();
    InputStream sampleSequence = new FileInputStream(Thread.currentThread().getContextClassLoader().getResource("sampleSequence.xml").getFile());
    Resource resource = Mockito.mock(Resource.class);
    Mockito.when(registry.get(eq("test"))).thenReturn(resource);
    Mockito.when(resource.getContentStream()).thenReturn(sampleSequence);
    Mockito.when(resource.getUUID()).thenReturn(expectedUUID);
    String actualUUID = APIUtil.getMediationSequenceUuid("sample", 1, "custom", apiIdentifier);
    Assert.assertEquals(expectedUUID, actualUUID);
    sampleSequence.close();
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Collection(org.wso2.carbon.registry.core.Collection) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) FileInputStream(java.io.FileInputStream) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 42 with UserRegistry

use of org.wso2.carbon.registry.core.session.UserRegistry in project carbon-apimgt by wso2.

the class APIUtilTest method testGetCustomFaultSequence.

@Test
public void testGetCustomFaultSequence() throws Exception {
    APIIdentifier apiIdentifier = Mockito.mock(APIIdentifier.class);
    ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
    RegistryService registryService = Mockito.mock(RegistryService.class);
    UserRegistry registry = Mockito.mock(UserRegistry.class);
    PowerMockito.mockStatic(ServiceReferenceHolder.class);
    Mockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    Mockito.when(serviceReferenceHolder.getRegistryService()).thenReturn(registryService);
    Mockito.when(registryService.getGovernanceSystemRegistry(eq(1))).thenReturn(registry);
    Collection collection = Mockito.mock(Collection.class);
    String artifactPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + apiIdentifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + apiIdentifier.getApiName() + RegistryConstants.PATH_SEPARATOR + apiIdentifier.getVersion();
    String path = artifactPath + RegistryConstants.PATH_SEPARATOR + "fault" + RegistryConstants.PATH_SEPARATOR;
    Mockito.when(registry.get(eq(path))).thenReturn(collection);
    String[] childPaths = { "test" };
    Mockito.when(collection.getChildren()).thenReturn(childPaths);
    InputStream sampleSequence = new FileInputStream(Thread.currentThread().getContextClassLoader().getResource("sampleSequence.xml").getFile());
    Resource resource = Mockito.mock(Resource.class);
    Mockito.when(registry.get(eq("test"))).thenReturn(resource);
    Mockito.when(resource.getContentStream()).thenReturn(sampleSequence);
    OMElement customSequence = APIUtil.getCustomSequence("sample", 1, "fault", apiIdentifier);
    Assert.assertNotNull(customSequence);
    sampleSequence.close();
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Collection(org.wso2.carbon.registry.core.Collection) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) OMElement(org.apache.axiom.om.OMElement) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) FileInputStream(java.io.FileInputStream) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 43 with UserRegistry

use of org.wso2.carbon.registry.core.session.UserRegistry in project carbon-apimgt by wso2.

the class RegistryCacheInvalidationServiceTestCase method setup.

@Before
public void setup() throws Exception {
    PowerMockito.mockStatic(ServiceReferenceHolder.class);
    PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
    Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
    Mockito.when(tenantManager.getTenantId(tenantDomain)).thenReturn(6543);
    Mockito.when(serviceReferenceHolder.getRegistryService()).thenReturn(registryService);
    Mockito.when(registryService.getGovernanceSystemRegistry(6543)).thenReturn(userRegistry);
    PowerMockito.mockStatic(RegistryUtils.class);
    Mockito.when(userRegistry.getRegistryContext()).thenReturn(registryContext);
    System.setProperty(CARBON_HOME, "");
    PrivilegedCarbonContext privilegedCarbonContext = Mockito.mock(PrivilegedCarbonContext.class);
    PowerMockito.mockStatic(PrivilegedCarbonContext.class);
    PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(privilegedCarbonContext);
}
Also used : PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) Before(org.junit.Before)

Example 44 with UserRegistry

use of org.wso2.carbon.registry.core.session.UserRegistry in project carbon-apimgt by wso2.

the class TenantServiceCreator method createdConfigurationContext.

public void createdConfigurationContext(ConfigurationContext configurationContext) {
    String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
    log.info("Initializing APIM TenantServiceCreator for the tenant domain : " + tenantDomain);
    try {
        // first check which configuration should be active
        org.wso2.carbon.registry.core.Registry registry = (org.wso2.carbon.registry.core.Registry) PrivilegedCarbonContext.getThreadLocalCarbonContext().getRegistry(RegistryType.SYSTEM_CONFIGURATION);
        AxisConfiguration axisConfig = configurationContext.getAxisConfiguration();
        // initialize the lock
        Lock lock = new ReentrantLock();
        axisConfig.addParameter("synapse.config.lock", lock);
        // creates the synapse configuration directory hierarchy if not exists
        // useful at the initial tenant creation
        File tenantAxis2Repo = new File(configurationContext.getAxisConfiguration().getRepository().getFile());
        File synapseConfigsDir = new File(tenantAxis2Repo, "synapse-configs");
        if (!synapseConfigsDir.exists()) {
            if (!synapseConfigsDir.mkdir()) {
                log.fatal("Couldn't create the synapse-config root on the file system " + "for the tenant domain : " + tenantDomain);
                return;
            }
        }
        String synapseConfigsDirLocation = synapseConfigsDir.getAbsolutePath();
        // set the required configuration parameters to initialize the ESB
        axisConfig.addParameter(SynapseConstants.Axis2Param.SYNAPSE_CONFIG_LOCATION, synapseConfigsDirLocation);
        // init the multiple configuration tracker
        ConfigurationManager manger = new ConfigurationManager((UserRegistry) registry, configurationContext);
        manger.init();
        File synapseConfigDir = new File(synapseConfigsDir, manger.getTracker().getCurrentConfigurationName());
        StringBuilder filepath = new StringBuilder();
        filepath.append(synapseConfigsDir).append('/').append(manger.getTracker().getCurrentConfigurationName()).append('/').append(MultiXMLConfigurationBuilder.SEQUENCES_DIR).append('/').append(authFailureHandlerSequenceName).append(".xml");
        File authFailureHandlerSequenceNameFile = new File(filepath.toString());
        // sequence synapse configurations by using resource artifacts
        if (!authFailureHandlerSequenceNameFile.exists()) {
            createTenantSynapseConfigHierarchy(synapseConfigDir, tenantDomain);
        }
        String graphqlFilepath = String.valueOf(synapseConfigsDir) + '/' + manger.getTracker().getCurrentConfigurationName() + '/' + MultiXMLConfigurationBuilder.SEQUENCES_DIR + '/' + graphqlAuthFailureHandlerSequenceName + ".xml";
        File graphqlFailureHandlerSequenceNameFile = new File(graphqlFilepath);
        if (!graphqlFailureHandlerSequenceNameFile.exists()) {
            createTenantSynapseConfigHierarchy(synapseConfigDir, tenantDomain);
        }
        String threatFaultConfigLocation = synapseConfigsDir.getAbsolutePath() + File.separator + manger.getTracker().getCurrentConfigurationName() + File.separator + MultiXMLConfigurationBuilder.SEQUENCES_DIR + File.separator + threatFaultSequenceName + ".xml";
        File threatFaultXml = new File(threatFaultConfigLocation);
        if (!threatFaultXml.exists()) {
            FileUtils.copyFile(new File(synapseConfigRootPath + threatFaultSequenceName + ".xml"), new File(synapseConfigDir.getAbsolutePath() + File.separator + MultiXMLConfigurationBuilder.SEQUENCES_DIR + File.separator + threatFaultSequenceName + ".xml"));
        }
        String blockingSequenceLocation = synapseConfigsDir.getAbsolutePath() + File.separator + manger.getTracker().getCurrentConfigurationName() + File.separator + MultiXMLConfigurationBuilder.SEQUENCES_DIR + File.separator + blockingSequence + ".xml";
        File blockingSequenceXml = new File(blockingSequenceLocation);
        if (!blockingSequenceXml.exists()) {
            FileUtils.copyFile(new File(synapseConfigRootPath + blockingSequence + ".xml"), new File(synapseConfigDir.getAbsolutePath() + File.separator + MultiXMLConfigurationBuilder.SEQUENCES_DIR + File.separator + blockingSequence + ".xml"));
        }
        String backEndFailureSequence = synapseConfigsDir.getAbsolutePath() + File.separator + manger.getTracker().getCurrentConfigurationName() + File.separator + MultiXMLConfigurationBuilder.SEQUENCES_DIR + File.separator + backendFailureSequenceName + ".xml";
        File backendSequenceXml = new File(backEndFailureSequence);
        if (!backendSequenceXml.exists()) {
            FileUtils.copyFile(new File(synapseConfigRootPath + backendFailureSequenceName + ".xml"), new File(synapseConfigDir.getAbsolutePath() + File.separator + MultiXMLConfigurationBuilder.SEQUENCES_DIR + File.separator + backendFailureSequenceName + ".xml"));
        }
        String opaPolicyFailureSequence = synapseConfigsDir.getAbsolutePath() + File.separator + manger.getTracker().getCurrentConfigurationName() + File.separator + MultiXMLConfigurationBuilder.SEQUENCES_DIR + File.separator + opaPolicyFailureHandlerSequenceName + ".xml";
        File opaPolicyFailureSequenceXml = new File(opaPolicyFailureSequence);
        if (!opaPolicyFailureSequenceXml.exists()) {
            FileUtils.copyFile(new File(synapseConfigRootPath + opaPolicyFailureHandlerSequenceName + ".xml"), new File(synapseConfigDir.getAbsolutePath() + File.separator + MultiXMLConfigurationBuilder.SEQUENCES_DIR + File.separator + opaPolicyFailureHandlerSequenceName + ".xml"));
        }
    } catch (RemoteException e) {
        log.error("Failed to create Tenant's synapse sequences.", e);
    } catch (Exception e) {
        log.error("Failed to create Tenant's synapse sequences.", e);
    }
    // Create caches for tenants
    CacheProvider.removeAllCaches();
    CacheProvider.createGatewayKeyCache();
    CacheProvider.createResourceCache();
    CacheProvider.createGatewayTokenCache();
    CacheProvider.createInvalidTokenCache();
    CacheProvider.createGatewayBasicAuthResourceCache();
    CacheProvider.createGatewayUsernameCache();
    CacheProvider.createInvalidUsernameCache();
    // Initialize product REST API token caches
    CacheProvider.createRESTAPITokenCache();
    CacheProvider.createRESTAPIInvalidTokenCache();
    CacheProvider.createGatewayJWTTokenCache();
    CacheProvider.createTenantConfigCache();
    CacheProvider.createRecommendationsCache();
    CacheProvider.createParsedSignJWTCache();
    CacheProvider.createInvalidGatewayApiKeyCache();
    CacheProvider.createGatewayInternalKeyCache();
    CacheProvider.createGatewayInternalKeyDataCache();
    CacheProvider.createInvalidInternalKeyCache();
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock) RemoteException(java.rmi.RemoteException) File(java.io.File) ConfigurationManager(org.wso2.carbon.mediation.initializer.configurations.ConfigurationManager)

Example 45 with UserRegistry

use of org.wso2.carbon.registry.core.session.UserRegistry in project carbon-apimgt by wso2.

the class APIUtil method writeDefinedSequencesToTenantRegistry.

/**
 * Adds the sequences defined in repository/resources/customsequences folder to tenant registry
 *
 * @param tenantID tenant Id
 * @throws APIManagementException
 */
public static void writeDefinedSequencesToTenantRegistry(int tenantID) throws APIManagementException {
    try {
        RegistryService registryService = ServiceReferenceHolder.getInstance().getRegistryService();
        UserRegistry govRegistry = registryService.getGovernanceSystemRegistry(tenantID);
        // Add all custom in,out and fault sequences to tenant registry
        APIUtil.addDefinedAllSequencesToRegistry(govRegistry, APIConstants.API_CUSTOM_SEQUENCE_TYPE_IN);
        APIUtil.addDefinedAllSequencesToRegistry(govRegistry, APIConstants.API_CUSTOM_SEQUENCE_TYPE_OUT);
        APIUtil.addDefinedAllSequencesToRegistry(govRegistry, APIConstants.API_CUSTOM_SEQUENCE_TYPE_FAULT);
    } catch (RegistryException e) {
        throw new APIManagementException("Error while saving defined sequences to the registry of tenant with id " + tenantID, e);
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Aggregations

UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)147 Resource (org.wso2.carbon.registry.core.Resource)96 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)86 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)84 Test (org.junit.Test)75 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)75 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)66 RegistryService (org.wso2.carbon.registry.core.service.RegistryService)65 ServiceReferenceHolder (org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder)53 API (org.wso2.carbon.apimgt.api.model.API)47 UserStoreException (org.wso2.carbon.user.api.UserStoreException)44 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)43 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)41 Registry (org.wso2.carbon.registry.core.Registry)39 IOException (java.io.IOException)38 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)37 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)34 ArrayList (java.util.ArrayList)33 QName (javax.xml.namespace.QName)32 GovernanceException (org.wso2.carbon.governance.api.exception.GovernanceException)32