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