2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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=========================================================
22 package org.openecomp.appc.adapter.ssh;
24 import java.io.IOException;
25 import java.io.OutputStream;
26 import java.util.ArrayList;
27 import java.util.List;
29 import org.openecomp.appc.adapter.ssh.SshConnection;
31 public class SshConnectionMock implements SshConnection {
33 private static final int DEF_SUCCESS_STATUS = 0;
37 private String username;
38 private String password;
41 private int returnStatus = DEF_SUCCESS_STATUS;
42 private String returnStdout;
43 private String returnStderr;
45 private int connectCallCount = 0;
46 private int disconnectCallCount = 0;
47 private List<String> executedCommands = new ArrayList<>();
49 public SshConnectionMock(String host, int port, String username, String password) {
52 this.username = username;
53 this.password = password;
57 public void connect() {
62 public void connectWithRetry() {
67 public void disconnect() {
68 disconnectCallCount++;
72 public int execCommand(String cmd, OutputStream out, OutputStream err) {
73 return execCommand(cmd, out, err, false);
77 public int execCommandWithPty(String cmd, OutputStream out) {
78 return execCommand(cmd, out, out, true);
81 private int execCommand(String cmd, OutputStream out, OutputStream err, boolean usePty) {
82 executedCommands.add(cmd);
84 if((out != null) && (returnStdout != null)) {
85 out.write(returnStdout.getBytes());
87 } catch(IOException e) {
88 throw new RuntimeException("Error writing to stdout output stream", e);
91 if((err != null) && (returnStderr != null)) {
92 err.write(returnStderr.getBytes());
94 } catch(IOException e) {
95 throw new RuntimeException("Error writing to stderr output stream", e);
101 public void setExecTimeout(long timeout) {
102 this.timeout = timeout;
105 public long getExecTimeout() {
109 public String getHost() {
113 public int getPort() {
117 public String getUsername() {
121 public String getPassword() {
125 public int getConnectCallCount() {
126 return connectCallCount;
129 public int getDisconnectCallCount() {
130 return disconnectCallCount;
133 public List<String> getExecutedCommands() {
134 return executedCommands;
137 public int getReturnStatus() {
141 public void setReturnStatus(int returnStatus) {
142 this.returnStatus = returnStatus;
145 public String getReturnStdout() {
149 public void setReturnStdout(String returnStdout) {
150 this.returnStdout = returnStdout;
153 public String getReturnStderr() {
157 public void setReturnStderr(String returnStderr) {
158 this.returnStderr = returnStderr;