diff options
Diffstat (limited to 'test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/message/MessageProvider.java')
-rw-r--r-- | test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/message/MessageProvider.java | 51 |
1 files changed, 36 insertions, 15 deletions
diff --git a/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/message/MessageProvider.java b/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/message/MessageProvider.java index 7c3bf9ef8..4931c3b91 100644 --- a/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/message/MessageProvider.java +++ b/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/message/MessageProvider.java @@ -21,40 +21,61 @@ package org.onap.pnfsimulator.message; import static org.onap.pnfsimulator.message.MessageConstants.COMMON_EVENT_HEADER; +import static org.onap.pnfsimulator.message.MessageConstants.DOMAIN; import static org.onap.pnfsimulator.message.MessageConstants.EVENT; -import static org.onap.pnfsimulator.message.MessageConstants.PNF_PREFIX; +import static org.onap.pnfsimulator.message.MessageConstants.EVENT_TYPE; +import static org.onap.pnfsimulator.message.MessageConstants.NOTIFICATION_FIELDS; +import static org.onap.pnfsimulator.message.MessageConstants.DOMAIN_PNF_REGISTRATION; +import static org.onap.pnfsimulator.message.MessageConstants.DOMAIN_NOTIFICATION; import static org.onap.pnfsimulator.message.MessageConstants.PNF_REGISTRATION_FIELDS; import java.util.Map; +import java.util.Optional; +import javax.annotation.Nonnull; import org.json.JSONObject; public class MessageProvider { - public JSONObject createMessage(JSONObject params) { + public JSONObject createMessage(@Nonnull JSONObject commonEventHeaderParams,@Nonnull Optional<JSONObject> pnfRegistrationParams, + @Nonnull Optional<JSONObject> notificationParams) { - if (params == null) { - throw new IllegalArgumentException("Params object cannot be null"); + if (!pnfRegistrationParams.isPresent() && !notificationParams.isPresent()) { + throw new IllegalArgumentException( + "Both PNF registration and notification parameters objects are not present"); } + JSONObject event = new JSONObject(); - Map<String, Object> paramsMap = params.toMap(); - JSONObject root = new JSONObject(); JSONObject commonEventHeader = JSONObjectFactory.generateConstantCommonEventHeader(); - JSONObject pnfRegistrationFields = JSONObjectFactory.generatePnfRegistrationFields(); + Map<String, Object> commonEventHeaderFields = commonEventHeaderParams.toMap(); + commonEventHeaderFields.forEach((key, value) -> { + commonEventHeader.put(key, value); + }); - paramsMap.forEach((key, value) -> { + JSONObject pnfRegistrationFields = JSONObjectFactory.generatePnfRegistrationFields(); + pnfRegistrationParams.ifPresent(jsonObject -> { + copyParametersToFields(jsonObject.toMap(), pnfRegistrationFields); + commonEventHeader.put(DOMAIN, DOMAIN_PNF_REGISTRATION); + commonEventHeader.put(EVENT_TYPE, DOMAIN_PNF_REGISTRATION); + event.put(PNF_REGISTRATION_FIELDS, pnfRegistrationFields); + }); - if (key.startsWith(PNF_PREFIX)) { - pnfRegistrationFields.put(key.substring(PNF_PREFIX.length()), value); - } else { - commonEventHeader.put(key, value); - } + JSONObject notificationFields = JSONObjectFactory.generateNotificationFields(); + notificationParams.ifPresent(jsonObject -> { + copyParametersToFields(jsonObject.toMap(), notificationFields); + commonEventHeader.put(DOMAIN, DOMAIN_NOTIFICATION); + event.put(NOTIFICATION_FIELDS, notificationFields); }); - JSONObject event = new JSONObject(); event.put(COMMON_EVENT_HEADER, commonEventHeader); - event.put(PNF_REGISTRATION_FIELDS, pnfRegistrationFields); + JSONObject root = new JSONObject(); root.put(EVENT, event); return root; } + private void copyParametersToFields(Map<String, Object> paramersMap, JSONObject fieldsJsonObject) { + paramersMap.forEach((key, value) -> { + fieldsJsonObject.put(key, value); + }); + } + } |