7bf6b77d3a20cbc93a9e91152e8ab70f5cdd703f
[appc.git] / appc-config / appc-config-adaptor / provider / src / test / java / org / onap / appc / ccadaptor / SshJcraftWrapperTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Modifications Copyright (C) 2018 IBM.
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  *
23  * ============LICENSE_END=========================================================
24  */
25  
26 package org.onap.appc.ccadaptor;
27
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertNull;
30 import static org.junit.Assert.assertTrue;
31 import static org.junit.Assert.assertFalse;
32 import static org.junit.Assert.fail;
33 import static org.mockito.BDDMockito.given;
34 import static org.mockito.Matchers.any;
35 import static org.mockito.Matchers.anyInt;
36 import static org.mockito.Matchers.anyString;
37 import static org.mockito.Matchers.eq;
38 import static org.mockito.Mockito.atLeastOnce;
39 import static org.mockito.Mockito.doThrow;
40 import static org.mockito.Mockito.inOrder;
41 import static org.mockito.Mockito.never;
42 import static org.mockito.Mockito.spy;
43 import static org.mockito.Mockito.times;
44 import static org.mockito.Mockito.verify;
45 import static org.mockito.Mockito.verifyNoMoreInteractions;
46
47 import com.jcraft.jsch.ChannelSftp;
48 import com.jcraft.jsch.ChannelShell;
49 import com.jcraft.jsch.ChannelSubsystem;
50 import com.jcraft.jsch.JSch;
51 import com.jcraft.jsch.JSchException;
52 import com.jcraft.jsch.Session;
53 import com.jcraft.jsch.SftpException;
54 import com.jcraft.jsch.UserInfo;
55 import java.io.File;
56 import java.io.FileNotFoundException;
57 import java.io.IOException;
58 import java.io.InputStream;
59 import java.io.OutputStream;
60 import java.net.URL;
61
62 import org.apache.commons.lang.StringUtils;
63 import org.junit.Assert;
64 import org.junit.Before;
65 import org.junit.Ignore;
66 import org.junit.Test;
67 import org.junit.runner.RunWith;
68 import org.mockito.InOrder;
69 import org.mockito.Mock;
70 import org.mockito.runners.MockitoJUnitRunner;
71 import org.onap.appc.ccadaptor.SshJcraftWrapper.MyUserInfo;
72 import org.apache.commons.io.IOUtils;
73
74 @RunWith(MockitoJUnitRunner.class)
75 public class SshJcraftWrapperTest {
76     @Test
77     public void TestCheckIfReceivedStringMatchesDelimeter(){
78         SshJcraftWrapper wrapper = new SshJcraftWrapper();
79         wrapper.getTheDate();
80         boolean result = wrapper.checkIfReceivedStringMatchesDelimeter("#", "test#", "test#");
81         Assert.assertEquals(true, result);
82     }
83
84     @Test
85     public void testRemoveWhiteSpaceAndNewLineCharactersAroundString(){
86         SshJcraftWrapper wrapper = new SshJcraftWrapper();
87         String nameSpace = wrapper.removeWhiteSpaceAndNewLineCharactersAroundString("namespace ");
88         Assert.assertEquals("namespace", nameSpace);
89     }
90
91     @Test
92     public void testStripOffCmdFromRouterResponse(){
93         SshJcraftWrapper wrapper = new SshJcraftWrapper();
94         String result = wrapper.stripOffCmdFromRouterResponse("test\nsuccess");
95         Assert.assertEquals("success\n", result);
96     }
97
98     //@Test
99     public void testGetLastFewLinesOfFile() throws FileNotFoundException, IOException{
100         SshJcraftWrapper wrapper = new SshJcraftWrapper();
101         URL path = SshJcraftWrapperTest.class.getResource("Test");
102         File file = new File(path.getFile());
103         String value = wrapper.getLastFewLinesOfFile(file,1);
104         Assert.assertEquals("\nTest data 3", value);
105     }
106
107     @Test(expected=Exception.class)
108     public void testSetRouterCommandType() throws IOException{
109         SshJcraftWrapper wrapper = new SshJcraftWrapper();
110         wrapper.setRouterCommandType("test");
111         wrapper.receiveUntil("test", 2, "test");
112     }
113
114     @Test
115     public void testValues() throws IOException{
116         SshJcraftWrapper wrapper = new SshJcraftWrapper();
117         wrapper.setEquipNameCode("testcode");
118         wrapper.setRouterCommandType("testcommand");
119         String equipName =wrapper.getEquipNameCode();
120         wrapper.getHostName();
121         wrapper.getPassWord();
122         wrapper.getRouterName();
123         wrapper.getUserName();
124         wrapper.getTheDate();
125         Assert.assertEquals("testcode", equipName);
126     }
127
128     @Test(expected=Exception.class)
129     public void testSetRouterCommandType2() throws IOException{
130         SshJcraftWrapper wrapper = new SshJcraftWrapper();
131         wrapper.appendToRouterFile("test", 2);
132         StringBuffer buffer = new StringBuffer();
133         buffer.append("test");
134         wrapper.appendToRouterFile("Test.txt", buffer);
135         wrapper.receiveUntilBufferFlush(3, 4, "test");
136     }
137
138     @Test(expected=Exception.class)
139     public void testSetRouterCommandType3() throws IOException{
140         SshJcraftWrapper wrapper = new SshJcraftWrapper();
141         wrapper.checkIfReceivedStringMatchesDelimeter(3, "test");
142     }
143     
144     @Test(expected=IOException.class)
145     public void testConnect() throws IOException{
146         SshJcraftWrapper wrapper = new SshJcraftWrapper();
147         wrapper.connect("testHost", "testUser", "testPswd", "3000", 1000);
148     }
149     
150     @Test
151     public void testMyUserInfoGetPassword() {
152         MyUserInfo myUserInfo=new MyUserInfo();
153         assertNull(myUserInfo.getPassword());
154     }
155     
156     @Test
157     public void testMyUserInfoPromptYesNo() {
158         MyUserInfo myUserInfo=new MyUserInfo();
159         assertFalse(myUserInfo.promptYesNo(""));
160     }
161 }