use of org.wso2.carbon.governance.custom.lifecycles.checklist.beans.LifecycleBean in project carbon-apimgt by wso2.
the class APIProviderImplTest method testGetAPILifeCycleData.
@Test
public void testGetAPILifeCycleData() throws Exception {
APIIdentifier identifier = new APIIdentifier("admin-AT-carbon.super", "API1", "1.0.0");
String path = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getApiName() + RegistryConstants.PATH_SEPARATOR + identifier.getVersion();
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
Resource apiSourceArtifact = Mockito.mock(Resource.class);
Mockito.when(apiSourceArtifact.getUUID()).thenReturn("12640983654");
PowerMockito.when(APIUtil.getAPIPath(identifier)).thenReturn(path);
PowerMockito.when(apiProvider.registry.get(path)).thenReturn(apiSourceArtifact);
Mockito.when(artifactManager.getGenericArtifact(apiSourceArtifact.getUUID())).thenReturn(artifact);
Mockito.when(artifact.getLifecycleState()).thenReturn("Created");
LifecycleBean bean = getLCBean();
Mockito.when(LifecycleBeanPopulator.getLifecycleBean(path, (UserRegistry) apiProvider.registry, apiProvider.configRegistry)).thenReturn(bean);
Map<String, Object> lcData = apiProvider.getAPILifeCycleData(identifier);
List checkListItems = (List) lcData.get(APIConstants.LC_CHECK_ITEMS);
Assert.assertEquals(2, checkListItems.size());
if (checkListItems.get(0) instanceof CheckListItem) {
CheckListItem checkListItem = (CheckListItem) checkListItems.get(0);
Assert.assertTrue((APIConstants.RESUBSCRIBE_CHECK_LIST_ITEM).equals(checkListItem.getName()) || (APIConstants.DEPRECATE_CHECK_LIST_ITEM).equals(checkListItem.getName()));
if ((APIConstants.RESUBSCRIBE_CHECK_LIST_ITEM).equals(checkListItem.getName())) {
Assert.assertEquals("1", checkListItem.getOrder());
} else {
Assert.assertEquals("0", checkListItem.getOrder());
}
Assert.assertEquals("Created", checkListItem.getLifeCycleStatus());
}
if (checkListItems.get(1) instanceof CheckListItem) {
CheckListItem checkListItem = (CheckListItem) checkListItems.get(1);
Assert.assertTrue((APIConstants.RESUBSCRIBE_CHECK_LIST_ITEM).equals(checkListItem.getName()) || (APIConstants.DEPRECATE_CHECK_LIST_ITEM).equals(checkListItem.getName()));
if ((APIConstants.RESUBSCRIBE_CHECK_LIST_ITEM).equals(checkListItem.getName())) {
Assert.assertEquals("1", checkListItem.getOrder());
} else {
Assert.assertEquals("0", checkListItem.getOrder());
}
Assert.assertEquals("Created", checkListItem.getLifeCycleStatus());
}
}
use of org.wso2.carbon.governance.custom.lifecycles.checklist.beans.LifecycleBean in project carbon-apimgt by wso2.
the class APIProviderImplTest method getLCBean.
private LifecycleBean getLCBean() {
LifecycleBean bean = new LifecycleBean();
List<Property> lifecycleProps = new ArrayList<Property>();
Property property1 = new Property();
property1.setKey("registry.custom_lifecycle.checklist.option.APILifeCycle.1.item");
String[] values1 = { "status:Created", "name:Requires re-subscription when publishing the API", "value:false", "order:1" };
property1.setValues(values1);
Property property2 = new Property();
property2.setKey("registry.lifecycle.APILifeCycle.state");
String[] values2 = { "Created" };
property2.setValues(values2);
Property property3 = new Property();
property3.setKey("registry.custom_lifecycle.checklist.option.APILifeCycle.0.item.permission");
String[] values3 = { "registry.custom_lifecycle.checklist.option.APILifeCycle.0.item.permission" };
property3.setValues(values3);
Property property4 = new Property();
property4.setKey("registry.lifecycle.APILifeCycle.lastStateUpdatedTime");
String[] values4 = { "2017-08-31 13:36:54.501" };
property4.setValues(values4);
Property property5 = new Property();
property5.setKey("registry.custom_lifecycle.checklist.option.APILifeCycle.1.item.permission");
String[] values5 = { "registry.custom_lifecycle.checklist.option.APILifeCycle.1.item.permission" };
property5.setValues(values5);
Property property6 = new Property();
property6.setKey("registry.custom_lifecycle.checklist.option.APILifeCycle.0.item");
String[] values6 = { "status:Created", "name:Deprecate old versions after publishing the API", "value:false", "order:0" };
property6.setValues(values6);
Property property7 = new Property();
property7.setKey("registry.LC.name");
String[] values7 = { "APILifeCycle" };
property7.setValues(values7);
lifecycleProps.add(property1);
lifecycleProps.add(property2);
lifecycleProps.add(property3);
lifecycleProps.add(property4);
lifecycleProps.add(property5);
lifecycleProps.add(property6);
Property[] propertyArr = new Property[lifecycleProps.size()];
bean.setLifecycleProperties(lifecycleProps.toArray(propertyArr));
String[] userRoles = { "publisher" };
bean.setRolesOfUser(userRoles);
return bean;
}
use of org.wso2.carbon.governance.custom.lifecycles.checklist.beans.LifecycleBean in project carbon-apimgt by wso2.
the class APIProviderImpl method getAPILifeCycleData.
@Override
public /*
* This method returns the lifecycle data for an API including current state,next states.
*
* @param apiId APIIdentifier
* @return Map<String,Object> a map with lifecycle data
*/
Map<String, Object> getAPILifeCycleData(APIIdentifier apiId) throws APIManagementException {
String path = APIUtil.getAPIPath(apiId);
Map<String, Object> lcData = new HashMap<String, Object>();
String providerTenantMode = apiId.getProviderName();
boolean isTenantFlowStarted = false;
try {
String tenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(providerTenantMode));
if (tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
isTenantFlowStarted = true;
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
}
Resource apiSourceArtifact = registry.get(path);
GenericArtifactManager artifactManager = APIUtil.getArtifactManager(registry, APIConstants.API_KEY);
if (artifactManager == null) {
String errorMessage = "Failed to retrieve artifact manager when getting lifecycle data for API " + apiId;
log.error(errorMessage);
throw new APIManagementException(errorMessage);
}
GenericArtifact artifact = artifactManager.getGenericArtifact(apiSourceArtifact.getUUID());
// Get all the actions corresponding to current state of the api artifact
String[] actions = artifact.getAllLifecycleActions(APIConstants.API_LIFE_CYCLE);
// Put next states into map
lcData.put(APIConstants.LC_NEXT_STATES, actions);
String lifeCycleState = artifact.getLifecycleState();
lcData.put(APIConstants.LC_STATUS, lifeCycleState);
LifecycleBean bean;
bean = LifecycleBeanPopulator.getLifecycleBean(path, (UserRegistry) registry, configRegistry);
if (bean != null) {
ArrayList<CheckListItem> checkListItems = new ArrayList<CheckListItem>();
ArrayList<String> permissionList = new ArrayList<String>();
// Get lc properties
Property[] lifecycleProps = bean.getLifecycleProperties();
// Get roles of the current session holder
String[] roleNames = bean.getRolesOfUser();
for (Property property : lifecycleProps) {
String propName = property.getKey();
String[] propValues = property.getValues();
// Check for permission properties if any exists
if (propValues != null && propValues.length != 0) {
if (propName.startsWith(APIConstants.LC_PROPERTY_CHECKLIST_PREFIX) && propName.endsWith(APIConstants.LC_PROPERTY_PERMISSION_SUFFIX) && propName.contains(APIConstants.API_LIFE_CYCLE)) {
for (String role : roleNames) {
for (String propValue : propValues) {
String key = propName.replace(APIConstants.LC_PROPERTY_CHECKLIST_PREFIX, "").replace(APIConstants.LC_PROPERTY_PERMISSION_SUFFIX, "");
if (propValue.equals(role)) {
permissionList.add(key);
} else if (propValue.startsWith(APIConstants.LC_PROPERTY_CHECKLIST_PREFIX) && propValue.endsWith(APIConstants.LC_PROPERTY_PERMISSION_SUFFIX)) {
permissionList.add(key);
}
}
}
}
}
}
// Check for lifecycle checklist item properties defined
for (Property property : lifecycleProps) {
String propName = property.getKey();
String[] propValues = property.getValues();
if (propValues != null && propValues.length != 0) {
CheckListItem checkListItem = new CheckListItem();
checkListItem.setVisible("false");
if (propName.startsWith(APIConstants.LC_PROPERTY_CHECKLIST_PREFIX) && propName.endsWith(APIConstants.LC_PROPERTY_ITEM_SUFFIX) && propName.contains(APIConstants.API_LIFE_CYCLE)) {
if (propValues.length > 2) {
for (String param : propValues) {
if (param.startsWith(APIConstants.LC_STATUS)) {
checkListItem.setLifeCycleStatus(param.substring(7));
} else if (param.startsWith(APIConstants.LC_CHECK_ITEM_NAME)) {
checkListItem.setName(param.substring(5));
} else if (param.startsWith(APIConstants.LC_CHECK_ITEM_VALUE)) {
checkListItem.setValue(param.substring(6));
} else if (param.startsWith(APIConstants.LC_CHECK_ITEM_ORDER)) {
checkListItem.setOrder(param.substring(6));
}
}
}
String key = propName.replace(APIConstants.LC_PROPERTY_CHECKLIST_PREFIX, "").replace(APIConstants.LC_PROPERTY_ITEM_SUFFIX, "");
if (permissionList.contains(key)) {
// Set visible to true if the checklist item permits
checkListItem.setVisible("true");
}
}
if (checkListItem.matchLifeCycleStatus(lifeCycleState)) {
checkListItems.add(checkListItem);
}
}
}
lcData.put("items", checkListItems);
}
} catch (Exception e) {
handleException(e.getMessage(), e);
} finally {
if (isTenantFlowStarted) {
PrivilegedCarbonContext.endTenantFlow();
}
}
return lcData;
}
Aggregations