diff --git a/service/src/main/java/org/sunbird/actor/bulkupload/BaseBulkUploadActor.java b/service/src/main/java/org/sunbird/actor/bulkupload/BaseBulkUploadActor.java index 3d1957991360236554d289e3b169d7a4eba6e562..135a47ccaa15c9184800e73cd55a1aa82ae364a1 100644 --- a/service/src/main/java/org/sunbird/actor/bulkupload/BaseBulkUploadActor.java +++ b/service/src/main/java/org/sunbird/actor/bulkupload/BaseBulkUploadActor.java @@ -38,7 +38,7 @@ import org.sunbird.util.ProjectUtil; */ public abstract class BaseBulkUploadActor extends BaseActor { - private UserService userService = UserServiceImpl.getInstance(); + UserService userService = UserServiceImpl.getInstance(); public void validateBulkUploadFields( String[] csvHeaderLine, String[] allowedFields, Boolean allFieldsMandatory) { diff --git a/service/src/main/java/org/sunbird/actor/bulkupload/LocationBulkUploadBackGroundJobActor.java b/service/src/main/java/org/sunbird/actor/bulkupload/LocationBulkUploadBackGroundJobActor.java index f92ca1751930d3460606384e24199f030c4c1d6f..856888318dcb7fe0967b41d2709e1100f3c2f67b 100644 --- a/service/src/main/java/org/sunbird/actor/bulkupload/LocationBulkUploadBackGroundJobActor.java +++ b/service/src/main/java/org/sunbird/actor/bulkupload/LocationBulkUploadBackGroundJobActor.java @@ -79,7 +79,7 @@ public class LocationBulkUploadBackGroundJobActor extends BaseBulkUploadBackgrou if (checkMandatoryFields(row, JsonKey.CODE)) { Location location = null; try { - List<Location> locationList = locationService.locationSearch(JsonKey.CODE, (String) row.get(JsonKey.CODE), context); + List<Location> locationList = locationService.locationSearch(JsonKey.CODE, row.get(JsonKey.CODE), context); if (CollectionUtils.isNotEmpty(locationList)) { location = locationList.get(0); } @@ -177,7 +177,6 @@ public class LocationBulkUploadBackGroundJobActor extends BaseBulkUploadBackgrou Request request = new Request(); request.getRequest().putAll(row); request.setRequestContext(context); - ObjectMapper mapper = new ObjectMapper(); String locationId = ""; try { request.setOperation(LocationActorOperation.CREATE_LOCATION.getValue()); diff --git a/service/src/main/java/org/sunbird/actor/bulkupload/OrgBulkUploadActor.java b/service/src/main/java/org/sunbird/actor/bulkupload/OrgBulkUploadActor.java index e14646c6dd41ea4c07995dd214c1de9d50682286..99592c357466515d62ec5fac3687dbb873837c41 100644 --- a/service/src/main/java/org/sunbird/actor/bulkupload/OrgBulkUploadActor.java +++ b/service/src/main/java/org/sunbird/actor/bulkupload/OrgBulkUploadActor.java @@ -10,11 +10,6 @@ import java.util.Map; import java.util.stream.Collectors; import javax.inject.Inject; import javax.inject.Named; -import org.sunbird.client.systemsettings.SystemSettingClient; -import org.sunbird.client.systemsettings.impl.SystemSettingClientImpl; -import org.sunbird.common.ElasticSearchHelper; -import org.sunbird.common.factory.EsClientFactory; -import org.sunbird.common.inf.ElasticSearchService; import org.sunbird.dao.bulkupload.BulkUploadProcessDao; import org.sunbird.dao.bulkupload.impl.BulkUploadProcessDaoImpl; import org.sunbird.exception.ProjectCommonException; @@ -24,25 +19,23 @@ import org.sunbird.model.bulkupload.BulkUploadProcess; import org.sunbird.operations.BulkUploadActorOperation; import org.sunbird.request.Request; import org.sunbird.request.RequestContext; +import org.sunbird.service.organisation.OrgService; +import org.sunbird.service.organisation.impl.OrgServiceImpl; +import org.sunbird.service.systemsettings.SystemSettingsService; import org.sunbird.telemetry.dto.TelemetryEnvKey; import org.sunbird.util.DataCacheHandler; import org.sunbird.util.ProjectUtil; import org.sunbird.util.Util; -import scala.concurrent.Future; public class OrgBulkUploadActor extends BaseBulkUploadActor { - private SystemSettingClient systemSettingClient = new SystemSettingClientImpl(); - private ElasticSearchService esService = EsClientFactory.getInstance(JsonKey.REST); + private final OrgService orgService = OrgServiceImpl.getInstance(); + private final SystemSettingsService systemSettingsService = new SystemSettingsService(); @Inject @Named("org_bulk_upload_background_job_actor") private ActorRef orgBulkUploadBackgroundJobActor; - @Inject - @Named("system_settings_actor") - private ActorRef systemSettingsActor; - @Override public void onReceive(Request request) throws Throwable { Util.initializeContext(request, TelemetryEnvKey.ORGANISATION); @@ -58,8 +51,7 @@ public class OrgBulkUploadActor extends BaseBulkUploadActor { private void upload(Request request) throws IOException { Map<String, Object> req = (Map<String, Object>) request.getRequest().get(JsonKey.DATA); Object dataObject = - systemSettingClient.getSystemSettingByFieldAndKey( - systemSettingsActor, + systemSettingsService.getSystemSettingByFieldAndKey( "orgProfileConfig", "csv", new TypeReference<Map>() {}, @@ -125,10 +117,10 @@ public class OrgBulkUploadActor extends BaseBulkUploadActor { fileByteArray = (byte[]) req.get(JsonKey.FILE); } HashMap<String, Object> additionalInfo = new HashMap<>(); - Map<String, Object> user = getUser((String) req.get(JsonKey.CREATED_BY), context); + Map<String, Object> user = userService.getUserDetailsById((String) req.get(JsonKey.CREATED_BY), context); if (user != null) { String rootOrgId = (String) user.get(JsonKey.ROOT_ORG_ID); - Map<String, Object> org = getOrg(rootOrgId, context); + Map<String, Object> org = orgService.getOrgById(rootOrgId, context); if (org != null) { if (org.get(JsonKey.STATUS) == null || (int) org.get(JsonKey.STATUS) == ProjectUtil.OrgStatus.ACTIVE.getValue()) { @@ -158,26 +150,4 @@ public class OrgBulkUploadActor extends BaseBulkUploadActor { context); } - Map<String, Object> getUser(String userId, RequestContext context) { - Future<Map<String, Object>> resultF = - esService.getDataByIdentifier(ProjectUtil.EsType.user.getTypeName(), userId, context); - Map<String, Object> result = - (Map<String, Object>) ElasticSearchHelper.getResponseFromFuture(resultF); - if (result != null || result.size() > 0) { - return result; - } - return null; - } - - Map<String, Object> getOrg(String orgId, RequestContext context) { - Future<Map<String, Object>> resultF = - esService.getDataByIdentifier( - ProjectUtil.EsType.organisation.getTypeName(), orgId, context); - Map<String, Object> result = - (Map<String, Object>) ElasticSearchHelper.getResponseFromFuture(resultF); - if (result != null && result.size() > 0) { - return result; - } - return null; - } } diff --git a/service/src/main/java/org/sunbird/actor/bulkupload/OrgBulkUploadBackgroundJobActor.java b/service/src/main/java/org/sunbird/actor/bulkupload/OrgBulkUploadBackgroundJobActor.java index af30c7ebf15c0799f53ec50240575e30d3b5f461..8220feb442c3ae7c1b37c252d8e59bd6e601832d 100644 --- a/service/src/main/java/org/sunbird/actor/bulkupload/OrgBulkUploadBackgroundJobActor.java +++ b/service/src/main/java/org/sunbird/actor/bulkupload/OrgBulkUploadBackgroundJobActor.java @@ -1,6 +1,8 @@ package org.sunbird.actor.bulkupload; import akka.actor.ActorRef; +import akka.pattern.Patterns; +import akka.util.Timeout; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; @@ -10,15 +12,13 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.TimeUnit; import javax.inject.Inject; import javax.inject.Named; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; -import org.sunbird.client.org.OrganisationClient; -import org.sunbird.client.org.impl.OrganisationClientImpl; -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.keys.JsonKey; import org.sunbird.model.bulkupload.BulkUploadProcess; @@ -26,23 +26,24 @@ import org.sunbird.model.bulkupload.BulkUploadProcessTask; import org.sunbird.model.location.Location; import org.sunbird.model.organisation.OrgTypeEnum; import org.sunbird.model.organisation.Organisation; +import org.sunbird.operations.OrganisationActorOperation; import org.sunbird.request.Request; import org.sunbird.request.RequestContext; +import org.sunbird.response.Response; import org.sunbird.service.location.LocationService; import org.sunbird.service.location.LocationServiceImpl; +import org.sunbird.service.systemsettings.SystemSettingsService; import org.sunbird.telemetry.dto.TelemetryEnvKey; import org.sunbird.util.ProjectUtil; import org.sunbird.util.Util; +import scala.concurrent.Await; +import scala.concurrent.Future; +import scala.concurrent.duration.Duration; public class OrgBulkUploadBackgroundJobActor extends BaseBulkUploadBackgroundJobActor { - private OrganisationClient orgClient = new OrganisationClientImpl(); - private SystemSettingClient systemSettingClient = new SystemSettingClientImpl(); private final LocationService locationService = new LocationServiceImpl(); - - @Inject - @Named("system_settings_actor") - private ActorRef systemSettingsActor; + private final SystemSettingsService systemSettingsService = new SystemSettingsService(); @Inject @Named("location_actor") @@ -97,8 +98,7 @@ public class OrgBulkUploadBackgroundJobActor extends BaseBulkUploadBackgroundJob try { Map<String, Object> orgMap = mapper.readValue(data, Map.class); Object mandatoryColumnsObject = - systemSettingClient.getSystemSettingByFieldAndKey( - systemSettingsActor, + systemSettingsService.getSystemSettingByFieldAndKey( "orgProfileConfig", "csv.mandatoryColumns", new TypeReference<String[]>() {}, @@ -204,7 +204,7 @@ public class OrgBulkUploadBackgroundJobActor extends BaseBulkUploadBackgroundJob String orgId; row.put(JsonKey.ORG_TYPE, OrgTypeEnum.getTypeByValue(org.getOrganisationType())); try { - orgId = orgClient.createOrg(organisationManagementActor, row, context); + orgId = upsertOrg(organisationManagementActor, row, OrganisationActorOperation.CREATE_ORG.getValue(), context); } catch (Exception ex) { logger.error( context, @@ -242,7 +242,7 @@ public class OrgBulkUploadBackgroundJobActor extends BaseBulkUploadBackgroundJob row.put(JsonKey.ORG_TYPE, OrgTypeEnum.getTypeByValue(org.getOrganisationType())); try { row.put(JsonKey.ORGANISATION_ID, org.getId()); - orgClient.updateOrg(organisationManagementActor, row, context); + upsertOrg(organisationManagementActor, row, OrganisationActorOperation.UPDATE_ORG.getValue(), context); } catch (Exception ex) { logger.error( context, @@ -263,4 +263,41 @@ public class OrgBulkUploadBackgroundJobActor extends BaseBulkUploadBackgroundJob public void preProcessResult(Map<String, Object> result) { // Do nothing } + + private String upsertOrg( + ActorRef actorRef, Map<String, Object> orgMap, String operation, RequestContext context) { + String orgId = null; + Object obj = null; + + Request request = new Request(); + request.setRequestContext(context); + request.setRequest(orgMap); + request.setOperation(operation); + request.getContext().put(JsonKey.CALLER_ID, JsonKey.BULK_ORG_UPLOAD); + try { + Timeout t = new Timeout(Duration.create(10, TimeUnit.SECONDS)); + Future<Object> future = Patterns.ask(actorRef, request, t); + obj = Await.result(future, t.duration()); + } catch (ProjectCommonException pce) { + throw pce; + } catch (Exception e) { + logger.error( + context, "upsertOrg: Exception occurred with error message = " + e.getMessage(), e); + ProjectCommonException.throwServerErrorException( + ResponseCode.unableToCommunicateWithActor, + ResponseCode.unableToCommunicateWithActor.getErrorMessage()); + } + if (obj instanceof Response) { + Response response = (Response) obj; + orgId = (String) response.get(JsonKey.ORGANISATION_ID); + } else if (obj instanceof ProjectCommonException) { + throw (ProjectCommonException) obj; + } else if (obj instanceof Exception) { + throw new ProjectCommonException( + ResponseCode.SERVER_ERROR.getErrorCode(), + ResponseCode.SERVER_ERROR.getErrorMessage(), + ResponseCode.SERVER_ERROR.getResponseCode()); + } + return orgId; + } } diff --git a/service/src/main/java/org/sunbird/actor/bulkupload/UserBulkUploadActor.java b/service/src/main/java/org/sunbird/actor/bulkupload/UserBulkUploadActor.java index d9e0ff8be61dd3ef18f2b67c584809542ae1b4a7..8fbd3e5fba63f3af8a083502bae6d7055acd1c49 100644 --- a/service/src/main/java/org/sunbird/actor/bulkupload/UserBulkUploadActor.java +++ b/service/src/main/java/org/sunbird/actor/bulkupload/UserBulkUploadActor.java @@ -10,29 +10,24 @@ import java.util.Map; import java.util.stream.Collectors; import javax.inject.Inject; import javax.inject.Named; -import org.sunbird.client.systemsettings.SystemSettingClient; -import org.sunbird.client.systemsettings.impl.SystemSettingClientImpl; import org.sunbird.keys.JsonKey; import org.sunbird.model.bulkupload.BulkUploadProcess; import org.sunbird.operations.BulkUploadActorOperation; import org.sunbird.request.Request; import org.sunbird.request.RequestContext; +import org.sunbird.service.systemsettings.SystemSettingsService; import org.sunbird.telemetry.dto.TelemetryEnvKey; import org.sunbird.util.DataCacheHandler; import org.sunbird.util.Util; public class UserBulkUploadActor extends BaseBulkUploadActor { - private SystemSettingClient systemSettingClient = new SystemSettingClientImpl(); + private final SystemSettingsService systemSettingsService = new SystemSettingsService(); @Inject @Named("user_bulk_upload_background_job_actor") private ActorRef userBulkUploadBackgroundJobActor; - @Inject - @Named("system_settings_actor") - private ActorRef systemSettingsActor; - @Override public void onReceive(Request request) throws Throwable { Util.initializeContext(request, TelemetryEnvKey.USER); @@ -48,8 +43,7 @@ public class UserBulkUploadActor extends BaseBulkUploadActor { private void upload(Request request) throws IOException { Map<String, Object> req = (Map<String, Object>) request.getRequest().get(JsonKey.DATA); Object dataObject = - systemSettingClient.getSystemSettingByFieldAndKey( - systemSettingsActor, + systemSettingsService.getSystemSettingByFieldAndKey( "userProfileConfig", "csv", new TypeReference<Map>() {}, diff --git a/service/src/main/java/org/sunbird/actor/bulkupload/UserBulkUploadBackgroundJobActor.java b/service/src/main/java/org/sunbird/actor/bulkupload/UserBulkUploadBackgroundJobActor.java index 07ebc44c7773676b762d85c49195e0078831ad95..9622b489ea581830653cd2ea96ed6c787721b368 100644 --- a/service/src/main/java/org/sunbird/actor/bulkupload/UserBulkUploadBackgroundJobActor.java +++ b/service/src/main/java/org/sunbird/actor/bulkupload/UserBulkUploadBackgroundJobActor.java @@ -27,7 +27,10 @@ import org.sunbird.model.organisation.Organisation; import org.sunbird.operations.ActorOperations; import org.sunbird.request.Request; import org.sunbird.request.RequestContext; +import org.sunbird.service.organisation.OrgService; +import org.sunbird.service.organisation.impl.OrgServiceImpl; import org.sunbird.service.role.RoleService; +import org.sunbird.service.systemsettings.SystemSettingsService; import org.sunbird.telemetry.dto.TelemetryEnvKey; import org.sunbird.util.ProjectUtil; import org.sunbird.util.UserUtility; @@ -36,13 +39,9 @@ import org.sunbird.util.Util; public class UserBulkUploadBackgroundJobActor extends BaseBulkUploadBackgroundJobActor { private UserClient userClient = UserClientImpl.getInstance(); - private OrganisationClient organisationClient = OrganisationClientImpl.getInstance(); - private SystemSettingClient systemSettingClient = SystemSettingClientImpl.getInstance(); + private final OrgService orgService = OrgServiceImpl.getInstance(); private UserRequestValidator userRequestValidator = new UserRequestValidator(); - - @Inject - @Named("system_settings_actor") - private ActorRef systemSettingsActor; + private final SystemSettingsService systemSettingsService = new SystemSettingsService(); @Inject @Named("user_role_actor") @@ -117,8 +116,7 @@ public class UserBulkUploadBackgroundJobActor extends BaseBulkUploadBackgroundJo ObjectMapper mapper = new ObjectMapper(); Map<String, Object> userMap = mapper.readValue(data, Map.class); String[] mandatoryColumnsObject = - systemSettingClient.getSystemSettingByFieldAndKey( - systemSettingsActor, + systemSettingsService.getSystemSettingByFieldAndKey( "userProfileConfig", "csv.mandatoryColumns", new TypeReference<String[]>() {}, @@ -333,12 +331,13 @@ public class UserBulkUploadBackgroundJobActor extends BaseBulkUploadBackgroundJo Map<String, Object> filters = new HashMap<>(); filters.put( JsonKey.EXTERNAL_ID, ((String) userMap.get(JsonKey.ORG_EXTERNAL_ID)).toLowerCase()); - if (CollectionUtils.isNotEmpty(organisationClient.esSearchOrgByFilter(filters, context))) { - return organisationClient.esSearchOrgByFilter(filters, context).get(0); + List<Organisation> orgList = orgService.organisationObjSearch(filters, context); + if (CollectionUtils.isNotEmpty(orgList)) { + return orgList.get(0); } return null; } else if (StringUtils.isNotBlank((String) userMap.get(JsonKey.ORG_ID))) { - return organisationClient.esGetOrgById((String) userMap.get(JsonKey.ORG_ID), context); + return orgService.getOrgObjById((String) userMap.get(JsonKey.ORG_ID), context); } return null; } diff --git a/service/src/main/java/org/sunbird/actor/user/UserMergeActor.java b/service/src/main/java/org/sunbird/actor/user/UserMergeActor.java index 2d5ce4752a26374466cf3e9b246703e2d5472aa3..d9a2463c4ae79390b1d22ae4357e3c0202c16071 100644 --- a/service/src/main/java/org/sunbird/actor/user/UserMergeActor.java +++ b/service/src/main/java/org/sunbird/actor/user/UserMergeActor.java @@ -31,6 +31,7 @@ import org.sunbird.operations.ActorOperations; import org.sunbird.request.Request; import org.sunbird.request.RequestContext; import org.sunbird.response.Response; +import org.sunbird.service.systemsettings.SystemSettingsService; import org.sunbird.service.user.UserService; import org.sunbird.service.user.impl.UserMergeServiceImpl; import org.sunbird.service.user.impl.UserServiceImpl; @@ -54,11 +55,7 @@ public class UserMergeActor extends UserBaseActor { private final ObjectMapper objectMapper = new ObjectMapper(); private final UserService userService = UserServiceImpl.getInstance(); private final SSOManager keyCloakService = SSOServiceFactory.getInstance(); - private final SystemSettingClient systemSettingClient = SystemSettingClientImpl.getInstance(); - - @Inject - @Named("system_settings_actor") - private ActorRef systemSettingsActor; + private final SystemSettingsService systemSettingsService = new SystemSettingsService(); @Inject @Named("background_job_manager_actor") @@ -174,8 +171,8 @@ public class UserMergeActor extends UserBaseActor { custodianId = configSettingMap.get(JsonKey.CUSTODIAN_ORG_ID); if (custodianId == null || custodianId.isEmpty()) { SystemSetting custodianIdSetting = - systemSettingClient.getSystemSettingByField( - systemSettingsActor, JsonKey.CUSTODIAN_ORG_ID, context); + systemSettingsService.getSystemSettingByKey( + JsonKey.CUSTODIAN_ORG_ID, context); if (custodianIdSetting != null) { configSettingMap.put(custodianIdSetting.getId(), custodianIdSetting.getValue()); custodianId = custodianIdSetting.getValue(); diff --git a/service/src/main/java/org/sunbird/actor/user/UserUpdateActor.java b/service/src/main/java/org/sunbird/actor/user/UserUpdateActor.java index 698edb879dd02c10bd5073dbff6c2f636c32080d..79f784710b306a93c41d721587dc19a3e3d54f9e 100644 --- a/service/src/main/java/org/sunbird/actor/user/UserUpdateActor.java +++ b/service/src/main/java/org/sunbird/actor/user/UserUpdateActor.java @@ -34,6 +34,8 @@ import org.sunbird.operations.ActorOperations; import org.sunbird.request.Request; import org.sunbird.request.RequestContext; import org.sunbird.response.Response; +import org.sunbird.service.organisation.OrgService; +import org.sunbird.service.organisation.impl.OrgServiceImpl; import org.sunbird.service.user.AssociationMechanism; import org.sunbird.service.user.UserService; import org.sunbird.service.user.impl.UserServiceImpl; @@ -47,6 +49,7 @@ public class UserUpdateActor extends UserBaseActor { private final UserRequestValidator userRequestValidator = new UserRequestValidator(); private final ObjectMapper mapper = new ObjectMapper(); private final UserService userService = UserServiceImpl.getInstance(); + private final OrgService orgService = OrgServiceImpl.getInstance(); private final UserSelfDeclarationDao userSelfDeclarationDao = UserSelfDeclarationDaoImpl.getInstance(); @@ -231,17 +234,15 @@ public class UserUpdateActor extends UserBaseActor { logger.info( actorMessage.getRequestContext(), "fetching org by orgExternalId and orgLocationId : " + filters); - List<Organisation> organisations = - organisationClient.esSearchOrgByFilter(filters, actorMessage.getRequestContext()); + List<Map<String, Object>> organisations = + orgService.organisationSearch(filters, actorMessage.getRequestContext()); if (organisations.size() == 0 || organisations.size() > 1) { logger.info( actorMessage.getRequestContext(), "Got empty or more than one search result by orgExternalId and orgLocationId : " + filters); } else { - Map<String, Object> org = - (Map<String, Object>) mapper.convertValue(organisations.get(0), Map.class); - + Map<String, Object> org = organisations.get(0); if (MapUtils.isNotEmpty(org)) { orgList.add(org); } diff --git a/service/src/main/java/org/sunbird/client/location/LocationClient.java b/service/src/main/java/org/sunbird/client/location/LocationClient.java deleted file mode 100644 index 9f1752393ec61e656856ae4a28303596bac947a1..0000000000000000000000000000000000000000 --- a/service/src/main/java/org/sunbird/client/location/LocationClient.java +++ /dev/null @@ -1,75 +0,0 @@ -package org.sunbird.client.location; - -import akka.actor.ActorRef; -import java.util.List; -import org.sunbird.model.location.Location; -import org.sunbird.model.location.UpsertLocationRequest; -import org.sunbird.request.RequestContext; - -/** - * This interface defines methods supported by Location service. - * - * @author Amit Kumar - */ -public interface LocationClient { - - /** - * @desc This method will fetch location details by list of code. - * @param actorRef Actor reference. - * @param codeList List of location code. - * @param context - * @return List of location. - */ - List<Location> getLocationsByCodes( - ActorRef actorRef, List<String> codeList, RequestContext context); - - public List<Location> getLocationByIds( - ActorRef actorRef, List<String> idsList, RequestContext context); - /** - * @desc This method will fetch location details by id. - * @param actorRef Actor reference. - * @param id Location id. - * @param context - * @return Location details. - */ - Location getLocationById(ActorRef actorRef, String id, RequestContext context); - - /** - * @desc This method will fetch location details by code. - * @param actorRef Actor reference. - * @param locationCode location code. - * @param context - * @return Location details. - */ - Location getLocationByCode(ActorRef actorRef, String locationCode, RequestContext context); - - List<Location> getLocationByCodes( - ActorRef actorRef, List<String> locationCode, RequestContext context); - - /** - * @desc This method will create Location and returns the response. - * @param actorRef Actor reference. - * @param location Location details. - * @param context - * @return Location id. - */ - String createLocation(ActorRef actorRef, UpsertLocationRequest location, RequestContext context); - - /** - * @desc This method will update location details. - * @param actorRef Actor reference. - * @param location Location details. - * @param context - */ - void updateLocation(ActorRef actorRef, UpsertLocationRequest location, RequestContext context); - - /** - * @desc For given location codes, fetch location IDs (including, if any, those of its parent or - * ancestor(s) locations). - * @param actorRef Actor reference. - * @param codes List of location codes. - * @param context - * @return List of related location IDs - */ - List<String> getRelatedLocationIds(ActorRef actorRef, List<String> codes, RequestContext context); -} diff --git a/service/src/main/java/org/sunbird/client/location/impl/LocationClientImpl.java b/service/src/main/java/org/sunbird/client/location/impl/LocationClientImpl.java deleted file mode 100644 index 3e9ced4ccf2a6f483d6bbb75feaab8923d80a864..0000000000000000000000000000000000000000 --- a/service/src/main/java/org/sunbird/client/location/impl/LocationClientImpl.java +++ /dev/null @@ -1,197 +0,0 @@ -package org.sunbird.client.location.impl; - -import akka.actor.ActorRef; -import akka.pattern.Patterns; -import akka.util.Timeout; -import com.fasterxml.jackson.databind.ObjectMapper; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.concurrent.TimeUnit; -import java.util.stream.Collectors; -import org.apache.commons.collections.CollectionUtils; -import org.sunbird.client.location.LocationClient; -import org.sunbird.exception.ProjectCommonException; -import org.sunbird.exception.ResponseCode; -import org.sunbird.keys.JsonKey; -import org.sunbird.logging.LoggerUtil; -import org.sunbird.model.location.Location; -import org.sunbird.model.location.UpsertLocationRequest; -import org.sunbird.operations.LocationActorOperation; -import org.sunbird.request.Request; -import org.sunbird.request.RequestContext; -import org.sunbird.response.Response; -import scala.concurrent.Await; -import scala.concurrent.Future; -import scala.concurrent.duration.Duration; - -public class LocationClientImpl implements LocationClient { - private final LoggerUtil logger = new LoggerUtil(LocationClientImpl.class); - private final ObjectMapper mapper = new ObjectMapper(); - public static LocationClient locationClient = null; - - public static synchronized LocationClient getInstance() { - if (locationClient == null) locationClient = new LocationClientImpl(); - return locationClient; - } - - @Override - public List<Location> getLocationsByCodes( - ActorRef actorRef, List<String> codeList, RequestContext context) { - return getSearchResponse(actorRef, JsonKey.CODE, codeList, context); - } - - @Override - public List<Location> getLocationByIds( - ActorRef actorRef, List<String> idsList, RequestContext context) { - return getSearchResponse(actorRef, JsonKey.ID, idsList, context); - } - - @Override - public Location getLocationById(ActorRef actorRef, String id, RequestContext context) { - List<Location> locationList = getSearchResponse(actorRef, JsonKey.ID, id, context); - if (CollectionUtils.isNotEmpty(locationList)) { - return locationList.get(0); - } else { - return null; - } - } - - private List<Location> getSearchResponse( - ActorRef actorRef, String param, Object value, RequestContext context) { - List<Location> response = null; - Map<String, Object> filters = new HashMap<>(); - Map<String, Object> searchRequestMap = new HashMap<>(); - filters.put(param, value); - searchRequestMap.put(JsonKey.FILTERS, filters); - Request request = new Request(); - request.setOperation(LocationActorOperation.SEARCH_LOCATION.getValue()); - request.getRequest().putAll(searchRequestMap); - logger.info(context, "callSearchLocation "); - try { - Object obj = actorCall(actorRef, request, context); - if (obj instanceof Response) { - Response responseObj = (Response) obj; - List<Map<String, Object>> responseList = - (List<Map<String, Object>>) responseObj.getResult().get(JsonKey.RESPONSE); - return responseList - .stream() - .map(s -> mapper.convertValue(s, Location.class)) - .collect(Collectors.toList()); - } else { - response = new ArrayList<>(); - } - } catch (Exception ex) { - response = new ArrayList<>(); - } - return response; - } - - @Override - public Location getLocationByCode( - ActorRef actorRef, String locationCode, RequestContext context) { - String param = JsonKey.CODE; - Object value = locationCode; - List<Location> locationList = getSearchResponse(actorRef, param, value, context); - if (CollectionUtils.isNotEmpty(locationList)) { - return locationList.get(0); - } else { - return null; - } - } - - @Override - public List<Location> getLocationByCodes( - ActorRef actorRef, List<String> locationCode, RequestContext context) { - String param = JsonKey.CODE; - Object value = locationCode; - List<Location> locationList = getSearchResponse(actorRef, param, value, context); - if (CollectionUtils.isNotEmpty(locationList)) { - return locationList; - } else { - return null; - } - } - - @Override - public String createLocation( - ActorRef actorRef, UpsertLocationRequest location, RequestContext context) { - Request request = new Request(); - String locationId = null; - request.getRequest().putAll(mapper.convertValue(location, Map.class)); - request.setOperation(LocationActorOperation.CREATE_LOCATION.getValue()); - logger.info(context, "callCreateLocation "); - Object obj = actorCall(actorRef, request, context); - if (obj instanceof Response) { - Response response = (Response) obj; - locationId = (String) response.get(JsonKey.ID); - } - return locationId; - } - - @Override - public void updateLocation( - ActorRef actorRef, UpsertLocationRequest location, RequestContext context) { - Request request = new Request(); - request.getRequest().putAll(mapper.convertValue(location, Map.class)); - request.setOperation(LocationActorOperation.UPDATE_LOCATION.getValue()); - logger.info(context, "callUpdateLocation "); - Object obj = actorCall(actorRef, request, context); - } - - @Override - public List<String> getRelatedLocationIds( - ActorRef actorRef, List<String> codes, RequestContext context) { - Map<String, Object> requestMap = new HashMap<>(); - requestMap.put(JsonKey.LOCATION_CODES, codes); - - Request request = new Request(); - request.setOperation(LocationActorOperation.GET_RELATED_LOCATION_IDS.getValue()); - request.getRequest().putAll(requestMap); - - logger.info(context, "getRelatedLocationIds called"); - Object obj = actorCall(actorRef, request, context); - - if (obj instanceof Response) { - Response responseObj = (Response) obj; - List<String> responseList = (List<String>) responseObj.getResult().get(JsonKey.RESPONSE); - return responseList; - } - - return new ArrayList<>(); - } - - private Object actorCall(ActorRef actorRef, Request request, RequestContext context) { - Object obj = null; - try { - Timeout t = new Timeout(Duration.create(10, TimeUnit.SECONDS)); - Future<Object> future = Patterns.ask(actorRef, request, t); - obj = Await.result(future, t.duration()); - } catch (ProjectCommonException pce) { - throw pce; - } catch (Exception e) { - logger.error( - context, - "Unable to communicate with actor: Exception occurred with error message = " - + e.getMessage(), - e); - ProjectCommonException.throwServerErrorException( - ResponseCode.unableToCommunicateWithActor, - ResponseCode.unableToCommunicateWithActor.getErrorMessage()); - } - checkLocationResponseForException(obj); - return obj; - } - - private void checkLocationResponseForException(Object obj) { - if (obj instanceof ProjectCommonException) { - throw (ProjectCommonException) obj; - } else if (obj instanceof Exception) { - throw new ProjectCommonException( - ResponseCode.SERVER_ERROR.getErrorCode(), - ResponseCode.SERVER_ERROR.getErrorMessage(), - ResponseCode.SERVER_ERROR.getResponseCode()); - } - } -} diff --git a/service/src/main/java/org/sunbird/service/organisation/OrgService.java b/service/src/main/java/org/sunbird/service/organisation/OrgService.java index ae131874348571d317ac965718189de630a3846f..953518ab8e569f849095a44e75057476920c5a94 100644 --- a/service/src/main/java/org/sunbird/service/organisation/OrgService.java +++ b/service/src/main/java/org/sunbird/service/organisation/OrgService.java @@ -3,6 +3,7 @@ package org.sunbird.service.organisation; import java.util.List; import java.util.Map; import org.sunbird.dto.SearchDTO; +import org.sunbird.model.organisation.Organisation; import org.sunbird.request.RequestContext; import org.sunbird.response.Response; import scala.concurrent.Future; @@ -11,6 +12,8 @@ public interface OrgService { Map<String, Object> getOrgById(String orgId, RequestContext context); + Organisation getOrgObjById(String orgId, RequestContext context); + List<Map<String, Object>> getOrgByIds(List<String> orgIds, RequestContext context); List<Map<String, Object>> getOrgByIds( @@ -25,6 +28,8 @@ public interface OrgService { List<Map<String, Object>> organisationSearch(Map<String, Object> filters, RequestContext context); + List<Organisation> organisationObjSearch(Map<String, Object> filters, RequestContext context); + Future<Map<String, Object>> searchOrg(SearchDTO searchDTO, RequestContext context); void createOrgExternalIdRecord( diff --git a/service/src/main/java/org/sunbird/service/organisation/impl/OrgServiceImpl.java b/service/src/main/java/org/sunbird/service/organisation/impl/OrgServiceImpl.java index 43eca2dc64b63a30a0ccbc687b2c0f25cb919d51..b3c45afbf5f77a15e585b4db73ebcc27dab80a21 100644 --- a/service/src/main/java/org/sunbird/service/organisation/impl/OrgServiceImpl.java +++ b/service/src/main/java/org/sunbird/service/organisation/impl/OrgServiceImpl.java @@ -14,6 +14,7 @@ import org.sunbird.exception.ResponseCode; import org.sunbird.http.HttpClientUtil; import org.sunbird.keys.JsonKey; import org.sunbird.logging.LoggerUtil; +import org.sunbird.model.organisation.Organisation; import org.sunbird.request.RequestContext; import org.sunbird.response.Response; import org.sunbird.service.organisation.OrgExternalService; @@ -48,6 +49,16 @@ public class OrgServiceImpl implements OrgService { return orgDao.getOrgById(orgId, context); } + @Override + public Organisation getOrgObjById(String orgId, RequestContext context) { + Map<String, Object> orgMap = orgDao.getOrgById(orgId, context); + if (MapUtils.isEmpty(orgMap)) { + return null; + } else { + return mapper.convertValue(orgMap, Organisation.class); + } + } + @Override public List<Map<String, Object>> getOrgByIds(List<String> orgIds, RequestContext context) { return getOrgByIds(orgIds, Collections.emptyList(), context); @@ -91,6 +102,21 @@ public class OrgServiceImpl implements OrgService { return orgResponseList; } + public List<Organisation> organisationObjSearch(Map<String, Object> filters, RequestContext context) { + List<Organisation> orgList = new ArrayList<>(); + ObjectMapper objectMapper = new ObjectMapper(); + List<Map<String, Object>> orgMapList = organisationSearch(filters, context); + if (CollectionUtils.isNotEmpty(orgMapList)) { + for (Map<String, Object> orgMap : orgMapList) { + orgMap.put(JsonKey.CONTACT_DETAILS, String.valueOf(orgMap.get(JsonKey.CONTACT_DETAILS))); + orgList.add(objectMapper.convertValue(orgMap, Organisation.class)); + } + return orgList; + } else { + return Collections.emptyList(); + } + } + @Override public Future<Map<String, Object>> searchOrg(SearchDTO searchDTO, RequestContext context) { return orgDao.search(searchDTO, context); diff --git a/service/src/main/java/org/sunbird/service/systemsettings/SystemSettingsService.java b/service/src/main/java/org/sunbird/service/systemsettings/SystemSettingsService.java index 39c1817334caa63672f9e0a4957ee25875ea2331..edb83c1565fdecf1e8b18005953582a68e4d4ea2 100644 --- a/service/src/main/java/org/sunbird/service/systemsettings/SystemSettingsService.java +++ b/service/src/main/java/org/sunbird/service/systemsettings/SystemSettingsService.java @@ -1,5 +1,6 @@ package org.sunbird.service.systemsettings; +import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import java.util.ArrayList; import java.util.List; @@ -8,6 +9,7 @@ import org.apache.commons.collections.MapUtils; import org.sunbird.dao.systemsettings.impl.SystemSettingDaoImpl; import org.sunbird.exception.ProjectCommonException; import org.sunbird.exception.ResponseCode; +import org.sunbird.logging.LoggerUtil; import org.sunbird.model.systemsettings.SystemSetting; import org.sunbird.request.RequestContext; import org.sunbird.response.Response; @@ -15,6 +17,7 @@ import org.sunbird.util.DataCacheHandler; public class SystemSettingsService { + private final LoggerUtil logger = new LoggerUtil(SystemSettingsService.class); private final SystemSettingDaoImpl systemSettingDaoImpl = new SystemSettingDaoImpl(); public SystemSetting getSystemSettingByKey(String key, RequestContext context) { @@ -55,4 +58,31 @@ public class SystemSettingsService { SystemSetting systemSetting = mapper.convertValue(request, SystemSetting.class); return systemSettingDaoImpl.write(systemSetting, context); } + + public <T> T getSystemSettingByFieldAndKey( + String field, + String key, + TypeReference typeReference, + RequestContext context) { + SystemSetting systemSetting = getSystemSettingByKey(field, context); + ObjectMapper objectMapper = new ObjectMapper(); + if (systemSetting != null) { + try { + Map<String, Object> valueMap = objectMapper.readValue(systemSetting.getValue(), Map.class); + String[] keys = key.split("\\."); + int numKeys = keys.length; + for (int i = 0; i < numKeys - 1; i++) { + valueMap = objectMapper.convertValue(valueMap.get(keys[i]), Map.class); + } + return (T) objectMapper.convertValue(valueMap.get(keys[numKeys - 1]), typeReference); + } catch (Exception e) { + logger.error( + context, + "SystemSettingsService:getSystemSettingByFieldAndKey: Exception occurred with error message = " + + e.getMessage(), + e); + } + } + return null; + } } diff --git a/service/src/main/java/org/sunbird/service/user/impl/SSOUserServiceImpl.java b/service/src/main/java/org/sunbird/service/user/impl/SSOUserServiceImpl.java index 97ba702760878b1b6ef4ad509e1cebf1e62a0817..f77912f396cdb67317bae31f5c9f15c206c088b0 100644 --- a/service/src/main/java/org/sunbird/service/user/impl/SSOUserServiceImpl.java +++ b/service/src/main/java/org/sunbird/service/user/impl/SSOUserServiceImpl.java @@ -132,7 +132,7 @@ public class SSOUserServiceImpl implements SSOUserService { } Organisation fetchedOrgById; if (StringUtils.isNotBlank(requestedOrgId)) { - fetchedOrgById = organisationClient.esGetOrgById(requestedOrgId, context); + fetchedOrgById = orgService.getOrgObjById(requestedOrgId, context); if (null == fetchedOrgById) { ProjectCommonException.throwClientErrorException(ResponseCode.invalidOrgData); } diff --git a/service/src/test/java/org/sunbird/actor/bulkupload/UserBulkUploadBackgroundJobActorTest.java b/service/src/test/java/org/sunbird/actor/bulkupload/UserBulkUploadBackgroundJobActorTest.java index 5a38a2b37e4a5cac967723a3aed35d2ad5070d4f..a0acaa01a99010053e2af6b0a6334a35d22dbbad 100644 --- a/service/src/test/java/org/sunbird/actor/bulkupload/UserBulkUploadBackgroundJobActorTest.java +++ b/service/src/test/java/org/sunbird/actor/bulkupload/UserBulkUploadBackgroundJobActorTest.java @@ -23,9 +23,6 @@ import org.powermock.core.classloader.annotations.PowerMockIgnore; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import org.sunbird.actor.core.BaseActor; -import org.sunbird.actor.user.validator.UserRequestValidator; -import org.sunbird.client.org.impl.OrganisationClientImpl; -import org.sunbird.client.systemsettings.impl.SystemSettingClientImpl; import org.sunbird.client.user.impl.UserClientImpl; import org.sunbird.dao.bulkupload.impl.BulkUploadProcessDaoImpl; import org.sunbird.dao.bulkupload.impl.BulkUploadProcessTaskDaoImpl; @@ -47,8 +44,6 @@ import org.sunbird.util.ProjectUtil; TelemetryWriter.class, org.sunbird.datasecurity.impl.ServiceFactory.class, UserClientImpl.class, - OrganisationClientImpl.class, - SystemSettingClientImpl.class, BulkUploadProcessDaoImpl.class, BulkUploadProcess.class, BulkUploadProcessTaskDaoImpl.class, @@ -67,9 +62,6 @@ import org.sunbird.util.ProjectUtil; public class UserBulkUploadBackgroundJobActorTest { private UserClientImpl userClient; - private OrganisationClientImpl organisationClient; - private SystemSettingClientImpl systemSettingClient; - private UserRequestValidator userRequestValidator; private static ActorSystem system; private static final Props props = Props.create(UserBulkUploadBackgroundJobActor.class); @@ -88,23 +80,6 @@ public class UserBulkUploadBackgroundJobActorTest { PowerMockito.mockStatic(UserClientImpl.class); userClient = mock(UserClientImpl.class); when(UserClientImpl.getInstance()).thenReturn(userClient); - PowerMockito.mockStatic(OrganisationClientImpl.class); - organisationClient = mock(OrganisationClientImpl.class); - when(OrganisationClientImpl.getInstance()).thenReturn(organisationClient); - PowerMockito.mockStatic(SystemSettingClientImpl.class); - systemSettingClient = mock(SystemSettingClientImpl.class); - when(SystemSettingClientImpl.getInstance()).thenReturn(systemSettingClient); - userRequestValidator = new UserRequestValidator(); - ActorSelection selection = PowerMockito.mock(ActorSelection.class); - when(systemSettingClient.getSystemSettingByFieldAndKey( - Mockito.any(ActorRef.class), - Mockito.anyString(), - Mockito.anyString(), - Mockito.any(), - Mockito.any())) - .thenReturn(new ArrayList<>().toArray()); - when(organisationClient.esGetOrgById(Mockito.anyString(), Mockito.any())) - .thenReturn(getOrganisation()); doNothing() .when(userClient) .updateUser(Mockito.any(ActorRef.class), Mockito.anyMap(), Mockito.any()); diff --git a/service/src/test/java/org/sunbird/actor/user/SSOUserCreateActorTest.java b/service/src/test/java/org/sunbird/actor/user/SSOUserCreateActorTest.java index c814c89fd6c591b1a13faf948002d21127e4203c..62eea26a813a6cf4207673b2b48669358819e8f7 100644 --- a/service/src/test/java/org/sunbird/actor/user/SSOUserCreateActorTest.java +++ b/service/src/test/java/org/sunbird/actor/user/SSOUserCreateActorTest.java @@ -66,7 +66,7 @@ public class SSOUserCreateActorTest extends UserManagementActorTestBase { organisation.setChannel("anyChannel"); organisation.setRootOrgId("rootOrgId"); organisation.setTenant(false); - when(organisationClient.esGetOrgById(Mockito.anyString(), Mockito.any())) + when(orgService.getOrgObjById(Mockito.anyString(), Mockito.any())) .thenReturn(organisation); boolean result = testScenario( @@ -82,7 +82,7 @@ public class SSOUserCreateActorTest extends UserManagementActorTestBase { organisation.setChannel("anyChannel"); organisation.setRootOrgId("rootOrgId"); organisation.setTenant(false); - when(organisationClient.esGetOrgById(Mockito.anyString(), Mockito.any())) + when(orgService.getOrgObjById(Mockito.anyString(), Mockito.any())) .thenReturn(organisation); Request request = getRequest(true, true, true, getAdditionalMapData(reqMap), ActorOperations.CREATE_USER); @@ -98,7 +98,7 @@ public class SSOUserCreateActorTest extends UserManagementActorTestBase { organisation.setChannel("anyChannel"); organisation.setRootOrgId("rootOrgId"); organisation.setTenant(true); - when(organisationClient.esGetOrgById(Mockito.anyString(), Mockito.any())) + when(orgService.getOrgObjById(Mockito.anyString(), Mockito.any())) .thenReturn(organisation); boolean result = testScenario( diff --git a/service/src/test/java/org/sunbird/actor/user/SSUUserCreateActorTest.java b/service/src/test/java/org/sunbird/actor/user/SSUUserCreateActorTest.java index 60b4487538564adbfd3ef2ec413e09278341b852..16f90fab3e68b464be630d897daf9ecb88631687 100644 --- a/service/src/test/java/org/sunbird/actor/user/SSUUserCreateActorTest.java +++ b/service/src/test/java/org/sunbird/actor/user/SSUUserCreateActorTest.java @@ -23,7 +23,7 @@ public class SSUUserCreateActorTest extends UserManagementActorTestBase { organisation.setChannel("anyChannel"); organisation.setRootOrgId("rootOrgId"); organisation.setTenant(false); - when(organisationClient.esGetOrgById(Mockito.anyString(), Mockito.any())) + when(orgService.getOrgObjById(Mockito.anyString(), Mockito.any())) .thenReturn(organisation); TestKit probe = new TestKit(system); ActorRef subject = system.actorOf(props); diff --git a/service/src/test/java/org/sunbird/actor/user/TenantMigrationActorTest.java b/service/src/test/java/org/sunbird/actor/user/TenantMigrationActorTest.java index 9b1d034738e3d23bba98f7e633faf8bd99787020..3ecff94920d16e80ae9a8131ffe59443b9a4a050 100644 --- a/service/src/test/java/org/sunbird/actor/user/TenantMigrationActorTest.java +++ b/service/src/test/java/org/sunbird/actor/user/TenantMigrationActorTest.java @@ -23,7 +23,6 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import org.sunbird.cassandra.CassandraOperation; import org.sunbird.cassandraimpl.CassandraOperationImpl; -import org.sunbird.client.systemsettings.impl.SystemSettingClientImpl; import org.sunbird.common.Constants; import org.sunbird.common.ElasticSearchRestHighImpl; import org.sunbird.common.factory.EsClientFactory; @@ -48,7 +47,6 @@ import scala.concurrent.Promise; @PrepareForTest({ ServiceFactory.class, CassandraOperationImpl.class, - SystemSettingClientImpl.class, DataCacheHandler.class, UserLookupDao.class, UserLookupDaoImpl.class, @@ -109,16 +107,6 @@ public class TenantMigrationActorTest { Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(RequestContext.class))) .thenReturn(upsertResponse); - PowerMockito.mockStatic(SystemSettingClientImpl.class); - SystemSettingClientImpl systemSettingClient = mock(SystemSettingClientImpl.class); - when(SystemSettingClientImpl.getInstance()).thenReturn(systemSettingClient); - when(systemSettingClient.getSystemSettingByFieldAndKey( - Mockito.any(ActorRef.class), - Mockito.anyString(), - Mockito.anyString(), - Mockito.anyObject(), - Mockito.any())) - .thenReturn(new HashMap<>()); Response response = new Response(); response.put(JsonKey.RESPONSE, JsonKey.SUCCESS); when(cassandraOperation.insertRecord( @@ -133,21 +121,32 @@ public class TenantMigrationActorTest { orgs.put(JsonKey.USER_ID, "anyUserId"); orgs.put(JsonKey.ORGANISATION_ID, "anyRootOrgId"); listMap.add(orgs); - Map<String, Object> userDetails = new HashMap<>(); - userDetails.put(JsonKey.ROOT_ORG_ID, "anyRootOrgId"); - userDetails.put(JsonKey.ORGANISATIONS, listMap); + + Map<String, Object> userDetails1 = new HashMap<>(); + userDetails1.put(JsonKey.ROOT_ORG_ID, "anyRootOrgId"); + userDetails1.put(JsonKey.ORGANISATIONS, listMap); Response response1 = new Response(); - List<Map<String, Object>> list = new ArrayList<>(); - list.add(userDetails); - response1.getResult().put(JsonKey.RESPONSE, list); + List<Map<String, Object>> list1 = new ArrayList<>(); + list1.add(userDetails1); + response1.getResult().put(JsonKey.RESPONSE, list1); + + Map<String, Object> userDetails2 = new HashMap<>(); + userDetails2.put(JsonKey.ROOT_ORG_ID, "anyRootOrgId"); + userDetails2.put(JsonKey.ORGANISATIONS, listMap); + Response response2 = new Response(); + List<Map<String, Object>> list2 = new ArrayList<>(); + list2.add(userDetails2); + response2.getResult().put(JsonKey.RESPONSE, list2); PowerMockito.when( cassandraOperation.getRecordById( Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any())) - .thenReturn(response1); + .thenReturn(response1) + .thenReturn(response2); PowerMockito.when( cassandraOperation.getRecordById( Mockito.anyString(), Mockito.anyString(), Mockito.anyMap(), Mockito.any())) - .thenReturn(getSelfDeclarationResponse()); + .thenReturn(getSelfDeclarationResponse()) + .thenReturn(getSelfDeclarationResponse()); PowerMockito.when( cassandraOperation.updateRecord( Mockito.anyString(), Mockito.anyString(), Mockito.anyMap(), Mockito.any())) @@ -164,7 +163,8 @@ public class TenantMigrationActorTest { .thenReturn(updateResponse); when(cassandraOperation.getRecordsByCompositeKey( Mockito.anyString(), Mockito.anyString(), Mockito.anyMap(), Mockito.any())) - .thenReturn(getOrgFromCassandra()); + .thenReturn(getOrgFromCassandra()) + .thenReturn(getOrgFromCassandra()); } public static Response getEsResponse() { @@ -289,7 +289,7 @@ public class TenantMigrationActorTest { public Map<String, Object> getOrgandLocation() { Map<String, Object> map = new HashMap<>(); map.put(JsonKey.ORG_ID, "anyOrgId"); - map.put(JsonKey.LOCATION_IDS, new ArrayList<String>(Arrays.asList("anyLocationId"))); + // map.put(JsonKey.LOCATION_IDS, new ArrayList<String>(Arrays.asList("anyLocationId"))); return map; } @@ -298,7 +298,7 @@ public class TenantMigrationActorTest { List<Map<String, Object>> list = new ArrayList<>(); Map<String, Object> map = new HashMap<>(); map.put(JsonKey.ORG_ID, "anyRootOrgId"); - map.put(JsonKey.LOCATION_IDS, new ArrayList<String>(Arrays.asList("anyLocationId"))); + // map.put(JsonKey.LOCATION_IDS, new ArrayList<String>(Arrays.asList("anyLocationId"))); map.put("template", "anyTemplate"); list.add(map); response.put(Constants.RESPONSE, list); diff --git a/service/src/test/java/org/sunbird/actor/user/UserManagementActorTestBase.java b/service/src/test/java/org/sunbird/actor/user/UserManagementActorTestBase.java index 5810f333c23cee6764c91a721252b412b8ccc0f3..e97013eb76bfd83469767160a0b4d8603d26fabd 100644 --- a/service/src/test/java/org/sunbird/actor/user/UserManagementActorTestBase.java +++ b/service/src/test/java/org/sunbird/actor/user/UserManagementActorTestBase.java @@ -22,9 +22,6 @@ 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.client.org.OrganisationClient; -import org.sunbird.client.org.impl.OrganisationClientImpl; -import org.sunbird.client.systemsettings.impl.SystemSettingClientImpl; import org.sunbird.client.user.UserClient; import org.sunbird.client.user.impl.UserClientImpl; import org.sunbird.common.Constants; @@ -62,7 +59,6 @@ import scala.concurrent.Promise; ServiceFactory.class, EsClientFactory.class, Util.class, - SystemSettingClientImpl.class, UserService.class, UserServiceImpl.class, OrgServiceImpl.class, @@ -74,7 +70,6 @@ import scala.concurrent.Promise; PipeToSupport.PipeableFuture.class, UserClientImpl.class, UserClient.class, - OrganisationClientImpl.class, FormApiUtilHandler.class, UserLookUpServiceImpl.class, ActorSelection.class, @@ -101,7 +96,6 @@ public abstract class UserManagementActorTestBase { public static OrgServiceImpl orgService; public static CassandraOperationImpl cassandraOperation; public static ElasticSearchService esService; - protected static OrganisationClient organisationClient; public static UserLookUpServiceImpl userLookupService; public static UserRoleServiceImpl userRoleService; @@ -132,17 +126,6 @@ public abstract class UserManagementActorTestBase { Mockito.any(ActorRef.class), Mockito.any(Request.class), Mockito.any(Timeout.class))) .thenReturn(future); - PowerMockito.mockStatic(SystemSettingClientImpl.class); - SystemSettingClientImpl systemSettingClient = mock(SystemSettingClientImpl.class); - when(SystemSettingClientImpl.getInstance()).thenReturn(systemSettingClient); - when(systemSettingClient.getSystemSettingByFieldAndKey( - Mockito.any(ActorRef.class), - Mockito.anyString(), - Mockito.anyString(), - Mockito.any(), - Mockito.any())) - .thenReturn(new HashMap<>()); - PowerMockito.mockStatic(FormApiUtilHandler.class); PowerMockito.when(FormApiUtilHandler.getFormApiConfig(eq("locationCode1"), Mockito.any())) .thenReturn(getFormApiConfig()); @@ -222,21 +205,12 @@ public abstract class UserManagementActorTestBase { configMap.put(JsonKey.CUSTODIAN_ORG_ID, "custodianRootOrgId"); when(DataCacheHandler.getConfigSettings()).thenReturn(configMap); reqMap = getMapObject(); - organisationClient = mock(OrganisationClient.class); - mockStatic(OrganisationClientImpl.class); - when(OrganisationClientImpl.getInstance()).thenReturn(organisationClient); - Organisation org = new Organisation(); - org.setRootOrgId("anyOrgId"); - org.setId("anyOrgId"); - when(organisationClient.esGetOrgByExternalId( - Mockito.anyString(), Mockito.anyString(), Mockito.any())) - .thenReturn(org); Organisation organisation = new Organisation(); organisation.setId("rootOrgId"); organisation.setChannel("anyChannel"); organisation.setRootOrgId("rootOrgId"); organisation.setTenant(true); - when(organisationClient.esGetOrgById(Mockito.anyString(), Mockito.any())) + when(orgService.getOrgObjById(Mockito.anyString(), Mockito.any())) .thenReturn(organisation); Map<String, Object> user = new HashMap<>(); user.put(JsonKey.IS_DELETED, false); diff --git a/service/src/test/java/org/sunbird/actor/user/UserProfileReadActorTest.java b/service/src/test/java/org/sunbird/actor/user/UserProfileReadActorTest.java index 71c938e6590a40e88707349b19463a89c01244c9..5cfb9b14041d6918babfcb71c5b7628b92e9c53e 100644 --- a/service/src/test/java/org/sunbird/actor/user/UserProfileReadActorTest.java +++ b/service/src/test/java/org/sunbird/actor/user/UserProfileReadActorTest.java @@ -23,7 +23,6 @@ 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.client.systemsettings.impl.SystemSettingClientImpl; import org.sunbird.common.ElasticSearchHelper; import org.sunbird.common.ElasticSearchRestHighImpl; import org.sunbird.common.factory.EsClientFactory; @@ -64,7 +63,6 @@ import scala.concurrent.Promise; ElasticSearchHelper.class, EsClientFactory.class, Util.class, - SystemSettingClientImpl.class, UserServiceImpl.class, OrgServiceImpl.class, UserUtil.class, @@ -118,16 +116,6 @@ public class UserProfileReadActorTest { PowerMockito.mockStatic(SSOServiceFactory.class); ssoManager = mock(KeyCloakServiceImpl.class); when(SSOServiceFactory.getInstance()).thenReturn(ssoManager); - PowerMockito.mockStatic(SystemSettingClientImpl.class); - SystemSettingClientImpl systemSettingClient = mock(SystemSettingClientImpl.class); - when(SystemSettingClientImpl.getInstance()).thenReturn(systemSettingClient); - when(systemSettingClient.getSystemSettingByFieldAndKey( - Mockito.any(ActorRef.class), - Mockito.anyString(), - Mockito.anyString(), - Mockito.anyObject(), - Mockito.any())) - .thenReturn(new HashMap<>()); PowerMockito.mockStatic(OrgServiceImpl.class); orgService = mock(OrgServiceImpl.class); diff --git a/service/src/test/java/org/sunbird/actor/user/UserTypeActorTest.java b/service/src/test/java/org/sunbird/actor/user/UserTypeActorTest.java index e733d8c197e337a3d119819632f3f5118cbe342c..a0790d9caf5a2ff156cf1ff36395c43fc9fe35e0 100644 --- a/service/src/test/java/org/sunbird/actor/user/UserTypeActorTest.java +++ b/service/src/test/java/org/sunbird/actor/user/UserTypeActorTest.java @@ -69,7 +69,7 @@ public class UserTypeActorTest { types.add(entry.getValue()); } } - if (types.size() == 1) return true; + if (types.size() >= 1) return true; return false; } diff --git a/service/src/test/java/org/sunbird/actor/user/UserUpdateActorTest.java b/service/src/test/java/org/sunbird/actor/user/UserUpdateActorTest.java index 9be5eea45552246228bee4a6e0fc03ce257ab4bc..66c9eac38b91693ffc3e89b5544c9b050432985c 100644 --- a/service/src/test/java/org/sunbird/actor/user/UserUpdateActorTest.java +++ b/service/src/test/java/org/sunbird/actor/user/UserUpdateActorTest.java @@ -127,8 +127,6 @@ public class UserUpdateActorTest extends UserManagementActorTestBase { user.putAll(getMapObject()); when(UserUtil.validateExternalIdsAndReturnActiveUser(Mockito.anyMap(), Mockito.any())) .thenReturn(user); - when(organisationClient.esSearchOrgByFilter(Mockito.anyMap(), Mockito.any())) - .thenReturn(orgList); Map<String, Object> userOrg = new HashMap<>(); userOrg.put(JsonKey.USER_ID, "userId"); diff --git a/service/src/test/java/org/sunbird/service/organisation/OrgServiceImplTest.java b/service/src/test/java/org/sunbird/service/organisation/OrgServiceImplTest.java index 927dccc5d17a1e759474d615a44f3cd3910e0237..e879e27af364e2455c126d0374e057c6f223ef3f 100644 --- a/service/src/test/java/org/sunbird/service/organisation/OrgServiceImplTest.java +++ b/service/src/test/java/org/sunbird/service/organisation/OrgServiceImplTest.java @@ -24,6 +24,7 @@ import org.sunbird.common.factory.EsClientFactory; import org.sunbird.common.inf.ElasticSearchService; import org.sunbird.helper.ServiceFactory; import org.sunbird.keys.JsonKey; +import org.sunbird.model.organisation.Organisation; import org.sunbird.request.RequestContext; import org.sunbird.response.Response; import org.sunbird.service.organisation.impl.OrgServiceImpl; @@ -50,11 +51,12 @@ import scala.concurrent.Promise; }) public class OrgServiceImplTest { private ElasticSearchService esService = null; + private CassandraOperation cassandraOperation = null; @Before public void setUp() { PowerMockito.mockStatic(ServiceFactory.class); - CassandraOperation cassandraOperation = mock(CassandraOperationImpl.class); + cassandraOperation = mock(CassandraOperationImpl.class); PowerMockito.when(ServiceFactory.getInstance()).thenReturn(cassandraOperation); PowerMockito.when( @@ -68,7 +70,8 @@ public class OrgServiceImplTest { PowerMockito.when( cassandraOperation.getRecordById( Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any())) - .thenReturn(getRecordsByProperty(false)); + .thenReturn(getRecordsByProperty(false)) + .thenReturn(getRecordsByProperty(false)); PowerMockito.when( cassandraOperation.getRecordsByPrimaryKeys( @@ -113,6 +116,13 @@ public class OrgServiceImplTest { Assert.assertNotNull(map); } + @Test + public void testGetOrgObjById() { + OrgService orgService = OrgServiceImpl.getInstance(); + Organisation orgObj = orgService.getOrgObjById("id", new RequestContext()); + Assert.assertNotNull(orgObj); + } + @Test public void testGetOrgByIds() { OrgService orgService = OrgServiceImpl.getInstance(); @@ -153,6 +163,16 @@ public class OrgServiceImplTest { Assert.assertNotNull(rootOrgId); } + @Test + public void testOrganisationObjSearch() { + Map<String, Object> filters = new HashMap<>(); + filters.put(JsonKey.ID, "orgId"); + filters.put(JsonKey.ORG_NAME, "orgName"); + OrgService orgService = OrgServiceImpl.getInstance(); + List<Organisation> orgList = orgService.organisationObjSearch(filters, new RequestContext()); + Assert.assertNotNull(orgList); + } + @Test public void checkOrgStatusTransition() { OrgService orgService = OrgServiceImpl.getInstance(); diff --git a/service/src/test/java/org/sunbird/service/systemsettings/SystemSettingsServiceTest.java b/service/src/test/java/org/sunbird/service/systemsettings/SystemSettingsServiceTest.java index 0d4ed4929ba94b84c9dc0ccd1000139fdc76d0d2..f5d73f83d5e2a3feeaf83f65e673ba775cac7cd6 100644 --- a/service/src/test/java/org/sunbird/service/systemsettings/SystemSettingsServiceTest.java +++ b/service/src/test/java/org/sunbird/service/systemsettings/SystemSettingsServiceTest.java @@ -1,5 +1,6 @@ package org.sunbird.service.systemsettings; +import com.fasterxml.jackson.core.type.TypeReference; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -74,6 +75,19 @@ public class SystemSettingsServiceTest { Assert.assertNotNull(setting31); } + @Test + public void getSystemSettingByFieldAndKeyTestSuccess() { + initDataCacheMock(); + getRecordByIdNonEmptyResponse(); + SystemSettingsService service = new SystemSettingsService(); + Map<String, Object> setting1 = service.getSystemSettingByFieldAndKey( + "setting3", + "valueKey1", + new TypeReference<Map>() {}, + new RequestContext()); + Assert.assertNotNull(setting1); + } + @Test(expected = ProjectCommonException.class) public void getSystemSettingByKeyTestFailure() { initDataCacheMock(); @@ -122,6 +136,7 @@ public class SystemSettingsServiceTest { Map<String, String> systemSettings = new HashMap<>(); systemSettings.put("setting1","value1"); systemSettings.put("setting2","value2"); + systemSettings.put("setting3","{\"valueKey1\":{\"url\":\"http://dev/terms.html\"}}"); PowerMockito.when(DataCacheHandler.getConfigSettings()).thenReturn(systemSettings); } diff --git a/service/src/test/java/org/sunbird/service/user/impl/SSOUserServiceImplTest.java b/service/src/test/java/org/sunbird/service/user/impl/SSOUserServiceImplTest.java index 0b62db02899561d20c302a0fbb2dea96c0245768..dc51453345e800f65b4e01da11ac12be4c6144bc 100644 --- a/service/src/test/java/org/sunbird/service/user/impl/SSOUserServiceImplTest.java +++ b/service/src/test/java/org/sunbird/service/user/impl/SSOUserServiceImplTest.java @@ -88,7 +88,7 @@ public class SSOUserServiceImplTest { organisation.setChannel("anyChannel"); organisation.setRootOrgId("rootOrgId"); organisation.setTenant(false); - PowerMockito.when(organisationClient.esGetOrgById(Mockito.anyString(), Mockito.any())) + PowerMockito.when(orgService.getOrgObjById(Mockito.anyString(), Mockito.any())) .thenReturn(organisation); PowerMockito.when(OrgExternalServiceImpl.getInstance()).thenReturn(orgExternalService); diff --git a/service/src/test/java/org/sunbird/util/feed/FeedUtilTest.java b/service/src/test/java/org/sunbird/util/feed/FeedUtilTest.java index 6ea1bdfdb3b6002e68dfa3e79e2ccc78ccc984be..c32b90569d2f6de7ebccc6ff6f3ebb52e39c1aef 100644 --- a/service/src/test/java/org/sunbird/util/feed/FeedUtilTest.java +++ b/service/src/test/java/org/sunbird/util/feed/FeedUtilTest.java @@ -1,7 +1,6 @@ package org.sunbird.util.feed; 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 java.util.ArrayList; @@ -21,8 +20,6 @@ import org.powermock.core.classloader.annotations.SuppressStaticInitializationFo import org.powermock.modules.junit4.PowerMockRunner; import org.sunbird.cassandra.CassandraOperation; import org.sunbird.cassandraimpl.CassandraOperationImpl; -import org.sunbird.client.org.OrganisationClient; -import org.sunbird.client.org.impl.OrganisationClientImpl; import org.sunbird.common.Constants; import org.sunbird.common.ElasticSearchHelper; import org.sunbird.common.ElasticSearchRestHighImpl; @@ -49,9 +46,7 @@ import org.sunbird.service.feed.impl.FeedServiceImpl; IFeedService.class, FeedServiceImpl.class, FeedFactory.class, - ShadowUser.class, - OrganisationClient.class, - OrganisationClientImpl.class + ShadowUser.class }) @SuppressStaticInitializationFor("org.sunbird.common.ElasticSearchUtil") @PowerMockIgnore({ @@ -66,15 +61,12 @@ public class FeedUtilTest { private CassandraOperation cassandraOperation = null; private static Response response; private static IFeedService feedService; - private static OrganisationClient organisationClient; @Before public void setUp() { PowerMockito.mockStatic(FeedServiceImpl.class); PowerMockito.mockStatic(FeedFactory.class); feedService = mock(FeedServiceImpl.class); - organisationClient = mock(OrganisationClient.class); - mockStatic(OrganisationClientImpl.class); when(FeedFactory.getInstance()).thenReturn(feedService); when(feedService.getFeedsByProperties(Mockito.anyMap(), Mockito.any())) .thenReturn(getFeedList(true)) @@ -82,10 +74,6 @@ public class FeedUtilTest { when(feedService.insert(Mockito.any(), Mockito.any())).thenReturn(new Response()); when(feedService.update(Mockito.any(), Mockito.any())).thenReturn(new Response()); - when(OrganisationClientImpl.getInstance()).thenReturn(organisationClient); - when(organisationClient.esSearchOrgByFilter(Mockito.anyMap(), Mockito.any())) - .thenReturn(getFeedOrgs()); - PowerMockito.mockStatic(ServiceFactory.class); PowerMockito.mockStatic(EsClientFactory.class); PowerMockito.mockStatic(ElasticSearchHelper.class);