|
18 | 18 | // |
19 | 19 | package org.apache.cloudstack.utils.redfish; |
20 | 20 |
|
21 | | -import java.io.IOException; |
22 | | - |
23 | | -import org.apache.cloudstack.utils.redfish.RedfishClient.RedfishPowerState; |
24 | | -import org.apache.cloudstack.utils.redfish.RedfishClient.RedfishResetCmd; |
| 21 | +import org.apache.commons.httpclient.HttpStatus; |
| 22 | +import org.apache.http.StatusLine; |
| 23 | +import org.apache.http.client.methods.CloseableHttpResponse; |
25 | 24 | import org.junit.Assert; |
26 | 25 | import org.junit.Test; |
27 | 26 | import org.junit.runner.RunWith; |
| 27 | +import org.mockito.Mockito; |
28 | 28 | import org.mockito.junit.MockitoJUnitRunner; |
29 | 29 |
|
30 | 30 | @RunWith(MockitoJUnitRunner.class) |
31 | 31 | public class RedfishClientTest { |
32 | 32 |
|
33 | | - private static final String REDFISH_HOST_ADDRESS = "oob-n05.2019.cldin.net"; |
34 | | - private static final String USERNAME = "root"; |
35 | | - private static final String PASSWORD = "calvin"; |
36 | | - RedfishClient redfishClientIgnoreSSL = new RedfishClient(USERNAME, PASSWORD, true, false); |
37 | | - RedfishClient redfishClientUseSSL = new RedfishClient(USERNAME, PASSWORD, true, true); |
| 33 | + private static final String USERNAME = "user"; |
| 34 | + private static final String PASSWORD = "password"; |
| 35 | + private static final String oobAddress = "oob.host.address"; |
| 36 | + private static final String systemID = "SystemID.1"; |
| 37 | + private final static String COMPUTER_SYSTEM_RESET_URL_PATH = "/Actions/ComputerSystem.Reset"; |
38 | 38 |
|
39 | | -// @Test |
40 | | - public void getSystemIdTest() throws IOException { |
41 | | - String systemId = redfishClientIgnoreSSL.getSystemId(REDFISH_HOST_ADDRESS); |
42 | | - System.out.println(systemId); |
43 | | - } |
| 39 | +// RedfishClient redfishClientIgnoreSSL = new RedfishClient(USERNAME, PASSWORD, true, false); |
| 40 | + RedfishClient redfishClientUseSSL = new RedfishClient(USERNAME, PASSWORD, true, true); |
| 41 | + RedfishClient redfishClientspy = Mockito.spy(new RedfishClient(USERNAME, PASSWORD, true, true)); |
44 | 42 |
|
45 | | -// @Test |
46 | | - public void getSystemPowerStateTest() throws IOException { |
47 | | - RedfishPowerState power = redfishClientIgnoreSSL.getSystemPowerState(REDFISH_HOST_ADDRESS); |
48 | | - System.out.println(power); |
| 43 | + @Test(expected = RedfishException.class) |
| 44 | + public void validateAddressAndPrepareForUrlTestExpect() { |
| 45 | + redfishClientUseSSL.validateAddressAndPrepareForUrl("1:1:2:3:1"); |
| 46 | + redfishClientUseSSL.validateAddressAndPrepareForUrl("1"); |
| 47 | + redfishClientUseSSL.validateAddressAndPrepareForUrl("hostname"); |
| 48 | + redfishClientUseSSL.validateAddressAndPrepareForUrl(oobAddress); |
49 | 49 | } |
50 | 50 |
|
51 | | -// @Test |
52 | | - public void getSystemPowerStateTestUseSSL() throws IOException { |
53 | | - RedfishPowerState power = redfishClientUseSSL.getSystemPowerState(REDFISH_HOST_ADDRESS); |
54 | | - System.out.println(power); |
| 51 | + @Test |
| 52 | + public void validateAddressAndPrepareForUrlTestDomainName() { |
| 53 | + String result = redfishClientUseSSL.validateAddressAndPrepareForUrl(oobAddress); |
| 54 | + Assert.assertEquals(oobAddress, result); |
55 | 55 | } |
56 | 56 |
|
57 | | -// @Test |
58 | | - public void computerSystemResetTestForceOff() throws IOException { |
59 | | - redfishClientIgnoreSSL.executeComputerSystemReset(REDFISH_HOST_ADDRESS, RedfishResetCmd.ForceOff); |
| 57 | + @Test |
| 58 | + public void validateAddressAndPrepareForUrlTestIpv4() { |
| 59 | + String ipv4 = "192.168.0.123"; |
| 60 | + String result = redfishClientUseSSL.validateAddressAndPrepareForUrl(ipv4); |
| 61 | + Assert.assertEquals(ipv4, result); |
60 | 62 | } |
61 | 63 |
|
62 | | -// @Test |
63 | | - public void computerSystemResetTestForceGracefulShutdown() throws IOException { |
64 | | - redfishClientIgnoreSSL.executeComputerSystemReset(REDFISH_HOST_ADDRESS, RedfishResetCmd.GracefulShutdown); |
| 64 | + @Test |
| 65 | + public void validateAddressAndPrepareForUrlTestIpv6() { |
| 66 | + String ipv6 = "100::ffff:ffff:ffff:ffff"; |
| 67 | + String expected = "["+ipv6+"]"; |
| 68 | + String result = redfishClientUseSSL.validateAddressAndPrepareForUrl(ipv6); |
| 69 | + Assert.assertEquals(expected, result); |
65 | 70 | } |
66 | 71 |
|
67 | | -// @Test |
68 | | - public void computerSystemResetTestOn() throws IOException { |
69 | | - redfishClientIgnoreSSL.executeComputerSystemReset(REDFISH_HOST_ADDRESS, RedfishResetCmd.On); |
| 72 | + @Test |
| 73 | + public void buildRequestUrlTestHttpsGetSystemId() { |
| 74 | + RedfishClient redfishclient = new RedfishClient(USERNAME, PASSWORD, true, false); |
| 75 | + String result = redfishclient.buildRequestUrl(oobAddress, RedfishClient.RedfishCmdType.GetSystemId, systemID); |
| 76 | + String expected = String.format("https://%s/redfish/v1/Systems/",oobAddress,systemID); |
| 77 | + Assert.assertEquals(expected, result); |
70 | 78 | } |
71 | 79 |
|
72 | | -// @Test |
73 | | - public void computerSystemResetTestNmi() throws IOException { |
74 | | - redfishClientIgnoreSSL.executeComputerSystemReset(REDFISH_HOST_ADDRESS, RedfishResetCmd.Nmi); |
| 80 | + @Test |
| 81 | + public void buildRequestUrlTestGetSystemId() { |
| 82 | + RedfishClient redfishclient = new RedfishClient(USERNAME, PASSWORD, false, false); |
| 83 | + String result = redfishclient.buildRequestUrl(oobAddress, RedfishClient.RedfishCmdType.GetSystemId, systemID); |
| 84 | + String expected = String.format("http://%s/redfish/v1/Systems/",oobAddress,systemID); |
| 85 | + Assert.assertEquals(expected, result); |
75 | 86 | } |
76 | 87 |
|
77 | | - @Test(expected = RedfishException.class) |
78 | | - public void validateAddressAndPrepareForUrlTestExpect() { |
79 | | - redfishClientIgnoreSSL.validateAddressAndPrepareForUrl("1:1:2:3:1"); |
80 | | - redfishClientIgnoreSSL.validateAddressAndPrepareForUrl("1"); |
81 | | - redfishClientIgnoreSSL.validateAddressAndPrepareForUrl("hostname"); |
| 88 | + @Test |
| 89 | + public void buildRequestUrlTestHttpsComputerSystemReset() { |
| 90 | + RedfishClient redfishclient = new RedfishClient(USERNAME, PASSWORD, true, false); |
| 91 | + String result = redfishclient.buildRequestUrl(oobAddress, RedfishClient.RedfishCmdType.ComputerSystemReset, systemID); |
| 92 | + String expected = String.format("https://%s/redfish/v1/Systems/%s%s",oobAddress,systemID,COMPUTER_SYSTEM_RESET_URL_PATH); |
| 93 | + Assert.assertEquals(expected, result); |
82 | 94 | } |
83 | 95 |
|
84 | 96 | @Test |
85 | | - public void validateAddressAndPrepareForUrlTestDomainName() { |
86 | | - String domainName = "mydomain.org"; |
87 | | - String result = redfishClientIgnoreSSL.validateAddressAndPrepareForUrl(domainName); |
88 | | - Assert.assertEquals(domainName, result); |
| 97 | + public void buildRequestUrlTestComputerSystemReset() { |
| 98 | + RedfishClient redfishclient = new RedfishClient(USERNAME, PASSWORD, false, false); |
| 99 | + String result = redfishclient.buildRequestUrl(oobAddress, RedfishClient.RedfishCmdType.ComputerSystemReset, systemID); |
| 100 | + String expected = String.format("http://%s/redfish/v1/Systems/%s%s",oobAddress,systemID,COMPUTER_SYSTEM_RESET_URL_PATH); |
| 101 | + Assert.assertEquals(expected, result); |
89 | 102 | } |
90 | 103 |
|
91 | 104 | @Test |
92 | | - public void validateAddressAndPrepareForUrlTestIpv4() { |
93 | | - String ipv4 = "192.168.0.123"; |
94 | | - String result = redfishClientIgnoreSSL.validateAddressAndPrepareForUrl(ipv4); |
95 | | - Assert.assertEquals(ipv4, result); |
| 105 | + public void buildRequestUrlTestHttpsGetPowerState() { |
| 106 | + RedfishClient redfishclient = new RedfishClient(USERNAME, PASSWORD, true, false); |
| 107 | + String result = redfishclient.buildRequestUrl(oobAddress, RedfishClient.RedfishCmdType.GetPowerState, systemID); |
| 108 | + String expected = String.format("https://%s/redfish/v1/Systems/%s",oobAddress,systemID); |
| 109 | + Assert.assertEquals(expected, result); |
96 | 110 | } |
97 | 111 |
|
98 | 112 | @Test |
99 | | - public void validateAddressAndPrepareForUrlTestIpv6() { |
100 | | - String ipv6 = "100::ffff:ffff:ffff:ffff"; |
101 | | - String expected = "["+ipv6+"]"; |
102 | | - String result = redfishClientIgnoreSSL.validateAddressAndPrepareForUrl(ipv6); |
| 113 | + public void buildRequestUrlTestGetPowerState() { |
| 114 | + RedfishClient redfishclient = new RedfishClient(USERNAME, PASSWORD, false, false); |
| 115 | + String result = redfishclient.buildRequestUrl(oobAddress, RedfishClient.RedfishCmdType.GetPowerState, systemID); |
| 116 | + String expected = String.format("http://%s/redfish/v1/Systems/%s",oobAddress,systemID); |
103 | 117 | Assert.assertEquals(expected, result); |
104 | 118 | } |
105 | 119 |
|
| 120 | + // @Test |
| 121 | + public void getSystemPowerStateTest() { |
| 122 | +// Preparing the test |
| 123 | + Mockito.doReturn(systemID).when(redfishClientspy).getSystemId(Mockito.anyString()); |
| 124 | +// Mockito.when(redfishClientspy.getSystemId(Mockito.anyString())).thenReturn(systemID); |
| 125 | + |
| 126 | + StatusLine statusLine = Mockito.mock(StatusLine.class); |
| 127 | + Mockito.doReturn(HttpStatus.SC_OK).when(statusLine).getStatusCode(); |
| 128 | + |
| 129 | + CloseableHttpResponse response = Mockito.mock(CloseableHttpResponse.class); |
| 130 | + Mockito.doReturn(statusLine).when(response).getStatusLine(); |
| 131 | + |
| 132 | + Mockito.doReturn(response).when(redfishClientspy).executeGetRequest(Mockito.anyString()); |
| 133 | + |
| 134 | +// Runnint the tested method |
| 135 | + redfishClientspy.getSystemPowerState(oobAddress); |
| 136 | + |
| 137 | +// validating the test |
| 138 | + } |
| 139 | + |
106 | 140 | } |
0 commit comments