Connector { public User login(User user) { for (int i = 0; i < 2; i++) { String userName = user.getUsername(); if (userName == null || userName.trim().length() == 0) { return user; } HttpPost request = new HttpPost(user.getActionUrl()); request.setHeader(HttpHeaders.USER_AGENT, USER_AGENT); String lgDomain = user.getDomain(); List<NameValuePair> params = new ArrayList<>(); params.addAll(Arrays.asList( new BasicNameValuePair(PARAM_ACTION, ACTION_LOGIN), new BasicNameValuePair(PARAM_FORMAT, FORMAT_XML), new BasicNameValuePair(PARAM_LOGIN_NAME, userName), new BasicNameValuePair(PARAM_LOGIN_PASSWORD, user.getPassword()) )); if (lgDomain != null && lgDomain.length() > 0) { params.add(new BasicNameValuePair(PARAM_LOGIN_DOMAIN, lgDomain)); } if (i > 0) { params.add(new BasicNameValuePair(PARAM_LOGIN_TOKEN, user.getToken())); } request.setEntity(new UrlEncodedFormEntity(params, (Charset) null)); try { HttpResponse response = client.execute(request); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { final String responseBody = getAsXmlString(response); XMLUserParser parser = new XMLUserParser(user, responseBody); parser.parse(); if (i == 0 && User.NEED_TOKEN_ID.equals(user.getResult())) { logger.debug("need_token_id"); } else if (User.SUCCESS_ID.equals(user.getResult())) { return user; } else { break; } } } catch (IOException | SAXException e) { logger.error(null, e); } finally { request.releaseConnection(); } } logger.warn("could not log user in"); return null; } Connector();  Connector(HttpClientBuilder builder); User login(User user); List<Page> queryContent(User user, List<String> listOfTitleStrings); List<Page> queryCategories(User user, List<String> listOfTitleStrings); List<Page> queryInfo(User user, List<String> listOfTitleStrings); List<Page> queryLinks(User user, List<String> listOfTitleStrings); List<Page> queryImageinfo(User user, List<String> listOfImageStrings); List<Page> queryImageinfo(User user, List<String> listOfImageStrings, int imageWidth); List<Page> query(User user, Query query);  }
@Test public void testLoginAsAnonymous() { User user = new User(null, null, null); User result = subject.login(user); assertThat(result).isSameAs(user); }
@Betamax(tape="loginWithUsernameFailed", mode = READ_SEQUENTIAL) @Test public void testLoginWithUsernameFailure() throws Exception { User user = new User("someUser", "somePassword", "https: User result = subject.login(user); assertThat(result).isNull(); }
@Betamax(tape = "loginWithUsernameSuccess", mode = READ_SEQUENTIAL) @Test public void testLoginWithUsernameSuccess() throws Exception { User user = new User("jberkel", "testing", "https: User result = subject.login(user); assertThat(result).isNotNull(); assertThat(result.getUsername()).isEqualTo("jberkel"); assertThat(result.getNormalizedUsername()).isEqualTo("Jberkel"); assertThat(result.getUserid()).isEqualTo("1580588"); }