use of org.alfresco.rest.rm.community.requests.gscore.api.RMUserAPI in project records-management by Alfresco.
the class CreateRMSiteMemberUnitTest method testCreateSiteMember.
@Test
public void testCreateSiteMember() throws Exception {
String userName = "user1";
Event mockedEvent = mock(Event.class);
DBObject mockedData = mock(DBObject.class);
when(mockedData.get(CreateRMSiteMember.FIELD_USERNAME)).thenReturn(userName);
when(mockedEvent.getData()).thenReturn(mockedData);
SiteMemberData mockedSiteMemberData = mock(SiteMemberData.class);
when(mockedSiteMemberData.getCreationState()).thenReturn(DataCreationState.Scheduled);
when(mockedSiteMemberData.getRole()).thenReturn(RMRole.Administrator.toString());
when(mockedSiteDataService.getSiteMember(PATH_SNIPPET_RM_SITE_ID, userName)).thenReturn(mockedSiteMemberData);
RMUserAPI mockedRMUserAPI = mock(RMUserAPI.class);
when(mockedRestAPIFactory.getRMUserAPI()).thenReturn(mockedRMUserAPI);
when(mockedApplicationContext.getBean("restAPIFactory", RestAPIFactory.class)).thenReturn(mockedRestAPIFactory);
EventResult result = createRMSiteMember.processEvent(mockedEvent, new StopWatch());
verify(mockedData, never()).put(CreateRMSiteMember.MSG_KEY, CreateRMSiteMember.INVALID_SITE_MEMBER_REQUEST_MSG);
verify(mockedData, never()).put(CreateRMSiteMember.MSG_KEY, MessageFormat.format(CreateRMSiteMember.SITE_MEMBER_MISSING_MSG_TEMPLATE, userName));
verify(mockedData, never()).put(CreateRMSiteMember.MSG_KEY, MessageFormat.format(CreateRMSiteMember.SITE_MEMBER_ALREADY_PROCESSED_MSG_TEMPLATE, mockedSiteMemberData));
verify(mockedRMUserAPI, times(1)).assignRoleToUser(userName, RMRole.Administrator.toString());
verify(mockedSiteDataService, times(1)).setSiteMemberCreationState(PATH_SNIPPET_RM_SITE_ID, userName, DataCreationState.Created);
verify(mockedSiteDataService, times(2)).getSiteMember(PATH_SNIPPET_RM_SITE_ID, userName);
assertEquals(true, result.isSuccess());
String data = (String) result.getData();
assertTrue(data.startsWith(CREATED_RM_SITE_MEMBER_MSG));
assertEquals(1, result.getNextEvents().size());
assertEquals(createRMSiteMember.getEventNameRMSiteMemberCreated(), result.getNextEvents().get(0).getName());
}
use of org.alfresco.rest.rm.community.requests.gscore.api.RMUserAPI in project records-management by Alfresco.
the class CreateRMSiteMemberUnitTest method testCreateSiteMemberWithExceptionOnRestAPI.
@Test(expected = RuntimeException.class)
public void testCreateSiteMemberWithExceptionOnRestAPI() throws Exception {
String userName = "user1";
Event mockedEvent = mock(Event.class);
DBObject mockedData = mock(DBObject.class);
when(mockedData.get(CreateRMSiteMember.FIELD_USERNAME)).thenReturn(userName);
when(mockedEvent.getData()).thenReturn(mockedData);
SiteMemberData mockedSiteMemberData = mock(SiteMemberData.class);
when(mockedSiteMemberData.getCreationState()).thenReturn(DataCreationState.Scheduled);
when(mockedSiteMemberData.getRole()).thenReturn(RMRole.Administrator.toString());
when(mockedSiteDataService.getSiteMember(PATH_SNIPPET_RM_SITE_ID, userName)).thenReturn(mockedSiteMemberData);
RMUserAPI mockedRMUserAPI = mock(RMUserAPI.class);
when(mockedRestAPIFactory.getRMUserAPI()).thenReturn(mockedRMUserAPI);
Mockito.doThrow(new Exception("someError")).when(mockedRMUserAPI).assignRoleToUser(userName, RMRole.Administrator.toString());
createRMSiteMember.processEvent(mockedEvent, new StopWatch());
}
use of org.alfresco.rest.rm.community.requests.gscore.api.RMUserAPI in project records-management by Alfresco.
the class CreateRMSiteMember method processEvent.
@Override
protected EventResult processEvent(Event event) throws Exception {
if (event == null) {
throw new IllegalStateException("This processor requires an event.");
}
DBObject dataObj = (DBObject) event.getData();
if (dataObj == null) {
throw new IllegalStateException("This processor requires data with field " + FIELD_USERNAME);
}
String username = (String) dataObj.get(FIELD_USERNAME);
EventProcessorResponse response = null;
Event nextEvent = null;
String msg = null;
// Check the input
if (StringUtils.isBlank(username)) {
dataObj.put(MSG_KEY, INVALID_SITE_MEMBER_REQUEST_MSG);
return new EventResult(dataObj, false);
}
// Get the membership data
SiteMemberData siteMember = siteDataService.getSiteMember(PATH_SNIPPET_RM_SITE_ID, username);
if (siteMember == null) {
dataObj.put(MSG_KEY, MessageFormat.format(SITE_MEMBER_MISSING_MSG_TEMPLATE, username));
return new EventResult(dataObj, false);
}
if (siteMember.getCreationState() != DataCreationState.Scheduled) {
dataObj.put(MSG_KEY, MessageFormat.format(SITE_MEMBER_ALREADY_PROCESSED_MSG_TEMPLATE, siteMember));
return new EventResult(dataObj, false);
}
// Start by marking it as a failure in order to handle all failure paths
siteDataService.setSiteMemberCreationState(PATH_SNIPPET_RM_SITE_ID, username, DataCreationState.Failed);
String roleStr = siteMember.getRole();
try {
// assign RM roles to new members as admin user
RMUserAPI rmUserAPI = getRestAPIFactory().getRMUserAPI();
rmUserAPI.assignRoleToUser(username, roleStr);
siteDataService.setSiteMemberCreationState(PATH_SNIPPET_RM_SITE_ID, username, DataCreationState.Created);
siteMember = siteDataService.getSiteMember(PATH_SNIPPET_RM_SITE_ID, username);
EventDataObject responseData = new EventDataObject(STATUS.SUCCESS, siteMember);
response = new EventProcessorResponse("Added RM site member", true, responseData);
msg = MessageFormat.format(CREATED_RM_SITE_MEMBER_MSG_TEMPLATE, response);
nextEvent = new Event(eventNameRMSiteMemberCreated, null);
} catch (Exception e) {
// Failure
throw new RuntimeException("Create RM site member as user: admin failed (" + e.getMessage() + "): " + siteMember, e);
}
logger.debug(msg);
EventResult result = new EventResult(msg, nextEvent);
return result;
}
Aggregations