7e72b85256b31b6dfe05776a2be9593172439608
[policy/distribution.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.distribution.main.startstop;
22
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.Assert.assertThrows;
25
26 import org.junit.Test;
27 import org.onap.policy.distribution.main.PolicyDistributionException;
28 import org.onap.policy.distribution.main.PolicyDistributionRuntimeException;
29
30 /**
31  * Class to perform unit test of DistributionCommandLineArguments.
32  *
33  * @author Adheli Tavares (adheli.tavares@est.tech)
34  *
35  */
36 public class TestDistributionCommandLineArguments {
37
38     @Test
39     public void testDistributionCommandLineArgumentsException() {
40         String[] wrongParams = {"arg1", "nothing", "{\"someJson\":1}"};
41         assertThrows(PolicyDistributionRuntimeException.class, () -> new DistributionCommandLineArguments(wrongParams));
42     }
43
44     @Test
45     public void testValidateFileNameEmpty() {
46         String[] argsOnlyKeyNoValue = {"-c", ""};
47         assertValidate(argsOnlyKeyNoValue, "policy distribution configuration file was not specified as an argument");
48     }
49
50     @Test
51     public void testValidateFileNameDoesNotExist() {
52         String[] fileNameNotExistentArgs = {"-c", "someFileName.json"};
53         assertValidate(fileNameNotExistentArgs,
54                 "policy distribution configuration file \"someFileName.json\" does not exist");
55     }
56
57     @Test
58     public void testValidateFileNameIsNotFile() {
59         String[] folderAsFileNameArgs = {"-c", "src/test/resources/parameters"};
60         assertValidate(folderAsFileNameArgs,
61                 "policy distribution configuration file \"src/test/resources/parameters\" is not a normal file");
62     }
63
64     protected void assertValidate(String[] testArgs, String expectedErrorMsg) {
65         DistributionCommandLineArguments sutArgs = new DistributionCommandLineArguments(testArgs);
66
67         assertThatThrownBy(() -> sutArgs.validate())
68             .isInstanceOf(PolicyDistributionException.class)
69             .hasMessage(expectedErrorMsg);
70     }
71 }