2 * ============LICENSE_START=======================================================
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
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 * ============LICENSE_END=========================================================
24 package org.onap.appc.adapter.ssh;
26 import java.io.IOException;
27 import java.io.OutputStream;
28 import java.util.ArrayList;
29 import java.util.List;
31 import org.onap.appc.adapter.ssh.SshConnection;
33 public class SshConnectionMock implements SshConnection {
35 private static final int DEF_SUCCESS_STATUS = 0;
37 private final String host;
38 private final int port;
39 private final String username;
40 private final String password;
41 private final String keyFile;
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, String keyFile) {
55 this.username = username;
56 this.password = password;
57 this.keyFile = keyFile;
61 public void connect() {
66 public void connectWithRetry() {
71 public void disconnect() {
72 disconnectCallCount++;
76 public int execCommand(String cmd, OutputStream out, OutputStream err) {
77 return execCommand(cmd, out, err, false);
81 public int execCommandWithPty(String cmd, OutputStream out) {
82 return execCommand(cmd, out, out, true);
85 private int execCommand(String cmd, OutputStream out, OutputStream err, boolean usePty) {
86 executedCommands.add(cmd);
88 if((out != null) && (returnStdout != null)) {
89 out.write(returnStdout.getBytes());
91 } catch(IOException e) {
92 throw new RuntimeException("Error writing to stdout output stream", e);
95 if((err != null) && (returnStderr != null)) {
96 err.write(returnStderr.getBytes());
98 } catch(IOException e) {
99 throw new RuntimeException("Error writing to stderr output stream", e);
105 public void setExecTimeout(long timeout) {
106 this.timeout = timeout;
109 public long getExecTimeout() {
113 public String getHost() {
117 public int getPort() {
121 public String getUsername() {
125 public String getPassword() {
129 public String getKeyFile() {
133 public int getConnectCallCount() {
134 return connectCallCount;
137 public int getDisconnectCallCount() {
138 return disconnectCallCount;
141 public List<String> getExecutedCommands() {
142 return executedCommands;
145 public int getReturnStatus() {
149 public void setReturnStatus(int returnStatus) {
150 this.returnStatus = returnStatus;
153 public String getReturnStdout() {
157 public void setReturnStdout(String returnStdout) {
158 this.returnStdout = returnStdout;
161 public String getReturnStderr() {
165 public void setReturnStderr(String returnStderr) {
166 this.returnStderr = returnStderr;