diff options
Diffstat (limited to 'test/mocks/pnfsimulator/src')
14 files changed, 649 insertions, 0 deletions
diff --git a/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/Main.java b/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/Main.java new file mode 100644 index 000000000..7dfa68669 --- /dev/null +++ b/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/Main.java @@ -0,0 +1,37 @@ +package org.onap.pnfsimulator; + +import java.io.IOException; +import org.apache.commons.cli.ParseException; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.onap.pnfsimulator.cli.SimulatorParamsProvider; +import org.onap.pnfsimulator.cli.SimulatorParams; +import org.onap.pnfsimulator.message.MessageProvider; +import org.onap.pnfsimulator.simulator.SimulatorFactory; +import org.onap.pnfsimulator.simulator.validation.ParamsValidator; +import org.onap.pnfsimulator.simulator.validation.ValidationException; + +public class Main { + + private static Logger logger = LogManager.getLogger(Main.class); + private static SimulatorFactory simulatorFactory = + new SimulatorFactory(MessageProvider.getInstance(), ParamsValidator.getInstance()); + + public static void main(String[] args) { + + try { + + SimulatorParams params = new SimulatorParamsProvider().parse(args); + simulatorFactory + .create(params.getVesAddress(), params.getConfigFilePath()) + .start(); + + } catch (IOException e) { + logger.error("Invalid config file format", e); + } catch (ParseException e) { + logger.error("Invalid cli params", e); + } catch (ValidationException e){ + logger.error("Missing some mandatory params:", e); + } + } +} diff --git a/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/cli/SimulatorParams.java b/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/cli/SimulatorParams.java new file mode 100644 index 000000000..64b3589ac --- /dev/null +++ b/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/cli/SimulatorParams.java @@ -0,0 +1,45 @@ +package org.onap.pnfsimulator.cli; + +import java.util.Objects; + +public class SimulatorParams { + + private String vesAddress; + private String configFilePath; + + public SimulatorParams(String vesAddress, String configFilePath) { + this.vesAddress = vesAddress; + this.configFilePath = configFilePath; + } + + public String getVesAddress() { + return vesAddress; + } + + public String getConfigFilePath() { + return configFilePath; + } + + @Override + public String toString() { + return String.format("VES address=%s, Configuration file=%s", vesAddress, configFilePath); + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof SimulatorParams)) { + return false; + } + SimulatorParams params = (SimulatorParams) o; + return Objects.equals(vesAddress, params.vesAddress) && + Objects.equals(configFilePath, params.configFilePath); + } + + @Override + public int hashCode() { + return Objects.hash(vesAddress, configFilePath); + } +} diff --git a/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/cli/SimulatorParamsProvider.java b/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/cli/SimulatorParamsProvider.java new file mode 100644 index 000000000..fcf5d79f6 --- /dev/null +++ b/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/cli/SimulatorParamsProvider.java @@ -0,0 +1,41 @@ +package org.onap.pnfsimulator.cli; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.CommandLineParser; +import org.apache.commons.cli.DefaultParser; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; + +public class SimulatorParamsProvider { + + private static final String CLI_VAR_VES_ADDRESS = "address"; + private static final String CLI_VAR_CONFIG_FILE_PATH = "config"; + private static final String ENV_VAR_VES_ADDRESS = "VES_ADDRESS"; + private static final String ENV_VAR_CONFIG_FILE_PATH = "CONFIG_FILE_PATH"; + + private Options options; + private CommandLineParser parser; + + public SimulatorParamsProvider() { + createOptions(); + parser = new DefaultParser(); + } + + public SimulatorParams parse(String[] arg) throws ParseException { + CommandLine line = parser.parse(options, arg); + return new SimulatorParams( + line.getOptionValue(CLI_VAR_VES_ADDRESS, System.getenv().get(ENV_VAR_VES_ADDRESS)), + line.getOptionValue(CLI_VAR_CONFIG_FILE_PATH, System.getenv().get(ENV_VAR_CONFIG_FILE_PATH))); + } + + private void createOptions() { + options = new Options(); + + Option vesCollectorUlrOpt = new Option(CLI_VAR_VES_ADDRESS, true, "VES collector URL"); + options.addOption(vesCollectorUlrOpt); + + Option simulatorConfigFilePathOpt = new Option(CLI_VAR_CONFIG_FILE_PATH, true, "Simulator configuration file location."); + options.addOption(simulatorConfigFilePathOpt); + } +} diff --git a/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/message/MessageConstants.java b/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/message/MessageConstants.java new file mode 100644 index 000000000..77b372721 --- /dev/null +++ b/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/message/MessageConstants.java @@ -0,0 +1,33 @@ +package org.onap.pnfsimulator.message; + +public final class MessageConstants { + + public static final String DOMAIN = "domain"; + public static final String EVENT_ID = "eventId"; + public static final String EVENT_TYPE = "eventType"; + public static final String LAST_EPOCH_MICROSEC = "lastEpochMicrosec"; + public static final String PRIORITY = "priority"; + public static final String SEQUENCE = "sequence"; + public static final String START_EPOCH_MICROSEC = "startEpochMicrosec"; + public static final String INTERNAL_HEADER_FIELDS = "internalHeaderFields"; + public static final String VERSION = "version"; + public static final String OTHER_FIELDS_VERSION = "otherFieldsVersion"; + public static final String PNF_LAST_SERVICE_DATE = "pnfLastServiceDate"; + public static final String PNF_MANUFACTURE_DATE = "pnfManufactureDate"; + + // mandatory + public static final String PNF_OAM_IPV4_ADDRESS = "pnfOamIpv4Address"; + public static final String PNF_OAM_IPV6_ADDRESS = "pnfOamIpv6Address"; + public static final String PNF_SERIAL_NUMBER = "pnfSerialNumber"; + public static final String PNF_VENDOR_NAME = "pnfVendorName"; + + public static final String PNF_PREFIX = "pnf"; + public static final String COMMON_EVENT_HEADER = "commonEventHeader"; + public static final String OTHER_FIELDS = "otherFields"; + public static final String TEST_DURATION = "testDuration"; + public static final String MESSAGE_INTERVAL = "messageInterval"; + + private MessageConstants() { + } + +} 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 new file mode 100644 index 000000000..5e8e535f9 --- /dev/null +++ b/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/message/MessageProvider.java @@ -0,0 +1,88 @@ +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_ID; +import static org.onap.pnfsimulator.message.MessageConstants.EVENT_TYPE; +import static org.onap.pnfsimulator.message.MessageConstants.INTERNAL_HEADER_FIELDS; +import static org.onap.pnfsimulator.message.MessageConstants.LAST_EPOCH_MICROSEC; +import static org.onap.pnfsimulator.message.MessageConstants.OTHER_FIELDS; +import static org.onap.pnfsimulator.message.MessageConstants.OTHER_FIELDS_VERSION; +import static org.onap.pnfsimulator.message.MessageConstants.PNF_LAST_SERVICE_DATE; +import static org.onap.pnfsimulator.message.MessageConstants.PNF_MANUFACTURE_DATE; +import static org.onap.pnfsimulator.message.MessageConstants.PNF_PREFIX; +import static org.onap.pnfsimulator.message.MessageConstants.PRIORITY; +import static org.onap.pnfsimulator.message.MessageConstants.SEQUENCE; +import static org.onap.pnfsimulator.message.MessageConstants.START_EPOCH_MICROSEC; +import static org.onap.pnfsimulator.message.MessageConstants.VERSION; + +import java.util.Map; +import java.util.UUID; + +import com.google.common.base.Preconditions; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.json.JSONObject; + +public class MessageProvider { + + private static MessageProvider instance; + + public static MessageProvider getInstance() { + if (instance == null) { + instance = new MessageProvider(); + } + return instance; + } + + public JSONObject createMessage(JSONObject params) { + + Preconditions.checkArgument(params != null, "Params object cannot be null"); + Map<String, Object> paramsMap = params.toMap(); + JSONObject root = new JSONObject(); + JSONObject commonEventHeader = generateConstantCommonEventHeader(); + JSONObject otherFields = generateConstantOtherFields(); + + paramsMap.forEach((key, value) -> { + + if (key.startsWith(PNF_PREFIX)) { + otherFields.put(key, value); + } else { + commonEventHeader.put(key, value); + } + }); + + root.put(COMMON_EVENT_HEADER, commonEventHeader); + root.put(OTHER_FIELDS, otherFields); + return root; + } + + private JSONObject generateConstantCommonEventHeader() { + + JSONObject commonEventHeader = new JSONObject(); + long timestamp = System.currentTimeMillis(); + + commonEventHeader.put(DOMAIN, "other"); + commonEventHeader.put(EVENT_ID, UUID.randomUUID() + "-reg"); + commonEventHeader.put(EVENT_TYPE, "pnfRegistration"); + commonEventHeader.put(LAST_EPOCH_MICROSEC, timestamp); + commonEventHeader.put(PRIORITY, "Normal"); + commonEventHeader.put(SEQUENCE, 0); + commonEventHeader.put(START_EPOCH_MICROSEC, timestamp); + commonEventHeader.put(INTERNAL_HEADER_FIELDS, new JSONObject()); + commonEventHeader.put(VERSION, 3); + + return commonEventHeader; + } + + private JSONObject generateConstantOtherFields() { + + JSONObject otherFields = new JSONObject(); + + otherFields.put(OTHER_FIELDS_VERSION, 1); + otherFields.put(PNF_LAST_SERVICE_DATE, System.currentTimeMillis()); + otherFields.put(PNF_MANUFACTURE_DATE, System.currentTimeMillis()); + + return otherFields; + } +} diff --git a/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/Simulator.java b/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/Simulator.java new file mode 100644 index 000000000..75ce71e5e --- /dev/null +++ b/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/Simulator.java @@ -0,0 +1,45 @@ +package org.onap.pnfsimulator.simulator; + +import java.time.Duration; +import java.time.Instant; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.json.JSONObject; +import org.onap.pnfsimulator.simulator.client.HttpClientProvider; + +public class Simulator { + + private static final Logger logger = LogManager.getLogger(HttpClientProvider.class); + private HttpClientProvider clientProvider; + private JSONObject messageBody; + private Duration duration; + private Duration interval; + + public Simulator(String vesServerUrl, JSONObject messageBody, Duration duration, Duration interval) { + this.messageBody = messageBody; + this.duration = duration; + this.interval = interval; + this.clientProvider = new HttpClientProvider(vesServerUrl); + } + + public void start() { + logger.info("SIMULATOR STARTED - DURATION: {}s, INTERVAL: {}s", duration.getSeconds(), interval.getSeconds()); + + Instant endTime = Instant.now().plus(duration); + while (runningTimeNotExceeded(endTime)) { + try { + logger.info("MESSAGE TO BE SENT:\n{}", messageBody.toString(4)); + clientProvider.sendMsg(messageBody.toString()); + Thread.sleep(interval.toMillis()); + } catch (InterruptedException e) { + logger.error("SIMULATOR INTERRUPTED"); + break; + } + } + logger.info("SIMULATOR FINISHED"); + } + + private boolean runningTimeNotExceeded(Instant endTime) { + return Instant.now().isBefore(endTime); + } +}
\ No newline at end of file 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 new file mode 100644 index 000000000..c9bd8d7fe --- /dev/null +++ b/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/SimulatorFactory.java @@ -0,0 +1,42 @@ +package org.onap.pnfsimulator.simulator; + +import static org.onap.pnfsimulator.message.MessageConstants.MESSAGE_INTERVAL; +import static org.onap.pnfsimulator.message.MessageConstants.TEST_DURATION; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.time.Duration; +import java.util.List; +import org.apache.commons.io.FileUtils; +import org.json.JSONObject; +import org.onap.pnfsimulator.message.MessageProvider; +import org.onap.pnfsimulator.simulator.validation.ParamsValidator; +import org.onap.pnfsimulator.simulator.validation.ValidationException; + +public class SimulatorFactory { + + private MessageProvider messageProvider; + private ParamsValidator paramsValidator; + + public SimulatorFactory(MessageProvider messageProvider, ParamsValidator paramsValidator) { + this.messageProvider = messageProvider; + this.paramsValidator = paramsValidator; + } + + public Simulator create(String vesServerUrl, String configFilePath) throws IOException, ValidationException { + + String configJson = FileUtils.readFileToString(new File(configFilePath), StandardCharsets.UTF_8); + JSONObject configObject = new JSONObject(configJson); + + paramsValidator.validate(configObject); + Duration duration = Duration.ofSeconds(parseJsonField(configObject, TEST_DURATION)); + Duration interval = Duration.ofSeconds(parseJsonField(configObject, MESSAGE_INTERVAL)); + JSONObject messageBody = messageProvider.createMessage(configObject); + return new Simulator(vesServerUrl, messageBody, duration, interval); + } + + private int parseJsonField(JSONObject json, String fieldName) { + return Integer.parseInt((String) json.remove(fieldName)); + } +}
\ No newline at end of file diff --git a/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/client/HttpClientProvider.java b/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/client/HttpClientProvider.java new file mode 100644 index 000000000..a24889cb3 --- /dev/null +++ b/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/client/HttpClientProvider.java @@ -0,0 +1,56 @@ +package org.onap.pnfsimulator.simulator.client; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +public class HttpClientProvider { + + private static final Logger logger = LogManager.getLogger(HttpClientProvider.class); + private static final String CONTENT_TYPE = "Content-Type"; + private static final String APPLICATION_JSON = "application/json"; + + private HttpClient client; + private String url; + + public HttpClientProvider(String url) { + + RequestConfig config = RequestConfig.custom() + .setConnectTimeout(1000) + .setConnectionRequestTimeout(1000) + .setSocketTimeout(1000) + .build(); + + this.client = HttpClientBuilder + .create() + .setDefaultRequestConfig(config) + .build(); + + this.url = url; + } + + public void sendMsg(String content) { + try { + HttpPost request = createRequest(content); + HttpResponse response = client.execute(request); + logger.info("MESSAGE SENT, VES RESPONSE CODE: {}", response.getStatusLine()); + } catch (IOException e) { + logger.info("ERROR SENDING MESSAGE TO VES: {}", e.getMessage()); + } + } + + private HttpPost createRequest(String content) throws UnsupportedEncodingException { + StringEntity stringEntity = new StringEntity(content); + HttpPost request = new HttpPost(url); + request.addHeader(CONTENT_TYPE, APPLICATION_JSON); + request.setEntity(stringEntity); + return request; + } +} diff --git a/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/validation/ParamsValidator.java b/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/validation/ParamsValidator.java new file mode 100644 index 000000000..a0fc110bd --- /dev/null +++ b/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/validation/ParamsValidator.java @@ -0,0 +1,82 @@ +package org.onap.pnfsimulator.simulator.validation; + +import static org.onap.pnfsimulator.message.MessageConstants.MESSAGE_INTERVAL; +import static org.onap.pnfsimulator.message.MessageConstants.PNF_OAM_IPV4_ADDRESS; +import static org.onap.pnfsimulator.message.MessageConstants.PNF_OAM_IPV6_ADDRESS; +import static org.onap.pnfsimulator.message.MessageConstants.PNF_SERIAL_NUMBER; +import static org.onap.pnfsimulator.message.MessageConstants.PNF_VENDOR_NAME; +import static org.onap.pnfsimulator.message.MessageConstants.TEST_DURATION; + +import com.google.common.collect.ImmutableMap; +import java.util.ArrayList; +import java.util.List; +import java.util.function.BiPredicate; +import org.apache.commons.lang3.StringUtils; +import org.json.JSONObject; + + +public class ParamsValidator { + + private final static String MISSING_PARAMS_ERROR = "Some mandatory params are missing"; + private static ParamsValidator instance; + + + public static ParamsValidator getInstance() { + if (instance == null) { + instance = new ParamsValidator(); + } + return instance; + } + + public void validate(JSONObject params) throws ValidationException { + ImmutableMap<String, BiPredicate<JSONObject, String>> paramValidators = ImmutableMap + .<String, BiPredicate<JSONObject, String>>builder() + .put(TEST_DURATION, this::isNotNumeric) + .put(MESSAGE_INTERVAL, this::isNotNumeric) + .put(PNF_SERIAL_NUMBER, this::nullOrEmpty) + .put(PNF_VENDOR_NAME, this::nullOrEmpty) + .put(PNF_OAM_IPV4_ADDRESS, this::nullOrEmpty) + .put(PNF_OAM_IPV6_ADDRESS, this::nullOrEmpty) + .build(); + + List<String> missingParams = new ArrayList<>(); + + paramValidators.forEach((param, validator) -> { + if (validator.test(params, param)) { + missingParams.add(param); + } + }); + + clearIPError(missingParams); + if (!missingParams.isEmpty()) { + throw new ValidationException(constructMessage(missingParams)); + } + } + + private String constructMessage(List<String> missingParams) { + StringBuilder msg = new StringBuilder(MISSING_PARAMS_ERROR); + + missingParams.forEach(param -> { + msg.append('\n'); + msg.append(param); + }); + + return msg.toString(); + } + + private boolean isNotNumeric(JSONObject params, String param) { + return nullOrEmpty(params, param) || !StringUtils.isNumeric(params.getString(param)); + } + + private boolean nullOrEmpty(JSONObject params, String param) { + return !params.has(param) || params.getString(param).isEmpty(); + } + + private void clearIPError(List<String> missingParams) { + // if only one IP is missing clear the error + if (!(missingParams.contains(PNF_OAM_IPV4_ADDRESS) && missingParams.contains(PNF_OAM_IPV6_ADDRESS))) { + missingParams.remove(PNF_OAM_IPV4_ADDRESS); + missingParams.remove(PNF_OAM_IPV6_ADDRESS); + } + } +} diff --git a/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/validation/ValidationException.java b/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/validation/ValidationException.java new file mode 100644 index 000000000..9855a784e --- /dev/null +++ b/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/validation/ValidationException.java @@ -0,0 +1,8 @@ +package org.onap.pnfsimulator.simulator.validation; + +public class ValidationException extends Exception { + + public ValidationException(String message) { + super(message); + } +} diff --git a/test/mocks/pnfsimulator/src/main/resources/log4j2.xml b/test/mocks/pnfsimulator/src/main/resources/log4j2.xml new file mode 100644 index 000000000..250f41709 --- /dev/null +++ b/test/mocks/pnfsimulator/src/main/resources/log4j2.xml @@ -0,0 +1,19 @@ +<?xml version="1.0" encoding="UTF-8"?> +<Configuration status="WARN"> + <Properties> + <Property name="log4j.logLevel" value="info"/> + </Properties> + <Appenders> + <Console name="Console" target="SYSTEM_OUT"> + <PatternLayout pattern="%d{ISO8601} +%r [%t] %-5p %c %x - %m%n"/> + </Console> + <Console name="ColorConsole" target="SYSTEM_OUT"> + <PatternLayout pattern="%style{%d{ISO8601} +%r}{yellow} %highlight{%-5level}{STYLE=Logback} %style{[%t]}{yellow} %style{%c{1.}}{BRIGHT} %message%n"/> + </Console> + </Appenders> + <Loggers> + <Root level="${sys:log4j.logLevel}"> + <AppenderRef ref="ColorConsole"/> + </Root> + </Loggers> +</Configuration> diff --git a/test/mocks/pnfsimulator/src/test/java/org/onap/pnfsimulator/SimulatorParamsProviderTest.java b/test/mocks/pnfsimulator/src/test/java/org/onap/pnfsimulator/SimulatorParamsProviderTest.java new file mode 100644 index 000000000..96397eae1 --- /dev/null +++ b/test/mocks/pnfsimulator/src/test/java/org/onap/pnfsimulator/SimulatorParamsProviderTest.java @@ -0,0 +1,31 @@ +package org.onap.pnfsimulator; + +import org.apache.commons.cli.ParseException; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.onap.pnfsimulator.cli.SimulatorParamsProvider; +import org.onap.pnfsimulator.cli.SimulatorParams; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Java6Assertions.assertThat; + +public class SimulatorParamsProviderTest { + + SimulatorParamsProvider parser; + + @BeforeEach + public void setUp() { + parser = new SimulatorParamsProvider(); + } + + @Test + public void whenParserReceiveArgLisWithTwoCorrectParametersShouldReturnCorrectStructOfParams() + throws ParseException { + String[] arg = new String[]{ + "-address", "http://localhost:808/eventListner/v5", + "-config", "config.json"}; + SimulatorParams params = parser.parse(arg); + assertThat(params.getConfigFilePath()).isEqualToIgnoringCase("config.json"); + assertThat(params.getVesAddress()).isEqualToIgnoringCase("http://localhost:808/eventListner/v5"); + } +}
\ No newline at end of file diff --git a/test/mocks/pnfsimulator/src/test/java/org/onap/pnfsimulator/SimulatorTest.java b/test/mocks/pnfsimulator/src/test/java/org/onap/pnfsimulator/SimulatorTest.java new file mode 100644 index 000000000..ccbb72846 --- /dev/null +++ b/test/mocks/pnfsimulator/src/test/java/org/onap/pnfsimulator/SimulatorTest.java @@ -0,0 +1,12 @@ +package org.onap.pnfsimulator; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; + +public class SimulatorTest { + + @Test + public void dummyTestToCheckEnvirometn() { + Assertions.assertThat(true).isTrue(); + } +}
\ No newline at end of file diff --git a/test/mocks/pnfsimulator/src/test/java/org/onap/pnfsimulator/message/MessageProviderTest.java b/test/mocks/pnfsimulator/src/test/java/org/onap/pnfsimulator/message/MessageProviderTest.java new file mode 100644 index 000000000..38540577e --- /dev/null +++ b/test/mocks/pnfsimulator/src/test/java/org/onap/pnfsimulator/message/MessageProviderTest.java @@ -0,0 +1,110 @@ +package org.onap.pnfsimulator.message; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +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_ID; +import static org.onap.pnfsimulator.message.MessageConstants.EVENT_TYPE; +import static org.onap.pnfsimulator.message.MessageConstants.INTERNAL_HEADER_FIELDS; +import static org.onap.pnfsimulator.message.MessageConstants.LAST_EPOCH_MICROSEC; +import static org.onap.pnfsimulator.message.MessageConstants.OTHER_FIELDS; +import static org.onap.pnfsimulator.message.MessageConstants.OTHER_FIELDS_VERSION; +import static org.onap.pnfsimulator.message.MessageConstants.PNF_LAST_SERVICE_DATE; +import static org.onap.pnfsimulator.message.MessageConstants.PNF_MANUFACTURE_DATE; +import static org.onap.pnfsimulator.message.MessageConstants.PRIORITY; +import static org.onap.pnfsimulator.message.MessageConstants.SEQUENCE; +import static org.onap.pnfsimulator.message.MessageConstants.START_EPOCH_MICROSEC; +import static org.onap.pnfsimulator.message.MessageConstants.VERSION; + +import java.util.UUID; +import org.json.JSONObject; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +public class MessageProviderTest { + + private static final String testParamsJson = + "{\"key1\": \"val1\",\"key2\": \"val2\",\"pnfKey3\": \"pnfVal3\",\"key4\": \"val4\"}"; + + private static MessageProvider messageProvider; + + @BeforeAll + public static void setup() { + messageProvider = MessageProvider.getInstance(); + } + + @Test + public void createMessage_should_throw_when_given_null_argument() { + assertThrows(IllegalArgumentException.class, + () -> messageProvider.createMessage(null), + "Params object cannot be null"); + } + + @Test + public void createMessage_should_create_constant_message_when_no_params_specified() { + JSONObject message = messageProvider.createMessage(new JSONObject()); + + JSONObject commonEventHeader = message.getJSONObject(COMMON_EVENT_HEADER); + JSONObject otherFields = message.getJSONObject(OTHER_FIELDS); + + JSONObject expectedCommonEventHeader = generateConstantCommonEventHeader(); + JSONObject expectedOtherFields = generateConstantOtherFields(); + + expectedCommonEventHeader + .toMap() + .forEach((key, val) -> assertTrue(commonEventHeader.has(key), + () -> String.format("Key %s is not present", key))); + + expectedOtherFields + .toMap() + .forEach((key, val) -> assertTrue(otherFields.has(key), + () -> String.format("Key %s is not present", key))); + } + + + @Test + public void createMessage_should_add_specified_params_to_valid_subobjects() { + JSONObject params = new JSONObject(testParamsJson); + JSONObject message = messageProvider.createMessage(params); + + JSONObject commonEventHeader = message.getJSONObject(COMMON_EVENT_HEADER); + JSONObject otherFields = message.getJSONObject(OTHER_FIELDS); + + assertEquals("pnfVal3", otherFields.getString("pnfKey3")); + assertEquals("val1", commonEventHeader.getString("key1")); + assertEquals("val2", commonEventHeader.getString("key2")); + assertEquals("val4", commonEventHeader.getString("key4")); + } + + + private JSONObject generateConstantCommonEventHeader() { + + JSONObject commonEventHeader = new JSONObject(); + long timestamp = System.currentTimeMillis(); + + commonEventHeader.put(DOMAIN, "other"); + commonEventHeader.put(EVENT_ID, UUID.randomUUID() + "-reg"); + commonEventHeader.put(EVENT_TYPE, "pnfRegistration"); + commonEventHeader.put(LAST_EPOCH_MICROSEC, timestamp); + commonEventHeader.put(PRIORITY, "Normal"); + commonEventHeader.put(SEQUENCE, 0); + commonEventHeader.put(START_EPOCH_MICROSEC, timestamp); + commonEventHeader.put(INTERNAL_HEADER_FIELDS, new JSONObject()); + commonEventHeader.put(VERSION, 3); + + return commonEventHeader; + } + + private JSONObject generateConstantOtherFields() { + JSONObject otherFields = new JSONObject(); + + otherFields.put(OTHER_FIELDS_VERSION, 1); + otherFields.put(PNF_LAST_SERVICE_DATE, 1517206400); + otherFields.put(PNF_MANUFACTURE_DATE, 1516406400); + + return otherFields; + } + +} |