Search in sources :

Example 1 with WorkType

use of org.orcid.jaxb.model.message.WorkType in project ORCID-Source by ORCID.

the class Api12MembersTest method addUpdateWorkTest.

@Test
public void addUpdateWorkTest() throws Exception {
    String clientId = getClient1ClientId();
    String clientRedirectUri = getClient1RedirectUri();
    String clientSecret = getClient1ClientSecret();
    String userId = getUser1OrcidId();
    String password = getUser1Password();
    String accessToken = getAccessToken(userId, password, Arrays.asList("/orcid-works/read-limited", "/activities/update"), clientId, clientSecret, clientRedirectUri, true);
    String title = "Work " + System.currentTimeMillis();
    Long putCode = null;
    Api12Helper.addWork(userId, accessToken, title, t2OAuthClient_1_2);
    ClientResponse response = t2OAuthClient_1_2.viewWorksDetailsXml(userId, accessToken);
    assertNotNull(response);
    assertEquals(200, response.getStatus());
    assertEquals("application/vnd.orcid+xml; charset=UTF-8; qs=5", response.getType().toString());
    OrcidMessage orcidMessageWithNewWork = response.getEntity(OrcidMessage.class);
    assertNotNull(orcidMessageWithNewWork);
    assertNotNull(orcidMessageWithNewWork.getOrcidProfile());
    assertNotNull(orcidMessageWithNewWork.getOrcidProfile().getOrcidActivities());
    assertNotNull(orcidMessageWithNewWork.getOrcidProfile().getOrcidActivities().getOrcidWorks());
    assertNotNull(orcidMessageWithNewWork.getOrcidProfile().getOrcidActivities().getOrcidWorks().getOrcidWork());
    int initialSize = orcidMessageWithNewWork.getOrcidProfile().getOrcidActivities().getOrcidWorks().getOrcidWork().size();
    boolean found = false;
    for (OrcidWork work : orcidMessageWithNewWork.getOrcidProfile().getOrcidActivities().getOrcidWorks().getOrcidWork()) {
        if (title.equals(work.getWorkTitle().getTitle().getContent())) {
            assertNotNull(work.getPutCode());
            putCode = Long.valueOf(work.getPutCode());
            found = true;
        }
    }
    assertTrue(found);
    //Update it
    String newTitle = "Updated - " + title;
    WorkType newType = WorkType.BOOK;
    String newExtId = String.valueOf(System.currentTimeMillis());
    Iterator<OrcidWork> it = orcidMessageWithNewWork.getOrcidProfile().getOrcidActivities().getOrcidWorks().getOrcidWork().iterator();
    while (it.hasNext()) {
        OrcidWork work = it.next();
        if (clientId.equals(work.getSource().retrieveSourcePath())) {
            if (title.equals(work.getWorkTitle().getTitle().getContent())) {
                assertNotNull(work.getPutCode());
                //Update title
                work.getWorkTitle().getTitle().setContent(newTitle);
                //Update ext ids
                work.getWorkExternalIdentifiers().getWorkExternalIdentifier().get(0).getWorkExternalIdentifierId().setContent(newExtId);
                //Update type
                work.setWorkType(newType);
            }
        } else {
            it.remove();
        }
    }
    ClientResponse updateResponse = t2OAuthClient_1_2.updateWorksXml(userId, orcidMessageWithNewWork, accessToken);
    assertEquals(ClientResponse.Status.OK.getStatusCode(), updateResponse.getStatus());
    //Fetch them again and verify the values has been updated
    response = t2OAuthClient_1_2.viewWorksDetailsXml(userId, accessToken);
    assertNotNull(response);
    assertEquals(200, response.getStatus());
    assertEquals("application/vnd.orcid+xml; charset=UTF-8; qs=5", response.getType().toString());
    OrcidMessage orcidMessageWithUpdatedWork = response.getEntity(OrcidMessage.class);
    assertNotNull(orcidMessageWithUpdatedWork);
    assertNotNull(orcidMessageWithUpdatedWork.getOrcidProfile());
    assertNotNull(orcidMessageWithUpdatedWork.getOrcidProfile().getOrcidActivities());
    assertNotNull(orcidMessageWithUpdatedWork.getOrcidProfile().getOrcidActivities().getOrcidWorks());
    assertNotNull(orcidMessageWithUpdatedWork.getOrcidProfile().getOrcidActivities().getOrcidWorks().getOrcidWork());
    int size = orcidMessageWithUpdatedWork.getOrcidProfile().getOrcidActivities().getOrcidWorks().getOrcidWork().size();
    assertEquals(initialSize, size);
    found = false;
    for (OrcidWork work : orcidMessageWithUpdatedWork.getOrcidProfile().getOrcidActivities().getOrcidWorks().getOrcidWork()) {
        assertNotNull(work.getPutCode());
        if (putCode.equals(Long.valueOf(work.getPutCode()))) {
            assertEquals(newTitle, work.getWorkTitle().getTitle().getContent());
            assertEquals(newType, work.getWorkType());
            assertEquals(1, work.getWorkExternalIdentifiers().getWorkExternalIdentifier().size());
            assertEquals(newExtId, work.getWorkExternalIdentifiers().getWorkExternalIdentifier().get(0).getWorkExternalIdentifierId().getContent());
            found = true;
        }
    }
    assertTrue(found);
    // Delete it
    ClientResponse deleteResponse = memberV2ApiClient.deleteWorkXml(this.getUser1OrcidId(), putCode, accessToken);
    assertEquals(Response.Status.NO_CONTENT.getStatusCode(), deleteResponse.getStatus());
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) WorkType(org.orcid.jaxb.model.message.WorkType) OrcidMessage(org.orcid.jaxb.model.message.OrcidMessage) OrcidWork(org.orcid.jaxb.model.message.OrcidWork) Test(org.junit.Test)

Aggregations

ClientResponse (com.sun.jersey.api.client.ClientResponse)1 Test (org.junit.Test)1 OrcidMessage (org.orcid.jaxb.model.message.OrcidMessage)1 OrcidWork (org.orcid.jaxb.model.message.OrcidWork)1 WorkType (org.orcid.jaxb.model.message.WorkType)1