Changed to unmaintained
[appc.git] / appc-adapters / appc-ssh-adapter / appc-ssh-adapter-tests / src / main / java / org / onap / appc / adapter / ssh / SshConnectionMock.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  * 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 public class SshConnectionMock implements SshConnection {
32
33         private static final int DEF_SUCCESS_STATUS = 0;
34
35         private final String host;
36         private final int port;
37         private final String username;
38         private final String password;
39         private final String keyFile;
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, String keyFile) {
51                 this.host = host;
52                 this.port = port;
53                 this.username = username;
54                 this.password = password;
55                 this.keyFile = keyFile;
56         }
57
58         @Override
59         public void connect() {
60                 connectCallCount++;
61         }
62
63         @Override
64         public void connectWithRetry() {
65                 connectCallCount++;
66         }
67
68         @Override
69         public void disconnect() {
70                 disconnectCallCount++;
71         }
72
73         @Override
74         public int execCommand(String cmd, OutputStream out, OutputStream err) {
75                 return execCommand(cmd, out, err, false);
76         }
77
78         @Override
79         public int execCommandWithPty(String cmd, OutputStream out) {
80                 return execCommand(cmd, out, out, true);
81         }
82
83         private int execCommand(String cmd, OutputStream out, OutputStream err, boolean usePty) {
84                 executedCommands.add(cmd);
85                 try {
86                         if((out != null) && (returnStdout != null)) {
87                                 out.write(returnStdout.getBytes());
88                         }
89                 } catch(IOException e) {
90                         throw new RuntimeException("Error writing to stdout output stream", e);
91                 }
92                 try {
93                         if((err != null) && (returnStderr != null)) {
94                                 err.write(returnStderr.getBytes());
95                         }
96                 } catch(IOException e) {
97                         throw new RuntimeException("Error writing to stderr output stream", e);
98                 }
99                 return returnStatus;
100         }
101
102         @Override
103         public void setExecTimeout(long timeout) {
104                 this.timeout = timeout;
105         }
106
107         public long getExecTimeout() {
108                 return timeout;
109         }
110
111         public String getHost() {
112                 return host;
113         }
114
115         public int getPort() {
116                 return port;
117         }
118
119         public String getUsername() {
120                 return username;
121         }
122
123         public String getPassword() {
124                 return password;
125         }
126
127         public String getKeyFile() {
128                 return keyFile;
129         }
130
131         public int getConnectCallCount() {
132                 return connectCallCount;
133         }
134
135         public int getDisconnectCallCount() {
136                 return disconnectCallCount;
137         }
138
139         public List<String> getExecutedCommands() {
140                 return executedCommands;
141         }
142
143         public int getReturnStatus() {
144                 return returnStatus;
145         }
146
147         public void setReturnStatus(int returnStatus) {
148                 this.returnStatus = returnStatus;
149         }
150
151         public String getReturnStdout() {
152                 return returnStdout;
153         }
154
155         public void setReturnStdout(String returnStdout) {
156                 this.returnStdout = returnStdout;
157         }
158
159         public String getReturnStderr() {
160                 return returnStderr;
161         }
162
163         public void setReturnStderr(String returnStderr) {
164                 this.returnStderr = returnStderr;
165         }
166 }