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();
}
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();
}
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);
}
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();
}
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);
}
}
Aggregations