2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Amdocs
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.appc.dg.ssh.impl;
25 import com.fasterxml.jackson.core.JsonProcessingException;
26 import com.fasterxml.jackson.databind.ObjectMapper;
27 import org.hamcrest.CoreMatchers;
28 import org.junit.Assert;
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.rules.ExpectedException;
32 import org.openecomp.appc.adapter.ssh.SshAdapterMock;
33 import org.openecomp.appc.adapter.ssh.SshConnectionDetails;
34 import org.openecomp.appc.adapter.ssh.SshConnectionMock;
35 import org.openecomp.appc.dg.ssh.SshService;
36 import org.openecomp.appc.dg.ssh.impl.SshServiceImpl;
37 import org.openecomp.appc.exceptions.APPCException;
38 import org.openecomp.sdnc.sli.SvcLogicContext;
40 import java.util.HashMap;
41 import java.util.List;
43 import java.util.Properties;
45 public class SshServiceImplTest {
47 private static final ObjectMapper mapper = new ObjectMapper();
50 public ExpectedException thrown = ExpectedException.none();
53 public void testExec() throws APPCException, JsonProcessingException {
54 String host = "testhost";
55 String username = "testuser";
56 String password = "testpassword";
57 String command = "cat keystonerc_Test";
59 SshServiceImpl sshService = new SshServiceImpl();
60 SshAdapterMock sshAdapterMock = new SshAdapterMock();
61 sshService.setSshAdapter(sshAdapterMock);
63 System.out.println("=> Executing SSH command [" + command + "]...");
65 Map<String, String> params = new HashMap<>();
66 params.put(SshService.PARAM_IN_connection_details, createConnectionDetails(host,username,password));
67 params.put(SshService.PARAM_IN_command, command);
68 SvcLogicContext svcLogicContext = new SvcLogicContext(new Properties());
69 sshService.exec(params, svcLogicContext);
70 int status = Integer.parseInt(svcLogicContext.getAttribute(SshService.PARAM_OUT_status));
71 String stdout = svcLogicContext.getAttribute(SshService.PARAM_OUT_stdout);
72 String stderr = svcLogicContext.getAttribute(SshService.PARAM_OUT_stderr);
73 System.out.println("=> SSH command [" + command + "] status is [" + status + "]. stdout is [" + stdout + "]. stderr is [" + stderr + "]");
75 List<SshConnectionMock> connectionMocks = sshAdapterMock.getConnectionMocks();
76 Assert.assertEquals(1, connectionMocks.size());
77 SshConnectionMock connectionMock = connectionMocks.get(0);
78 Assert.assertNotNull(connectionMock);
79 Assert.assertEquals(host, connectionMock.getHost());
80 Assert.assertEquals(SshService.DEF_port, connectionMock.getPort());
81 Assert.assertEquals(username, connectionMock.getUsername());
82 Assert.assertEquals(password, connectionMock.getPassword());
83 Assert.assertEquals(1, connectionMock.getConnectCallCount());
84 Assert.assertEquals(1, connectionMock.getDisconnectCallCount());
85 List<String> executedCommands = connectionMock.getExecutedCommands();
86 Assert.assertEquals(1, executedCommands.size());
87 String executedCommand = executedCommands.get(0);
88 Assert.assertEquals(command, executedCommand);
92 public void testExecWithStatusCheck() throws APPCException, JsonProcessingException {
93 String host = "testhost";
94 String username = "testuser";
95 String password = "testpassword";
96 String command = "cat keystonerc_Test";
98 SshServiceImpl sshService = new SshServiceImpl();
99 SshAdapterMock sshAdapterMock = new SshAdapterMock();
100 sshService.setSshAdapter(sshAdapterMock);
102 System.out.println("=> Executing SSH command [" + command + "]...");
103 Map<String, String> params = new HashMap<>();
104 params.put(SshService.PARAM_IN_connection_details, createConnectionDetails(host,username,password));
105 params.put(SshService.PARAM_IN_command, command);
106 SvcLogicContext svcLogicContext = new SvcLogicContext(new Properties());
107 sshService.execWithStatusCheck(params, svcLogicContext);
108 int status = Integer.parseInt(svcLogicContext.getAttribute(SshService.PARAM_OUT_status));
109 String stdout = svcLogicContext.getAttribute(SshService.PARAM_OUT_stdout);
110 String stderr = svcLogicContext.getAttribute(SshService.PARAM_OUT_stderr);
111 System.out.println("=> SSH command [" + command + "] status is [" + status + "]. stdout is [" + stdout + "]. stderr is [" + stderr + "]");
113 List<SshConnectionMock> connectionMocks = sshAdapterMock.getConnectionMocks();
114 Assert.assertEquals(1, connectionMocks.size());
115 SshConnectionMock connectionMock = connectionMocks.get(0);
116 Assert.assertNotNull(connectionMock);
117 Assert.assertEquals(host, connectionMock.getHost());
118 Assert.assertEquals(SshService.DEF_port, connectionMock.getPort());
119 Assert.assertEquals(username, connectionMock.getUsername());
120 Assert.assertEquals(password, connectionMock.getPassword());
121 Assert.assertEquals(1, connectionMock.getConnectCallCount());
122 Assert.assertEquals(1, connectionMock.getDisconnectCallCount());
123 List<String> executedCommands = connectionMock.getExecutedCommands();
124 Assert.assertEquals(1, executedCommands.size());
125 String executedCommand = executedCommands.get(0);
126 Assert.assertEquals(command, executedCommand);
130 * Checks that execWithStatusCheck() throws appropriate exception if execution status != 0.
132 * @throws APPCException
133 * @throws JsonProcessingException
136 public void testExecWithStatusCheckFail() throws APPCException, JsonProcessingException {
137 String host = "testhost";
138 String username = "testuser";
139 String password = "testpassword";
140 String command = "cat keystonerc_Test";
142 int expectedStatus = 2;
143 String expectedErr = "Test failure";
145 SshServiceImpl sshService = new SshServiceImpl();
146 SshAdapterMock sshAdapterMock = new SshAdapterMock();
147 sshAdapterMock.setReturnStatus(expectedStatus);
148 sshAdapterMock.setReturnStderr(expectedErr);
149 sshService.setSshAdapter(sshAdapterMock);
151 thrown.expect(APPCException.class);
152 thrown.expectMessage(CoreMatchers.containsString(expectedErr));
154 System.out.println("=> Executing SSH command [" + command + "]...");
155 Map<String, String> params = new HashMap<>();
156 params.put(SshService.PARAM_IN_connection_details, createConnectionDetails(host,username,password));
157 params.put(SshService.PARAM_IN_command, command);
158 SvcLogicContext svcLogicContext = new SvcLogicContext(new Properties());
159 // should fail, no need to perform further assertions
160 sshService.execWithStatusCheck(params, svcLogicContext);
163 private String createConnectionDetails(String host, String username, String password) throws JsonProcessingException {
164 SshConnectionDetails connDetails = new SshConnectionDetails();
165 connDetails.setHost(host);
166 connDetails.setUsername(username);
167 connDetails.setPassword(password);
168 return mapper.writeValueAsString(connDetails);