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

implement addBitstreamToItem

parent cb449215
......@@ -5,6 +5,7 @@ import java.util.List;
import javax.inject.Inject;
import org.gesis.dda.feeder.ssoar.domain.Bitstream;
import org.gesis.dda.feeder.ssoar.domain.Collection;
import org.gesis.dda.feeder.ssoar.domain.Item;
import org.gesis.dda.feeder.ssoar.domain.MetadataEntry;
......@@ -15,6 +16,7 @@ import org.gesis.dda.wizard.config.IngesterProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
......@@ -55,6 +57,10 @@ public class SsoarRestService {
return authenticationUser;
}
public String getEndpoint() {
return endpoint;
}
public boolean login() {
isAuthenticated = false;
......@@ -157,4 +163,18 @@ public class SsoarRestService {
replaceAllMetadataOfItem(updatedItem);
}
public Bitstream addBitstreamToItem(Item itemSpecification, Resource resourceToBeUploaded, String filename) {
int itemId = itemSpecification.getId();
HttpHeaders headers = new HttpHeaders();
headers.set("rest-dspace-token", authenticationToken);
HttpEntity<?> resourceToBeUploadedHttpEntity = new HttpEntity<>(resourceToBeUploaded, headers);
ResponseEntity<Bitstream> addedBitstreamResponseEntity = restTemplate.exchange( endpoint + "/items/{itemId}/bitstreams", HttpMethod.POST, resourceToBeUploadedHttpEntity, Bitstream.class, itemId );
Bitstream addedBitstream = addedBitstreamResponseEntity.getBody();
log.debug("Bitstream: {}", addedBitstream);
return addedBitstream;
}
}
......@@ -8,7 +8,10 @@ import static org.junit.Assert.assertTrue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.core.IsNot.not;
import static org.hamcrest.beans.HasPropertyWithValue.hasProperty;
import static org.hamcrest.core.IsEqual.equalTo;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
......@@ -17,6 +20,8 @@ import java.util.UUID;
import javax.inject.Inject;
import org.apache.commons.io.IOUtils;
import org.gesis.dda.feeder.ssoar.domain.Bitstream;
import org.gesis.dda.feeder.ssoar.domain.Collection;
import org.gesis.dda.feeder.ssoar.domain.Item;
import org.gesis.dda.feeder.ssoar.domain.MetadataEntry;
......@@ -30,6 +35,9 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.transaction.annotation.Transactional;
......@@ -133,7 +141,7 @@ public class SsoarRestServiceIntTest {
public void removeMetadataFromItem() {
Item itemSpecification = new Item();
itemSpecification.setId(39939);
itemSpecification.setExpand(Arrays.asList("all"));
itemSpecification.setExpand( Arrays.asList("all") );
Item beforeManipulationItem = ssoarRestService.getItem(itemSpecification);
List<MetadataEntry> beforeUpdateMetadataEntry = beforeManipulationItem.getMetadata();
......@@ -169,4 +177,35 @@ public class SsoarRestServiceIntTest {
int maxMustermannContributorOccurencesAfterRemoval = Collections.frequency(afterRemovalMetadata, toBeRemovedMetadatum);
assertEquals(0, maxMustermannContributorOccurencesAfterRemoval);
}
@Test
public void addBitstreamToItem() throws IOException {
String randomUuidString = UUID.randomUUID().toString();
Item itemSpecification = new Item();
itemSpecification.setId(39939);
itemSpecification.setExpand( Arrays.asList("all") );
Item beforeItem = ssoarRestService.getItem(itemSpecification);
List<Bitstream> beforeBitstreams = beforeItem.getBitstreams();
int beforeBitstreamsCount = beforeBitstreams.size();
String uniqueFileName = "some file " + randomUuidString + ".pdf";
assertThat( beforeBitstreams, not( hasItem( hasProperty("Name", equalTo(uniqueFileName) ) ) ) );
Resource originalPdfResource = new ClassPathResource("Bundeskabinett_PDF.pdf");
Bitstream addedBitstream = ssoarRestService.addBitstreamToItem(itemSpecification, originalPdfResource, uniqueFileName);
log.debug("Added bitstream: {}", addedBitstream);
Item afterItem = ssoarRestService.getItem(itemSpecification);
List<Bitstream> afterBitstreams = afterItem.getBitstreams();
int afterBitstreamsCount = afterBitstreams.size();
assertEquals(beforeBitstreamsCount + 1, afterBitstreamsCount);
String createdPdfRetrieveLink = ssoarRestService.getEndpoint() + addedBitstream.getRetrieveLink();
Resource createdPdfResource = new UrlResource(createdPdfRetrieveLink);
boolean pdfsAreEqual = IOUtils.contentEquals( originalPdfResource.getInputStream(), createdPdfResource.getInputStream() );
assertTrue(pdfsAreEqual);
}
}
Markdown is supported
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