Skip to content

Commit

Permalink
issue# SB-14625: validating notification send api request
Browse files Browse the repository at this point in the history
  • Loading branch information
aishwa8141 committed Sep 5, 2019
1 parent ba7b6b5 commit 91c521f
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 7 deletions.
24 changes: 24 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,27 @@ typings/

# next.js build output
.next


/target/
project/project
project/target
target
tmp
.history
dist
.idea/
/*.iml:
*.iml
*.log
/*.log
RUNNING_PID
/out
/.idea_modules
/.classpath
/.project
/.settings
/.target/
/bin/
/logs
*.zip
2 changes: 1 addition & 1 deletion all-actors/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<artifactId>notification-play-service</artifactId>
<groupId>org.sunbird</groupId>
<version>1.0.0-SNAPSHOT</version>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>all-actors</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion sb-actor/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>notification-play-service</artifactId>
<groupId>org.sunbird</groupId>
<version>1.0.0-SNAPSHOT</version>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion sb-utils/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>notification-play-service</artifactId>
<groupId>org.sunbird</groupId>
<version>1.0.0-SNAPSHOT</version>
<version>1.0.0</version>
</parent>

<modelVersion>4.0.0</modelVersion>
Expand Down
7 changes: 7 additions & 0 deletions sb-utils/src/main/java/org.sunbird/JsonKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,11 @@ public interface JsonKey {
String MESSAGE = "message";
String ERRORS = "errors";
String SUCCESS = "success";
String NOTIFICATIONS = "notifications";
String DELIVERY_TYPE = "deliveryType";
String MODE = "mode";
String IDS = "ids";
String PHONE = "phone";
String DEVICE = "device";
String EMAIL ="email";
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ public interface IResponseMessage extends IUserResponseMessage, IOrgResponseMess
String INVALID_OPERATION_NAME = "INVALID_OPERATION_NAME";
String INTERNAL_ERROR = "INTERNAL_ERROR";
String UNAUTHORIZED = "UNAUTHORIZED";
String MANDATORY_PARAMETER_MISSING = "Mandatory parameter {0} is missing.";
String INVALID_VALUE = "{0} VALUE IS INVALID, {1}";
String DATA_TYPE_REQUIRED="{0} SHOULD BE {1}";
}
13 changes: 9 additions & 4 deletions service/app/controllers/notification/NotificationController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
*
*
*/
package controllers.notification;

Expand All @@ -9,6 +9,7 @@
import org.apache.log4j.Logger;

import controllers.BaseController;
import org.sunbird.request.Request;
import play.mvc.Result;

/**
Expand All @@ -19,17 +20,21 @@
*/
public class NotificationController extends BaseController{
Logger logger = LogManager.getLogger(NotificationController.class);

public static final String NOTIFICATION = "notification";

/**
* This method will accept request for sending notification.
* notification can be sent on email, sms or push on device
* @return a CompletableFuture of success response
*/
public CompletionStage<Result> sendNotification() {
logger.info("method call started for sendNotification " + request().body().asJson());
CompletionStage<Result> response = handleRequest(request(),null,NOTIFICATION);
CompletionStage<Result> response = handleRequest(request()
,request -> {Request req = (Request) request;
new NotificationValidator().validateSendNotificationRequest(req);
return null;
}, NOTIFICATION);
logger.info("Method call end for sendNotification");
return response;
}
Expand Down
92 changes: 92 additions & 0 deletions service/app/controllers/notification/NotificationValidator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package controllers.notification;

import org.apache.commons.lang3.StringUtils;
import org.sunbird.BaseException;
import org.sunbird.JsonKey;
import org.sunbird.message.IResponseMessage;
import org.sunbird.message.ResponseCode;
import org.sunbird.request.Request;

import java.text.MessageFormat;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;

/**
* Validates send notification api request
*/
public class NotificationValidator {


public static void validateSendNotificationRequest(Request request) throws BaseException {
List<Map<String, Object>> sendReq = (List<Map<String, Object>>) request.getRequest().get(JsonKey.NOTIFICATIONS);
checkMandatoryParamsPresent(sendReq, JsonKey.NOTIFICATIONS, Arrays.asList(JsonKey.DELIVERY_TYPE, JsonKey.MODE, JsonKey.IDS));
}

private static void checkMandatoryParamsPresent(
List<Map<String, Object>> data, String parentKey, List<String> keys) throws BaseException {
if (CollectionUtils.isEmpty(data)) {
throw new BaseException("MANDATORY_PARAMETER_MISSING",
MessageFormat.format(IResponseMessage.MANDATORY_PARAMETER_MISSING, parentKey),
ResponseCode.CLIENT_ERROR.getCode());
}
for (Map<String, Object> map : data) {
checkChildrenMapMandatoryParams(map, keys, parentKey);
}

}

private static void checkChildrenMapMandatoryParams(Map<String, Object> data, List<String> keys, String parentKey) throws BaseException {

for (String key : keys) {
if (key.equals(JsonKey.IDS)) {
validateSendNotificationIds(data.get(key));
} else if (StringUtils.isEmpty((String) data.get(key))) {
throw new BaseException("MANDATORY_PARAMETER_MISSING",
MessageFormat.format(IResponseMessage.MANDATORY_PARAMETER_MISSING, parentKey + "." + key),
ResponseCode.CLIENT_ERROR.getCode());
}
if (key.equals(JsonKey.MODE)) {
checkModeValueIsValid((String) data.get(key));
}
}
}

private static void checkModeValueIsValid(String mode) throws BaseException {
String[] modeType = {JsonKey.PHONE, JsonKey.EMAIL, JsonKey.DEVICE};
if (!Arrays.asList(modeType).contains(mode)) {
throw new BaseException("INVALID_VALUE",
MessageFormat.format(IResponseMessage.INVALID_VALUE, JsonKey.NOTIFICATIONS + "." + JsonKey.MODE, "IT SHOULD BE ONE OF THIS VAlUES " + Arrays.asList(modeType)),
ResponseCode.CLIENT_ERROR.getCode());
}
}

/**
* validates notification ids , throws if it is null or empty or string
* @param ids
* @throws BaseException
*/
private static void validateSendNotificationIds(Object ids) throws BaseException {
if (ids == null) {
throw new BaseException("MANDATORY_PARAMETER_MISSING",
MessageFormat.format(IResponseMessage.MANDATORY_PARAMETER_MISSING, JsonKey.NOTIFICATIONS + "." + JsonKey.IDS),
ResponseCode.CLIENT_ERROR.getCode());
}
if (ids instanceof List) {
if (((List) ids).isEmpty()) {
throw new BaseException("MANDATORY_PARAMETER_MISSING",
MessageFormat.format(IResponseMessage.MANDATORY_PARAMETER_MISSING, JsonKey.NOTIFICATIONS + "." + JsonKey.IDS),
ResponseCode.CLIENT_ERROR.getCode());
}
}
if (ids instanceof String) {
throw new BaseException("DATA_TYPE_REQUIRED",
MessageFormat.format(IResponseMessage.DATA_TYPE_REQUIRED, JsonKey.NOTIFICATIONS + "." + JsonKey.IDS, "LIST"),
ResponseCode.CLIENT_ERROR.getCode());
}
}

}

5 changes: 5 additions & 0 deletions service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@
<version>2.6.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>${project.basedir}/app</sourceDirectory>
Expand Down

0 comments on commit 91c521f

Please sign in to comment.