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 public class SshConnectionMock implements SshConnection {
33 private static final int DEF_SUCCESS_STATUS = 0;
35 private final String host;
36 private final int port;
37 private final String username;
38 private final String password;
39 private final String keyFile;
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, String keyFile) {
53 this.username = username;
54 this.password = password;
55 this.keyFile = keyFile;
59 public void connect() {
64 public void connectWithRetry() {
69 public void disconnect() {
70 disconnectCallCount++;
74 public int execCommand(String cmd, OutputStream out, OutputStream err) {
75 return execCommand(cmd, out, err, false);
79 public int execCommandWithPty(String cmd, OutputStream out) {
80 return execCommand(cmd, out, out, true);
83 private int execCommand(String cmd, OutputStream out, OutputStream err, boolean usePty) {
84 executedCommands.add(cmd);
86 if((out != null) && (returnStdout != null)) {
87 out.write(returnStdout.getBytes());
89 } catch(IOException e) {
90 throw new RuntimeException("Error writing to stdout output stream", e);
93 if((err != null) && (returnStderr != null)) {
94 err.write(returnStderr.getBytes());
96 } catch(IOException e) {
97 throw new RuntimeException("Error writing to stderr output stream", e);
103 public void setExecTimeout(long timeout) {
104 this.timeout = timeout;
107 public long getExecTimeout() {
111 public String getHost() {
115 public int getPort() {
119 public String getUsername() {
123 public String getPassword() {
127 public String getKeyFile() {
131 public int getConnectCallCount() {
132 return connectCallCount;
135 public int getDisconnectCallCount() {
136 return disconnectCallCount;
139 public List<String> getExecutedCommands() {
140 return executedCommands;
143 public int getReturnStatus() {
147 public void setReturnStatus(int returnStatus) {
148 this.returnStatus = returnStatus;
151 public String getReturnStdout() {
155 public void setReturnStdout(String returnStdout) {
156 this.returnStdout = returnStdout;
159 public String getReturnStderr() {
163 public void setReturnStderr(String returnStderr) {
164 this.returnStderr = returnStderr;