Skip to content

Commit

Permalink
SB-27940: add soft delete feature and validation for template api
Browse files Browse the repository at this point in the history
  • Loading branch information
sknirmalkar89 committed Jan 4, 2022
1 parent 7712462 commit 1309e59
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 30 deletions.
20 changes: 13 additions & 7 deletions all-actors/src/main/java/org/sunbird/dao/NotificationDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,19 @@ public Response updateNotificationFeed( List<Map<String,Object>> feeds, Map<Stri

@Override
public Response deleteUserFeed(List<NotificationFeed> feeds, Map<String,Object> context) throws BaseException {
List<Map<String,Object>> properties = new ArrayList<>();
List<Map<String, Map<String, Object>>> properties = new ArrayList<>();
for (NotificationFeed feed : feeds) {
Map<String,Object> map = new HashMap<>();
map.put(JsonKey.ID,feed.getId());
map.put(JsonKey.USER_ID,feed.getUserId());
properties.add(map);
Map<String,Map<String,Object>> keysMap = new HashMap<>();
Map<String, Object> primaryKeyMap = new HashMap<>();
Map<String, Object> nonPrimaryKeyMap = new HashMap<>();
primaryKeyMap.put(JsonKey.ID,feed.getId());
primaryKeyMap.put(JsonKey.USER_ID,feed.getUserId());
nonPrimaryKeyMap.put(JsonKey.STATUS,"deleted");
keysMap.put(Constants.PRIMARY_KEY,primaryKeyMap);
keysMap.put(Constants.NON_PRIMARY_KEY,nonPrimaryKeyMap);
properties.add(keysMap);
}
return cassandraOperation.batchDelete(KEY_SPACE_NAME,NOTIFICATION_FEED, properties, context);
return cassandraOperation.batchUpdate(KEY_SPACE_NAME,NOTIFICATION_FEED, properties, context);
}

@Override
Expand All @@ -100,8 +105,9 @@ public Response deleteUserFeedMap(List<String> feedIds, Map<String, Object> cont
for (String feedId : feedIds) {
Map<String,Object> map = new HashMap<>();
map.put(JsonKey.ID,feedId);
map.put(JsonKey.STATUS,"deleted");
properties.add(map);
}
return cassandraOperation.batchDelete(KEY_SPACE_NAME,FEED_VERSION_MAP, properties, context);
return cassandraOperation.batchUpdateById(KEY_SPACE_NAME,FEED_VERSION_MAP,properties,context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,15 @@ public Response sendV1Notification(Map<String, Object> notification, Map<String,
private void deleteUserFeed(Map<String,List<String>> feedListMap, boolean isSupportEnabled, Map<String,Object> reqContext) throws IOException {
if(MapUtils.isNotEmpty(feedListMap)){
List<String> feedList = new ArrayList<>();
for (List<String> feed: feedListMap.values()) {
feedList.addAll(feed);
for (Map.Entry<String,List<String>> entry: feedListMap.entrySet()) {
feedList = entry.getValue();
if (isSupportEnabled) {
List<Map<String, Object>> mappedFeedIdLists = notificationService.getFeedMap(feedList, reqContext);
List<String> feedIds = mappedFeedIdLists.stream().map(x -> x.get(JsonKey.FEED_ID)).filter(Objects::nonNull).map(Object::toString)
.collect(Collectors.toList());
feedList.addAll(feedIds);
}
}
if (isSupportEnabled) {
List<Map<String, Object>> mappedFeedIdLists = notificationService.getFeedMap(feedList, reqContext);
List<String> feedIds = mappedFeedIdLists.stream().map(x -> x.get(JsonKey.FEED_ID)).filter(Objects::nonNull).map(Object::toString)
.collect(Collectors.toList());
feedList.addAll(feedIds);
}
notificationService.deleteNotificationFeed(feedListMap, reqContext);
if(isSupportEnabled) {
notificationService.deleteNotificationFeedMap(feedList, reqContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.io.StringWriter;
import java.sql.Timestamp;
import java.util.*;
import java.util.stream.Collectors;

public class NotificationServiceImpl implements NotificationService {
private static LoggerUtil logger = new LoggerUtil(NotificationServiceImpl.class);
Expand Down Expand Up @@ -165,8 +166,11 @@ public List<Map<String, Object>> getFeedMap(List<String> feedIds, Map<String,Ob
Response response = notificationDao.getFeedMap(feedIds, reqContext);
List<Map<String, Object>> feedMapLists = new ArrayList<>();
if(null != response){
feedMapLists = (List<Map<String, Object>>) response.getResult().get(JsonKey.RESPONSE);
List<Map<String, Object>> feedLists = (List<Map<String, Object>>) response.getResult().get(JsonKey.RESPONSE);
//remove deleted feeds
feedMapLists = feedLists.stream().filter(x->!JsonKey.DELETED.equals(x.get(JsonKey.STATUS))).collect(Collectors.toList());
}

return feedMapLists;
}

Expand All @@ -187,6 +191,10 @@ public List<Map<String, Object>> readNotificationFeed(String userId, Map<String,
Iterator<Map<String,Object>> notifyItr = notifications.iterator();
while (notifyItr.hasNext()) {
Map<String,Object> notification = notifyItr.next();
if(JsonKey.DELETED.equals(notification.get(JsonKey.STATUS))){
notifyItr.remove();
continue;
}
if(JsonKey.V1.equals(notification.get(JsonKey.VERSION))){
notifyItr.remove();
}else{
Expand Down Expand Up @@ -218,6 +226,10 @@ public List<Map<String, Object>> readV1NotificationFeed(String userId, Map<Strin
Iterator<Map<String,Object>> notifyItr = notifications.iterator();
while (notifyItr.hasNext()) {
Map<String,Object> notification = notifyItr.next();
if(JsonKey.DELETED.equals(notification.get(JsonKey.STATUS))){
notifyItr.remove();
continue;
}
if(!JsonKey.V1.equals(notification.get(JsonKey.VERSION))){
notifyItr.remove();
}else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.sunbird.pojo.NotificationV2Request;
import org.sunbird.request.LoggerUtil;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -75,14 +76,17 @@ public Response upsertActionTemplate(ActionTemplate actionTemplate,Map<String,Ob
public Map<String,Object> getActionTemplate(String action, Map<String,Object> reqContext) throws BaseException {
Map<String,Object> actionDetails = new HashMap<>();
Response actionResponseObj =templateDao.getTemplateId(action,reqContext);
if (null != actionResponseObj && MapUtils.isNotEmpty(actionResponseObj.getResult())) {
if (null != actionResponseObj) {
actionDetails.put(JsonKey.ACTION,action);
if(MapUtils.isEmpty(actionResponseObj.getResult())){
throw new BaseException(IResponseMessage.TEMPLATE_NOT_FOUND, MessageFormat.format(IResponseMessage.Message.TEMPLATE_NOT_FOUND,action), ResponseCode.CLIENT_ERROR.getResponseCode());
}
List<Map<String, Object>> templateIdDetails =
(List<Map<String, Object>>) actionResponseObj.getResult().get(JsonKey.RESPONSE);
if (CollectionUtils.isNotEmpty(templateIdDetails)) {
Map<String, Object> dbTemplateIdDetail = templateIdDetails.get(0);
String templateId = (String) dbTemplateIdDetail.get(JsonKey.TEMPLATE_ID);
Response templateDetailResponseObj = templateDao.getTemplate(templateId,reqContext);
actionDetails.put(JsonKey.ACTION,action);
actionDetails.put(JsonKey.TYPE,dbTemplateIdDetail.get(JsonKey.TYPE));
Map<String, Object> templateDetailMap = new HashMap<>();
if (null != templateDetailResponseObj && MapUtils.isNotEmpty(templateDetailResponseObj.getResult())) {
Expand All @@ -94,7 +98,7 @@ public Map<String,Object> getActionTemplate(String action, Map<String,Object> re
}
actionDetails.put(JsonKey.TEMPLATE,templateDetailMap);
}else{
throw new BaseException(IResponseMessage.TEMPLATE_NOT_FOUND,IResponseMessage.Message.TEMPLATE_NOT_FOUND, ResponseCode.CLIENT_ERROR.getResponseCode());
throw new BaseException(IResponseMessage.TEMPLATE_NOT_FOUND,MessageFormat.format(IResponseMessage.Message.TEMPLATE_NOT_FOUND,action), ResponseCode.CLIENT_ERROR.getResponseCode());
}
}else{
throw new BaseException(IResponseMessage.INTERNAL_ERROR,IResponseMessage.SERVER_ERROR, ResponseCode.SERVER_ERROR.getCode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,13 @@ public void testDeleteSuccess(){
PowerMockito.mockStatic(ServiceFactory.class);
cassandraOperation = mock(CassandraOperationImpl.class);
when(ServiceFactory.getInstance()).thenReturn(cassandraOperation);
when(cassandraOperation.batchDelete(
when(cassandraOperation.batchUpdate(
Mockito.anyString(),
Mockito.anyString(),
Mockito.anyList(),
Mockito.anyMap()))
.thenReturn(getCassandraResponse());
when(cassandraOperation.batchUpdateById(
Mockito.anyString(),
Mockito.anyString(),
Mockito.anyList(),
Expand Down Expand Up @@ -113,7 +119,13 @@ public void testDeleteAuthorizationException(){
PowerMockito.mockStatic(ServiceFactory.class);
cassandraOperation = mock(CassandraOperationImpl.class);
when(ServiceFactory.getInstance()).thenReturn(cassandraOperation);
when(cassandraOperation.batchDelete(
when(cassandraOperation.batchUpdate(
Mockito.anyString(),
Mockito.anyString(),
Mockito.anyList(),
Mockito.anyMap()))
.thenReturn(getCassandraResponse());
when(cassandraOperation.batchUpdateById(
Mockito.anyString(),
Mockito.anyString(),
Mockito.anyList(),
Expand Down Expand Up @@ -144,7 +156,13 @@ public void testV1DeleteSuccess(){
PowerMockito.mockStatic(ServiceFactory.class);
cassandraOperation = mock(CassandraOperationImpl.class);
when(ServiceFactory.getInstance()).thenReturn(cassandraOperation);
when(cassandraOperation.batchDelete(
when(cassandraOperation.batchUpdate(
Mockito.anyString(),
Mockito.anyString(),
Mockito.anyList(),
Mockito.anyMap()))
.thenReturn(getCassandraResponse());
when(cassandraOperation.batchUpdateById(
Mockito.anyString(),
Mockito.anyString(),
Mockito.anyList(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public enum ResponseCode {
IResponseMessage.INVALID_REQUESTED_DATA, IResponseMessage.Message.INVALID_REQUESTED_DATA,JsonKey.FAILED),
serverError(IResponseMessage.SERVER_ERROR, IResponseMessage.SERVER_ERROR,JsonKey.FAILED),
internalError(IResponseMessage.INTERNAL_ERROR, IResponseMessage.INTERNAL_ERROR,JsonKey.FAILED),

templateNotFound(IResponseMessage.TEMPLATE_NOT_FOUND, IResponseMessage.Message.TEMPLATE_NOT_FOUND,JsonKey.FAILED),
OK(200),
CLIENT_ERROR(400),
SERVER_ERROR(500),
Expand Down
1 change: 1 addition & 0 deletions sb-utils/src/main/java/org.sunbird/JsonKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,5 @@ public interface JsonKey {
String DELETE_TEMPLATE="deleteTemplate";
String MAP_ACTION_TEMPLATE = "mapActionTemplate" ;
String READ_ACTION_TEMPLATE= "readActionTemplate";
String DELETED="deleted";
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import play.mvc.Http;
import play.mvc.Result;
import utils.RequestMapper;
import validators.TemplateActionUpdateRequestValidator;
import validators.TemplateRequestValidator;
import validators.TemplateUpdateRequestValidator;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
Expand Down Expand Up @@ -50,7 +52,7 @@ public CompletionStage<Result> updateTemplate() {
Request request = new Request();
try {
request = RequestMapper.createSBRequest(request());
CompletionStage<Result> response = handleRequest(request, null, JsonKey.UPDATE_TEMPLATE, request());
CompletionStage<Result> response = handleRequest(request, new TemplateUpdateRequestValidator(), JsonKey.UPDATE_TEMPLATE, request());
logger.info("Method call end for updateTemplate");
return response;
}catch (Exception ex){
Expand All @@ -64,7 +66,7 @@ public CompletionStage<Result> deleteTemplate() {
Request request = new Request();
try {
request = RequestMapper.createSBRequest(request());
CompletionStage<Result> response = handleRequest(request, null, JsonKey.DELETE_TEMPLATE, request());
CompletionStage<Result> response = handleRequest(request, new TemplateUpdateRequestValidator(), JsonKey.DELETE_TEMPLATE, request());
logger.info("Method call end for deleteTemplate");
return response;
}catch (Exception ex){
Expand All @@ -78,7 +80,7 @@ public CompletionStage<Result> upsertActionTemplate() {
Request request = new Request();
try {
request = RequestMapper.createSBRequest(request());
CompletionStage<Result> response = handleRequest(request, null, JsonKey.MAP_ACTION_TEMPLATE, request());
CompletionStage<Result> response = handleRequest(request, new TemplateActionUpdateRequestValidator(), JsonKey.MAP_ACTION_TEMPLATE, request());
logger.info("Method call end for listTemplate");
return response;
}catch (Exception ex){
Expand Down
28 changes: 28 additions & 0 deletions service/app/validators/TemplateActionUpdateRequestValidator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package validators;

import org.sunbird.JsonKey;
import org.sunbird.common.exception.BaseException;
import org.sunbird.common.message.IResponseMessage;
import org.sunbird.common.message.ResponseCode;
import org.sunbird.common.request.Request;
import org.sunbird.common.response.Response;
import org.sunbird.request.LoggerUtil;
import utils.ValidationUtil;

import java.util.Arrays;

public class TemplateActionUpdateRequestValidator implements RequestValidatorFunction<Request, Response> {
private static LoggerUtil logger = new LoggerUtil(TemplateActionUpdateRequestValidator.class);
@Override
public Response apply(Request request) throws BaseException {
try{
ValidationUtil.validateRequestObject(request);
ValidationUtil.validateMandatoryParamsWithType(request.getRequest(), Arrays.asList(JsonKey.TEMPLATE_ID,JsonKey.ACTION),String.class,true,JsonKey.REQUEST,request.getContext());
}catch (Exception ex){
logger.error(request.getContext(),"Validation error",ex);
throw new BaseException(IResponseMessage.Key.INVALID_REQUESTED_DATA, ex.getMessage(), ResponseCode.BAD_REQUEST.getCode());

}
return null;
}
}
28 changes: 28 additions & 0 deletions service/app/validators/TemplateUpdateRequestValidator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package validators;

import org.sunbird.JsonKey;
import org.sunbird.common.exception.BaseException;
import org.sunbird.common.message.IResponseMessage;
import org.sunbird.common.message.ResponseCode;
import org.sunbird.common.request.Request;
import org.sunbird.common.response.Response;
import org.sunbird.request.LoggerUtil;
import utils.ValidationUtil;

import java.util.Arrays;

public class TemplateUpdateRequestValidator implements RequestValidatorFunction<Request, Response> {
private static LoggerUtil logger = new LoggerUtil(TemplateUpdateRequestValidator.class);
@Override
public Response apply(Request request) throws BaseException {
try{
ValidationUtil.validateRequestObject(request);
ValidationUtil.validateMandatoryParamsWithType(request.getRequest(), Arrays.asList(JsonKey.TEMPLATE_ID),String.class,true,JsonKey.REQUEST,request.getContext());
}catch (Exception ex){
logger.error(request.getContext(),"Validation error",ex);
throw new BaseException(IResponseMessage.Key.INVALID_REQUESTED_DATA, ex.getMessage(), ResponseCode.BAD_REQUEST.getCode());

}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@ public void createTemplate(){
@Test
public void updateTemplate(){
Map<String, Object> reqMap = new HashMap<>();
reqMap.put("accept", "yes");
Result result = performTest("/v1/notification/template/update", "PATCH",reqMap);
reqMap.put(JsonKey.TEMPLATE_ID,"user-add");
Map<String,Object> request = new HashMap<>();
request.put("accept", "yes");
request.put(JsonKey.REQUEST,reqMap);
Result result = performTest("/v1/notification/template/update", "PATCH",request);
assertTrue(getResponseStatus(result) == Response.Status.OK.getStatusCode());

}
Expand All @@ -64,8 +67,11 @@ public void listTemplate(){
@Test
public void deleteTemplate(){
Map<String, Object> reqMap = new HashMap<>();
reqMap.put("accept", "yes");
Result result = performTest("/v1/notification/template/delete", "POST",reqMap);
reqMap.put(JsonKey.TEMPLATE_ID,"user-add");
Map<String,Object> request = new HashMap<>();
request.put("accept", "yes");
request.put(JsonKey.REQUEST,reqMap);
Result result = performTest("/v1/notification/template/delete", "POST",request);
assertTrue(getResponseStatus(result) == Response.Status.OK.getStatusCode());

}
Expand Down

0 comments on commit 1309e59

Please sign in to comment.