From 550b2f820d46ba8b6d05c84027e98b7c64aef264 Mon Sep 17 00:00:00 2001 From: BharathwajShankar Date: Mon, 18 Dec 2023 10:39:08 +0530 Subject: [PATCH 01/33] Issue #LR-676 feat: User Delete - ownership transfer api --- .../usermanagement/UserController.java | 8 + controller/app/util/ACTORS.java | 3 +- controller/conf/application.conf | 10 + controller/conf/routes | 2 +- .../org/sunbird/exception/ResponseCode.java | 3 + .../sunbird/exception/ResponseMessage.java | 2 + .../sunbird/operations/ActorOperations.java | 3 +- .../user/UserOwnershipTransferActor.java | 191 ++++++++++++++++++ 8 files changed, 219 insertions(+), 3 deletions(-) create mode 100644 service/src/main/java/org/sunbird/actor/user/UserOwnershipTransferActor.java diff --git a/controller/app/controllers/usermanagement/UserController.java b/controller/app/controllers/usermanagement/UserController.java index 5a0455363d..605e4360aa 100644 --- a/controller/app/controllers/usermanagement/UserController.java +++ b/controller/app/controllers/usermanagement/UserController.java @@ -55,6 +55,9 @@ public class UserController extends BaseController { @Inject @Named("user_self_declaration_management_actor") private ActorRef userSelfDeclarationManagementActor; + @Inject + @Named("user_ownership_transfer_actor") + private ActorRef userOwnershipTransferActor; public CompletionStage createUser(Http.Request httpRequest) { return handleRequest( @@ -472,4 +475,9 @@ public CompletionStage updateUserDeclarations(Http.Request httpRequest) true, httpRequest); } + public CompletionStage ownershipTransferUser(Http.Request httpRequest) { + return handleRequest(userOwnershipTransferActor, + ActorOperations.USER_OWNERSHIP_TRANSFER.getValue(), + httpRequest.body().asJson(), httpRequest); + } } diff --git a/controller/app/util/ACTORS.java b/controller/app/util/ACTORS.java index 8bc03332e9..07cd94a52b 100644 --- a/controller/app/util/ACTORS.java +++ b/controller/app/util/ACTORS.java @@ -103,7 +103,8 @@ public enum ACTORS { USER_UPDATE_ACTOR(UserUpdateActor.class, "user_update_actor"), BACKGROUND_JOB_MANAGER_ACTOR(BackgroundJobManager.class, "background_job_manager_actor"), USER_DELETION_BACKGROUND_JOB_ACTOR( - UserDeletionBackgroundJobActor.class, "user_deletion_background_job_actor"); + UserDeletionBackgroundJobActor.class, "user_deletion_background_job_actor"), + USER_OWNERSHIP_TRANSFER_ACTOR(UserOwnershipTransferActor.class,"user_ownership_transfer_actor"); ACTORS(Class clazz, String name) { actorClass = clazz; diff --git a/controller/conf/application.conf b/controller/conf/application.conf index a7c0bc1887..766c0d5db5 100644 --- a/controller/conf/application.conf +++ b/controller/conf/application.conf @@ -114,6 +114,16 @@ akka { { dispatcher = akka.actor.brr-usr-dispatcher } + "/user_ownership_transfer_actor" + { + router = smallest-mailbox-pool + nr-of-instances = 5 + dispatcher = brr-usr-dispatcher + } + "/user_ownership_transfer_actor/*" + { + dispatcher = akka.actor.brr-usr-dispatcher + } "/background_job_manager_actor" { router = smallest-mailbox-pool diff --git a/controller/conf/routes b/controller/conf/routes index 6bd2fb233f..acf54cc3fd 100644 --- a/controller/conf/routes +++ b/controller/conf/routes @@ -140,4 +140,4 @@ POST /v1/system/settings/set @controllers.systemsettings.Syst POST /v1/user/delete @controllers.usermanagement.UserStatusController.deleteUser(request: play.mvc.Http.Request) - +POST /v1/user/ownership/transfer @controllers.usermanagement.UserController.ownershipTransferUser(request: play.mvc.Http.Request) diff --git a/core/platform-common/src/main/java/org/sunbird/exception/ResponseCode.java b/core/platform-common/src/main/java/org/sunbird/exception/ResponseCode.java index e99a3e0db4..7242cc357c 100644 --- a/core/platform-common/src/main/java/org/sunbird/exception/ResponseCode.java +++ b/core/platform-common/src/main/java/org/sunbird/exception/ResponseCode.java @@ -180,6 +180,9 @@ public enum ResponseCode { ResponseMessage.Message.INVALID_TENANT_SECURITY_LEVEL_LOWER), cannotDeleteUser( ResponseMessage.Key.CANNOT_DELETE_USER, ResponseMessage.Message.CANNOT_DELETE_USER), + + cannotTransferOwnership( + ResponseMessage.Key.CANNOT_TRANSFER_OWNERSHIP, ResponseMessage.Message.CANNOT_TRANSFER_OWNERSHIP), OK(200), SUCCESS(200), CLIENT_ERROR(400), diff --git a/core/platform-common/src/main/java/org/sunbird/exception/ResponseMessage.java b/core/platform-common/src/main/java/org/sunbird/exception/ResponseMessage.java index 827a90a32b..4fb12567c4 100644 --- a/core/platform-common/src/main/java/org/sunbird/exception/ResponseMessage.java +++ b/core/platform-common/src/main/java/org/sunbird/exception/ResponseMessage.java @@ -123,6 +123,7 @@ interface Message { String INVALID_TENANT_SECURITY_LEVEL_LOWER = "Tenant level's security {0} cannot be lower than system level's security {1}. Please provide a valid data security level."; String CANNOT_DELETE_USER = "User is restricted from deleting account based on roles!"; + String CANNOT_TRANSFER_OWNERSHIP = "User is restricted from transfering the ownership based on roles!"; } interface Key { @@ -208,5 +209,6 @@ interface Key { String MISSING_DEFAULT_SECURITY_LEVEL = "0081"; String INVALID_TENANT_SECURITY_LEVEL_LOWER = "0082"; String CANNOT_DELETE_USER = "0083"; + String CANNOT_TRANSFER_OWNERSHIP = "0084"; } } diff --git a/core/platform-common/src/main/java/org/sunbird/operations/ActorOperations.java b/core/platform-common/src/main/java/org/sunbird/operations/ActorOperations.java index efef94ef13..f07066689a 100644 --- a/core/platform-common/src/main/java/org/sunbird/operations/ActorOperations.java +++ b/core/platform-common/src/main/java/org/sunbird/operations/ActorOperations.java @@ -148,7 +148,8 @@ public enum ActorOperations { DELETE_LOCATION_FROM_ES("deleteLocationDataFromES", "LBKGDEL"), ADD_ENCRYPTION_KEY("addEncryptionKey", "ADENCKEY"), USER_CURRENT_LOGIN("userCurrentLogin", "USRLOG"), - DELETE_USER("deleteUser", "USRDLT"); + DELETE_USER("deleteUser", "USRDLT"), + USER_OWNERSHIP_TRANSFER("userOwnershipTransfer","UOWNTRANS"); private String value; diff --git a/service/src/main/java/org/sunbird/actor/user/UserOwnershipTransferActor.java b/service/src/main/java/org/sunbird/actor/user/UserOwnershipTransferActor.java new file mode 100644 index 0000000000..bbc3c9f1ba --- /dev/null +++ b/service/src/main/java/org/sunbird/actor/user/UserOwnershipTransferActor.java @@ -0,0 +1,191 @@ +package org.sunbird.actor.user; + +import org.apache.commons.lang.StringUtils; +import org.sunbird.actor.core.BaseActor; +import org.sunbird.exception.ProjectCommonException; +import org.sunbird.exception.ResponseCode; +import org.sunbird.kafka.InstructionEventGenerator; +import org.sunbird.keys.JsonKey; +import org.sunbird.request.Request; +import org.sunbird.request.RequestContext; +import org.sunbird.response.Response; +import org.sunbird.response.ResponseParams; +import org.sunbird.service.user.UserRoleService; +import org.sunbird.service.user.UserService; +import org.sunbird.service.user.impl.UserRoleServiceImpl; +import org.sunbird.service.user.impl.UserServiceImpl; +import org.sunbird.util.ProjectUtil; + +import java.text.MessageFormat; +import java.util.*; + +import static org.sunbird.validator.orgvalidator.BaseOrgRequestValidator.ERROR_CODE; + +public class UserOwnershipTransferActor extends BaseActor { + + private static final String USER_TRANSFER_TOPIC = "user_transfer_topic"; + private final UserRoleService userRoleService = UserRoleServiceImpl.getInstance(); + private final UserService userService = UserServiceImpl.getInstance(); + + @Override + public void onReceive(Request request) throws Throwable { + handleOwnershipTransfer(request); + } + + private void handleOwnershipTransfer(Request request) { + validateUserDetails(request.getRequest(), request.getRequestContext()); + String userId = (String) ((Map) request.getRequest().get("actionBy")).get("userId"); + validateUserRole(userId, request); + List> objects = getObjectsFromRequest(request); + objects.forEach(object -> sendInstructionEvent(request, object)); + sendResponse("Ownership transfer process is submitted successfully!"); + } + + private void validateUserDetails(Map data, RequestContext requestContext) { + validateUser(data.get("actionBy"), "actionBy", requestContext); + validateUser(data.get("fromUser"), "fromUser", requestContext); + validateUser(data.get("toUser"), "toUser", requestContext); + } + + private void validateUser(Object userNode, String userLabel, RequestContext requestContext) { + if (userNode instanceof Map) { + Map user = (Map) userNode; + String userId = StringUtils.trimToNull(Objects.toString(user.get("userId"), "")); + String userName = StringUtils.trimToNull(Objects.toString(user.get("userName"), "")); + + if (StringUtils.isBlank(StringUtils.trimToNull(userId)) || + StringUtils.isBlank(StringUtils.trimToNull(userName))) { + throwInvalidRequestDataException(userLabel); + } + + if (validUser(userId, requestContext)) { + validateRoles(user, userLabel); + } else { + throwClientErrorException(); + } + } + } + + private void validateRoles(Map user, String userLabel) { + if (user.containsKey(JsonKey.ROLES)) { + Object roles = user.get(JsonKey.ROLES); + if (roles instanceof List) { + List rolesList = (List) roles; + if (rolesList.isEmpty()) { + throwInvalidRequestDataException("Roles are empty in " + userLabel + " details."); + } + } else { + throwDataTypeErrorException(); + } + } + } + + private void throwInvalidRequestDataException(String message) { + throw new ProjectCommonException( + ResponseCode.invalidRequestData, + message, + ResponseCode.CLIENT_ERROR.getResponseCode()); + } + + private void throwClientErrorException() { + ProjectCommonException.throwClientErrorException( + ResponseCode.invalidParameter, + MessageFormat.format(ResponseCode.invalidParameter.getErrorMessage(), JsonKey.USER_ID)); + } + + private void throwDataTypeErrorException() { + throw new ProjectCommonException( + ResponseCode.dataTypeError, + ProjectUtil.formatMessage( + ResponseCode.dataTypeError.getErrorMessage(), JsonKey.ROLES, JsonKey.LIST), + ERROR_CODE); + } + + private boolean validUser(String userId, RequestContext context) { + return StringUtils.isNotBlank(userId) && userExists(userId, context); + } + + private boolean userExists(String userId, RequestContext context) { + try { + userService.getUserById(userId, context); + return true; + } catch (Exception ex) { + return false; + } + } + + private void validateUserRole(String userId, Request request) { + List> userRoles = userRoleService.getUserRoles(userId, request.getRequestContext()); + boolean hasOrgAdminRole = userRoles.stream().anyMatch(role -> JsonKey.ORG_ADMIN.equals(role.get("role"))); + if (!hasOrgAdminRole) { + throw new ProjectCommonException( + ResponseCode.cannotTransferOwnership, + ResponseCode.cannotTransferOwnership.getErrorMessage(), + ResponseCode.CLIENT_ERROR.getResponseCode()); + } + } + + private List> getObjectsFromRequest(Request request) { + return Optional.ofNullable((List>) request.getRequest().get("objects")) + .orElse(Collections.emptyList()); + } + + private void sendInstructionEvent(Request request, Map object) { + Map data = prepareEventData(request, object); + try { + InstructionEventGenerator.pushInstructionEvent(USER_TRANSFER_TOPIC, data); + } catch (Exception e) { + logger.error("Error pushing to instruction event", e); + } + } + + private Map prepareEventData(Request request, Map object) { + Map actor = Map.of("id", "ownership-transfer", "type", "System"); + Map context = Map.of( + "channel", "01309282781705830427", + "pdata", Map.of("id", "org.sunbird.platform", "ver", "1.0"), + "env", "dev" + ); + Map edataBase = Map.of( + "action", "ownership-transfer", + "organisationId", request.getRequest().get("organisationId"), + "context", request.getRequest().get("context"), + "actionBy", request.getRequest().get("actionBy"), + "fromUserProfile", request.getRequest().get("fromUser"), + "toUserProfile", request.getRequest().get("toUser") + ); + Map edata = new HashMap<>(edataBase); + Map assetInformation = new HashMap<>(object); + edata.put("assetInformation", assetInformation); + + Map result = new HashMap<>(); + result.put("eid", JsonKey.BE_JOB_REQUEST); + result.put("ets", System.currentTimeMillis()); + result.put("mid", "LP." + System.currentTimeMillis() + "." + UUID.randomUUID()); + result.put("actor", actor); + result.put("context", context); + + // Use 'fromUser' details for 'object' + Map fromUserDetails = (Map) request.getRequest().get("fromUser"); + Map objectDetails = Map.of("id", fromUserDetails.get("userId"), "type", JsonKey.USER); + result.put("object", objectDetails); + result.put("edata", edata); + + return result; + } + + private void sendResponse(String statusMessage) { + Response response = new Response(); + response.setId("api.user.ownership.transfer"); + response.setVer("1.0"); + response.setTs(String.valueOf(Calendar.getInstance().getTime().getTime())); + ResponseParams params = new ResponseParams(); + params.setResmsgid(UUID.randomUUID().toString()); + params.setStatus(String.valueOf(ResponseParams.StatusType.SUCCESSFUL)); + response.setParams(params); + response.setResponseCode(ResponseCode.OK); + Map result = Map.of("status", statusMessage); + response.putAll(result); + sender().tell(response, self()); + } +} From a132bd96806e68c39337f7c0a7e83343c3bbd727 Mon Sep 17 00:00:00 2001 From: BharathwajShankar Date: Tue, 19 Dec 2023 11:02:55 +0530 Subject: [PATCH 02/33] Issue #LR-676 feat: User Delete - ownership transfer api - unittestcase --- .../user/UserOwnershipTransferActorTest.java | 172 ++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java diff --git a/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java b/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java new file mode 100644 index 0000000000..8a13dae281 --- /dev/null +++ b/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java @@ -0,0 +1,172 @@ +package org.sunbird.actor.user; + +import akka.actor.ActorRef; +import akka.actor.ActorSystem; +import akka.actor.Props; +import akka.dispatch.Futures; +import akka.testkit.javadsl.TestKit; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PowerMockIgnore; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.sunbird.cassandraimpl.CassandraOperationImpl; +import org.sunbird.exception.ProjectCommonException; +import org.sunbird.exception.ResponseCode; +import org.sunbird.helper.ServiceFactory; +import org.sunbird.keys.JsonKey; +import org.sunbird.operations.ActorOperations; +import org.sunbird.request.Request; +import org.sunbird.request.RequestContext; +import org.sunbird.response.Response; +import org.sunbird.service.user.UserService; +import org.sunbird.service.user.impl.UserServiceImpl; +import scala.concurrent.Promise; + +import java.util.*; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; +import static org.powermock.api.mockito.PowerMockito.mockStatic; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({ + ServiceFactory.class, + UserServiceImpl.class +}) +@PowerMockIgnore({ + "javax.management.*", + "javax.net.ssl.*", + "javax.security.*", + "jdk.internal.reflect.*", + "javax.crypto.*" +}) +public class UserOwnershipTransferActorTest { + + private static final CassandraOperationImpl cassandraOperation = PowerMockito.mock(CassandraOperationImpl.class); + private static ActorSystem system; + + @BeforeClass + public static void setup() { + system = ActorSystem.create("system"); + } + + @AfterClass + public static void tearDown() { + TestKit.shutdownActorSystem(system, true); + system = null; + } + + private static Response getSuccessResponse() { + Response response = new Response(); + response.put(JsonKey.RESPONSE, JsonKey.SUCCESS); + return response; + } + + @Test + public void testOwnershipTransferSuccess() { + new TestKit(system) {{ + ActorRef userOwnershipTransferActor = system.actorOf(Props.create(UserOwnershipTransferActor.class)); + mockStaticDependencies(); + Request request = createTestRequest(); + request.setRequestContext(new RequestContext()); + userOwnershipTransferActor.tell(request, getRef()); + Object msg = expectMsgAnyClassOf(duration("10 second"), Response.class, ProjectCommonException.class); + if (msg instanceof Response) { + Response res = (Response) msg; + assertSame(res.getResponseCode(), ResponseCode.OK); + } else if (msg instanceof ProjectCommonException) { + ProjectCommonException ex = (ProjectCommonException) msg; + // Handle the exception as needed + assertEquals("UOS_UOWNTRANS0019", ex.getErrorCode()); + assertEquals("Please provide valid userId.", ex.getMessage()); + } else { + fail("Unexpected response type: " + msg.getClass().getName()); + } + }}; + } + + private void mockStaticDependencies() { + mockStatic(ServiceFactory.class); + mockStatic(UserServiceImpl.class); + UserService userServiceMock = mock(UserService.class); + when(UserServiceImpl.getInstance()).thenReturn(userServiceMock); + when(ServiceFactory.getInstance()).thenReturn(cassandraOperation); + Promise booleanPromise = Futures.promise(); + booleanPromise.success(true); + when(cassandraOperation.updateRecord(Mockito.anyString(), Mockito.anyString(), Mockito.anyMap(), Mockito.any())) + .thenReturn(getSuccessResponse()); + when(cassandraOperation.insertRecord(Mockito.anyString(), Mockito.anyString(), Mockito.anyMap(), Mockito.any())) + .thenReturn(getSuccessResponse()); + + Response response2 = new Response(); + Map user = new HashMap<>(); + user.put(JsonKey.ID, "46545665465465"); + user.put(JsonKey.IS_DELETED, false); + user.put(JsonKey.FIRST_NAME, "firstName"); + user.put(JsonKey.STATUS, 1); + List> userList = new ArrayList<>(); + userList.add(user); + response2.getResult().put(JsonKey.RESPONSE, userList); + PowerMockito.when(cassandraOperation.getRecordById( + Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any())) + .thenReturn(response2); + + Response mockResponse = new Response(); + List> mockRoles = new ArrayList<>(); + Map mockRoleData = new HashMap<>(); + mockRoleData.put(JsonKey.ROLE, "ORG_ADMIN"); + mockRoles.add(mockRoleData); + mockResponse.getResult().put(JsonKey.RESPONSE, mockRoles); + PowerMockito.when(cassandraOperation.getRecordById( + Mockito.anyString(), + Mockito.anyString(), + Mockito.anyMap(), + Mockito.any(RequestContext.class))) + .thenReturn(mockResponse); + } + + private Request createTestRequest() { + Request request = new Request(); + Map reqMap = new HashMap<>(); + reqMap.put("context", "User Deletion"); + reqMap.put("organisationId", "0137038836873134080"); + reqMap.put("actionBy", getUserDetailsMap()); + reqMap.put("fromUser", getUserDetailsMap()); + reqMap.put("toUser", getUserDetailsMap()); + reqMap.put("objects", getObjectsList()); + request.setRequest(reqMap); + request.setOperation(ActorOperations.USER_OWNERSHIP_TRANSFER.getValue()); + return request; + } + + private Map getUserDetailsMap() { + Map userDetails = new HashMap<>(); + userDetails.put("userId", "c9e6006e-5811-4337-aa7c-48d0f535e3b8"); + userDetails.put("userName", "TestUser"); + userDetails.put("roles", List.of("ORG_ADMIN")); + return userDetails; + } + + private List> getObjectsList() { + List> objects = new ArrayList<>(); + Map object1 = new HashMap<>(); + object1.put("objectType", "Content"); + object1.put("identifier", "do_id1"); + object1.put("primaryCategory", "ExplanationContent"); + object1.put("name", "TestContent"); + objects.add(object1); + + Map object2 = new HashMap<>(); + object2.put("objectType", "Program"); + object2.put("identifier", "programId1"); + object2.put("name", "TestProgram"); + objects.add(object2); + + return objects; + } +} From d9c3efb44660795c0fcd0fa3ceb78123555fc012 Mon Sep 17 00:00:00 2001 From: BharathwajShankar Date: Tue, 19 Dec 2023 11:12:11 +0530 Subject: [PATCH 03/33] Issue #LR-676 feat: User Delete - ownership transfer api - unittestcase --- .../org/sunbird/actor/user/UserOwnershipTransferActorTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java b/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java index 8a13dae281..a53feecc77 100644 --- a/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java +++ b/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java @@ -75,7 +75,7 @@ public void testOwnershipTransferSuccess() { Request request = createTestRequest(); request.setRequestContext(new RequestContext()); userOwnershipTransferActor.tell(request, getRef()); - Object msg = expectMsgAnyClassOf(duration("10 second"), Response.class, ProjectCommonException.class); + Object msg = expectMsgAnyClassOf(duration("100 second"), Response.class, ProjectCommonException.class); if (msg instanceof Response) { Response res = (Response) msg; assertSame(res.getResponseCode(), ResponseCode.OK); From a7db3993351937bd949d07e9669fad31faa44c53 Mon Sep 17 00:00:00 2001 From: BharathwajShankar Date: Tue, 19 Dec 2023 11:32:00 +0530 Subject: [PATCH 04/33] Issue #LR-676 feat: User Delete - ownership transfer api - unittestcase --- .../org/sunbird/actor/user/UserOwnershipTransferActorTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java b/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java index a53feecc77..f1812dd888 100644 --- a/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java +++ b/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java @@ -75,7 +75,7 @@ public void testOwnershipTransferSuccess() { Request request = createTestRequest(); request.setRequestContext(new RequestContext()); userOwnershipTransferActor.tell(request, getRef()); - Object msg = expectMsgAnyClassOf(duration("100 second"), Response.class, ProjectCommonException.class); + Object msg = expectMsgAnyClassOf(duration("30 second"), Response.class, ProjectCommonException.class); if (msg instanceof Response) { Response res = (Response) msg; assertSame(res.getResponseCode(), ResponseCode.OK); From 37288023e8dfb0cbeb9b36379ae05ed94a5113c3 Mon Sep 17 00:00:00 2001 From: BharathwajShankar Date: Tue, 19 Dec 2023 11:40:44 +0530 Subject: [PATCH 05/33] Issue #LR-676 feat: User Delete - ownership transfer api - unittestcase --- .../org/sunbird/actor/user/UserOwnershipTransferActorTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java b/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java index f1812dd888..8a13dae281 100644 --- a/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java +++ b/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java @@ -75,7 +75,7 @@ public void testOwnershipTransferSuccess() { Request request = createTestRequest(); request.setRequestContext(new RequestContext()); userOwnershipTransferActor.tell(request, getRef()); - Object msg = expectMsgAnyClassOf(duration("30 second"), Response.class, ProjectCommonException.class); + Object msg = expectMsgAnyClassOf(duration("10 second"), Response.class, ProjectCommonException.class); if (msg instanceof Response) { Response res = (Response) msg; assertSame(res.getResponseCode(), ResponseCode.OK); From bba7bbc9131c733332a406de2e70154e6e445110 Mon Sep 17 00:00:00 2001 From: BharathwajShankar Date: Tue, 19 Dec 2023 12:18:03 +0530 Subject: [PATCH 06/33] Issue #LR-676 feat: User Delete - ownership transfer api - unittestcase --- .../sunbird/actor/user/UserOwnershipTransferActorTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java b/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java index 8a13dae281..3e544980e1 100644 --- a/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java +++ b/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java @@ -105,9 +105,9 @@ private void mockStaticDependencies() { Response response2 = new Response(); Map user = new HashMap<>(); - user.put(JsonKey.ID, "46545665465465"); + user.put(JsonKey.ID, "c9e6006e-5811-4337-aa7c-48d0f535e3b8"); user.put(JsonKey.IS_DELETED, false); - user.put(JsonKey.FIRST_NAME, "firstName"); + user.put(JsonKey.FIRST_NAME, "Testuser"); user.put(JsonKey.STATUS, 1); List> userList = new ArrayList<>(); userList.add(user); From 57437fc2b585a7a981390c1a5b1a8b5b1dfa92b6 Mon Sep 17 00:00:00 2001 From: BharathwajShankar Date: Tue, 19 Dec 2023 12:27:49 +0530 Subject: [PATCH 07/33] Issue #LR-676 feat: User Delete - ownership transfer api - unittestcase --- .../org/sunbird/actor/user/UserOwnershipTransferActorTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java b/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java index 3e544980e1..ef1fd91bee 100644 --- a/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java +++ b/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java @@ -75,7 +75,7 @@ public void testOwnershipTransferSuccess() { Request request = createTestRequest(); request.setRequestContext(new RequestContext()); userOwnershipTransferActor.tell(request, getRef()); - Object msg = expectMsgAnyClassOf(duration("10 second"), Response.class, ProjectCommonException.class); + Object msg = expectMsgAnyClassOf(duration("30 second"), Response.class, ProjectCommonException.class); if (msg instanceof Response) { Response res = (Response) msg; assertSame(res.getResponseCode(), ResponseCode.OK); From c598f670562b47b222937492fc7540c1a1006bcb Mon Sep 17 00:00:00 2001 From: BharathwajShankar Date: Wed, 20 Dec 2023 17:04:18 +0530 Subject: [PATCH 08/33] Issue #LR-676 feat: User Delete - ownership transfer api - unittestcase --- .../user/UserOwnershipTransferActor.java | 19 ++- .../user/UserOwnershipTransferActorTest.java | 122 +++++++++++++----- 2 files changed, 102 insertions(+), 39 deletions(-) diff --git a/service/src/main/java/org/sunbird/actor/user/UserOwnershipTransferActor.java b/service/src/main/java/org/sunbird/actor/user/UserOwnershipTransferActor.java index bbc3c9f1ba..6972145c64 100644 --- a/service/src/main/java/org/sunbird/actor/user/UserOwnershipTransferActor.java +++ b/service/src/main/java/org/sunbird/actor/user/UserOwnershipTransferActor.java @@ -42,9 +42,17 @@ private void handleOwnershipTransfer(Request request) { } private void validateUserDetails(Map data, RequestContext requestContext) { - validateUser(data.get("actionBy"), "actionBy", requestContext); - validateUser(data.get("fromUser"), "fromUser", requestContext); - validateUser(data.get("toUser"), "toUser", requestContext); + validateAndProceed(data, "actionBy", requestContext); + validateAndProceed(data, "fromUser", requestContext); + validateAndProceed(data, "toUser", requestContext); + } + + private void validateAndProceed(Map data, String key, RequestContext requestContext) { + if (data.containsKey(key)) { + validateUser(data.get(key), key, requestContext); + } else { + throwInvalidRequestDataException(key + " key is not present in the data."); + } } private void validateUser(Object userNode, String userLabel, RequestContext requestContext) { @@ -55,7 +63,7 @@ private void validateUser(Object userNode, String userLabel, RequestContext requ if (StringUtils.isBlank(StringUtils.trimToNull(userId)) || StringUtils.isBlank(StringUtils.trimToNull(userName))) { - throwInvalidRequestDataException(userLabel); + throwInvalidRequestDataException("User id / user name key is not present in the " + userLabel); } if (validUser(userId, requestContext)) { @@ -67,6 +75,9 @@ private void validateUser(Object userNode, String userLabel, RequestContext requ } private void validateRoles(Map user, String userLabel) { + if (!userLabel.equals("actionBy") && !user.containsKey(JsonKey.ROLES)) { + throwInvalidRequestDataException("Roles key is not present for " + userLabel); + } if (user.containsKey(JsonKey.ROLES)) { Object roles = user.get(JsonKey.ROLES); if (roles instanceof List) { diff --git a/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java b/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java index ef1fd91bee..7f69073e6c 100644 --- a/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java +++ b/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java @@ -5,8 +5,8 @@ import akka.actor.Props; import akka.dispatch.Futures; import akka.testkit.javadsl.TestKit; -import org.junit.AfterClass; -import org.junit.BeforeClass; +import org.junit.After; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; @@ -27,10 +27,16 @@ import org.sunbird.service.user.impl.UserServiceImpl; import scala.concurrent.Promise; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; -import static org.junit.Assert.*; -import static org.mockito.Mockito.*; +import static akka.testkit.JavaTestKit.duration; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import static org.powermock.api.mockito.PowerMockito.mockStatic; @RunWith(PowerMockRunner.class) @@ -48,48 +54,94 @@ public class UserOwnershipTransferActorTest { private static final CassandraOperationImpl cassandraOperation = PowerMockito.mock(CassandraOperationImpl.class); - private static ActorSystem system; + Props props = Props.create(UserOwnershipTransferActor.class); + ActorSystem system = ActorSystem.create("userOwnershipTransferActor"); - @BeforeClass - public static void setup() { - system = ActorSystem.create("system"); + private static Response getSuccessResponse() { + Response response = new Response(); + response.put(JsonKey.RESPONSE, JsonKey.SUCCESS); + return response; + } + + private static boolean computeResult(Object msg) { + if (msg instanceof Response) { + Response res = (Response) msg; + return res.getResponseCode() == ResponseCode.OK; + } else if (msg instanceof ProjectCommonException) { + ProjectCommonException res = (ProjectCommonException) msg; + if (res.getErrorCode().equals(ResponseCode.invalidRequestData.getErrorCode())) { + return res.getErrorResponseCode() == ResponseCode.CLIENT_ERROR.getResponseCode(); + } else if (res.getErrorCode().equals(ResponseCode.invalidParameter.getErrorCode())) { + return res.getErrorResponseCode() == ResponseCode.invalidParameter.getResponseCode(); + } else if (res.getErrorCode().equals(ResponseCode.dataTypeError.getErrorCode())) { + return res.getErrorResponseCode() == ResponseCode.dataTypeError.getResponseCode(); + } else { + return false; + } + } else { + return false; + } + } + + @Before + public void beforeEachTest() { + mockStaticDependencies(); } - @AfterClass - public static void tearDown() { - TestKit.shutdownActorSystem(system, true); + @After + public void afterEachTest() { + TestKit.shutdownActorSystem(system); system = null; } - private static Response getSuccessResponse() { - Response response = new Response(); - response.put(JsonKey.RESPONSE, JsonKey.SUCCESS); - return response; + private boolean testActorBehavior(Request request, ResponseCode errorCode) { + TestKit probe = new TestKit(system); + ActorRef subject = system.actorOf(props); + + try { + subject.tell(request, probe.getRef()); + Object msg = probe.expectMsgAnyClassOf(duration("30 second"), Response.class, ProjectCommonException.class); + return computeResult(msg); + } catch (Exception e) { + return false; + } } @Test public void testOwnershipTransferSuccess() { - new TestKit(system) {{ - ActorRef userOwnershipTransferActor = system.actorOf(Props.create(UserOwnershipTransferActor.class)); - mockStaticDependencies(); - Request request = createTestRequest(); - request.setRequestContext(new RequestContext()); - userOwnershipTransferActor.tell(request, getRef()); - Object msg = expectMsgAnyClassOf(duration("30 second"), Response.class, ProjectCommonException.class); - if (msg instanceof Response) { - Response res = (Response) msg; - assertSame(res.getResponseCode(), ResponseCode.OK); - } else if (msg instanceof ProjectCommonException) { - ProjectCommonException ex = (ProjectCommonException) msg; - // Handle the exception as needed - assertEquals("UOS_UOWNTRANS0019", ex.getErrorCode()); - assertEquals("Please provide valid userId.", ex.getMessage()); - } else { - fail("Unexpected response type: " + msg.getClass().getName()); - } - }}; + Request request = createTestRequest(); + request.setRequestContext(new RequestContext()); + boolean result = testActorBehavior(request, null); + assertTrue(result); + } + + @Test + public void testInvalidUserDetails() { + Request request = createTestRequest(); + request.getRequest().remove("fromUser"); + request.setRequestContext(new RequestContext()); + boolean response = testActorBehavior(request, null); + assertFalse(response); + } + + @Test + public void testInvalidRoleDetails() { + Request request = createTestRequest(); + ((Map) request.getRequest().get("fromUser")).remove("roles"); + request.setRequestContext(new RequestContext()); + boolean response = testActorBehavior(request, null); + assertFalse(response); } + @Test + public void testInvalidActorDetails() { + // Test scenario where "actionBy" details are invalid (e.g., missing "userId" or "userName"). + Request request = createTestRequest(); + ((Map) request.getRequest().get("actionBy")).remove("userId"); + request.setRequestContext(new RequestContext()); + boolean response = testActorBehavior(request, null); + assertFalse(response); + } private void mockStaticDependencies() { mockStatic(ServiceFactory.class); mockStatic(UserServiceImpl.class); From eecce1161977784d3cae1aa8c16b3afa0f8d59dc Mon Sep 17 00:00:00 2001 From: BharathwajShankar Date: Tue, 26 Dec 2023 10:22:46 +0530 Subject: [PATCH 09/33] Issue #LR-676 feat: User Delete - ownership transfer api - unittestcase --- .../org/sunbird/actor/user/UserOwnershipTransferActorTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java b/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java index 7f69073e6c..5f32b30699 100644 --- a/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java +++ b/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java @@ -208,7 +208,7 @@ private List> getObjectsList() { List> objects = new ArrayList<>(); Map object1 = new HashMap<>(); object1.put("objectType", "Content"); - object1.put("identifier", "do_id1"); + object1.put("identifier", "do_id12"); object1.put("primaryCategory", "ExplanationContent"); object1.put("name", "TestContent"); objects.add(object1); From 5152ec19dfc353b2fef8e27a103ff6b54ea41148 Mon Sep 17 00:00:00 2001 From: BharathwajShankar Date: Wed, 27 Dec 2023 13:44:54 +0530 Subject: [PATCH 10/33] Issue #LR-676 feat: User Delete - ownership transfer api - unittestcase --- .../sunbird/actor/user/UserOwnershipTransferActorTest.java | 6 ------ 1 file changed, 6 deletions(-) diff --git a/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java b/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java index 5f32b30699..45847641b8 100644 --- a/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java +++ b/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java @@ -88,12 +88,6 @@ public void beforeEachTest() { mockStaticDependencies(); } - @After - public void afterEachTest() { - TestKit.shutdownActorSystem(system); - system = null; - } - private boolean testActorBehavior(Request request, ResponseCode errorCode) { TestKit probe = new TestKit(system); ActorRef subject = system.actorOf(props); From a15a84b5b2b535cbde35a6b12bd54ff8087549c8 Mon Sep 17 00:00:00 2001 From: BharathwajShankar Date: Wed, 27 Dec 2023 14:01:17 +0530 Subject: [PATCH 11/33] Issue #LR-676 feat: User Delete - ownership transfer api - unittestcase --- .../actor/user/UserOwnershipTransferActorTest.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java b/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java index 45847641b8..3ba23e381b 100644 --- a/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java +++ b/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java @@ -88,6 +88,12 @@ public void beforeEachTest() { mockStaticDependencies(); } + @After + public void afterEachTest() { + TestKit.shutdownActorSystem(system); + system = null; + } + private boolean testActorBehavior(Request request, ResponseCode errorCode) { TestKit probe = new TestKit(system); ActorRef subject = system.actorOf(props); @@ -193,7 +199,7 @@ private Request createTestRequest() { private Map getUserDetailsMap() { Map userDetails = new HashMap<>(); userDetails.put("userId", "c9e6006e-5811-4337-aa7c-48d0f535e3b8"); - userDetails.put("userName", "TestUser"); + userDetails.put("userName", "Testuser"); userDetails.put("roles", List.of("ORG_ADMIN")); return userDetails; } From 31c17aa121599f9c89c7c4242f9e77ff5b1902cd Mon Sep 17 00:00:00 2001 From: BharathwajShankar Date: Wed, 27 Dec 2023 14:32:16 +0530 Subject: [PATCH 12/33] Issue #LR-676 feat: User Delete - ownership transfer api - unittestcase --- .../user/UserOwnershipTransferActorTest.java | 174 ++++++++++-------- 1 file changed, 96 insertions(+), 78 deletions(-) diff --git a/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java b/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java index 3ba23e381b..cfcffacf54 100644 --- a/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java +++ b/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java @@ -5,8 +5,8 @@ import akka.actor.Props; import akka.dispatch.Futures; import akka.testkit.javadsl.TestKit; -import org.junit.After; -import org.junit.Before; +import org.junit.AfterClass; +import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; @@ -27,16 +27,10 @@ import org.sunbird.service.user.impl.UserServiceImpl; import scala.concurrent.Promise; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; -import static akka.testkit.JavaTestKit.duration; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; import static org.powermock.api.mockito.PowerMockito.mockStatic; @RunWith(PowerMockRunner.class) @@ -54,94 +48,118 @@ public class UserOwnershipTransferActorTest { private static final CassandraOperationImpl cassandraOperation = PowerMockito.mock(CassandraOperationImpl.class); - Props props = Props.create(UserOwnershipTransferActor.class); - ActorSystem system = ActorSystem.create("userOwnershipTransferActor"); + private static ActorSystem system; - private static Response getSuccessResponse() { - Response response = new Response(); - response.put(JsonKey.RESPONSE, JsonKey.SUCCESS); - return response; - } - - private static boolean computeResult(Object msg) { - if (msg instanceof Response) { - Response res = (Response) msg; - return res.getResponseCode() == ResponseCode.OK; - } else if (msg instanceof ProjectCommonException) { - ProjectCommonException res = (ProjectCommonException) msg; - if (res.getErrorCode().equals(ResponseCode.invalidRequestData.getErrorCode())) { - return res.getErrorResponseCode() == ResponseCode.CLIENT_ERROR.getResponseCode(); - } else if (res.getErrorCode().equals(ResponseCode.invalidParameter.getErrorCode())) { - return res.getErrorResponseCode() == ResponseCode.invalidParameter.getResponseCode(); - } else if (res.getErrorCode().equals(ResponseCode.dataTypeError.getErrorCode())) { - return res.getErrorResponseCode() == ResponseCode.dataTypeError.getResponseCode(); - } else { - return false; - } - } else { - return false; - } - } - - @Before - public void beforeEachTest() { - mockStaticDependencies(); + @BeforeClass + public static void setup() { + system = ActorSystem.create("system"); } - @After - public void afterEachTest() { - TestKit.shutdownActorSystem(system); + @AfterClass + public static void tearDown() { + TestKit.shutdownActorSystem(system, true); system = null; } - private boolean testActorBehavior(Request request, ResponseCode errorCode) { - TestKit probe = new TestKit(system); - ActorRef subject = system.actorOf(props); - - try { - subject.tell(request, probe.getRef()); - Object msg = probe.expectMsgAnyClassOf(duration("30 second"), Response.class, ProjectCommonException.class); - return computeResult(msg); - } catch (Exception e) { - return false; - } + private static Response getSuccessResponse() { + Response response = new Response(); + response.put(JsonKey.RESPONSE, JsonKey.SUCCESS); + return response; } @Test public void testOwnershipTransferSuccess() { - Request request = createTestRequest(); - request.setRequestContext(new RequestContext()); - boolean result = testActorBehavior(request, null); - assertTrue(result); + new TestKit(system) {{ + ActorRef userOwnershipTransferActor = system.actorOf(Props.create(UserOwnershipTransferActor.class)); + mockStaticDependencies(); + Request request = createTestRequest(); + request.setRequestContext(new RequestContext()); + userOwnershipTransferActor.tell(request, getRef()); + Object msg = expectMsgAnyClassOf(duration("30 second"), Response.class, ProjectCommonException.class); + if (msg instanceof Response) { + Response res = (Response) msg; + assertSame(res.getResponseCode(), ResponseCode.OK); + } else if (msg instanceof ProjectCommonException) { + ProjectCommonException ex = (ProjectCommonException) msg; + // Handle the exception as needed + assertEquals("UOS_UOWNTRANS0019", ex.getErrorCode()); + assertEquals("Please provide valid userId.", ex.getMessage()); + } else { + fail("Unexpected response type: " + msg.getClass().getName()); + } + }}; } @Test public void testInvalidUserDetails() { - Request request = createTestRequest(); - request.getRequest().remove("fromUser"); - request.setRequestContext(new RequestContext()); - boolean response = testActorBehavior(request, null); - assertFalse(response); + new TestKit(system) {{ + ActorRef userOwnershipTransferActor = system.actorOf(Props.create(UserOwnershipTransferActor.class)); + mockStaticDependencies(); + Request request = createTestRequest(); + request.getRequest().remove("fromUser"); + request.setRequestContext(new RequestContext()); + userOwnershipTransferActor.tell(request, getRef()); + Object msg = expectMsgAnyClassOf(duration("30 second"), Response.class, ProjectCommonException.class); + if (msg instanceof Response) { + Response res = (Response) msg; + assertSame(res.getResponseCode(), ResponseCode.invalidRequestData); + } else if (msg instanceof ProjectCommonException) { + ProjectCommonException ex = (ProjectCommonException) msg; + assertEquals("UOS_UOWNTRANS0028", ex.getErrorCode()); + assertEquals("fromUser key is not present in the data.", ex.getMessage()); + } else { + fail("Unexpected response type: " + msg.getClass().getName()); + } + }}; } @Test public void testInvalidRoleDetails() { - Request request = createTestRequest(); - ((Map) request.getRequest().get("fromUser")).remove("roles"); - request.setRequestContext(new RequestContext()); - boolean response = testActorBehavior(request, null); - assertFalse(response); + new TestKit(system) {{ + ActorRef userOwnershipTransferActor = system.actorOf(Props.create(UserOwnershipTransferActor.class)); + mockStaticDependencies(); + Request request = createTestRequest(); + ((Map) request.getRequest().get("fromUser")).remove("roles"); + request.setRequestContext(new RequestContext()); + userOwnershipTransferActor.tell(request, getRef()); + Object msg = expectMsgAnyClassOf(duration("30 second"), Response.class, ProjectCommonException.class); + if (msg instanceof Response) { + Response res = (Response) msg; + assertSame(res.getResponseCode(), ResponseCode.invalidParameter); + } else if (msg instanceof ProjectCommonException) { + ProjectCommonException ex = (ProjectCommonException) msg; + assertEquals("UOS_UOWNTRANS0028", ex.getErrorCode()); + assertEquals("Roles key is not present for fromUser", ex.getMessage()); + } else { + fail("Unexpected response type: " + msg.getClass().getName()); + } + }}; } @Test public void testInvalidActorDetails() { - // Test scenario where "actionBy" details are invalid (e.g., missing "userId" or "userName"). - Request request = createTestRequest(); - ((Map) request.getRequest().get("actionBy")).remove("userId"); - request.setRequestContext(new RequestContext()); - boolean response = testActorBehavior(request, null); - assertFalse(response); + new TestKit(system) {{ + ActorRef userOwnershipTransferActor = system.actorOf(Props.create(UserOwnershipTransferActor.class)); + mockStaticDependencies(); + Request request = createTestRequest(); + // Test scenario where "actionBy" details are invalid (e.g., missing "userId" or "userName"). + ((Map) request.getRequest().get("actionBy")).remove("userId"); + request.setRequestContext(new RequestContext()); + userOwnershipTransferActor.tell(request, getRef()); + Object msg = expectMsgAnyClassOf(duration("30 second"), Response.class, ProjectCommonException.class); + if (msg instanceof Response) { + Response res = (Response) msg; + assertSame(res.getResponseCode(), ResponseCode.invalidParameter); + } else if (msg instanceof ProjectCommonException) { + ProjectCommonException ex = (ProjectCommonException) msg; + assertEquals("UOS_UOWNTRANS0028", ex.getErrorCode()); + assertEquals("User id / user name key is not present in the actionBy", ex.getMessage()); + } else { + fail("Unexpected response type: " + msg.getClass().getName()); + } + }}; } + private void mockStaticDependencies() { mockStatic(ServiceFactory.class); mockStatic(UserServiceImpl.class); @@ -199,7 +217,7 @@ private Request createTestRequest() { private Map getUserDetailsMap() { Map userDetails = new HashMap<>(); userDetails.put("userId", "c9e6006e-5811-4337-aa7c-48d0f535e3b8"); - userDetails.put("userName", "Testuser"); + userDetails.put("userName", "TestUser"); userDetails.put("roles", List.of("ORG_ADMIN")); return userDetails; } @@ -208,7 +226,7 @@ private List> getObjectsList() { List> objects = new ArrayList<>(); Map object1 = new HashMap<>(); object1.put("objectType", "Content"); - object1.put("identifier", "do_id12"); + object1.put("identifier", "do_id1"); object1.put("primaryCategory", "ExplanationContent"); object1.put("name", "TestContent"); objects.add(object1); From 993642e77ebb0e4e40a93d9da85e63f738394cb1 Mon Sep 17 00:00:00 2001 From: BharathwajShankar Date: Wed, 27 Dec 2023 15:00:40 +0530 Subject: [PATCH 13/33] Issue #LR-676 feat: User Delete - ownership transfer api - unittestcase --- .../user/UserOwnershipTransferActorTest.java | 173 ++++++++---------- 1 file changed, 80 insertions(+), 93 deletions(-) diff --git a/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java b/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java index cfcffacf54..442fa4169c 100644 --- a/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java +++ b/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java @@ -5,9 +5,7 @@ import akka.actor.Props; import akka.dispatch.Futures; import akka.testkit.javadsl.TestKit; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.*; import org.junit.runner.RunWith; import org.mockito.Mockito; import org.powermock.api.mockito.PowerMockito; @@ -26,11 +24,18 @@ import org.sunbird.service.user.UserService; import org.sunbird.service.user.impl.UserServiceImpl; import scala.concurrent.Promise; - -import java.util.*; - -import static org.junit.Assert.*; -import static org.mockito.Mockito.*; +import scala.concurrent.duration.FiniteDuration; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static akka.testkit.JavaTestKit.duration; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import static org.powermock.api.mockito.PowerMockito.mockStatic; @RunWith(PowerMockRunner.class) @@ -48,118 +53,100 @@ public class UserOwnershipTransferActorTest { private static final CassandraOperationImpl cassandraOperation = PowerMockito.mock(CassandraOperationImpl.class); + Props props = Props.create(UserOwnershipTransferActor.class); + private static final FiniteDuration ACTOR_MAX_WAIT_DURATION = duration("30 second"); private static ActorSystem system; + + private static Response getSuccessResponse() { + Response response = new Response(); + response.put(JsonKey.RESPONSE, JsonKey.SUCCESS); + return response; + } + + private static boolean computeResult(Object msg) { + if (msg instanceof Response) { + Response res = (Response) msg; + return res.getResponseCode() == ResponseCode.OK; + } else if (msg instanceof ProjectCommonException) { + ProjectCommonException res = (ProjectCommonException) msg; + if (res.getErrorCode().equals(ResponseCode.invalidRequestData.getErrorCode())) { + return res.getErrorResponseCode() == ResponseCode.CLIENT_ERROR.getResponseCode(); + } else if (res.getErrorCode().equals(ResponseCode.invalidParameter.getErrorCode())) { + return res.getErrorResponseCode() == ResponseCode.invalidParameter.getResponseCode(); + } else if (res.getErrorCode().equals(ResponseCode.dataTypeError.getErrorCode())) { + return res.getErrorResponseCode() == ResponseCode.dataTypeError.getResponseCode(); + } else { + return false; + } + } else { + return false; + } + } + @BeforeClass public static void setup() { system = ActorSystem.create("system"); } + @Before + public void beforeEachTest() { + mockStaticDependencies(); + } + @AfterClass public static void tearDown() { TestKit.shutdownActorSystem(system, true); - system = null; } - private static Response getSuccessResponse() { - Response response = new Response(); - response.put(JsonKey.RESPONSE, JsonKey.SUCCESS); - return response; + private boolean testActorBehavior(Request request, ResponseCode errorCode) { + TestKit probe = new TestKit(system); + ActorRef subject = system.actorOf(props); + + try { + subject.tell(request, probe.getRef()); + Object msg = probe.expectMsgAnyClassOf(ACTOR_MAX_WAIT_DURATION, Response.class, ProjectCommonException.class); + return computeResult(msg); + } catch (Exception e) { + return false; + } } @Test public void testOwnershipTransferSuccess() { - new TestKit(system) {{ - ActorRef userOwnershipTransferActor = system.actorOf(Props.create(UserOwnershipTransferActor.class)); - mockStaticDependencies(); - Request request = createTestRequest(); - request.setRequestContext(new RequestContext()); - userOwnershipTransferActor.tell(request, getRef()); - Object msg = expectMsgAnyClassOf(duration("30 second"), Response.class, ProjectCommonException.class); - if (msg instanceof Response) { - Response res = (Response) msg; - assertSame(res.getResponseCode(), ResponseCode.OK); - } else if (msg instanceof ProjectCommonException) { - ProjectCommonException ex = (ProjectCommonException) msg; - // Handle the exception as needed - assertEquals("UOS_UOWNTRANS0019", ex.getErrorCode()); - assertEquals("Please provide valid userId.", ex.getMessage()); - } else { - fail("Unexpected response type: " + msg.getClass().getName()); - } - }}; + Request request = createTestRequest(); + request.setRequestContext(new RequestContext()); + boolean result = testActorBehavior(request, null); + assertTrue(result); } @Test public void testInvalidUserDetails() { - new TestKit(system) {{ - ActorRef userOwnershipTransferActor = system.actorOf(Props.create(UserOwnershipTransferActor.class)); - mockStaticDependencies(); - Request request = createTestRequest(); - request.getRequest().remove("fromUser"); - request.setRequestContext(new RequestContext()); - userOwnershipTransferActor.tell(request, getRef()); - Object msg = expectMsgAnyClassOf(duration("30 second"), Response.class, ProjectCommonException.class); - if (msg instanceof Response) { - Response res = (Response) msg; - assertSame(res.getResponseCode(), ResponseCode.invalidRequestData); - } else if (msg instanceof ProjectCommonException) { - ProjectCommonException ex = (ProjectCommonException) msg; - assertEquals("UOS_UOWNTRANS0028", ex.getErrorCode()); - assertEquals("fromUser key is not present in the data.", ex.getMessage()); - } else { - fail("Unexpected response type: " + msg.getClass().getName()); - } - }}; + Request request = createTestRequest(); + request.getRequest().remove("fromUser"); + request.setRequestContext(new RequestContext()); + boolean response = testActorBehavior(request, null); + assertFalse(response); } @Test public void testInvalidRoleDetails() { - new TestKit(system) {{ - ActorRef userOwnershipTransferActor = system.actorOf(Props.create(UserOwnershipTransferActor.class)); - mockStaticDependencies(); - Request request = createTestRequest(); - ((Map) request.getRequest().get("fromUser")).remove("roles"); - request.setRequestContext(new RequestContext()); - userOwnershipTransferActor.tell(request, getRef()); - Object msg = expectMsgAnyClassOf(duration("30 second"), Response.class, ProjectCommonException.class); - if (msg instanceof Response) { - Response res = (Response) msg; - assertSame(res.getResponseCode(), ResponseCode.invalidParameter); - } else if (msg instanceof ProjectCommonException) { - ProjectCommonException ex = (ProjectCommonException) msg; - assertEquals("UOS_UOWNTRANS0028", ex.getErrorCode()); - assertEquals("Roles key is not present for fromUser", ex.getMessage()); - } else { - fail("Unexpected response type: " + msg.getClass().getName()); - } - }}; + Request request = createTestRequest(); + ((Map) request.getRequest().get("fromUser")).remove("roles"); + request.setRequestContext(new RequestContext()); + boolean response = testActorBehavior(request, null); + assertFalse(response); } @Test public void testInvalidActorDetails() { - new TestKit(system) {{ - ActorRef userOwnershipTransferActor = system.actorOf(Props.create(UserOwnershipTransferActor.class)); - mockStaticDependencies(); - Request request = createTestRequest(); - // Test scenario where "actionBy" details are invalid (e.g., missing "userId" or "userName"). - ((Map) request.getRequest().get("actionBy")).remove("userId"); - request.setRequestContext(new RequestContext()); - userOwnershipTransferActor.tell(request, getRef()); - Object msg = expectMsgAnyClassOf(duration("30 second"), Response.class, ProjectCommonException.class); - if (msg instanceof Response) { - Response res = (Response) msg; - assertSame(res.getResponseCode(), ResponseCode.invalidParameter); - } else if (msg instanceof ProjectCommonException) { - ProjectCommonException ex = (ProjectCommonException) msg; - assertEquals("UOS_UOWNTRANS0028", ex.getErrorCode()); - assertEquals("User id / user name key is not present in the actionBy", ex.getMessage()); - } else { - fail("Unexpected response type: " + msg.getClass().getName()); - } - }}; + // Test scenario where "actionBy" details are invalid (e.g., missing "userId" or "userName"). + Request request = createTestRequest(); + ((Map) request.getRequest().get("actionBy")).remove("userId"); + request.setRequestContext(new RequestContext()); + boolean response = testActorBehavior(request, null); + assertFalse(response); } - private void mockStaticDependencies() { mockStatic(ServiceFactory.class); mockStatic(UserServiceImpl.class); @@ -217,7 +204,7 @@ private Request createTestRequest() { private Map getUserDetailsMap() { Map userDetails = new HashMap<>(); userDetails.put("userId", "c9e6006e-5811-4337-aa7c-48d0f535e3b8"); - userDetails.put("userName", "TestUser"); + userDetails.put("userName", "Testuser"); userDetails.put("roles", List.of("ORG_ADMIN")); return userDetails; } @@ -226,7 +213,7 @@ private List> getObjectsList() { List> objects = new ArrayList<>(); Map object1 = new HashMap<>(); object1.put("objectType", "Content"); - object1.put("identifier", "do_id1"); + object1.put("identifier", "do_id12"); object1.put("primaryCategory", "ExplanationContent"); object1.put("name", "TestContent"); objects.add(object1); From 7cb80d673c8ff84d12cc259c6400f53cb843be45 Mon Sep 17 00:00:00 2001 From: BharathwajShankar Date: Tue, 2 Jan 2024 10:25:48 +0530 Subject: [PATCH 14/33] Issue #LR-676 feat: User Delete - ownership transfer api - unittestcase --- .../user/UserOwnershipTransferActorTest.java | 92 ++++++++----------- 1 file changed, 40 insertions(+), 52 deletions(-) diff --git a/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java b/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java index 442fa4169c..4574cd1e57 100644 --- a/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java +++ b/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java @@ -5,7 +5,10 @@ import akka.actor.Props; import akka.dispatch.Futures; import akka.testkit.javadsl.TestKit; -import org.junit.*; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; import org.powermock.api.mockito.PowerMockito; @@ -24,16 +27,14 @@ import org.sunbird.service.user.UserService; import org.sunbird.service.user.impl.UserServiceImpl; import scala.concurrent.Promise; -import scala.concurrent.duration.FiniteDuration; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import static akka.testkit.JavaTestKit.duration; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.powermock.api.mockito.PowerMockito.mockStatic; @@ -53,10 +54,8 @@ public class UserOwnershipTransferActorTest { private static final CassandraOperationImpl cassandraOperation = PowerMockito.mock(CassandraOperationImpl.class); - Props props = Props.create(UserOwnershipTransferActor.class); - private static final FiniteDuration ACTOR_MAX_WAIT_DURATION = duration("30 second"); private static ActorSystem system; - + Props props = Props.create(UserOwnershipTransferActor.class); private static Response getSuccessResponse() { Response response = new Response(); @@ -64,89 +63,78 @@ private static Response getSuccessResponse() { return response; } - private static boolean computeResult(Object msg) { - if (msg instanceof Response) { - Response res = (Response) msg; - return res.getResponseCode() == ResponseCode.OK; - } else if (msg instanceof ProjectCommonException) { - ProjectCommonException res = (ProjectCommonException) msg; - if (res.getErrorCode().equals(ResponseCode.invalidRequestData.getErrorCode())) { - return res.getErrorResponseCode() == ResponseCode.CLIENT_ERROR.getResponseCode(); - } else if (res.getErrorCode().equals(ResponseCode.invalidParameter.getErrorCode())) { - return res.getErrorResponseCode() == ResponseCode.invalidParameter.getResponseCode(); - } else if (res.getErrorCode().equals(ResponseCode.dataTypeError.getErrorCode())) { - return res.getErrorResponseCode() == ResponseCode.dataTypeError.getResponseCode(); - } else { - return false; - } - } else { - return false; - } - } - @BeforeClass public static void setup() { system = ActorSystem.create("system"); } - @Before - public void beforeEachTest() { - mockStaticDependencies(); - } - @AfterClass public static void tearDown() { TestKit.shutdownActorSystem(system, true); } - private boolean testActorBehavior(Request request, ResponseCode errorCode) { - TestKit probe = new TestKit(system); - ActorRef subject = system.actorOf(props); - - try { - subject.tell(request, probe.getRef()); - Object msg = probe.expectMsgAnyClassOf(ACTOR_MAX_WAIT_DURATION, Response.class, ProjectCommonException.class); - return computeResult(msg); - } catch (Exception e) { - return false; - } + @Before + public void beforeEachTest() { + mockStaticDependencies(); } @Test public void testOwnershipTransferSuccess() { + TestKit probe = new TestKit(system); + ActorRef subject = system.actorOf(props); Request request = createTestRequest(); request.setRequestContext(new RequestContext()); - boolean result = testActorBehavior(request, null); - assertTrue(result); + subject.tell(request, probe.getRef()); + Response response = probe.expectMsgClass(Response.class); + Map result = response.getResult(); + assertNotNull(result); + assertEquals("Ownership transfer process is submitted successfully!", result.get("status")); } @Test public void testInvalidUserDetails() { + TestKit probe = new TestKit(system); + ActorRef subject = system.actorOf(props); Request request = createTestRequest(); request.getRequest().remove("fromUser"); request.setRequestContext(new RequestContext()); - boolean response = testActorBehavior(request, null); - assertFalse(response); + subject.tell(request, probe.getRef()); + ProjectCommonException errorResponse = probe.expectMsgClass(ProjectCommonException.class); + assertEquals("UOS_UOWNTRANS0028", errorResponse.getErrorCode()); + assertEquals("fromUser key is not present in the data.", errorResponse.getMessage()); + assertEquals(ResponseCode.CLIENT_ERROR.getResponseCode(), errorResponse.getErrorResponseCode()); } @Test public void testInvalidRoleDetails() { + TestKit probe = new TestKit(system); + ActorRef subject = system.actorOf(props); Request request = createTestRequest(); ((Map) request.getRequest().get("fromUser")).remove("roles"); request.setRequestContext(new RequestContext()); - boolean response = testActorBehavior(request, null); - assertFalse(response); + subject.tell(request, probe.getRef()); + ProjectCommonException errorResponse = probe.expectMsgClass(ProjectCommonException.class); + assertEquals("UOS_UOWNTRANS0028", errorResponse.getErrorCode()); + assertEquals("Roles key is not present for fromUser", errorResponse.getMessage()); + assertEquals(ResponseCode.CLIENT_ERROR.getResponseCode(), errorResponse.getErrorResponseCode()); } @Test public void testInvalidActorDetails() { // Test scenario where "actionBy" details are invalid (e.g., missing "userId" or "userName"). + TestKit probe = new TestKit(system); + ActorRef subject = system.actorOf(props); Request request = createTestRequest(); ((Map) request.getRequest().get("actionBy")).remove("userId"); request.setRequestContext(new RequestContext()); - boolean response = testActorBehavior(request, null); - assertFalse(response); + subject.tell(request, probe.getRef()); + ProjectCommonException errorResponse = probe.expectMsgClass(ProjectCommonException.class); + assertEquals("UOS_UOWNTRANS0028", errorResponse.getErrorCode()); + assertEquals("User id / user name key is not present in the actionBy", + errorResponse.getMessage()); + assertEquals(ResponseCode.CLIENT_ERROR.getResponseCode(), errorResponse.getErrorResponseCode()); } + private void mockStaticDependencies() { mockStatic(ServiceFactory.class); mockStatic(UserServiceImpl.class); From 7830b4b3eb25d18663bcf8c1e9d39a184c441a01 Mon Sep 17 00:00:00 2001 From: BharathwajShankar Date: Tue, 2 Jan 2024 10:38:41 +0530 Subject: [PATCH 15/33] Issue #LR-676 feat: User Delete - ownership transfer api - unittestcase --- .../actor/user/UserOwnershipTransferActorTest.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java b/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java index 4574cd1e57..a7a8a7bdf7 100644 --- a/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java +++ b/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java @@ -28,6 +28,7 @@ import org.sunbird.service.user.impl.UserServiceImpl; import scala.concurrent.Promise; +import java.time.Duration; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -85,7 +86,7 @@ public void testOwnershipTransferSuccess() { Request request = createTestRequest(); request.setRequestContext(new RequestContext()); subject.tell(request, probe.getRef()); - Response response = probe.expectMsgClass(Response.class); + Response response = probe.expectMsgClass(Duration.ofSeconds(30), Response.class); Map result = response.getResult(); assertNotNull(result); assertEquals("Ownership transfer process is submitted successfully!", result.get("status")); @@ -99,7 +100,7 @@ public void testInvalidUserDetails() { request.getRequest().remove("fromUser"); request.setRequestContext(new RequestContext()); subject.tell(request, probe.getRef()); - ProjectCommonException errorResponse = probe.expectMsgClass(ProjectCommonException.class); + ProjectCommonException errorResponse = probe.expectMsgClass(Duration.ofSeconds(30), ProjectCommonException.class); assertEquals("UOS_UOWNTRANS0028", errorResponse.getErrorCode()); assertEquals("fromUser key is not present in the data.", errorResponse.getMessage()); assertEquals(ResponseCode.CLIENT_ERROR.getResponseCode(), errorResponse.getErrorResponseCode()); @@ -113,7 +114,8 @@ public void testInvalidRoleDetails() { ((Map) request.getRequest().get("fromUser")).remove("roles"); request.setRequestContext(new RequestContext()); subject.tell(request, probe.getRef()); - ProjectCommonException errorResponse = probe.expectMsgClass(ProjectCommonException.class); + ProjectCommonException errorResponse = probe.expectMsgClass(Duration.ofSeconds(30), + ProjectCommonException.class); assertEquals("UOS_UOWNTRANS0028", errorResponse.getErrorCode()); assertEquals("Roles key is not present for fromUser", errorResponse.getMessage()); assertEquals(ResponseCode.CLIENT_ERROR.getResponseCode(), errorResponse.getErrorResponseCode()); @@ -128,7 +130,8 @@ public void testInvalidActorDetails() { ((Map) request.getRequest().get("actionBy")).remove("userId"); request.setRequestContext(new RequestContext()); subject.tell(request, probe.getRef()); - ProjectCommonException errorResponse = probe.expectMsgClass(ProjectCommonException.class); + ProjectCommonException errorResponse = probe.expectMsgClass(Duration.ofSeconds(30), + ProjectCommonException.class); assertEquals("UOS_UOWNTRANS0028", errorResponse.getErrorCode()); assertEquals("User id / user name key is not present in the actionBy", errorResponse.getMessage()); From 81675b1ffb4731cd8e3ee7e28fd5e14cde3d2c25 Mon Sep 17 00:00:00 2001 From: BharathwajShankar Date: Tue, 2 Jan 2024 11:47:39 +0530 Subject: [PATCH 16/33] Issue #LR-676 feat: User Delete - ownership transfer api - unittestcase --- .../app/controllers/usermanagement/UserController.java | 4 ++++ .../sunbird/actor/user/UserOwnershipTransferActor.java | 2 +- .../actor/user/UserOwnershipTransferActorTest.java | 8 ++++---- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/controller/app/controllers/usermanagement/UserController.java b/controller/app/controllers/usermanagement/UserController.java index 605e4360aa..602a1fece3 100644 --- a/controller/app/controllers/usermanagement/UserController.java +++ b/controller/app/controllers/usermanagement/UserController.java @@ -13,6 +13,7 @@ import org.sunbird.request.Request; import org.sunbird.util.ProjectUtil; import org.sunbird.validator.BaseRequestValidator; +import play.libs.typedmap.TypedKey; import play.mvc.Http; import play.mvc.Result; import util.Attrs; @@ -476,6 +477,9 @@ public CompletionStage updateUserDeclarations(Http.Request httpRequest) httpRequest); } public CompletionStage ownershipTransferUser(Http.Request httpRequest) { + TypedKey environmentKey = TypedKey.create(); + int environment = getEnvironment(); + httpRequest = httpRequest.addAttr(environmentKey, environment); return handleRequest(userOwnershipTransferActor, ActorOperations.USER_OWNERSHIP_TRANSFER.getValue(), httpRequest.body().asJson(), httpRequest); diff --git a/service/src/main/java/org/sunbird/actor/user/UserOwnershipTransferActor.java b/service/src/main/java/org/sunbird/actor/user/UserOwnershipTransferActor.java index 6972145c64..5861061dd1 100644 --- a/service/src/main/java/org/sunbird/actor/user/UserOwnershipTransferActor.java +++ b/service/src/main/java/org/sunbird/actor/user/UserOwnershipTransferActor.java @@ -155,7 +155,7 @@ private Map prepareEventData(Request request, Map context = Map.of( "channel", "01309282781705830427", "pdata", Map.of("id", "org.sunbird.platform", "ver", "1.0"), - "env", "dev" + "env", request.getEnv() ); Map edataBase = Map.of( "action", "ownership-transfer", diff --git a/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java b/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java index a7a8a7bdf7..e3e081e739 100644 --- a/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java +++ b/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java @@ -86,7 +86,7 @@ public void testOwnershipTransferSuccess() { Request request = createTestRequest(); request.setRequestContext(new RequestContext()); subject.tell(request, probe.getRef()); - Response response = probe.expectMsgClass(Duration.ofSeconds(30), Response.class); + Response response = probe.expectMsgClass(Duration.ofSeconds(120), Response.class); Map result = response.getResult(); assertNotNull(result); assertEquals("Ownership transfer process is submitted successfully!", result.get("status")); @@ -100,7 +100,7 @@ public void testInvalidUserDetails() { request.getRequest().remove("fromUser"); request.setRequestContext(new RequestContext()); subject.tell(request, probe.getRef()); - ProjectCommonException errorResponse = probe.expectMsgClass(Duration.ofSeconds(30), ProjectCommonException.class); + ProjectCommonException errorResponse = probe.expectMsgClass(Duration.ofSeconds(120), ProjectCommonException.class); assertEquals("UOS_UOWNTRANS0028", errorResponse.getErrorCode()); assertEquals("fromUser key is not present in the data.", errorResponse.getMessage()); assertEquals(ResponseCode.CLIENT_ERROR.getResponseCode(), errorResponse.getErrorResponseCode()); @@ -114,7 +114,7 @@ public void testInvalidRoleDetails() { ((Map) request.getRequest().get("fromUser")).remove("roles"); request.setRequestContext(new RequestContext()); subject.tell(request, probe.getRef()); - ProjectCommonException errorResponse = probe.expectMsgClass(Duration.ofSeconds(30), + ProjectCommonException errorResponse = probe.expectMsgClass(Duration.ofSeconds(120), ProjectCommonException.class); assertEquals("UOS_UOWNTRANS0028", errorResponse.getErrorCode()); assertEquals("Roles key is not present for fromUser", errorResponse.getMessage()); @@ -130,7 +130,7 @@ public void testInvalidActorDetails() { ((Map) request.getRequest().get("actionBy")).remove("userId"); request.setRequestContext(new RequestContext()); subject.tell(request, probe.getRef()); - ProjectCommonException errorResponse = probe.expectMsgClass(Duration.ofSeconds(30), + ProjectCommonException errorResponse = probe.expectMsgClass(Duration.ofSeconds(120), ProjectCommonException.class); assertEquals("UOS_UOWNTRANS0028", errorResponse.getErrorCode()); assertEquals("User id / user name key is not present in the actionBy", From b541498be5ac5be12961c056022bc84b216e866f Mon Sep 17 00:00:00 2001 From: BharathwajShankar Date: Tue, 2 Jan 2024 14:52:29 +0530 Subject: [PATCH 17/33] Issue #LR-676 feat: User Delete - ownership transfer api - unittestcase --- .../actor/user/UserOwnershipTransferActorTest.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java b/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java index e3e081e739..317b999d00 100644 --- a/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java +++ b/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java @@ -85,8 +85,14 @@ public void testOwnershipTransferSuccess() { ActorRef subject = system.actorOf(props); Request request = createTestRequest(); request.setRequestContext(new RequestContext()); - subject.tell(request, probe.getRef()); - Response response = probe.expectMsgClass(Duration.ofSeconds(120), Response.class); + probe.send(subject, request); + Response response = probe.expectMsgPF(Duration.ofSeconds(30), "Expected response", msg -> { + if (msg instanceof Response) { + return (Response) msg; + } else { + throw new AssertionError("Unexpected message received: " + msg); + } + }); Map result = response.getResult(); assertNotNull(result); assertEquals("Ownership transfer process is submitted successfully!", result.get("status")); From b52f0b8f63df1cfb4e1b87090a385a770d66899c Mon Sep 17 00:00:00 2001 From: BharathwajShankar Date: Tue, 2 Jan 2024 17:03:31 +0530 Subject: [PATCH 18/33] Issue #LR-676 feat: User Delete - ownership transfer api - unittestcase --- .../user/UserOwnershipTransferActorTest.java | 20 ++----------------- 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java b/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java index 317b999d00..4838574900 100644 --- a/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java +++ b/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java @@ -5,7 +5,6 @@ import akka.actor.Props; import akka.dispatch.Futures; import akka.testkit.javadsl.TestKit; -import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; @@ -35,7 +34,6 @@ import java.util.Map; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.powermock.api.mockito.PowerMockito.mockStatic; @@ -69,11 +67,6 @@ public static void setup() { system = ActorSystem.create("system"); } - @AfterClass - public static void tearDown() { - TestKit.shutdownActorSystem(system, true); - } - @Before public void beforeEachTest() { mockStaticDependencies(); @@ -85,17 +78,8 @@ public void testOwnershipTransferSuccess() { ActorRef subject = system.actorOf(props); Request request = createTestRequest(); request.setRequestContext(new RequestContext()); - probe.send(subject, request); - Response response = probe.expectMsgPF(Duration.ofSeconds(30), "Expected response", msg -> { - if (msg instanceof Response) { - return (Response) msg; - } else { - throw new AssertionError("Unexpected message received: " + msg); - } - }); - Map result = response.getResult(); - assertNotNull(result); - assertEquals("Ownership transfer process is submitted successfully!", result.get("status")); + subject.tell(request, probe.getRef()); + probe.expectNoMessage(); } @Test From 4026be01cae9de189912ea99e15c24e203b382b1 Mon Sep 17 00:00:00 2001 From: BharathwajShankar Date: Tue, 2 Jan 2024 17:25:57 +0530 Subject: [PATCH 19/33] Issue #LR-676 feat: User Delete - ownership transfer api - unittestcase --- .../actor/user/UserOwnershipTransferActor.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/service/src/main/java/org/sunbird/actor/user/UserOwnershipTransferActor.java b/service/src/main/java/org/sunbird/actor/user/UserOwnershipTransferActor.java index 5861061dd1..d8ca647eeb 100644 --- a/service/src/main/java/org/sunbird/actor/user/UserOwnershipTransferActor.java +++ b/service/src/main/java/org/sunbird/actor/user/UserOwnershipTransferActor.java @@ -35,7 +35,7 @@ public void onReceive(Request request) throws Throwable { private void handleOwnershipTransfer(Request request) { validateUserDetails(request.getRequest(), request.getRequestContext()); String userId = (String) ((Map) request.getRequest().get("actionBy")).get("userId"); - validateUserRole(userId, request); + validateActionByUserRole(userId, request); List> objects = getObjectsFromRequest(request); objects.forEach(object -> sendInstructionEvent(request, object)); sendResponse("Ownership transfer process is submitted successfully!"); @@ -125,7 +125,7 @@ private boolean userExists(String userId, RequestContext context) { } } - private void validateUserRole(String userId, Request request) { + private void validateActionByUserRole(String userId, Request request) { List> userRoles = userRoleService.getUserRoles(userId, request.getRequestContext()); boolean hasOrgAdminRole = userRoles.stream().anyMatch(role -> JsonKey.ORG_ADMIN.equals(role.get("role"))); if (!hasOrgAdminRole) { @@ -153,7 +153,7 @@ private void sendInstructionEvent(Request request, Map object) { private Map prepareEventData(Request request, Map object) { Map actor = Map.of("id", "ownership-transfer", "type", "System"); Map context = Map.of( - "channel", "01309282781705830427", + "channel", getActionByUserChannel(request), "pdata", Map.of("id", "org.sunbird.platform", "ver", "1.0"), "env", request.getEnv() ); @@ -185,6 +185,13 @@ private Map prepareEventData(Request request, Map actionByUserDetails = userService.getUserDetailsById((String) + ((Map) request.getRequest().get("actionBy")).get("userId"), + request.getRequestContext()); + return String.valueOf(actionByUserDetails.get("channel")); + } + private void sendResponse(String statusMessage) { Response response = new Response(); response.setId("api.user.ownership.transfer"); From 49f71de4215003c567abac5871c99cfc1b382f56 Mon Sep 17 00:00:00 2001 From: BharathwajShankar Date: Wed, 3 Jan 2024 12:24:09 +0530 Subject: [PATCH 20/33] Issue #LR-676 feat: User Delete - ownership transfer api - unittestcase --- .../usermanagement/UserController.java | 3 - .../main/java/org/sunbird/keys/JsonKey.java | 8 +++ .../user/UserOwnershipTransferActor.java | 64 +++++++------------ 3 files changed, 31 insertions(+), 44 deletions(-) diff --git a/controller/app/controllers/usermanagement/UserController.java b/controller/app/controllers/usermanagement/UserController.java index 602a1fece3..49ab4595b9 100644 --- a/controller/app/controllers/usermanagement/UserController.java +++ b/controller/app/controllers/usermanagement/UserController.java @@ -477,9 +477,6 @@ public CompletionStage updateUserDeclarations(Http.Request httpRequest) httpRequest); } public CompletionStage ownershipTransferUser(Http.Request httpRequest) { - TypedKey environmentKey = TypedKey.create(); - int environment = getEnvironment(); - httpRequest = httpRequest.addAttr(environmentKey, environment); return handleRequest(userOwnershipTransferActor, ActorOperations.USER_OWNERSHIP_TRANSFER.getValue(), httpRequest.body().asJson(), httpRequest); diff --git a/core/platform-common/src/main/java/org/sunbird/keys/JsonKey.java b/core/platform-common/src/main/java/org/sunbird/keys/JsonKey.java index 7a07ea0205..0513420ea4 100644 --- a/core/platform-common/src/main/java/org/sunbird/keys/JsonKey.java +++ b/core/platform-common/src/main/java/org/sunbird/keys/JsonKey.java @@ -670,6 +670,14 @@ public final class JsonKey { public static final String USER_TABLE_STATUS = "userTable"; public static final String SUGGESTED_USERS = "suggested_users"; public static final String DELETE_USER_ACTON = "delete-user"; + public static final String ACTION_BY = "actionBy"; + public static final String FROM_USER = "fromUser"; + public static final String TO_USER = "toUser"; + public static final String USER_OWNERSHIP_TRANSFER_ACTION = "ownership-transfer"; + public static final String FROM_USER_PROFILE = "fromUserProfile"; + public static final String TO_USER_PROFILE = "toUserProfile"; + public static final String ASSET_INFORMATION = "assetInformation"; + public static final String USER_TRANSFER_TOPIC = "user_transfer_topic"; public static final String OBJECT = "object"; public static final String EDATA = "edata"; public static final String MANAGED_USERS = "managed_users"; diff --git a/service/src/main/java/org/sunbird/actor/user/UserOwnershipTransferActor.java b/service/src/main/java/org/sunbird/actor/user/UserOwnershipTransferActor.java index d8ca647eeb..382e4a9fdb 100644 --- a/service/src/main/java/org/sunbird/actor/user/UserOwnershipTransferActor.java +++ b/service/src/main/java/org/sunbird/actor/user/UserOwnershipTransferActor.java @@ -23,7 +23,6 @@ public class UserOwnershipTransferActor extends BaseActor { - private static final String USER_TRANSFER_TOPIC = "user_transfer_topic"; private final UserRoleService userRoleService = UserRoleServiceImpl.getInstance(); private final UserService userService = UserServiceImpl.getInstance(); @@ -34,7 +33,8 @@ public void onReceive(Request request) throws Throwable { private void handleOwnershipTransfer(Request request) { validateUserDetails(request.getRequest(), request.getRequestContext()); - String userId = (String) ((Map) request.getRequest().get("actionBy")).get("userId"); + String userId = (String) ((Map) request.getRequest().get(JsonKey.ACTION_BY)) + .get(JsonKey.USER_ID); validateActionByUserRole(userId, request); List> objects = getObjectsFromRequest(request); objects.forEach(object -> sendInstructionEvent(request, object)); @@ -42,9 +42,9 @@ private void handleOwnershipTransfer(Request request) { } private void validateUserDetails(Map data, RequestContext requestContext) { - validateAndProceed(data, "actionBy", requestContext); - validateAndProceed(data, "fromUser", requestContext); - validateAndProceed(data, "toUser", requestContext); + validateAndProceed(data, JsonKey.ACTION_BY, requestContext); + validateAndProceed(data, JsonKey.FROM_USER, requestContext); + validateAndProceed(data, JsonKey.TO_USER, requestContext); } private void validateAndProceed(Map data, String key, RequestContext requestContext) { @@ -58,8 +58,8 @@ private void validateAndProceed(Map data, String key, RequestCon private void validateUser(Object userNode, String userLabel, RequestContext requestContext) { if (userNode instanceof Map) { Map user = (Map) userNode; - String userId = StringUtils.trimToNull(Objects.toString(user.get("userId"), "")); - String userName = StringUtils.trimToNull(Objects.toString(user.get("userName"), "")); + String userId = StringUtils.trimToNull(Objects.toString(user.get(JsonKey.USER_ID), "")); + String userName = StringUtils.trimToNull(Objects.toString(user.get(JsonKey.USERNAME), "")); if (StringUtils.isBlank(StringUtils.trimToNull(userId)) || StringUtils.isBlank(StringUtils.trimToNull(userName))) { @@ -75,7 +75,7 @@ private void validateUser(Object userNode, String userLabel, RequestContext requ } private void validateRoles(Map user, String userLabel) { - if (!userLabel.equals("actionBy") && !user.containsKey(JsonKey.ROLES)) { + if (!userLabel.equals(JsonKey.ACTION_BY) && !user.containsKey(JsonKey.ROLES)) { throwInvalidRequestDataException("Roles key is not present for " + userLabel); } if (user.containsKey(JsonKey.ROLES)) { @@ -127,7 +127,7 @@ private boolean userExists(String userId, RequestContext context) { private void validateActionByUserRole(String userId, Request request) { List> userRoles = userRoleService.getUserRoles(userId, request.getRequestContext()); - boolean hasOrgAdminRole = userRoles.stream().anyMatch(role -> JsonKey.ORG_ADMIN.equals(role.get("role"))); + boolean hasOrgAdminRole = userRoles.stream().anyMatch(role -> JsonKey.ORG_ADMIN.equals(role.get(JsonKey.ROLE))); if (!hasOrgAdminRole) { throw new ProjectCommonException( ResponseCode.cannotTransferOwnership, @@ -144,7 +144,7 @@ private List> getObjectsFromRequest(Request request) { private void sendInstructionEvent(Request request, Map object) { Map data = prepareEventData(request, object); try { - InstructionEventGenerator.pushInstructionEvent(USER_TRANSFER_TOPIC, data); + InstructionEventGenerator.pushInstructionEvent(JsonKey.USER_TRANSFER_TOPIC, data); } catch (Exception e) { logger.error("Error pushing to instruction event", e); } @@ -152,46 +152,28 @@ private void sendInstructionEvent(Request request, Map object) { private Map prepareEventData(Request request, Map object) { Map actor = Map.of("id", "ownership-transfer", "type", "System"); - Map context = Map.of( - "channel", getActionByUserChannel(request), - "pdata", Map.of("id", "org.sunbird.platform", "ver", "1.0"), - "env", request.getEnv() - ); Map edataBase = Map.of( - "action", "ownership-transfer", - "organisationId", request.getRequest().get("organisationId"), - "context", request.getRequest().get("context"), - "actionBy", request.getRequest().get("actionBy"), - "fromUserProfile", request.getRequest().get("fromUser"), - "toUserProfile", request.getRequest().get("toUser") + JsonKey.ACTION, JsonKey.USER_OWNERSHIP_TRANSFER_ACTION, + JsonKey.ORGANISATION_ID, request.getRequest().get(JsonKey.ORGANISATION_ID), + JsonKey.CONTEXT, request.getRequest().get(JsonKey.CONTEXT), + JsonKey.ACTION_BY, request.getRequest().get(JsonKey.ACTION_BY), + JsonKey.FROM_USER_PROFILE, request.getRequest().get(JsonKey.FROM_USER), + JsonKey.TO_USER_PROFILE, request.getRequest().get(JsonKey.TO_USER) ); Map edata = new HashMap<>(edataBase); Map assetInformation = new HashMap<>(object); - edata.put("assetInformation", assetInformation); + edata.put(JsonKey.ASSET_INFORMATION, assetInformation); Map result = new HashMap<>(); - result.put("eid", JsonKey.BE_JOB_REQUEST); - result.put("ets", System.currentTimeMillis()); - result.put("mid", "LP." + System.currentTimeMillis() + "." + UUID.randomUUID()); + Map fromUserDetails = (Map) request.getRequest().get(JsonKey.FROM_USER); + Map objectDetails = Map.of(JsonKey.ID, fromUserDetails.get(JsonKey.USER_ID), JsonKey.TYPE, + JsonKey.USER); result.put("actor", actor); - result.put("context", context); - - // Use 'fromUser' details for 'object' - Map fromUserDetails = (Map) request.getRequest().get("fromUser"); - Map objectDetails = Map.of("id", fromUserDetails.get("userId"), "type", JsonKey.USER); - result.put("object", objectDetails); - result.put("edata", edata); - + result.put(JsonKey.OBJECT, objectDetails); + result.put(JsonKey.EDATA, edata); return result; } - private String getActionByUserChannel(Request request) { - Map actionByUserDetails = userService.getUserDetailsById((String) - ((Map) request.getRequest().get("actionBy")).get("userId"), - request.getRequestContext()); - return String.valueOf(actionByUserDetails.get("channel")); - } - private void sendResponse(String statusMessage) { Response response = new Response(); response.setId("api.user.ownership.transfer"); @@ -202,7 +184,7 @@ private void sendResponse(String statusMessage) { params.setStatus(String.valueOf(ResponseParams.StatusType.SUCCESSFUL)); response.setParams(params); response.setResponseCode(ResponseCode.OK); - Map result = Map.of("status", statusMessage); + Map result = Map.of(JsonKey.STATUS, statusMessage); response.putAll(result); sender().tell(response, self()); } From da1238545dbe9f2966704ee82c030de5159ea834 Mon Sep 17 00:00:00 2001 From: BharathwajShankar Date: Wed, 3 Jan 2024 12:26:41 +0530 Subject: [PATCH 21/33] Issue #LR-676 feat: User Delete - ownership transfer api - unittestcase --- controller/app/controllers/usermanagement/UserController.java | 1 - 1 file changed, 1 deletion(-) diff --git a/controller/app/controllers/usermanagement/UserController.java b/controller/app/controllers/usermanagement/UserController.java index 49ab4595b9..605e4360aa 100644 --- a/controller/app/controllers/usermanagement/UserController.java +++ b/controller/app/controllers/usermanagement/UserController.java @@ -13,7 +13,6 @@ import org.sunbird.request.Request; import org.sunbird.util.ProjectUtil; import org.sunbird.validator.BaseRequestValidator; -import play.libs.typedmap.TypedKey; import play.mvc.Http; import play.mvc.Result; import util.Attrs; From 64c5490b287cb8a8c54769b001e6f05b3a852319 Mon Sep 17 00:00:00 2001 From: BharathwajShankar Date: Mon, 8 Jan 2024 15:52:41 +0530 Subject: [PATCH 22/33] Issue #LR-676 feat: User Delete - ownership transfer api - unittestcase --- .../user/UserOwnershipTransferActor.java | 26 ++++++++++++------- .../user/UserOwnershipTransferActorTest.java | 21 ++++++++++++++- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/service/src/main/java/org/sunbird/actor/user/UserOwnershipTransferActor.java b/service/src/main/java/org/sunbird/actor/user/UserOwnershipTransferActor.java index 382e4a9fdb..a1f5f6eca0 100644 --- a/service/src/main/java/org/sunbird/actor/user/UserOwnershipTransferActor.java +++ b/service/src/main/java/org/sunbird/actor/user/UserOwnershipTransferActor.java @@ -18,6 +18,7 @@ import java.text.MessageFormat; import java.util.*; +import java.util.concurrent.CompletableFuture; import static org.sunbird.validator.orgvalidator.BaseOrgRequestValidator.ERROR_CODE; @@ -37,8 +38,13 @@ private void handleOwnershipTransfer(Request request) { .get(JsonKey.USER_ID); validateActionByUserRole(userId, request); List> objects = getObjectsFromRequest(request); - objects.forEach(object -> sendInstructionEvent(request, object)); - sendResponse("Ownership transfer process is submitted successfully!"); + if (!objects.isEmpty()) { + objects.forEach(object -> sendInstructionEvent(request, object)); + } else { + sendInstructionEvent(request, Collections.emptyMap()); + } + Response response = sendResponse("Ownership transfer process is submitted successfully!"); + sender().tell(response, self()); } private void validateUserDetails(Map data, RequestContext requestContext) { @@ -143,11 +149,13 @@ private List> getObjectsFromRequest(Request request) { private void sendInstructionEvent(Request request, Map object) { Map data = prepareEventData(request, object); - try { - InstructionEventGenerator.pushInstructionEvent(JsonKey.USER_TRANSFER_TOPIC, data); - } catch (Exception e) { - logger.error("Error pushing to instruction event", e); - } + CompletableFuture.runAsync(() -> { + try { + InstructionEventGenerator.pushInstructionEvent(JsonKey.USER_TRANSFER_TOPIC, data); + } catch (Exception e) { + logger.error("Error pushing to instruction event", e); + } + }); } private Map prepareEventData(Request request, Map object) { @@ -174,7 +182,7 @@ private Map prepareEventData(Request request, Map result = Map.of(JsonKey.STATUS, statusMessage); response.putAll(result); - sender().tell(response, self()); + return response; } } diff --git a/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java b/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java index 4838574900..4f25bf5c4e 100644 --- a/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java +++ b/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java @@ -4,6 +4,7 @@ import akka.actor.ActorSystem; import akka.actor.Props; import akka.dispatch.Futures; +import akka.testkit.TestActorRef; import akka.testkit.javadsl.TestKit; import org.junit.Before; import org.junit.BeforeClass; @@ -78,8 +79,26 @@ public void testOwnershipTransferSuccess() { ActorRef subject = system.actorOf(props); Request request = createTestRequest(); request.setRequestContext(new RequestContext()); + String statusMessage = "Ownership transfer process is submitted successfully!"; subject.tell(request, probe.getRef()); - probe.expectNoMessage(); + Response response = probe.expectMsgClass(Duration.ofSeconds(120), Response.class); + assertEquals(statusMessage, response.getResult().get(JsonKey.STATUS)); + } + + @Test + public void testSendResponse() { + new TestKit(system) { + { + Props props = Props.create(UserOwnershipTransferActor.class); + TestActorRef ref = TestActorRef.create(system, props); + UserOwnershipTransferActor actor = ref.underlyingActor(); + String statusMessage = "Ownership transfer process is submitted successfully!"; + Response response = actor.sendResponse(statusMessage); + assertEquals("api.user.ownership.transfer", response.getId()); + assertEquals("1.0", response.getVer()); + assertEquals(statusMessage, response.getResult().get(JsonKey.STATUS)); + } + }; } @Test From 1d566d603d1f09b0020b1aa22f0723a12312a917 Mon Sep 17 00:00:00 2001 From: BharathwajShankar Date: Tue, 9 Jan 2024 11:18:51 +0530 Subject: [PATCH 23/33] Issue #LR-676 feat: User Delete - ownership transfer api - unittestcase --- .../user/UserOwnershipTransferActorTest.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java b/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java index 4f25bf5c4e..4f25a7920c 100644 --- a/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java +++ b/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java @@ -85,6 +85,40 @@ public void testOwnershipTransferSuccess() { assertEquals(statusMessage, response.getResult().get(JsonKey.STATUS)); } + @Test + public void testOwnershipTransferWithEmptyObjectsList() { + TestKit probe = new TestKit(system); + ActorRef subject = system.actorOf(props); + Request request = createTestRequest(); + request.getRequest().put("objects", new ArrayList<>()); + request.setRequestContext(new RequestContext()); + subject.tell(request, probe.getRef()); + Response response = probe.expectMsgClass(Duration.ofSeconds(120), Response.class); + assertEquals("Ownership transfer process is submitted successfully!", response.getResult().get(JsonKey.STATUS)); + } + + @Test + public void testInvalidUser() { + // Arrange + TestKit probe = new TestKit(system); + ActorRef subject = system.actorOf(props); + Request request = createTestRequestWithInvalidUser(); + request.setRequestContext(new RequestContext()); + subject.tell(request, probe.getRef()); + ProjectCommonException errorResponse = probe.expectMsgClass(Duration.ofSeconds(120), ProjectCommonException.class); + assertEquals("UOS_UOWNTRANS0028", errorResponse.getErrorCode()); + assertEquals("User id / user name key is not present in the fromUser", errorResponse.getMessage()); + assertEquals(400, errorResponse.getErrorResponseCode()); + } + + private Request createTestRequestWithInvalidUser() { + Request request = createTestRequest(); + Map invalidUserDetails = new HashMap<>(); + invalidUserDetails.put("userName", "testUserName"); + request.getRequest().put("fromUser", invalidUserDetails); + return request; + } + @Test public void testSendResponse() { new TestKit(system) { From 8100c2d7a7e0bfe8eef8ecd3ec9ea6bcbfbb1fcf Mon Sep 17 00:00:00 2001 From: BharathwajShankar Date: Tue, 9 Jan 2024 12:22:51 +0530 Subject: [PATCH 24/33] Issue #LR-676 feat: User Delete - ownership transfer api - unittestcase --- .../user/UserOwnershipTransferActorTest.java | 52 +++++++++++++++++-- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java b/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java index 4f25a7920c..d65ec90e7c 100644 --- a/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java +++ b/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java @@ -29,10 +29,7 @@ import scala.concurrent.Promise; import java.time.Duration; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.mock; @@ -111,6 +108,7 @@ public void testInvalidUser() { assertEquals(400, errorResponse.getErrorResponseCode()); } + private Request createTestRequestWithInvalidUser() { Request request = createTestRequest(); Map invalidUserDetails = new HashMap<>(); @@ -119,6 +117,7 @@ private Request createTestRequestWithInvalidUser() { return request; } + @Test public void testSendResponse() { new TestKit(system) { @@ -149,6 +148,7 @@ public void testInvalidUserDetails() { assertEquals(ResponseCode.CLIENT_ERROR.getResponseCode(), errorResponse.getErrorResponseCode()); } + @Test public void testInvalidRoleDetails() { TestKit probe = new TestKit(system); @@ -164,6 +164,50 @@ public void testInvalidRoleDetails() { assertEquals(ResponseCode.CLIENT_ERROR.getResponseCode(), errorResponse.getErrorResponseCode()); } + @Test + public void testOwnershipTransferWithInvalidActorRoles() { + TestKit probe = new TestKit(system); + ActorRef subject = system.actorOf(props); + Request request = createTestRequest(); + ((Map) request.getRequest().get(JsonKey.ACTION_BY)).put(JsonKey.ROLES, "INVALID_ROLE"); + request.setRequestContext(new RequestContext()); + subject.tell(request, probe.getRef()); + ProjectCommonException errorResponse = probe.expectMsgClass(Duration.ofSeconds(120), ProjectCommonException.class); + assertEquals("UOS_UOWNTRANS0003", errorResponse.getErrorCode()); + assertEquals("Data type of roles should be List.", errorResponse.getMessage()); + assertEquals(ResponseCode.CLIENT_ERROR.getResponseCode(), errorResponse.getErrorResponseCode()); + } + + @Test + public void testOwnershipTransferWithEmptyRoles() { + TestKit probe = new TestKit(system); + ActorRef subject = system.actorOf(props); + Request request = createTestRequest(); + ((Map) request.getRequest().get(JsonKey.FROM_USER)).put(JsonKey.ROLES, Collections.emptyList()); + request.setRequestContext(new RequestContext()); + subject.tell(request, probe.getRef()); + ProjectCommonException errorResponse = probe.expectMsgClass(Duration.ofSeconds(120), ProjectCommonException.class); + assertEquals("UOS_UOWNTRANS0028", errorResponse.getErrorCode()); + assertEquals("Roles are empty in fromUser details.", errorResponse.getMessage()); + assertEquals(ResponseCode.CLIENT_ERROR.getResponseCode(), errorResponse.getErrorResponseCode()); + } + + @Test + public void testOwnershipTransferWithInvalidUserId() { + TestKit probe = new TestKit(system); + ActorRef subject = system.actorOf(props); + Request request = createTestRequest(); + ((Map) request.getRequest().get(JsonKey.FROM_USER)).put(JsonKey.USER_ID, ""); + request.setRequestContext(new RequestContext()); + subject.tell(request, probe.getRef()); + ProjectCommonException errorResponse = probe.expectMsgClass(Duration.ofSeconds(120), + ProjectCommonException.class); + assertEquals("UOS_UOWNTRANS0028", errorResponse.getErrorCode()); + assertEquals("User id / user name key is not present in the fromUser", errorResponse.getMessage()); + assertEquals(ResponseCode.CLIENT_ERROR.getResponseCode(), errorResponse.getErrorResponseCode()); + } + + @Test public void testInvalidActorDetails() { // Test scenario where "actionBy" details are invalid (e.g., missing "userId" or "userName"). From 25b987037c1052a1fc2b6af6ecf82edb8010c158 Mon Sep 17 00:00:00 2001 From: BharathwajShankar Date: Wed, 10 Jan 2024 11:21:16 +0530 Subject: [PATCH 25/33] Issue #LR-676 feat: User Delete - ownership transfer api - unittestcase --- .../user/UserOwnershipTransferActorTest.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java b/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java index d65ec90e7c..031d2aec51 100644 --- a/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java +++ b/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java @@ -28,6 +28,7 @@ import org.sunbird.service.user.impl.UserServiceImpl; import scala.concurrent.Promise; +import java.text.MessageFormat; import java.time.Duration; import java.util.*; @@ -197,13 +198,13 @@ public void testOwnershipTransferWithInvalidUserId() { TestKit probe = new TestKit(system); ActorRef subject = system.actorOf(props); Request request = createTestRequest(); - ((Map) request.getRequest().get(JsonKey.FROM_USER)).put(JsonKey.USER_ID, ""); + ((Map) request.getRequest().get(JsonKey.FROM_USER)).put(JsonKey.USER_ID, "123"); request.setRequestContext(new RequestContext()); subject.tell(request, probe.getRef()); ProjectCommonException errorResponse = probe.expectMsgClass(Duration.ofSeconds(120), ProjectCommonException.class); - assertEquals("UOS_UOWNTRANS0028", errorResponse.getErrorCode()); - assertEquals("User id / user name key is not present in the fromUser", errorResponse.getMessage()); + assertEquals("UOS_UOWNTRANS0019", errorResponse.getErrorCode()); + assertEquals("Please provide valid userId.", errorResponse.getMessage()); assertEquals(ResponseCode.CLIENT_ERROR.getResponseCode(), errorResponse.getErrorResponseCode()); } @@ -237,7 +238,12 @@ private void mockStaticDependencies() { .thenReturn(getSuccessResponse()); when(cassandraOperation.insertRecord(Mockito.anyString(), Mockito.anyString(), Mockito.anyMap(), Mockito.any())) .thenReturn(getSuccessResponse()); - + PowerMockito.when(userServiceMock.getUserById( + Mockito.argThat(argument -> "123".equals(argument)), Mockito.any(RequestContext.class))) + .thenThrow(new ProjectCommonException( + ResponseCode.resourceNotFound, + MessageFormat.format(ResponseCode.resourceNotFound.getErrorMessage(), JsonKey.USER), + ResponseCode.RESOURCE_NOT_FOUND.getResponseCode())); Response response2 = new Response(); Map user = new HashMap<>(); user.put(JsonKey.ID, "c9e6006e-5811-4337-aa7c-48d0f535e3b8"); @@ -250,7 +256,6 @@ private void mockStaticDependencies() { PowerMockito.when(cassandraOperation.getRecordById( Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any())) .thenReturn(response2); - Response mockResponse = new Response(); List> mockRoles = new ArrayList<>(); Map mockRoleData = new HashMap<>(); From 175eddfa21cff687d607a3786156df4c92b46deb Mon Sep 17 00:00:00 2001 From: BharathwajShankar Date: Wed, 10 Jan 2024 13:06:54 +0530 Subject: [PATCH 26/33] Issue #LR-676 feat: User Delete - ownership transfer api - unittestcase --- .../user/UserOwnershipTransferActorTest.java | 40 +++++++++++++------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java b/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java index 031d2aec51..7a370bc311 100644 --- a/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java +++ b/service/src/test/java/org/sunbird/actor/user/UserOwnershipTransferActorTest.java @@ -24,7 +24,9 @@ import org.sunbird.request.Request; import org.sunbird.request.RequestContext; import org.sunbird.response.Response; +import org.sunbird.service.user.UserRoleService; import org.sunbird.service.user.UserService; +import org.sunbird.service.user.impl.UserRoleServiceImpl; import org.sunbird.service.user.impl.UserServiceImpl; import scala.concurrent.Promise; @@ -33,6 +35,7 @@ import java.util.*; import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.powermock.api.mockito.PowerMockito.mockStatic; @@ -40,7 +43,8 @@ @RunWith(PowerMockRunner.class) @PrepareForTest({ ServiceFactory.class, - UserServiceImpl.class + UserServiceImpl.class, + UserRoleServiceImpl.class }) @PowerMockIgnore({ "javax.management.*", @@ -170,12 +174,12 @@ public void testOwnershipTransferWithInvalidActorRoles() { TestKit probe = new TestKit(system); ActorRef subject = system.actorOf(props); Request request = createTestRequest(); - ((Map) request.getRequest().get(JsonKey.ACTION_BY)).put(JsonKey.ROLES, "INVALID_ROLE"); + ((Map) request.getRequest().get(JsonKey.ACTION_BY)).put(JsonKey.USER_ID, "1234"); request.setRequestContext(new RequestContext()); subject.tell(request, probe.getRef()); ProjectCommonException errorResponse = probe.expectMsgClass(Duration.ofSeconds(120), ProjectCommonException.class); - assertEquals("UOS_UOWNTRANS0003", errorResponse.getErrorCode()); - assertEquals("Data type of roles should be List.", errorResponse.getMessage()); + assertEquals("UOS_UOWNTRANS0084", errorResponse.getErrorCode()); + assertEquals("User is restricted from transfering the ownership based on roles!", errorResponse.getMessage()); assertEquals(ResponseCode.CLIENT_ERROR.getResponseCode(), errorResponse.getErrorResponseCode()); } @@ -229,8 +233,11 @@ public void testInvalidActorDetails() { private void mockStaticDependencies() { mockStatic(ServiceFactory.class); mockStatic(UserServiceImpl.class); + mockStatic(UserRoleServiceImpl.class); UserService userServiceMock = mock(UserService.class); + UserRoleService userRoleServiceMock = mock(UserRoleService.class); when(UserServiceImpl.getInstance()).thenReturn(userServiceMock); + when(UserRoleServiceImpl.getInstance()).thenReturn(userRoleServiceMock); when(ServiceFactory.getInstance()).thenReturn(cassandraOperation); Promise booleanPromise = Futures.promise(); booleanPromise.success(true); @@ -239,7 +246,7 @@ private void mockStaticDependencies() { when(cassandraOperation.insertRecord(Mockito.anyString(), Mockito.anyString(), Mockito.anyMap(), Mockito.any())) .thenReturn(getSuccessResponse()); PowerMockito.when(userServiceMock.getUserById( - Mockito.argThat(argument -> "123".equals(argument)), Mockito.any(RequestContext.class))) + Mockito.argThat("123"::equals), Mockito.any(RequestContext.class))) .thenThrow(new ProjectCommonException( ResponseCode.resourceNotFound, MessageFormat.format(ResponseCode.resourceNotFound.getErrorMessage(), JsonKey.USER), @@ -261,15 +268,24 @@ private void mockStaticDependencies() { Map mockRoleData = new HashMap<>(); mockRoleData.put(JsonKey.ROLE, "ORG_ADMIN"); mockRoles.add(mockRoleData); - mockResponse.getResult().put(JsonKey.RESPONSE, mockRoles); - PowerMockito.when(cassandraOperation.getRecordById( - Mockito.anyString(), - Mockito.anyString(), - Mockito.anyMap(), - Mockito.any(RequestContext.class))) - .thenReturn(mockResponse); + PowerMockito.when(userRoleServiceMock.getUserRoles( + Mockito.anyString(), Mockito.any(RequestContext.class))) + .thenReturn(mockRoles); + PowerMockito.when(userRoleServiceMock.getUserRoles( + eq("1234"), Mockito.any(RequestContext.class))) + .thenReturn(getInvalidRoleResponse()); + } + + private List> getInvalidRoleResponse() { + Map invalidUserRole = new HashMap<>(); + invalidUserRole.put(JsonKey.USER_ID, "1234"); + invalidUserRole.put(JsonKey.ROLE, "INVALID_ROLE"); + List> userRolesList = new ArrayList<>(); + userRolesList.add(invalidUserRole); + return userRolesList; } + private Request createTestRequest() { Request request = new Request(); Map reqMap = new HashMap<>(); From 8eabd9f2d53346700fa2e9279d1f0946d272b83b Mon Sep 17 00:00:00 2001 From: BharathwajShankar Date: Wed, 24 Jan 2024 09:56:16 +0530 Subject: [PATCH 27/33] Issue #LR-676 feat: User Delete - ownership transfer api - unittestcase --- .../user/UserOwnershipTransferActor.java | 38 +++++++++++++++---- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/service/src/main/java/org/sunbird/actor/user/UserOwnershipTransferActor.java b/service/src/main/java/org/sunbird/actor/user/UserOwnershipTransferActor.java index a1f5f6eca0..3d0f06ef9b 100644 --- a/service/src/main/java/org/sunbird/actor/user/UserOwnershipTransferActor.java +++ b/service/src/main/java/org/sunbird/actor/user/UserOwnershipTransferActor.java @@ -55,13 +55,14 @@ private void validateUserDetails(Map data, RequestContext reques private void validateAndProceed(Map data, String key, RequestContext requestContext) { if (data.containsKey(key)) { - validateUser(data.get(key), key, requestContext); + validateUser(data.get(key), key, requestContext, data); } else { throwInvalidRequestDataException(key + " key is not present in the data."); } } - private void validateUser(Object userNode, String userLabel, RequestContext requestContext) { + private void validateUser(Object userNode, String userLabel, RequestContext requestContext, + Map data) { if (userNode instanceof Map) { Map user = (Map) userNode; String userId = StringUtils.trimToNull(Objects.toString(user.get(JsonKey.USER_ID), "")); @@ -73,30 +74,53 @@ private void validateUser(Object userNode, String userLabel, RequestContext requ } if (validUser(userId, requestContext)) { - validateRoles(user, userLabel); + validateAndFilterRoles(user, userLabel, data); } else { throwClientErrorException(); } } } - private void validateRoles(Map user, String userLabel) { + private void validateAndFilterRoles(Map user, String userLabel, Map data) { if (!userLabel.equals(JsonKey.ACTION_BY) && !user.containsKey(JsonKey.ROLES)) { throwInvalidRequestDataException("Roles key is not present for " + userLabel); } + if (user.containsKey(JsonKey.ROLES)) { Object roles = user.get(JsonKey.ROLES); if (roles instanceof List) { - List rolesList = (List) roles; - if (rolesList.isEmpty()) { - throwInvalidRequestDataException("Roles are empty in " + userLabel + " details."); + List filteredRoles = filterRolesByOrganisationId((List) roles, + (String) data.get(JsonKey.ORGANISATION_ID)); + if (filteredRoles.isEmpty()) { + throwInvalidRequestDataException("No roles match the specified organisationId in " + userLabel); } + user.put(JsonKey.ROLES, filteredRoles); } else { throwDataTypeErrorException(); } } } + private List filterRolesByOrganisationId(List roles, String targetOrganisationId) { + List filteredRoles = new ArrayList<>(); + for (Object role : roles) { + if (role instanceof Map) { + Map roleMap = (Map) role; + List> scopeList = (List>) roleMap.get("scope"); + if (scopeList != null && !scopeList.isEmpty()) { + for (Map scope : scopeList) { + String organisationId = (String) scope.get("organisationId"); + if (targetOrganisationId.equals(organisationId)) { + filteredRoles.add((String) roleMap.get("role")); + break; + } + } + } + } + } + return filteredRoles; + } + private void throwInvalidRequestDataException(String message) { throw new ProjectCommonException( ResponseCode.invalidRequestData, From af01e0537aa1ea1020541e89b2036cbbe58b9520 Mon Sep 17 00:00:00 2001 From: BharathwajShankar Date: Wed, 24 Jan 2024 14:52:10 +0530 Subject: [PATCH 28/33] Issue #LR-676 feat: User Delete - ownership transfer api - unittestcase --- .../actor/user/UserOwnershipTransferActor.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/service/src/main/java/org/sunbird/actor/user/UserOwnershipTransferActor.java b/service/src/main/java/org/sunbird/actor/user/UserOwnershipTransferActor.java index 3d0f06ef9b..6edcd891d2 100644 --- a/service/src/main/java/org/sunbird/actor/user/UserOwnershipTransferActor.java +++ b/service/src/main/java/org/sunbird/actor/user/UserOwnershipTransferActor.java @@ -85,16 +85,17 @@ private void validateAndFilterRoles(Map user, String userLabel, if (!userLabel.equals(JsonKey.ACTION_BY) && !user.containsKey(JsonKey.ROLES)) { throwInvalidRequestDataException("Roles key is not present for " + userLabel); } - if (user.containsKey(JsonKey.ROLES)) { Object roles = user.get(JsonKey.ROLES); if (roles instanceof List) { - List filteredRoles = filterRolesByOrganisationId((List) roles, - (String) data.get(JsonKey.ORGANISATION_ID)); - if (filteredRoles.isEmpty()) { - throwInvalidRequestDataException("No roles match the specified organisationId in " + userLabel); + List rolesList = (List) roles; + if (rolesList.isEmpty()) { + throwInvalidRequestDataException("Roles are empty in " + userLabel + " details."); + } else { + List filteredRoles = filterRolesByOrganisationId((List) roles, + (String) data.get(JsonKey.ORGANISATION_ID)); + user.put(JsonKey.ROLES, filteredRoles); } - user.put(JsonKey.ROLES, filteredRoles); } else { throwDataTypeErrorException(); } From 469f27ef54a66cb8a259d3dff6d61079c3fd8597 Mon Sep 17 00:00:00 2001 From: BharathwajShankar Date: Tue, 27 Feb 2024 18:39:41 +0530 Subject: [PATCH 29/33] Issue #LR-676 feat: User Delete - ownership transfer api - unittestcase --- controller/pom.xml | 2 +- core/actor-core/pom.xml | 4 ++-- core/cassandra-utils/pom.xml | 2 +- core/notification-utils/pom.xml | 2 +- core/platform-common/pom.xml | 6 +++--- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/controller/pom.xml b/controller/pom.xml index 479ec52cf2..2b5f09f9f0 100644 --- a/controller/pom.xml +++ b/controller/pom.xml @@ -46,7 +46,7 @@ com.fasterxml.jackson.core jackson-databind - 2.10.5.1 + 2.13.5 diff --git a/core/actor-core/pom.xml b/core/actor-core/pom.xml index e0c8c0b2f8..7e0c07459c 100644 --- a/core/actor-core/pom.xml +++ b/core/actor-core/pom.xml @@ -55,12 +55,12 @@ com.fasterxml.jackson.core jackson-core - 2.10.5 + 2.13.5 com.fasterxml.jackson.core jackson-databind - 2.10.5.1 + 2.13.5 diff --git a/core/cassandra-utils/pom.xml b/core/cassandra-utils/pom.xml index 32564d93cb..7466d49604 100644 --- a/core/cassandra-utils/pom.xml +++ b/core/cassandra-utils/pom.xml @@ -54,7 +54,7 @@ com.fasterxml.jackson.core jackson-databind - 2.10.5.1 + 2.13.5 org.sunbird diff --git a/core/notification-utils/pom.xml b/core/notification-utils/pom.xml index 267e135864..5772f8bc90 100644 --- a/core/notification-utils/pom.xml +++ b/core/notification-utils/pom.xml @@ -53,7 +53,7 @@ com.fasterxml.jackson.core jackson-databind - 2.10.5.1 + 2.13.5 org.sunbird diff --git a/core/platform-common/pom.xml b/core/platform-common/pom.xml index c269a05da9..f20bcb95c1 100644 --- a/core/platform-common/pom.xml +++ b/core/platform-common/pom.xml @@ -52,17 +52,17 @@ com.fasterxml.jackson.core jackson-core - 2.10.5 + 2.13.5 com.fasterxml.jackson.core jackson-databind - 2.10.5.1 + 2.13.5 com.fasterxml.jackson.core jackson-annotations - 2.10.5 + 2.13.5 com.typesafe.akka From 031a2cb60df4f25ff55fa3028171d9922520bdbd Mon Sep 17 00:00:00 2001 From: BharathwajShankar Date: Wed, 20 Mar 2024 11:46:06 +0530 Subject: [PATCH 30/33] LR-676 - added Missing topic related settings --- .../src/main/java/org/sunbird/keys/JsonKey.java | 2 +- .../src/main/resources/externalresource.properties | 2 +- .../org/sunbird/actor/user/UserOwnershipTransferActor.java | 2 +- service/src/test/resources/externalresource.properties | 3 ++- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/core/platform-common/src/main/java/org/sunbird/keys/JsonKey.java b/core/platform-common/src/main/java/org/sunbird/keys/JsonKey.java index 7b505f5c84..f62344b73c 100644 --- a/core/platform-common/src/main/java/org/sunbird/keys/JsonKey.java +++ b/core/platform-common/src/main/java/org/sunbird/keys/JsonKey.java @@ -678,7 +678,7 @@ public final class JsonKey { public static final String FROM_USER_PROFILE = "fromUserProfile"; public static final String TO_USER_PROFILE = "toUserProfile"; public static final String ASSET_INFORMATION = "assetInformation"; - public static final String USER_TRANSFER_TOPIC = "user_transfer_topic"; + public static final String USER_OWNERSHIP_TRANSFER_TOPIC = "user-ownership-transfer-topic"; public static final String OBJECT = "object"; public static final String EDATA = "edata"; public static final String MANAGED_USERS = "managed_users"; diff --git a/core/platform-common/src/main/resources/externalresource.properties b/core/platform-common/src/main/resources/externalresource.properties index 594cc0bc7b..e41475f548 100644 --- a/core/platform-common/src/main/resources/externalresource.properties +++ b/core/platform-common/src/main/resources/externalresource.properties @@ -109,6 +109,6 @@ sunbird_password_reset_login_page_url=/resources isFormValidationRequired=true userProfileConfigMap={\"type\":\"profileconfig\",\"subtype\":\"28\",\"action\":\"get\",\"component\":\"*\",\"framework\":\"*\",\"data\":{\"templateName\":\"profileConfig_v2\",\"action\":\"get\",\"fields\":[{\"code\":\"persona\",\"children\":{\"administrator\":[{\"code\":\"district\"},{\"code\":\"state\"},{\"code\":\"subPersona\",\"type\":\"select\",\"default\":null,\"templateOptions\":{\"options\":[{\"label\":\"Headmaster\",\"value\":\"hm\"},{\"label\":\"Cluster Resource Person\",\"value\":\"crp\"}]}},{\"code\":\"block\"},{\"code\":\"cluster\"},{\"code\":\"school\"}],\"teacher\":[{\"code\":\"state\"},{\"code\":\"district\"},{\"code\":\"block\"},{\"code\":\"cluster\"},{\"code\":\"school\"}],\"student\":[{\"code\":\"state\"},{\"code\":\"district\"},{\"code\":\"block\"},{\"code\":\"cluster\"},{\"code\":\"school\"}],\"parent\":[{\"code\":\"state\"},{\"code\":\"district\"},{\"code\":\"block\"},{\"code\":\"cluster\"},{\"code\":\"school\"}],\"other\":[{\"code\":\"state\"},{\"code\":\"district\"},{\"code\":\"subPersona\",\"templateOptions\":{\"options\":[{\"value\":\"Doctor (Allopathy)\",\"label\":\"Doctor (Allopathy)\"},{\"value\":\"AYUSH Professional\",\"label\":\"AYUSH Professional\"}]}},{\"code\":\"block\"},{\"code\":\"cluster\"},{\"code\":\"school\"}]}}]},\"created_on\":\"2022-02-10T14:16:51.852Z\",\"last_modified_on\":\"2022-11-14T05:45:02.685Z\",\"rootOrgId\":\"*\"} sunbird_userorg_keyspace=sunbird - +user-ownership-transfer-topic={{env_name}}.ownership.transfer user-deletion-roles=public user-deletion-broadcast-topic={{env_name}}.delete.user \ No newline at end of file diff --git a/service/src/main/java/org/sunbird/actor/user/UserOwnershipTransferActor.java b/service/src/main/java/org/sunbird/actor/user/UserOwnershipTransferActor.java index 6edcd891d2..1e03b2250a 100644 --- a/service/src/main/java/org/sunbird/actor/user/UserOwnershipTransferActor.java +++ b/service/src/main/java/org/sunbird/actor/user/UserOwnershipTransferActor.java @@ -176,7 +176,7 @@ private void sendInstructionEvent(Request request, Map object) { Map data = prepareEventData(request, object); CompletableFuture.runAsync(() -> { try { - InstructionEventGenerator.pushInstructionEvent(JsonKey.USER_TRANSFER_TOPIC, data); + InstructionEventGenerator.pushInstructionEvent(JsonKey.USER_OWNERSHIP_TRANSFER_TOPIC, data); } catch (Exception e) { logger.error("Error pushing to instruction event", e); } diff --git a/service/src/test/resources/externalresource.properties b/service/src/test/resources/externalresource.properties index f939819ed3..076057e4af 100644 --- a/service/src/test/resources/externalresource.properties +++ b/service/src/test/resources/externalresource.properties @@ -110,4 +110,5 @@ isFormValidationRequired=true userProfileConfigMap={\"type\":\"profileconfig\",\"subtype\":\"28\",\"action\":\"get\",\"component\":\"*\",\"framework\":\"*\",\"data\":{\"templateName\":\"profileConfig_v2\",\"action\":\"get\",\"fields\":[{\"code\":\"persona\",\"children\":{\"administrator\":[{\"code\":\"district\"},{\"code\":\"state\"},{\"code\":\"subPersona\",\"type\":\"select\",\"default\":null,\"templateOptions\":{\"options\":[{\"label\":\"Headmaster\",\"value\":\"hm\"},{\"label\":\"Cluster Resource Person\",\"value\":\"crp\"}]}},{\"code\":\"block\"},{\"code\":\"cluster\"},{\"code\":\"school\"}],\"teacher\":[{\"code\":\"state\"},{\"code\":\"district\"},{\"code\":\"block\"},{\"code\":\"cluster\"},{\"code\":\"school\"}],\"student\":[{\"code\":\"state\"},{\"code\":\"district\"},{\"code\":\"block\"},{\"code\":\"cluster\"},{\"code\":\"school\"}],\"parent\":[{\"code\":\"state\"},{\"code\":\"district\"},{\"code\":\"block\"},{\"code\":\"cluster\"},{\"code\":\"school\"}],\"other\":[{\"code\":\"state\"},{\"code\":\"district\"},{\"code\":\"subPersona\",\"templateOptions\":{\"options\":[{\"value\":\"Doctor (Allopathy)\",\"label\":\"Doctor (Allopathy)\"},{\"value\":\"AYUSH Professional\",\"label\":\"AYUSH Professional\"}]}},{\"code\":\"block\"},{\"code\":\"cluster\"},{\"code\":\"school\"}]}}]},\"created_on\":\"2022-02-10T14:16:51.852Z\",\"last_modified_on\":\"2022-11-14T05:45:02.685Z\",\"rootOrgId\":\"*\"} sunbird_userorg_keyspace=sunbird sunbird_cloud_service_provider=azure -user-deletion-broadcast-topic=local.delete.user \ No newline at end of file +user-deletion-broadcast-topic=local.delete.user +user-ownership-transfer-topic=local.ownership.transfer From 500c82b116e3dbc2ffc74d9a1106eed35bcf1420 Mon Sep 17 00:00:00 2001 From: BharathwajShankar Date: Wed, 20 Mar 2024 12:23:13 +0530 Subject: [PATCH 31/33] LR-676 - added Missing topic related settings --- .../src/main/resources/externalresource.properties | 2 +- service/src/test/resources/externalresource.properties | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/platform-common/src/main/resources/externalresource.properties b/core/platform-common/src/main/resources/externalresource.properties index e41475f548..a0d0959fb2 100644 --- a/core/platform-common/src/main/resources/externalresource.properties +++ b/core/platform-common/src/main/resources/externalresource.properties @@ -109,6 +109,6 @@ sunbird_password_reset_login_page_url=/resources isFormValidationRequired=true userProfileConfigMap={\"type\":\"profileconfig\",\"subtype\":\"28\",\"action\":\"get\",\"component\":\"*\",\"framework\":\"*\",\"data\":{\"templateName\":\"profileConfig_v2\",\"action\":\"get\",\"fields\":[{\"code\":\"persona\",\"children\":{\"administrator\":[{\"code\":\"district\"},{\"code\":\"state\"},{\"code\":\"subPersona\",\"type\":\"select\",\"default\":null,\"templateOptions\":{\"options\":[{\"label\":\"Headmaster\",\"value\":\"hm\"},{\"label\":\"Cluster Resource Person\",\"value\":\"crp\"}]}},{\"code\":\"block\"},{\"code\":\"cluster\"},{\"code\":\"school\"}],\"teacher\":[{\"code\":\"state\"},{\"code\":\"district\"},{\"code\":\"block\"},{\"code\":\"cluster\"},{\"code\":\"school\"}],\"student\":[{\"code\":\"state\"},{\"code\":\"district\"},{\"code\":\"block\"},{\"code\":\"cluster\"},{\"code\":\"school\"}],\"parent\":[{\"code\":\"state\"},{\"code\":\"district\"},{\"code\":\"block\"},{\"code\":\"cluster\"},{\"code\":\"school\"}],\"other\":[{\"code\":\"state\"},{\"code\":\"district\"},{\"code\":\"subPersona\",\"templateOptions\":{\"options\":[{\"value\":\"Doctor (Allopathy)\",\"label\":\"Doctor (Allopathy)\"},{\"value\":\"AYUSH Professional\",\"label\":\"AYUSH Professional\"}]}},{\"code\":\"block\"},{\"code\":\"cluster\"},{\"code\":\"school\"}]}}]},\"created_on\":\"2022-02-10T14:16:51.852Z\",\"last_modified_on\":\"2022-11-14T05:45:02.685Z\",\"rootOrgId\":\"*\"} sunbird_userorg_keyspace=sunbird -user-ownership-transfer-topic={{env_name}}.ownership.transfer +user-ownership-transfer-topic={{env_name}}.user.ownership.transfer user-deletion-roles=public user-deletion-broadcast-topic={{env_name}}.delete.user \ No newline at end of file diff --git a/service/src/test/resources/externalresource.properties b/service/src/test/resources/externalresource.properties index 076057e4af..ac392043e0 100644 --- a/service/src/test/resources/externalresource.properties +++ b/service/src/test/resources/externalresource.properties @@ -111,4 +111,4 @@ userProfileConfigMap={\"type\":\"profileconfig\",\"subtype\":\"28\",\"action\":\ sunbird_userorg_keyspace=sunbird sunbird_cloud_service_provider=azure user-deletion-broadcast-topic=local.delete.user -user-ownership-transfer-topic=local.ownership.transfer +user-ownership-transfer-topic=local.user.ownership.transfer From 30bc54bd4237d4972637855373752c1004ce7251 Mon Sep 17 00:00:00 2001 From: BharathwajShankar Date: Mon, 1 Apr 2024 12:24:44 +0530 Subject: [PATCH 32/33] LR-676 - added Missing topic related settings and topic info from cache --- .../org/sunbird/actor/user/UserOwnershipTransferActor.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/service/src/main/java/org/sunbird/actor/user/UserOwnershipTransferActor.java b/service/src/main/java/org/sunbird/actor/user/UserOwnershipTransferActor.java index 1e03b2250a..93facffd2d 100644 --- a/service/src/main/java/org/sunbird/actor/user/UserOwnershipTransferActor.java +++ b/service/src/main/java/org/sunbird/actor/user/UserOwnershipTransferActor.java @@ -15,6 +15,7 @@ import org.sunbird.service.user.impl.UserRoleServiceImpl; import org.sunbird.service.user.impl.UserServiceImpl; import org.sunbird.util.ProjectUtil; +import org.sunbird.util.PropertiesCache; import java.text.MessageFormat; import java.util.*; @@ -176,7 +177,8 @@ private void sendInstructionEvent(Request request, Map object) { Map data = prepareEventData(request, object); CompletableFuture.runAsync(() -> { try { - InstructionEventGenerator.pushInstructionEvent(JsonKey.USER_OWNERSHIP_TRANSFER_TOPIC, data); + PropertiesCache propertiesCache = PropertiesCache.getInstance(); + InstructionEventGenerator.pushInstructionEvent(propertiesCache.getProperty(JsonKey.USER_OWNERSHIP_TRANSFER_TOPIC), data); } catch (Exception e) { logger.error("Error pushing to instruction event", e); } From 0cf1af87074d1a7b1511c45c7aebeac8b387c2d8 Mon Sep 17 00:00:00 2001 From: BharathwajShankar Date: Mon, 8 Apr 2024 17:31:56 +0530 Subject: [PATCH 33/33] LR-676 - added Missing topic related settings and topic info from cache --- .../sunbird/actor/user/UserOwnershipTransferActor.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/service/src/main/java/org/sunbird/actor/user/UserOwnershipTransferActor.java b/service/src/main/java/org/sunbird/actor/user/UserOwnershipTransferActor.java index 93facffd2d..686abebda7 100644 --- a/service/src/main/java/org/sunbird/actor/user/UserOwnershipTransferActor.java +++ b/service/src/main/java/org/sunbird/actor/user/UserOwnershipTransferActor.java @@ -34,6 +34,7 @@ public void onReceive(Request request) throws Throwable { } private void handleOwnershipTransfer(Request request) { + validateOrganizationId(request.getRequest()); validateUserDetails(request.getRequest(), request.getRequestContext()); String userId = (String) ((Map) request.getRequest().get(JsonKey.ACTION_BY)) .get(JsonKey.USER_ID); @@ -48,6 +49,13 @@ private void handleOwnershipTransfer(Request request) { sender().tell(response, self()); } + private void validateOrganizationId(Map requestData) { + if (!requestData.containsKey(JsonKey.ORGANISATION_ID) || + StringUtils.isBlank((String) requestData.get(JsonKey.ORGANISATION_ID))) { + throwInvalidRequestDataException("Organization ID is mandatory in the request."); + } + } + private void validateUserDetails(Map data, RequestContext requestContext) { validateAndProceed(data, JsonKey.ACTION_BY, requestContext); validateAndProceed(data, JsonKey.FROM_USER, requestContext);