use of org.wso2.carbon.registry.core.session.UserRegistry in project carbon-apimgt by wso2.
the class APIUtil method addDefinedAllSequencesToRegistry.
/**
* Add all the custom sequences of given type to registry
*
* @param registry Registry instance
* @param customSequenceType Custom sequence type which is in/out or fault
* @throws APIManagementException
*/
public static void addDefinedAllSequencesToRegistry(UserRegistry registry, String customSequenceType) throws APIManagementException {
InputStream inSeqStream = null;
String seqFolderLocation = CarbonUtils.getCarbonHome() + File.separator + APIConstants.API_CUSTOM_SEQUENCES_FOLDER_LOCATION + File.separator + customSequenceType;
try {
File inSequenceDir = new File(seqFolderLocation);
File[] sequences;
sequences = inSequenceDir.listFiles();
if (sequences != null) {
for (File sequenceFile : sequences) {
String sequenceFileName = sequenceFile.getName();
String regResourcePath = APIConstants.API_CUSTOM_SEQUENCE_LOCATION + '/' + customSequenceType + '/' + sequenceFileName;
if (registry.resourceExists(regResourcePath)) {
if (log.isDebugEnabled()) {
log.debug("The sequence file with the name " + sequenceFileName + " already exists in the registry path " + regResourcePath);
}
} else {
if (log.isDebugEnabled()) {
log.debug("Adding sequence file with the name " + sequenceFileName + " to the registry path " + regResourcePath);
}
inSeqStream = new FileInputStream(sequenceFile);
byte[] inSeqData = IOUtils.toByteArray(inSeqStream);
Resource inSeqResource = registry.newResource();
inSeqResource.setContent(inSeqData);
registry.put(regResourcePath, inSeqResource);
}
}
} else {
log.error("Custom sequence template location unavailable for custom sequence type " + customSequenceType + " : " + seqFolderLocation);
}
} catch (RegistryException e) {
throw new APIManagementException("Error while saving defined sequences to the registry ", e);
} catch (IOException e) {
throw new APIManagementException("Error while reading defined sequence ", e);
} finally {
IOUtils.closeQuietly(inSeqStream);
}
}
use of org.wso2.carbon.registry.core.session.UserRegistry in project carbon-apimgt by wso2.
the class APIUtil method isPerAPISequence.
/**
* Returns true if the sequence is a per API one
*
* @param sequenceName
* @param tenantId
* @param identifier API identifier
* @param sequenceType in/out/fault
* @return true/false
* @throws APIManagementException
*/
public static boolean isPerAPISequence(String sequenceName, int tenantId, APIIdentifier identifier, String sequenceType) throws APIManagementException {
org.wso2.carbon.registry.api.Collection seqCollection = null;
try {
UserRegistry registry = ServiceReferenceHolder.getInstance().getRegistryService().getGovernanceSystemRegistry(tenantId);
if (registry.resourceExists(getSequencePath(identifier, sequenceType))) {
seqCollection = (org.wso2.carbon.registry.api.Collection) registry.get(getSequencePath(identifier, sequenceType));
if (seqCollection != null) {
String[] childPaths = seqCollection.getChildren();
for (String childPath : childPaths) {
Resource sequence = registry.get(childPath);
OMElement seqElment = APIUtil.buildOMElement(sequence.getContentStream());
if (sequenceName.equals(seqElment.getAttributeValue(new QName("name")))) {
return true;
}
}
}
}
} catch (RegistryException e) {
String msg = "Error while retrieving registry for tenant " + tenantId;
log.error(msg);
throw new APIManagementException(msg, e);
} catch (org.wso2.carbon.registry.api.RegistryException e) {
String msg = "Error while processing the " + sequenceType + " sequences of " + identifier + " in the registry";
log.error(msg);
throw new APIManagementException(msg, e);
} catch (Exception e) {
throw new APIManagementException(e.getMessage(), e);
}
return false;
}
use of org.wso2.carbon.registry.core.session.UserRegistry in project carbon-apimgt by wso2.
the class APIUtil method getMediationPolicyAttributes.
/**
* Returns attributes correspond to the given mediation policy name and direction
*
* @param policyName name of the sequence
* @param tenantId logged in user's tenantId
* @param direction in/out/fault
* @param identifier API identifier
* @return attributes(path, uuid) of the given mediation sequence or null
* @throws APIManagementException If failed to get the uuid of the mediation sequence
*/
public static Map<String, String> getMediationPolicyAttributes(String policyName, int tenantId, String direction, APIIdentifier identifier) throws APIManagementException {
org.wso2.carbon.registry.api.Collection seqCollection = null;
String seqCollectionPath = "";
Map<String, String> mediationPolicyAttributes = new HashMap<>(3);
try {
UserRegistry registry = ServiceReferenceHolder.getInstance().getRegistryService().getGovernanceSystemRegistry(tenantId);
if (APIConstants.API_CUSTOM_SEQUENCE_TYPE_IN.equals(direction)) {
seqCollection = (org.wso2.carbon.registry.api.Collection) registry.get(APIConstants.API_CUSTOM_SEQUENCE_LOCATION + "/" + APIConstants.API_CUSTOM_SEQUENCE_TYPE_IN);
} else if (APIConstants.API_CUSTOM_SEQUENCE_TYPE_OUT.equals(direction)) {
seqCollection = (org.wso2.carbon.registry.api.Collection) registry.get(APIConstants.API_CUSTOM_SEQUENCE_LOCATION + "/" + APIConstants.API_CUSTOM_SEQUENCE_TYPE_OUT);
} else if (APIConstants.API_CUSTOM_SEQUENCE_TYPE_FAULT.equals(direction)) {
seqCollection = (org.wso2.carbon.registry.api.Collection) registry.get(APIConstants.API_CUSTOM_SEQUENCE_LOCATION + "/" + APIConstants.API_CUSTOM_SEQUENCE_TYPE_FAULT);
}
if (seqCollection == null) {
seqCollection = (org.wso2.carbon.registry.api.Collection) registry.get(getSequencePath(identifier, direction));
}
if (seqCollection != null) {
String[] childPaths = seqCollection.getChildren();
for (String childPath : childPaths) {
Resource mediationPolicy = registry.get(childPath);
OMElement seqElment = APIUtil.buildOMElement(mediationPolicy.getContentStream());
String seqElmentName = seqElment.getAttributeValue(new QName("name"));
if (policyName.equals(seqElmentName)) {
mediationPolicyAttributes.put("path", childPath);
mediationPolicyAttributes.put("uuid", mediationPolicy.getUUID());
mediationPolicyAttributes.put("name", policyName);
return mediationPolicyAttributes;
}
}
}
// If the sequence not found the default sequences, check in custom sequences
seqCollection = (org.wso2.carbon.registry.api.Collection) registry.get(getSequencePath(identifier, direction));
if (seqCollection != null) {
String[] childPaths = seqCollection.getChildren();
for (String childPath : childPaths) {
Resource mediationPolicy = registry.get(childPath);
OMElement seqElment = APIUtil.buildOMElement(mediationPolicy.getContentStream());
if (policyName.equals(seqElment.getAttributeValue(new QName("name")))) {
mediationPolicyAttributes.put("path", childPath);
mediationPolicyAttributes.put("uuid", mediationPolicy.getUUID());
mediationPolicyAttributes.put("name", policyName);
return mediationPolicyAttributes;
}
}
}
} catch (Exception e) {
String msg = "Issue is in accessing the Registry";
log.error(msg);
throw new APIManagementException(msg, e);
}
return mediationPolicyAttributes;
}
use of org.wso2.carbon.registry.core.session.UserRegistry in project carbon-apimgt by wso2.
the class APIProviderImplTest method testUpdateAPIforStateChange_ToPublished.
@Test
public void testUpdateAPIforStateChange_ToPublished() throws RegistryException, UserStoreException, APIManagementException, FaultGatewaysException, APIPersistenceException, XMLStreamException {
APIIdentifier apiId = new APIIdentifier("admin", "API1", "1.0.0");
Set<String> environments = new HashSet<String>();
environments.add("Production");
environments.add("Sandbox");
API api = new API(apiId);
api.setContext("/test");
api.setStatus(APIConstants.CREATED);
api.setAsDefaultVersion(true);
api.setEnvironments(environments);
api.setOrganization("carbon.super");
Mockito.when(apimgtDAO.getPublishedDefaultVersion(apiId)).thenReturn("1.0.0");
Map<String, String> failedGWEnv = new HashMap<String, String>();
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apiPersistenceInstance, apimgtDAO, scopesDAO, null, null);
Mockito.when(artifactManager.newGovernanceArtifact(any(QName.class))).thenReturn(artifact);
Mockito.when(APIUtil.createAPIArtifactContent(artifact, api)).thenReturn(artifact);
RegistryService registryService = Mockito.mock(RegistryService.class);
UserRegistry userRegistry = Mockito.mock(UserRegistry.class);
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.getRegistryService()).thenReturn(registryService);
Mockito.when(registryService.getConfigSystemRegistry(Mockito.anyInt())).thenReturn(userRegistry);
Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
PublisherAPI publisherAPI = Mockito.mock(PublisherAPI.class);
PowerMockito.when(apiPersistenceInstance.addAPI(any(Organization.class), any(PublisherAPI.class))).thenReturn(publisherAPI);
apiProvider.addAPI(api);
// Mock Updating API
Resource apiSourceArtifact = Mockito.mock(Resource.class);
Mockito.when(apiSourceArtifact.getUUID()).thenReturn("12640983654");
String apiSourcePath = "path";
PowerMockito.when(APIUtil.getAPIPath(api.getId())).thenReturn(apiSourcePath);
PowerMockito.when(apiProvider.registry.get(apiSourcePath)).thenReturn(apiSourceArtifact);
Mockito.when(artifact.getAttribute(APIConstants.API_OVERVIEW_STATUS)).thenReturn("CREATED");
Mockito.when(artifactManager.getGenericArtifact(apiSourceArtifact.getUUID())).thenReturn(artifact);
PowerMockito.when(APIUtil.createAPIArtifactContent(artifact, api)).thenReturn(artifact);
Mockito.when(artifact.getId()).thenReturn("12640983654");
PowerMockito.when(GovernanceUtils.getArtifactPath(apiProvider.registry, "12640983654")).thenReturn(apiSourcePath);
Mockito.doNothing().when(artifactManager).updateGenericArtifact(artifact);
apiProvider.updateAPIforStateChange(api, APIConstants.CREATED, APIConstants.PUBLISHED);
// From the 2 environments, only 1 was successful
Assert.assertEquals(2, api.getEnvironments().size());
}
use of org.wso2.carbon.registry.core.session.UserRegistry in project carbon-apimgt by wso2.
the class APIProviderImplTest method testGetCustomApiFaultSequences.
@Test
public void testGetCustomApiFaultSequences() throws Exception {
APIIdentifier apiId = new APIIdentifier("admin", "API1", "1.0.1");
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
mockSequences(APIConstants.API_CUSTOM_FAULTSEQUENCE_LOCATION, APIConstants.API_CUSTOM_SEQUENCE_TYPE_FAULT, apiId);
List<String> sequenceList = apiProvider.getCustomApiFaultSequences(apiId);
Assert.assertNotNull(sequenceList);
Assert.assertEquals(1, sequenceList.size());
// OMException when building OMElement
PowerMockito.when(APIUtil.buildOMElement(any(InputStream.class))).thenThrow(new OMException());
apiProvider.getCustomApiFaultSequences(apiId);
// org.wso2.carbon.registry.api.RegistryException
ServiceReferenceHolder sh = PowerMockito.mock(ServiceReferenceHolder.class);
PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(sh);
RegistryService registryService = Mockito.mock(RegistryService.class);
PowerMockito.when(sh.getRegistryService()).thenReturn(registryService);
UserRegistry registry = Mockito.mock(UserRegistry.class);
PowerMockito.when(registryService.getGovernanceSystemRegistry(Matchers.anyInt())).thenReturn(registry);
Mockito.when(registry.resourceExists(APIUtil.getSequencePath(apiId, APIConstants.API_CUSTOM_SEQUENCE_TYPE_FAULT))).thenThrow(org.wso2.carbon.registry.api.RegistryException.class);
String msg = "Error while processing the " + APIConstants.API_CUSTOM_SEQUENCE_TYPE_FAULT + " sequences of " + apiId + " in the registry";
try {
apiProvider.getCustomApiFaultSequences(apiId);
} catch (APIManagementException e) {
Assert.assertEquals(msg, e.getMessage());
}
// Registry Exception
PowerMockito.when(registryService.getGovernanceSystemRegistry(Matchers.anyInt())).thenThrow(RegistryException.class);
String msg1 = "Error while retrieving registry for tenant -1";
try {
apiProvider.getCustomApiFaultSequences(apiId);
} catch (APIManagementException e) {
Assert.assertEquals(msg1, e.getMessage());
}
}
Aggregations