diff --git a/controller/app/controllers/scheduler/SchedulerController.java b/controller/app/controllers/scheduler/SchedulerController.java deleted file mode 100644 index a45dff3bb08576028cb4c62e9002657b074d5a85..0000000000000000000000000000000000000000 --- a/controller/app/controllers/scheduler/SchedulerController.java +++ /dev/null @@ -1,20 +0,0 @@ -package controllers.scheduler; - -import controllers.BaseController; -import java.util.concurrent.CompletionStage; -import org.sunbird.operations.ActorOperations; -import play.mvc.Http; -import play.mvc.Result; - -public class SchedulerController extends BaseController { - public CompletionStage<Result> startScheduler(Http.Request httpRequest) { - return handleRequest( - ActorOperations.ONDEMAND_START_SCHEDULER.getValue(), - httpRequest.body().asJson(), - null, - null, - null, - true, - httpRequest); - } -} diff --git a/controller/app/modules/ApplicationStart.java b/controller/app/modules/ApplicationStart.java index 7e3eabbee9fa27e2595954a3ec82c17f3fc5ead5..ebf6f7d7f92bffa14451ea2c007cd38d31dc41b6 100644 --- a/controller/app/modules/ApplicationStart.java +++ b/controller/app/modules/ApplicationStart.java @@ -27,9 +27,7 @@ public class ApplicationStart { // initialize HttpClientUtil class HttpClientUtil.getInstance(); applicationLifecycle.addStopHook( - () -> { - return CompletableFuture.completedFuture(null); - }); + () -> CompletableFuture.completedFuture(null)); KeyManager.init(); } @@ -46,8 +44,5 @@ public class ApplicationStart { private static void checkCassandraConnections() { Util.checkCassandraDbConnections(); SchedulerManager.schedule(); - // Run quartz scheduler in a separate thread as it waits for 4 minutes - // before scheduling various jobs. - new Thread(() -> org.sunbird.quartzscheduler.SchedulerManager.getInstance()).start(); } } diff --git a/controller/test/controllers/scheduler/SchedulerControllerTest.java b/controller/test/controllers/scheduler/SchedulerControllerTest.java deleted file mode 100644 index c397fbc1d0cb1d83fd18383cf7723ee260f893a1..0000000000000000000000000000000000000000 --- a/controller/test/controllers/scheduler/SchedulerControllerTest.java +++ /dev/null @@ -1,91 +0,0 @@ -package controllers.scheduler; - -import static org.junit.Assert.*; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import controllers.BaseApplicationTest; -import controllers.DummyActor; -import java.io.IOException; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import modules.OnRequestHandler; -import org.apache.commons.lang3.StringUtils; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; -import org.powermock.core.classloader.annotations.PowerMockIgnore; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.sunbird.exception.ResponseCode; -import org.sunbird.keys.JsonKey; -import org.sunbird.request.HeaderParam; -import play.libs.Json; -import play.mvc.Http; -import play.mvc.Result; -import play.test.Helpers; - -@Ignore -@PrepareForTest(OnRequestHandler.class) -@PowerMockIgnore({"javax.management.*", "jdk.internal.reflect.*", "javax.crypto.*"}) -public class SchedulerControllerTest extends BaseApplicationTest { - - public static Map<String, List<String>> headerMap; - - @Before - public void before() { - setup(DummyActor.class); - headerMap = new HashMap<>(); - headerMap.put(HeaderParam.X_Consumer_ID.getName(), Arrays.asList("Some consumer ID")); - headerMap.put(HeaderParam.X_Device_ID.getName(), Arrays.asList("Some device ID")); - headerMap.put( - HeaderParam.X_Authenticated_Userid.getName(), Arrays.asList("Some authenticated user ID")); - headerMap.put(JsonKey.MESSAGE_ID, Arrays.asList("Some message ID")); - headerMap.put(HeaderParam.X_APP_ID.getName(), Arrays.asList("Some app Id")); - } - - @Test - public void testStartScheduler() { - System.out.println("applied"); - Result result = - performTest("/private/user/v1/scheduler", "POST", (Map) createOnDemandSchedulerRequest()); - assertEquals(getResponseCode(result), ResponseCode.success.getErrorCode().toLowerCase()); - } - - public Result performTest(String url, String method, Map map) { - String data = mapToJson(map); - Http.RequestBuilder req; - if (StringUtils.isNotBlank(data)) { - JsonNode json = Json.parse(data); - req = new Http.RequestBuilder().bodyJson(json).uri(url).method(method); - } else { - req = new Http.RequestBuilder().uri(url).method(method); - } - Result result = Helpers.route(application, req); - return result; - } - - public String mapToJson(Map map) { - ObjectMapper mapperObj = new ObjectMapper(); - String jsonResp = ""; - - if (map != null) { - try { - jsonResp = mapperObj.writeValueAsString(map); - } catch (IOException e) { - e.printStackTrace(); - } - } - return jsonResp; - } - - private Object createOnDemandSchedulerRequest() { - Map<String, Object> requestMap = new HashMap<>(); - - Map<String, Object> innerMap = new HashMap<>(); - innerMap.put(JsonKey.TYPE, "shadowuser"); - requestMap.put(JsonKey.REQUEST, innerMap); - return requestMap; - } -} 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 b215dcd63de413d9709c2fe3042a75c1582d7d37..2c308f07bbdcc4c5df70f8ef88ebc719a8930c0b 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 @@ -35,7 +35,6 @@ public enum ActorOperations { UPDATE_USER_ROLES_ES("updateUserRoles"), SYNC("sync"), BACKGROUND_SYNC("backgroundSync"), - SCHEDULE_BULK_UPLOAD("scheduleBulkUpload"), EMAIL_SERVICE("emailService"), FILE_STORAGE_SERVICE("fileStorageService"), HEALTH_CHECK("healthCheck"), @@ -84,7 +83,6 @@ public enum ActorOperations { CREATE_SSU_USER("createSSUUser"), CREATE_USER_V4("createUserV4"), CREATE_MANAGED_USER("createManagedUser"), - ONDEMAND_START_SCHEDULER("onDemandStartScheduler"), V2_NOTIFICATION("v2Notification"), GET_MANAGED_USERS("getManagedUsers"), CHECK_USER_EXISTENCEV2("checkUserExistenceV2"), diff --git a/core/platform-common/src/test/java/org/sunbird/operations/ActorOperationTest.java b/core/platform-common/src/test/java/org/sunbird/operations/ActorOperationTest.java index 255dc2fe068fac07a4b4720ed1f1245e7fe00181..d2ef4d36ba1348cab6356dd1edee5f2275ec5091 100644 --- a/core/platform-common/src/test/java/org/sunbird/operations/ActorOperationTest.java +++ b/core/platform-common/src/test/java/org/sunbird/operations/ActorOperationTest.java @@ -39,7 +39,6 @@ public class ActorOperationTest { Assert.assertEquals("updateUserOrgES", ActorOperations.UPDATE_USER_ORG_ES.getValue()); Assert.assertEquals("updateUserRoles", ActorOperations.UPDATE_USER_ROLES_ES.getValue()); Assert.assertEquals("sync", ActorOperations.SYNC.getValue()); - Assert.assertEquals("scheduleBulkUpload", ActorOperations.SCHEDULE_BULK_UPLOAD.getValue()); Assert.assertEquals("fileStorageService", ActorOperations.FILE_STORAGE_SERVICE.getValue()); Assert.assertEquals("healthCheck", ActorOperations.HEALTH_CHECK.getValue()); Assert.assertEquals("sendMail", ActorOperations.SEND_MAIL.getValue()); diff --git a/service/pom.xml b/service/pom.xml index 67f78a51ff3ecde6789ca193bd3c7c13b2fd25e6..8cc9180f2111a0f49c6156416efac5027dfb194a 100644 --- a/service/pom.xml +++ b/service/pom.xml @@ -73,18 +73,6 @@ <version>4.1</version> </dependency> - <!-- adding dependency for quartz scheduler job --> - <dependency> - <groupId>org.quartz-scheduler</groupId> - <artifactId>quartz</artifactId> - <version>2.2.1</version> - </dependency> - <!-- this postgres driver is used for making connection to postgres by quartz scheduler--> - <dependency> - <groupId>org.postgresql</groupId> - <artifactId>postgresql</artifactId> - <version>42.1.4</version> - </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> diff --git a/service/src/main/java/org/sunbird/actor/BackgroundOperations.java b/service/src/main/java/org/sunbird/actor/BackgroundOperations.java index 90ee5c1039a878d69e2351bc47b0e6eae8b8e4ca..7fcc6d74c88fc24b98bad0a8ccfc202be6164797 100644 --- a/service/src/main/java/org/sunbird/actor/BackgroundOperations.java +++ b/service/src/main/java/org/sunbird/actor/BackgroundOperations.java @@ -2,6 +2,5 @@ package org.sunbird.actor; /** @author Mahesh Kumar Gangula */ public enum BackgroundOperations { - registerChannel, emailService; } diff --git a/service/src/main/java/org/sunbird/actor/ChannelRegistrationActor.java b/service/src/main/java/org/sunbird/actor/ChannelRegistrationActor.java deleted file mode 100644 index 079c18392e363bd869d850dc5e3da57c0b1cdd84..0000000000000000000000000000000000000000 --- a/service/src/main/java/org/sunbird/actor/ChannelRegistrationActor.java +++ /dev/null @@ -1,170 +0,0 @@ -package org.sunbird.actor; - -import com.fasterxml.jackson.databind.ObjectMapper; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import org.apache.commons.lang3.StringUtils; -import org.json.JSONArray; -import org.json.JSONObject; -import org.sunbird.actor.core.BaseActor; -import org.sunbird.actor.router.ActorConfig; -import org.sunbird.cassandra.CassandraOperation; -import org.sunbird.common.ElasticSearchHelper; -import org.sunbird.common.factory.EsClientFactory; -import org.sunbird.common.inf.ElasticSearchService; -import org.sunbird.dto.SearchDTO; -import org.sunbird.helper.ServiceFactory; -import org.sunbird.http.HttpClientUtil; -import org.sunbird.keys.JsonKey; -import org.sunbird.util.Util; -import org.sunbird.request.Request; -import org.sunbird.request.RequestContext; -import org.sunbird.response.Response; -import org.sunbird.util.ProjectUtil; -import org.sunbird.util.PropertiesCache; -import scala.concurrent.Future; - -/** @author Amit Kumar */ -@ActorConfig( - tasks = {}, - asyncTasks = {"registerChannel"} -) -public class ChannelRegistrationActor extends BaseActor { - - private CassandraOperation cassandraOperation = ServiceFactory.getInstance(); - private ElasticSearchService esService = EsClientFactory.getInstance(JsonKey.REST); - - @Override - public void onReceive(Request request) throws Throwable { - if (request.getOperation().equalsIgnoreCase(BackgroundOperations.registerChannel.name())) { - registerChannel(request); - } else { - onReceiveUnsupportedOperation(request.getOperation()); - } - } - - private void registerChannel(Request request) { - List<String> ekstepChannelList = getEkstepChannelList(request.getRequestContext()); - List<Map<String, Object>> sunbirdChannelList = null; - if (null != ekstepChannelList) { - logger.info( - request.getRequestContext(), - "channel list size from ekstep : " + ekstepChannelList.size()); - sunbirdChannelList = getSunbirdChannelList(request.getRequestContext()); - logger.info( - request.getRequestContext(), - "channel list size from sunbird : " + sunbirdChannelList.size()); - if (!ekstepChannelList.isEmpty()) { - processChannelReg(ekstepChannelList, sunbirdChannelList, request.getRequestContext()); - } - } - } - - private void processChannelReg( - List<String> ekstepChannelList, - List<Map<String, Object>> sunbirdChannelList, - RequestContext context) { - Boolean bool = true; - for (Map<String, Object> map : sunbirdChannelList) { - logger.info(context, "processing start for hashTagId " + map.get(JsonKey.ID)); - if (!StringUtils.isBlank((String) map.get(JsonKey.ID)) - && (!ekstepChannelList.contains(map.get(JsonKey.ID))) - && (!Util.registerChannel(map, context))) { - bool = false; - } - } - if (bool) { - updateSystemSettingTable(bool, context); - } - } - - private void updateSystemSettingTable(Boolean bool, RequestContext context) { - Map<String, Object> map = new HashMap<>(); - map.put(JsonKey.ID, JsonKey.CHANNEL_REG_STATUS_ID); - map.put(JsonKey.FIELD, JsonKey.CHANNEL_REG_STATUS); - map.put(JsonKey.VALUE, String.valueOf(bool)); - Response response = - cassandraOperation.upsertRecord("sunbird", JsonKey.SYSTEM_SETTINGS_DB, map, context); - logger.info( - context, - "Upsert operation result for channel reg status = " - + response.getResult().get(JsonKey.RESPONSE)); - } - - private List<Map<String, Object>> getSunbirdChannelList(RequestContext context) { - logger.info(context, "start call for getting List of channel from sunbird ES"); - SearchDTO searchDto = new SearchDTO(); - List<String> list = new ArrayList<>(); - list.add(JsonKey.ID); - list.add(JsonKey.DESCRIPTION); - list.add(JsonKey.CHANNEL); - searchDto.setFields(list); - Map<String, Object> filter = new HashMap<>(); - filter.put(JsonKey.IS_TENANT, true); - searchDto.getAdditionalProperties().put(JsonKey.FILTERS, filter); - Future<Map<String, Object>> esResponseF = - esService.search(searchDto, ProjectUtil.EsType.organisation.getTypeName(), context); - Map<String, Object> esResponse = - (Map<String, Object>) ElasticSearchHelper.getResponseFromFuture(esResponseF); - List<Map<String, Object>> orgList = (List<Map<String, Object>>) esResponse.get(JsonKey.CONTENT); - logger.info(context, "End call for getting List of channel from sunbird ES"); - return orgList; - } - - private List<String> getEkstepChannelList(RequestContext context) { - List<String> channelList = new ArrayList<>(); - Map<String, String> headerMap = new HashMap<>(); - String header = System.getenv(JsonKey.EKSTEP_AUTHORIZATION); - if (StringUtils.isBlank(header)) { - header = PropertiesCache.getInstance().getProperty(JsonKey.EKSTEP_AUTHORIZATION); - } else { - header = JsonKey.BEARER + header; - } - headerMap.put(JsonKey.AUTHORIZATION, header); - headerMap.put("Content-Type", "application/json"); - headerMap.put("user-id", ""); - String reqString = ""; - String response = ""; - JSONObject data; - JSONObject jObject; - Object[] result = null; - try { - logger.info(context, "start call for getting List of channel from Ekstep"); - String ekStepBaseUrl = System.getenv(JsonKey.EKSTEP_BASE_URL); - if (StringUtils.isBlank(ekStepBaseUrl)) { - ekStepBaseUrl = PropertiesCache.getInstance().getProperty(JsonKey.EKSTEP_BASE_URL); - } - Map<String, Object> map = new HashMap<>(); - Map<String, Object> reqMap = new HashMap<>(); - map.put(JsonKey.REQUEST, reqMap); - - ObjectMapper mapper = new ObjectMapper(); - reqString = mapper.writeValueAsString(map); - response = - HttpClientUtil.post( - (ekStepBaseUrl - + PropertiesCache.getInstance().getProperty(JsonKey.EKSTEP_GET_CHANNEL_LIST)), - reqString, - headerMap); - jObject = new JSONObject(response); - data = jObject.getJSONObject(JsonKey.RESULT); - logger.info( - context, - "Total number of content fetched from Ekstep while getting List of channel : " - + data.get("count")); - JSONArray contentArray = data.getJSONArray(JsonKey.CHANNELS); - result = mapper.readValue(contentArray.toString(), Object[].class); - for (Object object : result) { - Map<String, Object> tempMap = (Map<String, Object>) object; - channelList.add((String) tempMap.get(JsonKey.CODE)); - } - logger.info(context, "end call for getting List of channel from Ekstep"); - } catch (Exception e) { - logger.error(context, e.getMessage(), e); - channelList = null; - } - return channelList; - } -} diff --git a/service/src/main/java/org/sunbird/actor/bulkupload/DeclaredExternalIdActor.java b/service/src/main/java/org/sunbird/actor/bulkupload/DeclaredExternalIdActor.java deleted file mode 100644 index cf44a1951381b06a501edf382dd49845a867e37f..0000000000000000000000000000000000000000 --- a/service/src/main/java/org/sunbird/actor/bulkupload/DeclaredExternalIdActor.java +++ /dev/null @@ -1,191 +0,0 @@ -package org.sunbird.actor.bulkupload; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import org.apache.commons.lang3.StringUtils; -import org.sunbird.actor.core.BaseActor; -import org.sunbird.actor.router.ActorConfig; -import org.sunbird.client.org.OrganisationClient; -import org.sunbird.client.org.impl.OrganisationClientImpl; -import org.sunbird.exception.ResponseCode; -import org.sunbird.keys.JsonKey; -import org.sunbird.model.SelfDeclaredUser; -import org.sunbird.model.bulkupload.BulkMigrationUser; -import org.sunbird.model.bulkupload.SelfDeclaredErrorTypeEnum; -import org.sunbird.model.bulkupload.SelfDeclaredStatusEnum; -import org.sunbird.model.user.UserDeclareEntity; -import org.sunbird.models.organisation.Organisation; -import org.sunbird.operations.ActorOperations; -import org.sunbird.operations.BulkUploadActorOperation; -import org.sunbird.request.Request; -import org.sunbird.request.RequestContext; -import org.sunbird.response.Response; -import org.sunbird.util.ProjectUtil; -import org.sunbird.util.bulkupload.UserUploadUtil; - -@ActorConfig( - tasks = {}, - asyncTasks = {"processUserBulkSelfDeclared"} -) -public class DeclaredExternalIdActor extends BaseActor { - - @Override - public void onReceive(Request request) throws Throwable { - String operation = request.getOperation(); - if (operation.equalsIgnoreCase( - BulkUploadActorOperation.PROCESS_USER_BULK_SELF_DECLARED.getValue())) { - processSelfDeclaredExternalId(request); - } else { - onReceiveUnsupportedOperation("userBulkMigration"); - } - } - - private void processSelfDeclaredExternalId(Request request) { - Response response = new Response(); - response.setResponseCode(ResponseCode.OK); - Map requestMap = request.getRequest(); - String processId = (String) requestMap.get(JsonKey.PROCESS_ID); - String rootOrgId = (String) requestMap.get(JsonKey.ROOT_ORG_ID); - Map<String, Object> row = - UserUploadUtil.getFullRecordFromProcessId(processId, request.getRequestContext()); - BulkMigrationUser bulkMigrationUser = - UserUploadUtil.convertRowToObject(row, request.getRequestContext()); - List<SelfDeclaredUser> userList = - UserUploadUtil.getMigrationUserAsList(bulkMigrationUser, request.getRequestContext()); - userList - .parallelStream() - .forEach( - migrateUser -> { - // add entry in usr_external_id - // modify status to validated to user_declarations - // call to migrate api - migrateUser.setOrgId(rootOrgId); - if (migrateUser.getPersona().equals(JsonKey.TEACHER_PERSONA)) { - switch (migrateUser.getInputStatus()) { - case JsonKey.VALIDATED: - migrateDeclaredUser(request, migrateUser); - break; - case JsonKey.REJECTED: - rejectDeclaredDetail(request, migrateUser); - break; - case JsonKey.SELF_DECLARED_ERROR: - updateErrorDetail(request, migrateUser); - break; - default: - } - } - }); - UserUploadUtil.updateStatusInUserBulkTable( - bulkMigrationUser.getId(), - ProjectUtil.BulkProcessStatus.COMPLETED.getValue(), - request.getRequestContext()); - logger.info( - request.getRequestContext(), - "DeclaredExternalIdActor:processSelfDeclaredExternalId: processing the DeclaredUser of processId: " - + bulkMigrationUser.getId() - + "is completed"); - sender().tell(response, self()); - } - - private void updateErrorDetail(Request request, SelfDeclaredUser declaredUser) { - - Request req = new Request(); - try { - req.setRequestContext(request.getRequestContext()); - req.setOperation("updateUserSelfDeclarationsErrorType"); - Map<String, Object> requestMap = new HashMap(); - UserDeclareEntity userDeclareEntity = new UserDeclareEntity(); - userDeclareEntity.setOrgId(declaredUser.getOrgId()); - userDeclareEntity.setPersona(declaredUser.getPersona()); - userDeclareEntity.setUserId(declaredUser.getUserId()); - userDeclareEntity.setErrorType(declaredUser.getErrorType()); - userDeclareEntity.setStatus(declaredUser.getInputStatus()); - requestMap.put(JsonKey.DECLARATIONS, userDeclareEntity); - req.setRequest(requestMap); - tellToAnother(req); - } catch (Exception e) { - logger.error( - req.getRequestContext(), - "DeclaredExternalIdActor:updateErrorDetail:Exception in processing the DeclaredUser: " - + e.getCause() - + declaredUser.getUserId(), - e); - } - } - - private void rejectDeclaredDetail(Request request, SelfDeclaredUser declaredUser) { - Request req = new Request(); - try { - req.setRequestContext(request.getRequestContext()); - req.setOperation("upsertUserSelfDeclarations"); - Map<String, Object> requestMap = new HashMap(); - UserDeclareEntity userDeclareEntity = new UserDeclareEntity(); - userDeclareEntity.setOrgId(declaredUser.getOrgId()); - userDeclareEntity.setPersona(declaredUser.getPersona()); - userDeclareEntity.setUserId(declaredUser.getUserId()); - userDeclareEntity.setOperation(JsonKey.REMOVE); - List userDeclareEntityLst = new ArrayList<UserDeclareEntity>(); - userDeclareEntityLst.add(userDeclareEntity); - requestMap.put(JsonKey.DECLARATIONS, userDeclareEntityLst); - req.setRequest(requestMap); - tellToAnother(req); - } catch (Exception e) { - logger.error( - req.getRequestContext(), - "DeclaredExternalIdActor:rejectDeclaredDetail:Exception in processing the DeclaredUser: " - + e.getCause() - + declaredUser.getUserId(), - e); - } - } - - private void migrateDeclaredUser(Request request, SelfDeclaredUser declaredUser) { - Request req = new Request(); - try { - req.setRequestContext(request.getRequestContext()); - req.setOperation(ActorOperations.USER_SELF_DECLARED_TENANT_MIGRATE.getValue()); - if (StringUtils.isNotEmpty(declaredUser.getSubOrgExternalId())) { - Organisation org = - getOrgDetails( - declaredUser.getSubOrgExternalId(), - declaredUser.getChannel(), - req.getRequestContext()); - if (org == null || (org != null && !org.getRootOrgId().equals(declaredUser.getOrgId()))) { - declaredUser.setErrorType( - SelfDeclaredErrorTypeEnum.ERROR_STATE.getErrorType().replace("_", "-")); - declaredUser.setInputStatus(SelfDeclaredStatusEnum.ERROR.name()); - updateErrorDetail(req, declaredUser); - return; - } - } - Map<String, Object> requestMap = new HashMap(); - Map<String, String> externalIdMap = new HashMap(); - List<Map<String, String>> externalIdLst = new ArrayList(); - requestMap.put(JsonKey.USER_ID, declaredUser.getUserId()); - requestMap.put(JsonKey.CHANNEL, declaredUser.getChannel()); - requestMap.put(JsonKey.ORG_EXTERNAL_ID, declaredUser.getSubOrgExternalId()); - externalIdMap.put(JsonKey.ID, declaredUser.getUserExternalId()); - externalIdMap.put(JsonKey.ID_TYPE, declaredUser.getChannel()); - externalIdMap.put(JsonKey.PROVIDER, declaredUser.getChannel()); - externalIdLst.add(externalIdMap); - requestMap.put(JsonKey.EXTERNAL_IDS, externalIdLst); - req.setRequest(requestMap); - tellToAnother(req); - } catch (Exception e) { - logger.error( - req.getRequestContext(), - "DeclaredExternalIdActor:migrateDeclaredUser:Exception in processing the DeclaredUser: " - + e.getCause() - + declaredUser.getUserId(), - e); - } - } - - private Organisation getOrgDetails( - String orgExternalId, String provider, RequestContext context) { - OrganisationClient organisationClient = OrganisationClientImpl.getInstance(); - return organisationClient.esGetOrgByExternalId(orgExternalId, provider, context); - } -} diff --git a/service/src/main/java/org/sunbird/actor/bulkupload/UserBulkMigrationActor.java b/service/src/main/java/org/sunbird/actor/bulkupload/UserBulkMigrationActor.java deleted file mode 100644 index 729213c9dbda58ffc55e5fbf70104a09e3ef42d7..0000000000000000000000000000000000000000 --- a/service/src/main/java/org/sunbird/actor/bulkupload/UserBulkMigrationActor.java +++ /dev/null @@ -1,660 +0,0 @@ -package org.sunbird.actor.bulkupload; - -import com.fasterxml.jackson.databind.ObjectMapper; -import com.google.common.collect.Iterables; -import com.mchange.v1.util.ArrayUtils; -import com.opencsv.CSVReader; -import java.io.IOException; -import java.sql.Timestamp; -import java.util.*; -import org.apache.commons.collections.MapUtils; -import org.apache.commons.io.IOUtils; -import org.apache.commons.lang3.StringUtils; -import org.sunbird.actor.router.ActorConfig; -import org.sunbird.cassandra.CassandraOperation; -import org.sunbird.client.systemsettings.SystemSettingClient; -import org.sunbird.client.systemsettings.impl.SystemSettingClientImpl; -import org.sunbird.exception.ProjectCommonException; -import org.sunbird.exception.ResponseCode; -import org.sunbird.helper.ServiceFactory; -import org.sunbird.keys.JsonKey; -import org.sunbird.model.MigrationUser; -import org.sunbird.model.SelfDeclaredUser; -import org.sunbird.model.ShadowUserUpload; -import org.sunbird.model.bulkupload.BulkMigrationUser; -import org.sunbird.model.systemsettings.SystemSetting; -import org.sunbird.operations.ActorOperations; -import org.sunbird.operations.BulkUploadActorOperation; -import org.sunbird.request.Request; -import org.sunbird.request.RequestContext; -import org.sunbird.response.Response; -import org.sunbird.telemetry.util.TelemetryUtil; -import org.sunbird.util.ProjectUtil; -import org.sunbird.util.PropertiesCache; -import org.sunbird.util.Util; - -/** @author anmolgupta */ -@ActorConfig( - tasks = {"userBulkMigration", "userBulkSelfDeclared"}, - asyncTasks = {} -) -public class UserBulkMigrationActor extends BaseBulkUploadActor { - - private SystemSettingClient systemSettingClient = new SystemSettingClientImpl(); - private CassandraOperation cassandraOperation = ServiceFactory.getInstance(); - public static final int RETRY_COUNT = 2; - private CSVReader csvReader; - private static SystemSetting systemSetting; - - @Override - public void onReceive(Request request) throws Throwable { - String env = null; - String operation = request.getOperation(); - logger.info(request.getRequestContext(), "OnReceive Upload csv processing " + operation); - if (operation.equals("userBulkSelfDeclared")) { - env = "SelfDeclaredUserUpload"; - } else { - env = "ShadowUserUpload"; - } - Util.initializeContext(request, env); - if (operation.equalsIgnoreCase(BulkUploadActorOperation.USER_BULK_MIGRATION.getValue())) { - uploadCsv(request); - } else if (operation.equalsIgnoreCase( - BulkUploadActorOperation.USER_BULK_SELF_DECLARED.getValue())) { - uploadSelfDeclaredCSV(request); - } else { - onReceiveUnsupportedOperation("userBulkMigration"); - } - } - - private void uploadCsv(Request request) throws IOException { - Map<String, Object> req = (Map<String, Object>) request.getRequest().get(JsonKey.DATA); - systemSetting = - systemSettingClient.getSystemSettingByField( - getActorRef(ActorOperations.GET_SYSTEM_SETTING.getValue()), - "shadowdbmandatorycolumn", - request.getRequestContext()); - processCsvBytes(req, request); - } - - private void uploadSelfDeclaredCSV(Request request) throws IOException { - Map<String, Object> req = (Map<String, Object>) request.getRequest().get(JsonKey.DATA); - processCsvBytes(req, request); - } - - private void processCsvBytes(Map<String, Object> data, Request request) throws IOException { - ObjectMapper mapper = new ObjectMapper(); - Map<String, Object> targetObject = null; - Map<String, Object> values = null; - List<Map<String, Object>> correlatedObject = new ArrayList<>(); - String processId = ProjectUtil.getUniqueIdFromTimestamp(1); - long validationStartTime = System.currentTimeMillis(); - String userId = getCreatedBy(request); - Map<String, Object> result = getUserById(userId, request.getRequestContext()); - String channel = getChannel(result); - String rootOrgId = getRootOrgId(result); - - request.getRequest().put(JsonKey.ROOT_ORG_ID, rootOrgId); - BulkMigrationUser migrationUser = null; - if (request - .getOperation() - .equals(BulkUploadActorOperation.USER_BULK_SELF_DECLARED.getValue())) { - PropertiesCache propertiesCache = PropertiesCache.getInstance(); - String mandatoryFields = propertiesCache.getProperty(JsonKey.SELF_DECLARED_MANDATORY_FIELDS); - String optionalFields = propertiesCache.getProperty(JsonKey.SELF_DECLARED_OPTIONAL_FIELDS); - Map fieldsMap = new HashMap(); - fieldsMap.put("mandatoryFields", Arrays.asList(mandatoryFields.split(","))); - fieldsMap.put("optionalFields", Arrays.asList(optionalFields.split(","))); - List<SelfDeclaredUser> selfDeclaredUserList = - getUsers(processId, (byte[]) data.get(JsonKey.FILE), fieldsMap); - logger.info( - request.getRequestContext(), - "UserBulkMigrationActor:processRecord: time taken to validate records of size " - .concat(selfDeclaredUserList.size() + "") - + "is(ms): ".concat((System.currentTimeMillis() - validationStartTime) + "")); - migrationUser = prepareSelfDeclaredRecord(request, processId, selfDeclaredUserList); - logger.info( - request.getRequestContext(), - "UserBulkMigrationActor:processRecord:processing record for number of users " - .concat(selfDeclaredUserList.size() + "")); - } else { - values = mapper.readValue(systemSetting.getValue(), Map.class); - List<MigrationUser> migrationUserList = - getMigrationUsers(channel, processId, (byte[]) data.get(JsonKey.FILE), values); - logger.info( - request.getRequestContext(), - "UserBulkMigrationActor:processRecord: time taken to validate records of size " - .concat(migrationUserList.size() + "") - + "is(ms): ".concat((System.currentTimeMillis() - validationStartTime) + "")); - migrationUser = prepareRecord(request, processId, migrationUserList); - logger.info( - request.getRequestContext(), - "UserBulkMigrationActor:processRecord:processing record for number of users " - .concat(migrationUserList.size() + "")); - } - insertRecord(migrationUser, request.getRequestContext()); - if (request - .getOperation() - .equals(BulkUploadActorOperation.USER_BULK_SELF_DECLARED.getValue())) { - request.setOperation(BulkUploadActorOperation.PROCESS_USER_BULK_SELF_DECLARED.getValue()); - request.getRequest().put(JsonKey.PROCESS_ID, migrationUser.getId()); - tellToAnother(request); - } - TelemetryUtil.generateCorrelatedObject(processId, JsonKey.PROCESS_ID, null, correlatedObject); - TelemetryUtil.generateCorrelatedObject( - migrationUser.getTaskCount() + "", JsonKey.TASK_COUNT, null, correlatedObject); - targetObject = - TelemetryUtil.generateTargetObject( - processId, - StringUtils.capitalize(JsonKey.MIGRATION_USER_OBJECT), - "ShadowUserUpload", - null); - TelemetryUtil.telemetryProcessingCall( - mapper.convertValue(migrationUser, Map.class), - targetObject, - correlatedObject, - request.getContext()); - } - - private void insertRecord(BulkMigrationUser bulkMigrationUser, RequestContext context) { - long insertStartTime = System.currentTimeMillis(); - ObjectMapper mapper = new ObjectMapper(); - logger.info( - context, - "UserBulkMigrationActor:insertRecord:record started inserting with " - .concat(bulkMigrationUser.getId() + "")); - Map<String, Object> record = mapper.convertValue(bulkMigrationUser, Map.class); - long createdOn = System.currentTimeMillis(); - record.put(JsonKey.CREATED_ON, new Timestamp(createdOn)); - record.put(JsonKey.LAST_UPDATED_ON, new Timestamp(createdOn)); - Util.DbInfo dbInfo = Util.dbInfoMap.get(JsonKey.BULK_OP_DB); - Response response = - cassandraOperation.insertRecord( - dbInfo.getKeySpace(), dbInfo.getTableName(), record, context); - response.put(JsonKey.PROCESS_ID, bulkMigrationUser.getId()); - logger.info( - context, - "UserBulkMigrationActor:insertRecord:time taken by cassandra to insert record of size " - .concat(record.size() + "") - + "is(ms):".concat((System.currentTimeMillis() - insertStartTime) + "")); - sender().tell(response, self()); - } - - private BulkMigrationUser prepareRecord( - Request request, String processID, List<MigrationUser> migrationUserList) { - try { - ObjectMapper mapper = new ObjectMapper(); - String decryptedData = mapper.writeValueAsString(migrationUserList); - BulkMigrationUser migrationUser = - new BulkMigrationUser.BulkMigrationUserBuilder(processID, decryptedData) - .setObjectType(JsonKey.MIGRATION_USER_OBJECT) - .setUploadedDate(ProjectUtil.getFormattedDate()) - .setStatus(ProjectUtil.BulkProcessStatus.NEW.getValue()) - .setRetryCount(RETRY_COUNT) - .setTaskCount(migrationUserList.size()) - .setCreatedBy(getCreatedBy(request)) - .setUploadedBy(getCreatedBy(request)) - .setOrganisationId((String) request.getRequest().get(JsonKey.ROOT_ORG_ID)) - .setTelemetryContext(getContextMap(processID, request)) - .build(); - return migrationUser; - } catch (Exception e) { - logger.error( - request.getRequestContext(), - "UserBulkMigrationActor:prepareRecord:error occurred while getting preparing record with processId" - .concat(processID + ""), - e); - throw new ProjectCommonException( - ResponseCode.SERVER_ERROR.getErrorCode(), - ResponseCode.SERVER_ERROR.getErrorMessage(), - ResponseCode.SERVER_ERROR.getResponseCode()); - } - } - - private BulkMigrationUser prepareSelfDeclaredRecord( - Request request, String processID, List<SelfDeclaredUser> migrationUserList) { - try { - ObjectMapper mapper = new ObjectMapper(); - String decryptedData = mapper.writeValueAsString(migrationUserList); - BulkMigrationUser migrationUser = - new BulkMigrationUser.BulkMigrationUserBuilder(processID, decryptedData) - .setObjectType(JsonKey.SELF_DECLARED_USER_OBJECT) - .setUploadedDate(ProjectUtil.getFormattedDate()) - .setStatus(ProjectUtil.BulkProcessStatus.NEW.getValue()) - .setRetryCount(RETRY_COUNT) - .setTaskCount(migrationUserList.size()) - .setCreatedBy(getCreatedBy(request)) - .setUploadedBy(getCreatedBy(request)) - .setOrganisationId((String) request.getRequest().get(JsonKey.ROOT_ORG_ID)) - .setTelemetryContext(getContextMap(processID, request)) - .build(); - return migrationUser; - } catch (Exception e) { - logger.error( - "UserBulkMigrationActor:prepareRecord:error occurred while getting preparing record with processId" - .concat(processID + ""), - e); - throw new ProjectCommonException( - ResponseCode.SERVER_ERROR.getErrorCode(), - ResponseCode.SERVER_ERROR.getErrorMessage(), - ResponseCode.SERVER_ERROR.getResponseCode()); - } - } - - private Map<String, String> getContextMap(String processId, Request request) { - Map<String, String> contextMap = (Map) request.getContext(); - logger.info( - request.getRequestContext(), - "UserBulkMigrationActor:getContextMap:started preparing record for processId:" - + processId - + "with request context:" - + contextMap); - contextMap.put(JsonKey.ACTOR_TYPE, StringUtils.capitalize(JsonKey.SYSTEM)); - contextMap.put(JsonKey.ACTOR_ID, ProjectUtil.getUniqueIdFromTimestamp(0)); - Iterables.removeIf(contextMap.values(), value -> StringUtils.isBlank(value)); - return contextMap; - } - - private String getCreatedBy(Request request) { - Map<String, String> data = (Map<String, String>) request.getRequest().get(JsonKey.DATA); - return MapUtils.isNotEmpty(data) ? data.get(JsonKey.CREATED_BY) : null; - } - - private List<MigrationUser> getMigrationUsers( - String channel, String processId, byte[] fileData, Map<String, Object> fieldsMap) { - Map<String, List<String>> columnsMap = - (Map<String, List<String>>) fieldsMap.get(JsonKey.FILE_TYPE_CSV); - List<String[]> csvData = readCsv(fileData); - List<String> csvHeaders = getCsvHeadersAsList(csvData); - List<String> mandatoryHeaders = columnsMap.get(JsonKey.MANDATORY_FIELDS); - List<String> supportedHeaders = columnsMap.get(JsonKey.SUPPORTED_COlUMNS); - mandatoryHeaders.replaceAll(String::toLowerCase); - supportedHeaders.replaceAll(String::toLowerCase); - checkCsvHeader(csvHeaders, mandatoryHeaders, supportedHeaders); - List<String> mappedCsvHeaders = mapCsvColumn(csvHeaders); - List<MigrationUser> migrationUserList = - parseCsvRows(channel, getCsvRowsAsList(csvData), mappedCsvHeaders); - ShadowUserUpload migration = - new ShadowUserUpload.ShadowUserUploadBuilder() - .setHeaders(csvHeaders) - .setMappedHeaders(mappedCsvHeaders) - .setProcessId(processId) - .setFileData(fileData) - .setFileSize(fileData.length + "") - .setMandatoryFields(columnsMap.get(JsonKey.MANDATORY_FIELDS)) - .setSupportedFields(columnsMap.get(JsonKey.SUPPORTED_COlUMNS)) - .setValues(migrationUserList) - .validate(); - logger.info( - "UserBulkMigrationActor:validateRequestAndReturnMigrationUsers: the migration object formed " - .concat(migration.toString())); - return migrationUserList; - } - - private List<SelfDeclaredUser> getUsers( - String processId, byte[] fileData, Map<String, List<String>> columnsMap) { - List<String[]> csvData = readCsv(fileData); - List<String> csvHeaders = getCsvHeadersAsList(csvData); - List<String> mandatoryHeaders = columnsMap.get(JsonKey.MANDATORY_FIELDS); - List<String> supportedHeaders = columnsMap.get("optionalFields"); - mandatoryHeaders.replaceAll(String::toLowerCase); - supportedHeaders.replaceAll(String::toLowerCase); - checkCsvHeader(csvHeaders, mandatoryHeaders, supportedHeaders); - List<String> mappedCsvHeaders = mapSelfDeclaredCsvColumn(csvHeaders); - List<SelfDeclaredUser> selfDeclaredUserList = - parseSelfDeclaredCsvRows(getCsvRowsAsList(csvData), mappedCsvHeaders); - ShadowUserUpload migration = - new ShadowUserUpload.ShadowUserUploadBuilder() - .setHeaders(csvHeaders) - .setMappedHeaders(mappedCsvHeaders) - .setProcessId(processId) - .setFileData(fileData) - .setFileSize(fileData.length + "") - .setMandatoryFields(columnsMap.get(JsonKey.MANDATORY_FIELDS)) - .setSupportedFields(supportedHeaders) - .setUserValues(selfDeclaredUserList) - .validateDeclaredUsers(); - logger.info( - "UserBulkMigrationActor:validateRequestAndReturnDeclaredUsers: the migration object formed " - .concat(migration.toString())); - return selfDeclaredUserList; - } - - private List<String[]> readCsv(byte[] fileData) { - List<String[]> values = new ArrayList<>(); - try { - csvReader = getCsvReader(fileData, ',', '"', 0); - logger.info( - "UserBulkMigrationActor:readCsv:csvReader initialized ".concat(csvReader.toString())); - values = csvReader.readAll(); - } catch (Exception ex) { - logger.error("UserBulkMigrationActor:readCsv:error occurred while getting csvReader", ex); - throw new ProjectCommonException( - ResponseCode.SERVER_ERROR.getErrorCode(), - ResponseCode.SERVER_ERROR.getErrorMessage(), - ResponseCode.SERVER_ERROR.getResponseCode()); - } finally { - IOUtils.closeQuietly(csvReader); - } - return values; - } - - private List<String> getCsvHeadersAsList(List<String[]> csvData) { - List<String> headers = new ArrayList<>(); - int CSV_COLUMN_NAMES = 0; - if (null == csvData || csvData.isEmpty()) { - throw new ProjectCommonException( - ResponseCode.blankCsvData.getErrorCode(), - ResponseCode.blankCsvData.getErrorMessage(), - ResponseCode.CLIENT_ERROR.getResponseCode()); - } - headers.addAll(Arrays.asList(csvData.get(CSV_COLUMN_NAMES))); - headers.replaceAll(String::toLowerCase); - return headers; - } - - private List<String[]> getCsvRowsAsList(List<String[]> csvData) { - return csvData.subList(1, csvData.size()); - } - - private List<String> mapCsvColumn(List<String> csvColumns) { - List<String> mappedColumns = new ArrayList<>(); - csvColumns.forEach( - column -> { - if (column.equalsIgnoreCase(JsonKey.EMAIL)) { - mappedColumns.add(column); - } - if (column.equalsIgnoreCase(JsonKey.PHONE)) { - mappedColumns.add(column); - } - if (column.equalsIgnoreCase(JsonKey.EXTERNAL_USER_ID)) { - mappedColumns.add(JsonKey.USER_EXTERNAL_ID); - } - if (column.equalsIgnoreCase(JsonKey.EXTERNAL_ORG_ID)) { - mappedColumns.add(JsonKey.ORG_EXTERNAL_ID); - } - if (column.equalsIgnoreCase(JsonKey.NAME)) { - mappedColumns.add(JsonKey.FIRST_NAME); - } - if (column.equalsIgnoreCase(JsonKey.INPUT_STATUS)) { - mappedColumns.add(column); - } - }); - return mappedColumns; - } - - private List<String> mapSelfDeclaredCsvColumn(List<String> csvColumns) { - List<String> mappedColumns = new ArrayList<>(); - csvColumns.forEach( - column -> { - switch (column) { - case "email id": - mappedColumns.add(JsonKey.EMAIL); - break; - case "phone number": - mappedColumns.add(JsonKey.PHONE); - break; - case "state provided ext. id": - mappedColumns.add(JsonKey.USER_EXTERNAL_ID); - break; - case "status": - mappedColumns.add(JsonKey.INPUT_STATUS); - break; - case "diksha uuid": - mappedColumns.add(JsonKey.USER_ID); - break; - case "channel": - mappedColumns.add(column); - break; - case "school name": - mappedColumns.add("schoolName"); - break; - case "school udise id": - mappedColumns.add("schoolUdiseId"); - break; - case "persona": - mappedColumns.add(column); - break; - case "error type": - mappedColumns.add(JsonKey.ERROR_TYPE); - break; - case "state": - mappedColumns.add(JsonKey.STATE); - break; - case "district": - mappedColumns.add("district"); - break; - case "name": - mappedColumns.add(JsonKey.NAME); - break; - default: - } - }); - return mappedColumns; - } - - private List<MigrationUser> parseCsvRows( - String channel, List<String[]> values, List<String> mappedHeaders) { - List<MigrationUser> migrationUserList = new ArrayList<>(); - values - .stream() - .forEach( - row -> { - int index = values.indexOf(row); - MigrationUser migrationUser = new MigrationUser(); - for (int i = 0; i < row.length; i++) { - if (row.length > mappedHeaders.size()) { - throw new ProjectCommonException( - ResponseCode.errorUnsupportedField.getErrorCode(), - ResponseCode.errorUnsupportedField.getErrorMessage(), - ResponseCode.CLIENT_ERROR.getResponseCode(), - "Invalid provided ROW:" + (index + 1)); - } - String columnName = getColumnNameByIndex(mappedHeaders, i); - setFieldToMigrationUserObject(migrationUser, columnName, trimValue(row[i])); - } - // channel to be added here - migrationUser.setChannel(channel); - migrationUserList.add(migrationUser); - }); - return migrationUserList; - } - - private List<SelfDeclaredUser> parseSelfDeclaredCsvRows( - List<String[]> values, List<String> mappedHeaders) { - List<SelfDeclaredUser> declaredUserList = new ArrayList<>(); - List<String> skipColumns = - new ArrayList<>(Arrays.asList(JsonKey.NAME, JsonKey.STATE, "district")); - values - .stream() - .forEach( - row -> { - int index = values.indexOf(row); - if (row.length > mappedHeaders.size()) { - throw new ProjectCommonException( - ResponseCode.errorUnsupportedField.getErrorCode(), - ResponseCode.errorUnsupportedField.getErrorMessage(), - ResponseCode.CLIENT_ERROR.getResponseCode(), - "Invalid provided ROW:" + (index + 1)); - } - SelfDeclaredUser selfDeclaredUser = new SelfDeclaredUser(); - for (int i = 0; i < row.length; i++) { - String columnName = getColumnNameByIndex(mappedHeaders, i); - if (!skipColumns.contains(columnName)) { - setFieldToDeclaredUserObject(selfDeclaredUser, columnName, trimValue(row[i])); - } - } - declaredUserList.add(selfDeclaredUser); - }); - return declaredUserList; - } - - private String trimValue(String value) { - if (StringUtils.isNotBlank(value)) { - return value.trim(); - } - return value; - } - - private void setFieldToMigrationUserObject( - MigrationUser migrationUser, String columnAttribute, Object value) { - - if (columnAttribute.equalsIgnoreCase(JsonKey.EMAIL)) { - String email = (String) value; - migrationUser.setEmail(email); - } - if (columnAttribute.equalsIgnoreCase(JsonKey.PHONE)) { - String phone = (String) value; - migrationUser.setPhone(phone); - } - if (columnAttribute.equalsIgnoreCase(JsonKey.ORG_EXTERNAL_ID)) { - migrationUser.setOrgExternalId((String) value); - } - if (columnAttribute.equalsIgnoreCase(JsonKey.USER_EXTERNAL_ID)) { - migrationUser.setUserExternalId((String) value); - } - - if (columnAttribute.equalsIgnoreCase(JsonKey.FIRST_NAME)) { - migrationUser.setName(StringUtils.trim((String) value)); - } - if (columnAttribute.equalsIgnoreCase(JsonKey.INPUT_STATUS)) { - migrationUser.setInputStatus((String) value); - } - } - - private void setFieldToDeclaredUserObject( - SelfDeclaredUser migrationUser, String columnAttribute, Object value) { - - if (columnAttribute.equalsIgnoreCase(JsonKey.EMAIL)) { - String email = (String) value; - migrationUser.setEmail(email); - } - if (columnAttribute.equalsIgnoreCase(JsonKey.PHONE)) { - String phone = (String) value; - migrationUser.setPhone(phone); - } - if (columnAttribute.equalsIgnoreCase(JsonKey.USER_EXTERNAL_ID)) { - migrationUser.setUserExternalId((String) value); - } - if (columnAttribute.equalsIgnoreCase(JsonKey.INPUT_STATUS)) { - migrationUser.setInputStatus((String) value); - } - if (columnAttribute.equalsIgnoreCase(JsonKey.CHANNEL)) { - migrationUser.setChannel((String) value); - } - if (columnAttribute.equalsIgnoreCase("schoolName")) { - migrationUser.setSchoolName((String) value); - } - // Assigning sub-org externalid - if (columnAttribute.equalsIgnoreCase("schoolUdiseId")) { - migrationUser.setSubOrgExternalId((String) value); - } - if (columnAttribute.equalsIgnoreCase(JsonKey.USER_ID)) { - migrationUser.setUserId((String) value); - } - if (columnAttribute.equalsIgnoreCase(JsonKey.PERSONA)) { - migrationUser.setPersona((String) value); - } - if (columnAttribute.equalsIgnoreCase(JsonKey.ERROR_TYPE)) { - migrationUser.setErrorType((String) value); - } - } - - private String getColumnNameByIndex(List<String> mappedHeaders, int index) { - return mappedHeaders.get(index); - } - - /** - * in bulk_upload_process db user will have channel of admin users - * - * @param result - * @return channel - */ - private String getChannel(Map<String, Object> result) { - String channel = (String) result.get(JsonKey.CHANNEL); - logger.info( - "UserBulkMigrationActor:getChannel: the channel of admin user ".concat(channel + "")); - return channel; - } - /** - * in bulk_upload_process db organisationId will be of user. - * - * @param result - * @return rootOrgId - */ - private String getRootOrgId(Map<String, Object> result) { - String rootOrgId = (String) result.get(JsonKey.ROOT_ORG_ID); - logger.info( - "UserBulkMigrationActor:getRootOrgId:the root org id of admin user " - .concat(rootOrgId + "")); - return rootOrgId; - } - - /** - * this method will fetch user record with userId from cassandra - * - * @param userId - * @return result - */ - private Map<String, Object> getUserById(String userId, RequestContext context) { - Util.DbInfo usrDbInfo = Util.dbInfoMap.get(JsonKey.USER_DB); - Response response = - cassandraOperation.getRecordById( - usrDbInfo.getKeySpace(), usrDbInfo.getTableName(), userId, context); - if (((List) response.getResult().get(JsonKey.RESPONSE)).isEmpty()) { - throw new ProjectCommonException( - ResponseCode.userNotFound.getErrorCode(), - ResponseCode.userNotFound.getErrorMessage(), - ResponseCode.CLIENT_ERROR.getResponseCode()); - } - Map<String, Object> result = ((Map) ((List) response.getResult().get(JsonKey.RESPONSE)).get(0)); - return result; - } - - private void checkCsvHeader( - List<String> csvHeaders, List<String> mandatoryHeaders, List<String> supportedHeaders) { - checkMandatoryColumns(csvHeaders, mandatoryHeaders); - checkSupportedColumns(csvHeaders, supportedHeaders); - } - - private void checkMandatoryColumns(List<String> csvHeaders, List<String> mandatoryHeaders) { - logger.info( - "UserBulkMigrationRequestValidator:checkMandatoryColumns:mandatory columns got " - + mandatoryHeaders); - mandatoryHeaders.forEach( - column -> { - if (!csvHeaders.contains(column)) { - logger.info( - "UserBulkMigrationRequestValidator:mandatoryColumns: mandatory column is not present" - .concat(column + "")); - throw new ProjectCommonException( - ResponseCode.mandatoryParamsMissing.getErrorCode(), - ResponseCode.mandatoryParamsMissing.getErrorMessage(), - ResponseCode.CLIENT_ERROR.getResponseCode(), - column); - } - }); - } - - private void checkSupportedColumns(List<String> csvHeaders, List<String> supportedHeaders) { - logger.info( - "UserBulkMigrationRequestValidator:checkSupportedColumns:mandatory columns got " - + supportedHeaders); - supportedHeaders.forEach( - suppColumn -> { - if (!csvHeaders.contains(suppColumn)) { - logger.info( - "UserBulkMigrationRequestValidator:supportedColumns: supported column is not present" - .concat(suppColumn + "")); - throw new ProjectCommonException( - ResponseCode.errorUnsupportedField.getErrorCode(), - ResponseCode.errorUnsupportedField.getErrorMessage(), - ResponseCode.CLIENT_ERROR.getResponseCode(), - "Invalid provided column:" - .concat(suppColumn) - .concat(":supported headers are:") - .concat(ArrayUtils.stringifyContents(supportedHeaders.toArray()))); - } - }); - } -} diff --git a/service/src/main/java/org/sunbird/actor/scheduler/OnDemandSchedulerActor.java b/service/src/main/java/org/sunbird/actor/scheduler/OnDemandSchedulerActor.java deleted file mode 100644 index 27abfa45bd516117cc440efba8a71811754c358d..0000000000000000000000000000000000000000 --- a/service/src/main/java/org/sunbird/actor/scheduler/OnDemandSchedulerActor.java +++ /dev/null @@ -1,78 +0,0 @@ -package org.sunbird.actor.scheduler; - -import static org.sunbird.validator.orgvalidator.BaseOrgRequestValidator.ERROR_CODE; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import org.sunbird.actor.core.BaseActor; -import org.sunbird.actor.router.ActorConfig; -import org.sunbird.exception.ProjectCommonException; -import org.sunbird.exception.ResponseCode; -import org.sunbird.keys.JsonKey; -import org.sunbird.operations.ActorOperations; -import org.sunbird.quartzscheduler.OnDemandSchedulerManager; -import org.sunbird.request.Request; -import org.sunbird.response.Response; - -/** @author Amit Kumar */ -@ActorConfig( - tasks = {"onDemandStartScheduler"}, - asyncTasks = {} -) -public class OnDemandSchedulerActor extends BaseActor { - private static final String TYPE = "type"; - - @Override - public void onReceive(Request actorMessage) throws Throwable { - if (actorMessage - .getOperation() - .equalsIgnoreCase(ActorOperations.ONDEMAND_START_SCHEDULER.getValue())) { - startSchedular(actorMessage); - } else { - onReceiveUnsupportedOperation(actorMessage.getOperation()); - } - } - - private void startSchedular(Request actorMessage) { - Map<String, Object> req = actorMessage.getRequest(); - ArrayList<String> jobTypes = (ArrayList<String>) req.get(TYPE); - if (jobTypes.size() > 0) { - String[] jobs = jobTypes.stream().toArray(String[]::new); - validateJobs(jobs); - scheduleJob(jobs); - } else { - throw new ProjectCommonException( - ResponseCode.mandatoryParamsMissing.getErrorCode(), - ResponseCode.mandatoryParamsMissing.getErrorMessage(), - ERROR_CODE, - TYPE); - } - } - - private void validateJobs(String[] jobs) { - List<String> jobsAllowed = new ArrayList<String>(); - jobsAllowed.add("bulkupload"); - jobsAllowed.add("shadowuser"); - for (String job : jobs) { - if (!jobsAllowed.contains(job)) { - throw new ProjectCommonException( - ResponseCode.invalidParameter.getErrorCode(), - ResponseCode.invalidParameter.getErrorMessage(), - ERROR_CODE, - TYPE); - } - } - } - - private void scheduleJob(String[] jobs) { - Response response = new Response(); - OnDemandSchedulerManager onDemandSchedulerManager = OnDemandSchedulerManager.getInstance(); - new Thread(() -> onDemandSchedulerManager.triggerScheduler(jobs)).start(); - Map result = new HashMap<String, Object>(); - result.put(JsonKey.STATUS, JsonKey.SUCCESS); - response.put(JsonKey.RESULT, result); - sender().tell(response, self()); - } -} diff --git a/service/src/main/java/org/sunbird/actor/scheduler/SchedularActor.java b/service/src/main/java/org/sunbird/actor/scheduler/SchedularActor.java deleted file mode 100644 index b3db7a9729fe8d567078b2aa31ddd0e4379a94a5..0000000000000000000000000000000000000000 --- a/service/src/main/java/org/sunbird/actor/scheduler/SchedularActor.java +++ /dev/null @@ -1,89 +0,0 @@ -package org.sunbird.actor.scheduler; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import org.sunbird.actor.core.BaseActor; -import org.sunbird.actor.router.ActorConfig; -import org.sunbird.cassandra.CassandraOperation; -import org.sunbird.helper.ServiceFactory; -import org.sunbird.keys.JsonKey; -import org.sunbird.operations.ActorOperations; -import org.sunbird.operations.BulkUploadActorOperation; -import org.sunbird.request.Request; -import org.sunbird.util.ProjectUtil; -import org.sunbird.util.UserUtility; -import org.sunbird.util.Util; - -/** @author Amit Kumar */ -@ActorConfig( - tasks = {}, - asyncTasks = {"scheduleBulkUpload"} -) -public class SchedularActor extends BaseActor { - @Override - public void onReceive(Request actorMessage) throws Throwable { - if (actorMessage - .getOperation() - .equalsIgnoreCase(ActorOperations.SCHEDULE_BULK_UPLOAD.getValue())) { - schedule(actorMessage); - } else { - onReceiveUnsupportedOperation("SchedularActor"); - } - } - - @SuppressWarnings("unchecked") - private void schedule(Request request) { - List<Map<String, Object>> result = (List<Map<String, Object>>) request.get(JsonKey.DATA); - Util.DbInfo bulkDb = Util.dbInfoMap.get(JsonKey.BULK_OP_DB); - CassandraOperation cassandraOperation = ServiceFactory.getInstance(); - for (Map<String, Object> map : result) { - int retryCount = 0; - if (null != map.get(JsonKey.RETRY_COUNT)) { - retryCount = (int) map.get(JsonKey.RETRY_COUNT); - } - if (retryCount > 2) { - String data = (String) map.get(JsonKey.DATA); - try { - Map<String, Object> bulkMap = new HashMap<>(); - bulkMap.put(JsonKey.DATA, UserUtility.encryptData(data)); - bulkMap.put(JsonKey.PROCESS_ID, map.get(JsonKey.ID)); - bulkMap.put(JsonKey.STATUS, ProjectUtil.BulkProcessStatus.FAILED.getValue()); - cassandraOperation.updateRecord( - bulkDb.getKeySpace(), bulkDb.getTableName(), bulkMap, request.getRequestContext()); - } catch (Exception e) { - logger.error( - request.getRequestContext(), - "Exception occurred while encrypting data while running scheduler for bulk upload process : ", - e); - } - } else { - Map<String, Object> bulkMap = new HashMap<>(); - bulkMap.put(JsonKey.RETRY_COUNT, retryCount + 1); - bulkMap.put(JsonKey.ID, map.get(JsonKey.ID)); - bulkMap.put(JsonKey.STATUS, ProjectUtil.BulkProcessStatus.IN_PROGRESS.getValue()); - cassandraOperation.updateRecord( - bulkDb.getKeySpace(), bulkDb.getTableName(), bulkMap, request.getRequestContext()); - Request req = new Request(); - req.put(JsonKey.PROCESS_ID, map.get(JsonKey.ID)); - req.setRequestContext(request.getRequestContext()); - logger.info( - request.getRequestContext(), - "SchedularActor: scheduleBulkUpload called with processId " - + map.get(JsonKey.ID) - + " and type " - + map.get(JsonKey.OBJECT_TYPE)); - if (JsonKey.LOCATION.equalsIgnoreCase((String) map.get(JsonKey.OBJECT_TYPE))) { - req.setOperation(BulkUploadActorOperation.LOCATION_BULK_UPLOAD_BACKGROUND_JOB.getValue()); - } else if (JsonKey.ORGANISATION.equalsIgnoreCase((String) map.get(JsonKey.OBJECT_TYPE))) { - req.setOperation(BulkUploadActorOperation.ORG_BULK_UPLOAD_BACKGROUND_JOB.getValue()); - } else if (JsonKey.USER.equals((String) map.get(JsonKey.OBJECT_TYPE))) { - req.setOperation(BulkUploadActorOperation.USER_BULK_UPLOAD_BACKGROUND_JOB.getValue()); - } else { - req.setOperation(ActorOperations.PROCESS_BULK_UPLOAD.getValue()); - } - tellToAnother(req); - } - } - } -} diff --git a/service/src/main/java/org/sunbird/actor/user/validator/UserBulkMigrationRequestValidator.java b/service/src/main/java/org/sunbird/actor/user/validator/UserBulkMigrationRequestValidator.java deleted file mode 100644 index 06f74d4aa82b9d96c25eda385c456f4a276dba45..0000000000000000000000000000000000000000 --- a/service/src/main/java/org/sunbird/actor/user/validator/UserBulkMigrationRequestValidator.java +++ /dev/null @@ -1,278 +0,0 @@ -package org.sunbird.actor.user.validator; - -import java.util.HashSet; -import java.util.regex.Pattern; -import java.util.stream.Collectors; -import java.util.stream.Stream; -import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.lang3.StringUtils; -import org.sunbird.model.MigrationUser; -import org.sunbird.model.SelfDeclaredUser; -import org.sunbird.model.ShadowUserUpload; -import org.sunbird.error.CsvError; -import org.sunbird.error.CsvRowErrorDetails; -import org.sunbird.error.ErrorEnum; -import org.sunbird.error.IErrorDispatcher; -import org.sunbird.error.factory.ErrorDispatcherFactory; -import org.sunbird.exception.ProjectCommonException; -import org.sunbird.exception.ResponseCode; -import org.sunbird.keys.JsonKey; -import org.sunbird.model.bulkupload.SelfDeclaredErrorTypeEnum; -import org.sunbird.model.bulkupload.SelfDeclaredStatusEnum; -import org.sunbird.util.ProjectUtil; - -/** - * this class will validate the csv file for shadow db - * - * @author anmolgupta - */ -public class UserBulkMigrationRequestValidator { - - private ShadowUserUpload shadowUserMigration; - private HashSet<String> userExternalIdsSet = new HashSet<>(); - private CsvError csvRowsErrors = new CsvError(); - private static final int MAX_ROW_SUPPORTED = 20000; - private static final String NAME_REG_MATCHER = "^[^.][^^;\\-()<>|!=’%_#$]+"; - - private UserBulkMigrationRequestValidator(ShadowUserUpload migration) { - this.shadowUserMigration = migration; - } - - public static UserBulkMigrationRequestValidator getInstance(ShadowUserUpload migration) { - return new UserBulkMigrationRequestValidator(migration); - } - - public void validate() { - checkCsvRows(); - } - - public void validateDeclaredUsers() { - checkDeclaredCsvRows(); - } - - private void checkCsvRows() { - validateRowsCount(); - shadowUserMigration - .getValues() - .stream() - .forEach( - onCsvRow -> { - int index = shadowUserMigration.getValues().indexOf(onCsvRow); - validateMigrationUser(onCsvRow, index); - }); - if (csvRowsErrors.getErrorsList().size() > 0) { - IErrorDispatcher errorDispatcher = ErrorDispatcherFactory.getErrorDispatcher(csvRowsErrors); - errorDispatcher.dispatchError(); - } - } - - private void checkDeclaredCsvRows() { - validateRowsCount(); - shadowUserMigration - .getUser() - .stream() - .forEach( - onCsvRow -> { - int index = shadowUserMigration.getUser().indexOf(onCsvRow); - validateSelfDeclaredUser(onCsvRow, index); - }); - if (CollectionUtils.isNotEmpty(csvRowsErrors.getErrorsList())) { - IErrorDispatcher errorDispatcher = ErrorDispatcherFactory.getErrorDispatcher(csvRowsErrors); - errorDispatcher.dispatchError(); - } - } - - private void validateRowsCount() { - int ROW_BEGINNING_INDEX = 1; - if ((shadowUserMigration.getValues() != null - && shadowUserMigration.getValues().size() >= MAX_ROW_SUPPORTED) - || (shadowUserMigration.getUser() != null - && shadowUserMigration.getUser().size() >= MAX_ROW_SUPPORTED)) { - throw new ProjectCommonException( - ResponseCode.csvRowsExceeds.getErrorCode(), - ResponseCode.csvRowsExceeds.getErrorMessage().concat("supported:" + MAX_ROW_SUPPORTED), - ResponseCode.CLIENT_ERROR.getResponseCode()); - } else if ((shadowUserMigration.getValues() != null - && shadowUserMigration.getValues().size() < ROW_BEGINNING_INDEX) - || (shadowUserMigration.getUser() != null - && shadowUserMigration.getUser().size() < ROW_BEGINNING_INDEX)) { - throw new ProjectCommonException( - ResponseCode.noDataForConsumption.getErrorCode(), - ResponseCode.noDataForConsumption.getErrorMessage(), - ResponseCode.CLIENT_ERROR.getResponseCode()); - } - } - - private void validateMigrationUser(MigrationUser migrationUser, int index) { - - checkEmailAndPhone(migrationUser.getEmail(), migrationUser.getPhone(), index); - checkUserExternalId(migrationUser.getUserExternalId(), index); - checkName(migrationUser.getName(), index); - checkInputStatus(migrationUser.getInputStatus(), index); - } - - private void validateSelfDeclaredUser(SelfDeclaredUser migrationUser, int index) { - checkUserDeclaredExternalId(migrationUser.getUserExternalId(), index); - checkSelfDeclaredInputStatus(migrationUser.getInputStatus(), index); - if ((migrationUser.getInputStatus().equals(JsonKey.SELF_DECLARED_ERROR))) { - checkSelfDeclaredErrorTypeIfPresent(migrationUser.getErrorType(), index); - } - checkValue(migrationUser.getUserId(), index, JsonKey.DIKSHA_UUID); - checkValue(migrationUser.getChannel(), index, JsonKey.CHANNEL); - checkValue(migrationUser.getPersona(), index, JsonKey.PERSONA); - } - - private void checkUserDeclaredExternalId(String userExternalId, int index) { - CsvRowErrorDetails errorDetails = new CsvRowErrorDetails(); - errorDetails.setRowId(index); - errorDetails.setHeader(JsonKey.STATE_PROVIDED_EXT_ID); - if (StringUtils.isBlank(userExternalId)) { - errorDetails.setErrorEnum(ErrorEnum.missing); - } - if (!userExternalIdsSet.add(userExternalId)) { - errorDetails.setErrorEnum(ErrorEnum.duplicate); - } - if (errorDetails.getErrorEnum() != null) { - addErrorToList(errorDetails); - } - } - - private void checkSelfDeclaredErrorTypeIfPresent(String errorType, int index) { - CsvRowErrorDetails errorDetails = new CsvRowErrorDetails(); - errorDetails.setRowId(index); - if (StringUtils.isBlank(errorType)) { - errorDetails.setHeader("Error Type"); - errorDetails.setErrorEnum(ErrorEnum.missing); - addErrorToList(errorDetails); - - } else if (!Stream.of(SelfDeclaredErrorTypeEnum.values()) - .map(Enum::name) - .collect(Collectors.toList()) - .contains((errorType.replace("-", "_")).replace(" ", "_"))) { - errorDetails.setHeader("Error Type"); - errorDetails.setErrorEnum(ErrorEnum.invalid); - addErrorToList(errorDetails); - } - } - - private void addErrorToList(CsvRowErrorDetails errorDetails) { - csvRowsErrors.setError(errorDetails); - } - - public void checkEmailAndPhone(String email, String phone, int index) { - CsvRowErrorDetails errorDetails = new CsvRowErrorDetails(); - errorDetails.setRowId(index); - boolean isEmailBlank = StringUtils.isBlank(email); - boolean isPhoneBlank = StringUtils.isBlank(phone); - if (isEmailBlank && isPhoneBlank) { - errorDetails.setErrorEnum(ErrorEnum.missing); - errorDetails.setHeader(JsonKey.EMAIL); - } - - if (!isEmailBlank) { - checkEmail(email, index); - } - if (!isPhoneBlank) { - checkPhone(phone, index); - } - if (errorDetails.getErrorEnum() != null) { - addErrorToList(errorDetails); - } - } - - private void checkEmail(String email, int index) { - CsvRowErrorDetails errorDetails = new CsvRowErrorDetails(); - errorDetails.setRowId(index); - errorDetails.setHeader(JsonKey.EMAIL); - boolean isEmailValid = ProjectUtil.isEmailvalid(email); - if (!isEmailValid) { - errorDetails.setErrorEnum(ErrorEnum.invalid); - } - if (errorDetails.getErrorEnum() != null) { - addErrorToList(errorDetails); - } - } - - private void checkPhone(String phone, int index) { - CsvRowErrorDetails errorDetails = new CsvRowErrorDetails(); - errorDetails.setRowId(index); - errorDetails.setHeader(JsonKey.PHONE); - boolean isPhoneValid = ProjectUtil.validatePhoneNumber(phone); - if (!isPhoneValid) { - errorDetails.setErrorEnum(ErrorEnum.invalid); - } - if (errorDetails.getErrorEnum() != null) { - addErrorToList(errorDetails); - } - } - - private void checkUserExternalId(String userExternalId, int index) { - CsvRowErrorDetails errorDetails = new CsvRowErrorDetails(); - errorDetails.setRowId(index); - errorDetails.setHeader(JsonKey.USER_EXTERNAL_ID); - if (StringUtils.isBlank(userExternalId)) { - errorDetails.setErrorEnum(ErrorEnum.missing); - } - if (!userExternalIdsSet.add(userExternalId)) { - errorDetails.setErrorEnum(ErrorEnum.duplicate); - } - if (errorDetails.getErrorEnum() != null) { - addErrorToList(errorDetails); - } - } - - private void checkName(String name, int index) { - if (StringUtils.isNotBlank(name) && !(Pattern.matches(NAME_REG_MATCHER, name))) { - CsvRowErrorDetails errorDetails = new CsvRowErrorDetails(); - errorDetails.setRowId(index); - errorDetails.setHeader(JsonKey.NAME); - errorDetails.setErrorEnum(ErrorEnum.invalid); - addErrorToList(errorDetails); - } - checkValue(name, index, JsonKey.NAME); - } - - private void checkInputStatus(String inputStatus, int index) { - CsvRowErrorDetails errorDetails = new CsvRowErrorDetails(); - errorDetails.setRowId(index); - if (StringUtils.isBlank(inputStatus)) { - errorDetails.setHeader(JsonKey.INPUT_STATUS); - errorDetails.setErrorEnum(ErrorEnum.missing); - addErrorToList(errorDetails); - - } else if (!(inputStatus.equalsIgnoreCase(JsonKey.ACTIVE) - || inputStatus.equalsIgnoreCase(JsonKey.INACTIVE))) { - errorDetails.setHeader(JsonKey.INPUT_STATUS); - errorDetails.setErrorEnum(ErrorEnum.invalid); - addErrorToList(errorDetails); - } - } - - private void checkSelfDeclaredInputStatus(String inputStatus, int index) { - CsvRowErrorDetails errorDetails = new CsvRowErrorDetails(); - errorDetails.setRowId(index); - if (StringUtils.isBlank(inputStatus)) { - errorDetails.setHeader(JsonKey.STATUS); - errorDetails.setErrorEnum(ErrorEnum.missing); - addErrorToList(errorDetails); - - } else if (!Stream.of(SelfDeclaredStatusEnum.values()) - .map(Enum::name) - .collect(Collectors.toList()) - .contains(inputStatus)) { - errorDetails.setHeader(JsonKey.STATUS); - errorDetails.setErrorEnum(ErrorEnum.invalid); - addErrorToList(errorDetails); - } - } - - private void checkValue(String column, int rowIndex, String header) { - if (StringUtils.isBlank(column)) { - CsvRowErrorDetails errorDetails = new CsvRowErrorDetails(); - errorDetails.setRowId(rowIndex); - errorDetails.setHeader(header); - errorDetails.setErrorEnum(ErrorEnum.missing); - addErrorToList(errorDetails); - } - } -} diff --git a/service/src/main/java/org/sunbird/error/CsvError.java b/service/src/main/java/org/sunbird/error/CsvError.java deleted file mode 100644 index baa0b3d10d0990c13b6055997f44d9e7acc29125..0000000000000000000000000000000000000000 --- a/service/src/main/java/org/sunbird/error/CsvError.java +++ /dev/null @@ -1,23 +0,0 @@ -package org.sunbird.error; - -import java.util.ArrayList; -import java.util.List; - -public class CsvError { - - private List<CsvRowErrorDetails> errorsList = new ArrayList<>(); - - public CsvError() {} - - public List<CsvRowErrorDetails> getErrorsList() { - return errorsList; - } - - public void setErrorsList(List<CsvRowErrorDetails> errorsList) { - this.errorsList = errorsList; - } - - public void setError(CsvRowErrorDetails errorDetails) { - errorsList.add(errorDetails); - } -} diff --git a/service/src/main/java/org/sunbird/error/CsvErrorDispatcher.java b/service/src/main/java/org/sunbird/error/CsvErrorDispatcher.java deleted file mode 100644 index 8c2b1a14c37a906ca050c153051a5020d2081bd8..0000000000000000000000000000000000000000 --- a/service/src/main/java/org/sunbird/error/CsvErrorDispatcher.java +++ /dev/null @@ -1,30 +0,0 @@ -package org.sunbird.error; - -import org.sunbird.exception.ProjectCommonException; -import org.sunbird.exception.ResponseCode; - -/** - * this class will dispatch the errors in the csv format - * - * @author anmolgupta - */ -public class CsvErrorDispatcher implements IErrorDispatcher { - - private CsvError error; - - private CsvErrorDispatcher(CsvError error) { - this.error = error; - } - - public static CsvErrorDispatcher getInstance(CsvError error) { - return new CsvErrorDispatcher(error); - } - - @Override - public void dispatchError() { - throw new ProjectCommonException( - ResponseCode.invalidRequestData.getErrorCode(), - error.getErrorsList().toString(), - ResponseCode.CLIENT_ERROR.getResponseCode()); - } -} diff --git a/service/src/main/java/org/sunbird/error/CsvRowErrorDetails.java b/service/src/main/java/org/sunbird/error/CsvRowErrorDetails.java deleted file mode 100644 index bdd407b0a2f385dffcbfb46b6ceea060df295439..0000000000000000000000000000000000000000 --- a/service/src/main/java/org/sunbird/error/CsvRowErrorDetails.java +++ /dev/null @@ -1,53 +0,0 @@ -package org.sunbird.error; - -public class CsvRowErrorDetails { - - int rowId; - private String header; - private ErrorEnum errorEnum; - - public CsvRowErrorDetails(int rowId, String header, ErrorEnum errorEnum) { - this.rowId = rowId; - this.header = header; - this.errorEnum = errorEnum; - } - - public CsvRowErrorDetails() {} - - public int getRowId() { - return rowId; - } - - public void setRowId(int rowId) { - this.rowId = rowId; - } - - public String getHeader() { - return header; - } - - public void setHeader(String header) { - this.header = header; - } - - public ErrorEnum getErrorEnum() { - return errorEnum; - } - - public void setErrorEnum(ErrorEnum errorEnum) { - this.errorEnum = errorEnum; - } - - @Override - public String toString() { - return "ErrorDetails{" - + "rowId=" - + rowId - + ", header='" - + header - + '\'' - + ", errorEnum=" - + errorEnum - + '}'; - } -} diff --git a/service/src/main/java/org/sunbird/error/ErrorEnum.java b/service/src/main/java/org/sunbird/error/ErrorEnum.java deleted file mode 100644 index 552accb0117905c26f21741b1b8f47473e8215eb..0000000000000000000000000000000000000000 --- a/service/src/main/java/org/sunbird/error/ErrorEnum.java +++ /dev/null @@ -1,16 +0,0 @@ -package org.sunbird.error; - -public enum ErrorEnum { - invalid("invalid"), - duplicate("duplicate"), - missing("missing"); - private String value; - - ErrorEnum(String value) { - this.value = value; - } - - public String getValue() { - return value; - } -} diff --git a/service/src/main/java/org/sunbird/error/IErrorDispatcher.java b/service/src/main/java/org/sunbird/error/IErrorDispatcher.java deleted file mode 100644 index 6d067c29f37eb281954786d7a9cb2fe004d62dee..0000000000000000000000000000000000000000 --- a/service/src/main/java/org/sunbird/error/IErrorDispatcher.java +++ /dev/null @@ -1,12 +0,0 @@ -package org.sunbird.error; - -/** - * this is an interface class for the error dispatcher - * - * @author anmolgupta - */ -public interface IErrorDispatcher { - - /** this method will prepare the error and will throw ProjectCommonException. */ - void dispatchError(); -} diff --git a/service/src/main/java/org/sunbird/error/ListErrorDispatcher.java b/service/src/main/java/org/sunbird/error/ListErrorDispatcher.java deleted file mode 100644 index 2cc8236c59457063290ac9862ba4c9f5b762709d..0000000000000000000000000000000000000000 --- a/service/src/main/java/org/sunbird/error/ListErrorDispatcher.java +++ /dev/null @@ -1,48 +0,0 @@ -package org.sunbird.error; - -import com.mchange.v1.util.ArrayUtils; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import org.sunbird.exception.ProjectCommonException; -import org.sunbird.exception.ResponseCode; - -/** - * this class will dispatch error in list format - * - * @author anmolgupta - */ -public class ListErrorDispatcher implements IErrorDispatcher { - - private CsvError error; - - private ListErrorDispatcher(CsvError error) { - this.error = error; - } - - public static ListErrorDispatcher getInstance(CsvError error) { - return new ListErrorDispatcher(error); - } - - @Override - public void dispatchError() { - Collections.sort(error.getErrorsList(), new RowComparator()); - List<String> errors = new ArrayList<>(); - error - .getErrorsList() - .stream() - .forEach( - errorDetails -> { - errors.add( - String.format( - "In Row %s:the Column %s:is %s", - errorDetails.getRowId() + 1, - errorDetails.getHeader(), - errorDetails.getErrorEnum().getValue())); - }); - throw new ProjectCommonException( - ResponseCode.invalidRequestData.getErrorCode(), - ArrayUtils.stringifyContents(errors.toArray()), - ResponseCode.CLIENT_ERROR.getResponseCode()); - } -} diff --git a/service/src/main/java/org/sunbird/error/RowComparator.java b/service/src/main/java/org/sunbird/error/RowComparator.java deleted file mode 100644 index f46894272168ab17d5d60c6488b4f25a4c66db36..0000000000000000000000000000000000000000 --- a/service/src/main/java/org/sunbird/error/RowComparator.java +++ /dev/null @@ -1,10 +0,0 @@ -package org.sunbird.error; - -import java.util.Comparator; - -public class RowComparator implements Comparator<CsvRowErrorDetails> { - @Override - public int compare(CsvRowErrorDetails o1, CsvRowErrorDetails o2) { - return o1.rowId - o2.rowId; - } -} diff --git a/service/src/main/java/org/sunbird/error/factory/ErrorDispatcherFactory.java b/service/src/main/java/org/sunbird/error/factory/ErrorDispatcherFactory.java deleted file mode 100644 index 7d12d3772ac53d3f428a9fc493d6737306328b54..0000000000000000000000000000000000000000 --- a/service/src/main/java/org/sunbird/error/factory/ErrorDispatcherFactory.java +++ /dev/null @@ -1,49 +0,0 @@ -package org.sunbird.error.factory; - -import org.sunbird.error.CsvError; -import org.sunbird.error.CsvErrorDispatcher; -import org.sunbird.error.IErrorDispatcher; -import org.sunbird.error.ListErrorDispatcher; -import org.sunbird.keys.JsonKey; -import org.sunbird.logging.LoggerUtil; -import org.sunbird.util.PropertiesCache; - -/** - * this is error dispatcher factory class which will judge type of error need to show on the basis - * of error count. - * - * @author anmolgupta - */ -public class ErrorDispatcherFactory { - - private static LoggerUtil logger = new LoggerUtil(ErrorDispatcherFactory.class); - - /** this ERROR_VISUALIZATION_THRESHOLD will decide in which need to show errors */ - public static final int ERROR_VISUALIZATION_THRESHOLD = getErrorVisualizationThreshold(); - - /** - * this method will return the required error dispatcher class object - * - * @param error - * @return IErrorDispatcher - */ - public static IErrorDispatcher getErrorDispatcher(CsvError error) { - if (error.getErrorsList().size() > ERROR_VISUALIZATION_THRESHOLD) { - return CsvErrorDispatcher.getInstance(error); - } - return ListErrorDispatcher.getInstance(error); - } - - /** - * this method will return the ERROR_VISUALIZATION_THRESHOLD value - * - * @return int - */ - private static int getErrorVisualizationThreshold() { - String value = - PropertiesCache.getInstance().readProperty(JsonKey.ERROR_VISUALIZATION_THRESHOLD); - logger.info( - "ErrorDispatcherFactory:getErrorVisualizationThreshold:threshold got ".concat(value + "")); - return Integer.parseInt(value); - } -} diff --git a/service/src/main/java/org/sunbird/model/ShadowUser.java b/service/src/main/java/org/sunbird/model/ShadowUser.java index 40191f432b534bc01239cd6f135b26e8f1de7564..588fba2159152dabde77c608b3beb03e80d9b589 100644 --- a/service/src/main/java/org/sunbird/model/ShadowUser.java +++ b/service/src/main/java/org/sunbird/model/ShadowUser.java @@ -114,48 +114,48 @@ public class ShadowUser { @Override public String toString() { return "ShadowUser{" - + "claimedOn=" - + claimedOn - + ", claimStatus=" - + claimStatus - + ", createdOn=" - + createdOn - + ", email='" - + email - + '\'' - + ", name='" - + name - + '\'' - + ", orgExtId='" - + orgExtId - + '\'' - + ", phone='" - + phone - + '\'' - + ", processId='" - + processId - + '\'' - + ", updatedOn=" - + updatedOn - + ", userExtId='" - + userExtId - + '\'' - + ", userId='" - + userId - + '\'' - + ", userStatus=" - + userStatus - + ", userIds=" - + userIds - + ", channel='" - + channel - + '\'' - + ", addedBy='" - + addedBy - + '\'' - + ", attemptedCount=" - + attemptedCount - + '}'; + + "claimedOn=" + + claimedOn + + ", claimStatus=" + + claimStatus + + ", createdOn=" + + createdOn + + ", email='" + + email + + '\'' + + ", name='" + + name + + '\'' + + ", orgExtId='" + + orgExtId + + '\'' + + ", phone='" + + phone + + '\'' + + ", processId='" + + processId + + '\'' + + ", updatedOn=" + + updatedOn + + ", userExtId='" + + userExtId + + '\'' + + ", userId='" + + userId + + '\'' + + ", userStatus=" + + userStatus + + ", userIds=" + + userIds + + ", channel='" + + channel + + '\'' + + ", addedBy='" + + addedBy + + '\'' + + ", attemptedCount=" + + attemptedCount + + '}'; } public static class ShadowUserBuilder { @@ -262,4 +262,4 @@ public class ShadowUser { return shadowUser; } } -} +} \ No newline at end of file diff --git a/service/src/main/java/org/sunbird/model/ShadowUserUpload.java b/service/src/main/java/org/sunbird/model/ShadowUserUpload.java deleted file mode 100644 index ab001f16e26b099e78b97402e8ef6c36001a20fe..0000000000000000000000000000000000000000 --- a/service/src/main/java/org/sunbird/model/ShadowUserUpload.java +++ /dev/null @@ -1,169 +0,0 @@ -package org.sunbird.model; - -import org.sunbird.actor.user.validator.UserBulkMigrationRequestValidator; - -import java.util.Arrays; -import java.util.List; - -public class ShadowUserUpload { - - private String fileSize; - private List<String> headers; - private List<String> mappedHeaders; - private byte[] fileData; - private List<String> mandatoryFields; - private List<String> supportedFields; - private String processId; - private List<MigrationUser> values; - private List<SelfDeclaredUser> user; - - private ShadowUserUpload(ShadowUserUploadBuilder migrationBuilder) { - this.fileSize = migrationBuilder.fileSize; - this.headers = migrationBuilder.headers; - this.fileData = migrationBuilder.fileData; - this.mandatoryFields = migrationBuilder.mandatoryFields; - this.supportedFields = migrationBuilder.supportedFields; - this.processId = migrationBuilder.processId; - this.values = migrationBuilder.values; - this.user = migrationBuilder.user; - this.mappedHeaders = migrationBuilder.mappedHeaders; - } - - public String getFileSize() { - return fileSize; - } - - public List<String> getHeaders() { - return headers; - } - - public byte[] getFileData() { - return fileData; - } - - public List<String> getMandatoryFields() { - return mandatoryFields; - } - - public List<String> getSupportedFields() { - return supportedFields; - } - - public List<MigrationUser> getValues() { - return values; - } - - public List<SelfDeclaredUser> getUser() { - return user; - } - - public List<String> getMappedHeaders() { - return mappedHeaders; - } - - @Override - public String toString() { - return "ShadowUserUpload{" - + "fileSize='" - + fileSize - + '\'' - + ", headers=" - + headers - + ", mappedHeaders=" - + mappedHeaders - + ", fileData=" - + Arrays.toString(fileData) - + ", mandatoryFields=" - + mandatoryFields - + ", supportedFields=" - + supportedFields - + ", processId='" - + processId - + '\'' - + ", values=" - + values - + '}'; - } - - public String getProcessId() { - return processId; - } - - public static class ShadowUserUploadBuilder { - - private String fileSize; - private List<String> headers; - private byte[] fileData; - private List<String> mandatoryFields; - private List<String> supportedFields; - private String processId; - private List<MigrationUser> values; - private List<SelfDeclaredUser> user; - private List<String> mappedHeaders; - - public ShadowUserUploadBuilder() {} - - public ShadowUserUploadBuilder setFileSize(String fileSize) { - this.fileSize = fileSize; - return this; - } - - public ShadowUserUploadBuilder setHeaders(List<String> headers) { - this.headers = headers; - return this; - } - - public ShadowUserUploadBuilder setFileData(byte[] fileData) { - this.fileData = fileData; - return this; - } - - public ShadowUserUploadBuilder setMandatoryFields(List<String> mandatoryFields) { - mandatoryFields.replaceAll(String::toLowerCase); - this.mandatoryFields = mandatoryFields; - return this; - } - - public ShadowUserUploadBuilder setSupportedFields(List<String> supportedFields) { - supportedFields.replaceAll(String::toLowerCase); - this.supportedFields = supportedFields; - return this; - } - - public ShadowUserUploadBuilder setProcessId(String processId) { - this.processId = processId; - return this; - } - - public ShadowUserUploadBuilder setValues(List<MigrationUser> values) { - this.values = values; - return this; - } - - public ShadowUserUploadBuilder setUserValues(List<SelfDeclaredUser> values) { - this.user = values; - return this; - } - - public ShadowUserUploadBuilder setMappedHeaders(List<String> mappedHeaders) { - this.mappedHeaders = mappedHeaders; - return this; - } - - public ShadowUserUpload validate() { - ShadowUserUpload migration = new ShadowUserUpload(this); - validate(migration); - return migration; - } - - public ShadowUserUpload validateDeclaredUsers() { - ShadowUserUpload migration = new ShadowUserUpload(this); - UserBulkMigrationRequestValidator.getInstance(migration).validateDeclaredUsers(); - return migration; - } - - private void validate(ShadowUserUpload migration) { - UserBulkMigrationRequestValidator.getInstance(migration).validate(); - } - } -} diff --git a/service/src/main/java/org/sunbird/quartzscheduler/BaseJob.java b/service/src/main/java/org/sunbird/quartzscheduler/BaseJob.java deleted file mode 100644 index ea49c1516f620139330d7d52b93187a519cbc346..0000000000000000000000000000000000000000 --- a/service/src/main/java/org/sunbird/quartzscheduler/BaseJob.java +++ /dev/null @@ -1,34 +0,0 @@ -package org.sunbird.quartzscheduler; - -import akka.actor.ActorRef; -import com.fasterxml.jackson.databind.ObjectMapper; -import java.util.HashSet; -import org.quartz.Job; -import org.sunbird.actor.service.SunbirdMWService; -import org.sunbird.cassandra.CassandraOperation; -import org.sunbird.common.factory.EsClientFactory; -import org.sunbird.common.inf.ElasticSearchService; -import org.sunbird.datasecurity.DecryptionService; -import org.sunbird.datasecurity.EncryptionService; -import org.sunbird.helper.ServiceFactory; -import org.sunbird.keys.JsonKey; -import org.sunbird.request.Request; -import org.sunbird.util.Util; - -/** @author Mahesh Kumar Gangula */ -public abstract class BaseJob implements Job { - - protected Util.DbInfo bulkUploadDbInfo = Util.dbInfoMap.get(JsonKey.BULK_OP_DB); - protected CassandraOperation cassandraOperation = ServiceFactory.getInstance(); - protected ObjectMapper mapper = new ObjectMapper(); - protected HashSet<String> verifiedChannelOrgExternalIdSet = new HashSet<>(); - protected ElasticSearchService elasticSearchService = EsClientFactory.getInstance(JsonKey.REST); - protected DecryptionService decryptionService = - org.sunbird.datasecurity.impl.ServiceFactory.getDecryptionServiceInstance(null); - protected EncryptionService encryptionService = - org.sunbird.datasecurity.impl.ServiceFactory.getEncryptionServiceInstance(null); - - public void tellToBGRouter(Request request) { - SunbirdMWService.tellToBGRouter(request, ActorRef.noSender()); - } -} diff --git a/service/src/main/java/org/sunbird/quartzscheduler/ChannelRegistrationScheduler.java b/service/src/main/java/org/sunbird/quartzscheduler/ChannelRegistrationScheduler.java deleted file mode 100644 index 54416147c0879081d8ffe46c9011c1d3ae95d0f4..0000000000000000000000000000000000000000 --- a/service/src/main/java/org/sunbird/quartzscheduler/ChannelRegistrationScheduler.java +++ /dev/null @@ -1,49 +0,0 @@ -package org.sunbird.quartzscheduler; - -import java.util.Calendar; -import java.util.List; -import java.util.Map; -import org.apache.commons.lang3.StringUtils; -import org.quartz.JobExecutionContext; -import org.sunbird.actor.BackgroundOperations; -import org.sunbird.keys.JsonKey; -import org.sunbird.logging.LoggerUtil; -import org.sunbird.request.Request; -import org.sunbird.response.Response; - -public class ChannelRegistrationScheduler extends BaseJob { - private static LoggerUtil logger = new LoggerUtil(ChannelRegistrationScheduler.class); - - @Override - public void execute(JobExecutionContext ctx) { - logger.info( - "ChannelRegistrationScheduler:execute: Running channel registration Scheduler Job at: " - + Calendar.getInstance().getTime() - + " triggered by: " - + ctx.getJobDetail().toString()); - Request request = new Request(); - request.setOperation(BackgroundOperations.registerChannel.name()); - Response response = - cassandraOperation.getRecordById( - JsonKey.SUNBIRD, JsonKey.SYSTEM_SETTINGS_DB, JsonKey.CHANNEL_REG_STATUS_ID, null); - List<Map<String, Object>> responseList = - (List<Map<String, Object>>) response.get(JsonKey.RESPONSE); - if (null != responseList && !responseList.isEmpty()) { - Map<String, Object> resultMap = responseList.get(0); - logger.info( - "value for CHANNEL_REG_STATUS_ID (003) from SYSTEM_SETTINGS_DB is : " - + (String) resultMap.get(JsonKey.VALUE)); - if (StringUtils.isBlank((String) resultMap.get(JsonKey.VALUE)) - && !Boolean.parseBoolean((String) resultMap.get(JsonKey.VALUE))) { - logger.info( - "calling ChannelRegistrationActor from ChannelRegistrationScheduler execute method."); - tellToBGRouter(request); - } - } else { - logger.info( - "calling ChannelRegistrationActor from ChannelRegistrationScheduler execute method, " - + "entry for CHANNEL_REG_STATUS_ID (003) is null."); - tellToBGRouter(request); - } - } -} diff --git a/service/src/main/java/org/sunbird/quartzscheduler/OnDemandSchedulerManager.java b/service/src/main/java/org/sunbird/quartzscheduler/OnDemandSchedulerManager.java deleted file mode 100644 index 7d6ecc6596e5c8892a821e995cb9283a18e772f5..0000000000000000000000000000000000000000 --- a/service/src/main/java/org/sunbird/quartzscheduler/OnDemandSchedulerManager.java +++ /dev/null @@ -1,83 +0,0 @@ -package org.sunbird.quartzscheduler; - -import static org.quartz.SimpleScheduleBuilder.simpleSchedule; - -import java.util.HashMap; -import java.util.Map; -import org.quartz.*; -import org.sunbird.logging.LoggerUtil; - -public class OnDemandSchedulerManager extends SchedulerManager { - private static LoggerUtil logger = new LoggerUtil(OnDemandSchedulerManager.class); - private static OnDemandSchedulerManager onDemandSchedulerManager = null; - private static final String BULK_UPLOAD = "bulkupload"; - private static final String SHADOW_USER = "shadowuser"; - - public static Map<String, String> schedulerMap = new HashMap<>(); - - public OnDemandSchedulerManager() { - if (schedulerMap.isEmpty()) { - schedulerMap.put(BULK_UPLOAD, "uploadVerify"); - schedulerMap.put(SHADOW_USER, "migrateShadowUser"); - } - } - - public void scheduleOnDemand(String identifier, String job) { - logger.info("onDemandSchedulerManager:scheduleOnDemand:" + job + " started"); - JobDetail jobDetail = null; - JobBuilder jobBuilder = null; - String jobName = schedulerMap.get(job); - if (job.equals(SHADOW_USER)) { - jobBuilder = JobBuilder.newJob(ShadowUserMigrationScheduler.class); - } else if (job.equals(BULK_UPLOAD)) { - jobBuilder = JobBuilder.newJob(UploadLookUpScheduler.class); - } - jobDetail = - jobBuilder - .requestRecovery(true) - .withDescription("Scheduler for migrating shadow user ") - .withIdentity(jobName + "Scheduler", identifier) - .build(); - Trigger trigger = - TriggerBuilder.newTrigger() - .withIdentity(jobName + "Trigger", identifier) - .startNow() - .withSchedule(simpleSchedule().withIntervalInSeconds(1).withRepeatCount(0)) - .build(); - try { - if (scheduler.checkExists(jobDetail.getKey())) { - scheduler.deleteJob(jobDetail.getKey()); - } - scheduler.scheduleJob(jobDetail, trigger); - scheduler.start(); - logger.info("onDemandSchedulerManager:scheduleOnDemand:scheduler ended"); - } catch (Exception e) { - logger.error("onDemandSchedulerManager:scheduleOnDemand Error occurred " + e.getMessage(), e); - } - } - - public static OnDemandSchedulerManager getInstance() { - if (onDemandSchedulerManager == null) { - synchronized (OnDemandSchedulerManager.class) { - if (onDemandSchedulerManager == null) { - onDemandSchedulerManager = new OnDemandSchedulerManager(); - } - } - } - return onDemandSchedulerManager; - } - - public void triggerScheduler(String[] jobs) { - String identifier = "NetOps-PC1502295457753"; - for (String job : jobs) { - switch (job) { - case BULK_UPLOAD: - case SHADOW_USER: - scheduleOnDemand(identifier, job); - break; - default: - logger.info("OnDemandSchedulerManager:triggerScheduler: There is no such job"); - } - } - } -} diff --git a/service/src/main/java/org/sunbird/quartzscheduler/SchedulerManager.java b/service/src/main/java/org/sunbird/quartzscheduler/SchedulerManager.java deleted file mode 100644 index ec0eb10acbca531f02074b2e2d462a968674b5af..0000000000000000000000000000000000000000 --- a/service/src/main/java/org/sunbird/quartzscheduler/SchedulerManager.java +++ /dev/null @@ -1,248 +0,0 @@ -package org.sunbird.quartzscheduler; - -import java.io.IOException; -import java.io.InputStream; -import java.util.Properties; -import org.apache.commons.lang3.StringUtils; -import org.quartz.CronScheduleBuilder; -import org.quartz.JobBuilder; -import org.quartz.JobDetail; -import org.quartz.Scheduler; -import org.quartz.SchedulerException; -import org.quartz.SimpleScheduleBuilder; -import org.quartz.Trigger; -import org.quartz.TriggerBuilder; -import org.quartz.impl.StdSchedulerFactory; -import org.sunbird.keys.JsonKey; -import org.sunbird.logging.LoggerUtil; -import org.sunbird.util.PropertiesCache; - -/** - * This class will manage all the Quartz scheduler. We need to call the schedule method at one time. - * we are calling this method from Util.java class. - * - * @author Manzarul - */ -public class SchedulerManager { - private static LoggerUtil logger = new LoggerUtil(SchedulerManager.class); - - private static final String FILE = "quartz.properties"; - public static Scheduler scheduler = null; - private static SchedulerManager schedulerManager = null; - - public SchedulerManager() { - schedule(); - } - - /** This method will register the quartz scheduler job. */ - public void schedule() { - logger.info( - "SchedulerManager:schedule: Call to start scheduler jobs - org.sunbird.common.quartz.scheduler.SchedulerManager"); - - try { - Thread.sleep(240000); - boolean isEmbedded = false; - Properties configProp = null; - String embeddVal = System.getenv(JsonKey.SUNBIRD_QUARTZ_MODE); - if (JsonKey.EMBEDDED.equalsIgnoreCase(embeddVal)) { - isEmbedded = true; - } else { - configProp = setUpClusterMode(); - } - if (!isEmbedded && configProp != null) { - - StdSchedulerFactory schedulerFactory = new StdSchedulerFactory(configProp); - logger.info("Quartz scheduler is running in cluster mode."); - scheduler = schedulerFactory.getScheduler("MyScheduler"); - - if (null == scheduler) { - Thread.sleep(5000); - scheduler = schedulerFactory.getScheduler(); - } - - String schedulerName = scheduler.getSchedulerName(); - logger.info( - "Quartz scheduler is running in cluster mode. scheduler Name is: " + schedulerName); - } else { - logger.info("Quartz scheduler is running in embedded mode."); - scheduler = new StdSchedulerFactory().getScheduler(); - } - String identifier = "NetOps-PC1502295457753"; - - scheduleBulkUploadJob(identifier); - scheduleChannelReg(identifier); - scheduleShadowUser(identifier); - - } catch (Exception e) { - logger.error( - "SchedulerManager:schedule: Error in starting scheduler jobs - org.sunbird.common.quartz.scheduler.SchedulerManager " - + e.getMessage(), - e); - } finally { - registerShutDownHook(); - } - logger.info( - "SchedulerManager:schedule: started scheduler jobs - org.sunbird.common.quartz.scheduler.SchedulerManager"); - } - - public static void scheduleChannelReg(String identifier) { - // add another job for registering channel to ekstep. - // 1- create a job and bind with class which is implementing Job - // interface. - JobDetail channelRegistrationJob = - JobBuilder.newJob(ChannelRegistrationScheduler.class) - .requestRecovery(true) - .withDescription("Scheduler for channel registration") - .withIdentity("channelRegistrationScheduler", identifier) - .build(); - - // 2- Create a trigger object that will define frequency of run. - // It will run only once after server startup - Trigger channelRegistrationTrigger = - TriggerBuilder.newTrigger() - .withIdentity("channelRegistrationScheduler", identifier) - .withSchedule(SimpleScheduleBuilder.repeatMinutelyForTotalCount(1)) - .build(); - try { - if (scheduler.checkExists(channelRegistrationJob.getKey())) { - scheduler.deleteJob(channelRegistrationJob.getKey()); - } - scheduler.scheduleJob(channelRegistrationJob, channelRegistrationTrigger); - scheduler.start(); - logger.info("SchedulerManager:scheduleChannelReg: channelRegistration schedular started"); - } catch (Exception e) { - logger.error(e.getMessage(), e); - } - } - - private void scheduleBulkUploadJob(String identifier) { - // add another job for verifying the bulk upload part. - // 1- create a job and bind with class which is implementing Job - // interface. - JobDetail uploadVerifyJob = - JobBuilder.newJob(UploadLookUpScheduler.class) - .requestRecovery(true) - .withDescription("Scheduler for bulk upload retry") - .withIdentity("uploadVerifyScheduler", identifier) - .build(); - - // 2- Create a trigger object that will define frequency of run. - // This will run every day 4:30 AM and in UTC 11 PM - Trigger uploadTrigger = - TriggerBuilder.newTrigger() - .withIdentity("uploadVerifyTrigger", identifier) - .withSchedule( - CronScheduleBuilder.cronSchedule( - PropertiesCache.getInstance().getProperty("quartz_upload_timer"))) - .build(); - try { - if (scheduler.checkExists(uploadVerifyJob.getKey())) { - scheduler.deleteJob(uploadVerifyJob.getKey()); - } - scheduler.scheduleJob(uploadVerifyJob, uploadTrigger); - scheduler.start(); - logger.info( - "SchedulerManager:scheduleBulkUploadJob: UploadLookUpScheduler schedular started"); - } catch (Exception e) { - logger.error(e.getMessage(), e); - } - } - - /** - * This method will do the Quartz scheduler set up in cluster mode. - * - * @return Properties - * @throws IOException - */ - public Properties setUpClusterMode() throws IOException { - Properties configProp = new Properties(); - InputStream in = this.getClass().getClassLoader().getResourceAsStream(FILE); - String host = System.getenv(JsonKey.SUNBIRD_PG_HOST); - String port = System.getenv(JsonKey.SUNBIRD_PG_PORT); - String db = System.getenv(JsonKey.SUNBIRD_PG_DB); - String username = System.getenv(JsonKey.SUNBIRD_PG_USER); - String password = System.getenv(JsonKey.SUNBIRD_PG_PASSWORD); - if (!StringUtils.isBlank(host) - && !StringUtils.isBlank(port) - && !StringUtils.isBlank(db) - && !StringUtils.isBlank(username) - && !StringUtils.isBlank(password)) { - logger.info("Taking Postgres value from Environment variable..."); - configProp.load(in); - configProp.put( - "org.quartz.dataSource.MySqlDS.URL", "jdbc:postgresql://" + host + ":" + port + "/" + db); - configProp.put("org.quartz.dataSource.MySqlDS.user", username); - configProp.put("org.quartz.dataSource.MySqlDS.password", password); - configProp.put("org.quartz.scheduler.instanceName", "MyScheduler"); - logger.info( - "SchedulerManager:setUpClusterMode: Connection is established from environment variable"); - } else { - logger.info( - "SchedulerManager:setUpClusterMode: Environment variable is not set for postgres SQl."); - configProp = null; - } - return configProp; - } - - public static SchedulerManager getInstance() { - if (schedulerManager != null) { - return schedulerManager; - } else { - schedulerManager = new SchedulerManager(); - } - return schedulerManager; - } - - /** - * This class will be called by registerShutDownHook to register the call inside jvm , when jvm - * terminate it will call the run method to clean up the resource. - * - * @author Manzarul - */ - static class ResourceCleanUp extends Thread { - @Override - public void run() { - try { - scheduler.shutdown(); - } catch (SchedulerException e) { - logger.error(e.getMessage(), e); - } - } - } - - /** Register the hook for resource clean up. this will be called when jvm shut down. */ - public static void registerShutDownHook() { - Runtime runtime = Runtime.getRuntime(); - runtime.addShutdownHook(new ResourceCleanUp()); - } - - private void scheduleShadowUser(String identifier) { - logger.info("SchedulerManager:scheduleShadowUser:scheduleShadowUser scheduler started"); - logger.info( - "SchedulerManager:scheduleShadowUser:scheduleShadowUser scheduler started second log"); - JobDetail migrateShadowUserJob = - JobBuilder.newJob(ShadowUserMigrationScheduler.class) - .requestRecovery(true) - .withDescription("Scheduler for migrating shadow user ") - .withIdentity("migrateShadowUserScheduler", identifier) - .build(); - String shadowUserTime = - PropertiesCache.getInstance().getProperty("quartz_shadow_user_migration_timer"); - logger.info("SchedulerManager:scheduleShadowUser: schedule time is : " + shadowUserTime); - Trigger migrateShadowUserTrigger = - TriggerBuilder.newTrigger() - .withIdentity("migrateShadowUserTrigger", identifier) - .withSchedule(CronScheduleBuilder.cronSchedule(shadowUserTime)) - .build(); - try { - if (scheduler.checkExists(migrateShadowUserJob.getKey())) { - scheduler.deleteJob(migrateShadowUserJob.getKey()); - } - scheduler.scheduleJob(migrateShadowUserJob, migrateShadowUserTrigger); - scheduler.start(); - logger.info("SchedulerManager:scheduleShadowUser:scheduleShadowUser scheduler ended"); - } catch (Exception e) { - logger.error("SchedulerManager:scheduleShadowUser Error occurred " + e.getMessage(), e); - } - } -} diff --git a/service/src/main/java/org/sunbird/quartzscheduler/ShadowUserMigrationScheduler.java b/service/src/main/java/org/sunbird/quartzscheduler/ShadowUserMigrationScheduler.java deleted file mode 100644 index 56c55a9812bea62880ed06fd5afc0eea604f5f45..0000000000000000000000000000000000000000 --- a/service/src/main/java/org/sunbird/quartzscheduler/ShadowUserMigrationScheduler.java +++ /dev/null @@ -1,469 +0,0 @@ -package org.sunbird.quartzscheduler; - -import com.fasterxml.jackson.core.type.TypeReference; -import java.sql.Timestamp; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Calendar; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.WeakHashMap; -import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.collections.MapUtils; -import org.apache.commons.lang3.StringUtils; -import org.quartz.JobExecutionContext; -import org.sunbird.common.ElasticSearchHelper; -import org.sunbird.dto.SearchDTO; -import org.sunbird.keys.JsonKey; -import org.sunbird.logging.LoggerUtil; -import org.sunbird.model.ClaimStatus; -import org.sunbird.model.MigrationUser; -import org.sunbird.model.ShadowUser; -import org.sunbird.model.bulkupload.BulkMigrationUser; -import org.sunbird.response.Response; -import org.sunbird.util.ProjectUtil; - -public class ShadowUserMigrationScheduler extends BaseJob { - private static LoggerUtil logger = new LoggerUtil(ShadowUserMigrationScheduler.class); - - @Override - public void execute(JobExecutionContext jobExecutionContext) { - logger.info( - "ShadowUserMigrationScheduler:execute:Running Shadow User Upload Scheduler Job at: " - + Calendar.getInstance().getTime() - + " triggered by: " - + jobExecutionContext.getJobDetail().toString()); - // startMigration(); - } - - public void startMigration() { - List<String> unprocessedRecordIds = getUnprocessedRecordIds(); - logger.info( - "ShadowUserMigrationScheduler:startMigration:Got Bulk Upload Db unprocessed and failed records size is:" - + unprocessedRecordIds.size()); - processRecords(unprocessedRecordIds); - ShadowUserProcessor processorObject = new ShadowUserProcessor(); - processorObject.process(null); - unprocessedRecordIds.clear(); - logger.info( - "ShadowUserMigrationScheduler:execute:Scheduler Job ended for shadow user migration"); - } - - /** - * - fetch rows from bulk upload table whose status is less than 2 - update the bulk upload table - * row status to processing - encrypting email and phone - start processing single row - fetch the - * single row data - Get List of Migration(csv) user from the row - process each Migration(csv) - * user - update the bulk upload table row status to completed and add message to success - if - * fails update the bulk upload row status to failed and add failureResult - */ - private void processRecords(List<String> unprocessedRecordIds) { - logger.info( - "ShadowUserMigrationScheduler:processRecords:Scheduler Job Started for ShadowUser Migration"); - unprocessedRecordIds - .stream() - .forEach( - id -> { - Map<String, Object> row = getFullRecordFromProcessId(id); - BulkMigrationUser bulkMigrationUser = convertRowToObject(row); - row.clear(); - try { - updateStatusInUserBulkTable( - bulkMigrationUser.getId(), - ProjectUtil.BulkProcessStatus.IN_PROGRESS.getValue()); - List<MigrationUser> migrationUserList = getMigrationUserAsList(bulkMigrationUser); - migrationUserList - .parallelStream() - .forEach( - singleMigrationUser -> { - encryptEmailAndPhone(singleMigrationUser); - processSingleMigUser( - bulkMigrationUser.getCreatedBy(), - bulkMigrationUser.getId(), - singleMigrationUser); - }); - updateMessageInBulkUserTable( - bulkMigrationUser.getId(), JsonKey.SUCCESS_RESULT, JsonKey.SUCCESS); - } catch (Exception e) { - logger.error("ShadowUserMigrationScheduler:processRecords:error occurred ", e); - updateMessageInBulkUserTable( - bulkMigrationUser.getId(), JsonKey.FAILURE_RESULT, e.getMessage()); - } - }); - logger.info("ShadowUserMigrationScheduler:processRecords:started stage3_____"); - } - - private void processSingleMigUser( - String createdBy, String processId, MigrationUser singleMigrationUser) { - logger.info( - "ShadowUserMigrationScheduler:processSingleMigUser:Single migration User Started processing with processId:" - + processId); - Map<String, Object> existingUserDetails = - getShadowExistingUserDetails( - singleMigrationUser.getChannel(), singleMigrationUser.getUserExternalId()); - if (MapUtils.isEmpty(existingUserDetails)) { - logger.info( - "ShadowUserMigrationScheduler:processSingleMigUser:existing user not found with processId:" - + processId); - insertShadowUserToDb(createdBy, processId, singleMigrationUser); - } else { - logger.info( - "ShadowUserMigrationScheduler:processSingleMigUser:existing user found with processId:" - + processId); - ShadowUser shadowUser = mapper.convertValue(existingUserDetails, ShadowUser.class); - updateUser(processId, singleMigrationUser, shadowUser); - } - } - - /** - * this method will read rows from the bulk_upload_process table who has status less than 2 - * - * @return list - */ - private List<String> getUnprocessedRecordIds() { - Response response = new Response(); // Will depricate these api with SC-2169 - /* cassandraOperation.getRecordByObjectType( - bulkUploadDbInfo.getKeySpace(), - bulkUploadDbInfo.getTableName(), - JsonKey.ID, - JsonKey.STATUS, - ProjectUtil.BulkProcessStatus.INTERRUPT.getValue(), - JsonKey.MIGRATION_USER_OBJECT, - null);*/ - List<Map<String, Object>> result = new ArrayList<>(); - if (!((List) response.getResult().get(JsonKey.RESPONSE)).isEmpty()) { - result = ((List) response.getResult().get(JsonKey.RESPONSE)); - } - List<String> processIds = new ArrayList<>(); - result - .stream() - .forEach( - rowMap -> { - processIds.add((String) rowMap.get(JsonKey.ID)); - }); - logger.info( - "ShadowUserMigrationScheduler:getUnprocessedRecordIds:got rows from Bulk user table is:"); - return processIds; - } - - private Map<String, Object> getFullRecordFromProcessId(String processId) { - - int FIRST_RECORD = 0; - Response response = - cassandraOperation.getRecordById( - bulkUploadDbInfo.getKeySpace(), bulkUploadDbInfo.getTableName(), processId, null); - List<Map<String, Object>> result = new ArrayList<>(); - if (!((List) response.getResult().get(JsonKey.RESPONSE)).isEmpty()) { - result = ((List) response.getResult().get(JsonKey.RESPONSE)); - } - logger.info( - "ShadowUserMigrationScheduler:getFullRecordFromProcessId:got single row data from bulk_upload_process with processId:" - + processId); - return result.get(FIRST_RECORD); - } - - private BulkMigrationUser convertRowToObject(Map<String, Object> row) { - BulkMigrationUser bulkMigrationUser = null; - try { - bulkMigrationUser = mapper.convertValue(row, BulkMigrationUser.class); - } catch (Exception e) { - logger.error( - "ShadowUserMigrationScheduler:convertMapToMigrationObject:error occurred while converting map to pojo", - e); - } - return bulkMigrationUser; - } - - private List<MigrationUser> getMigrationUserAsList(BulkMigrationUser bulkMigrationUser) { - List<MigrationUser> migrationUserList = new ArrayList<>(); - try { - String decryptedData = decryptionService.decryptData(bulkMigrationUser.getData(), null); - migrationUserList = - mapper.readValue(decryptedData, new TypeReference<List<MigrationUser>>() {}); - } catch (Exception e) { - logger.error( - "ShadowUserMigrationScheduler:getMigrationUserAsList:error occurred while converting map to POJO: ", - e); - } - return migrationUserList; - } - - private Map<String, Object> getShadowExistingUserDetails(String channel, String userExtId) { - Map<String, Object> propertiesMap = new WeakHashMap<>(); - propertiesMap.put(JsonKey.CHANNEL, channel); - propertiesMap.put("userExtId", userExtId); - Map<String, Object> result = new HashMap<>(); - Response response = - cassandraOperation.getRecordsByProperties( - JsonKey.SUNBIRD, JsonKey.SHADOW_USER, propertiesMap, null); - if (!((List) response.getResult().get(JsonKey.RESPONSE)).isEmpty()) { - result = ((Map) ((List) response.getResult().get(JsonKey.RESPONSE)).get(0)); - } - return result; - } - - /** - * this method will prepare the shawdow user object and insert the record into shawdow_user table - * - * @param createdBy - * @param processId - * @param migrationUser - */ - private void insertShadowUserToDb( - String createdBy, String processId, MigrationUser migrationUser) { - Map<String, Object> dbMap = new WeakHashMap<>(); - dbMap.put(JsonKey.USER_EXT_ID, migrationUser.getUserExternalId()); - dbMap.put(JsonKey.ORG_EXT_ID, migrationUser.getOrgExternalId()); - logger.info( - "ShadowUserMigrationScheduler:insertShadowUser: email got " - + migrationUser.getEmail() - + " " - + migrationUser.getPhone()); - dbMap.put(JsonKey.EMAIL, migrationUser.getEmail()); - dbMap.put(JsonKey.PHONE, migrationUser.getPhone()); - dbMap.put(JsonKey.ADDED_BY, createdBy); - dbMap.put(JsonKey.CHANNEL, migrationUser.getChannel()); - dbMap.put(JsonKey.NAME, migrationUser.getName()); - dbMap.put(JsonKey.PROCESS_ID, processId); - dbMap.put(JsonKey.ATTEMPTED_COUNT, 0); - dbMap.put(JsonKey.CLAIM_STATUS, ClaimStatus.UNCLAIMED.getValue()); - dbMap.put(JsonKey.USER_STATUS, getInputStatus(migrationUser.getInputStatus())); - dbMap.put(JsonKey.CREATED_ON, new Timestamp(System.currentTimeMillis())); - if (!isOrgExternalIdValid(migrationUser)) { - dbMap.put(JsonKey.CLAIM_STATUS, ClaimStatus.ORGEXTERNALIDMISMATCH.getValue()); - } - Response response = - cassandraOperation.insertRecord(JsonKey.SUNBIRD, JsonKey.SHADOW_USER, dbMap, null); - dbMap.clear(); - logger.info("ShadowUserMigrationScheduler:insertShadowUser: record status in cassandra "); - } - - private int getInputStatus(String inputStatus) { - if (inputStatus.equalsIgnoreCase(JsonKey.ACTIVE)) { - return ProjectUtil.Status.ACTIVE.getValue(); - } - return ProjectUtil.Status.INACTIVE.getValue(); - } - - private void updateUser(String processId, MigrationUser migrationUser, ShadowUser shadowUser) { - updateUserInShadowDb(processId, migrationUser, shadowUser); - } - - /** - * @param processId - * @param migrationUser - * @param existingShadowUser - */ - private void updateUserInShadowDb( - String processId, MigrationUser migrationUser, ShadowUser existingShadowUser) { - int currentClaimStatus = existingShadowUser.getClaimStatus(); - int newClaimStatus = currentClaimStatus; - boolean isClaimed = (currentClaimStatus == ClaimStatus.CLAIMED.getValue()); - boolean isNotSame = !isSame(existingShadowUser, migrationUser); - - if (isNotSame && !isOrgExternalIdValid(migrationUser)) { - newClaimStatus = ClaimStatus.ORGEXTERNALIDMISMATCH.getValue(); - } else if (!isClaimed) { - // Allow all non-claimed users another chance - newClaimStatus = ClaimStatus.UNCLAIMED.getValue(); - } - - if (isNotSame || (currentClaimStatus != newClaimStatus)) { - Map<String, Object> propertiesMap = populateUserMap(processId, migrationUser); - - propertiesMap.put(JsonKey.CLAIM_STATUS, newClaimStatus); - propertiesMap.put( - JsonKey.ATTEMPTED_COUNT, - 0); // we wanted to reset the attempted count for the failed records - - Map<String, Object> compositeKeysMap = new HashMap<>(); - compositeKeysMap.put(JsonKey.CHANNEL, migrationUser.getChannel()); - compositeKeysMap.put(JsonKey.USER_EXT_ID, migrationUser.getUserExternalId()); - - cassandraOperation.updateRecord( - JsonKey.SUNBIRD, JsonKey.SHADOW_USER, propertiesMap, compositeKeysMap, null); - - if (isClaimed) { - logger.info("ShadowUserMigrationScheduler:updateUserInShadowDb: isClaimed "); - ShadowUser newShadowUser = getUpdatedShadowUser(compositeKeysMap); - new ShadowUserProcessor().processClaimedUser(newShadowUser, null); - } - } - - logger.info( - "ShadowUserMigrationScheduler:updateUserInShadowDb channel:" - + migrationUser.getChannel() - + " userExternalId: " - + migrationUser.getUserExternalId() - + " isNotSame: " - + isNotSame - + " newClaimStatus:" - + newClaimStatus); - } - - private Map<String, Object> populateUserMap(String processId, MigrationUser migrationUser) { - Map<String, Object> propertiesMap = new WeakHashMap<>(); - propertiesMap.put(JsonKey.EMAIL, migrationUser.getEmail()); - propertiesMap.put(JsonKey.PHONE, migrationUser.getPhone()); - propertiesMap.put(JsonKey.PROCESS_ID, processId); - propertiesMap.put(JsonKey.NAME, migrationUser.getName()); - propertiesMap.put(JsonKey.ORG_EXT_ID, migrationUser.getOrgExternalId()); - propertiesMap.put(JsonKey.UPDATED_ON, new Timestamp(System.currentTimeMillis())); - propertiesMap.put(JsonKey.USER_STATUS, getInputStatus(migrationUser.getInputStatus())); - return propertiesMap; - } - - /** - * this method will return the updated shadow user object when new orgExtId , name is been passed - * - * @param compositeKeysMap - * @return shawdow user - */ - private ShadowUser getUpdatedShadowUser(Map<String, Object> compositeKeysMap) { - Response response = - cassandraOperation.getRecordsByCompositeKey( - JsonKey.SUNBIRD, JsonKey.SHADOW_USER, compositeKeysMap, null); - logger.info( - "ShadowUserMigrationScheduler:getUpdatedShadowUser: record status in cassandra for getting the updated shawdow user object "); - Map<String, Object> resultmap = - ((List<Map<String, Object>>) response.getResult().get(JsonKey.RESPONSE)).get(0); - ShadowUser shadowUser = mapper.convertValue(resultmap, ShadowUser.class); - return shadowUser; - } - - private void updateStatusInUserBulkTable(String processId, int statusVal) { - try { - logger.info( - "ShadowUserMigrationScheduler:updateStatusInUserBulkTable: got status to change in bulk upload table" - .concat(statusVal + "") - + "with processId" - + processId); - Map<String, Object> propertiesMap = new WeakHashMap<>(); - propertiesMap.put(JsonKey.ID, processId); - propertiesMap.put(JsonKey.STATUS, statusVal); - updateBulkUserTable(propertiesMap); - } catch (Exception e) { - logger.error( - "ShadowUserMigrationScheduler:updateStatusInUserBulkTable: status update failed" - .concat(e + "") - + "with processId" - + processId, - e); - } - } - - private void updateBulkUserTable(Map<String, Object> propertiesMap) { - cassandraOperation.updateRecord( - bulkUploadDbInfo.getKeySpace(), bulkUploadDbInfo.getTableName(), propertiesMap, null); - logger.info("ShadowUserMigrationScheduler:updateBulkUserTable: status update result"); - } - - private void updateMessageInBulkUserTable(String processId, String key, String value) { - Map<String, Object> propertiesMap = new WeakHashMap<>(); - propertiesMap.put(JsonKey.ID, processId); - propertiesMap.put(key, value); - if (StringUtils.equalsIgnoreCase(value, JsonKey.SUCCESS)) { - propertiesMap.put(JsonKey.STATUS, ProjectUtil.BulkProcessStatus.COMPLETED.getValue()); - propertiesMap.put(JsonKey.FAILURE_RESULT, ""); - } else { - propertiesMap.put(JsonKey.SUCCESS_RESULT, ""); - propertiesMap.put(JsonKey.STATUS, ProjectUtil.BulkProcessStatus.FAILED.getValue()); - } - updateBulkUserTable(propertiesMap); - propertiesMap.clear(); - } - - /** - * this method will take descision wheather to update the record or not. - * - * @param shadowUser - * @param migrationUser - * @return boolean - */ - private boolean isSame(ShadowUser shadowUser, MigrationUser migrationUser) { - - if (StringUtils.isBlank(migrationUser.getOrgExternalId())) { - return false; - } - if (!shadowUser.getName().equalsIgnoreCase(migrationUser.getName())) { - return false; - } - if (!StringUtils.equalsIgnoreCase(shadowUser.getEmail(), migrationUser.getEmail())) { - return false; - } - if (!StringUtils.equalsIgnoreCase(shadowUser.getOrgExtId(), migrationUser.getOrgExternalId())) { - return false; - } - if (!StringUtils.equalsIgnoreCase(shadowUser.getPhone(), migrationUser.getPhone())) { - return false; - } - if ((getInputStatus(migrationUser.getInputStatus()) != shadowUser.getUserStatus())) { - return false; - } - - if (!migrationUser.getName().equalsIgnoreCase(shadowUser.getName())) { - return false; - } - return true; - } - - /** - * this method will check weather the provided orgExternalId is correct or not - * - * @param migrationUser - * @return true if correct else false - */ - private boolean isOrgExternalIdValid(MigrationUser migrationUser) { - if (StringUtils.isBlank(migrationUser.getOrgExternalId())) { - return true; - } else if (verifiedChannelOrgExternalIdSet.contains( - migrationUser.getChannel() + ":" + migrationUser.getOrgExternalId())) { - logger.info( - "ShadowUserMigrationScheduler:isOrgExternalIdValid: found orgexternalid in cache:" - + migrationUser.getOrgExternalId()); - return true; - } - Map<String, Object> request = new WeakHashMap<>(); - Map<String, Object> filters = new WeakHashMap<>(); - filters.put(JsonKey.EXTERNAL_ID, migrationUser.getOrgExternalId()); - filters.put(JsonKey.CHANNEL, migrationUser.getChannel()); - request.put(JsonKey.FILTERS, filters); - SearchDTO searchDTO = ElasticSearchHelper.createSearchDTO(request); - searchDTO.setFields(new ArrayList<>(Arrays.asList(JsonKey.ID))); - Map<String, Object> response = - (Map<String, Object>) - ElasticSearchHelper.getResponseFromFuture( - elasticSearchService.search( - searchDTO, ProjectUtil.EsType.organisation.getTypeName(), null)); - if (CollectionUtils.isNotEmpty((List<Map<String, Object>>) response.get(JsonKey.CONTENT))) { - verifiedChannelOrgExternalIdSet.add( - migrationUser.getChannel() + ":" + migrationUser.getOrgExternalId()); - response.clear(); - return true; - } - response.clear(); - return false; - } - - private String encryptValue(String key) { - try { - return encryptionService.encryptData(key, null); - } catch (Exception e) { - logger.error( - "ShadowUserMigrationScheduler:getEncryptedValue: error occurred in encrypting value " - + key, - e); - return key; - } - } - - private void encryptEmailAndPhone(MigrationUser migrationUser) { - - if (StringUtils.isNotBlank(migrationUser.getEmail())) { - migrationUser.setEmail(encryptValue(migrationUser.getEmail().toLowerCase())); - } - if (StringUtils.isNotBlank(migrationUser.getPhone())) { - migrationUser.setPhone(encryptValue(migrationUser.getPhone())); - } - } -} diff --git a/service/src/main/java/org/sunbird/quartzscheduler/ShadowUserProcessor.java b/service/src/main/java/org/sunbird/quartzscheduler/ShadowUserProcessor.java deleted file mode 100644 index ccf691836e2210fb27d2fd7e97b4354b68df509b..0000000000000000000000000000000000000000 --- a/service/src/main/java/org/sunbird/quartzscheduler/ShadowUserProcessor.java +++ /dev/null @@ -1,693 +0,0 @@ -package org.sunbird.quartzscheduler; - -import com.datastax.driver.core.ResultSet; -import com.datastax.driver.core.Row; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.google.common.util.concurrent.FutureCallback; -import java.util.*; -import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.collections.MapUtils; -import org.apache.commons.lang3.StringUtils; -import org.sunbird.cassandra.CassandraOperation; -import org.sunbird.common.CassandraUtil; -import org.sunbird.common.ElasticSearchHelper; -import org.sunbird.common.factory.EsClientFactory; -import org.sunbird.common.inf.ElasticSearchService; -import org.sunbird.dto.SearchDTO; -import org.sunbird.helper.ServiceFactory; -import org.sunbird.keys.JsonKey; -import org.sunbird.logging.LoggerUtil; -import org.sunbird.model.ClaimStatus; -import org.sunbird.model.ShadowUser; -import org.sunbird.request.RequestContext; -import org.sunbird.response.Response; -import org.sunbird.sso.SSOManager; -import org.sunbird.sso.SSOServiceFactory; -import org.sunbird.telemetry.util.TelemetryUtil; -import org.sunbird.util.ProjectUtil; -import org.sunbird.util.UserFlagEnum; -import org.sunbird.util.UserFlagUtil; -import org.sunbird.util.Util; -import org.sunbird.util.feed.FeedUtil; -import scala.concurrent.Future; - -public class ShadowUserProcessor { - private static LoggerUtil logger = new LoggerUtil(ShadowUserProcessor.class); - - private Util.DbInfo usrDbInfo = Util.dbInfoMap.get(JsonKey.USER_DB); - private CassandraOperation cassandraOperation = ServiceFactory.getInstance(); - private ObjectMapper mapper = new ObjectMapper(); - private Util.DbInfo bulkUploadDbInfo = Util.dbInfoMap.get(JsonKey.BULK_OP_DB); - private Map<String, String> extOrgIdMap = new HashMap<>(); - private String custodianOrgId; - private SSOManager keyCloakService = SSOServiceFactory.getInstance(); - private Map<String, Map<String, Object>> processIdtelemetryCtxMap = new HashMap<>(); - private ElasticSearchService elasticSearchService = EsClientFactory.getInstance(JsonKey.REST); - - public void process(RequestContext context) { - processAllUnclaimedUser(context); - logger.info( - context, "ShadowUserProcessor:process:successfully processed shadow user Stage3 ended"); - } - - private void processSingleShadowUser(ShadowUser shadowUser, RequestContext context) { - logger.info( - context, - "ShadowUserProcessor:processSingleShadowUser:started claiming shadow user with processId: " - + shadowUser.getProcessId()); - updateUser(shadowUser, context); - } - - /** - * this method will be called when the user is already claimed need to update the user - * - * @param shadowUser - */ - public void processClaimedUser(ShadowUser shadowUser, RequestContext context) { - logger.info( - context, - "ShadowUserProcessor:processClaimedUser:started claming shadow user with processId: " - + shadowUser.getProcessId()); - String orgId = getOrgId(shadowUser, context); - Map<String, Object> esUser = - (Map<String, Object>) - ElasticSearchHelper.getResponseFromFuture( - elasticSearchService.getDataByIdentifier( - ProjectUtil.EsType.user.getTypeName(), shadowUser.getUserId(), context)); - String userId = (String) esUser.get(JsonKey.ID); - String rootOrgId = (String) esUser.get(JsonKey.ROOT_ORG_ID); - logger.info( - context, - "ShadowUserProcessor:processClaimedUser:started: flag value got from es " - + esUser.get(JsonKey.FLAGS_VALUE)); - int flagsValue = - null != esUser.get(JsonKey.FLAGS_VALUE) - ? (int) esUser.get(JsonKey.FLAGS_VALUE) - : 0; // since we are migrating the user from custodian org to non custodian org. - logger.info(context, "ShadowUserProcessor:processClaimedUser:Got Flag Value " + flagsValue); - if (!((String) esUser.get(JsonKey.FIRST_NAME)).equalsIgnoreCase(shadowUser.getName()) - || ((int) esUser.get(JsonKey.STATUS)) != shadowUser.getUserStatus()) { - updateUserInUserTable(flagsValue, shadowUser.getUserId(), rootOrgId, shadowUser, context); - if (shadowUser.getUserStatus() == ProjectUtil.Status.INACTIVE.getValue()) { - deactivateUserFromKC(userId, context); - } - } - deleteUserFromOrganisations( - shadowUser, - rootOrgId, - (List<Map<String, Object>>) esUser.get(JsonKey.ORGANISATIONS), - context); - if (StringUtils.isNotBlank(orgId) && !getOrganisationIds(esUser).contains(orgId)) { - registerUserToOrg(userId, orgId, context); - } - syncUserToES(userId, context); - updateUserInShadowDb(userId, shadowUser, ClaimStatus.CLAIMED.getValue(), null, context); - } - - private void deactivateUserFromKC(String userId, RequestContext context) { - Map<String, Object> userMap = new HashMap<>(); - userMap.put(JsonKey.USER_ID, userId); - try { - logger.info( - context, - "ShadowUserProcessor:processClaimedUse:request Got to deactivate user account from KC:" - + userMap); - String status = keyCloakService.deactivateUser(userMap, context); - logger.info( - context, - "ShadowUserProcessor:processClaimedUse:deactivate user account from KC:" + status); - } catch (Exception e) { - logger.error( - context, - "ShadowUserProcessor:processClaimedUse:Error occurred while deactivate user account from KC:" - + userId, - e); - } - } - - private boolean isRootOrgMatchedWithOrgId(String rootOrgId, String orgId) { - if (StringUtils.equalsIgnoreCase(rootOrgId, orgId)) { - return true; - } - return false; - } - - private void deleteUserFromOrganisations( - ShadowUser shadowUser, - String rootOrgId, - List<Map<String, Object>> userOrg, - RequestContext context) { - userOrg - .stream() - .forEach( - organisation -> { - String orgId = (String) organisation.get(JsonKey.ORGANISATION_ID); - if (!isRootOrgMatchedWithOrgId(rootOrgId, orgId)) { - updateStatusInUserOrg(shadowUser, organisation, context); - } - }); - } - - private void updateStatusInUserOrg( - ShadowUser shadowUser, Map<String, Object> userOrg, RequestContext context) { - Map<String, Object> compositeKey = new LinkedHashMap<>(2); - compositeKey.put(JsonKey.USER_ID, userOrg.get(JsonKey.USER_ID)); - compositeKey.put(JsonKey.ORGANISATION_ID, userOrg.get(JsonKey.ORGANISATION_ID)); - Map<String, Object> request = new WeakHashMap<>(); - request.put(JsonKey.IS_DELETED, true); - request.put(JsonKey.UPDATED_BY, shadowUser.getAddedBy()); - request.put(JsonKey.UPDATED_DATE, ProjectUtil.getFormattedDate()); - - Response response = - cassandraOperation.updateRecord( - JsonKey.SUNBIRD, JsonKey.USER_ORG, request, compositeKey, context); - logger.info( - context, - "ShadowUserProcessor:updateStatusInUserOrg:response from cassandra in updating user org " - .concat(response + "")); - } - - private List<Map<String, Object>> getUserMatchedIdentifierFromES( - ShadowUser shadowUser, RequestContext context) { - Map<String, Object> request = new WeakHashMap<>(); - Map<String, Object> filters = new WeakHashMap<>(); - Map<String, Object> or = new WeakHashMap<>(); - if (StringUtils.isNotBlank(shadowUser.getEmail())) { - or.put(JsonKey.EMAIL, shadowUser.getEmail()); - } - if (StringUtils.isNotBlank(shadowUser.getPhone())) { - or.put(JsonKey.PHONE, shadowUser.getPhone()); - } - filters.put(JsonKey.ES_OR_OPERATION, or); - filters.put(JsonKey.ROOT_ORG_ID, getCustodianOrgId(context)); - request.put(JsonKey.FILTERS, filters); - logger.info( - context, - "ShadowUserProcessor:getUserMatchedIdentifierFromES:the filter prepared for elastic search with processId: " - + shadowUser.getProcessId() - + " :filters are:" - + filters); - SearchDTO searchDTO = ElasticSearchHelper.createSearchDTO(request); - searchDTO.setFields( - new ArrayList<String>( - Arrays.asList( - JsonKey.ID, - JsonKey.CHANNEL, - JsonKey.EMAIL, - JsonKey.PHONE, - JsonKey.ROOT_ORG_ID, - JsonKey.FLAGS_VALUE, - JsonKey.ORGANISATIONS, - JsonKey.IS_DELETED, - JsonKey.STATUS))); - Map<String, Object> response = - (Map<String, Object>) - ElasticSearchHelper.getResponseFromFuture( - elasticSearchService.search( - searchDTO, ProjectUtil.EsType.user.getTypeName(), context)); - logger.info( - context, - "ShadowUserProcessor:getUserMatchedIdentifierFromES:response got from elasticSearch is with processId: " - + shadowUser.getProcessId() - + " :response is" - + response); - return (List<Map<String, Object>>) response.get(JsonKey.CONTENT); - } - - /** - * this method will search in ES with phone/email, if multiple user's found(cond: if a user - * provide email of himself and phone of other user) it will mark user claimStatus to multimatch. - * else it will get the user_id from ES response and then it will fetch all the record from - * shadow_user table who are not claimed, rejected and failed with same user_id. if found multiple - * records after removing the users of same channel from multiMatchRecords (cond: to avoid - * changing of claimStatus to MULTIMATCH if respective user update happened )then will update - * respective and rest of the user's claimStatus to MULTIMATCH. else if filterRecords is empty - * then the respective user claimStatus will be marked as ELIGIBLE IN shadow_user table - * - * @param shadowUser - */ - private void updateUser(ShadowUser shadowUser, RequestContext context) { - List<Map<String, Object>> esUser = getUserMatchedIdentifierFromES(shadowUser, context); - logger.info( - context, - "ShadowUserProcessor:updateUser:GOT ES RESPONSE FOR USER WITH SIZE " + esUser.size()); - if (CollectionUtils.isNotEmpty(esUser)) { - if (esUser.size() == 1) { - logger.info( - context, - "ShadowUserProcessor:updateUser:Got single user:" - + esUser - + " :with processId" - + shadowUser.getProcessId()); - Map<String, Object> userMap = esUser.get(0); - if (!isSame(shadowUser, userMap, context)) { - logger.info( - context, - "ShadowUserProcessor:updateUser: provided user details doesn't match with existing user details with processId" - + shadowUser.getProcessId() - + userMap); - List<String> userIds = new ArrayList<>(); - userIds.add((String) userMap.get(JsonKey.ID)); - updateUserInShadowDb(null, shadowUser, ClaimStatus.ELIGIBLE.getValue(), userIds, context); - logger.info( - context, - "ShadowUserProcessor:updateUser: calling FeedUtil.saveFeed method for user id " - + userIds.get(0)); - FeedUtil.saveFeed(shadowUser, userIds, context); - } - } else if (esUser.size() > 1) { - logger.info(context, "ShadowUserProcessor:updateUser:GOT response from ES :" + esUser); - updateUserInShadowDb( - null, - shadowUser, - ClaimStatus.MULTIMATCH.getValue(), - getMatchingUserIds(esUser), - context); - } - } else { - logger.info( - context, "ShadowUserProcessor:updateUser:SKIPPING SHADOW USER:" + shadowUser.toString()); - } - esUser.clear(); - } - - private void generateTelemetry(String userId, String rootOrgId, ShadowUser shadowUser) { - logger.info( - "ShadowUserProcessor:generateTelemetry:generate telemetry:" + shadowUser.toString()); - Map<String, Object> targetObject = new HashMap<>(); - Map<String, String> rollUp = new HashMap<>(); - rollUp.put("l1", rootOrgId); - List<Map<String, Object>> correlatedObject = new ArrayList<>(); - TelemetryUtil.generateCorrelatedObject( - shadowUser.getProcessId(), JsonKey.PROCESS_ID, null, correlatedObject); - TelemetryUtil.generateTargetObject( - userId, StringUtils.capitalize(JsonKey.USER), JsonKey.MIGRATION_USER_OBJECT, null); - } - - /** - * this method will be used to get all the userIds got from ES response by searching with - * phone/email + custoRootOrgId - * - * @param esUser - * @return - */ - private List<String> getMatchingUserIds(List<Map<String, Object>> esUser) { - logger.info( - "ShadowUserProcessor:getMatchingUserIds:GOT response from counting matchingUserIds:" - + esUser.size()); - List<String> matchingUserIds = new ArrayList<>(); - esUser - .stream() - .forEach( - singleEsUser -> { - matchingUserIds.add((String) singleEsUser.get(JsonKey.ID)); - }); - return matchingUserIds; - } - - private void updateUserInUserTable( - int flagValue, - String userId, - String rootOrgId, - ShadowUser shadowUser, - RequestContext context) { - Map<String, Object> propertiesMap = new WeakHashMap<>(); - propertiesMap.put(JsonKey.FIRST_NAME, shadowUser.getName()); - propertiesMap.put(JsonKey.ID, userId); - if (!(UserFlagUtil.assignUserFlagValues(flagValue).get(JsonKey.STATE_VALIDATED))) { - logger.info(context, "ShadowUserProcessor:updateUserInUserTable: updating Flag Value"); - propertiesMap.put( - JsonKey.FLAGS_VALUE, flagValue + UserFlagEnum.STATE_VALIDATED.getUserFlagValue()); - } - propertiesMap.put(JsonKey.UPDATED_BY, shadowUser.getAddedBy()); - propertiesMap.put(JsonKey.UPDATED_DATE, ProjectUtil.getFormattedDate()); - if (shadowUser.getUserStatus() == ProjectUtil.Status.ACTIVE.getValue()) { - propertiesMap.put(JsonKey.IS_DELETED, false); - propertiesMap.put(JsonKey.STATUS, ProjectUtil.Status.ACTIVE.getValue()); - } else { - propertiesMap.put(JsonKey.IS_DELETED, true); - propertiesMap.put(JsonKey.STATUS, ProjectUtil.Status.INACTIVE.getValue()); - } - propertiesMap.put(JsonKey.CHANNEL, shadowUser.getChannel()); - propertiesMap.put(JsonKey.ROOT_ORG_ID, rootOrgId); - logger.info( - context, - "ShadowUserProcessor:updateUserInUserTable: properties map formed for user update: " - + propertiesMap); - Response response = - cassandraOperation.updateRecord( - usrDbInfo.getKeySpace(), usrDbInfo.getTableName(), propertiesMap, context); - logger.info( - context, - "ShadowUserProcessor:updateUserInUserTable:user is updated with shadow user:RESPONSE FROM CASSANDRA IS:" - + response.getResult()); - generateTelemetry(userId, rootOrgId, shadowUser); - } - - /** - * this method - * - * @return - */ - private String getCustodianOrgId(RequestContext context) { - if (StringUtils.isNotBlank(custodianOrgId)) { - logger.info( - context, - "ShadowUserProcessor:getCustodianOrgId:CUSTODIAN ORD ID FOUND in cache:" - + custodianOrgId); - return custodianOrgId; - } - Response response = - cassandraOperation.getRecordById( - JsonKey.SUNBIRD, JsonKey.SYSTEM_SETTINGS_DB, JsonKey.CUSTODIAN_ORG_ID, context); - List<Map<String, Object>> result = new ArrayList<>(); - if (!((List) response.getResult().get(JsonKey.RESPONSE)).isEmpty()) { - result = ((List) response.getResult().get(JsonKey.RESPONSE)); - Map<String, Object> resultMap = result.get(0); - custodianOrgId = (String) resultMap.get(JsonKey.VALUE); - logger.info( - context, - "ShadowUserProcessor:getCustodianOrgId:CUSTODIAN ORD ID FOUND in DB:" + custodianOrgId); - } - - if (StringUtils.isBlank(custodianOrgId)) { - logger.info( - context, - "ShadowUserProcessor:getCustodianOrgId:No CUSTODIAN ORD ID FOUND PLEASE HAVE THAT IN YOUR ENVIRONMENT"); - System.exit(-1); - } - return custodianOrgId; - } - - private FutureCallback<ResultSet> getSyncCallback(RequestContext context) { - return new FutureCallback<ResultSet>() { - @Override - public void onSuccess(ResultSet result) { - Map<String, String> columnMap = CassandraUtil.fetchColumnsMapping(result); - try { - Iterator<Row> resultIterator = result.iterator(); - while (resultIterator.hasNext()) { - Row row = resultIterator.next(); - Map<String, Object> doc = syncDataForEachRow(row, columnMap); - ShadowUser singleShadowUser = mapper.convertValue(doc, ShadowUser.class); - processSingleShadowUser(singleShadowUser, context); - logger.info( - context, - "ShadowUserProcessor:getSyncCallback:SUCCESS:SYNC CALLBACK SUCCESSFULLY PROCESSED for Shadow user: " - + singleShadowUser.toString()); - } - logger.info( - context, - "ShadowUserProcessor:getSyncCallback:SUCCESS:SYNC CALLBACK SUCCESSFULLY MIGRATED ALL Shadow user"); - - } catch (Exception e) { - logger.error( - context, - "ShadowUserProcessor:getSyncCallback:SUCCESS:ERROR OCCURRED WHILE GETTING SYNC CALLBACKS", - e); - } - } - - @Override - public void onFailure(Throwable t) { - logger.error( - "ShadowUserProcessor:getSyncCallback:FAILURE:ERROR OCCURRED WHILE GETTING SYNC CALLBACKS", - t); - } - }; - } - - private Map<String, Object> syncDataForEachRow(Row row, Map<String, String> columnMap) { - Map<String, Object> rowMap = new HashMap<>(); - columnMap - .entrySet() - .forEach( - entry -> { - Object value = row.getObject(entry.getValue()); - rowMap.put(entry.getKey(), value); - }); - logger.info("ShadowUserProcessor:syncDataForEachRow:row map returned " + rowMap); - return rowMap; - } - - private void processAllUnclaimedUser(RequestContext context) { - - logger.info( - context, - "ShadowUserProcessor:processAllUnclaimedUser:started processing all unclaimed user"); - getUnclaimedRowsFromShadowUserDb(context); - } - - private void getUnclaimedRowsFromShadowUserDb(RequestContext context) { - Map<String, Object> propertiesMap = new WeakHashMap<>(); - propertiesMap.put(JsonKey.CLAIM_STATUS, ClaimStatus.UNCLAIMED.getValue()); - cassandraOperation.applyOperationOnRecordsAsync( - JsonKey.SUNBIRD, - JsonKey.SHADOW_USER, - propertiesMap, - null, - getSyncCallback(context), - context); - propertiesMap.clear(); - } - - private boolean isSame( - ShadowUser shadowUser, Map<String, Object> esUserMap, RequestContext context) { - String orgId = getOrgId(shadowUser, context); - if (!shadowUser.getName().equalsIgnoreCase((String) esUserMap.get(JsonKey.FIRST_NAME))) { - return false; - } - if (StringUtils.isNotBlank(orgId) && !getOrganisationIds(esUserMap).contains(orgId)) { - return false; - } - if (shadowUser.getUserStatus() != (int) (esUserMap.get(JsonKey.STATUS))) { - return false; - } - if (StringUtils.isBlank(orgId)) { - return false; - } - return true; - } - - private void updateUserInShadowDb( - String userId, - ShadowUser shadowUser, - int claimStatus, - List<String> matchingUserIds, - RequestContext context) { - Map<String, Object> propertiesMap = new HashMap<>(); - propertiesMap.put(JsonKey.CLAIM_STATUS, claimStatus); - propertiesMap.put(JsonKey.PROCESS_ID, shadowUser.getProcessId()); - propertiesMap.put(JsonKey.USER_ID, userId); - propertiesMap.put(JsonKey.USER_IDs, matchingUserIds); - Map<String, Object> compositeKeysMap = new HashMap<>(); - compositeKeysMap.put(JsonKey.CHANNEL, shadowUser.getChannel()); - compositeKeysMap.put(JsonKey.USER_EXT_ID, shadowUser.getUserExtId()); - Response response = - cassandraOperation.updateRecord( - JsonKey.SUNBIRD, JsonKey.SHADOW_USER, propertiesMap, compositeKeysMap, context); - logger.info( - context, - "ShadowUserProcessor:updateUserInShadowDb:update:with processId: " - + shadowUser.getProcessId() - + " :and response is:" - + response); - } - - private String getOrgId(ShadowUser shadowUser, RequestContext context) { - if (StringUtils.isNotBlank(shadowUser.getOrgExtId())) { - String orgId = - extOrgIdMap.get(shadowUser.getChannel().concat(":").concat(shadowUser.getOrgExtId())); - if (StringUtils.isNotBlank(orgId)) { - return orgId; - } - Map<String, Object> request = new HashMap<>(); - Map<String, Object> filters = new HashMap<>(); - filters.put(JsonKey.EXTERNAL_ID, shadowUser.getOrgExtId().toLowerCase()); - filters.put(JsonKey.CHANNEL, shadowUser.getChannel()); - request.put(JsonKey.FILTERS, filters); - logger.info( - context, - "ShadowUserProcessor:getOrgId: request map prepared to query elasticsearch for org id :" - + filters - + "with processId" - + shadowUser.getProcessId()); - SearchDTO searchDTO = ElasticSearchHelper.createSearchDTO(request); - Map<String, Object> response = - (Map<String, Object>) - ElasticSearchHelper.getResponseFromFuture( - elasticSearchService.search( - searchDTO, ProjectUtil.EsType.organisation.getTypeName(), context)); - List<Map<String, Object>> orgData = - ((List<Map<String, Object>>) response.get(JsonKey.CONTENT)); - if (CollectionUtils.isNotEmpty(orgData)) { - Map<String, Object> orgMap = orgData.get(0); - extOrgIdMap.put( - shadowUser.getChannel().concat(":").concat(shadowUser.getOrgExtId()), - (String) orgMap.get(JsonKey.ID)); - return (String) orgMap.get(JsonKey.ID); - } - } - return StringUtils.EMPTY; - } - - private List<String> getOrganisationIds(Map<String, Object> dbUser) { - List<String> organisationsIds = new ArrayList<>(); - ((List<Map<String, Object>>) dbUser.get(JsonKey.ORGANISATIONS)) - .stream() - .forEach( - organisation -> { - organisationsIds.add((String) organisation.get(JsonKey.ORGANISATION_ID)); - }); - return organisationsIds; - } - - private void syncUserToES(String userId, RequestContext context) { - Map<String, Object> fullUserDetails = Util.getUserDetails(userId, context); - try { - Future<Boolean> future = - elasticSearchService.update(JsonKey.USER, userId, fullUserDetails, context); - if ((boolean) ElasticSearchHelper.getResponseFromFuture(future)) { - logger.info( - context, - "ShadowUserMigrationScheduler:updateUserStatus: data successfully updated to elastic search with userId:" - .concat(userId + "")); - } - } catch (Exception e) { - logger.error( - context, - "ShadowUserMigrationScheduler:syncUserToES: data failed to updates in elastic search with userId:" - .concat(userId + ""), - e); - } - } - - private void registerUserToOrg(String userId, String organisationId, RequestContext context) { - Map<String, Object> reqMap = new WeakHashMap<>(); - List<String> roles = new ArrayList<>(); - roles.add(ProjectUtil.UserRole.PUBLIC.getValue()); - reqMap.put(JsonKey.ROLES, roles); - reqMap.put(JsonKey.HASHTAGID, organisationId); - reqMap.put(JsonKey.ID, ProjectUtil.getUniqueIdFromTimestamp(1)); - reqMap.put(JsonKey.USER_ID, userId); - reqMap.put(JsonKey.ORGANISATION_ID, organisationId); - reqMap.put(JsonKey.ORG_JOIN_DATE, ProjectUtil.getFormattedDate()); - reqMap.put(JsonKey.IS_DELETED, false); - Util.DbInfo usrOrgDb = Util.dbInfoMap.get(JsonKey.USER_ORG_DB); - try { - Response response = - cassandraOperation.insertRecord( - usrOrgDb.getKeySpace(), usrOrgDb.getTableName(), reqMap, context); - logger.info( - context, - "ShadowUserProcessor:registerUserToOrg:user status while registration with org is:" - + response.getResult()); - - } catch (Exception e) { - logger.error( - context, - "ShadowUserProcessor:registerUserToOrg:user is failed to register with org" + userId, - e); - } - } - - private Map<String, Object> getTelemetryContextByProcessId(String processId) { - - if (MapUtils.isNotEmpty(processIdtelemetryCtxMap.get(processId))) { - return processIdtelemetryCtxMap.get(processId); - } - Map<String, String> contextMap = new HashMap<>(); - Map<String, Object> telemetryContext = new HashMap<>(); - Response response = - cassandraOperation.getRecordById( - bulkUploadDbInfo.getKeySpace(), bulkUploadDbInfo.getTableName(), processId, null); - List<Map<String, Object>> result = new ArrayList<>(); - if (!((List) response.getResult().get(JsonKey.RESPONSE)).isEmpty()) { - result = ((List) response.getResult().get(JsonKey.RESPONSE)); - Map<String, Object> responseMap = result.get(0); - contextMap = (Map<String, String>) responseMap.get(JsonKey.CONTEXT_TELEMETRY); - telemetryContext.putAll(contextMap); - processIdtelemetryCtxMap.put(processId, telemetryContext); - } - logger.info( - "ShadowUserMigrationScheduler:getFullRecordFromProcessId:got single row data from bulk_upload_process with processId:" - + processId); - return telemetryContext; - } - - /** - * METHOD WILL RETURN THE LIST OF SHADOW USER WHICH IS PRE EXISTING WITH USERID this method will - * give all the record which are not claimed failed and rejected i.e will update the claim status - * of the user who are ELIGIBLE. - * - * @param userId - * @return - */ - public List<ShadowUser> getMultiMatchRecords(String userId, RequestContext context) { - List<ShadowUser> shadowUsersList = new ArrayList<>(); - Response response = - cassandraOperation.searchValueInList( - JsonKey.SUNBIRD, JsonKey.SHADOW_USER, JsonKey.USERIDS, userId, context); - if (!((List) response.getResult().get(JsonKey.RESPONSE)).isEmpty()) { - ((List) response.getResult().get(JsonKey.RESPONSE)) - .stream() - .forEach( - shadowMap -> { - ShadowUser shadowUser = mapper.convertValue(shadowMap, ShadowUser.class); - if (shadowUser.getClaimStatus() != ClaimStatus.CLAIMED.getValue() - && shadowUser.getClaimStatus() != ClaimStatus.REJECTED.getValue() - && shadowUser.getClaimStatus() != ClaimStatus.FAILED.getValue()) { - shadowUsersList.add(shadowUser); - } - }); - } - return shadowUsersList; - } - - /** - * userExtId, channel , userIds, claimStatus all the user found with same id in shadow_user table - * there claim status will be updated to multimatch - * - * @param shadowUserList - */ - private void changeStatusToMultiMatch(List<ShadowUser> shadowUserList, RequestContext context) { - shadowUserList - .stream() - .forEach( - shadowUser -> { - if (shadowUser.getClaimStatus() != ClaimStatus.CLAIMED.getValue()) { - updateUserInShadowDb( - null, - shadowUser, - ClaimStatus.MULTIMATCH.getValue(), - shadowUser.getUserIds(), - context); - // TODO DELETE ENTRY FROM ALERTS TABLE - } - }); - } - - /** - * this method will return all the user who doesn't belong to the same provided channel This - * filtering will be needed to avoid update of claimStatus to MULTIMATCH of same user while - * updating. - * - * @param channel - * @param shadowUserList - */ - private List<ShadowUser> getDiffChannelUsers(String channel, List<ShadowUser> shadowUserList) { - - List<ShadowUser> filterShadowUser = new ArrayList<>(); - if (CollectionUtils.isNotEmpty(shadowUserList)) { - shadowUserList - .stream() - .forEach( - singleShadowUser -> { - if (!StringUtils.equalsIgnoreCase(singleShadowUser.getChannel(), channel)) { - filterShadowUser.add(singleShadowUser); - } - }); - } - return filterShadowUser; - } -} diff --git a/service/src/main/java/org/sunbird/quartzscheduler/UploadLookUpScheduler.java b/service/src/main/java/org/sunbird/quartzscheduler/UploadLookUpScheduler.java deleted file mode 100644 index 8afca2d8d727516754f6e37a403f5beee3bd6e30..0000000000000000000000000000000000000000 --- a/service/src/main/java/org/sunbird/quartzscheduler/UploadLookUpScheduler.java +++ /dev/null @@ -1,98 +0,0 @@ -package org.sunbird.quartzscheduler; - -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Calendar; -import java.util.Date; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import org.quartz.JobExecutionContext; -import org.quartz.JobExecutionException; -import org.sunbird.keys.JsonKey; -import org.sunbird.logging.LoggerUtil; -import org.sunbird.operations.ActorOperations; -import org.sunbird.request.Request; -import org.sunbird.response.Response; -import org.sunbird.util.Util; - -/** - * This class will lookup into bulk process table. if process type is new or in progress (more than - * x hours) then take the process id and do the re-process of job. - * - * @author Manzarul - */ -public class UploadLookUpScheduler extends BaseJob { - private static LoggerUtil logger = new LoggerUtil(UploadLookUpScheduler.class); - - private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSSZ"); - - public void execute(JobExecutionContext ctx) throws JobExecutionException { - logger.info( - "Running Upload Scheduler Job at: " - + Calendar.getInstance().getTime() - + " triggered by: " - + ctx.getJobDetail().toString()); - Util.DbInfo bulkDb = Util.dbInfoMap.get(JsonKey.BULK_OP_DB); - List<Map<String, Object>> result = null; - // get List of process with status as New - Response res = new Response(); // Will depricate these api with SC-2169 - /*cassandraOperation.getRecordsByIndexedProperty( - bulkDb.getKeySpace(), - bulkDb.getTableName(), - JsonKey.STATUS, - ProjectUtil.BulkProcessStatus.NEW.getValue(), - null);*/ - result = ((List<Map<String, Object>>) res.get(JsonKey.RESPONSE)); - logger.info( - "Total No. of record in Bulk_upload_process table with status as NEW are : :" - + result.size()); - if (!result.isEmpty()) { - process(result); - } - // get List of Process with status as InProgress - res = new Response(); // Will depricate these api with SC-2169 - /*cassandraOperation.getRecordsByIndexedProperty( - bulkDb.getKeySpace(), - bulkDb.getTableName(), - JsonKey.STATUS, - ProjectUtil.BulkProcessStatus.IN_PROGRESS.getValue(), - null);*/ - result = ((List<Map<String, Object>>) res.get(JsonKey.RESPONSE)); - logger.info( - "Total No. of record in Bulk_upload_process table with status as IN_PROGRESS are : :" - + result.size()); - if (null != result) { - Iterator<Map<String, Object>> itr = result.iterator(); - while (itr.hasNext()) { - Map<String, Object> map = itr.next(); - try { - Date startTime = format.parse((String) map.get(JsonKey.PROCESS_START_TIME)); - Date currentTime = format.parse(format.format(new Date())); - long difference = currentTime.getTime() - startTime.getTime(); - int hourDiff = (int) (difference / (1000 * 3600)); - // if diff is more than 5Hr then only process it. - if (hourDiff < 5) { - itr.remove(); - } - } catch (ParseException ex) { - logger.error("UploadLookUpScheduler: " + ex.getMessage(), ex); - } - } - if (!result.isEmpty()) { - logger.info( - "Total No. of record in Bulk_upload_process table with status as IN_PROGRESS " - + "with diff bw start time and current time greater than 5Hr are : :" - + result.size()); - process(result); - } - } - } - - private void process(List<Map<String, Object>> result) { - Request request = new Request(); - request.put(JsonKey.DATA, result); - request.setOperation(ActorOperations.SCHEDULE_BULK_UPLOAD.getValue()); - tellToBGRouter(request); - } -} diff --git a/service/src/main/java/org/sunbird/util/Util.java b/service/src/main/java/org/sunbird/util/Util.java index 322fbfdb1af5b15f46c3a34afb130a0c2e9fea26..94c825303c0ba398cb4ddc78cfc3214c048c736d 100644 --- a/service/src/main/java/org/sunbird/util/Util.java +++ b/service/src/main/java/org/sunbird/util/Util.java @@ -33,7 +33,6 @@ import org.sunbird.response.Response; import org.sunbird.sso.KeycloakRequiredActionLinkUtil; import org.sunbird.url.URLShortner; import org.sunbird.url.URLShortnerImpl; -import org.sunbird.quartzscheduler.SchedulerManager; import scala.concurrent.Future; /** @@ -67,7 +66,6 @@ public final class Util { initializeDBProperty(); // EkStep HttpClient headers init headers.put("content-type", "application/json"); headers.put("accept", "application/json"); - new Thread(() -> SchedulerManager.getInstance()).start(); } private Util() {} diff --git a/service/src/main/resources/quartz.properties b/service/src/main/resources/quartz.properties deleted file mode 100644 index 3ad176a1aedf2c4318647f82b5421e0be18671e4..0000000000000000000000000000000000000000 --- a/service/src/main/resources/quartz.properties +++ /dev/null @@ -1,35 +0,0 @@ -#============================================================================ -# Configure Main Scheduler Properties -#============================================================================ - -org.quartz.scheduler.instanceName = MyScheduler -org.quartz.scheduler.instanceId = AUTO - -#============================================================================ -# Configure ThreadPool -#============================================================================ - -org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool -org.quartz.threadPool.threadCount = 1 - -#============================================================================ -# Configure JobStore -#============================================================================ - -org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX -org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.PostgreSQLDelegate -org.quartz.jobStore.tablePrefix = QRTZ_ -org.quartz.jobStore.useProperties = false -org.quartz.jobStore.dataSource=MySqlDS -org.quartz.jobStore.isClustered = true -org.quartz.jobStore.clusterCheckinInterval = 5000 - -#============================================================================ -# Configure Datasources -#============================================================================ -org.quartz.dataSource.MySqlDS.driver = org.postgresql.Driver -org.quartz.dataSource.MySqlDS.URL = jdbc:postgresql://localhost/postgres -org.quartz.dataSource.MySqlDS.user = postgres -org.quartz.dataSource.MySqlDS.password = root -org.quartz.dataSource.MySqlDS.maxConnections = 2 -org.quartz.dataSource.MySqlDS.validationQuery=select 0 from qrtz_triggers \ No newline at end of file diff --git a/service/src/main/resources/tables_postgres.sql b/service/src/main/resources/tables_postgres.sql deleted file mode 100644 index 512a9b1ed4f7c1559a5eee30632dffeececdcbad..0000000000000000000000000000000000000000 --- a/service/src/main/resources/tables_postgres.sql +++ /dev/null @@ -1,187 +0,0 @@ --- Thanks to Patrick Lightbody for submitting this... --- --- In your Quartz properties file, you'll need to set --- org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.PostgreSQLDelegate - -drop table qrtz_fired_triggers; -DROP TABLE QRTZ_PAUSED_TRIGGER_GRPS; -DROP TABLE QRTZ_SCHEDULER_STATE; -DROP TABLE QRTZ_LOCKS; -drop table qrtz_simple_triggers; -drop table qrtz_cron_triggers; -drop table qrtz_simprop_triggers; -DROP TABLE QRTZ_BLOB_TRIGGERS; -drop table qrtz_triggers; -drop table qrtz_job_details; -drop table qrtz_calendars; - -CREATE TABLE qrtz_job_details - ( - SCHED_NAME VARCHAR(120) NOT NULL, - JOB_NAME VARCHAR(200) NOT NULL, - JOB_GROUP VARCHAR(200) NOT NULL, - DESCRIPTION VARCHAR(250) NULL, - JOB_CLASS_NAME VARCHAR(250) NOT NULL, - IS_DURABLE BOOL NOT NULL, - IS_NONCONCURRENT BOOL NOT NULL, - IS_UPDATE_DATA BOOL NOT NULL, - REQUESTS_RECOVERY BOOL NOT NULL, - JOB_DATA BYTEA NULL, - PRIMARY KEY (SCHED_NAME,JOB_NAME,JOB_GROUP) -); - -CREATE TABLE qrtz_triggers - ( - SCHED_NAME VARCHAR(120) NOT NULL, - TRIGGER_NAME VARCHAR(200) NOT NULL, - TRIGGER_GROUP VARCHAR(200) NOT NULL, - JOB_NAME VARCHAR(200) NOT NULL, - JOB_GROUP VARCHAR(200) NOT NULL, - DESCRIPTION VARCHAR(250) NULL, - NEXT_FIRE_TIME BIGINT NULL, - PREV_FIRE_TIME BIGINT NULL, - PRIORITY INTEGER NULL, - TRIGGER_STATE VARCHAR(16) NOT NULL, - TRIGGER_TYPE VARCHAR(8) NOT NULL, - START_TIME BIGINT NOT NULL, - END_TIME BIGINT NULL, - CALENDAR_NAME VARCHAR(200) NULL, - MISFIRE_INSTR SMALLINT NULL, - JOB_DATA BYTEA NULL, - PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP), - FOREIGN KEY (SCHED_NAME,JOB_NAME,JOB_GROUP) - REFERENCES QRTZ_JOB_DETAILS(SCHED_NAME,JOB_NAME,JOB_GROUP) -); - -CREATE TABLE qrtz_simple_triggers - ( - SCHED_NAME VARCHAR(120) NOT NULL, - TRIGGER_NAME VARCHAR(200) NOT NULL, - TRIGGER_GROUP VARCHAR(200) NOT NULL, - REPEAT_COUNT BIGINT NOT NULL, - REPEAT_INTERVAL BIGINT NOT NULL, - TIMES_TRIGGERED BIGINT NOT NULL, - PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP), - FOREIGN KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP) - REFERENCES QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP) -); - -CREATE TABLE qrtz_cron_triggers - ( - SCHED_NAME VARCHAR(120) NOT NULL, - TRIGGER_NAME VARCHAR(200) NOT NULL, - TRIGGER_GROUP VARCHAR(200) NOT NULL, - CRON_EXPRESSION VARCHAR(120) NOT NULL, - TIME_ZONE_ID VARCHAR(80), - PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP), - FOREIGN KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP) - REFERENCES QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP) -); - -CREATE TABLE qrtz_simprop_triggers - ( - SCHED_NAME VARCHAR(120) NOT NULL, - TRIGGER_NAME VARCHAR(200) NOT NULL, - TRIGGER_GROUP VARCHAR(200) NOT NULL, - STR_PROP_1 VARCHAR(512) NULL, - STR_PROP_2 VARCHAR(512) NULL, - STR_PROP_3 VARCHAR(512) NULL, - INT_PROP_1 INT NULL, - INT_PROP_2 INT NULL, - LONG_PROP_1 BIGINT NULL, - LONG_PROP_2 BIGINT NULL, - DEC_PROP_1 NUMERIC(13,4) NULL, - DEC_PROP_2 NUMERIC(13,4) NULL, - BOOL_PROP_1 BOOL NULL, - BOOL_PROP_2 BOOL NULL, - PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP), - FOREIGN KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP) - REFERENCES QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP) -); - -CREATE TABLE qrtz_blob_triggers - ( - SCHED_NAME VARCHAR(120) NOT NULL, - TRIGGER_NAME VARCHAR(200) NOT NULL, - TRIGGER_GROUP VARCHAR(200) NOT NULL, - BLOB_DATA BYTEA NULL, - PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP), - FOREIGN KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP) - REFERENCES QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP) -); - -CREATE TABLE qrtz_calendars - ( - SCHED_NAME VARCHAR(120) NOT NULL, - CALENDAR_NAME VARCHAR(200) NOT NULL, - CALENDAR BYTEA NOT NULL, - PRIMARY KEY (SCHED_NAME,CALENDAR_NAME) -); - - -CREATE TABLE qrtz_paused_trigger_grps - ( - SCHED_NAME VARCHAR(120) NOT NULL, - TRIGGER_GROUP VARCHAR(200) NOT NULL, - PRIMARY KEY (SCHED_NAME,TRIGGER_GROUP) -); - -CREATE TABLE qrtz_fired_triggers - ( - SCHED_NAME VARCHAR(120) NOT NULL, - ENTRY_ID VARCHAR(95) NOT NULL, - TRIGGER_NAME VARCHAR(200) NOT NULL, - TRIGGER_GROUP VARCHAR(200) NOT NULL, - INSTANCE_NAME VARCHAR(200) NOT NULL, - FIRED_TIME BIGINT NOT NULL, - SCHED_TIME BIGINT NOT NULL, - PRIORITY INTEGER NOT NULL, - STATE VARCHAR(16) NOT NULL, - JOB_NAME VARCHAR(200) NULL, - JOB_GROUP VARCHAR(200) NULL, - IS_NONCONCURRENT BOOL NULL, - REQUESTS_RECOVERY BOOL NULL, - PRIMARY KEY (SCHED_NAME,ENTRY_ID) -); - -CREATE TABLE qrtz_scheduler_state - ( - SCHED_NAME VARCHAR(120) NOT NULL, - INSTANCE_NAME VARCHAR(200) NOT NULL, - LAST_CHECKIN_TIME BIGINT NOT NULL, - CHECKIN_INTERVAL BIGINT NOT NULL, - PRIMARY KEY (SCHED_NAME,INSTANCE_NAME) -); - -CREATE TABLE qrtz_locks - ( - SCHED_NAME VARCHAR(120) NOT NULL, - LOCK_NAME VARCHAR(40) NOT NULL, - PRIMARY KEY (SCHED_NAME,LOCK_NAME) -); - -create index idx_qrtz_j_req_recovery on qrtz_job_details(SCHED_NAME,REQUESTS_RECOVERY); -create index idx_qrtz_j_grp on qrtz_job_details(SCHED_NAME,JOB_GROUP); - -create index idx_qrtz_t_j on qrtz_triggers(SCHED_NAME,JOB_NAME,JOB_GROUP); -create index idx_qrtz_t_jg on qrtz_triggers(SCHED_NAME,JOB_GROUP); -create index idx_qrtz_t_c on qrtz_triggers(SCHED_NAME,CALENDAR_NAME); -create index idx_qrtz_t_g on qrtz_triggers(SCHED_NAME,TRIGGER_GROUP); -create index idx_qrtz_t_state on qrtz_triggers(SCHED_NAME,TRIGGER_STATE); -create index idx_qrtz_t_n_state on qrtz_triggers(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP,TRIGGER_STATE); -create index idx_qrtz_t_n_g_state on qrtz_triggers(SCHED_NAME,TRIGGER_GROUP,TRIGGER_STATE); -create index idx_qrtz_t_next_fire_time on qrtz_triggers(SCHED_NAME,NEXT_FIRE_TIME); -create index idx_qrtz_t_nft_st on qrtz_triggers(SCHED_NAME,TRIGGER_STATE,NEXT_FIRE_TIME); -create index idx_qrtz_t_nft_misfire on qrtz_triggers(SCHED_NAME,MISFIRE_INSTR,NEXT_FIRE_TIME); -create index idx_qrtz_t_nft_st_misfire on qrtz_triggers(SCHED_NAME,MISFIRE_INSTR,NEXT_FIRE_TIME,TRIGGER_STATE); -create index idx_qrtz_t_nft_st_misfire_grp on qrtz_triggers(SCHED_NAME,MISFIRE_INSTR,NEXT_FIRE_TIME,TRIGGER_GROUP,TRIGGER_STATE); - -create index idx_qrtz_ft_trig_inst_name on qrtz_fired_triggers(SCHED_NAME,INSTANCE_NAME); -create index idx_qrtz_ft_inst_job_req_rcvry on qrtz_fired_triggers(SCHED_NAME,INSTANCE_NAME,REQUESTS_RECOVERY); -create index idx_qrtz_ft_j_g on qrtz_fired_triggers(SCHED_NAME,JOB_NAME,JOB_GROUP); -create index idx_qrtz_ft_jg on qrtz_fired_triggers(SCHED_NAME,JOB_GROUP); -create index idx_qrtz_ft_t_g on qrtz_fired_triggers(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP); -create index idx_qrtz_ft_tg on qrtz_fired_triggers(SCHED_NAME,TRIGGER_GROUP); - - -commit; diff --git a/service/src/test/java/org/sunbird/actor/bulkupload/DeclaredExternalIdActorTest.java b/service/src/test/java/org/sunbird/actor/bulkupload/DeclaredExternalIdActorTest.java deleted file mode 100644 index d969448406af82e3442ec96fb53abab59085ee34..0000000000000000000000000000000000000000 --- a/service/src/test/java/org/sunbird/actor/bulkupload/DeclaredExternalIdActorTest.java +++ /dev/null @@ -1,219 +0,0 @@ -package org.sunbird.actor.bulkupload; - -import static akka.testkit.JavaTestKit.duration; -import static org.junit.Assert.assertTrue; -import static org.powermock.api.mockito.PowerMockito.mock; -import static org.powermock.api.mockito.PowerMockito.mockStatic; -import static org.powermock.api.mockito.PowerMockito.when; - -import akka.actor.ActorRef; -import akka.actor.ActorSystem; -import akka.actor.Props; -import akka.testkit.javadsl.TestKit; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -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.actor.service.SunbirdMWService; -import org.sunbird.cassandraimpl.CassandraOperationImpl; -import org.sunbird.client.org.OrganisationClient; -import org.sunbird.client.org.impl.OrganisationClientImpl; -import org.sunbird.datasecurity.DecryptionService; -import org.sunbird.exception.ProjectCommonException; -import org.sunbird.exception.ResponseCode; -import org.sunbird.helper.ServiceFactory; -import org.sunbird.keys.JsonKey; -import org.sunbird.models.organisation.Organisation; -import org.sunbird.operations.BulkUploadActorOperation; -import org.sunbird.request.Request; -import org.sunbird.response.Response; -import org.sunbird.util.Util; - -@RunWith(PowerMockRunner.class) -@PrepareForTest({ - ServiceFactory.class, - Util.class, - ServiceFactory.class, - SunbirdMWService.class, - OrganisationClientImpl.class, - org.sunbird.datasecurity.impl.ServiceFactory.class -}) -@PowerMockIgnore({ - "javax.management.*", - "javax.net.ssl.*", - "javax.security.*", - "jdk.internal.reflect.*", - "javax.crypto.*" -}) -public class DeclaredExternalIdActorTest { - private static final Props props = Props.create(DeclaredExternalIdActor.class); - private static ActorSystem system = ActorSystem.create("system"); - private static CassandraOperationImpl cassandraOperation; - private static DecryptionService decryptionService; - private static SunbirdMWService SunbirdMWService; - private static OrganisationClient organisationClient; - - @BeforeClass - public static void beforeEachTest() { - PowerMockito.mockStatic(ServiceFactory.class); - - cassandraOperation = mock(CassandraOperationImpl.class); - when(ServiceFactory.getInstance()).thenReturn(cassandraOperation); - PowerMockito.mockStatic(org.sunbird.datasecurity.impl.ServiceFactory.class); - decryptionService = mock(DecryptionService.class); - when(org.sunbird.datasecurity.impl.ServiceFactory.getDecryptionServiceInstance(null)) - .thenReturn(decryptionService); - PowerMockito.mockStatic(SunbirdMWService.class); - SunbirdMWService.tellToBGRouter(Mockito.any(), Mockito.any()); - when(cassandraOperation.updateRecord( - Mockito.anyString(), Mockito.anyString(), Mockito.anyMap(), Mockito.any())) - .thenReturn(updateData(true)); - } - - private static Response updateData(boolean empty) { - Response res = new Response(); - List<Map<String, Object>> list = new ArrayList<>(); - if (!empty) { - Map<String, Object> map = new HashMap<>(); - map.put(JsonKey.ID, "userId"); - map.put(JsonKey.IS_DELETED, true); - list.add(map); - } - res.put(JsonKey.RESPONSE, list); - return res; - } - - private Response createDeclaredBulkUploadData(String statusType) { - Response response = new Response(); - Map<String, Object> declaredUserUploadMap = new HashMap<>(); - declaredUserUploadMap.put(JsonKey.ID, "0130752540065218560"); - if (JsonKey.VALIDATED.equals(statusType)) { - declaredUserUploadMap.put( - JsonKey.DATA, - "PYOeYF3BCS4Xv8duenjraA46kuN5J96XQMHyZz9NRDdLiOkPtBMYGuj6CRytyGwVrvNknVJ8Kzt7\\ndRBNmR5A7IPfuGm0X1XEb/YQjXzgR5G/A6fNKJKMiTYyYHeR9vJyPGJ5jf5zDC/wiAxk80kD3X9b\\nixZo4RN6k3/PPHvaU7iVxSPc1+F74JD2WV72fSbHEuabJAYAH4inbvCRiYq37/KvH9u2bhTV7TPV\\neI6rVIkaYzD07JPNMeVXJqIGbpBCOof9I/yVoczuDRCCPma3fz549diFEVWmUDkKppTGBIRlA1AP\\njIxTkX0DPSEQ5HRHvcKEMtiO6w/S/NNkyP7n6ddkjr75U0hM1uswr4HoeJh9SQeSxMGsvJpgrITO\\nEsI7rp1gID/xTOxz7eIsu7GG+ZDmfECggGC9pAiEsO3dsQZqhOeU8Ge2WAw+a4pvnGIhk478k98e\\nDtATCCcOlJt8zX4hRNuZp0gX9lKAZh5Zb1PfAaYl+wfqRl5fT12EqVdwPoMxO4DeiztONbe65fy0\\n52WWZlBorSgzxjA3qH5xUV4/EuRPcnI3DypooVZgmxDKNKm7AeAABPUNMzgH4spfsFOf1n0XnOut\\nJ2ZNY6vCaduqZluhXE9eTGPqvIzlJDKAEPH+gxPt6LzdndcUbALegmGD7YmpdY8iggsVOM0vtb4="); - } else if (JsonKey.REJECTED.equals(statusType)) { - declaredUserUploadMap.put( - JsonKey.DATA, - "PYOeYF3BCS4Xv8duenjraA46kuN5J96XQMHyZz9NRDdLiOkPtBMYGuj6CRytyGwVrvNknVJ8Kzt7\\ndRBNmR5A7IPfuGm0X1XEb/YQjXzgR5G/A6fNKJKMiTYyYHeR9vJyPGJ5jf5zDC/wiAxk80kD3X9b\\nixZo4RN6k3/PPHvaU7iVxSPc1+F74JD2WV72fSbHEuabJAYAH4inbvCRiYq37/KvH9u2bhTV7TPV\\neI6rVIkaYzD07JPNMeVXJqIGbpBCOof9I/yVoczuDRCCPma3fz549diFEVWmUDkKppTGBIRlA1AP\\njIxTkX0DPSEQ5HRH9GqKI8c53rQKl9OmwLn+Kdi+pJMK3/K/DKq3noKu5jVzytvonHUIid0OFDdk\\nHIqXXYHSajzCw9hzQeoaD5sIrK+YCQklglUYq9QTkXAfFUoBDHq5jTV+qTbtLhApsCyt/H/ExLRd\\ne3W8zHKY/z2jh38VRww7G0B7Pz2xNj4auP09xbIkXRG2NLx9gQk4+pQ8r6PImlxJRkFJlYn6ePQl\\n403NiDZfKefKn4dBLm+ZAiTPT0+QO6gWevtuJgkO6VhRc8aHd/uiEZnRpudFmYu7hGgkVk4qaBJv\\n6ULNlXPpi2LT3pKUFpVaEqVkiU+H2iY1chyf2ZRjlyHabORsk2PKvQzXQ7/CA4/fLbxEU+wvlo8r\\nvNqUr8TCIIf22PnZKxlyv7HWLAL2tEhPYIdBrxUnUbKQo/LFzf4rl6QvL1wdYXAgOeWid21HySz4\\nMHmYxhkFPKboJqKv4eqZ+iijfJzxoT38RMkZtI8alOJ5ePDUmUAsmAW11Lmi7M6+YMZc2Kjg"); - } else if (JsonKey.ERROR.equals(statusType)) { - declaredUserUploadMap.put( - JsonKey.DATA, - "PYOeYF3BCS4Xv8duenjraA46kuN5J96XQMHyZz9NRDdLiOkPtBMYGuj6CRytyGwVrvNknVJ8Kzt7\\ndRBNmR5A7IPfuGm0X1XEb/YQjXzgR5G/A6fNKJKMiTYyYHeR9vJyPGJ5jf5zDC/wiAxk80kD3X9b\\nixZo4RN6k3/PPHvaU7iVxSPc1+F74JD2WV72fSbHEuabJAYAH4inbvCRiYq37/KvH9u2bhTV7TPV\\neI6rVIkaYzD07JPNMeVXJqIGbpBCOof9I/yVoczuDRCCPma3fz549diFEVWmUDkKppTGBIRlA1AP\\njIxTkX0DPSEQ5HRH9GqKI8c53rQKl9OmwLn+Kdi+pJMK3/K/DKq3noKu5jVzytvonHUIid0OFDdk\\nHIqXXYHSajzCw9hzQeoaD5sIrK+YCQklglUYq9QTkXAfFUoBDHq5jTV+qTbtLhApsCyt/H/ExLRd\\ne3W8zHKY/z2jh38VRww7G0B7Pz2xNj4auP09xbIkXRG2NLx9gQk4+pQ8r6PImlxJRkFJlYn6ePQl\\n403NiDZfKefKn4dBLm+ZAiTPT0+QO6gWevtuJgkO6VhRc8aHd/uiEZnRpudFmYu7hGgkVk4qaBJv\\n6ULNlXPpi2LT3pKUFpVaEqVkiU+H2iY1chyf2ZRjlyHabORsk2PKvQzXQ7/CA4/fLbxEU+wvlo8r\\nvNqUr8TCIIf22PnZKxlyv7HWLAL2tEhPYIdBrxUnUbKQo/LFzf4rl6QvL1wdYXAgOeWid21HySz4\\nMHmYxhkFPKboJqKv4eqZ+iijfJzxoT38RMkZtI8alOJ5ePDUmUAsmAW11Lmi7M6+YMZc2Kjg"); - } - List<Map<String, Object>> result = new ArrayList<>(); - result.add(declaredUserUploadMap); - response.put(JsonKey.RESPONSE, result); - return response; - } - - @Test - public void testUploadDeclaredUser() { - when(cassandraOperation.getRecordById( - Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any())) - .thenReturn(createDeclaredBulkUploadData(JsonKey.VALIDATED)); - when(decryptionService.decryptData(Mockito.anyString(), Mockito.any())) - .thenReturn( - "[{\"email\":null,\"phone\":null,\"name\":null,\"userExternalId\":\"\",\"orgExternalId\":null,\"channel\":\"\",\"inputStatus\":\"VALIDATED\",\"schoolName\":null,\"userId\":\"\",\"persona\":\"teacher\"}]"); - boolean result = - testScenario(createRequest(BulkUploadActorOperation.PROCESS_USER_BULK_SELF_DECLARED), null); - assertTrue(result); - } - - @Test - public void testUploadDeclaredUserOFErrorStatus() { - when(cassandraOperation.getRecordById( - Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any())) - .thenReturn(createDeclaredBulkUploadData(JsonKey.ERROR)); - when(decryptionService.decryptData(Mockito.anyString(), Mockito.any())) - .thenReturn( - "[{\"email\":null,\"phone\":null,\"name\":null,\"userExternalId\":\"\",\"orgExternalId\":null,\"channel\":\"\",\"inputStatus\":\"ERROR\",\"schoolName\":null,\"userId\":\"\",\"persona\":\"teacher\"}]"); - boolean result = - testScenario(createRequest(BulkUploadActorOperation.PROCESS_USER_BULK_SELF_DECLARED), null); - assertTrue(result); - } - - @Test - public void testUploadDeclaredUserOFErrorStatusWithErrorType() { - when(cassandraOperation.getRecordById( - Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any())) - .thenReturn(createDeclaredBulkUploadData(JsonKey.ERROR)); - when(decryptionService.decryptData(Mockito.anyString(), Mockito.any())) - .thenReturn( - "[{\"email\":null,\"phone\":null,\"name\":null,\"userExternalId\":\"\",\"orgExternalId\":null,\"channel\":\"\",\"inputStatus\":\"ERROR\",\"errorType\":\"ERROR_ID\",\"schoolName\":null,\"userId\":\"\",\"persona\":\"teacher\"}]"); - boolean result = - testScenario(createRequest(BulkUploadActorOperation.PROCESS_USER_BULK_SELF_DECLARED), null); - assertTrue(result); - } - - @Test - public void testUploadDeclaredUserOfRejectedStatus() { - when(cassandraOperation.getRecordById( - Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any())) - .thenReturn(createDeclaredBulkUploadData(JsonKey.REJECTED)); - when(decryptionService.decryptData(Mockito.anyString(), Mockito.any())) - .thenReturn( - "[{\"email\":null,\"phone\":null,\"name\":null,\"userExternalId\":\"\",\"orgExternalId\":null,\"channel\":\"\",\"inputStatus\":\"REJECTED\",\"schoolName\":null,\"userId\":\"\",\"persona\":\"teacher\"}]"); - boolean result = - testScenario(createRequest(BulkUploadActorOperation.PROCESS_USER_BULK_SELF_DECLARED), null); - assertTrue(result); - } - - @Test - public void testUploadDeclaredUserOfWrongSubOrg() { - Organisation org = new Organisation(); - org.setRootOrgId("anyOrgId"); - org.setId("anyOrgId"); - when(cassandraOperation.getRecordById( - Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any())) - .thenReturn(createDeclaredBulkUploadData(JsonKey.VALIDATED)); - when(decryptionService.decryptData(Mockito.anyString(), Mockito.any())) - .thenReturn( - "[{\"email\":null,\"phone\":null,\"name\":null,\"userExternalId\":\"\",\"subOrgExternalId\":\"anyOrgExternalId\",\"channel\":\"\",\"inputStatus\":\"VALIDATED\",\"userId\":\"\",\"persona\":\"teacher\"}]"); - organisationClient = mock(OrganisationClient.class); - mockStatic(OrganisationClientImpl.class); - when(OrganisationClientImpl.getInstance()).thenReturn(organisationClient); - when(organisationClient.esGetOrgByExternalId( - Mockito.anyString(), Mockito.anyString(), Mockito.any())) - .thenReturn(org); - Request request = createRequest(BulkUploadActorOperation.PROCESS_USER_BULK_SELF_DECLARED); - request.getRequest().put(JsonKey.ROOT_ORG_ID, "anyRootOrgId"); - boolean result = - testScenario(createRequest(BulkUploadActorOperation.PROCESS_USER_BULK_SELF_DECLARED), null); - assertTrue(result); - } - - Request createRequest(BulkUploadActorOperation actorOperation) { - Request reqObj = new Request(); - Map reqMap = new HashMap<>(); - reqMap.put(JsonKey.PROCESS_ID, "anyProcessId"); - reqMap.put(JsonKey.ROOT_ORG_ID, "anyOrgId"); - reqObj.setRequest(reqMap); - reqObj.setOperation(actorOperation.getValue()); - return reqObj; - } - - public boolean testScenario(Request reqObj, ResponseCode errorCode) { - - TestKit probe = new TestKit(system); - ActorRef subject = system.actorOf(props); - subject.tell(reqObj, probe.getRef()); - - if (errorCode == null) { - Response res = probe.expectMsgClass(duration("100 second"), Response.class); - return null != res && res.getResponseCode() == ResponseCode.OK; - } else { - ProjectCommonException res = - probe.expectMsgClass(duration("100 second"), ProjectCommonException.class); - return res.getCode().equals(errorCode.getErrorCode()) - || res.getResponseCode() == errorCode.getResponseCode(); - } - } -} diff --git a/service/src/test/java/org/sunbird/actor/bulkupload/UserBulkMigrationActorTest.java b/service/src/test/java/org/sunbird/actor/bulkupload/UserBulkMigrationActorTest.java deleted file mode 100644 index 3097dd89e2463476d1edb2e8229eb38a5e582440..0000000000000000000000000000000000000000 --- a/service/src/test/java/org/sunbird/actor/bulkupload/UserBulkMigrationActorTest.java +++ /dev/null @@ -1,167 +0,0 @@ -package org.sunbird.actor.bulkupload; - -import static akka.testkit.JavaTestKit.duration; -import static org.powermock.api.mockito.PowerMockito.mock; -import static org.powermock.api.mockito.PowerMockito.when; - -import akka.actor.ActorRef; -import akka.actor.ActorSystem; -import akka.actor.Props; -import akka.testkit.javadsl.TestKit; -import java.io.File; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import org.junit.Assert; -import org.junit.Before; -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.datasecurity.EncryptionService; -import org.sunbird.exception.ProjectCommonException; -import org.sunbird.exception.ResponseCode; -import org.sunbird.helper.ServiceFactory; -import org.sunbird.keys.JsonKey; -import org.sunbird.operations.BulkUploadActorOperation; -import org.sunbird.request.Request; -import org.sunbird.response.Response; -import org.sunbird.telemetry.util.TelemetryWriter; -import org.sunbird.util.ProjectUtil; - -@PrepareForTest({ - ServiceFactory.class, - TelemetryWriter.class, - org.sunbird.datasecurity.impl.ServiceFactory.class -}) -@RunWith(PowerMockRunner.class) -@PowerMockIgnore({ - "javax.management.*", - "javax.net.ssl.*", - "javax.security.*", - "jdk.internal.reflect.*", - "javax.crypto.*" -}) -public class UserBulkMigrationActorTest { - private static ActorSystem system; - private static final Props props = Props.create(UserBulkMigrationActor.class); - private static CassandraOperationImpl cassandraOperation; - private static EncryptionService encryptionService; - - @Before - public void setUp() { - PowerMockito.mockStatic(ServiceFactory.class); - cassandraOperation = mock(CassandraOperationImpl.class); - when(ServiceFactory.getInstance()).thenReturn(cassandraOperation); - Response response = getCassandraRecordById(); - when(cassandraOperation.getRecordById( - Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any())) - .thenReturn(response); - when(cassandraOperation.insertRecord( - Mockito.anyString(), Mockito.anyString(), Mockito.anyMap(), Mockito.any())) - .thenReturn(new Response()); - encryptionService = PowerMockito.mock(EncryptionService.class); - PowerMockito.mockStatic(org.sunbird.datasecurity.impl.ServiceFactory.class); - when(org.sunbird.datasecurity.impl.ServiceFactory.getEncryptionServiceInstance(null)) - .thenReturn(encryptionService); - system = ActorSystem.create("system"); - } - - @Test - public void testSelfUserBulkUploadWithOutInputInCsv() { - TestKit probe = new TestKit(system); - ActorRef subject = system.actorOf(props); - byte[] bytes = getFileAsBytes("BulkSelfDeclaredUserUploadEmpty.csv"); - Request reqObj = new Request(); - reqObj.setOperation(BulkUploadActorOperation.USER_BULK_SELF_DECLARED.getValue()); - HashMap<String, Object> innerMap = new HashMap<>(); - innerMap.put(JsonKey.CREATED_BY, "anyUserID"); - innerMap.put(JsonKey.OBJECT_TYPE, JsonKey.USER); - innerMap.put(JsonKey.FILE, bytes); - reqObj.getRequest().put(JsonKey.DATA, innerMap); - subject.tell(reqObj, probe.getRef()); - ProjectCommonException res = - probe.expectMsgClass(duration("10 second"), ProjectCommonException.class); - Assert.assertNotNull(res); - Assert.assertEquals(ResponseCode.noDataForConsumption.getErrorCode(), res.getCode()); - Assert.assertEquals(ResponseCode.noDataForConsumption.getErrorMessage(), res.getMessage()); - } - - @Test - public void testSelfUserBulkUploadWithImproperHeadersInCsv() { - TestKit probe = new TestKit(system); - ActorRef subject = system.actorOf(props); - byte[] bytes = getFileAsBytes("BulkSelfDeclaredUserUploadImproperSample.csv"); - Request reqObj = new Request(); - reqObj.setOperation(BulkUploadActorOperation.USER_BULK_SELF_DECLARED.getValue()); - HashMap<String, Object> innerMap = new HashMap<>(); - innerMap.put(JsonKey.CREATED_BY, "anyUserID"); - innerMap.put(JsonKey.OBJECT_TYPE, JsonKey.USER); - innerMap.put(JsonKey.FILE, bytes); - reqObj.getRequest().put(JsonKey.DATA, innerMap); - subject.tell(reqObj, probe.getRef()); - ProjectCommonException res = - probe.expectMsgClass(duration("100 second"), ProjectCommonException.class); - Assert.assertNotNull(res); - Assert.assertEquals(ResponseCode.mandatoryParamsMissing.getErrorCode(), res.getCode()); - Assert.assertEquals( - ProjectUtil.formatMessage( - ResponseCode.mandatoryParamsMissing.getErrorMessage(), JsonKey.PERSONA), - res.getMessage()); - } - - @Test - public void testSelfUserBulkUploadWithProperCsv() { - TestKit probe = new TestKit(system); - ActorRef subject = system.actorOf(props); - byte[] bytes = getFileAsBytes("BulkSelfDeclaredUserUploadSample.csv"); - Request reqObj = new Request(); - reqObj.setOperation(BulkUploadActorOperation.USER_BULK_SELF_DECLARED.getValue()); - HashMap<String, Object> innerMap = new HashMap<>(); - innerMap.put(JsonKey.CREATED_BY, "anyUserID"); - innerMap.put(JsonKey.OBJECT_TYPE, JsonKey.USER); - innerMap.put(JsonKey.FILE, bytes); - reqObj.getRequest().put(JsonKey.DATA, innerMap); - subject.tell(reqObj, probe.getRef()); - Response res = probe.expectMsgClass(duration("100 second"), Response.class); - Assert.assertNotNull(res); - Assert.assertEquals(ResponseCode.OK.getResponseCode(), res.getResponseCode().getResponseCode()); - Assert.assertNotNull(res.getResult().get("processId")); - // Assert.assertEquals(ResponseCode.mandatoryParamsMissing.getErrorMessage(), res.getMessage()); - } - - private byte[] getFileAsBytes(String fileName) { - File file = null; - byte[] bytes = null; - try { - file = - new File( - BulkUploadManagementActorTest.class.getClassLoader().getResource(fileName).getFile()); - Path path = Paths.get(file.getPath()); - bytes = Files.readAllBytes(path); - } catch (Exception e) { - e.printStackTrace(); - } - return bytes; - } - - private static Response getCassandraRecordById() { - Response response = new Response(); - List<Map<String, Object>> list = new ArrayList<>(); - Map<String, Object> bulkUploadProcessMap = new HashMap<>(); - bulkUploadProcessMap.put(JsonKey.ID, "anyId"); - bulkUploadProcessMap.put(JsonKey.CHANNEL, "anyChannel"); - bulkUploadProcessMap.put(JsonKey.ROOT_ORG_ID, "anyRootOrgId"); - list.add(bulkUploadProcessMap); - response.put(JsonKey.RESPONSE, list); - return response; - } -} diff --git a/service/src/test/java/org/sunbird/actor/scheduler/OnDemandSchedulerActorTest.java b/service/src/test/java/org/sunbird/actor/scheduler/OnDemandSchedulerActorTest.java deleted file mode 100644 index 472a15bb842384303b8eac59ac93a619f73fe693..0000000000000000000000000000000000000000 --- a/service/src/test/java/org/sunbird/actor/scheduler/OnDemandSchedulerActorTest.java +++ /dev/null @@ -1,100 +0,0 @@ -package org.sunbird.actor.scheduler; - -import static akka.testkit.JavaTestKit.duration; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.doNothing; -import static org.powermock.api.mockito.PowerMockito.mock; -import static org.powermock.api.mockito.PowerMockito.when; - -import akka.actor.ActorRef; -import akka.actor.ActorSystem; -import akka.actor.Props; -import akka.testkit.javadsl.TestKit; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -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.exception.ProjectCommonException; -import org.sunbird.exception.ResponseCode; -import org.sunbird.keys.JsonKey; -import org.sunbird.operations.ActorOperations; -import org.sunbird.quartzscheduler.OnDemandSchedulerManager; -import org.sunbird.quartzscheduler.SchedulerManager; -import org.sunbird.request.Request; -import org.sunbird.response.Response; - -@RunWith(PowerMockRunner.class) -@PrepareForTest({OnDemandSchedulerManager.class, SchedulerManager.class}) -@PowerMockIgnore({ - "javax.management.*", - "javax.net.ssl.*", - "javax.security.*", - "jdk.internal.reflect.*" -}) -public class OnDemandSchedulerActorTest { - private ActorSystem system = ActorSystem.create("system"); - private static final Props props = Props.create(OnDemandSchedulerActor.class); - private static OnDemandSchedulerManager onDemandSchedulerManager; - private static SchedulerManager schedulerManager; - - @Before - public void beforeEachTest() throws Exception { - schedulerManager = mock(SchedulerManager.class); - onDemandSchedulerManager = mock(OnDemandSchedulerManager.class); - // PowerMockito.whenNew(OnDemandSchedulerManager.class).withNoArguments().thenReturn(onDemandSchedulerManager); - PowerMockito.mockStatic(SchedulerManager.class); - - PowerMockito.mockStatic(OnDemandSchedulerManager.class); - doNothing().when(schedulerManager).schedule(); - when(OnDemandSchedulerManager.getInstance()).thenReturn(onDemandSchedulerManager); - } - - @Test - public void testOnDemandScheduler() { - Request req = new Request(); - Map<String, Object> reqMap = new HashMap<>(); - List<String> types = new ArrayList<>(); - types.add("shadowuser"); - reqMap.put(JsonKey.TYPE, types); - req.setRequest(reqMap); - req.setOperation(ActorOperations.ONDEMAND_START_SCHEDULER.getValue()); - boolean result = testScenario(req, null); - assertTrue(result); - } - - @Test - public void testOnDemandSchedulerWithInvalidType() { - Request req = new Request(); - Map<String, Object> reqMap = new HashMap<>(); - List<String> types = new ArrayList<>(); - types.add("shadowuser1"); - reqMap.put(JsonKey.TYPE, types); - req.setRequest(reqMap); - req.setOperation(ActorOperations.ONDEMAND_START_SCHEDULER.getValue()); - boolean result = testScenario(req, ResponseCode.invalidParameter); - assertTrue(result); - } - - private boolean testScenario(Request request, ResponseCode errorCode) { - TestKit probe = new TestKit(system); - ActorRef subject = system.actorOf(props); - subject.tell(request, probe.getRef()); - - if (errorCode == null) { - Response res = probe.expectMsgClass(duration("100 second"), Response.class); - return null != res && res.getResponseCode() == ResponseCode.OK; - } else { - ProjectCommonException res = - probe.expectMsgClass(duration("100 second"), ProjectCommonException.class); - return res.getCode().equals(errorCode.getErrorCode()) - || res.getResponseCode() == errorCode.getResponseCode(); - } - } -} diff --git a/service/src/test/java/org/sunbird/actor/user/validator/UserBulkMigrationRequestValidatorTest.java b/service/src/test/java/org/sunbird/actor/user/validator/UserBulkMigrationRequestValidatorTest.java deleted file mode 100644 index 311d0f881e73324c22c30f886e155c20079a87a2..0000000000000000000000000000000000000000 --- a/service/src/test/java/org/sunbird/actor/user/validator/UserBulkMigrationRequestValidatorTest.java +++ /dev/null @@ -1,387 +0,0 @@ -package org.sunbird.actor.user.validator; - -import java.util.ArrayList; -import java.util.List; -import org.junit.Assert; -import org.junit.Test; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.sunbird.exception.ResponseCode; -import org.sunbird.keys.JsonKey; -import org.sunbird.model.MigrationUser; -import org.sunbird.model.SelfDeclaredUser; -import org.sunbird.model.ShadowUserUpload; -import org.sunbird.util.ProjectUtil; - -@PrepareForTest(UserBulkMigrationRequestValidator.class) -public class UserBulkMigrationRequestValidatorTest { - private static final int MAX_ROW_SUPPORTED = 20000; - - @Test - public void testRowsCountFailureWithEmptyCSVFile() { - List<MigrationUser> migrationUserList = new ArrayList<>(); - try { - new ShadowUserUpload.ShadowUserUploadBuilder() - .setProcessId(ProjectUtil.generateUniqueId()) - .setFileSize("1024") - .setValues(migrationUserList) - .validate(); - } catch (Exception e) { - Assert.assertEquals(ResponseCode.noDataForConsumption.getErrorMessage(), e.getMessage()); - } - } - - @Test - public void testRowsCountFailureWithMoreCsvRowsSupported() { - List<MigrationUser> migrationUserList = new ArrayList<>(); - for (int i = 0; i < MAX_ROW_SUPPORTED + 1; i++) { - MigrationUser migrationUser = new MigrationUser(); - migrationUser.setChannel("TN"); - migrationUserList.add(migrationUser); - } - try { - new ShadowUserUpload.ShadowUserUploadBuilder() - .setProcessId(ProjectUtil.generateUniqueId()) - .setFileSize("2024") - .setValues(migrationUserList) - .validate(); - } catch (Exception e) { - Assert.assertEquals( - ResponseCode.csvRowsExceeds.getErrorMessage().concat("supported:" + MAX_ROW_SUPPORTED), - e.getMessage()); - } - } - - @Test - public void testShadowUserMigrationWithBlankName() { - List<MigrationUser> migrationUserList = new ArrayList<>(); - MigrationUser migrationUser = new MigrationUser(); - migrationUser.setChannel("TN"); - migrationUser.setPhone("9876543210"); - migrationUser.setInputStatus(JsonKey.ACTIVE); - migrationUser.setOrgExternalId("org ext id"); - migrationUser.setUserExternalId("user ext id"); - migrationUserList.add(migrationUser); - try { - new ShadowUserUpload.ShadowUserUploadBuilder() - .setProcessId(ProjectUtil.generateUniqueId()) - .setFileSize("1024") - .setValues(migrationUserList) - .validate(); - } catch (Exception e) { - Assert.assertEquals("[ In Row 1:the Column name:is missing ]", e.getMessage()); - } - } - - @Test - public void testShadowUserMigrationWithBlankEmail() { - List<MigrationUser> migrationUserList = new ArrayList<>(); - MigrationUser migrationUser = new MigrationUser(); - migrationUser.setChannel("TN"); - migrationUser.setInputStatus(JsonKey.ACTIVE); - migrationUser.setOrgExternalId("org ext id"); - migrationUser.setName("Shadow User Name"); - migrationUser.setUserExternalId("user ext id"); - migrationUserList.add(migrationUser); - try { - new ShadowUserUpload.ShadowUserUploadBuilder() - .setProcessId(ProjectUtil.generateUniqueId()) - .setFileSize("1024") - .setValues(migrationUserList) - .validate(); - } catch (Exception e) { - Assert.assertEquals("[ In Row 1:the Column email:is missing ]", e.getMessage()); - } - } - - @Test - public void testShadowUserMigrationWithInvalidEmail() { - List<MigrationUser> migrationUserList = new ArrayList<>(); - MigrationUser migrationUser = new MigrationUser(); - migrationUser.setChannel("TN"); - migrationUser.setInputStatus(JsonKey.ACTIVE); - migrationUser.setOrgExternalId("org ext id"); - migrationUser.setEmail("wrongemail"); - migrationUser.setName("Shadow User Name"); - migrationUser.setUserExternalId("user ext id"); - migrationUserList.add(migrationUser); - try { - new ShadowUserUpload.ShadowUserUploadBuilder() - .setProcessId(ProjectUtil.generateUniqueId()) - .setFileSize("1024") - .setValues(migrationUserList) - .validate(); - } catch (Exception e) { - System.out.println(e.getMessage()); - Assert.assertEquals("[ In Row 1:the Column email:is invalid ]", e.getMessage()); - } - } - - @Test - public void testShadowUserMigrationWithInvalidPhone() { - List<MigrationUser> migrationUserList = new ArrayList<>(); - MigrationUser migrationUser = new MigrationUser(); - migrationUser.setChannel("TN"); - migrationUser.setInputStatus(JsonKey.ACTIVE); - migrationUser.setOrgExternalId("org ext id"); - migrationUser.setPhone("987897"); - migrationUser.setName("Shadow User Name"); - migrationUser.setUserExternalId("user ext id"); - migrationUserList.add(migrationUser); - try { - new ShadowUserUpload.ShadowUserUploadBuilder() - .setProcessId(ProjectUtil.generateUniqueId()) - .setFileSize("1024") - .setValues(migrationUserList) - .validate(); - } catch (Exception e) { - Assert.assertEquals("[ In Row 1:the Column phone:is invalid ]", e.getMessage()); - } - } - - @Test - public void testShadowUserMigrationWithBlankUserExtId() { - List<MigrationUser> migrationUserList = new ArrayList<>(); - MigrationUser migrationUser = new MigrationUser(); - migrationUser.setChannel("TN"); - migrationUser.setInputStatus(JsonKey.ACTIVE); - migrationUser.setOrgExternalId("org ext id"); - migrationUser.setPhone("9876543210"); - migrationUser.setName("Shadow User Name"); - migrationUserList.add(migrationUser); - try { - new ShadowUserUpload.ShadowUserUploadBuilder() - .setProcessId(ProjectUtil.generateUniqueId()) - .setFileSize("1024") - .setValues(migrationUserList) - .validate(); - } catch (Exception e) { - Assert.assertEquals("[ In Row 1:the Column userExternalId:is missing ]", e.getMessage()); - } - } - - @Test - public void testShadowUserMigrationWithBlankInputStatus() { - List<MigrationUser> migrationUserList = new ArrayList<>(); - MigrationUser migrationUser = new MigrationUser(); - migrationUser.setChannel("TN"); - migrationUser.setOrgExternalId("org ext id"); - migrationUser.setPhone("9876543210"); - migrationUser.setUserExternalId("any user ext id"); - migrationUser.setName("Shadow User Name"); - migrationUserList.add(migrationUser); - try { - new ShadowUserUpload.ShadowUserUploadBuilder() - .setProcessId(ProjectUtil.generateUniqueId()) - .setFileSize("1024") - .setValues(migrationUserList) - .validate(); - } catch (Exception e) { - Assert.assertEquals("[ In Row 1:the Column input status:is missing ]", e.getMessage()); - } - } - - @Test - public void testShadowUserMigrationWithInvalidInputStatus() { - List<MigrationUser> migrationUserList = new ArrayList<>(); - MigrationUser migrationUser = new MigrationUser(); - migrationUser.setChannel("TN"); - migrationUser.setInputStatus("wrong input status"); - migrationUser.setOrgExternalId("org ext id"); - migrationUser.setPhone("9876543210"); - migrationUser.setUserExternalId("any user ext id"); - migrationUser.setName("Shadow User Name"); - migrationUserList.add(migrationUser); - try { - new ShadowUserUpload.ShadowUserUploadBuilder() - .setProcessId(ProjectUtil.generateUniqueId()) - .setFileSize("1024") - .setValues(migrationUserList) - .validate(); - } catch (Exception e) { - Assert.assertEquals("[ In Row 1:the Column input status:is invalid ]", e.getMessage()); - } - } - - @Test - public void testShadowUserMigrationWithDuplicateUserExtId() { - List<MigrationUser> migrationUserList = new ArrayList<>(); - MigrationUser migrationUser = new MigrationUser(); - migrationUser.setChannel("TN"); - migrationUser.setInputStatus("Active"); - migrationUser.setOrgExternalId("org ext id"); - migrationUser.setPhone("9876543210"); - migrationUser.setUserExternalId("any user ext id"); - migrationUser.setName("Shadow User Name"); - migrationUserList.add(migrationUser); - MigrationUser anotherMigrationUser = new MigrationUser(); - anotherMigrationUser.setChannel("TN"); - anotherMigrationUser.setInputStatus("Active"); - anotherMigrationUser.setOrgExternalId("org ext id"); - anotherMigrationUser.setPhone("9876543210"); - anotherMigrationUser.setUserExternalId("any user ext id"); - anotherMigrationUser.setName("Shadow User Name"); - migrationUserList.add(anotherMigrationUser); - try { - new ShadowUserUpload.ShadowUserUploadBuilder() - .setProcessId(ProjectUtil.generateUniqueId()) - .setFileSize("1024") - .setValues(migrationUserList) - .validate(); - } catch (Exception e) { - Assert.assertEquals("[ In Row 2:the Column userExternalId:is duplicate ]", e.getMessage()); - } - } - - @Test - public void testShadowUserMigrationWithInvalidName() { - List<MigrationUser> migrationUserList = new ArrayList<>(); - MigrationUser migrationUser = new MigrationUser(); - migrationUser.setChannel("TN"); - migrationUser.setInputStatus("Active"); - migrationUser.setOrgExternalId("org ext id"); - migrationUser.setPhone("9876543210"); - migrationUser.setUserExternalId("any user ext id"); - migrationUser.setName("###Shadow User Name"); - migrationUserList.add(migrationUser); - try { - new ShadowUserUpload.ShadowUserUploadBuilder() - .setProcessId(ProjectUtil.generateUniqueId()) - .setFileSize("1024") - .setValues(migrationUserList) - .validate(); - } catch (Exception e) { - System.out.println(e.getMessage()); - Assert.assertEquals("[ In Row 1:the Column name:is invalid ]", e.getMessage()); - } - } - - @Test - public void testRowsCountFailureWithEmptyCSVFileForDeclaredUsers() { - List<MigrationUser> declaredUserList = new ArrayList<>(); - try { - new ShadowUserUpload.ShadowUserUploadBuilder() - .setProcessId(ProjectUtil.generateUniqueId()) - .setFileSize("1024") - .setValues(declaredUserList) - .validateDeclaredUsers(); - } catch (Exception e) { - Assert.assertEquals(ResponseCode.noDataForConsumption.getErrorMessage(), e.getMessage()); - } - } - - @Test - public void testRowsCountFailureWithMoreCsvRowsSupportedForDeclaredUsers() { - List<SelfDeclaredUser> declaredUserList = new ArrayList<>(); - for (int i = 0; i < MAX_ROW_SUPPORTED + 1; i++) { - SelfDeclaredUser declaredUser = new SelfDeclaredUser(); - declaredUser.setChannel("TN"); - declaredUserList.add(declaredUser); - } - try { - new ShadowUserUpload.ShadowUserUploadBuilder() - .setProcessId(ProjectUtil.generateUniqueId()) - .setFileSize("2024") - .setUserValues(declaredUserList) - .validateDeclaredUsers(); - } catch (Exception e) { - Assert.assertEquals( - ResponseCode.csvRowsExceeds.getErrorMessage().concat("supported:" + MAX_ROW_SUPPORTED), - e.getMessage()); - } - } - - @Test - public void testMigrationWithBlankStatusDikshaUUIDForDeclaredUsers() { - List<SelfDeclaredUser> declaredUserList = new ArrayList<>(); - SelfDeclaredUser declaredUser = new SelfDeclaredUser(); - declaredUser.setChannel("TN"); - declaredUser.setPhone("9876543210"); - declaredUser.setInputStatus(JsonKey.ACTIVE); - declaredUser.setOrgExternalId("org ext id"); - declaredUser.setUserExternalId("user ext id"); - declaredUserList.add(declaredUser); - try { - new ShadowUserUpload.ShadowUserUploadBuilder() - .setProcessId(ProjectUtil.generateUniqueId()) - .setFileSize("1024") - .setUserValues(declaredUserList) - .validateDeclaredUsers(); - } catch (Exception e) { - Assert.assertEquals( - "[ In Row 1:the Column status:is invalid, In Row 1:the Column Diksha UUID:is missing, In Row 1:the Column persona:is missing ]", - e.getMessage()); - } - } - - @Test - public void testMigrationWithInValidStatusDikshaUUIDForDeclaredUsers() { - List<SelfDeclaredUser> declaredUserList = new ArrayList<>(); - SelfDeclaredUser declaredUser = new SelfDeclaredUser(); - declaredUser.setChannel("TN"); - declaredUser.setPhone("9876543210"); - declaredUser.setInputStatus(JsonKey.ACTIVE); - declaredUser.setOrgExternalId("org ext id"); - declaredUser.setUserExternalId("user ext id"); - declaredUser.setInputStatus("INVALIDATED"); - declaredUser.setUserId("user ext id"); - declaredUser.setPersona("teacher"); - declaredUserList.add(declaredUser); - try { - new ShadowUserUpload.ShadowUserUploadBuilder() - .setProcessId(ProjectUtil.generateUniqueId()) - .setFileSize("1024") - .setUserValues(declaredUserList) - .validateDeclaredUsers(); - } catch (Exception e) { - Assert.assertEquals("[ In Row 1:the Column status:is invalid ]", e.getMessage()); - } - } - - @Test - public void testMigrationWithInValidErrorTypeForDeclaredUsers() { - List<SelfDeclaredUser> declaredUserList = new ArrayList<>(); - SelfDeclaredUser declaredUser = new SelfDeclaredUser(); - declaredUser.setChannel("TN"); - declaredUser.setPhone("9876543210"); - declaredUser.setInputStatus(JsonKey.SELF_DECLARED_ERROR); - declaredUser.setOrgExternalId("org ext id"); - declaredUser.setUserId("user ext id"); - declaredUser.setErrorType("INVALIDATED"); - declaredUser.setUserExternalId("user ext id"); - declaredUser.setPersona("teacher"); - declaredUserList.add(declaredUser); - try { - new ShadowUserUpload.ShadowUserUploadBuilder() - .setProcessId(ProjectUtil.generateUniqueId()) - .setFileSize("1024") - .setUserValues(declaredUserList) - .validateDeclaredUsers(); - } catch (Exception e) { - Assert.assertEquals("[ In Row 1:the Column Error Type:is invalid ]", e.getMessage()); - } - } - - @Test - public void testMigrationWithValidErrorTypeForDeclaredUsers() { - List<SelfDeclaredUser> declaredUserList = new ArrayList<>(); - SelfDeclaredUser declaredUser = new SelfDeclaredUser(); - declaredUser.setChannel("TN"); - declaredUser.setPhone("9876543210"); - declaredUser.setInputStatus(JsonKey.SELF_DECLARED_ERROR); - declaredUser.setOrgExternalId("org ext id"); - declaredUser.setUserId("user ext id"); - declaredUser.setErrorType("ERROR_ID"); - declaredUser.setUserExternalId("user ext id"); - declaredUser.setPersona("teacher"); - declaredUserList.add(declaredUser); - try { - new ShadowUserUpload.ShadowUserUploadBuilder() - .setProcessId(ProjectUtil.generateUniqueId()) - .setFileSize("1024") - .setUserValues(declaredUserList) - .validateDeclaredUsers(); - } catch (Exception e) { - Assert.assertEquals("[ In Row 1:the Column Error Type:is invalid ]", e.getMessage()); - } - } -} diff --git a/service/src/test/java/org/sunbird/quartzscheduler/OnDemandSchedulerManagerTest.java b/service/src/test/java/org/sunbird/quartzscheduler/OnDemandSchedulerManagerTest.java deleted file mode 100644 index 125d610b7a4ffb2ae3e9827361e727b05063d873..0000000000000000000000000000000000000000 --- a/service/src/test/java/org/sunbird/quartzscheduler/OnDemandSchedulerManagerTest.java +++ /dev/null @@ -1,49 +0,0 @@ -package org.sunbird.common.quartz.scheduler; - -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.verify; -import static org.powermock.api.mockito.PowerMockito.when; - -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.quartz.JobKey; -import org.quartz.Scheduler; -import org.quartz.SchedulerException; -import org.sunbird.quartzscheduler.OnDemandSchedulerManager; -import org.sunbird.quartzscheduler.SchedulerManager; - -@RunWith(PowerMockRunner.class) -@PrepareForTest({ - SchedulerManager.class, -}) -@PowerMockIgnore({ - "javax.management.*", - "javax.net.ssl.*", - "javax.security.*", - "jdk.internal.reflect.*" -}) -public class OnDemandSchedulerManagerTest { - - static OnDemandSchedulerManager onDemandSchedulerManager; - static SchedulerManager schedulerManager; - static Scheduler scheduler; - - @Test - public void testTriggerScheduler() throws SchedulerException { - PowerMockito.suppress(PowerMockito.constructor(SchedulerManager.class)); - PowerMockito.suppress(PowerMockito.methodsDeclaredIn(SchedulerManager.class)); - scheduler = mock(Scheduler.class); - PowerMockito.mockStatic(SchedulerManager.class); - String[] jobs = {"shadowuser"}; - onDemandSchedulerManager = spy(OnDemandSchedulerManager.class); - when(scheduler.checkExists((JobKey) Mockito.anyObject())).thenReturn(false); - onDemandSchedulerManager.triggerScheduler(jobs); - verify(onDemandSchedulerManager).scheduleOnDemand(Mockito.anyString(), Mockito.anyString()); - } -}