diff options
Diffstat (limited to 'test/mocks/pnfsimulator/src/main')
4 files changed, 204 insertions, 140 deletions
diff --git a/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/message/MessageBuilder.java b/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/message/MessageBuilder.java new file mode 100644 index 000000000..ef0b2fdd2 --- /dev/null +++ b/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/message/MessageBuilder.java @@ -0,0 +1,81 @@ +/* + * ============LICENSE_START======================================================= + * PNF-REGISTRATION-HANDLER + * ================================================================================ + * Copyright (C) 2019 NOKIA Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.pnfsimulator.message; + +import org.json.JSONObject; + +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.DOMAIN_NOTIFICATION; +import static org.onap.pnfsimulator.message.MessageConstants.DOMAIN_PNF_REGISTRATION; +import static org.onap.pnfsimulator.message.MessageConstants.EVENT; +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.PNF_REGISTRATION_FIELDS; + +public final class MessageBuilder { + private JSONObject root; + private JSONObject event; + private JSONObject commonEventHeader; + + private MessageBuilder() { + } + + public static MessageBuilder withCommonEventHeaderParams(JSONObject commonEventHeaderParams) { + MessageBuilder builder = new MessageBuilder(); + builder.initializeBuilder(commonEventHeaderParams); + return builder; + } + + public MessageBuilder withNotificationParams(JSONObject notificationParams) { + JSONObject notificationFields = JSONObjectFactory.generateNotificationFields(); + merge(notificationParams, notificationFields); + commonEventHeader.put(DOMAIN, DOMAIN_NOTIFICATION); + event.put(NOTIFICATION_FIELDS, notificationFields); + return this; + } + + public MessageBuilder withPnfRegistrationParams(JSONObject pnfRegistrationParams) { + JSONObject pnfRegistrationFields = JSONObjectFactory.generatePnfRegistrationFields(); + merge(pnfRegistrationParams, pnfRegistrationFields); + commonEventHeader.put(DOMAIN, DOMAIN_PNF_REGISTRATION); + commonEventHeader.put(EVENT_TYPE, DOMAIN_PNF_REGISTRATION); + event.put(PNF_REGISTRATION_FIELDS, pnfRegistrationFields); + return this; + } + + public JSONObject build() { + return root; + } + + private void initializeBuilder(JSONObject commonEventHeaderParams) { + root = new JSONObject(); + event = new JSONObject(); + commonEventHeader = JSONObjectFactory.generateConstantCommonEventHeader(); + commonEventHeaderParams.toMap().forEach(commonEventHeader::put); + event.put(COMMON_EVENT_HEADER, commonEventHeader); + root.put(EVENT, event); + } + + private void merge(JSONObject source, JSONObject destination) { + source.toMap().forEach(destination::put); + } +} 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 13114eefb..fee574597 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 @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * PNF-REGISTRATION-HANDLER * ================================================================================ - * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. + * Copyright (C) 2018-2019 NOKIA Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,61 +20,23 @@ 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.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 org.json.JSONObject; public class MessageProvider { - public JSONObject createMessage(JSONObject commonEventHeaderParams, Optional<JSONObject> pnfRegistrationParams, - Optional<JSONObject> notificationParams) { - - if (!pnfRegistrationParams.isPresent() && !notificationParams.isPresent()) { - throw new IllegalArgumentException( - "Both PNF registration and notification parameters objects are not present"); - } - JSONObject event = new JSONObject(); - - JSONObject commonEventHeader = JSONObjectFactory.generateConstantCommonEventHeader(); - Map<String, Object> commonEventHeaderFields = commonEventHeaderParams.toMap(); - commonEventHeaderFields.forEach((key, value) -> { - commonEventHeader.put(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); - }); - - JSONObject notificationFields = JSONObjectFactory.generateNotificationFields(); - notificationParams.ifPresent(jsonObject -> { - copyParametersToFields(jsonObject.toMap(), notificationFields); - commonEventHeader.put(DOMAIN, DOMAIN_NOTIFICATION); - event.put(NOTIFICATION_FIELDS, notificationFields); - }); - - event.put(COMMON_EVENT_HEADER, commonEventHeader); - JSONObject root = new JSONObject(); - root.put(EVENT, event); - return root; + public JSONObject createMessageWithNotification(JSONObject commonEventHeaderParams, + JSONObject notificationParams) { + return MessageBuilder + .withCommonEventHeaderParams(commonEventHeaderParams) + .withNotificationParams(notificationParams) + .build(); } - private void copyParametersToFields(Map<String, Object> paramersMap, JSONObject fieldsJsonObject) { - paramersMap.forEach((key, value) -> { - fieldsJsonObject.put(key, value); - }); + public JSONObject createMessageWithPnfRegistration(JSONObject commonEventHeaderParams, JSONObject pnfRegistrationParams) { + return MessageBuilder + .withCommonEventHeaderParams(commonEventHeaderParams) + .withPnfRegistrationParams(pnfRegistrationParams) + .build(); } } diff --git a/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/rest/SimulatorController.java b/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/rest/SimulatorController.java index 506d21b6c..6b9f02622 100644 --- a/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/rest/SimulatorController.java +++ b/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/rest/SimulatorController.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * PNF-REGISTRATION-HANDLER * ================================================================================ - * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. + * Copyright (C) 2018-2019 NOKIA Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,32 +20,9 @@ package org.onap.pnfsimulator.rest; -import static org.onap.pnfsimulator.logging.MDCVariables.INSTANCE_UUID; -import static org.onap.pnfsimulator.logging.MDCVariables.INVOCATION_ID; -import static org.onap.pnfsimulator.logging.MDCVariables.REQUEST_ID; -import static org.onap.pnfsimulator.logging.MDCVariables.RESPONSE_CODE; -import static org.onap.pnfsimulator.logging.MDCVariables.SERVICE_NAME; -import static org.onap.pnfsimulator.logging.MDCVariables.X_INVOCATION_ID; -import static org.onap.pnfsimulator.logging.MDCVariables.X_ONAP_REQUEST_ID; -import static org.onap.pnfsimulator.message.MessageConstants.SIMULATOR_PARAMS; -import static org.onap.pnfsimulator.message.MessageConstants.COMMON_EVENT_HEADER_PARAMS; -import static org.onap.pnfsimulator.rest.util.ResponseBuilder.MESSAGE; -import static org.onap.pnfsimulator.rest.util.ResponseBuilder.REMAINING_TIME; -import static org.onap.pnfsimulator.rest.util.ResponseBuilder.SIMULATOR_STATUS; -import static org.onap.pnfsimulator.rest.util.ResponseBuilder.TIMESTAMP; -import static org.springframework.http.HttpStatus.BAD_REQUEST; -import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; -import static org.springframework.http.HttpStatus.OK; - -import java.io.IOException; -import java.text.DateFormat; -import java.text.SimpleDateFormat; import com.github.fge.jsonschema.core.exceptions.ProcessingException; -import java.util.Optional; -import java.util.UUID; import org.json.JSONException; import org.json.JSONObject; -import org.onap.pnfsimulator.message.MessageConstants; import org.onap.pnfsimulator.rest.util.DateUtil; import org.onap.pnfsimulator.rest.util.ResponseBuilder; import org.onap.pnfsimulator.simulator.Simulator; @@ -67,6 +44,30 @@ import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import java.io.IOException; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.UUID; + +import static org.onap.pnfsimulator.logging.MDCVariables.INSTANCE_UUID; +import static org.onap.pnfsimulator.logging.MDCVariables.INVOCATION_ID; +import static org.onap.pnfsimulator.logging.MDCVariables.REQUEST_ID; +import static org.onap.pnfsimulator.logging.MDCVariables.RESPONSE_CODE; +import static org.onap.pnfsimulator.logging.MDCVariables.SERVICE_NAME; +import static org.onap.pnfsimulator.logging.MDCVariables.X_INVOCATION_ID; +import static org.onap.pnfsimulator.logging.MDCVariables.X_ONAP_REQUEST_ID; +import static org.onap.pnfsimulator.message.MessageConstants.COMMON_EVENT_HEADER_PARAMS; +import static org.onap.pnfsimulator.message.MessageConstants.NOTIFICATION_PARAMS; +import static org.onap.pnfsimulator.message.MessageConstants.PNF_REGISTRATION_PARAMS; +import static org.onap.pnfsimulator.message.MessageConstants.SIMULATOR_PARAMS; +import static org.onap.pnfsimulator.rest.util.ResponseBuilder.MESSAGE; +import static org.onap.pnfsimulator.rest.util.ResponseBuilder.REMAINING_TIME; +import static org.onap.pnfsimulator.rest.util.ResponseBuilder.SIMULATOR_STATUS; +import static org.onap.pnfsimulator.rest.util.ResponseBuilder.TIMESTAMP; +import static org.springframework.http.HttpStatus.BAD_REQUEST; +import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; +import static org.springframework.http.HttpStatus.OK; + @RestController @RequestMapping("/simulator") public class SimulatorController { @@ -95,10 +96,10 @@ public class SimulatorController { if (isSimulatorRunning()) { MDC.put(RESPONSE_CODE, BAD_REQUEST.toString()); return ResponseBuilder - .status(BAD_REQUEST) - .put(TIMESTAMP, DateUtil.getTimestamp(RESPONSE_DATE_FORMAT)) - .put(MESSAGE, "Cannot start simulator since it's already running") - .build(); + .status(BAD_REQUEST) + .put(TIMESTAMP, DateUtil.getTimestamp(RESPONSE_DATE_FORMAT)) + .put(MESSAGE, "Cannot start simulator since it's already running") + .build(); } try { @@ -106,73 +107,81 @@ public class SimulatorController { JSONObject root = new JSONObject(message); JSONObject simulatorParams = root.getJSONObject(SIMULATOR_PARAMS); JSONObject commonEventHeaderParams = root.getJSONObject(COMMON_EVENT_HEADER_PARAMS); - Optional<JSONObject> pnfRegistrationFields = root.has(MessageConstants.PNF_REGISTRATION_PARAMS) ? Optional - .of(root.getJSONObject(MessageConstants.PNF_REGISTRATION_PARAMS)) : Optional.empty(); - Optional<JSONObject> notificationFields = root.has(MessageConstants.NOTIFICATION_PARAMS) ? Optional - .of(root.getJSONObject(MessageConstants.NOTIFICATION_PARAMS)) : Optional.empty(); - simulator = factory - .create(simulatorParams, commonEventHeaderParams, pnfRegistrationFields, notificationFields); + simulator = createSimulator(root, simulatorParams, commonEventHeaderParams); simulator.start(); MDC.put(RESPONSE_CODE, OK.toString()); return ResponseBuilder - .status(OK) - .put(TIMESTAMP, DateUtil.getTimestamp(RESPONSE_DATE_FORMAT)) - .put(MESSAGE, "Simulator started") - .build(); + .status(OK) + .put(TIMESTAMP, DateUtil.getTimestamp(RESPONSE_DATE_FORMAT)) + .put(MESSAGE, "Simulator started") + .build(); } catch (JSONException e) { MDC.put(RESPONSE_CODE, BAD_REQUEST.toString()); LOGGER.warn("Cannot start simulator, invalid json format: {}", e.getMessage()); LOGGER.debug("Received json has invalid format", e); return ResponseBuilder - .status(BAD_REQUEST) - .put(TIMESTAMP, DateUtil.getTimestamp(RESPONSE_DATE_FORMAT)) - .put(MESSAGE, "Cannot start simulator, invalid json format") - .build(); + .status(BAD_REQUEST) + .put(TIMESTAMP, DateUtil.getTimestamp(RESPONSE_DATE_FORMAT)) + .put(MESSAGE, "Cannot start simulator, invalid json format") + .build(); } catch (ProcessingException | ValidationException | IOException e) { MDC.put(RESPONSE_CODE, BAD_REQUEST.toString()); LOGGER.warn("Json validation failed: {}", e.getMessage()); return ResponseBuilder - .status(BAD_REQUEST) - .put(TIMESTAMP, DateUtil.getTimestamp(RESPONSE_DATE_FORMAT)) - .put(MESSAGE, "Cannot start simulator - Json format is not compatible with schema definitions") - .build(); + .status(BAD_REQUEST) + .put(TIMESTAMP, DateUtil.getTimestamp(RESPONSE_DATE_FORMAT)) + .put(MESSAGE, "Cannot start simulator - Json format is not compatible with schema definitions") + .build(); } catch (Exception e) { MDC.put(RESPONSE_CODE, INTERNAL_SERVER_ERROR.toString()); LOGGER.error("Cannot start simulator - unexpected exception", e); return ResponseBuilder - .status(INTERNAL_SERVER_ERROR) - .put(TIMESTAMP, DateUtil.getTimestamp(RESPONSE_DATE_FORMAT)) - .put(MESSAGE, "Unexpected exception: " + e.getMessage()) - .build(); + .status(INTERNAL_SERVER_ERROR) + .put(TIMESTAMP, DateUtil.getTimestamp(RESPONSE_DATE_FORMAT)) + .put(MESSAGE, "Unexpected exception: " + e.getMessage()) + .build(); } finally { MDC.clear(); } } + private Simulator createSimulator(JSONObject root, JSONObject simulatorParams, JSONObject commonEventHeaderParams) + throws ProcessingException, IOException, ValidationException { + JSONObject pnfRegistrationFields = root.optJSONObject(PNF_REGISTRATION_PARAMS); + JSONObject notificationFields = root.optJSONObject(NOTIFICATION_PARAMS); + if (pnfRegistrationFields != null && notificationFields == null) { + return factory.createSimulatorWithPnfRegistration(simulatorParams, commonEventHeaderParams, pnfRegistrationFields); + } else if (pnfRegistrationFields == null && notificationFields != null) { + return factory.createSimulatorWithNotification(simulatorParams, commonEventHeaderParams, notificationFields); + } else { + throw new ValidationException("Exactly one of pnfRegistrationFields or notificationFields should be present"); + } + } + @GetMapping("status") public ResponseEntity status() { if (isSimulatorRunning()) { ResponseBuilder responseBuilder = ResponseBuilder - .status(OK) - .put(TIMESTAMP, DateUtil.getTimestamp(RESPONSE_DATE_FORMAT)) - .put(SIMULATOR_STATUS, "RUNNING"); + .status(OK) + .put(TIMESTAMP, DateUtil.getTimestamp(RESPONSE_DATE_FORMAT)) + .put(SIMULATOR_STATUS, "RUNNING"); return !simulator.isEndless() ? - responseBuilder - .put(REMAINING_TIME, simulator.getRemainingTime()) - .build() : - responseBuilder - .build(); + responseBuilder + .put(REMAINING_TIME, simulator.getRemainingTime()) + .build() : + responseBuilder + .build(); } else { return ResponseBuilder - .status(OK) - .put(TIMESTAMP, DateUtil.getTimestamp(RESPONSE_DATE_FORMAT)) - .put(SIMULATOR_STATUS, "NOT RUNNING") - .build(); + .status(OK) + .put(TIMESTAMP, DateUtil.getTimestamp(RESPONSE_DATE_FORMAT)) + .put(SIMULATOR_STATUS, "NOT RUNNING") + .build(); } } @@ -182,16 +191,16 @@ public class SimulatorController { simulator.interrupt(); return ResponseBuilder - .status(OK) - .put(TIMESTAMP, DateUtil.getTimestamp(RESPONSE_DATE_FORMAT)) - .put(MESSAGE, "Simulator successfully stopped") - .build(); + .status(OK) + .put(TIMESTAMP, DateUtil.getTimestamp(RESPONSE_DATE_FORMAT)) + .put(MESSAGE, "Simulator successfully stopped") + .build(); } else { return ResponseBuilder - .status(BAD_REQUEST) - .put(TIMESTAMP, DateUtil.getTimestamp(RESPONSE_DATE_FORMAT)) - .put(MESSAGE, "Cannot stop simulator, because it's not running") - .build(); + .status(BAD_REQUEST) + .put(TIMESTAMP, DateUtil.getTimestamp(RESPONSE_DATE_FORMAT)) + .put(MESSAGE, "Cannot stop simulator, because it's not running") + .build(); } } diff --git a/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/SimulatorFactory.java b/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/SimulatorFactory.java index 046c97cad..8e16ad2ce 100644 --- a/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/SimulatorFactory.java +++ b/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/SimulatorFactory.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * PNF-REGISTRATION-HANDLER * ================================================================================ - * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. + * Copyright (C) 2018-2019 NOKIA Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,15 +20,7 @@ package org.onap.pnfsimulator.simulator; -import static java.lang.Integer.parseInt; -import static org.onap.pnfsimulator.message.MessageConstants.MESSAGE_INTERVAL; -import static org.onap.pnfsimulator.message.MessageConstants.TEST_DURATION; -import static org.onap.pnfsimulator.message.MessageConstants.VES_SERVER_URL; - import com.github.fge.jsonschema.core.exceptions.ProcessingException; -import java.io.IOException; -import java.time.Duration; -import java.util.Optional; import org.json.JSONObject; import org.onap.pnfsimulator.message.MessageProvider; import org.onap.pnfsimulator.simulator.validation.JSONValidator; @@ -36,6 +28,14 @@ import org.onap.pnfsimulator.simulator.validation.ValidationException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.io.IOException; +import java.time.Duration; + +import static java.lang.Integer.parseInt; +import static org.onap.pnfsimulator.message.MessageConstants.MESSAGE_INTERVAL; +import static org.onap.pnfsimulator.message.MessageConstants.TEST_DURATION; +import static org.onap.pnfsimulator.message.MessageConstants.VES_SERVER_URL; + @Service public class SimulatorFactory { @@ -50,22 +50,34 @@ public class SimulatorFactory { this.validator = validator; } - public Simulator create(JSONObject simulatorParams, JSONObject commonEventHeaderParams, - Optional<JSONObject> pnfRegistrationParams, Optional<JSONObject> notificationParams) - throws ProcessingException, IOException, ValidationException { + public Simulator createSimulatorWithNotification(JSONObject simulatorParams, JSONObject commonEventHeaderParams, + JSONObject notificationParams) + throws ProcessingException, IOException, ValidationException { + JSONObject messageBody = messageProvider + .createMessageWithNotification(commonEventHeaderParams, notificationParams); + return createSimulatorWithMessage(simulatorParams, messageBody); + } + + public Simulator createSimulatorWithPnfRegistration(JSONObject simulatorParams, JSONObject commonEventHeaderParams, + JSONObject pnfRegistrationParams) + throws ProcessingException, IOException, ValidationException { + JSONObject messageBody = messageProvider + .createMessageWithPnfRegistration(commonEventHeaderParams, pnfRegistrationParams); + return createSimulatorWithMessage(simulatorParams, messageBody); + } + + private Simulator createSimulatorWithMessage(JSONObject simulatorParams, JSONObject messageBody) + throws ValidationException, ProcessingException, IOException { Duration duration = Duration.ofSeconds(parseInt(simulatorParams.getString(TEST_DURATION))); Duration interval = Duration.ofSeconds(parseInt(simulatorParams.getString(MESSAGE_INTERVAL))); String vesUrl = simulatorParams.getString(VES_SERVER_URL); - - JSONObject messageBody = messageProvider - .createMessage(commonEventHeaderParams, pnfRegistrationParams, notificationParams); validator.validate(messageBody.toString(), DEFAULT_OUTPUT_SCHEMA_PATH); return Simulator.builder() - .withVesUrl(vesUrl) - .withDuration(duration) - .withInterval(interval) - .withMessageBody(messageBody) - .build(); + .withVesUrl(vesUrl) + .withDuration(duration) + .withInterval(interval) + .withMessageBody(messageBody) + .build(); } }
\ No newline at end of file |