2  * ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2019-2021 Nordix Foundation.
 
   4  *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
 
   5  * ================================================================================
 
   6  * Licensed under the Apache License, Version 2.0 (the "License");
 
   7  * you may not use this file except in compliance with the License.
 
   8  * You may obtain a copy of the License at
 
  10  *      http://www.apache.org/licenses/LICENSE-2.0
 
  12  * Unless required by applicable law or agreed to in writing, software
 
  13  * distributed under the License is distributed on an "AS IS" BASIS,
 
  14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  15  * See the License for the specific language governing permissions and
 
  16  * limitations under the License.
 
  18  * SPDX-License-Identifier: Apache-2.0
 
  19  * ============LICENSE_END=========================================================
 
  22 package org.onap.policy.models.sim.pdp.parameters;
 
  24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
 
  25 import static org.junit.Assert.assertEquals;
 
  26 import static org.junit.Assert.assertTrue;
 
  27 import static org.junit.Assert.fail;
 
  29 import java.io.FileNotFoundException;
 
  30 import org.junit.Test;
 
  31 import org.onap.policy.common.utils.cmd.CommandLineException;
 
  32 import org.onap.policy.common.utils.coder.CoderException;
 
  33 import org.onap.policy.models.sim.pdp.PdpSimulatorCommandLineArguments;
 
  34 import org.onap.policy.models.sim.pdp.exception.PdpSimulatorException;
 
  37  * Class to perform unit test of {@link PdpSimulatorParameterHandler}.
 
  39  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
 
  41 public class TestPdpSimulatorParameterHandler {
 
  44     public void testParameterHandlerNoParameterFile() throws PdpSimulatorException, CommandLineException {
 
  45         final String[] emptyArgumentString = { "-c", "src/test/resources/NoParametersFile.json" };
 
  47         final PdpSimulatorCommandLineArguments emptyArguments = new PdpSimulatorCommandLineArguments();
 
  48         emptyArguments.parse(emptyArgumentString);
 
  51             new PdpSimulatorParameterHandler().getParameters(emptyArguments);
 
  52             fail("test should throw an exception here");
 
  53         } catch (final Exception e) {
 
  54             assertTrue(e.getCause() instanceof CoderException);
 
  55             assertTrue(e.getCause().getCause() instanceof FileNotFoundException);
 
  60     public void testParameterHandlerEmptyParameters() throws PdpSimulatorException, CommandLineException {
 
  61         final String[] noArgumentString = { "-c", "src/test/resources/NoParameters.json" };
 
  63         final PdpSimulatorCommandLineArguments noArguments = new PdpSimulatorCommandLineArguments();
 
  64         noArguments.parse(noArgumentString);
 
  66         assertThatThrownBy(() -> new PdpSimulatorParameterHandler().getParameters(noArguments))
 
  67                         .hasMessageContaining("no parameters found");
 
  71     public void testParameterHandlerInvalidParameters() throws PdpSimulatorException, CommandLineException {
 
  72         final String[] invalidArgumentString = { "-c", "src/test/resources/InvalidParameters.json" };
 
  74         final PdpSimulatorCommandLineArguments invalidArguments = new PdpSimulatorCommandLineArguments();
 
  75         invalidArguments.parse(invalidArgumentString);
 
  77         assertThatThrownBy(() -> new PdpSimulatorParameterHandler().getParameters(invalidArguments))
 
  78                         .hasMessageStartingWith("error reading parameters from")
 
  79                         .hasCauseInstanceOf(CoderException.class);
 
  83     public void testParameterHandlerNoParameters() throws PdpSimulatorException, CommandLineException {
 
  84         final String[] noArgumentString = { "-c", "src/test/resources/EmptyConfigParameters.json" };
 
  86         final PdpSimulatorCommandLineArguments noArguments = new PdpSimulatorCommandLineArguments();
 
  87         noArguments.parse(noArgumentString);
 
  89         assertThatThrownBy(() -> new PdpSimulatorParameterHandler().getParameters(noArguments))
 
  90                         .hasMessageContaining("is null");
 
  94     public void testPdpSimulatorParameterGroup() throws PdpSimulatorException, CommandLineException {
 
  95         final String[] pdpSimulatorConfigParameters = { "-c", "src/test/resources/PdpSimulatorConfigParameters.json" };
 
  97         final PdpSimulatorCommandLineArguments arguments = new PdpSimulatorCommandLineArguments();
 
  98         arguments.parse(pdpSimulatorConfigParameters);
 
 100         final PdpSimulatorParameterGroup parGroup = new PdpSimulatorParameterHandler().getParameters(arguments);
 
 101         assertTrue(arguments.checkSetConfigurationFilePath());
 
 102         assertEquals(CommonTestData.PDP_SIMULATOR_GROUP_NAME, parGroup.getName());
 
 106     public void testPdpSimulatorParameterGroup_InvalidName() throws PdpSimulatorException, CommandLineException {
 
 107         final String[] pdpSimulatorConfigParameters = {"-c",
 
 108             "src/test/resources/PdpSimulatorConfigParameters_InvalidName.json"};
 
 110         final PdpSimulatorCommandLineArguments arguments = new PdpSimulatorCommandLineArguments();
 
 111         arguments.parse(pdpSimulatorConfigParameters);
 
 113         assertThatThrownBy(() -> new PdpSimulatorParameterHandler().getParameters(arguments)).hasMessageContaining(
 
 114                         "field \"name\" type \"java.lang.String\" value \" \" INVALID, must be a non-blank string");
 
 118     public void testPdpSimulatorVersion() throws PdpSimulatorException, CommandLineException {
 
 119         final String[] pdpSimulatorConfigParameters = { "-v" };
 
 120         final PdpSimulatorCommandLineArguments arguments = new PdpSimulatorCommandLineArguments();
 
 121         final String version = arguments.parse(pdpSimulatorConfigParameters);
 
 122         assertTrue(version.startsWith("ONAP Policy-PDP simulator Service"));
 
 126     public void testPdpSimulatorHelp() throws PdpSimulatorException, CommandLineException {
 
 127         final String[] pdpSimulatorConfigParameters = { "-h" };
 
 128         final PdpSimulatorCommandLineArguments arguments = new PdpSimulatorCommandLineArguments();
 
 129         final String help = arguments.parse(pdpSimulatorConfigParameters);
 
 130         assertTrue(help.startsWith("usage:"));
 
 134     public void testPdpSimulatorInvalidOption() {
 
 135         final String[] pdpSimulatorConfigParameters = { "-d" };
 
 136         final PdpSimulatorCommandLineArguments arguments = new PdpSimulatorCommandLineArguments();
 
 138         assertThatThrownBy(() -> arguments.parse(pdpSimulatorConfigParameters))
 
 139                         .hasMessageStartingWith("invalid command line arguments specified");
 
 143     public void testPdpSimulatorProperty() throws PdpSimulatorException, CommandLineException {
 
 144         final String[] pdpSimulatorConfigParameters = { "-p", "dummyProperties.json" };
 
 145         final PdpSimulatorCommandLineArguments arguments = new PdpSimulatorCommandLineArguments();
 
 146         arguments.parse(pdpSimulatorConfigParameters);
 
 147         assertTrue(arguments.checkSetPropertyFilePath());