2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.appc.adapter.ssh;
27 import java.io.IOException;
28 import java.io.OutputStream;
29 import java.util.ArrayList;
30 import java.util.List;
32 import org.openecomp.appc.adapter.ssh.SshConnection;
34 public class SshConnectionMock implements SshConnection {
36 private static final int DEF_SUCCESS_STATUS = 0;
40 private String username;
41 private String password;
44 private int returnStatus = DEF_SUCCESS_STATUS;
45 private String returnStdout;
46 private String returnStderr;
48 private int connectCallCount = 0;
49 private int disconnectCallCount = 0;
50 private List<String> executedCommands = new ArrayList<>();
52 public SshConnectionMock(String host, int port, String username, String password) {
55 this.username = username;
56 this.password = password;
60 public void connect() {
65 public void connectWithRetry() {
70 public void disconnect() {
71 disconnectCallCount++;
75 public int execCommand(String cmd, OutputStream out, OutputStream err) {
76 return execCommand(cmd, out, err, false);
80 public int execCommandWithPty(String cmd, OutputStream out) {
81 return execCommand(cmd, out, out, true);
84 private int execCommand(String cmd, OutputStream out, OutputStream err, boolean usePty) {
85 executedCommands.add(cmd);
87 if((out != null) && (returnStdout != null)) {
88 out.write(returnStdout.getBytes());
90 } catch(IOException e) {
91 throw new RuntimeException("Error writing to stdout output stream", e);
94 if((err != null) && (returnStderr != null)) {
95 err.write(returnStderr.getBytes());
97 } catch(IOException e) {
98 throw new RuntimeException("Error writing to stderr output stream", e);
104 public void setExecTimeout(long timeout) {
105 this.timeout = timeout;
108 public long getExecTimeout() {
112 public String getHost() {
116 public int getPort() {
120 public String getUsername() {
124 public String getPassword() {
128 public int getConnectCallCount() {
129 return connectCallCount;
132 public int getDisconnectCallCount() {
133 return disconnectCallCount;
136 public List<String> getExecutedCommands() {
137 return executedCommands;
140 public int getReturnStatus() {
144 public void setReturnStatus(int returnStatus) {
145 this.returnStatus = returnStatus;
148 public String getReturnStdout() {
152 public void setReturnStdout(String returnStdout) {
153 this.returnStdout = returnStdout;
156 public String getReturnStderr() {
160 public void setReturnStderr(String returnStderr) {
161 this.returnStderr = returnStderr;