020e272dd478e129f5b2f9ddb5444a013a42bb91
[policy/apex-pdp.git] / auth / cli-editor / src / test / java / org / onap / policy / apex / auth / clieditor / CommandLineCommandTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (c) 2020 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 package org.onap.policy.apex.auth.clieditor;
21
22 import static org.junit.Assert.*;
23
24 import java.util.List;
25
26 import org.junit.Before;
27 import org.junit.Test;
28
29 public class CommandLineCommandTest {
30
31     CommandLineCommand commandLineCommand = null;
32
33     @Before
34     public void initializeCommandLineCommand() {
35         commandLineCommand = new CommandLineCommand();
36     }
37
38     @Test
39     public void testCommandLine() {
40         commandLineCommand.setName("TestName");
41         commandLineCommand.setDescription("testDescription");
42         commandLineCommand.setSystemCommand(true);
43         assertTrue(commandLineCommand.isSystemCommand());
44         assertEquals("testDescription", commandLineCommand.getDescription());
45         assertEquals("TestName", commandLineCommand.getName());
46         assertEquals("CLICommand [name=TestName,keywordlist=[], argumentList=[], apiMethod=, systemCommand=true, description=testDescription]",commandLineCommand.toString());
47     }
48
49     @Test(expected = CommandLineException.class)
50     public void testInvalidApiClassName() {
51         commandLineCommand.getApiClassName();
52     }
53
54     @Test
55     public void testGetValidApiClassName() {
56         commandLineCommand.setApiMethod("Java.Get");
57         assertEquals("Java", commandLineCommand.getApiClassName());
58     }
59
60     @Test(expected = CommandLineException.class)
61     public void testInvalidApiMethodName() {
62         commandLineCommand.getApiMethodName();
63     }
64
65     @Test(expected = CommandLineException.class)
66     public void testInvalidApiMethod() {
67         commandLineCommand.setApiMethod("fail.");
68         assertEquals("fail.", commandLineCommand.getApiMethod());
69         commandLineCommand.getApiMethodName();
70     }
71
72     @Test
73     public void testValidApiMethodName() {
74         commandLineCommand.setApiMethod("Java.Get");
75         assertEquals("Get",commandLineCommand.getApiMethodName());
76     }
77
78     @Test
79     public void testGetHelp() {
80         List<String> keywordList = commandLineCommand.getKeywordlist();
81         List<CommandLineArgument> argumentList = commandLineCommand.getArgumentList();
82         assertEquals("{}: ",commandLineCommand.getHelp());
83         keywordList.add("TestKeyword");
84         argumentList.add(new CommandLineArgument("TestArgument"));
85         argumentList.add(null);
86         assertEquals("TestKeyword {}: \n" + 
87                 "       TestArgument: (M) ",commandLineCommand.getHelp());
88     }
89
90     @Test
91     public void testCompareTo() {
92         assertEquals(0,commandLineCommand.compareTo(commandLineCommand));
93         CommandLineCommand otherCommand = new CommandLineCommand();
94         otherCommand.setSystemCommand(true);
95         assertEquals(6,commandLineCommand.compareTo(otherCommand));
96         otherCommand.getArgumentList().add(new CommandLineArgument("testArgument"));
97         assertEquals(-609496833, commandLineCommand.compareTo(otherCommand));
98     }
99
100     @Test
101     public void testCompareKeywordList() {
102         CommandLineCommand otherCommand = new CommandLineCommand();
103         otherCommand.getKeywordlist().add("test");
104         assertEquals(-1, commandLineCommand.compareTo(otherCommand));
105         commandLineCommand.getKeywordlist().add("test");
106         assertEquals(0, commandLineCommand.compareTo(otherCommand));
107         commandLineCommand.getKeywordlist().add("test2");
108         assertEquals(1, commandLineCommand.compareTo(otherCommand));
109         otherCommand.getKeywordlist().add("test3");
110         assertEquals(-1, commandLineCommand.compareTo(otherCommand));
111     }
112
113     @Test
114     public void testHashCode() {
115         CommandLineCommand otherCommand = new CommandLineCommand();
116         assertEquals(commandLineCommand.hashCode(), otherCommand.hashCode());
117         commandLineCommand.getKeywordlist().add("Test");
118         otherCommand.setDescription(null);
119         otherCommand.setApiMethod(null);
120         otherCommand.setName(null);
121         assertNotEquals(commandLineCommand.hashCode(), otherCommand.hashCode());
122     }
123
124     @Test
125     public void testEquals() {
126         CommandLineCommand otherCommand = new CommandLineCommand();
127         assertFalse(commandLineCommand.equals(new Object()));
128         assertTrue(commandLineCommand.equals(commandLineCommand));
129         assertFalse(commandLineCommand.equals(null));
130         assertTrue(commandLineCommand.equals(otherCommand));
131         otherCommand.getKeywordlist().add("TestKeyword");
132         assertFalse(commandLineCommand.equals(otherCommand));
133     }
134 }