diff options
author | RehanRaza <muhammad.rehan.raza@est.tech> | 2019-04-09 08:18:07 +0000 |
---|---|---|
committer | RehanRaza <muhammad.rehan.raza@est.tech> | 2019-04-09 08:18:07 +0000 |
commit | f2a0cfdc146805275c6f6c869fd75e5c3c342cbb (patch) | |
tree | 18da1569342374f9ddfacfb985278b742c83150e /test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/main | |
parent | afb9786b012d43bef177e659e6f37c8017f2642c (diff) |
Fix unit-tests of mass-pnf-simulator
Change-Id: Ie11e97862c8cbbdb269b8b5e6d4361340b98f1b2
Issue-ID: DCAEGEN2-1225
Signed-off-by: RehanRaza <muhammad.rehan.raza@est.tech>
Diffstat (limited to 'test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/main')
4 files changed, 57 insertions, 15 deletions
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<String> getFiles() { + public List<String> getFiles() throws NoRopFilesException { List<String> files = queryFiles(); @@ -22,17 +21,15 @@ public class FileProvider { return fileListSorted; } - private static List<String> queryFiles() { + private static List<String> queryFiles() throws NoRopFilesException { File folder = new File("./files/onap/"); File[] listOfFiles = folder.listFiles(); - List<String> 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<String> 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<JSONObject> 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<String> fileList = FileProvider.getFiles(); + + List<String> 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<JSONObject> 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); + } +} |