Moving all files to root directory
[appc.git] / appc-dg / appc-dg-shared / appc-dg-ssh / src / test / java / org / openecomp / appc / dg / ssh / impl / SshServiceImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.appc.dg.ssh.impl;
23
24 import com.fasterxml.jackson.core.JsonProcessingException;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import org.hamcrest.CoreMatchers;
27 import org.junit.Assert;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.junit.rules.ExpectedException;
31 import org.openecomp.appc.adapter.ssh.SshAdapterMock;
32 import org.openecomp.appc.adapter.ssh.SshConnectionDetails;
33 import org.openecomp.appc.adapter.ssh.SshConnectionMock;
34 import org.openecomp.appc.dg.ssh.SshService;
35 import org.openecomp.appc.dg.ssh.impl.SshServiceImpl;
36 import org.openecomp.appc.exceptions.APPCException;
37 import org.openecomp.sdnc.sli.SvcLogicContext;
38
39 import java.util.HashMap;
40 import java.util.List;
41 import java.util.Map;
42 import java.util.Properties;
43
44 public class SshServiceImplTest {
45
46         private static final ObjectMapper mapper = new ObjectMapper();
47
48         @Rule
49         public ExpectedException thrown = ExpectedException.none();
50
51         @Test
52         public void testExec() throws APPCException, JsonProcessingException {
53                 String host = "testhost";
54                 String username = "testuser";
55                 String password = "testpassword";
56                 String command = "cat keystonerc_Test";
57
58                 SshServiceImpl sshService = new SshServiceImpl();
59                 SshAdapterMock sshAdapterMock = new SshAdapterMock();
60                 sshService.setSshAdapter(sshAdapterMock);
61
62                 System.out.println("=> Executing SSH command [" + command + "]...");
63
64                 Map<String, String> params = new HashMap<>();
65                 params.put(SshService.PARAM_IN_connection_details, createConnectionDetails(host,username,password));
66                 params.put(SshService.PARAM_IN_command, command);
67                 SvcLogicContext svcLogicContext = new SvcLogicContext(new Properties());
68                 sshService.exec(params, svcLogicContext);
69                 int status = Integer.parseInt(svcLogicContext.getAttribute(SshService.PARAM_OUT_status));
70                 String stdout = svcLogicContext.getAttribute(SshService.PARAM_OUT_stdout);
71                 String stderr = svcLogicContext.getAttribute(SshService.PARAM_OUT_stderr);
72                 System.out.println("=> SSH command [" + command + "] status is [" + status + "]. stdout is [" + stdout + "]. stderr is [" + stderr + "]");
73
74                 List<SshConnectionMock> connectionMocks = sshAdapterMock.getConnectionMocks();
75                 Assert.assertEquals(1, connectionMocks.size());
76                 SshConnectionMock connectionMock = connectionMocks.get(0);
77                 Assert.assertNotNull(connectionMock);
78                 Assert.assertEquals(host, connectionMock.getHost());
79                 Assert.assertEquals(SshService.DEF_port, connectionMock.getPort());
80                 Assert.assertEquals(username, connectionMock.getUsername());
81                 Assert.assertEquals(password, connectionMock.getPassword());
82                 Assert.assertEquals(1, connectionMock.getConnectCallCount());
83                 Assert.assertEquals(1, connectionMock.getDisconnectCallCount());
84                 List<String> executedCommands = connectionMock.getExecutedCommands();
85                 Assert.assertEquals(1, executedCommands.size());
86                 String executedCommand = executedCommands.get(0);
87                 Assert.assertEquals(command, executedCommand);
88         }
89
90         @Test
91         public void testExecWithStatusCheck() throws APPCException, JsonProcessingException {
92                 String host = "testhost";
93                 String username = "testuser";
94                 String password = "testpassword";
95                 String command = "cat keystonerc_Test";
96
97                 SshServiceImpl sshService = new SshServiceImpl();
98                 SshAdapterMock sshAdapterMock = new SshAdapterMock();
99                 sshService.setSshAdapter(sshAdapterMock);
100
101                 System.out.println("=> Executing SSH command [" + command + "]...");
102                 Map<String, String> params = new HashMap<>();
103                 params.put(SshService.PARAM_IN_connection_details, createConnectionDetails(host,username,password));
104                 params.put(SshService.PARAM_IN_command, command);
105                 SvcLogicContext svcLogicContext = new SvcLogicContext(new Properties());
106                 sshService.execWithStatusCheck(params, svcLogicContext);
107                 int status = Integer.parseInt(svcLogicContext.getAttribute(SshService.PARAM_OUT_status));
108                 String stdout = svcLogicContext.getAttribute(SshService.PARAM_OUT_stdout);
109                 String stderr = svcLogicContext.getAttribute(SshService.PARAM_OUT_stderr);
110                 System.out.println("=> SSH command [" + command + "] status is [" + status + "]. stdout is [" + stdout + "]. stderr is [" + stderr + "]");
111
112                 List<SshConnectionMock> connectionMocks = sshAdapterMock.getConnectionMocks();
113                 Assert.assertEquals(1, connectionMocks.size());
114                 SshConnectionMock connectionMock = connectionMocks.get(0);
115                 Assert.assertNotNull(connectionMock);
116                 Assert.assertEquals(host, connectionMock.getHost());
117                 Assert.assertEquals(SshService.DEF_port, connectionMock.getPort());
118                 Assert.assertEquals(username, connectionMock.getUsername());
119                 Assert.assertEquals(password, connectionMock.getPassword());
120                 Assert.assertEquals(1, connectionMock.getConnectCallCount());
121                 Assert.assertEquals(1, connectionMock.getDisconnectCallCount());
122                 List<String> executedCommands = connectionMock.getExecutedCommands();
123                 Assert.assertEquals(1, executedCommands.size());
124                 String executedCommand = executedCommands.get(0);
125                 Assert.assertEquals(command, executedCommand);
126         }
127
128         /**
129          * Checks that execWithStatusCheck() throws appropriate exception if execution status != 0.
130          *
131          * @throws APPCException
132          * @throws JsonProcessingException
133          */
134         @Test
135         public void testExecWithStatusCheckFail() throws APPCException, JsonProcessingException {
136                 String host = "testhost";
137                 String username = "testuser";
138                 String password = "testpassword";
139                 String command = "cat keystonerc_Test";
140
141                 int expectedStatus = 2;
142                 String expectedErr = "Test failure";
143
144                 SshServiceImpl sshService = new SshServiceImpl();
145                 SshAdapterMock sshAdapterMock = new SshAdapterMock();
146                 sshAdapterMock.setReturnStatus(expectedStatus);
147                 sshAdapterMock.setReturnStderr(expectedErr);
148                 sshService.setSshAdapter(sshAdapterMock);
149
150                 thrown.expect(APPCException.class);
151                 thrown.expectMessage(CoreMatchers.containsString(expectedErr));
152
153                 System.out.println("=> Executing SSH command [" + command + "]...");
154                 Map<String, String> params = new HashMap<>();
155                 params.put(SshService.PARAM_IN_connection_details, createConnectionDetails(host,username,password));
156                 params.put(SshService.PARAM_IN_command, command);
157                 SvcLogicContext svcLogicContext = new SvcLogicContext(new Properties());
158                 // should fail, no need to perform further assertions
159                 sshService.execWithStatusCheck(params, svcLogicContext);
160         }
161
162         private String createConnectionDetails(String host, String username, String password) throws JsonProcessingException {
163                 SshConnectionDetails connDetails = new SshConnectionDetails();
164                 connDetails.setHost(host);
165                 connDetails.setUsername(username);
166                 connDetails.setPassword(password);
167                 return mapper.writeValueAsString(connDetails);
168         }
169
170 }