Bug 4998: Intermittent VNMappingException in NIC BoD flow
[nemo.git] / nemo-tools / sandbox / src / test / java / org / opendaylight / nemo / tool / sandbox / CmdExecutorTest.java
1 /*
2  * Copyright (c) 2016 Huawei, Inc. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.nemo.tool.sandbox;
9
10 import junit.framework.TestCase;
11 import org.junit.Assert;
12 import org.junit.Before;
13 import org.junit.Test;
14
15 import java.awt.color.CMMException;
16
17 import static org.junit.Assert.*;
18 import ch.ethz.ssh2.Connection;
19 import ch.ethz.ssh2.Session;
20 import ch.ethz.ssh2.StreamGobbler;
21 import org.junit.runner.RunWith;
22 import org.opendaylight.nemo.tool.sandbox.utils.Config;
23 import org.opendaylight.nemo.tool.sandbox.utils.FileUtils;
24 import org.powermock.api.mockito.PowerMockito;
25 import org.powermock.api.support.membermodification.MemberMatcher;
26 import org.powermock.api.support.membermodification.MemberModifier;
27 import org.powermock.core.classloader.annotations.PrepareForTest;
28 import org.powermock.modules.junit4.PowerMockRunner;
29 import java.io.BufferedReader;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.InputStreamReader;
33 import java.lang.reflect.Field;
34 import java.lang.reflect.Method;
35
36 import static org.mockito.Mockito.*;
37 /**
38  * Created by zhangmeng on 2016/1/14.
39  */
40 @RunWith(PowerMockRunner.class)
41 @PrepareForTest(ch.ethz.ssh2.Connection.class)
42 public class CmdExecutorTest extends TestCase {
43     private CmdExecutor cmdExecutor;
44     private Class<CmdExecutor> class1;
45     private Field field_sshConnection;
46     @Before
47     public void setUp() throws Exception {
48         class1 = CmdExecutor.class;
49         field_sshConnection = class1.getDeclaredField("sshConnection");
50         field_sshConnection.setAccessible(true);
51
52         cmdExecutor = new CmdExecutor();
53     }
54
55     @Test
56     public void testOpen() throws Exception {
57         String command = "cd ~";
58         Session session = mock(Session.class);
59         Connection sshConnection = mock(Connection.class);
60
61         Assert.assertTrue(CmdExecutor.open() == false);
62         PowerMockito.whenNew(Connection.class).withArguments(any(String.class)).thenReturn(sshConnection);
63         when(sshConnection.authenticateWithPassword(any(String.class), any(String.class))).thenReturn(true);
64         Assert.assertTrue(CmdExecutor.open() == false);
65 //        Assert.assertTrue(field_sshConnection.get(class1) == sshConnection);
66 //        verify(sshConnection).authenticateWithPassword(any(String.class), any(String.class));
67
68         field_sshConnection.set(class1, sshConnection);
69         when(sshConnection.openSession()).thenReturn(session);
70         doNothing().when(session).execCommand(command);
71         when(session.getStdout()).thenReturn(mock(InputStream.class));
72         Assert.assertTrue(CmdExecutor.sshExecute(command) != null);
73         verify(session).getStdout();
74         CmdExecutor.queryInterfaceMac("eth0");
75         CmdExecutor.queryInterfaceMac("1","eth0");
76
77         CmdExecutor.close();
78     }
79
80     @Test
81     public void testgetMacFromResult() throws Exception {
82         Method method = class1.getDeclaredMethod("getMacFromResult",new Class[]{
83                 String.class
84         });
85         method.setAccessible(true);
86         Assert.assertTrue(method.invoke(class1,"HWaddr") != null);
87     }
88
89 }