2 * ============LICENSE_START=======================================================
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
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.appc.dg.ssh.impl;
27 import com.fasterxml.jackson.core.JsonProcessingException;
28 import com.fasterxml.jackson.databind.ObjectMapper;
29 import org.hamcrest.CoreMatchers;
30 import org.junit.Assert;
31 import org.junit.Rule;
32 import org.junit.Test;
33 import org.junit.rules.ExpectedException;
34 import org.openecomp.appc.adapter.ssh.SshAdapterMock;
35 import org.openecomp.appc.adapter.ssh.SshConnectionDetails;
36 import org.openecomp.appc.adapter.ssh.SshConnectionMock;
37 import org.openecomp.appc.dg.ssh.SshService;
38 import org.openecomp.appc.dg.ssh.impl.SshServiceImpl;
39 import org.openecomp.appc.exceptions.APPCException;
40 import org.openecomp.sdnc.sli.SvcLogicContext;
42 import java.util.HashMap;
43 import java.util.List;
45 import java.util.Properties;
47 public class SshServiceImplTest {
49 private static final ObjectMapper mapper = new ObjectMapper();
52 public ExpectedException thrown = ExpectedException.none();
55 public void testExec() throws APPCException, JsonProcessingException {
56 String host = "testhost";
57 String username = "testuser";
58 String password = "testpassword";
59 String command = "cat keystonerc_Test";
61 SshServiceImpl sshService = new SshServiceImpl();
62 SshAdapterMock sshAdapterMock = new SshAdapterMock();
63 sshService.setSshAdapter(sshAdapterMock);
65 System.out.println("=> Executing SSH command [" + command + "]...");
67 Map<String, String> params = new HashMap<>();
68 params.put(SshService.PARAM_IN_connection_details, createConnectionDetails(host,username,password));
69 params.put(SshService.PARAM_IN_command, command);
70 SvcLogicContext svcLogicContext = new SvcLogicContext(new Properties());
71 sshService.exec(params, svcLogicContext);
72 int status = Integer.parseInt(svcLogicContext.getAttribute(SshService.PARAM_OUT_status));
73 String stdout = svcLogicContext.getAttribute(SshService.PARAM_OUT_stdout);
74 String stderr = svcLogicContext.getAttribute(SshService.PARAM_OUT_stderr);
75 System.out.println("=> SSH command [" + command + "] status is [" + status + "]. stdout is [" + stdout + "]. stderr is [" + stderr + "]");
77 List<SshConnectionMock> connectionMocks = sshAdapterMock.getConnectionMocks();
78 Assert.assertEquals(1, connectionMocks.size());
79 SshConnectionMock connectionMock = connectionMocks.get(0);
80 Assert.assertNotNull(connectionMock);
81 Assert.assertEquals(host, connectionMock.getHost());
82 Assert.assertEquals(SshService.DEF_port, connectionMock.getPort());
83 Assert.assertEquals(username, connectionMock.getUsername());
84 Assert.assertEquals(password, connectionMock.getPassword());
85 Assert.assertEquals(1, connectionMock.getConnectCallCount());
86 Assert.assertEquals(1, connectionMock.getDisconnectCallCount());
87 List<String> executedCommands = connectionMock.getExecutedCommands();
88 Assert.assertEquals(1, executedCommands.size());
89 String executedCommand = executedCommands.get(0);
90 Assert.assertEquals(command, executedCommand);
94 public void testExecWithStatusCheck() throws APPCException, JsonProcessingException {
95 String host = "testhost";
96 String username = "testuser";
97 String password = "testpassword";
98 String command = "cat keystonerc_Test";
100 SshServiceImpl sshService = new SshServiceImpl();
101 SshAdapterMock sshAdapterMock = new SshAdapterMock();
102 sshService.setSshAdapter(sshAdapterMock);
104 System.out.println("=> Executing SSH command [" + command + "]...");
105 Map<String, String> params = new HashMap<>();
106 params.put(SshService.PARAM_IN_connection_details, createConnectionDetails(host,username,password));
107 params.put(SshService.PARAM_IN_command, command);
108 SvcLogicContext svcLogicContext = new SvcLogicContext(new Properties());
109 sshService.execWithStatusCheck(params, svcLogicContext);
110 int status = Integer.parseInt(svcLogicContext.getAttribute(SshService.PARAM_OUT_status));
111 String stdout = svcLogicContext.getAttribute(SshService.PARAM_OUT_stdout);
112 String stderr = svcLogicContext.getAttribute(SshService.PARAM_OUT_stderr);
113 System.out.println("=> SSH command [" + command + "] status is [" + status + "]. stdout is [" + stdout + "]. stderr is [" + stderr + "]");
115 List<SshConnectionMock> connectionMocks = sshAdapterMock.getConnectionMocks();
116 Assert.assertEquals(1, connectionMocks.size());
117 SshConnectionMock connectionMock = connectionMocks.get(0);
118 Assert.assertNotNull(connectionMock);
119 Assert.assertEquals(host, connectionMock.getHost());
120 Assert.assertEquals(SshService.DEF_port, connectionMock.getPort());
121 Assert.assertEquals(username, connectionMock.getUsername());
122 Assert.assertEquals(password, connectionMock.getPassword());
123 Assert.assertEquals(1, connectionMock.getConnectCallCount());
124 Assert.assertEquals(1, connectionMock.getDisconnectCallCount());
125 List<String> executedCommands = connectionMock.getExecutedCommands();
126 Assert.assertEquals(1, executedCommands.size());
127 String executedCommand = executedCommands.get(0);
128 Assert.assertEquals(command, executedCommand);
132 * Checks that execWithStatusCheck() throws appropriate exception if execution status != 0.
134 * @throws APPCException
135 * @throws JsonProcessingException
138 public void testExecWithStatusCheckFail() throws APPCException, JsonProcessingException {
139 String host = "testhost";
140 String username = "testuser";
141 String password = "testpassword";
142 String command = "cat keystonerc_Test";
144 int expectedStatus = 2;
145 String expectedErr = "Test failure";
147 SshServiceImpl sshService = new SshServiceImpl();
148 SshAdapterMock sshAdapterMock = new SshAdapterMock();
149 sshAdapterMock.setReturnStatus(expectedStatus);
150 sshAdapterMock.setReturnStderr(expectedErr);
151 sshService.setSshAdapter(sshAdapterMock);
153 thrown.expect(APPCException.class);
154 thrown.expectMessage(CoreMatchers.containsString(expectedErr));
156 System.out.println("=> Executing SSH command [" + command + "]...");
157 Map<String, String> params = new HashMap<>();
158 params.put(SshService.PARAM_IN_connection_details, createConnectionDetails(host,username,password));
159 params.put(SshService.PARAM_IN_command, command);
160 SvcLogicContext svcLogicContext = new SvcLogicContext(new Properties());
161 // should fail, no need to perform further assertions
162 sshService.execWithStatusCheck(params, svcLogicContext);
165 private String createConnectionDetails(String host, String username, String password) throws JsonProcessingException {
166 SshConnectionDetails connDetails = new SshConnectionDetails();
167 connDetails.setHost(host);
168 connDetails.setUsername(username);
169 connDetails.setPassword(password);
170 return mapper.writeValueAsString(connDetails);