|
19 | 19 | package org.apache.cloudstack.utils.redfish; |
20 | 20 |
|
21 | 21 | import org.apache.commons.httpclient.HttpStatus; |
| 22 | +import org.apache.http.HttpResponse; |
22 | 23 | import org.apache.http.StatusLine; |
| 24 | +import org.apache.http.client.HttpClient; |
23 | 25 | import org.apache.http.client.methods.CloseableHttpResponse; |
| 26 | +import org.apache.http.client.methods.HttpGet; |
| 27 | +import org.apache.http.client.methods.HttpRequestBase; |
24 | 28 | import org.junit.Assert; |
25 | 29 | import org.junit.Test; |
26 | 30 | import org.junit.runner.RunWith; |
| 31 | +import org.mockito.Mock; |
27 | 32 | import org.mockito.Mockito; |
28 | 33 | import org.mockito.junit.MockitoJUnitRunner; |
29 | 34 |
|
30 | | -@RunWith(MockitoJUnitRunner.class) public class RedfishClientTest { |
| 35 | +import java.io.IOException; |
| 36 | + |
| 37 | +@RunWith(MockitoJUnitRunner.class) |
| 38 | +public class RedfishClientTest { |
31 | 39 |
|
32 | 40 | private static final String USERNAME = "user"; |
33 | 41 | private static final String PASSWORD = "password"; |
34 | 42 | private static final String oobAddress = "oob.host.address"; |
35 | 43 | private static final String systemId = "SystemID.1"; |
36 | 44 | private final static String COMPUTER_SYSTEM_RESET_URL_PATH = "/Actions/ComputerSystem.Reset"; |
| 45 | + private final static Integer REDFISHT_REQUEST_RETRIES = Integer.valueOf(2); |
| 46 | + private static final String url = "https://address.system.net/redfish/v1/Systems/"; |
| 47 | + private static final HttpRequestBase httpReq = new HttpGet(url); |
| 48 | + |
| 49 | + @Mock |
| 50 | + HttpClient client; |
37 | 51 |
|
38 | | - RedfishClient redfishClientspy = Mockito.spy(new RedfishClient(USERNAME, PASSWORD, true, true)); |
| 52 | + @Mock |
| 53 | + HttpResponse httpResponse; |
| 54 | + |
| 55 | + RedfishClient redfishClientspy = Mockito.spy(new RedfishClient(USERNAME, PASSWORD, true, true, REDFISHT_REQUEST_RETRIES)); |
39 | 56 |
|
40 | 57 | @Test(expected = RedfishException.class) |
41 | 58 | public void validateAddressAndPrepareForUrlTestExpect() { |
@@ -68,47 +85,47 @@ public void validateAddressAndPrepareForUrlTestIpv6() { |
68 | 85 |
|
69 | 86 | @Test |
70 | 87 | public void buildRequestUrlTestHttpsGetSystemId() { |
71 | | - RedfishClient redfishclient = new RedfishClient(USERNAME, PASSWORD, true, false); |
| 88 | + RedfishClient redfishclient = new RedfishClient(USERNAME, PASSWORD, true, false, REDFISHT_REQUEST_RETRIES); |
72 | 89 | String result = redfishclient.buildRequestUrl(oobAddress, RedfishClient.RedfishCmdType.GetSystemId, systemId); |
73 | 90 | String expected = String.format("https://%s/redfish/v1/Systems/", oobAddress, systemId); |
74 | 91 | Assert.assertEquals(expected, result); |
75 | 92 | } |
76 | 93 |
|
77 | 94 | @Test |
78 | 95 | public void buildRequestUrlTestGetSystemId() { |
79 | | - RedfishClient redfishclient = new RedfishClient(USERNAME, PASSWORD, false, false); |
| 96 | + RedfishClient redfishclient = new RedfishClient(USERNAME, PASSWORD, false, false, REDFISHT_REQUEST_RETRIES); |
80 | 97 | String result = redfishclient.buildRequestUrl(oobAddress, RedfishClient.RedfishCmdType.GetSystemId, systemId); |
81 | 98 | String expected = String.format("http://%s/redfish/v1/Systems/", oobAddress, systemId); |
82 | 99 | Assert.assertEquals(expected, result); |
83 | 100 | } |
84 | 101 |
|
85 | 102 | @Test |
86 | 103 | public void buildRequestUrlTestHttpsComputerSystemReset() { |
87 | | - RedfishClient redfishclient = new RedfishClient(USERNAME, PASSWORD, true, false); |
| 104 | + RedfishClient redfishclient = new RedfishClient(USERNAME, PASSWORD, true, false, REDFISHT_REQUEST_RETRIES); |
88 | 105 | String result = redfishclient.buildRequestUrl(oobAddress, RedfishClient.RedfishCmdType.ComputerSystemReset, systemId); |
89 | 106 | String expected = String.format("https://%s/redfish/v1/Systems/%s%s", oobAddress, systemId, COMPUTER_SYSTEM_RESET_URL_PATH); |
90 | 107 | Assert.assertEquals(expected, result); |
91 | 108 | } |
92 | 109 |
|
93 | 110 | @Test |
94 | 111 | public void buildRequestUrlTestComputerSystemReset() { |
95 | | - RedfishClient redfishclient = new RedfishClient(USERNAME, PASSWORD, false, false); |
| 112 | + RedfishClient redfishclient = new RedfishClient(USERNAME, PASSWORD, false, false, REDFISHT_REQUEST_RETRIES); |
96 | 113 | String result = redfishclient.buildRequestUrl(oobAddress, RedfishClient.RedfishCmdType.ComputerSystemReset, systemId); |
97 | 114 | String expected = String.format("http://%s/redfish/v1/Systems/%s%s", oobAddress, systemId, COMPUTER_SYSTEM_RESET_URL_PATH); |
98 | 115 | Assert.assertEquals(expected, result); |
99 | 116 | } |
100 | 117 |
|
101 | 118 | @Test |
102 | 119 | public void buildRequestUrlTestHttpsGetPowerState() { |
103 | | - RedfishClient redfishclient = new RedfishClient(USERNAME, PASSWORD, true, false); |
| 120 | + RedfishClient redfishclient = new RedfishClient(USERNAME, PASSWORD, true, false, REDFISHT_REQUEST_RETRIES); |
104 | 121 | String result = redfishclient.buildRequestUrl(oobAddress, RedfishClient.RedfishCmdType.GetPowerState, systemId); |
105 | 122 | String expected = String.format("https://%s/redfish/v1/Systems/%s", oobAddress, systemId); |
106 | 123 | Assert.assertEquals(expected, result); |
107 | 124 | } |
108 | 125 |
|
109 | 126 | @Test |
110 | 127 | public void buildRequestUrlTestGetPowerState() { |
111 | | - RedfishClient redfishclient = new RedfishClient(USERNAME, PASSWORD, false, false); |
| 128 | + RedfishClient redfishclient = new RedfishClient(USERNAME, PASSWORD, false, false, REDFISHT_REQUEST_RETRIES); |
112 | 129 | String result = redfishclient.buildRequestUrl(oobAddress, RedfishClient.RedfishCmdType.GetPowerState, systemId); |
113 | 130 | String expected = String.format("http://%s/redfish/v1/Systems/%s", oobAddress, systemId); |
114 | 131 | Assert.assertEquals(expected, result); |
@@ -160,4 +177,34 @@ public void getSystemIdTestHttpStatusNotOk() { |
160 | 177 | redfishClientspy.getSystemId(oobAddress); |
161 | 178 | } |
162 | 179 |
|
| 180 | + @Test(expected = RedfishException.class) |
| 181 | + public void retryHttpRequestNoRetries() throws IOException { |
| 182 | + RedfishClient newRedfishClientspy = Mockito.spy(new RedfishClient(USERNAME, PASSWORD, true, true, Integer.valueOf(0))); |
| 183 | + newRedfishClientspy.retryHttpRequest(url, httpReq, client); |
| 184 | + |
| 185 | + Mockito.verify(newRedfishClientspy, Mockito.never()).retryHttpRequest(Mockito.anyString(), Mockito.any(), Mockito.any()); |
| 186 | + Mockito.verify(client, Mockito.never()).execute(Mockito.any()); |
| 187 | + } |
| 188 | + |
| 189 | + @Test(expected = RedfishException.class) |
| 190 | + public void retryHttpRequestExceptionAfterOneRetry() throws IOException { |
| 191 | + Mockito.when(client.execute(httpReq)).thenThrow(IOException.class).thenReturn(httpResponse); |
| 192 | + |
| 193 | + RedfishClient newRedfishClientspy = Mockito.spy(new RedfishClient(USERNAME, PASSWORD, true, true, Integer.valueOf(1))); |
| 194 | + newRedfishClientspy.retryHttpRequest(url, httpReq, client); |
| 195 | + |
| 196 | + Mockito.verify(newRedfishClientspy, Mockito.never()).retryHttpRequest(Mockito.anyString(), Mockito.any(), Mockito.any()); |
| 197 | + Mockito.verify(client, Mockito.never()).execute(Mockito.any()); |
| 198 | + } |
| 199 | + |
| 200 | + @Test |
| 201 | + public void retryHttpRequestNoException() throws IOException { |
| 202 | + Mockito.when(client.execute(httpReq)).thenThrow(IOException.class).thenThrow(IOException.class).thenReturn(httpResponse); |
| 203 | + |
| 204 | + RedfishClient newRedfishClientspy = Mockito.spy(new RedfishClient(USERNAME, PASSWORD, true, true, Integer.valueOf(3))); |
| 205 | + newRedfishClientspy.retryHttpRequest(url, httpReq, client); |
| 206 | + |
| 207 | + Mockito.verify(newRedfishClientspy, Mockito.times(1)).retryHttpRequest(Mockito.anyString(), Mockito.any(), Mockito.any()); |
| 208 | + Mockito.verify(client, Mockito.times(3)).execute(Mockito.any()); |
| 209 | + } |
163 | 210 | } |
0 commit comments