Update user deletion logic

device-task-configuration
Lasantha Dharmakeerthi 6 months ago
commit dd03a073b2

@ -35,6 +35,8 @@ public class BasicUserInfo {
private String createdDate; private String createdDate;
@ApiModelProperty(name = "modifiedDate", value = "User modifiedDate date." ) @ApiModelProperty(name = "modifiedDate", value = "User modifiedDate date." )
private String modifiedDate; private String modifiedDate;
@ApiModelProperty(name = "isRemovable", value = "User's removable status." )
private boolean isRemovable;
public String getUsername() { public String getUsername() {
return username; return username;
@ -84,4 +86,11 @@ public class BasicUserInfo {
this.modifiedDate = modifiedDate; this.modifiedDate = modifiedDate;
} }
public boolean isRemovable() {
return isRemovable;
}
public void setRemovable(boolean removable) {
isRemovable = removable;
}
} }

@ -252,7 +252,7 @@ public class UserManagementServiceImpl implements UserManagementService {
BasicUserInfo user = this.getBasicUserInfo(username); BasicUserInfo user = this.getBasicUserInfo(username);
return Response.status(Response.Status.OK).entity(user).build(); return Response.status(Response.Status.OK).entity(user).build();
} catch (UserStoreException e) { } catch (UserStoreException | DeviceManagementException e) {
String msg = "Error occurred while retrieving information of the user '" + username + "'"; String msg = "Error occurred while retrieving information of the user '" + username + "'";
log.error(msg, e); log.error(msg, e);
return Response.serverError().entity( return Response.serverError().entity(
@ -334,7 +334,7 @@ public class UserManagementServiceImpl implements UserManagementService {
BasicUserInfo updatedUserInfo = this.getBasicUserInfo(username); BasicUserInfo updatedUserInfo = this.getBasicUserInfo(username);
return Response.ok().entity(updatedUserInfo).build(); return Response.ok().entity(updatedUserInfo).build();
} catch (UserStoreException e) { } catch (UserStoreException | DeviceManagementException e) {
String msg = "Error occurred while trying to update user '" + username + "'"; String msg = "Error occurred while trying to update user '" + username + "'";
log.error(msg, e); log.error(msg, e);
return Response.serverError().entity( return Response.serverError().entity(
@ -495,7 +495,7 @@ public class UserManagementServiceImpl implements UserManagementService {
result.setCount(userList.size()); result.setCount(userList.size());
return Response.status(Response.Status.OK).entity(result).build(); return Response.status(Response.Status.OK).entity(result).build();
} catch (UserStoreException e) { } catch (UserStoreException | DeviceManagementException e) {
String msg = "Error occurred while retrieving the list of users."; String msg = "Error occurred while retrieving the list of users.";
log.error(msg, e); log.error(msg, e);
return Response.serverError().entity( return Response.serverError().entity(
@ -574,6 +574,7 @@ public class UserManagementServiceImpl implements UserManagementService {
basicUserInfo.setEmailAddress(getClaimValue(user, Constants.USER_CLAIM_EMAIL_ADDRESS)); basicUserInfo.setEmailAddress(getClaimValue(user, Constants.USER_CLAIM_EMAIL_ADDRESS));
basicUserInfo.setFirstname(getClaimValue(user, Constants.USER_CLAIM_FIRST_NAME)); basicUserInfo.setFirstname(getClaimValue(user, Constants.USER_CLAIM_FIRST_NAME));
basicUserInfo.setLastname(getClaimValue(user, Constants.USER_CLAIM_LAST_NAME)); basicUserInfo.setLastname(getClaimValue(user, Constants.USER_CLAIM_LAST_NAME));
basicUserInfo.setRemovable(isUserRemovable(username));
filteredUserList.add(basicUserInfo); filteredUserList.add(basicUserInfo);
} }
} }
@ -598,7 +599,7 @@ public class UserManagementServiceImpl implements UserManagementService {
result.setCount(commonUsers != null ? commonUsers.size() : 0); result.setCount(commonUsers != null ? commonUsers.size() : 0);
return Response.status(Response.Status.OK).entity(result).build(); return Response.status(Response.Status.OK).entity(result).build();
} catch (UserStoreException e) { } catch (UserStoreException | DeviceManagementException e) {
String msg = "Error occurred while retrieving the list of users."; String msg = "Error occurred while retrieving the list of users.";
log.error(msg, e); log.error(msg, e);
return Response.serverError().entity( return Response.serverError().entity(
@ -1249,7 +1250,7 @@ public class UserManagementServiceImpl implements UserManagementService {
return initialUserPassword.toString(); return initialUserPassword.toString();
} }
private BasicUserInfo getBasicUserInfo(String username) throws UserStoreException { private BasicUserInfo getBasicUserInfo(String username) throws UserStoreException, DeviceManagementException {
BasicUserInfo userInfo = new BasicUserInfo(); BasicUserInfo userInfo = new BasicUserInfo();
userInfo.setUsername(username); userInfo.setUsername(username);
userInfo.setEmailAddress(getClaimValue(username, Constants.USER_CLAIM_EMAIL_ADDRESS)); userInfo.setEmailAddress(getClaimValue(username, Constants.USER_CLAIM_EMAIL_ADDRESS));
@ -1257,9 +1258,21 @@ public class UserManagementServiceImpl implements UserManagementService {
userInfo.setLastname(getClaimValue(username, Constants.USER_CLAIM_LAST_NAME)); userInfo.setLastname(getClaimValue(username, Constants.USER_CLAIM_LAST_NAME));
userInfo.setCreatedDate(getClaimValue(username, Constants.USER_CLAIM_CREATED)); userInfo.setCreatedDate(getClaimValue(username, Constants.USER_CLAIM_CREATED));
userInfo.setModifiedDate(getClaimValue(username, Constants.USER_CLAIM_MODIFIED)); userInfo.setModifiedDate(getClaimValue(username, Constants.USER_CLAIM_MODIFIED));
userInfo.setRemovable(isUserRemovable(username));
return userInfo; return userInfo;
} }
/**
* Check if the user can be removed or not
* @param username Username of the user
* @return True when user can be removed, otherwise false
* @throws DeviceManagementException Throws when error occurred while getting device count
*/
private boolean isUserRemovable(String username) throws DeviceManagementException {
DeviceManagementProviderService deviceManagementProviderService = DeviceMgtAPIUtils.getDeviceManagementService();
return deviceManagementProviderService.getDeviceCount(username.contains("/") ? username.split("/")[1] : username) == 0;
}
private String getClaimValue(String username, String claimUri) throws UserStoreException { private String getClaimValue(String username, String claimUri) throws UserStoreException {
UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager();
return userStoreManager.getUserClaimValue(username, claimUri, null); return userStoreManager.getUserClaimValue(username, claimUri, null);

@ -119,8 +119,9 @@ public class UserManagementServiceImplTest {
PowerMockito.stub(PowerMockito.method(CarbonContext.class, "getThreadLocalCarbonContext")) PowerMockito.stub(PowerMockito.method(CarbonContext.class, "getThreadLocalCarbonContext"))
.toReturn(carbonContext); .toReturn(carbonContext);
Mockito.when(carbonContext.getTenantId()).thenReturn(-1234); Mockito.when(carbonContext.getTenantId()).thenReturn(-1234);
Mockito.when(carbonContext.getUsername()).thenReturn("admin");
Mockito.when(carbonContext.getTenantDomain()).thenReturn("carbon.super"); Mockito.when(carbonContext.getTenantDomain()).thenReturn("carbon.super");
Mockito.when(carbonContext.getUsername()).thenReturn("admin");
Mockito.doReturn(0).when(deviceManagementProviderService).getDeviceCount(TEST_USERNAME);
Mockito.doAnswer(new Answer() { Mockito.doAnswer(new Answer() {
private int count = 0; private int count = 0;
@ -156,9 +157,17 @@ public class UserManagementServiceImplTest {
@Test(description = "This method tests the getUser method of UserManagementService", dependsOnMethods = @Test(description = "This method tests the getUser method of UserManagementService", dependsOnMethods =
"testAddUser") "testAddUser")
public void testGetUser() throws UserStoreException { public void testGetUser() throws UserStoreException, DeviceManagementException {
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getUserStoreManager")) PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getUserStoreManager"))
.toReturn(this.userStoreManager); .toReturn(this.userStoreManager);
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService"))
.toReturn(this.deviceManagementProviderService);
CarbonContext carbonContext = Mockito.mock(CarbonContext.class, Mockito.RETURNS_MOCKS);
PowerMockito.stub(PowerMockito.method(CarbonContext.class, "getThreadLocalCarbonContext"))
.toReturn(carbonContext);
Mockito.when(carbonContext.getTenantId()).thenReturn(-1234);
Mockito.when(carbonContext.getTenantDomain()).thenReturn("carbon.super");
Mockito.doReturn(0).when(deviceManagementProviderService).getDeviceCount(TEST_USERNAME);
Response response = userManagementService.getUser(TEST_USERNAME, null, null); Response response = userManagementService.getUser(TEST_USERNAME, null, null);
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), "User retrieval failed"); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), "User retrieval failed");
BasicUserInfo userInfo = (BasicUserInfo) response.getEntity(); BasicUserInfo userInfo = (BasicUserInfo) response.getEntity();
@ -173,15 +182,18 @@ public class UserManagementServiceImplTest {
@Test(description = "This method tests the updateUser method of UserManagementService", dependsOnMethods = @Test(description = "This method tests the updateUser method of UserManagementService", dependsOnMethods =
{"testGetUser"}) {"testGetUser"})
public void testUpdateUser() throws UserStoreException { public void testUpdateUser() throws UserStoreException, DeviceManagementException {
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getUserStoreManager")) PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getUserStoreManager"))
.toReturn(this.userStoreManager); .toReturn(this.userStoreManager);
CarbonContext carbonContext = Mockito.mock(CarbonContext.class, Mockito.RETURNS_MOCKS); CarbonContext carbonContext = Mockito.mock(CarbonContext.class, Mockito.RETURNS_MOCKS);
PowerMockito.stub(PowerMockito.method(CarbonContext.class, "getThreadLocalCarbonContext")) PowerMockito.stub(PowerMockito.method(CarbonContext.class, "getThreadLocalCarbonContext"))
.toReturn(carbonContext); .toReturn(carbonContext);
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService"))
.toReturn(this.deviceManagementProviderService);
Mockito.when(carbonContext.getTenantId()).thenReturn(-1234); Mockito.when(carbonContext.getTenantId()).thenReturn(-1234);
Mockito.when(carbonContext.getUsername()).thenReturn("admin");
Mockito.when(carbonContext.getTenantDomain()).thenReturn("carbon.super"); Mockito.when(carbonContext.getTenantDomain()).thenReturn("carbon.super");
Mockito.when(carbonContext.getUsername()).thenReturn("admin");
Mockito.doReturn(0).when(deviceManagementProviderService).getDeviceCount(TEST_USERNAME);
Response response = userManagementService.updateUser(TEST2_USERNAME, null, null); Response response = userManagementService.updateUser(TEST2_USERNAME, null, null);
Assert.assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode(), Assert.assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode(),
"Non-existing user was successfully updated"); "Non-existing user was successfully updated");
@ -250,10 +262,18 @@ public class UserManagementServiceImplTest {
@Test(description = "This method tests the getUsers method of UserManagementService", @Test(description = "This method tests the getUsers method of UserManagementService",
dependsOnMethods = {"testGetUserNames"}) dependsOnMethods = {"testGetUserNames"})
public void testGetUsers() { public void testGetUsers() throws DeviceManagementException {
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getUserStoreManager")) PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getUserStoreManager"))
.toReturn(userStoreManager); .toReturn(userStoreManager);
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService"))
.toReturn(this.deviceManagementProviderService);
Response response = userManagementService.getUsers(null, "00", 0, 10, null); Response response = userManagementService.getUsers(null, "00", 0, 10, null);
CarbonContext carbonContext = Mockito.mock(CarbonContext.class, Mockito.RETURNS_MOCKS);
PowerMockito.stub(PowerMockito.method(CarbonContext.class, "getThreadLocalCarbonContext"))
.toReturn(carbonContext);
Mockito.when(carbonContext.getTenantId()).thenReturn(-1234);
Mockito.when(carbonContext.getTenantDomain()).thenReturn("carbon.super");
Mockito.doReturn(0).when(deviceManagementProviderService).getDeviceCount(TEST_USERNAME);
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), "GetUsers request failed"); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), "GetUsers request failed");
} }

Loading…
Cancel
Save