3194edcfbae60c41ea5fcee7a509645fe08a4010
[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 disconnect() {
63                 disconnectCallCount++;
64         }
65
66         @Override
67         public int execCommand(String cmd, OutputStream out, OutputStream err) {
68                 return execCommand(cmd, out, err, false);
69         }
70
71         @Override
72         public int execCommandWithPty(String cmd, OutputStream out) {
73                 return execCommand(cmd, out, out, true);
74         }
75
76         private int execCommand(String cmd, OutputStream out, OutputStream err, boolean usePty) {
77                 executedCommands.add(cmd);
78                 try {
79                         if((out != null) && (returnStdout != null)) {
80                                 out.write(returnStdout.getBytes());
81                         }
82                 } catch(IOException e) {
83                         throw new RuntimeException("Error writing to stdout output stream", e);
84                 }
85                 try {
86                         if((err != null) && (returnStderr != null)) {
87                                 err.write(returnStderr.getBytes());
88                         }
89                 } catch(IOException e) {
90                         throw new RuntimeException("Error writing to stderr output stream", e);
91                 }
92                 return returnStatus;
93         }
94
95         @Override
96         public void setExecTimeout(long timeout) {
97                 this.timeout = timeout;
98         }
99
100         public long getExecTimeout() {
101                 return timeout;
102         }
103
104         public String getHost() {
105                 return host;
106         }
107
108         public int getPort() {
109                 return port;
110         }
111
112         public String getUsername() {
113                 return username;
114         }
115
116         public String getPassword() {
117                 return password;
118         }
119
120         public int getConnectCallCount() {
121                 return connectCallCount;
122         }
123
124         public int getDisconnectCallCount() {
125                 return disconnectCallCount;
126         }
127
128         public List<String> getExecutedCommands() {
129                 return executedCommands;
130         }
131
132         public int getReturnStatus() {
133                 return returnStatus;
134         }
135
136         public void setReturnStatus(int returnStatus) {
137                 this.returnStatus = returnStatus;
138         }
139
140         public String getReturnStdout() {
141                 return returnStdout;
142         }
143
144         public void setReturnStdout(String returnStdout) {
145                 this.returnStdout = returnStdout;
146         }
147
148         public String getReturnStderr() {
149                 return returnStderr;
150         }
151
152         public void setReturnStderr(String returnStderr) {
153                 this.returnStderr = returnStderr;
154         }
155 }