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