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 disconnect() {
63 disconnectCallCount++;
67 public int execCommand(String cmd, OutputStream out, OutputStream err) {
68 return execCommand(cmd, out, err, false);
72 public int execCommandWithPty(String cmd, OutputStream out) {
73 return execCommand(cmd, out, out, true);
76 private int execCommand(String cmd, OutputStream out, OutputStream err, boolean usePty) {
77 executedCommands.add(cmd);
79 if((out != null) && (returnStdout != null)) {
80 out.write(returnStdout.getBytes());
82 } catch(IOException e) {
83 throw new RuntimeException("Error writing to stdout output stream", e);
86 if((err != null) && (returnStderr != null)) {
87 err.write(returnStderr.getBytes());
89 } catch(IOException e) {
90 throw new RuntimeException("Error writing to stderr output stream", e);
96 public void setExecTimeout(long timeout) {
97 this.timeout = timeout;
100 public long getExecTimeout() {
104 public String getHost() {
108 public int getPort() {
112 public String getUsername() {
116 public String getPassword() {
120 public int getConnectCallCount() {
121 return connectCallCount;
124 public int getDisconnectCallCount() {
125 return disconnectCallCount;
128 public List<String> getExecutedCommands() {
129 return executedCommands;
132 public int getReturnStatus() {
136 public void setReturnStatus(int returnStatus) {
137 this.returnStatus = returnStatus;
140 public String getReturnStdout() {
144 public void setReturnStdout(String returnStdout) {
145 this.returnStdout = returnStdout;
148 public String getReturnStderr() {
152 public void setReturnStderr(String returnStderr) {
153 this.returnStderr = returnStderr;