Search in sources :

Example 1 with DefaultAttributeValueRegistry

use of ddf.catalog.data.DefaultAttributeValueRegistry in project ddf by codice.

the class TestCatalog method testDefaultValuesCreate.

@Test
// DDF-2743
@ConditionalIgnore(condition = SkipUnstableTest.class)
public void testDefaultValuesCreate() throws Exception {
    final String customMetacardTypeName = "custom";
    File file = ingestDefinitionJsonWithWaitCondition("defaults.json", () -> {
        expect("Service to be available: " + MetacardType.class.getName()).within(30, TimeUnit.SECONDS).until(() -> getServiceManager().getServiceReferences(MetacardType.class, "(name=" + customMetacardTypeName + ")"), not(empty()));
        return null;
    });
    String metacard1Xml = getFileContent("metacard1.xml");
    String metacard2Xml = getFileContent("metacard2.xml");
    metacard2Xml = metacard2Xml.replaceFirst("ddf\\.metacard", customMetacardTypeName);
    verifyMetacardDoesNotContainAttribute(metacard1Xml, Metacard.DESCRIPTION);
    verifyMetacardDoesNotContainAttribute(metacard1Xml, Metacard.EXPIRATION);
    verifyMetacardDoesNotContainAttribute(metacard2Xml, Metacard.DESCRIPTION);
    verifyMetacardDoesNotContainAttribute(metacard2Xml, Metacard.EXPIRATION);
    final String id1 = ingest(metacard1Xml, MediaType.APPLICATION_XML);
    final String id2 = ingest(metacard2Xml, MediaType.APPLICATION_XML);
    try {
        final String defaultDescription = "Default description";
        final String defaultCustomMetacardDescription = "Default custom description";
        final String defaultExpiration = getDefaultExpirationAsString();
        final String metacard1XPath = format(METACARD_X_PATH, id1);
        final String metacard2XPath = format(METACARD_X_PATH, id2);
        executeOpenSearch("xml", "q=*").log().all().assertThat().body(hasXPath(metacard1XPath + "/string[@name='title']/value", is("Metacard-1"))).body(hasXPath(metacard1XPath + "/string[@name='description']/value", is(defaultDescription))).body(hasXPath(metacard1XPath + "/dateTime[@name='expiration']/value", is(defaultExpiration))).body(hasXPath(metacard2XPath + "/string[@name='title']/value", is("Metacard-2"))).body(hasXPath(metacard2XPath + "/string[@name='description']/value", is(defaultCustomMetacardDescription))).body(hasXPath(metacard2XPath + "/dateTime[@name='expiration']/value", is(defaultExpiration)));
    } finally {
        deleteMetacard(id1);
        deleteMetacard(id2);
        uninstallDefinitionJson(file, () -> {
            DefaultAttributeValueRegistry defaultsRegistry = getServiceManager().getService(DefaultAttributeValueRegistry.class);
            expect("Defaults to be unregistered").within(10, TimeUnit.SECONDS).until(() -> !defaultsRegistry.getDefaultValue(customMetacardTypeName, Metacard.DESCRIPTION).isPresent());
            return null;
        });
    }
}
Also used : CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) File(java.io.File) MetacardType(ddf.catalog.data.MetacardType) DefaultAttributeValueRegistry(ddf.catalog.data.DefaultAttributeValueRegistry) ConditionalIgnore(org.codice.ddf.itests.common.annotations.ConditionalIgnoreRule.ConditionalIgnore) SkipUnstableTest(org.codice.ddf.itests.common.annotations.SkipUnstableTest) Test(org.junit.Test) AbstractIntegrationTest(org.codice.ddf.itests.common.AbstractIntegrationTest)

Example 2 with DefaultAttributeValueRegistry

use of ddf.catalog.data.DefaultAttributeValueRegistry in project ddf by codice.

the class OperationsMetacardSupport method setDefaultValues.

/**
     * Updates any empty metacard attributes with those defined in the
     * {@link DefaultAttributeValueRegistry}.
     *
     * @param metacard the metacard to update with default attribute values
     */
void setDefaultValues(Metacard metacard) {
    MetacardType metacardType = metacard.getMetacardType();
    DefaultAttributeValueRegistry registry = frameworkProperties.getDefaultAttributeValueRegistry();
    metacardType.getAttributeDescriptors().stream().map(AttributeDescriptor::getName).filter(attributeName -> hasNoValue(metacard.getAttribute(attributeName))).forEach(attributeName -> {
        registry.getDefaultValue(metacardType.getName(), attributeName).ifPresent(defaultValue -> metacard.setAttribute(new AttributeImpl(attributeName, defaultValue)));
    });
}
Also used : DefaultProbDetector(org.apache.tika.detect.DefaultProbDetector) StringUtils(org.apache.commons.lang.StringUtils) ContentItemImpl(ddf.catalog.content.data.impl.ContentItemImpl) LoggerFactory(org.slf4j.LoggerFactory) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) HashMap(java.util.HashMap) MediaType(org.apache.tika.mime.MediaType) DefaultAttributeValueRegistry(ddf.catalog.data.DefaultAttributeValueRegistry) StandardCopyOption(java.nio.file.StandardCopyOption) Metadata(org.apache.tika.metadata.Metadata) Charset(java.nio.charset.Charset) ContentItem(ddf.catalog.content.data.ContentItem) Metacard(ddf.catalog.data.Metacard) Map(java.util.Map) TikaInputStream(org.apache.tika.io.TikaInputStream) Path(java.nio.file.Path) Logger(org.slf4j.Logger) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) Files(java.nio.file.Files) IngestException(ddf.catalog.source.IngestException) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Detector(org.apache.tika.detect.Detector) AttributeInjector(ddf.catalog.data.AttributeInjector) InputStreamReader(java.io.InputStreamReader) FrameworkProperties(ddf.catalog.impl.FrameworkProperties) MimeTypeResolutionException(ddf.mime.MimeTypeResolutionException) MetacardType(ddf.catalog.data.MetacardType) List(java.util.List) Attribute(ddf.catalog.data.Attribute) InputValidation(org.codice.ddf.platform.util.InputValidation) BufferedReader(java.io.BufferedReader) FilenameUtils(org.apache.commons.io.FilenameUtils) InputStream(java.io.InputStream) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) MetacardType(ddf.catalog.data.MetacardType) DefaultAttributeValueRegistry(ddf.catalog.data.DefaultAttributeValueRegistry)

Example 3 with DefaultAttributeValueRegistry

use of ddf.catalog.data.DefaultAttributeValueRegistry in project ddf by codice.

the class TestCatalog method testDefaultValuesUpdate.

@Test
// DDF-2743
@ConditionalIgnore(condition = SkipUnstableTest.class)
public void testDefaultValuesUpdate() throws Exception {
    final String customMetacardTypeName = "custom";
    File file = ingestDefinitionJsonWithWaitCondition("defaults.json", () -> {
        expect("Service to be available: " + MetacardType.class.getName()).within(30, TimeUnit.SECONDS).until(() -> getServiceManager().getServiceReferences(MetacardType.class, "(name=" + customMetacardTypeName + ")"), not(empty()));
        return null;
    });
    String metacard1Xml = getFileContent("metacard1.xml");
    final String id1 = ingest(metacard1Xml, MediaType.APPLICATION_XML);
    String metacard2Xml = getFileContent("metacard2.xml");
    metacard2Xml = metacard2Xml.replaceFirst("ddf\\.metacard", customMetacardTypeName);
    final String id2 = ingest(metacard2Xml, MediaType.APPLICATION_XML);
    try {
        final String updatedTitle1 = "Metacard-1 (Updated)";
        final String updatedTitle2 = "Metacard-2 (Updated)";
        metacard1Xml = metacard1Xml.replaceFirst("Metacard\\-1", updatedTitle1);
        metacard2Xml = metacard2Xml.replaceFirst("Metacard\\-2", updatedTitle2);
        verifyMetacardDoesNotContainAttribute(metacard1Xml, Metacard.DESCRIPTION);
        verifyMetacardDoesNotContainAttribute(metacard1Xml, Metacard.EXPIRATION);
        verifyMetacardDoesNotContainAttribute(metacard2Xml, Metacard.DESCRIPTION);
        verifyMetacardDoesNotContainAttribute(metacard2Xml, Metacard.EXPIRATION);
        update(id1, metacard1Xml, MediaType.APPLICATION_XML);
        update(id2, metacard2Xml, MediaType.APPLICATION_XML);
        final String defaultDescription = "Default description";
        final String defaultCustomMetacardDescription = "Default custom description";
        final String defaultExpiration = getDefaultExpirationAsString();
        final String metacard1XPath = format(METACARD_X_PATH, id1);
        final String metacard2XPath = format(METACARD_X_PATH, id2);
        executeOpenSearch("xml", "q=*").log().all().assertThat().body(hasXPath(metacard1XPath + "/string[@name='title']/value", is(updatedTitle1))).body(hasXPath(metacard1XPath + "/string[@name='description']/value", is(defaultDescription))).body(hasXPath(metacard1XPath + "/dateTime[@name='expiration']/value", is(defaultExpiration))).body(hasXPath(metacard2XPath + "/string[@name='title']/value", is(updatedTitle2))).body(hasXPath(metacard2XPath + "/string[@name='description']/value", is(defaultCustomMetacardDescription))).body(hasXPath(metacard2XPath + "/dateTime[@name='expiration']/value", is(defaultExpiration)));
    } finally {
        deleteMetacard(id1);
        deleteMetacard(id2);
        uninstallDefinitionJson(file, () -> {
            DefaultAttributeValueRegistry defaultsRegistry = getServiceManager().getService(DefaultAttributeValueRegistry.class);
            expect("Defaults to be unregistered").within(10, TimeUnit.SECONDS).until(() -> !defaultsRegistry.getDefaultValue(customMetacardTypeName, Metacard.DESCRIPTION).isPresent());
            return null;
        });
    }
}
Also used : CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) File(java.io.File) MetacardType(ddf.catalog.data.MetacardType) DefaultAttributeValueRegistry(ddf.catalog.data.DefaultAttributeValueRegistry) ConditionalIgnore(org.codice.ddf.itests.common.annotations.ConditionalIgnoreRule.ConditionalIgnore) SkipUnstableTest(org.codice.ddf.itests.common.annotations.SkipUnstableTest) Test(org.junit.Test) AbstractIntegrationTest(org.codice.ddf.itests.common.AbstractIntegrationTest)

Aggregations

DefaultAttributeValueRegistry (ddf.catalog.data.DefaultAttributeValueRegistry)3 MetacardType (ddf.catalog.data.MetacardType)3 File (java.io.File)2 AbstractIntegrationTest (org.codice.ddf.itests.common.AbstractIntegrationTest)2 ConditionalIgnore (org.codice.ddf.itests.common.annotations.ConditionalIgnoreRule.ConditionalIgnore)2 SkipUnstableTest (org.codice.ddf.itests.common.annotations.SkipUnstableTest)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 Test (org.junit.Test)2 ContentItem (ddf.catalog.content.data.ContentItem)1 ContentItemImpl (ddf.catalog.content.data.impl.ContentItemImpl)1 Attribute (ddf.catalog.data.Attribute)1 AttributeDescriptor (ddf.catalog.data.AttributeDescriptor)1 AttributeInjector (ddf.catalog.data.AttributeInjector)1 Metacard (ddf.catalog.data.Metacard)1 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)1 FrameworkProperties (ddf.catalog.impl.FrameworkProperties)1 IngestException (ddf.catalog.source.IngestException)1 MimeTypeResolutionException (ddf.mime.MimeTypeResolutionException)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1