2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 package org.openecomp.appc.adapter.ssh;
25 import java.io.IOException;
26 import java.io.OutputStream;
27 import java.util.ArrayList;
28 import java.util.List;
30 import org.openecomp.appc.adapter.ssh.SshConnection;
32 public class SshConnectionMock implements SshConnection {
34 private static final int DEF_SUCCESS_STATUS = 0;
38 private String username;
39 private String password;
42 private int returnStatus = DEF_SUCCESS_STATUS;
43 private String returnStdout;
44 private String returnStderr;
46 private int connectCallCount = 0;
47 private int disconnectCallCount = 0;
48 private List<String> executedCommands = new ArrayList<>();
50 public SshConnectionMock(String host, int port, String username, String password) {
53 this.username = username;
54 this.password = password;
58 public void connect() {
63 public void connectWithRetry() {
68 public void disconnect() {
69 disconnectCallCount++;
73 public int execCommand(String cmd, OutputStream out, OutputStream err) {
74 return execCommand(cmd, out, err, false);
78 public int execCommandWithPty(String cmd, OutputStream out) {
79 return execCommand(cmd, out, out, true);
82 private int execCommand(String cmd, OutputStream out, OutputStream err, boolean usePty) {
83 executedCommands.add(cmd);
85 if((out != null) && (returnStdout != null)) {
86 out.write(returnStdout.getBytes());
88 } catch(IOException e) {
89 throw new RuntimeException("Error writing to stdout output stream", e);
92 if((err != null) && (returnStderr != null)) {
93 err.write(returnStderr.getBytes());
95 } catch(IOException e) {
96 throw new RuntimeException("Error writing to stderr output stream", e);
102 public void setExecTimeout(long timeout) {
103 this.timeout = timeout;
106 public long getExecTimeout() {
110 public String getHost() {
114 public int getPort() {
118 public String getUsername() {
122 public String getPassword() {
126 public int getConnectCallCount() {
127 return connectCallCount;
130 public int getDisconnectCallCount() {
131 return disconnectCallCount;
134 public List<String> getExecutedCommands() {
135 return executedCommands;
138 public int getReturnStatus() {
142 public void setReturnStatus(int returnStatus) {
143 this.returnStatus = returnStatus;
146 public String getReturnStdout() {
150 public void setReturnStdout(String returnStdout) {
151 this.returnStdout = returnStdout;
154 public String getReturnStderr() {
158 public void setReturnStderr(String returnStderr) {
159 this.returnStderr = returnStderr;