@Test
  public void testCantAddDuplicatedResearcherUrl()
      throws InterruptedException, JSONException, URISyntaxException {
    String accessToken =
        getAccessToken(this.client1ClientId, this.client1ClientSecret, this.client1RedirectUri);
    assertNotNull(accessToken);
    Long now = System.currentTimeMillis();
    ResearcherUrl rUrlToCreate = new ResearcherUrl();
    rUrlToCreate.setUrl(new Url("http://newurl.com/" + now));
    rUrlToCreate.setUrlName("url-name-" + System.currentTimeMillis());
    rUrlToCreate.setVisibility(Visibility.PUBLIC);

    // Create it
    ClientResponse postResponse =
        memberV2ApiClient.createResearcherUrls(user1OrcidId, rUrlToCreate, accessToken);
    assertNotNull(postResponse);
    assertEquals(Response.Status.CREATED.getStatusCode(), postResponse.getStatus());

    // Add it again
    postResponse = memberV2ApiClient.createResearcherUrls(user1OrcidId, rUrlToCreate, accessToken);
    assertNotNull(postResponse);
    assertEquals(Response.Status.CONFLICT.getStatusCode(), postResponse.getStatus());

    // Check it can be created by other client
    String otherClientToken =
        getAccessToken(this.client2ClientId, this.client2ClientSecret, this.client2RedirectUri);
    postResponse =
        memberV2ApiClient.createResearcherUrls(user1OrcidId, rUrlToCreate, otherClientToken);
    assertNotNull(postResponse);
    assertEquals(Response.Status.CREATED.getStatusCode(), postResponse.getStatus());
  }
  @Test
  public void testAddMultipleResearcherUrlAndGetThem()
      throws InterruptedException, JSONException, URISyntaxException {
    String accessToken =
        getAccessToken(this.client1ClientId, this.client1ClientSecret, this.client1RedirectUri);
    assertNotNull(accessToken);
    Long now = System.currentTimeMillis();
    ResearcherUrl rUrlToCreate = new ResearcherUrl();
    rUrlToCreate.setVisibility(Visibility.PUBLIC);

    for (int i = 0; i < 3; i++) {
      // Change the name
      rUrlToCreate.setUrlName("url-name-" + now + "-" + i);
      rUrlToCreate.setUrl(new Url("http://newurl.com/" + now + "/" + i));
      // Create it
      ClientResponse postResponse =
          memberV2ApiClient.createResearcherUrls(user1OrcidId, rUrlToCreate, accessToken);
      assertNotNull(postResponse);
      assertEquals(Response.Status.CREATED.getStatusCode(), postResponse.getStatus());
    }

    ClientResponse getAllResponse =
        memberV2ApiClient.getResearcherUrls(this.user1OrcidId, accessToken);
    assertNotNull(getAllResponse);
    ResearcherUrls researcherUrls = getAllResponse.getEntity(ResearcherUrls.class);
    assertNotNull(researcherUrls);
    assertNotNull(researcherUrls.getPath());
    assertTrue(researcherUrls.getPath().contains(this.user1OrcidId));
    assertNotNull(researcherUrls.getResearcherUrls());

    boolean found1 = false, found2 = false, found3 = false;

    for (ResearcherUrl rUrl : researcherUrls.getResearcherUrls()) {
      if (rUrl.getUrlName().equals("url-name-" + now + "-0")) {
        found1 = true;
      } else if (rUrl.getUrlName().equals("url-name-" + now + "-1")) {
        found2 = true;
      } else if (rUrl.getUrlName().equals("url-name-" + now + "-2")) {
        found3 = true;
      }
    }

    assertTrue(found1 && found2 && found3);

    // Clean
    for (ResearcherUrl rUrl : researcherUrls.getResearcherUrls()) {
      memberV2ApiClient.deletePeerReviewXml(this.user1OrcidId, rUrl.getPutCode(), accessToken);
    }
  }
 /**
  * PRECONDITIONS: The user should have one public address US
  *
  * @throws JSONException
  * @throws InterruptedException
  */
 @Test
 public void testGetAddressWithMembersAPI() throws InterruptedException, JSONException {
   String accessToken =
       getAccessToken(this.client1ClientId, this.client1ClientSecret, this.client1RedirectUri);
   assertNotNull(accessToken);
   ClientResponse response = memberV2ApiClient.viewAddresses(user1OrcidId, accessToken);
   assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
   Addresses addresses = response.getEntity(Addresses.class);
   assertNotNull(addresses);
   assertNotNull(addresses.getAddress());
   assertEquals(1, addresses.getAddress().size());
   assertEquals(Visibility.PUBLIC, addresses.getAddress().get(0).getVisibility());
   assertEquals(Iso3166Country.US, addresses.getAddress().get(0).getCountry().getValue());
 }
  @Test
  public void testInvalidPutCodeReturns404() throws InterruptedException, JSONException {
    String accessToken =
        getAccessToken(this.client1ClientId, this.client1ClientSecret, this.client1RedirectUri);
    assertNotNull(accessToken);

    Address address = new Address();
    address.setCountry(new Country(Iso3166Country.CR));
    address.setVisibility(Visibility.PUBLIC);
    address.setPutCode(1234567890L);

    ClientResponse response = memberV2ApiClient.updateAddress(user1OrcidId, address, accessToken);
    assertNotNull(response);
    assertEquals(ClientResponse.Status.NOT_FOUND.getStatusCode(), response.getStatus());
  }
  @Test
  public void testInvalidPutCodeReturns404() throws InterruptedException, JSONException {
    String accessToken =
        getAccessToken(this.client1ClientId, this.client1ClientSecret, this.client1RedirectUri);
    assertNotNull(accessToken);

    ResearcherUrl rUrlToCreate = new ResearcherUrl();
    rUrlToCreate.setUrl(new Url("http://newurl.com/" + System.currentTimeMillis()));
    rUrlToCreate.setUrlName("url-name-" + System.currentTimeMillis());
    rUrlToCreate.setVisibility(Visibility.PUBLIC);
    rUrlToCreate.setPutCode(1234567890L);

    ClientResponse response =
        memberV2ApiClient.updateResearcherUrls(user1OrcidId, rUrlToCreate, accessToken);
    assertNotNull(response);
    assertEquals(ClientResponse.Status.NOT_FOUND.getStatusCode(), response.getStatus());
  }
  @Test
  public void testTryingToAddInvalidResearcherUrls()
      throws InterruptedException, JSONException, URISyntaxException {
    String accessToken =
        getAccessToken(this.client1ClientId, this.client1ClientSecret, this.client1RedirectUri);
    assertNotNull(accessToken);
    ResearcherUrl rUrlToCreate = new ResearcherUrl();
    rUrlToCreate.setUrl(new Url(""));
    rUrlToCreate.setUrlName("");
    // Create
    ClientResponse postResponse =
        memberV2ApiClient.createResearcherUrls(user1OrcidId, rUrlToCreate, accessToken);
    assertNotNull(postResponse);
    assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), postResponse.getStatus());

    String _351Chars = new String();
    for (int i = 0; i < 531; i++) {
      _351Chars += "a";
    }

    rUrlToCreate.setUrl(new Url(_351Chars));
    postResponse = memberV2ApiClient.createResearcherUrls(user1OrcidId, rUrlToCreate, accessToken);
    assertNotNull(postResponse);
    assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), postResponse.getStatus());

    rUrlToCreate.setUrl(new Url("http://myurl.com"));
    rUrlToCreate.setUrlName(_351Chars);
    postResponse = memberV2ApiClient.createResearcherUrls(user1OrcidId, rUrlToCreate, accessToken);
    assertNotNull(postResponse);
    assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), postResponse.getStatus());

    rUrlToCreate.setUrlName("The name");
    postResponse = memberV2ApiClient.createResearcherUrls(user1OrcidId, rUrlToCreate, accessToken);
    assertNotNull(postResponse);
    assertEquals(Response.Status.CREATED.getStatusCode(), postResponse.getStatus());

    // Read it to delete it
    ClientResponse getResponse =
        memberV2ApiClient.viewLocationXml(postResponse.getLocation(), accessToken);
    assertEquals(Response.Status.OK.getStatusCode(), getResponse.getStatus());
    ResearcherUrl gotResearcherUrl = getResponse.getEntity(ResearcherUrl.class);
    ClientResponse deleteResponse =
        memberV2ApiClient.deleteResearcherUrl(
            this.user1OrcidId, gotResearcherUrl.getPutCode(), accessToken);
    assertEquals(Response.Status.NO_CONTENT.getStatusCode(), deleteResponse.getStatus());
  }
  @Test
  public void testResearcherUrl() throws InterruptedException, JSONException, URISyntaxException {
    String accessToken =
        getAccessToken(this.client1ClientId, this.client1ClientSecret, this.client1RedirectUri);
    assertNotNull(accessToken);
    ResearcherUrl rUrlToCreate =
        (ResearcherUrl)
            unmarshallFromPath(
                "/record_2.0_rc2/samples/researcher-url-2.0_rc2.xml", ResearcherUrl.class);
    assertNotNull(rUrlToCreate);
    Long time = System.currentTimeMillis();
    rUrlToCreate.setCreatedDate(null);
    rUrlToCreate.setLastModifiedDate(null);
    rUrlToCreate.setPath(null);
    rUrlToCreate.setPutCode(null);
    rUrlToCreate.setSource(null);
    rUrlToCreate.setUrl(new Url(rUrlToCreate.getUrl().getValue() + time));
    rUrlToCreate.setUrlName(String.valueOf(time));

    // Create
    ClientResponse postResponse =
        memberV2ApiClient.createResearcherUrls(user1OrcidId, rUrlToCreate, accessToken);
    assertNotNull(postResponse);
    assertEquals(Response.Status.CREATED.getStatusCode(), postResponse.getStatus());
    String locationPath = postResponse.getLocation().getPath();
    assertTrue(
        "Location header path should match pattern, but was " + locationPath,
        locationPath.matches(".*/v2.0_rc2/" + user1OrcidId + "/researcher-urls/\\d+"));

    // Read
    ClientResponse getResponse =
        memberV2ApiClient.viewLocationXml(postResponse.getLocation(), accessToken);
    assertEquals(Response.Status.OK.getStatusCode(), getResponse.getStatus());
    ResearcherUrl gotResearcherUrl = getResponse.getEntity(ResearcherUrl.class);
    assertNotNull(gotResearcherUrl);
    assertNotNull(gotResearcherUrl.getPutCode());
    assertNotNull(gotResearcherUrl.getSource());
    assertNotNull(gotResearcherUrl.getCreatedDate());
    assertNotNull(gotResearcherUrl.getLastModifiedDate());
    assertEquals(this.client1ClientId, gotResearcherUrl.getSource().retrieveSourcePath());
    assertEquals("http://site1.com/" + time, gotResearcherUrl.getUrl().getValue());
    assertEquals(String.valueOf(time), gotResearcherUrl.getUrlName());
    assertEquals("public", gotResearcherUrl.getVisibility().value());

    // Update
    LastModifiedDate initialLastModified = gotResearcherUrl.getLastModifiedDate();
    Long currentTime = System.currentTimeMillis();
    gotResearcherUrl.setUrlName(gotResearcherUrl.getUrlName() + " - " + currentTime);
    gotResearcherUrl.getUrl().setValue(gotResearcherUrl.getUrl().getValue() + currentTime);
    gotResearcherUrl.setVisibility(Visibility.LIMITED);
    ClientResponse updatedResearcherUrlResponse =
        memberV2ApiClient.updateResearcherUrls(this.user1OrcidId, gotResearcherUrl, accessToken);
    assertNotNull(updatedResearcherUrlResponse);
    assertEquals(Response.Status.OK.getStatusCode(), updatedResearcherUrlResponse.getStatus());
    ResearcherUrl updatedResearcherUrl =
        updatedResearcherUrlResponse.getEntity(ResearcherUrl.class);
    assertNotNull(updatedResearcherUrl);
    assertEquals(
        "http://site1.com/" + time + currentTime, updatedResearcherUrl.getUrl().getValue());
    assertEquals(String.valueOf(time) + " - " + currentTime, updatedResearcherUrl.getUrlName());
    // Keep it public, since it is more restrictive than the user visibility
    // default
    assertEquals("public", updatedResearcherUrl.getVisibility().value());
    assertFalse(initialLastModified.equals(updatedResearcherUrl.getLastModifiedDate()));

    // Delete
    ClientResponse deleteResponse =
        memberV2ApiClient.deleteResearcherUrl(
            this.user1OrcidId, gotResearcherUrl.getPutCode(), accessToken);
    assertEquals(Response.Status.NO_CONTENT.getStatusCode(), deleteResponse.getStatus());
  }
  @SuppressWarnings({"deprecation", "rawtypes"})
  @Test
  public void testCreateGetUpdateAndDeleteKeyword() throws InterruptedException, JSONException {
    String accessToken =
        getAccessToken(this.client1ClientId, this.client1ClientSecret, this.client1RedirectUri);
    assertNotNull(accessToken);

    Address address = new Address();
    address.setCountry(new Country(Iso3166Country.CR));
    address.setVisibility(Visibility.PUBLIC);
    // Create
    ClientResponse response = memberV2ApiClient.createAddress(user1OrcidId, address, accessToken);
    assertNotNull(response);
    assertEquals(ClientResponse.Status.CREATED.getStatusCode(), response.getStatus());
    Map map = response.getMetadata();
    assertNotNull(map);
    assertTrue(map.containsKey("Location"));
    List resultWithPutCode = (List) map.get("Location");
    String location = resultWithPutCode.get(0).toString();
    Long putCode = Long.valueOf(location.substring(location.lastIndexOf('/') + 1));

    // Get all and verify
    response = memberV2ApiClient.viewAddresses(user1OrcidId, accessToken);
    assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
    Addresses addresses = response.getEntity(Addresses.class);
    assertNotNull(addresses);
    assertNotNull(addresses.getAddress());
    assertEquals(2, addresses.getAddress().size());

    boolean foundUS = false;
    boolean foundCR = false;

    for (Address add : addresses.getAddress()) {
      assertEquals(Visibility.PUBLIC, add.getVisibility());
      assertNotNull(add.getCountry());
      assertNotNull(add.getCountry().getValue());
      if (add.getCountry().getValue().equals(Iso3166Country.US)) {
        foundUS = true;
      } else if (add.getCountry().getValue().equals(Iso3166Country.CR)) {
        foundCR = true;
      }
    }

    assertTrue(foundUS);
    assertTrue(foundCR);

    // Get it
    response = memberV2ApiClient.viewAddress(user1OrcidId, putCode, accessToken);
    assertNotNull(response);
    address = response.getEntity(Address.class);
    assertNotNull(address);
    assertNotNull(address.getSource());
    assertEquals(client1ClientId, address.getSource().retrieveSourcePath());
    assertNotNull(address.getCountry());
    assertNotNull(address.getCountry().getValue());
    assertEquals(Iso3166Country.CR, address.getCountry().getValue());
    assertEquals(Visibility.PUBLIC, address.getVisibility());

    // Update
    address.getCountry().setValue(Iso3166Country.PA);
    response = memberV2ApiClient.updateAddress(user1OrcidId, address, accessToken);
    assertNotNull(response);
    assertEquals(ClientResponse.Status.OK.getStatusCode(), response.getStatus());
    response = memberV2ApiClient.viewAddress(user1OrcidId, putCode, accessToken);
    assertNotNull(response);
    Address updatedAddress = response.getEntity(Address.class);
    assertNotNull(updatedAddress);
    assertNotNull(updatedAddress.getCountry());
    assertEquals(Iso3166Country.PA, updatedAddress.getCountry().getValue());
    assertEquals(address.getPutCode(), updatedAddress.getPutCode());

    // Delete
    response = memberV2ApiClient.deleteAddress(user1OrcidId, putCode, accessToken);
    assertNotNull(response);
    assertEquals(ClientResponse.Status.NO_CONTENT.getStatusCode(), response.getStatus());

    // Check it was deleted
    testGetAddressWithMembersAPI();
  }