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