RestClient { public Connect.ConnectType connect(final String host, final String username, final String password) { String oldHostUri = hostUri; hostUri = Constants.HTTPS_CONNECTION_PREFIX + host + Constants.HTTPS_CONNECTION_LOGIN_SUFFIX; Connect.ConnectType connectType = null; try { LoginResponse response = loginClient.login(hostUri, username, password); if (response.getResponseCode() == HttpStatus.OK.value()) { if(CommonUtil.isBlank(response.getSessionId())) { if (isConnected()) { System.out.println(Constants.CONNECTION_ALREADY_ESTABLISHED); connectType = Connect.ConnectType.SUCCESS; } else { System.out.println(Constants.CONNECT_FAILURE_NO_SESSION_ID); connectType = Connect.ConnectType.ERROR; } } else { writeCookieInfo(response.getSessionId()); System.out.println(Constants.CONNECT_SUCCESS); connectType = Connect.ConnectType.SUCCESS; } } else if(response.getResponseCode() == HttpStatus.UNAUTHORIZED.value()) { System.out.println(Constants.CONNECT_UNAUTHORIZATION_CONNECT); hostUri = oldHostUri; connectType = Connect.ConnectType.UNAUTHORIZATION; } else if(response.getResponseCode() == HttpStatus.INTERNAL_SERVER_ERROR.value()) { System.out.println(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase()); connectType = Connect.ConnectType.ERROR; } else { System.out.println( String.format( Constants.UNSUPPORTED_HTTP_RESPONSE_CODE, response.getResponseCode())); hostUri = oldHostUri; connectType = Connect.ConnectType.ERROR; } } catch (Exception e) { System.out.println(Constants.CONNECT_FAILURE + ": " + (CommandsUtils.getExceptionMessage(e))); connectType = Connect.ConnectType.ERROR; } return connectType; } private  RestClient(); String getContentAsString(String path); Connect.ConnectType connect(final String host, final String username,
         final String password); void disconnect(); void createObject(Object entity, final String path,
         final HttpMethod verb, PrettyOutput... prettyOutput); T getObject(final String id, Class<T> entityType,
         final String path, final HttpMethod verb, final boolean detail); T getObject(final String path, Class<T> entityType, final HttpMethod verb, Object body); T getObjectByPath(Class<T> entityType, final String path,
         final HttpMethod verb, final boolean detail); T getAllObjects(final Class<T> entityType, final String path,
         final HttpMethod verb, final boolean detail); void deleteObject(final String id, final String path,
         final HttpMethod verb, PrettyOutput... prettyOutput); void actionOps(final String id, final String path,
         final HttpMethod verb, final Map<String, String> queryStrings,
         PrettyOutput... prettyOutput); void update(Object entity, final String path, final HttpMethod verb,
         PrettyOutput... prettyOutput); TaskRead updateWithReturn(Object entity, final String path, final HttpMethod verb, PrettyOutput... prettyOutput); void updateWithQueryStrings(Object entity, final String path, Map<String, String> queryStrings,
                                      final HttpMethod verb, PrettyOutput... prettyOutput);  }
@Test(dataProvider = "RestClientTest.LoginDP") public void testLogin(LoginResponse loginResponse, Connect.ConnectType expectedConnectType) throws IOException { Mockito.when(loginClient.login(Matchers.anyString(), Matchers.anyString(), Matchers.anyString())).thenReturn( loginResponse); Connect.ConnectType connectType = restClient.connect("127.0.0.1:8443", "root", "vmware"); Assert.assertEquals(connectType, expectedConnectType); if(loginResponse.getSessionId() != null) { Assert.assertEquals(loginResponse.getSessionId(), CookieCache.get(CookieCache.COOKIE)); } }
@Test public void testLoginWithException() throws IOException { Mockito.when(loginClient.login(Matchers.anyString(), Matchers.anyString(), Matchers.anyString())).thenThrow( new IOException("can't connect to network") ); Connect.ConnectType connectType = restClient.connect("127.0.0.1:8443", "root", "vmware"); Assert.assertEquals(connectType, Connect.ConnectType.ERROR); }
@Test public void testRelogin() throws IOException { Mockito.when(loginClient.login(Matchers.anyString(), Matchers.anyString(), Matchers.anyString())).thenReturn( new LoginResponse(200, "JSESSIONID=B6926322AF4D8A8B9CEF3906D5735D41")); Connect.ConnectType connectType = restClient.connect("127.0.0.1:8443", "root", "vmware"); Assert.assertEquals(connectType, Connect.ConnectType.SUCCESS); Mockito.when(loginClient.login(Matchers.anyString(), Matchers.anyString(), Matchers.anyString())).thenReturn( new LoginResponse(200, null)); connectType = restClient.connect("127.0.0.1:8443", "root", "vmware"); Assert.assertEquals(connectType, Connect.ConnectType.SUCCESS); }