Skip to content

Commit

Permalink
Issue #LR-7:feat Added traceability data for event in del-user and ow…
Browse files Browse the repository at this point in the history
…n-trans (#1251)
  • Loading branch information
BharathwajShankar authored May 23, 2024
1 parent d157d9a commit 6467848
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.apache.commons.lang3.StringUtils;
import org.sunbird.exception.ProjectCommonException;
import org.sunbird.exception.ResponseCode;
import org.sunbird.keys.JsonKey;
import org.sunbird.logging.LoggerUtil;
import org.sunbird.telemetry.dto.TelemetryBJREvent;

Expand Down Expand Up @@ -67,13 +68,12 @@ private static String generateInstructionEventMetadata(Map<String, Object> data)
pdata.put("id", pdataId);
pdata.put("ver", pdataVersion);
context.put("pdata", pdata);
context.put(JsonKey.CDATA,data.get(JsonKey.CDATA));
if (MapUtils.isNotEmpty((Map) data.get("object"))) object.putAll((Map) data.get("object"));

if (MapUtils.isNotEmpty((Map) data.get("edata"))) edata.putAll((Map) data.get("edata"));

if (StringUtils.isNotBlank((String) data.get("action")))
edata.put("action", data.get("action"));

return logInstructionEvent(actor, context, object, edata);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,7 @@ public final class JsonKey {
public static final String USER_OWNERSHIP_TRANSFER_TOPIC = "user-ownership-transfer-topic";
public static final String OBJECT = "object";
public static final String EDATA = "edata";
public static final String CDATA = "cdata";
public static final String MANAGED_USERS = "managed_users";
public static final String USER_DELETION_TOPIC = "user-deletion-broadcast-topic";
public static final String USER_DELETION_ROLES = "user-deletion-roles";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.sunbird.request.Request;
import org.sunbird.service.user.UserService;
import org.sunbird.service.user.impl.UserServiceImpl;
import org.sunbird.telemetry.dto.TelemetryEnvKey;
import org.sunbird.util.PropertiesCache;
import org.sunbird.util.user.UserUtil;

Expand Down Expand Up @@ -110,6 +111,11 @@ private void inputKafkaTopic(Request request) throws Exception {
objectMap.put(JsonKey.TYPE, JsonKey.USER);
data.put(JsonKey.OBJECT, objectMap);

Map<String, Object> cData = new HashMap<>();
cData.put(JsonKey.ID,request.getRequestContext().getReqId());
cData.put(JsonKey.TYPE, TelemetryEnvKey.REQUEST_UPPER_CAMEL);
data.put(JsonKey.CDATA, cData);

Map<String, Object> eData = new HashMap<>();
eData.put(JsonKey.ORGANISATION_ID, rootOrgId);
eData.put(JsonKey.USER_ID, userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.sunbird.service.user.UserService;
import org.sunbird.service.user.impl.UserRoleServiceImpl;
import org.sunbird.service.user.impl.UserServiceImpl;
import org.sunbird.telemetry.dto.TelemetryEnvKey;
import org.sunbird.util.ProjectUtil;
import org.sunbird.util.PropertiesCache;
import org.sunbird.util.user.UserUtil;
Expand Down Expand Up @@ -218,12 +219,17 @@ private Map<String, Object> prepareEventData(Request request, Map<String, Object
Map<String, Object> assetInformation = new HashMap<>(object);
edata.put(JsonKey.ASSET_INFORMATION, assetInformation);

Map<String, Object> cData = new HashMap<>();
cData.put(JsonKey.ID,request.getRequestContext().getReqId());
cData.put(JsonKey.TYPE, TelemetryEnvKey.REQUEST_UPPER_CAMEL);

Map<String, Object> result = new HashMap<>();
Map<String, Object> fromUserDetails = (Map<String, Object>) request.getRequest().get(JsonKey.FROM_USER);
Map<String, Object> objectDetails = Map.of(JsonKey.ID, fromUserDetails.get(JsonKey.USER_ID), JsonKey.TYPE,
JsonKey.USER);
result.put("actor", actor);
result.put(JsonKey.OBJECT, objectDetails);
result.put(JsonKey.CDATA, cData);
result.put(JsonKey.EDATA, edata);
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@ public void beforeEachTest() throws Exception {
searchResult.put(JsonKey.CONTENT, getUsersList());
PowerMockito.when(userService.searchUser(Mockito.any(), Mockito.any()))
.thenReturn(searchResult);

// Mock RequestContext and ReqId
RequestContext requestContext = PowerMockito.mock(RequestContext.class);
PowerMockito.when(requestContext.getReqId()).thenReturn("test-req-id");

// Mock Request to include RequestContext
Request request = PowerMockito.mock(Request.class);
PowerMockito.when(request.getRequestContext()).thenReturn(requestContext);

// Ensure Request object returns expected data
Map<String, Object> requestData = new HashMap<>();
requestData.put(JsonKey.USER_ID, "46545665465465");
requestData.put(JsonKey.USER_ROLES, new ArrayList<>(Arrays.asList(JsonKey.PUBLIC)));
PowerMockito.when(request.getRequest()).thenReturn(requestData);
}

@Test
Expand All @@ -86,6 +100,10 @@ public void callUserDeletionTest() {
reqObj.getRequest().putAll(userData);
reqObj.setOperation("inputKafkaTopic");

RequestContext requestContext = new RequestContext();
requestContext.setReqId("test-req-id");
reqObj.setRequestContext(requestContext);

subject.tell(reqObj, probe.getRef());
probe.expectNoMessage();
}
Expand Down

0 comments on commit 6467848

Please sign in to comment.