Skip to content

Commit b6a754d

Browse files
rename ssl boolean and add a few test methods for RedfishClient
1 parent c9953b9 commit b6a754d

File tree

2 files changed

+96
-61
lines changed

2 files changed

+96
-61
lines changed

utils/src/main/java/org/apache/cloudstack/utils/redfish/RedfishClient.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public class RedfishClient {
6868
private String username;
6969
private String password;
7070
private boolean useHttps;
71-
private boolean useSSl;
71+
private boolean ignoreSsl;
7272

7373
private final static String SYSTEMS_URL_PATH = "redfish/v1/Systems/";
7474
private final static String COMPUTER_SYSTEM_RESET_URL_PATH = "/Actions/ComputerSystem.Reset";
@@ -85,7 +85,8 @@ public class RedfishClient {
8585
* <b>GetSystemId:</b> get the system ID. </br>
8686
* <b>GetPowerState:</b> used for get the system power state. </br>
8787
*/
88-
public enum RedfishCmdType {
88+
public enum
89+
RedfishCmdType {
8990
ComputerSystemReset, GetSystemId, GetPowerState
9091
}
9192

@@ -122,14 +123,14 @@ public enum RedfishResetCmd {
122123
ForceOff, ForceOn, ForceRestart, GracefulRestart, GracefulShutdown, Nmi, On, PowerCycle, PushPowerButton
123124
}
124125

125-
public RedfishClient(String username, String password, boolean useHttps, boolean ignoreSSL) {
126+
public RedfishClient(String username, String password, boolean useHttps, boolean ignoreSsl) {
126127
this.username = username;
127128
this.password = password;
128129
this.useHttps = useHttps;
129-
this.useSSl = ignoreSSL;
130+
this.ignoreSsl = ignoreSsl;
130131
}
131132

132-
private String buildRequestUrl(String hostAddress, RedfishCmdType cmd, String resourceId) {
133+
protected String buildRequestUrl(String hostAddress, RedfishCmdType cmd, String resourceId) {
133134
String urlHostAddress = validateAddressAndPrepareForUrl(hostAddress);
134135
String requestPath = getRequestPathForCommand(cmd, resourceId);
135136

@@ -143,7 +144,7 @@ private String buildRequestUrl(String hostAddress, RedfishCmdType cmd, String re
143144
/**
144145
* Executes a GET request for the given URL address.
145146
*/
146-
private HttpResponse executeGetRequest(String url) {
147+
protected HttpResponse executeGetRequest(String url) {
147148
try {
148149
URIBuilder builder = new URIBuilder(url);
149150
builder.setUserInfo(username, password);
@@ -152,10 +153,10 @@ private HttpResponse executeGetRequest(String url) {
152153
httpReq.addHeader(ACCEPT, APPLICATION_JSON);
153154

154155
HttpClient client;
155-
if (useSSl) {
156-
client = HttpClientBuilder.create().build();
157-
} else {
156+
if (ignoreSsl) {
158157
client = ignoreSSLCertValidator();
158+
} else {
159+
client = HttpClientBuilder.create().build();
159160
}
160161
return client.execute(httpReq);
161162
} catch (URISyntaxException | IOException | KeyManagementException | NoSuchAlgorithmException e) {
@@ -179,7 +180,7 @@ private HttpResponse executePostRequest(String url, JsonObject jsonToSend) {
179180
System.out.println("\nRequest: " + httpReq);
180181

181182
HttpClient client;
182-
if (useSSl) {
183+
if (ignoreSsl) {
183184
client = HttpClientBuilder.create().build();
184185
} else {
185186
client = ignoreSSLCertValidator();

utils/src/test/java/org/apache/cloudstack/utils/redfish/RedfishClientTest.java

Lines changed: 85 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -18,89 +18,123 @@
1818
//
1919
package org.apache.cloudstack.utils.redfish;
2020

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;
2524
import org.junit.Assert;
2625
import org.junit.Test;
2726
import org.junit.runner.RunWith;
27+
import org.mockito.Mockito;
2828
import org.mockito.junit.MockitoJUnitRunner;
2929

3030
@RunWith(MockitoJUnitRunner.class)
3131
public class RedfishClientTest {
3232

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";
3838

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));
4442

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);
4949
}
5050

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);
5555
}
5656

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);
6062
}
6163

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);
6570
}
6671

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);
7078
}
7179

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);
7586
}
7687

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);
8294
}
8395

8496
@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);
89102
}
90103

91104
@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);
96110
}
97111

98112
@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);
103117
Assert.assertEquals(expected, result);
104118
}
105119

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+
106140
}

0 commit comments

Comments
 (0)