Commit 0a967d9e authored by Gerrit Hübbers's avatar Gerrit Hübbers 🃏
Browse files

WIP. Implement addMetadataToItem method

parent cc8bd127
......@@ -6,6 +6,8 @@ import java.util.List;
import javax.inject.Inject;
import org.gesis.dda.feeder.ssoar.domain.Collection;
import org.gesis.dda.feeder.ssoar.domain.Item;
import org.gesis.dda.feeder.ssoar.domain.Status;
import org.gesis.dda.feeder.ssoar.domain.User;
import org.gesis.dda.feeder.ssoar.domain.XmlWorkflowItem;
import org.gesis.dda.wizard.config.IngesterProperties;
......@@ -13,6 +15,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.BufferingClientHttpRequestFactory;
......@@ -29,7 +32,7 @@ public class SsoarRestService {
private RestTemplate restTemplate;
private String endpoint;
private User user;
private User authenticationUser;
private String authenticationToken;
private boolean isAuthenticated = false;
......@@ -38,7 +41,7 @@ public class SsoarRestService {
public SsoarRestService(IngesterProperties ingesterProperties) {
this.endpoint = ingesterProperties.getEndpoint();
log.debug("SSOAR REST endpoint: {}", endpoint);
this.user = new User( ingesterProperties.getEmail(), ingesterProperties.getPassword() );
this.authenticationUser = new User( ingesterProperties.getEmail(), ingesterProperties.getPassword() );
//restTemplate = new RestTemplate();
restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()));
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
......@@ -46,10 +49,14 @@ public class SsoarRestService {
restTemplate.setInterceptors(interceptors);
}
public User getAuthenticationUser() {
return authenticationUser;
}
public boolean login() {
isAuthenticated = false;
ResponseEntity<String> authenticationTokenResponseEntity = restTemplate.postForEntity( endpoint + "/login", user, String.class );
ResponseEntity<String> authenticationTokenResponseEntity = restTemplate.postForEntity( endpoint + "/login", authenticationUser, String.class );
HttpStatus loginStatus = authenticationTokenResponseEntity.getStatusCode();
log.debug("Login status: {}", loginStatus);
if ( loginStatus.is2xxSuccessful() ) {
......@@ -61,16 +68,51 @@ public class SsoarRestService {
return isAuthenticated;
}
public XmlWorkflowItem createNewXmlWorkflowItemInCollection(Collection targetCollection) {
//ResponseEntity<XmlWorkflowItem> xmlWorkflowItemResponseEntity = restTemplate.postForEntity( endpoint + "/xmlworkflowitems?collection_id={}", user, XmlWorkflowItem.class, targetCollection.getId() );
// .addHeader("rest-dspace-token", DUMMY_EDITOR_AUTHENTICATION_TOKEN_LOCALHOST)
public Status getStatus() {
HttpHeaders headers = new HttpHeaders();
headers.set("rest-dspace-token", authenticationToken);
HttpEntity<?> httpEntity = new HttpEntity<>(headers);
ResponseEntity<Status> statusResponseEntity = restTemplate.exchange( endpoint + "/status", HttpMethod.GET, httpEntity, Status.class);
Status status = statusResponseEntity.getBody();
log.debug("Status: {}", status);
return status;
}
public Item getItem(Item itemSpecification) {
String requestedExpands;
if (itemSpecification.getExpand() != null) {
requestedExpands = String.join(",", itemSpecification.getExpand());
}
else {
requestedExpands = "null";
}
HttpHeaders headers = new HttpHeaders();
headers.set("rest-dspace-token", authenticationToken);
HttpEntity<?> httpEntity = new HttpEntity<>(headers);
HttpEntity<Collection> entity = new HttpEntity<>(targetCollection, headers);
ResponseEntity<XmlWorkflowItem> xmlWorkflowItemResponseEntity = restTemplate.postForEntity( endpoint + "/xmlworkflowitems?collection_id={targetCollectionId}", entity, XmlWorkflowItem.class, targetCollection.getId() );
ResponseEntity<Item> itemResponseEntity = restTemplate.exchange( endpoint + "/items/{itemId}?expand={requestedExpands}", HttpMethod.GET, httpEntity, Item.class, itemSpecification.getId(), requestedExpands );
Item item = itemResponseEntity.getBody();
log.debug("Item: {}", item);
return item;
}
public XmlWorkflowItem createNewXmlWorkflowItemInCollection(Collection targetCollectionSpecification) {
HttpHeaders headers = new HttpHeaders();
headers.set("rest-dspace-token", authenticationToken);
HttpEntity<Collection> entity = new HttpEntity<>(targetCollectionSpecification, headers);
ResponseEntity<XmlWorkflowItem> xmlWorkflowItemResponseEntity = restTemplate.postForEntity( endpoint + "/xmlworkflowitems?collection_id={targetCollectionId}", entity, XmlWorkflowItem.class, targetCollectionSpecification.getId() );
XmlWorkflowItem xmlWorkflowItem = xmlWorkflowItemResponseEntity.getBody();
log.debug("new XmlWorkflowItem: {}", xmlWorkflowItem);
return xmlWorkflowItem;
}
public void addMetadataToItem(Item itemSpecification) {
HttpHeaders headers = new HttpHeaders();
headers.set("rest-dspace-token", authenticationToken);
HttpEntity<?> httpEntity = new HttpEntity<>( itemSpecification.getMetadata(), headers );
restTemplate.postForEntity( endpoint + "/items/{itemId}/metadata", httpEntity, null, itemSpecification.getId() );
}
}
package org.gesis.dda.feeder.ssoar.domain;
import org.apache.commons.lang3.builder.ToStringBuilder;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
......@@ -33,4 +35,48 @@ public class MetadataEntry {
this.language = language;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((key == null) ? 0 : key.hashCode());
result = prime * result + ((language == null) ? 0 : language.hashCode());
result = prime * result + ((value == null) ? 0 : value.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MetadataEntry other = (MetadataEntry) obj;
if (key == null) {
if (other.key != null)
return false;
}
else if (!key.equals(other.key))
return false;
if (language == null) {
if (other.language != null)
return false;
}
else if (!language.equals(other.language))
return false;
if (value == null) {
if (other.value != null)
return false;
}
else if (!value.equals(other.value))
return false;
return true;
}
}
package org.gesis.dda.feeder.ssoar.domain;
import org.apache.commons.lang3.builder.ToStringBuilder;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
......@@ -16,23 +18,31 @@ public class Status {
public void setOkay(boolean okay) {
this.okay = okay;
}
public boolean isAuthenticated() {
return authenticated;
}
public void setAuthenticated(boolean authenticated) {
this.authenticated = authenticated;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getFullname() {
return fullname;
}
public void setFullname(String fullname) {
this.fullname = fullname;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
package org.gesis.dda.feeder.ssoar;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.hasItem;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import javax.inject.Inject;
import org.gesis.dda.feeder.ssoar.domain.Collection;
import org.gesis.dda.feeder.ssoar.domain.Item;
import org.gesis.dda.feeder.ssoar.domain.MetadataEntry;
import org.gesis.dda.feeder.ssoar.domain.Status;
import org.gesis.dda.feeder.ssoar.domain.XmlWorkflowItem;
import org.gesis.dda.wizard.Application;
import org.junit.Before;
......@@ -28,9 +41,17 @@ public class SsoarRestServiceIntTest {
private final static Logger log = LoggerFactory.getLogger(SsoarRestServiceIntTest.class);
private final static Collection MONOGRAPHY_COLLECTION = new Collection();
private final static Collection MONOGRAPHY_COLLECTION_SPECIFICATION = new Collection();
private final static Item NOTES_OF_A_DUMMY_EDITOR_ITEM_ALL_EXPANDED_SPECIFICATION = new Item();
private final static Item NOTES_OF_A_DUMMY_EDITOR_ITEM_NULL_EXPANDED_SPECIFICATION = new Item();
static {
MONOGRAPHY_COLLECTION.setId(2);
MONOGRAPHY_COLLECTION_SPECIFICATION.setId(2);
NOTES_OF_A_DUMMY_EDITOR_ITEM_ALL_EXPANDED_SPECIFICATION.setId(39921);
NOTES_OF_A_DUMMY_EDITOR_ITEM_ALL_EXPANDED_SPECIFICATION.setExpand( Arrays.asList("all") );
NOTES_OF_A_DUMMY_EDITOR_ITEM_NULL_EXPANDED_SPECIFICATION.setId(39921);
}
@Inject
......@@ -47,9 +68,70 @@ public class SsoarRestServiceIntTest {
assertEquals(true, successfullyLoggedIn);
}
@Test
public void getStatus() {
Status actualStatus = ssoarRestService.getStatus();
assertNotNull(actualStatus);
log.debug("Status: {}", actualStatus);
assertTrue(actualStatus.isAuthenticated() );
assertEquals(ssoarRestService.getAuthenticationUser().getEmail(), actualStatus.getEmail());
}
@Test
public void getItemWithAllData() {
Item actualItem = ssoarRestService.getItem(NOTES_OF_A_DUMMY_EDITOR_ITEM_ALL_EXPANDED_SPECIFICATION);
assertNotNull(actualItem);
log.debug("Item: {}", actualItem);
assertEquals("Notes of a dummy editor", actualItem.getName() );
List<MetadataEntry> actualMetadata = actualItem.getMetadata();
MetadataEntry expectedMetadatum = new MetadataEntry();
expectedMetadatum.setKey("dc.contributor.author");
expectedMetadatum.setLanguage("en_US");
expectedMetadatum.setValue("Editor, Dummy");
assertThat( actualMetadata, hasItem(expectedMetadatum) );
}
@Test
public void getItemWithMinimalData() {
Item actualItem = ssoarRestService.getItem(NOTES_OF_A_DUMMY_EDITOR_ITEM_NULL_EXPANDED_SPECIFICATION);
assertNotNull(actualItem);
log.debug("Item: {}", actualItem);
assertEquals("Notes of a dummy editor", actualItem.getName() );
assertNull(actualItem.getMetadata());
}
@Test
public void createNewXmlWorkflowItemInMonographyCollection() {
XmlWorkflowItem createdXmlWorkflowItem = ssoarRestService.createNewXmlWorkflowItemInCollection(MONOGRAPHY_COLLECTION);
XmlWorkflowItem createdXmlWorkflowItem = ssoarRestService.createNewXmlWorkflowItemInCollection(MONOGRAPHY_COLLECTION_SPECIFICATION);
assertNotNull(createdXmlWorkflowItem);
log.debug("created XmlWorkflowItem: {}", createdXmlWorkflowItem);
}
@Test
public void addMetadataToItem() {
String randomString = UUID.randomUUID().toString();
MetadataEntry metadatum1 = new MetadataEntry();
metadatum1.setKey("dc.contributor.author");
metadatum1.setValue("Editor, Dummy " + randomString);
MetadataEntry metadatum2 = new MetadataEntry();
metadatum2.setKey("dc.title.alternative");
metadatum2.setLanguage("de");
metadatum2.setValue("Deutsch " + randomString);
List<MetadataEntry> requestedNewMetadata = Arrays.asList(metadatum1, metadatum2);
Item itemSpecification = new Item();
itemSpecification.setId(39921);
itemSpecification.setMetadata(requestedNewMetadata);
itemSpecification.setExpand(Arrays.asList("all"));
ssoarRestService.addMetadataToItem(itemSpecification);
Item actualUpdatedItem = ssoarRestService.getItem(itemSpecification);
assertNotNull(actualUpdatedItem);
List<MetadataEntry> actualUpdatedMetadata = actualUpdatedItem.getMetadata();
assertTrue( actualUpdatedMetadata.containsAll(requestedNewMetadata) );
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment