@@ -33,108 +33,132 @@ public class RedfishClientTest {
3333 private static final String USERNAME = "user" ;
3434 private static final String PASSWORD = "password" ;
3535 private static final String oobAddress = "oob.host.address" ;
36- private static final String systemID = "SystemID.1" ;
36+ private static final String systemId = "SystemID.1" ;
3737 private final static String COMPUTER_SYSTEM_RESET_URL_PATH = "/Actions/ComputerSystem.Reset" ;
3838
39- // RedfishClient redfishClientIgnoreSSL = new RedfishClient(USERNAME, PASSWORD, true, false);
40- RedfishClient redfishClientUseSSL = new RedfishClient (USERNAME , PASSWORD , true , true );
4139 RedfishClient redfishClientspy = Mockito .spy (new RedfishClient (USERNAME , PASSWORD , true , true ));
4240
4341 @ Test (expected = RedfishException .class )
4442 public void validateAddressAndPrepareForUrlTestExpect () {
45- redfishClientUseSSL .validateAddressAndPrepareForUrl ("1:1:2:3:1" );
46- redfishClientUseSSL .validateAddressAndPrepareForUrl ("1" );
47- redfishClientUseSSL .validateAddressAndPrepareForUrl ("hostname" );
48- redfishClientUseSSL .validateAddressAndPrepareForUrl (oobAddress );
43+ redfishClientspy .validateAddressAndPrepareForUrl ("1:1:2:3:1" );
44+ redfishClientspy .validateAddressAndPrepareForUrl ("1" );
45+ redfishClientspy .validateAddressAndPrepareForUrl ("hostname" );
46+ redfishClientspy .validateAddressAndPrepareForUrl (oobAddress );
4947 }
5048
5149 @ Test
5250 public void validateAddressAndPrepareForUrlTestDomainName () {
53- String result = redfishClientUseSSL .validateAddressAndPrepareForUrl (oobAddress );
51+ String result = redfishClientspy .validateAddressAndPrepareForUrl (oobAddress );
5452 Assert .assertEquals (oobAddress , result );
5553 }
5654
5755 @ Test
5856 public void validateAddressAndPrepareForUrlTestIpv4 () {
5957 String ipv4 = "192.168.0.123" ;
60- String result = redfishClientUseSSL .validateAddressAndPrepareForUrl (ipv4 );
58+ String result = redfishClientspy .validateAddressAndPrepareForUrl (ipv4 );
6159 Assert .assertEquals (ipv4 , result );
6260 }
6361
6462 @ Test
6563 public void validateAddressAndPrepareForUrlTestIpv6 () {
6664 String ipv6 = "100::ffff:ffff:ffff:ffff" ;
6765 String expected = "[" +ipv6 +"]" ;
68- String result = redfishClientUseSSL .validateAddressAndPrepareForUrl (ipv6 );
66+ String result = redfishClientspy .validateAddressAndPrepareForUrl (ipv6 );
6967 Assert .assertEquals (expected , result );
7068 }
7169
7270 @ Test
7371 public void buildRequestUrlTestHttpsGetSystemId () {
7472 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 );
73+ String result = redfishclient .buildRequestUrl (oobAddress , RedfishClient .RedfishCmdType .GetSystemId , systemId );
74+ String expected = String .format ("https://%s/redfish/v1/Systems/" ,oobAddress , systemId );
7775 Assert .assertEquals (expected , result );
7876 }
7977
8078 @ Test
8179 public void buildRequestUrlTestGetSystemId () {
8280 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 );
81+ String result = redfishclient .buildRequestUrl (oobAddress , RedfishClient .RedfishCmdType .GetSystemId , systemId );
82+ String expected = String .format ("http://%s/redfish/v1/Systems/" ,oobAddress , systemId );
8583 Assert .assertEquals (expected , result );
8684 }
8785
8886 @ Test
8987 public void buildRequestUrlTestHttpsComputerSystemReset () {
9088 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 );
89+ String result = redfishclient .buildRequestUrl (oobAddress , RedfishClient .RedfishCmdType .ComputerSystemReset , systemId );
90+ String expected = String .format ("https://%s/redfish/v1/Systems/%s%s" ,oobAddress , systemId ,COMPUTER_SYSTEM_RESET_URL_PATH );
9391 Assert .assertEquals (expected , result );
9492 }
9593
9694 @ Test
9795 public void buildRequestUrlTestComputerSystemReset () {
9896 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 );
97+ String result = redfishclient .buildRequestUrl (oobAddress , RedfishClient .RedfishCmdType .ComputerSystemReset , systemId );
98+ String expected = String .format ("http://%s/redfish/v1/Systems/%s%s" ,oobAddress , systemId ,COMPUTER_SYSTEM_RESET_URL_PATH );
10199 Assert .assertEquals (expected , result );
102100 }
103101
104102 @ Test
105103 public void buildRequestUrlTestHttpsGetPowerState () {
106104 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 );
105+ String result = redfishclient .buildRequestUrl (oobAddress , RedfishClient .RedfishCmdType .GetPowerState , systemId );
106+ String expected = String .format ("https://%s/redfish/v1/Systems/%s" ,oobAddress , systemId );
109107 Assert .assertEquals (expected , result );
110108 }
111109
112110 @ Test
113111 public void buildRequestUrlTestGetPowerState () {
114112 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 );
113+ String result = redfishclient .buildRequestUrl (oobAddress , RedfishClient .RedfishCmdType .GetPowerState , systemId );
114+ String expected = String .format ("http://%s/redfish/v1/Systems/%s" ,oobAddress , systemId );
117115 Assert .assertEquals (expected , result );
118116 }
119117
120- // @Test
118+ @ Test
121119 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);
120+ Mockito .doReturn (systemId ).when (redfishClientspy ).getSystemId (Mockito .anyString ());
121+ mockResponse (HttpStatus .SC_OK );
122+ RedfishClient .RedfishPowerState expectedState = RedfishClient .RedfishPowerState .On ;
123+ Mockito .doReturn (expectedState ).when (redfishClientspy ).processGetSystemRequestResponse (Mockito .any (CloseableHttpResponse .class ));
125124
126- StatusLine statusLine = Mockito .mock (StatusLine .class );
127- Mockito .doReturn (HttpStatus .SC_OK ).when (statusLine ).getStatusCode ();
125+ RedfishClient .RedfishPowerState result = redfishClientspy .getSystemPowerState (oobAddress );
128126
127+ Assert .assertEquals (expectedState , result );
128+ }
129+
130+ @ Test (expected = RedfishException .class )
131+ public void getSystemPowerStateTestHttpStatusNotOk () {
132+ Mockito .doReturn (systemId ).when (redfishClientspy ).getSystemId (Mockito .anyString ());
133+ mockResponse (HttpStatus .SC_BAD_REQUEST );
134+ redfishClientspy .getSystemPowerState (oobAddress );
135+ }
136+
137+ private CloseableHttpResponse mockResponse (int httpStatusCode ) {
138+ StatusLine statusLine = Mockito .mock (StatusLine .class );
139+ Mockito .doReturn (httpStatusCode ).when (statusLine ).getStatusCode ();
129140 CloseableHttpResponse response = Mockito .mock (CloseableHttpResponse .class );
130141 Mockito .doReturn (statusLine ).when (response ).getStatusLine ();
131-
132142 Mockito .doReturn (response ).when (redfishClientspy ).executeGetRequest (Mockito .anyString ());
143+ return response ;
144+ }
133145
134- // Runnint the tested method
135- redfishClientspy .getSystemPowerState (oobAddress );
146+ @ Test
147+ public void getSystemIdTest () {
148+ CloseableHttpResponse mockedResponse = mockResponse (HttpStatus .SC_OK );
149+ Mockito .doReturn (mockedResponse ).when (redfishClientspy ).executeGetRequest (Mockito .anyString ());
150+ Mockito .doReturn (systemId ).when (redfishClientspy ).processGetSystemIdResponse (Mockito .any (CloseableHttpResponse .class ));
136151
137- // validating the test
152+ String result = redfishClientspy .getSystemId (oobAddress );
153+
154+ Assert .assertEquals (systemId , result );
155+ }
156+
157+ @ Test (expected = RedfishException .class )
158+ public void getSystemIdTestHttpStatusNotOk () {
159+ CloseableHttpResponse mockedResponse = mockResponse (HttpStatus .SC_UNAUTHORIZED );
160+ Mockito .doReturn (mockedResponse ).when (redfishClientspy ).executeGetRequest (Mockito .anyString ());
161+ redfishClientspy .getSystemId (oobAddress );
138162 }
139163
140164}
0 commit comments