b61fc136f9733e7497f32d6878750801cc9c53f4
[appc.git] /
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  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.adapter.ssh;
25
26 import java.io.IOException;
27 import java.io.OutputStream;
28 import java.util.ArrayList;
29 import java.util.List;
30
31 import org.onap.appc.adapter.ssh.SshConnection;
32
33 public class SshConnectionMock implements SshConnection {
34
35         private static final int DEF_SUCCESS_STATUS = 0;
36
37         private final String host;
38         private final int port;
39         private final String username;
40         private final String password;
41         private final String keyFile;
42         private long timeout;
43
44         private int returnStatus = DEF_SUCCESS_STATUS;
45         private String returnStdout;
46         private String returnStderr;
47
48         private int connectCallCount = 0;
49         private int disconnectCallCount = 0;
50         private List<String> executedCommands = new ArrayList<>();
51
52         public SshConnectionMock(String host, int port, String username, String password, String keyFile) {
53                 this.host = host;
54                 this.port = port;
55                 this.username = username;
56                 this.password = password;
57                 this.keyFile = keyFile;
58         }
59
60         @Override
61         public void connect() {
62                 connectCallCount++;
63         }
64
65         @Override
66         public void connectWithRetry() {
67                 connectCallCount++;
68         }
69
70         @Override
71         public void disconnect() {
72                 disconnectCallCount++;
73         }
74
75         @Override
76         public int execCommand(String cmd, OutputStream out, OutputStream err) {
77                 return execCommand(cmd, out, err, false);
78         }
79
80         @Override
81         public int execCommandWithPty(String cmd, OutputStream out) {
82                 return execCommand(cmd, out, out, true);
83         }
84
85         private int execCommand(String cmd, OutputStream out, OutputStream err, boolean usePty) {
86                 executedCommands.add(cmd);
87                 try {
88                         if((out != null) && (returnStdout != null)) {
89                                 out.write(returnStdout.getBytes());
90                         }
91                 } catch(IOException e) {
92                         throw new RuntimeException("Error writing to stdout output stream", e);
93                 }
94                 try {
95                         if((err != null) && (returnStderr != null)) {
96                                 err.write(returnStderr.getBytes());
97                         }
98                 } catch(IOException e) {
99                         throw new RuntimeException("Error writing to stderr output stream", e);
100                 }
101                 return returnStatus;
102         }
103
104         @Override
105         public void setExecTimeout(long timeout) {
106                 this.timeout = timeout;
107         }
108
109         public long getExecTimeout() {
110                 return timeout;
111         }
112
113         public String getHost() {
114                 return host;
115         }
116
117         public int getPort() {
118                 return port;
119         }
120
121         public String getUsername() {
122                 return username;
123         }
124
125         public String getPassword() {
126                 return password;
127         }
128
129         public String getKeyFile() {
130                 return keyFile;
131         }
132
133         public int getConnectCallCount() {
134                 return connectCallCount;
135         }
136
137         public int getDisconnectCallCount() {
138                 return disconnectCallCount;
139         }
140
141         public List<String> getExecutedCommands() {
142                 return executedCommands;
143         }
144
145         public int getReturnStatus() {
146                 return returnStatus;
147         }
148
149         public void setReturnStatus(int returnStatus) {
150                 this.returnStatus = returnStatus;
151         }
152
153         public String getReturnStdout() {
154                 return returnStdout;
155         }
156
157         public void setReturnStdout(String returnStdout) {
158                 this.returnStdout = returnStdout;
159         }
160
161         public String getReturnStderr() {
162                 return returnStderr;
163         }
164
165         public void setReturnStderr(String returnStderr) {
166                 this.returnStderr = returnStderr;
167         }
168 }