use of ddf.catalog.data.AttributeDescriptor in project ddf by codice.
the class SolrProviderTest method testNumericSort.
@Test
public void testNumericSort() throws Exception {
deleteAllIn(provider);
/* SETUP */
String doubleField = "hertz";
double doubleFieldValue = 16065.435;
String floatField = "inches";
float floatFieldValue = 4.435f;
String intField = "count";
int intFieldValue = 4;
String longField = "milliseconds";
long longFieldValue = 9876543293L;
String shortField = "daysOfTheWeek";
short shortFieldValue = 1;
int factor = 5;
List<Metacard> list = new ArrayList<Metacard>();
DateTime now = new DateTime();
for (int i = 0; i < 5; i++) {
Set<AttributeDescriptor> descriptors = numericalDescriptors(doubleField, floatField, intField, longField, shortField);
MetacardTypeImpl mType = new MetacardTypeImpl("numberMetacardType", descriptors);
MetacardImpl customMetacard1 = new MetacardImpl(mType);
customMetacard1.setAttribute(Metacard.ID, "");
customMetacard1.setAttribute(doubleField, doubleFieldValue + factor * i);
customMetacard1.setAttribute(floatField, floatFieldValue + factor * i);
customMetacard1.setAttribute(intField, intFieldValue + factor * i);
customMetacard1.setAttribute(longField, longFieldValue + factor * i);
customMetacard1.setAttribute(shortField, shortFieldValue + factor * i);
list.add(customMetacard1);
}
create(list);
Filter filter = null;
QueryImpl query = null;
SourceResponse sourceResponse = null;
// Sort all Double ASCENDING
filter = filterBuilder.attribute(Metacard.ANY_TEXT).like().text("*");
query = new QueryImpl(filter);
query.setSortBy(new ddf.catalog.filter.impl.SortByImpl(doubleField, SortOrder.ASCENDING.name()));
sourceResponse = provider.query(new QueryRequestImpl(query));
assertEquals(list.size(), sourceResponse.getResults().size());
for (int i = 0; i < list.size(); i++) {
Result r = sourceResponse.getResults().get(i);
assertEquals(doubleFieldValue + factor * i, r.getMetacard().getAttribute(doubleField).getValue());
}
// Sort all double DESCENDING
query.setSortBy(new ddf.catalog.filter.impl.SortByImpl(doubleField, SortOrder.DESCENDING.name()));
sourceResponse = provider.query(new QueryRequestImpl(query));
assertEquals(list.size(), sourceResponse.getResults().size());
int counter = 0;
for (int i = (list.size() - 1); i >= 0; i--) {
Result r = sourceResponse.getResults().get(i);
assertEquals(doubleFieldValue + factor * counter, r.getMetacard().getAttribute(doubleField).getValue());
counter++;
}
// Sort all Float ASCENDING
query.setSortBy(new ddf.catalog.filter.impl.SortByImpl(floatField, SortOrder.ASCENDING.name()));
sourceResponse = provider.query(new QueryRequestImpl(query));
assertEquals(list.size(), sourceResponse.getResults().size());
for (int i = 0; i < list.size(); i++) {
Result r = sourceResponse.getResults().get(i);
assertEquals(floatFieldValue + factor * i, r.getMetacard().getAttribute(floatField).getValue());
}
// Sort all Float DESCENDING
query.setSortBy(new ddf.catalog.filter.impl.SortByImpl(floatField, SortOrder.DESCENDING.name()));
sourceResponse = provider.query(new QueryRequestImpl(query));
assertEquals(list.size(), sourceResponse.getResults().size());
counter = 0;
for (int i = (list.size() - 1); i >= 0; i--) {
Result r = sourceResponse.getResults().get(i);
assertEquals(floatFieldValue + factor * counter, r.getMetacard().getAttribute(floatField).getValue());
counter++;
}
// Sort all int ASCENDING
query.setSortBy(new ddf.catalog.filter.impl.SortByImpl(intField, SortOrder.ASCENDING.name()));
sourceResponse = provider.query(new QueryRequestImpl(query));
assertEquals(list.size(), sourceResponse.getResults().size());
for (int i = 0; i < list.size(); i++) {
Result r = sourceResponse.getResults().get(i);
assertEquals(intFieldValue + factor * i, r.getMetacard().getAttribute(intField).getValue());
}
// Sort all int DESCENDING
query.setSortBy(new ddf.catalog.filter.impl.SortByImpl(intField, SortOrder.DESCENDING.name()));
sourceResponse = provider.query(new QueryRequestImpl(query));
assertEquals(list.size(), sourceResponse.getResults().size());
counter = 0;
for (int i = (list.size() - 1); i >= 0; i--) {
Result r = sourceResponse.getResults().get(i);
assertEquals(intFieldValue + factor * counter, r.getMetacard().getAttribute(intField).getValue());
counter++;
}
// Sort all long ASCENDING
query.setSortBy(new ddf.catalog.filter.impl.SortByImpl(longField, SortOrder.ASCENDING.name()));
sourceResponse = provider.query(new QueryRequestImpl(query));
assertEquals(list.size(), sourceResponse.getResults().size());
for (int i = 0; i < list.size(); i++) {
Result r = sourceResponse.getResults().get(i);
assertEquals(longFieldValue + factor * i, r.getMetacard().getAttribute(longField).getValue());
}
// Sort all long DESCENDING
query.setSortBy(new ddf.catalog.filter.impl.SortByImpl(longField, SortOrder.DESCENDING.name()));
sourceResponse = provider.query(new QueryRequestImpl(query));
assertEquals(list.size(), sourceResponse.getResults().size());
counter = 0;
for (int i = (list.size() - 1); i >= 0; i--) {
Result r = sourceResponse.getResults().get(i);
assertEquals(longFieldValue + factor * counter, r.getMetacard().getAttribute(longField).getValue());
counter++;
}
// Sort all short ASCENDING
query.setSortBy(new ddf.catalog.filter.impl.SortByImpl(shortField, SortOrder.ASCENDING.name()));
sourceResponse = provider.query(new QueryRequestImpl(query));
assertEquals(list.size(), sourceResponse.getResults().size());
for (int i = 0; i < list.size(); i++) {
Result r = sourceResponse.getResults().get(i);
assertEquals((short) (shortFieldValue + factor * i), r.getMetacard().getAttribute(shortField).getValue());
}
// Sort all short DESCENDING
query.setSortBy(new ddf.catalog.filter.impl.SortByImpl(shortField, SortOrder.DESCENDING.name()));
sourceResponse = provider.query(new QueryRequestImpl(query));
assertEquals(list.size(), sourceResponse.getResults().size());
counter = 0;
for (int i = (list.size() - 1); i >= 0; i--) {
Result r = sourceResponse.getResults().get(i);
assertEquals((short) (shortFieldValue + factor * counter), r.getMetacard().getAttribute(shortField).getValue());
counter++;
}
}
use of ddf.catalog.data.AttributeDescriptor in project ddf by codice.
the class CachedResourceMetacardComparator method allMetacardAttributesEqual.
private static boolean allMetacardAttributesEqual(Metacard cachedMetacard, Metacard updatedMetacard) {
MetacardType cachedMetacardType = cachedMetacard.getMetacardType();
MetacardType updatedMetacardType = updatedMetacard.getMetacardType();
if (!Objects.equals(cachedMetacardType, updatedMetacardType)) {
return false;
}
// attributes need to be compared so we can return true.
if (cachedMetacardType == null) {
return true;
}
Set<AttributeDescriptor> cachedDescriptors = cachedMetacardType.getAttributeDescriptors();
// as well and no attributes need to be compared so we can return true.
if (cachedDescriptors == null) {
return true;
}
Optional<String> difference = cachedDescriptors.stream().map(AttributeDescriptor::getName).filter(attributeName -> !ATTRIBUTES_TO_IGNORE.contains(attributeName)).filter(attributeName -> !Objects.equals(cachedMetacard.getAttribute(attributeName), updatedMetacard.getAttribute(attributeName))).findFirst();
if (LOGGER.isDebugEnabled() && difference.isPresent()) {
String attributeName = difference.get();
LOGGER.debug("Metacard updated. Attribute changed: {}, cached value: {}, updated value: {}", attributeName, cachedMetacard.getAttribute(attributeName), updatedMetacard.getAttribute(attributeName));
}
return !difference.isPresent();
}
use of ddf.catalog.data.AttributeDescriptor in project ddf by codice.
the class SolrProviderTest method testNumericalOperations.
@Test()
public void testNumericalOperations() throws Exception {
deleteAllIn(provider);
/* SETUP */
String doubleField = "hertz";
double doubleFieldValue = 16065.435;
String floatField = "inches";
float floatFieldValue = 4.435f;
String intField = "count";
int intFieldValue = 4;
String longField = "milliseconds";
long longFieldValue = 9876543293L;
String shortField = "daysOfTheWeek";
short shortFieldValue = 1;
Set<AttributeDescriptor> descriptors = numericalDescriptors(doubleField, floatField, intField, longField, shortField);
MetacardTypeImpl mType = new MetacardTypeImpl("anotherCustom", descriptors);
MetacardImpl customMetacard1 = new MetacardImpl(mType);
customMetacard1.setAttribute(Metacard.ID, "");
customMetacard1.setAttribute(doubleField, doubleFieldValue);
customMetacard1.setAttribute(floatField, floatFieldValue);
customMetacard1.setAttribute(intField, intFieldValue);
customMetacard1.setAttribute(longField, longFieldValue);
customMetacard1.setAttribute(shortField, shortFieldValue);
MetacardImpl customMetacard2 = new MetacardImpl(mType);
customMetacard2.setAttribute(Metacard.ID, "");
customMetacard2.setAttribute(doubleField, doubleFieldValue + 10.0);
customMetacard2.setAttribute(floatField, (floatFieldValue + 10.0f));
customMetacard2.setAttribute(intField, intFieldValue + 1);
customMetacard2.setAttribute(longField, longFieldValue + 10L);
customMetacard2.setAttribute(shortField, (shortFieldValue + 1));
create(Arrays.asList((Metacard) customMetacard1, customMetacard2));
// on exact DOUBLE
greaterThanQueryAssertion(doubleField, doubleFieldValue, 1);
greaterThanOrEqualToQueryAssertion(doubleField, doubleFieldValue, 2);
// beyond the DOUBLE
greaterThanQueryAssertion(doubleField, doubleFieldValue - 0.00000001, 2);
greaterThanOrEqualToQueryAssertion(doubleField, doubleFieldValue - 0.00000001, 2);
greaterThanQueryAssertion(doubleField, doubleFieldValue + 12.0, 0);
greaterThanOrEqualToQueryAssertion(doubleField, doubleFieldValue + 12.0, 0);
// on exact FLOAT
greaterThanQueryAssertion(floatField, floatFieldValue, 1);
greaterThanOrEqualToQueryAssertion(floatField, floatFieldValue, 2);
// beyond the FLOAT
greaterThanQueryAssertion(floatField, floatFieldValue - 0.00001f, 2);
greaterThanOrEqualToQueryAssertion(floatField, floatFieldValue - 0.00001f, 2);
greaterThanQueryAssertion(floatField, floatFieldValue + 12.0f, 0);
greaterThanOrEqualToQueryAssertion(floatField, floatFieldValue + 12.0f, 0);
// on exact LONG
greaterThanQueryAssertion(longField, longFieldValue, 1);
greaterThanOrEqualToQueryAssertion(longField, longFieldValue, 2);
// beyond the LONG
greaterThanQueryAssertion(longField, longFieldValue - 1L, 2);
greaterThanOrEqualToQueryAssertion(longField, longFieldValue - 1L, 2);
greaterThanQueryAssertion(longField, longFieldValue + 12L, 0);
greaterThanOrEqualToQueryAssertion(longField, longFieldValue + 12L, 0);
// on exact INT
greaterThanQueryAssertion(intField, intFieldValue, 1);
greaterThanOrEqualToQueryAssertion(intField, intFieldValue, 2);
// beyond the INT
greaterThanQueryAssertion(intField, intFieldValue - 1, 2);
greaterThanOrEqualToQueryAssertion(intField, intFieldValue - 1, 2);
greaterThanQueryAssertion(intField, intFieldValue + 2, 0);
greaterThanOrEqualToQueryAssertion(intField, intFieldValue + 2, 0);
// on exact SHORT
greaterThanQueryAssertion(shortField, shortFieldValue, 1);
greaterThanOrEqualToQueryAssertion(shortField, shortFieldValue, 2);
// beyond the SHORT
greaterThanQueryAssertion(shortField, (short) (shortFieldValue - 1), 2);
greaterThanOrEqualToQueryAssertion(shortField, (short) (shortFieldValue - 1), 2);
greaterThanQueryAssertion(shortField, (short) (shortFieldValue + 2), 0);
greaterThanOrEqualToQueryAssertion(shortField, (short) (shortFieldValue + 2), 0);
}
use of ddf.catalog.data.AttributeDescriptor in project ddf by codice.
the class CatalogFrameworkImplTest method testInjectsAttributesOnDelete.
@Test
public void testInjectsAttributesOnDelete() throws Exception {
final String title = "Delete this";
final String injectAttributeName = "new attribute";
final double injectAttributeValue = 11.1;
final MetacardImpl metacard = new MetacardImpl();
metacard.setTitle(title);
metacard.setAttribute(injectAttributeName, injectAttributeValue);
final String id = framework.create(new CreateRequestImpl(Collections.singletonList(metacard), null)).getCreatedMetacards().get(0).getId();
final DeleteRequest request = new DeleteRequestImpl(id);
final AttributeDescriptor injectAttribute = new AttributeDescriptorImpl(injectAttributeName, true, true, false, false, BasicTypes.DOUBLE_TYPE);
stubMetacardInjection(injectAttribute);
List<Result> mockFederationResults = Stream.of(metacard).map(m -> {
Result mockResult = mock(Result.class);
when(mockResult.getMetacard()).thenReturn(m);
return mockResult;
}).collect(Collectors.toList());
QueryResponseImpl queryResponse = new QueryResponseImpl(mock(QueryRequest.class), mockFederationResults, 1);
when(mockFederationStrategy.federate(anyList(), anyObject())).thenReturn(queryResponse);
when(mockRemoteDeleteOperations.performRemoteDelete(any(), any())).then(returnsSecondArg());
deleteOperations.setRemoteDeleteOperations(mockRemoteDeleteOperations);
final DeleteResponse response = framework.delete(request);
final Metacard deletedMetacard = response.getDeletedMetacards().get(0);
final MetacardType originalMetacardType = metacard.getMetacardType();
final MetacardType deletedMetacardType = deletedMetacard.getMetacardType();
assertThat(deletedMetacardType.getName(), is(originalMetacardType.getName()));
final Set<AttributeDescriptor> expectedAttributeDescriptors = new HashSet<>(originalMetacardType.getAttributeDescriptors());
expectedAttributeDescriptors.add(injectAttribute);
assertThat(deletedMetacardType.getAttributeDescriptors(), is(expectedAttributeDescriptors));
assertThat(deletedMetacard.getTitle(), is(title));
assertThat(deletedMetacard.getAttribute(injectAttributeName).getValue(), is(injectAttributeValue));
}
use of ddf.catalog.data.AttributeDescriptor in project ddf by codice.
the class HandlebarsMetacard method getAttributes.
public Set<AttributeEntry> getAttributes() {
MetacardType metacardType = this.getMetacardType();
Set<AttributeEntry> attributes = new TreeSet<>();
for (AttributeDescriptor descriptor : metacardType.getAttributeDescriptors()) {
Attribute attr = this.getAttribute(descriptor.getName());
if (attr != null) {
attributes.add(new AttributeEntry(attr, descriptor.getType().getAttributeFormat()));
}
}
return attributes;
}
Aggregations