From f2a0cfdc146805275c6f6c869fd75e5c3c342cbb Mon Sep 17 00:00:00 2001 From: RehanRaza Date: Tue, 9 Apr 2019 08:18:07 +0000 Subject: Fix unit-tests of mass-pnf-simulator Change-Id: Ie11e97862c8cbbdb269b8b5e6d4361340b98f1b2 Issue-ID: DCAEGEN2-1225 Signed-off-by: RehanRaza --- .../java/org/onap/pnfsimulator/FileProvider.java | 15 +++++------- .../org/onap/pnfsimulator/simulator/Simulator.java | 23 ++++++++++++++---- .../pnfsimulator/simulator/SimulatorFactory.java | 6 +++-- .../simulator/validation/NoRopFilesException.java | 28 ++++++++++++++++++++++ 4 files changed, 57 insertions(+), 15 deletions(-) create mode 100644 test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/main/java/org/onap/pnfsimulator/simulator/validation/NoRopFilesException.java (limited to 'test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/main/java/org/onap') diff --git a/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/main/java/org/onap/pnfsimulator/FileProvider.java b/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/main/java/org/onap/pnfsimulator/FileProvider.java index 9eb733227..beb564da8 100644 --- a/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/main/java/org/onap/pnfsimulator/FileProvider.java +++ b/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/main/java/org/onap/pnfsimulator/FileProvider.java @@ -4,12 +4,11 @@ import java.io.File; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import org.onap.pnfsimulator.simulator.validation.NoRopFilesException; public class FileProvider { - private FileProvider() {} - - public static List getFiles() { + public List getFiles() throws NoRopFilesException { List files = queryFiles(); @@ -22,17 +21,15 @@ public class FileProvider { return fileListSorted; } - private static List queryFiles() { + private static List queryFiles() throws NoRopFilesException { File folder = new File("./files/onap/"); File[] listOfFiles = folder.listFiles(); - List results = new ArrayList<>(); - - if (listOfFiles.length == 0) { - return results; - // TODO: this should be a thrown exception catched in the Simulator class + if (listOfFiles == null || listOfFiles.length == 0) { + throw new NoRopFilesException("No ROP files found in specified directory"); } + List results = new ArrayList<>(); for (int i = 0; i < listOfFiles.length; i++) { if (listOfFiles[i].isFile()) { results.add(listOfFiles[i].getName()); diff --git a/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/main/java/org/onap/pnfsimulator/simulator/Simulator.java b/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/main/java/org/onap/pnfsimulator/simulator/Simulator.java index 5c6405742..ba114760f 100644 --- a/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/main/java/org/onap/pnfsimulator/simulator/Simulator.java +++ b/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/main/java/org/onap/pnfsimulator/simulator/Simulator.java @@ -30,6 +30,7 @@ import org.onap.pnfsimulator.message.MessageProvider; import org.onap.pnfsimulator.simulator.client.HttpClientAdapter; import org.onap.pnfsimulator.simulator.client.HttpClientAdapterImpl; import org.onap.pnfsimulator.simulator.validation.JSONValidator; +import org.onap.pnfsimulator.simulator.validation.NoRopFilesException; import org.onap.pnfsimulator.simulator.validation.ValidationException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -54,6 +55,8 @@ public class Simulator extends Thread { private Optional notificationParams; private String xnfUrl; private static final String DEFAULT_OUTPUT_SCHEMA_PATH = "json_schema/output_validator_ves_schema_30.0.1.json"; + private FileProvider fileProvider; + private Exception thrownException = null; private Simulator() {} @@ -68,10 +71,10 @@ public class Simulator extends Thread { endTime = Instant.now().plus(duration); while (isEndless || runningTimeNotExceeded()) { try { - List fileList = FileProvider.getFiles(); + + List fileList = fileProvider.getFiles(); MessageProvider messageProvider = new MessageProvider(); JSONValidator validator = new JSONValidator(); - messageBody = messageProvider.createMessage(this.commonEventHeaderParams, this.pnfRegistrationParams, this.notificationParams, fileList, this.xnfUrl); validator.validate(messageBody.toString(), DEFAULT_OUTPUT_SCHEMA_PATH); @@ -79,8 +82,9 @@ public class Simulator extends Thread { LOGGER.info("Message to be sent:\n" + getMessage()); httpClient.send(messageBody.toString(), vesUrl); Thread.sleep(interval.toMillis()); - } catch (InterruptedException | ValidationException | ProcessingException | IOException e) { - LOGGER.info("Simulation stopped due to an exception"); + } catch (InterruptedException | ValidationException | ProcessingException | IOException | NoRopFilesException e) { + LOGGER.info("Simulation stopped due to an exception: " + e); + thrownException = e; return; } } @@ -109,6 +113,10 @@ public class Simulator extends Thread { return isEndless; } + public Exception getThrownException() { + return thrownException; + } + public long getRemainingTime() { return Duration.between(Instant.now(), endTime).getSeconds(); } @@ -124,6 +132,7 @@ public class Simulator extends Thread { private Optional pnfRegistrationParams; private JSONObject commonEventHeaderParams; private String xnfUrl; + private FileProvider fileProvider; private Builder() { this.vesUrl = ""; @@ -180,6 +189,11 @@ public class Simulator extends Thread { return this; } + public Builder withFileProvider(FileProvider fileProvider) { + this.fileProvider = fileProvider; + return this; + } + public Simulator build() { Simulator simulator = new Simulator(); simulator.vesUrl = this.vesUrl; @@ -188,6 +202,7 @@ public class Simulator extends Thread { simulator.duration = this.duration; simulator.interval = this.interval; simulator.xnfUrl = this.xnfUrl; + simulator.fileProvider = this.fileProvider; simulator.commonEventHeaderParams = this.commonEventHeaderParams; simulator.pnfRegistrationParams = this.pnfRegistrationParams; simulator.notificationParams = this.notificationParams; diff --git a/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/main/java/org/onap/pnfsimulator/simulator/SimulatorFactory.java b/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/main/java/org/onap/pnfsimulator/simulator/SimulatorFactory.java index a01c2e0c6..21717d863 100644 --- a/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/main/java/org/onap/pnfsimulator/simulator/SimulatorFactory.java +++ b/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/main/java/org/onap/pnfsimulator/simulator/SimulatorFactory.java @@ -24,6 +24,7 @@ import java.time.Duration; import java.util.Optional; import org.json.JSONObject; import org.onap.pnfsimulator.ConfigurationProvider; +import org.onap.pnfsimulator.FileProvider; import org.onap.pnfsimulator.PnfSimConfig; import org.springframework.stereotype.Service; @@ -46,7 +47,8 @@ public class SimulatorFactory { Duration interval = Duration.ofSeconds(parseInt(simulatorParams.getString(MESSAGE_INTERVAL))); return Simulator.builder().withVesUrl(urlVes).withXnfUrl(xnfUrl).withDuration(duration) - .withCommonEventHeaderParams(commonEventHeaderParams).withNotificationParams(notificationParams) - .withPnfRegistrationParams(pnfRegistrationParams).withInterval(interval).build(); + .withFileProvider(new FileProvider()).withCommonEventHeaderParams(commonEventHeaderParams) + .withNotificationParams(notificationParams).withPnfRegistrationParams(pnfRegistrationParams) + .withInterval(interval).build(); } } diff --git a/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/main/java/org/onap/pnfsimulator/simulator/validation/NoRopFilesException.java b/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/main/java/org/onap/pnfsimulator/simulator/validation/NoRopFilesException.java new file mode 100644 index 000000000..d3765a8c1 --- /dev/null +++ b/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/main/java/org/onap/pnfsimulator/simulator/validation/NoRopFilesException.java @@ -0,0 +1,28 @@ +/* + * ============LICENSE_START======================================================= + * PNF-REGISTRATION-HANDLER + * ================================================================================ + * Copyright (C) 2018 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.simulator.validation; + +public class NoRopFilesException extends Exception { + + public NoRopFilesException(String message) { + super(message); + } +} -- cgit 1.2.3-korg