af55dba2c47dfa14f173f74367c5cbbe25f5b163
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Amdocs
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23 package org.openecomp.appc.adapter.ssh;
24
25 import java.io.IOException;
26 import java.io.OutputStream;
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import org.openecomp.appc.adapter.ssh.SshConnection;
31
32 public class SshConnectionMock implements SshConnection {
33
34         private static final int DEF_SUCCESS_STATUS = 0;
35
36         private String host;
37         private int port;
38         private String username;
39         private String password;
40         private long timeout;
41
42         private int returnStatus = DEF_SUCCESS_STATUS;
43         private String returnStdout;
44         private String returnStderr;
45
46         private int connectCallCount = 0;
47         private int disconnectCallCount = 0;
48         private List<String> executedCommands = new ArrayList<>();
49
50         public SshConnectionMock(String host, int port, String username, String password) {
51                 this.host = host;
52                 this.port = port;
53                 this.username = username;
54                 this.password = password;
55         }
56
57         @Override
58         public void connect() {
59                 connectCallCount++;
60         }
61
62         @Override
63         public void connectWithRetry() {
64                 connectCallCount++;
65         }
66
67         @Override
68         public void disconnect() {
69                 disconnectCallCount++;
70         }
71
72         @Override
73         public int execCommand(String cmd, OutputStream out, OutputStream err) {
74                 return execCommand(cmd, out, err, false);
75         }
76
77         @Override
78         public int execCommandWithPty(String cmd, OutputStream out) {
79                 return execCommand(cmd, out, out, true);
80         }
81
82         private int execCommand(String cmd, OutputStream out, OutputStream err, boolean usePty) {
83                 executedCommands.add(cmd);
84                 try {
85                         if((out != null) && (returnStdout != null)) {
86                                 out.write(returnStdout.getBytes());
87                         }
88                 } catch(IOException e) {
89                         throw new RuntimeException("Error writing to stdout output stream", e);
90                 }
91                 try {
92                         if((err != null) && (returnStderr != null)) {
93                                 err.write(returnStderr.getBytes());
94                         }
95                 } catch(IOException e) {
96                         throw new RuntimeException("Error writing to stderr output stream", e);
97                 }
98                 return returnStatus;
99         }
100
101         @Override
102         public void setExecTimeout(long timeout) {
103                 this.timeout = timeout;
104         }
105
106         public long getExecTimeout() {
107                 return timeout;
108         }
109
110         public String getHost() {
111                 return host;
112         }
113
114         public int getPort() {
115                 return port;
116         }
117
118         public String getUsername() {
119                 return username;
120         }
121
122         public String getPassword() {
123                 return password;
124         }
125
126         public int getConnectCallCount() {
127                 return connectCallCount;
128         }
129
130         public int getDisconnectCallCount() {
131                 return disconnectCallCount;
132         }
133
134         public List<String> getExecutedCommands() {
135                 return executedCommands;
136         }
137
138         public int getReturnStatus() {
139                 return returnStatus;
140         }
141
142         public void setReturnStatus(int returnStatus) {
143                 this.returnStatus = returnStatus;
144         }
145
146         public String getReturnStdout() {
147                 return returnStdout;
148         }
149
150         public void setReturnStdout(String returnStdout) {
151                 this.returnStdout = returnStdout;
152         }
153
154         public String getReturnStderr() {
155                 return returnStderr;
156         }
157
158         public void setReturnStderr(String returnStderr) {
159                 this.returnStderr = returnStderr;
160         }
161 }