use of org.wso2.carbon.registry.core.Collection in project carbon-apimgt by wso2.
the class APIManagerComponent method addApplicationsPermissionsToRegistry.
/**
* This method will create new permission name "applications" in registry permission.
*/
private void addApplicationsPermissionsToRegistry() throws APIManagementException {
Registry tenantGovReg = getRegistry();
String permissionResourcePath = CarbonConstants.UI_PERMISSION_NAME + RegistryConstants.PATH_SEPARATOR + APPLICATION_ROOT_PERMISSION;
try {
if (!tenantGovReg.resourceExists(permissionResourcePath)) {
String loggedInUser = CarbonContext.getThreadLocalCarbonContext().getUsername();
UserRealm realm = (UserRealm) CarbonContext.getThreadLocalCarbonContext().getUserRealm();
// Logged in user is not authorized to create the permission.
// Temporarily change the user to the admin for creating the permission
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(realm.getRealmConfiguration().getAdminUserName());
tenantGovReg = CarbonContext.getThreadLocalCarbonContext().getRegistry(RegistryType.USER_GOVERNANCE);
Collection appRootNode = tenantGovReg.newCollection();
appRootNode.setProperty("name", "Applications");
tenantGovReg.put(permissionResourcePath, appRootNode);
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(loggedInUser);
}
} catch (org.wso2.carbon.user.core.UserStoreException e) {
throw new APIManagementException("Error while reading user store information.", e);
} catch (org.wso2.carbon.registry.api.RegistryException e) {
throw new APIManagementException("Error while creating new permission in registry", e);
}
}
use of org.wso2.carbon.registry.core.Collection in project carbon-apimgt by wso2.
the class APIUtilTest method testIsPerAPISequenceNoPathsInCollection.
@Test
public void testIsPerAPISequenceNoPathsInCollection() 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);
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 + "in" + RegistryConstants.PATH_SEPARATOR;
PowerMockito.mockStatic(ServiceReferenceHolder.class);
Mockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
Mockito.when(serviceReferenceHolder.getRegistryService()).thenReturn(registryService);
Mockito.when(registryService.getGovernanceSystemRegistry(eq(1))).thenReturn(registry);
Mockito.when(registry.resourceExists(eq(path))).thenReturn(false);
Collection collection = Mockito.mock(Collection.class);
Mockito.when(registry.get(eq(path))).thenReturn(collection);
String[] childPaths = {};
Mockito.when(collection.getChildren()).thenReturn(childPaths);
boolean isPerAPiSequence = APIUtil.isPerAPISequence("sample", 1, apiIdentifier, "in");
Assert.assertFalse(isPerAPiSequence);
}
use of org.wso2.carbon.registry.core.Collection in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method getSoapToRestSequences.
protected List<SOAPToRestSequence> getSoapToRestSequences(Registry registry, API api, Direction direction) throws RegistryException, APIPersistenceException {
String resourcePath = APIConstants.API_LOCATION + RegistryConstants.PATH_SEPARATOR + RegistryPersistenceUtil.replaceEmailDomain(api.getId().getProviderName()) + RegistryConstants.PATH_SEPARATOR + api.getId().getName() + RegistryConstants.PATH_SEPARATOR + api.getId().getVersion() + RegistryConstants.PATH_SEPARATOR + "soap_to_rest" + RegistryConstants.PATH_SEPARATOR;
if (direction == Direction.IN) {
resourcePath = resourcePath + "in";
} else if (direction == Direction.OUT) {
resourcePath = resourcePath + "out";
} else {
throw new APIPersistenceException("Invalid sequence type");
}
List<SOAPToRestSequence> sequences = new ArrayList<SOAPToRestSequence>();
if (registry.resourceExists(resourcePath)) {
Collection collection = (Collection) registry.get(resourcePath);
String[] resources = collection.getChildren();
for (String path : resources) {
Resource resource = registry.get(path);
String content = new String((byte[]) resource.getContent(), Charset.defaultCharset());
String resourceName;
if (resource.getProperty("resourcePath") != null) {
resourceName = resource.getProperty("resourcePath");
} else {
resourceName = ((ResourceImpl) resource).getName();
}
resourceName = resourceName.replaceAll("\\.xml", "");
resourceName = resourceName.split("_")[0];
String httpMethod = resource.getProperty("method");
SOAPToRestSequence seq = new SOAPToRestSequence(httpMethod, resourceName, content, direction);
seq.setUuid(resource.getUUID());
sequences.add(seq);
}
}
return sequences;
}
use of org.wso2.carbon.registry.core.Collection in project carbon-apimgt by wso2.
the class APIUtilTest method testGetMediationSequenceUuidInSequence.
@Test
public void testGetMediationSequenceUuidInSequence() 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 path = APIConstants.API_CUSTOM_SEQUENCE_LOCATION + File.separator + APIConstants.API_CUSTOM_SEQUENCE_TYPE_IN;
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, "in", apiIdentifier);
Assert.assertEquals(expectedUUID, actualUUID);
sampleSequence.close();
}
use of org.wso2.carbon.registry.core.Collection in project carbon-apimgt by wso2.
the class APIUtilTest method testGetMediationSequenceUuidFaultSequence.
@Test
public void testGetMediationSequenceUuidFaultSequence() 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 path = APIConstants.API_CUSTOM_SEQUENCE_LOCATION + File.separator + APIConstants.API_CUSTOM_SEQUENCE_TYPE_FAULT;
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, "fault", apiIdentifier);
Assert.assertEquals(expectedUUID, actualUUID);
sampleSequence.close();
}
Aggregations