public void testResponseError() {
    startServer(HttpMethodResponseErrorResource.class);
    WebResource r = createClient().resource(getUri().path("test").build());

    testResponseError(r.get(ClientResponse.class));
    testResponseError(r.post(ClientResponse.class));
    testResponseError(r.put(ClientResponse.class));
    testResponseError(r.delete(ClientResponse.class));
  }
  public void testAll() {
    startServer(HttpMethodResource.class);
    WebResource r = createClient().resource(getUri().path("test").build());
    assertEquals("GET", r.get(String.class));

    assertEquals("POST", r.post(String.class, "POST"));

    assertEquals("PUT", r.put(String.class, "PUT"));

    assertEquals("DELETE", r.delete(String.class));
  }
 public void testUser(String userName, ClientResponse.Status expectedStatus, String expectedResult)
     throws Exception {
   Client webClient = Client.create();
   webClient.addFilter(new HTTPBasicAuthFilter(userName, generalPassword));
   WebResource webResource =
       webClient.resource(
           new URI(contextUri(scheduler().injector()) + "/jobscheduler/engine-cpp/command"));
   ClientResponse response = webResource.post(ClientResponse.class, xmlCommand);
   assertThat(response.getStatus(), equalTo(expectedStatus.getStatusCode()));
   assertThat(response.getEntity(String.class), containsString(expectedResult));
   webClient.destroy();
 }
  public static void main(String[] args) {
    try {
      // Get The Node List From File
      JAXBContext ctx = JAXBContext.newInstance(NodeList.class);
      Unmarshaller um = ctx.createUnmarshaller();
      NodeList tempNodeList =
          (NodeList) um.unmarshal(new StreamSource(new File("graph/NodeList.xml")));

      // Get the Edge List From File
      JAXBContext ctx1 = JAXBContext.newInstance(EdgeList.class);
      Unmarshaller um1 = ctx1.createUnmarshaller();
      EdgeList tempEdgeList =
          (EdgeList) um1.unmarshal(new StreamSource(new File("graph/EdgeList.xml")));

      RestFloeInfo tempFloeInfo = new RestFloeInfo();
      tempFloeInfo.setEdge(tempEdgeList);
      tempFloeInfo.setNode(tempNodeList);
      Client c = Client.create();
      WebResource r = c.resource("http://localhost:45000/Coordinator/createFloe");
      c.getProperties().put(ClientConfig.PROPERTY_FOLLOW_REDIRECTS, true);
      ClientResponse response;
      c.getProperties().put(ClientConfig.PROPERTY_FOLLOW_REDIRECTS, true);
      response = r.post(ClientResponse.class, tempFloeInfo);
      // GenericType<JAXBElement<StartFloeInfo>> retJAXBInfo = new
      // GenericType<JAXBElement<StartFloeInfo>>() {};
      GenericType<StartFloeInfo> retJAXBInfo = new GenericType<StartFloeInfo>() {};
      StartFloeInfo retFloeInfo = response.getEntity(retJAXBInfo);
      List<ConnectionInfo> startConnectionList = new ArrayList<ConnectionInfo>();
      startConnectionList = retFloeInfo.getConnection();
      int tempCount = 0;
      while (tempCount < startConnectionList.size()) {

        System.out.println(
            startConnectionList.get(tempCount).getSourceAddress()
                + " "
                + startConnectionList.get(tempCount).getInPortNo());
        tempCount++;
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
  /**
   * IHTSDO authenticate.
   *
   * @param username the username
   * @param password the password
   * @return the string
   * @throws Exception the exception
   */
  @SuppressWarnings("unchecked")
  private String ihtsdoAuthenticate(String username, String password) throws Exception {

    String ihtsdoSecurityUrl = config.getProperty("ihtsdo.security.url");

    // set up request to be posted to ihtsdo security service
    Form form = new Form();
    form.add("username", username);
    form.add("password", password);
    form.add("queryName", "getUserByNameAuth");

    Client client = Client.create();
    WebResource resource = client.resource(ihtsdoSecurityUrl);

    resource.type(MediaType.APPLICATION_FORM_URLENCODED_TYPE);

    ClientResponse response = resource.post(ClientResponse.class, form);

    String resultString = "";
    if (response.getClientResponseStatus().getFamily() == Family.SUCCESSFUL) {
      resultString = response.getEntity(String.class);
    } else {
      // TODO Differentiate error messages with NO RESPONSE and
      // Authentication Failed (Check text)
      Logger.getLogger(this.getClass()).info("ERROR! " + response.getStatus());
      resultString = response.getEntity(String.class);
      Logger.getLogger(this.getClass()).info(resultString);
      throw new LocalException("Incorrect user name or password.");
    }

    /*
     * Synchronize the information sent back from ITHSDO with the MapUser
     * object. Add a new map user if there isn't one matching the username If
     * there is, load and update that map user and save the changes
     */
    String ihtsdoUserName = "";
    String ihtsdoEmail = "";
    String ihtsdoGivenName = "";
    String ihtsdoSurname = "";

    // converting json to Map
    byte[] mapData = resultString.getBytes();
    Map<String, HashMap<String, String>> jsonMap = new HashMap<>();

    // parse username from json object
    ObjectMapper objectMapper = new ObjectMapper();
    jsonMap = objectMapper.readValue(mapData, HashMap.class);
    for (Entry<String, HashMap<String, String>> entrySet : jsonMap.entrySet()) {
      if (entrySet.getKey().equals("user")) {
        HashMap<String, String> innerMap = entrySet.getValue();
        for (Entry<String, String> innerEntrySet : innerMap.entrySet()) {
          if (innerEntrySet.getKey().equals("name")) {
            ihtsdoUserName = innerEntrySet.getValue();
          } else if (innerEntrySet.getKey().equals("email")) {
            ihtsdoEmail = innerEntrySet.getValue();
          } else if (innerEntrySet.getKey().equals("givenName")) {
            ihtsdoGivenName = innerEntrySet.getValue();
          } else if (innerEntrySet.getKey().equals("surname")) {
            ihtsdoSurname = innerEntrySet.getValue();
          }
        }
      }
    }
    // check if ihtsdo user matches one of our MapUsers
    MappingService mappingService = new MappingServiceJpa();
    MapUserList userList = mappingService.getMapUsers();
    MapUser userFound = null;
    for (MapUser user : userList.getMapUsers()) {
      if (user.getUserName().equals(ihtsdoUserName)) {
        userFound = user;
        break;
      }
    }
    // if MapUser was found, update to match ihtsdo settings
    if (userFound != null) {
      userFound.setEmail(ihtsdoEmail);
      userFound.setName(ihtsdoGivenName + " " + ihtsdoSurname);
      userFound.setUserName(ihtsdoUserName);
      mappingService.updateMapUser(userFound);
      // if MapUser not found, create one for our use
    } else {
      MapUser newMapUser = new MapUserJpa();
      newMapUser.setName(ihtsdoGivenName + " " + ihtsdoSurname);
      newMapUser.setUserName(ihtsdoUserName);
      newMapUser.setEmail(ihtsdoEmail);
      newMapUser.setApplicationRole(MapUserRole.VIEWER);
      mappingService.addMapUser(newMapUser);
    }
    mappingService.close();

    // Generate application-managed token
    String token = UUID.randomUUID().toString();
    tokenUsernameMap.put(token, ihtsdoUserName);
    tokenLoginMap.put(token, new Date());

    Logger.getLogger(this.getClass()).info("User = " + resultString);

    return token;
  }
 public void testPostEmpty() {
   startServer(HttpMethodResource.class);
   WebResource r = createClient().resource(getUri().path("test").build());
   assertEquals("", r.post(String.class, ""));
 }