Applying license changes to all files
[appc.git] / appc-adapters / appc-ssh-adapter / appc-ssh-adapter-tests / src / main / java / org / openecomp / appc / adapter / ssh / SshConnectionMock.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.appc.adapter.ssh;
26
27 import java.io.IOException;
28 import java.io.OutputStream;
29 import java.util.ArrayList;
30 import java.util.List;
31
32 import org.openecomp.appc.adapter.ssh.SshConnection;
33
34 public class SshConnectionMock implements SshConnection {
35
36         private static final int DEF_SUCCESS_STATUS = 0;
37
38         private String host;
39         private int port;
40         private String username;
41         private String password;
42         private long timeout;
43
44         private int returnStatus = DEF_SUCCESS_STATUS;
45         private String returnStdout;
46         private String returnStderr;
47
48         private int connectCallCount = 0;
49         private int disconnectCallCount = 0;
50         private List<String> executedCommands = new ArrayList<>();
51
52         public SshConnectionMock(String host, int port, String username, String password) {
53                 this.host = host;
54                 this.port = port;
55                 this.username = username;
56                 this.password = password;
57         }
58
59         @Override
60         public void connect() {
61                 connectCallCount++;
62         }
63
64         @Override
65         public void connectWithRetry() {
66                 connectCallCount++;
67         }
68
69         @Override
70         public void disconnect() {
71                 disconnectCallCount++;
72         }
73
74         @Override
75         public int execCommand(String cmd, OutputStream out, OutputStream err) {
76                 return execCommand(cmd, out, err, false);
77         }
78
79         @Override
80         public int execCommandWithPty(String cmd, OutputStream out) {
81                 return execCommand(cmd, out, out, true);
82         }
83
84         private int execCommand(String cmd, OutputStream out, OutputStream err, boolean usePty) {
85                 executedCommands.add(cmd);
86                 try {
87                         if((out != null) && (returnStdout != null)) {
88                                 out.write(returnStdout.getBytes());
89                         }
90                 } catch(IOException e) {
91                         throw new RuntimeException("Error writing to stdout output stream", e);
92                 }
93                 try {
94                         if((err != null) && (returnStderr != null)) {
95                                 err.write(returnStderr.getBytes());
96                         }
97                 } catch(IOException e) {
98                         throw new RuntimeException("Error writing to stderr output stream", e);
99                 }
100                 return returnStatus;
101         }
102
103         @Override
104         public void setExecTimeout(long timeout) {
105                 this.timeout = timeout;
106         }
107
108         public long getExecTimeout() {
109                 return timeout;
110         }
111
112         public String getHost() {
113                 return host;
114         }
115
116         public int getPort() {
117                 return port;
118         }
119
120         public String getUsername() {
121                 return username;
122         }
123
124         public String getPassword() {
125                 return password;
126         }
127
128         public int getConnectCallCount() {
129                 return connectCallCount;
130         }
131
132         public int getDisconnectCallCount() {
133                 return disconnectCallCount;
134         }
135
136         public List<String> getExecutedCommands() {
137                 return executedCommands;
138         }
139
140         public int getReturnStatus() {
141                 return returnStatus;
142         }
143
144         public void setReturnStatus(int returnStatus) {
145                 this.returnStatus = returnStatus;
146         }
147
148         public String getReturnStdout() {
149                 return returnStdout;
150         }
151
152         public void setReturnStdout(String returnStdout) {
153                 this.returnStdout = returnStdout;
154         }
155
156         public String getReturnStderr() {
157                 return returnStderr;
158         }
159
160         public void setReturnStderr(String returnStderr) {
161                 this.returnStderr = returnStderr;
162         }
163 }