Search in sources :

Example 76 with ContentItem

use of ddf.catalog.content.data.ContentItem in project ddf by codice.

the class FileSystemStorageProviderTest method submitAndVerifySuccessfulUpdateStorageRequest.

private void submitAndVerifySuccessfulUpdateStorageRequest(ContentItem... requestContentItems) throws Exception {
    final UpdateStorageRequest updateStorageRequest = new UpdateStorageRequestImpl(Arrays.asList(requestContentItems), null);
    final UpdateStorageResponse updateStorageResponse = provider.update(updateStorageRequest);
    final List<ContentItem> responseContentItems = updateStorageResponse.getUpdatedContentItems();
    assertThat("Expect number of ContentItems in UpdateStorageResponse", responseContentItems, hasSize(requestContentItems.length));
    for (final ContentItem responseContentItem : responseContentItems) {
        // assert file exists
        final URI uri = new URI(responseContentItem.getUri());
        final List<String> parts = provider.getContentFilePathParts(uri.getSchemeSpecificPart(), uri.getFragment());
        final String expectedFilePath = baseDir + File.separator + FileSystemStorageProvider.DEFAULT_CONTENT_REPOSITORY + File.separator + FileSystemStorageProvider.DEFAULT_CONTENT_STORE + File.separator + FileSystemStorageProvider.DEFAULT_TMP + File.separator + updateStorageRequest.getId() + File.separator + parts.get(0) + File.separator + parts.get(1) + File.separator + parts.get(2) + (StringUtils.isNotBlank(responseContentItem.getQualifier()) ? File.separator + responseContentItem.getQualifier() : "") + File.separator + responseContentItem.getFilename();
        assertThat("Expect file exists at " + expectedFilePath, Files.exists(Paths.get(expectedFilePath)));
        // assert metacard attributes set
        final ArgumentCaptor<Attribute> captor = ArgumentCaptor.forClass(Attribute.class);
        final Metacard metacard = responseContentItem.getMetacard();
        if (StringUtils.isBlank(responseContentItem.getQualifier())) {
            verify(metacard, times(2)).setAttribute(captor.capture());
            Attribute resourceUriAttribute = captor.getAllValues().get(0);
            assertThat(resourceUriAttribute.getName(), is(Metacard.RESOURCE_URI));
            assertThat(resourceUriAttribute.getValue(), is(uri.toString()));
            Attribute resourceSizeAttribute = captor.getAllValues().get(1);
            assertThat(resourceSizeAttribute.getName(), is(Metacard.RESOURCE_SIZE));
            assertThat(resourceSizeAttribute.getValue(), is(responseContentItem.getSize()));
        } else {
            verify(metacard, never()).setAttribute(any());
        }
    }
    provider.commit(updateStorageRequest);
    for (ContentItem responseContentItem : responseContentItems) {
        assertReadRequest(responseContentItem.getUri(), responseContentItem.getMimeType().toString());
    }
}
Also used : Metacard(ddf.catalog.data.Metacard) UpdateStorageRequestImpl(ddf.catalog.content.operation.impl.UpdateStorageRequestImpl) Attribute(ddf.catalog.data.Attribute) UpdateStorageResponse(ddf.catalog.content.operation.UpdateStorageResponse) UpdateStorageRequest(ddf.catalog.content.operation.UpdateStorageRequest) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) URI(java.net.URI) ContentItem(ddf.catalog.content.data.ContentItem)

Example 77 with ContentItem

use of ddf.catalog.content.data.ContentItem in project ddf by codice.

the class FileSystemStorageProviderTest method testUpdate.

@Test
public void testUpdate() throws Exception {
    CreateStorageResponse createResponse = assertContentItem(TEST_INPUT_CONTENTS, NITF_MIME_TYPE, TEST_INPUT_FILENAME);
    String id = createResponse.getCreatedContentItems().get(0).getId();
    ByteSource byteSource = new ByteSource() {

        @Override
        public InputStream openStream() throws IOException {
            return IOUtils.toInputStream("Updated NITF", StandardCharsets.UTF_8);
        }
    };
    ContentItem updateItem = new ContentItemImpl(id, byteSource, NITF_MIME_TYPE, mock(Metacard.class));
    submitAndVerifySuccessfulUpdateStorageRequest(updateItem);
}
Also used : CreateStorageResponse(ddf.catalog.content.operation.CreateStorageResponse) Metacard(ddf.catalog.data.Metacard) ByteSource(com.google.common.io.ByteSource) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) ContentItem(ddf.catalog.content.data.ContentItem) ContentItemImpl(ddf.catalog.content.data.impl.ContentItemImpl) Test(org.junit.Test)

Example 78 with ContentItem

use of ddf.catalog.content.data.ContentItem in project ddf by codice.

the class FileSystemStorageProviderTest method testDeleteWithSimilarIds.

@Test
public void testDeleteWithSimilarIds() throws Exception {
    CreateStorageResponse createResponse = assertContentItem(TEST_INPUT_CONTENTS, NITF_MIME_TYPE, TEST_INPUT_FILENAME);
    String id = createResponse.getCreatedContentItems().get(0).getId();
    String uuid = UUID.randomUUID().toString().replaceAll("-", "");
    String badId = id.substring(0, 6) + uuid.substring(6, uuid.length() - 1);
    boolean hadError = false;
    try {
        CreateStorageResponse badCreateResponse = assertContentItemWithQualifier(TEST_INPUT_CONTENTS, NITF_MIME_TYPE, TEST_INPUT_FILENAME, badId, "");
    } catch (AssertionError e) {
        // bad id is not a valid ID
        hadError = true;
    } finally {
        if (!hadError) {
            fail("Create succeeded when it should not have! " + badId + "Should not be valid!");
        }
        hadError = false;
    }
    DeleteStorageRequest deleteRequest = new DeleteStorageRequestImpl(createResponse.getCreatedContentItems().stream().map(ContentItem::getMetacard).collect(Collectors.toList()), null);
    when(deleteRequest.getMetacards().get(0).getId()).thenReturn(id);
    DeleteStorageResponse deleteResponse = provider.delete(deleteRequest);
    List<ContentItem> items = deleteResponse.getDeletedContentItems();
    ContentItem item = items.get(0);
    LOGGER.debug("Item retrieved: {}", item);
    assertEquals(id, item.getId());
    assertThat(item.getFilename(), is(""));
    provider.commit(deleteRequest);
    try {
        assertReadRequest(createResponse.getCreatedContentItems().get(0).getUri(), NITF_MIME_TYPE);
    } catch (StorageException e) {
        // The item was deleted so it shouldn't have found it
        hadError = true;
    } finally {
        if (!hadError) {
            fail("read succeeded when it should not have! ");
        }
    }
}
Also used : DeleteStorageRequest(ddf.catalog.content.operation.DeleteStorageRequest) CreateStorageResponse(ddf.catalog.content.operation.CreateStorageResponse) DeleteStorageRequestImpl(ddf.catalog.content.operation.impl.DeleteStorageRequestImpl) DeleteStorageResponse(ddf.catalog.content.operation.DeleteStorageResponse) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) StorageException(ddf.catalog.content.StorageException) ContentItem(ddf.catalog.content.data.ContentItem) Test(org.junit.Test)

Example 79 with ContentItem

use of ddf.catalog.content.data.ContentItem in project ddf by codice.

the class FileSystemStorageProviderTest method assertContentItemWithQualifier.

public CreateStorageResponse assertContentItemWithQualifier(String data, String mimeTypeRawData, String filename, String id, String qualifier, Map<String, Serializable> properties) throws Exception {
    // Simulates what ContentFrameworkImpl would do
    String uuid = StringUtils.defaultIfBlank(id, UUID.randomUUID().toString().replaceAll("-", ""));
    ByteSource byteSource = new ByteSource() {

        @Override
        public InputStream openStream() throws IOException {
            return IOUtils.toInputStream(data, StandardCharsets.UTF_8);
        }
    };
    ContentItem contentItem = new ContentItemImpl(uuid, qualifier, byteSource, mimeTypeRawData, filename, byteSource.size(), mock(Metacard.class));
    CreateStorageRequest createRequest = new CreateStorageRequestImpl(Collections.singletonList(contentItem), properties);
    CreateStorageResponse createResponse = provider.create(createRequest);
    List<ContentItem> createdContentItems = createResponse.getCreatedContentItems();
    ContentItem createdContentItem = createdContentItems.isEmpty() ? null : createdContentItems.get(0);
    assertNotNull(createdContentItem);
    String createdId = createdContentItem.getId();
    assertNotNull(createdId);
    assertThat(createdId, equalTo(uuid));
    String contentUri = createdContentItem.getUri();
    LOGGER.debug("contentUri = {}", contentUri);
    assertNotNull(contentUri);
    String expectedContentUri = ContentItem.CONTENT_SCHEME + ":" + uuid + ((StringUtils.isNotBlank(qualifier)) ? "#" + qualifier : "");
    assertThat(contentUri, equalTo(expectedContentUri));
    assertTrue(createdContentItem.getSize() > 0);
    String createdMimeType = createdContentItem.getMimeTypeRawData().replace(";", "");
    List<String> createdMimeTypeArr = new ArrayList<>(Arrays.asList(createdMimeType.split(" ")));
    List<String> givenMimeTypeArr = new ArrayList<>(Arrays.asList(mimeTypeRawData.replace(";", "").split(" ")));
    assertEquals(createdMimeTypeArr.size(), givenMimeTypeArr.size());
    givenMimeTypeArr.removeAll(createdMimeTypeArr);
    assertThat(givenMimeTypeArr.size(), is(0));
    provider.commit(createRequest);
    return createResponse;
}
Also used : CreateStorageResponse(ddf.catalog.content.operation.CreateStorageResponse) Metacard(ddf.catalog.data.Metacard) CreateStorageRequestImpl(ddf.catalog.content.operation.impl.CreateStorageRequestImpl) ArrayList(java.util.ArrayList) ByteSource(com.google.common.io.ByteSource) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) ContentItem(ddf.catalog.content.data.ContentItem) ContentItemImpl(ddf.catalog.content.data.impl.ContentItemImpl) CreateStorageRequest(ddf.catalog.content.operation.CreateStorageRequest)

Example 80 with ContentItem

use of ddf.catalog.content.data.ContentItem in project ddf by codice.

the class FileSystemStorageProviderTest method testUpdateWithQualifier.

@Test
public void testUpdateWithQualifier() throws Exception {
    CreateStorageResponse createResponse = assertContentItemWithQualifier(TEST_INPUT_CONTENTS, NITF_MIME_TYPE, TEST_INPUT_FILENAME, "", QUALIFIER);
    String id = createResponse.getCreatedContentItems().get(0).getId();
    ByteSource byteSource = new ByteSource() {

        @Override
        public InputStream openStream() throws IOException {
            return IOUtils.toInputStream("Updated NITF", StandardCharsets.UTF_8);
        }
    };
    ContentItem updateItem = new ContentItemImpl(id, QUALIFIER, byteSource, NITF_MIME_TYPE, mock(Metacard.class));
    submitAndVerifySuccessfulUpdateStorageRequest(updateItem);
}
Also used : CreateStorageResponse(ddf.catalog.content.operation.CreateStorageResponse) Metacard(ddf.catalog.data.Metacard) ByteSource(com.google.common.io.ByteSource) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) ContentItem(ddf.catalog.content.data.ContentItem) ContentItemImpl(ddf.catalog.content.data.impl.ContentItemImpl) Test(org.junit.Test)

Aggregations

ContentItem (ddf.catalog.content.data.ContentItem)92 Test (org.junit.Test)51 Metacard (ddf.catalog.data.Metacard)44 ContentItemImpl (ddf.catalog.content.data.impl.ContentItemImpl)31 CreateStorageResponse (ddf.catalog.content.operation.CreateStorageResponse)30 ArrayList (java.util.ArrayList)29 IOException (java.io.IOException)21 HashMap (java.util.HashMap)21 ByteSource (com.google.common.io.ByteSource)20 URI (java.net.URI)20 UpdateStorageRequest (ddf.catalog.content.operation.UpdateStorageRequest)17 CreateStorageRequestImpl (ddf.catalog.content.operation.impl.CreateStorageRequestImpl)16 Matchers.isEmptyString (org.hamcrest.Matchers.isEmptyString)16 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)15 InputStream (java.io.InputStream)14 StorageException (ddf.catalog.content.StorageException)13 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)13 UpdateStorageResponse (ddf.catalog.content.operation.UpdateStorageResponse)12 Map (java.util.Map)12 StorageProvider (ddf.catalog.content.StorageProvider)11