use of org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.DeleteActionImpl in project ddf by codice.
the class CswEndpointTest method testDeleteBatching.
@Test
public void testDeleteBatching() throws Exception {
// configure query responses
queryResponseBatch = getQueryResponseBatch(500, 800);
QueryResponse[] qrRest = queryResponseBatch.subList(1, queryResponseBatch.size()).toArray(new QueryResponse[0]);
when(catalogFramework.query(any(QueryRequest.class))).thenReturn(queryResponseBatch.get(0), qrRest);
// configure delete responses
DeleteType deleteType = mock(DeleteType.class);
doReturn(CswConstants.CSW_RECORD).when(deleteType).getTypeName();
doReturn("").when(deleteType).getHandle();
QueryConstraintType queryConstraintType = new QueryConstraintType();
queryConstraintType.setCqlText("title = \"foo\"");
doReturn(queryConstraintType).when(deleteType).getConstraint();
when(deleteResponse.getDeletedMetacards()).thenReturn(populateMetacardList(1));
when(catalogFramework.delete(any(DeleteRequest.class))).thenAnswer((Answer<DeleteResponse>) invocation -> deleteResponse);
DeleteAction deleteAction = new DeleteActionImpl(deleteType, DefaultCswRecordMap.getPrefixToUriMapping());
CswTransactionRequest deleteRequest = new CswTransactionRequest();
deleteRequest.getDeleteActions().add(deleteAction);
TransactionResponseType response = csw.transaction(deleteRequest);
assertThat(response.getTransactionSummary().getTotalDeleted().intValue(), equalTo(800));
verify(catalogFramework, times(4)).query(any());
verify(catalogFramework, times(800)).delete(any());
}
use of org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.DeleteActionImpl in project ddf by codice.
the class TransactionRequestConverterTest method testValidDeleteMarshal.
@Test
public void testValidDeleteMarshal() throws SAXException, IOException, XpathException {
CswTransactionRequest transactionRequest = new CswTransactionRequest();
MetacardImpl metacard = new MetacardImpl();
metacard.setId(METACARD_ID);
DeleteType deleteType = new DeleteType();
QueryConstraintType queryConstraintType = new QueryConstraintType();
queryConstraintType.setCqlText("identifier = " + METACARD_ID);
deleteType.setConstraint(queryConstraintType);
DeleteAction deleteAction = new DeleteActionImpl(deleteType, null);
transactionRequest.getDeleteActions().add(deleteAction);
transactionRequest.setService(CswConstants.CSW);
transactionRequest.setVerbose(true);
transactionRequest.setVersion(CswConstants.VERSION_2_0_2);
String xml = xStream.toXML(transactionRequest);
Diff diff = XMLUnit.compareXML(xml, EXPECTED_DELETE_XML);
assertThat(diff.similar(), is(true));
}
use of org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.DeleteActionImpl in project ddf by codice.
the class TransactionRequestConverterTest method testMultipleOperations.
@Test
public void testMultipleOperations() throws Exception {
CswTransactionRequest transactionRequest = new CswTransactionRequest();
MetacardImpl metacard = new MetacardImpl();
metacard.setId(METACARD_ID);
transactionRequest.setService(CswConstants.CSW);
transactionRequest.setVerbose(true);
transactionRequest.setVersion(CswConstants.VERSION_2_0_2);
InsertAction insertAction = new InsertActionImpl(CswConstants.CSW_METACARD_TYPE_NAME, null, Arrays.asList(metacard));
transactionRequest.getInsertActions().add(insertAction);
UpdateAction updateAction = new UpdateActionImpl(metacard, CswConstants.CSW_METACARD_TYPE_NAME, null);
transactionRequest.getUpdateActions().add(updateAction);
DeleteType deleteType = new DeleteType();
QueryConstraintType queryConstraintType = new QueryConstraintType();
queryConstraintType.setCqlText("identifier = " + METACARD_ID);
deleteType.setConstraint(queryConstraintType);
DeleteAction deleteAction = new DeleteActionImpl(deleteType, null);
transactionRequest.getDeleteActions().add(deleteAction);
String xml = xStream.toXML(transactionRequest);
Diff diff = XMLUnit.compareXML(xml, EXPECTED_MULTI_OP_XML);
assertThat(diff.similar(), is(true));
}
use of org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.DeleteActionImpl in project ddf by codice.
the class TransactionRequestConverter method unmarshal.
@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
CswTransactionRequest cswTransactionRequest = new CswTransactionRequest();
cswTransactionRequest.setVersion(reader.getAttribute(CswConstants.VERSION));
cswTransactionRequest.setService(reader.getAttribute(CswConstants.SERVICE));
cswTransactionRequest.setVerbose(Boolean.valueOf(reader.getAttribute(CswConstants.VERBOSE_RESPONSE)));
XStreamAttributeCopier.copyXmlNamespaceDeclarationsIntoContext(reader, context);
while (reader.hasMoreChildren()) {
reader.moveDown();
if (reader.getNodeName().contains("Insert")) {
String typeName = StringUtils.defaultIfEmpty(reader.getAttribute(CswConstants.TYPE_NAME_PARAMETER), CswConstants.CSW_RECORD);
String handle = StringUtils.defaultIfEmpty(reader.getAttribute(CswConstants.HANDLE_PARAMETER), "");
context.put(CswConstants.TRANSFORMER_LOOKUP_KEY, TransformerManager.ID);
context.put(CswConstants.TRANSFORMER_LOOKUP_VALUE, typeName);
List<Metacard> metacards = new ArrayList<>();
// Loop through the individual records to be inserted, converting each into a Metacard
while (reader.hasMoreChildren()) {
// move down to the record's tag
reader.moveDown();
Metacard metacard = (Metacard) context.convertAnother(null, MetacardImpl.class, delegatingTransformer);
if (metacard != null) {
metacards.add(metacard);
}
// move back up to the <SearchResults> parent of the <csw:Record> tags
reader.moveUp();
}
cswTransactionRequest.getInsertActions().add(new InsertActionImpl(typeName, handle, metacards));
} else if (reader.getNodeName().contains("Delete")) {
XStreamAttributeCopier.copyXmlNamespaceDeclarationsIntoContext(reader, context);
Map<String, String> xmlnsAttributeToUriMappings = getXmlnsAttributeToUriMappingsFromContext(context);
Map<String, String> prefixToUriMappings = getPrefixToUriMappingsFromXmlnsAttributes(xmlnsAttributeToUriMappings);
StringWriter writer = new StringWriter();
XStreamAttributeCopier.copyXml(reader, writer, xmlnsAttributeToUriMappings);
DeleteType deleteType = getElementFromXml(writer.toString(), DeleteType.class);
cswTransactionRequest.getDeleteActions().add(new DeleteActionImpl(deleteType, prefixToUriMappings));
} else if (reader.getNodeName().contains("Update")) {
XStreamAttributeCopier.copyXmlNamespaceDeclarationsIntoContext(reader, context);
UpdateAction updateAction = parseUpdateAction(reader, context);
cswTransactionRequest.getUpdateActions().add(updateAction);
}
reader.moveUp();
}
return cswTransactionRequest;
}
use of org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.DeleteActionImpl in project ddf by codice.
the class AbstractCswStore method delete.
@Override
public DeleteResponse delete(DeleteRequest deleteRequest) throws IngestException {
Map<String, Serializable> properties = new HashMap<>();
validateOperation();
Subject subject = (Subject) deleteRequest.getPropertyValue(SecurityConstants.SECURITY_SUBJECT);
Csw csw = factory.getClientForSubject(subject);
CswTransactionRequest transactionRequest = getTransactionRequest();
OperationTransaction opTrans = (OperationTransaction) deleteRequest.getPropertyValue(Constants.OPERATION_TRANSACTION_KEY);
String typeName = schemaTransformerManager.getTransformerIdForSchema(cswSourceConfiguration.getOutputSchema());
if (typeName == null) {
typeName = CswConstants.CSW_RECORD;
}
for (Serializable itemToDelete : deleteRequest.getAttributeValues()) {
try {
DeleteType deleteType = new DeleteType();
deleteType.setTypeName(typeName);
QueryConstraintType queryConstraintType = new QueryConstraintType();
Filter filter;
FilterType filterType;
filter = filterBuilder.attribute(deleteRequest.getAttributeName()).is().equalTo().text(itemToDelete.toString());
filterType = filterAdapter.adapt(filter, cswFilterDelegate);
queryConstraintType.setCqlText(CswCqlTextFilter.getInstance().getCqlText(filterType));
deleteType.setConstraint(queryConstraintType);
DeleteAction deleteAction = new DeleteActionImpl(deleteType, DefaultCswRecordMap.getPrefixToUriMapping());
transactionRequest.getDeleteActions().add(deleteAction);
} catch (UnsupportedQueryException e) {
throw new IngestException("Unsupported Query.", e);
}
}
try {
TransactionResponseType response = csw.transaction(transactionRequest);
if (response.getTransactionSummary().getTotalDeleted().intValue() != deleteRequest.getAttributeValues().size()) {
throw new IngestException("Csw Transaction Failed. Number of metacards deleted did not match number requested.");
}
} catch (CswException e) {
throw new IngestException("Csw Transaction Failed", e);
}
return new DeleteResponseImpl(deleteRequest, properties, new ArrayList(opTrans.getPreviousStateMetacards()));
}
Aggregations