use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection in project ddf by codice.
the class CswRecordCollectionMessageBodyWriterTest method testWriteToWithSchema.
@Test
public void testWriteToWithSchema() throws WebApplicationException, IOException, JAXBException, CatalogTransformerException {
CswRecordCollectionMessageBodyWriter writer = new CswRecordCollectionMessageBodyWriter(mockManager);
when(mockManager.getTransformerBySchema(anyString())).thenReturn(mockTransformer);
ArgumentCaptor<Map> captor = ArgumentCaptor.forClass(Map.class);
when(mockTransformer.transform(any(SourceResponse.class), any(Map.class))).thenReturn(mockContent);
when(mockContent.getInputStream()).thenReturn(new ByteArrayInputStream("bytes".getBytes()));
CswRecordCollection collection = createCswRecordCollection(6);
collection.setNumberOfRecordsMatched(22);
collection.setNumberOfRecordsReturned(6);
final String exampleSchema = "http://example.com/schema";
collection.setOutputSchema(exampleSchema);
collection.setById(true);
collection.setResultType(ResultType.HITS);
collection.setSourceResponse(mock(SourceResponse.class));
QName example = new QName("example");
collection.setElementName(Arrays.asList(example));
collection.setElementSetType(ElementSetType.BRIEF);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
writer.writeTo(collection, null, null, null, null, null, stream);
verify(mockTransformer).transform(any(SourceResponse.class), captor.capture());
Map arguments = captor.getValue();
assertThat(arguments.get(CswConstants.WRITE_NAMESPACES), is(false));
assertThat(arguments.get(CswConstants.OUTPUT_SCHEMA_PARAMETER), is(exampleSchema));
assertThat(arguments.get(CswConstants.RESULT_TYPE_PARAMETER), is(ResultType.HITS));
assertThat(arguments.get(CswConstants.IS_BY_ID_QUERY), is(true));
assertThat(arguments.get(CswConstants.ELEMENT_SET_TYPE), is(ElementSetType.BRIEF));
assertThat(((Object[]) arguments.get(CswConstants.ELEMENT_NAMES))[0], is(example));
}
use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection in project ddf by codice.
the class CswRecordCollectionMessageBodyWriterTest method testWriteToWithMimeType.
@Test
public void testWriteToWithMimeType() throws WebApplicationException, IOException, JAXBException, CatalogTransformerException {
CswRecordCollectionMessageBodyWriter writer = new CswRecordCollectionMessageBodyWriter(mockManager);
when(mockManager.getTransformerByMimeType(any(String.class))).thenReturn(mockTransformer);
when(mockTransformer.transform(any(SourceResponse.class), any(Map.class))).thenReturn(mockContent);
when(mockContent.getInputStream()).thenReturn(new ByteArrayInputStream("bytes".getBytes()));
CswRecordCollection collection = createCswRecordCollection(6);
collection.setNumberOfRecordsMatched(22);
collection.setNumberOfRecordsReturned(6);
collection.setById(true);
collection.setResultType(ResultType.RESULTS);
collection.setMimeType(MediaType.APPLICATION_JSON);
collection.setSourceResponse(mock(SourceResponse.class));
QName example = new QName("example");
collection.setElementName(Arrays.asList(example));
collection.setElementSetType(ElementSetType.BRIEF);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
writer.writeTo(collection, null, null, null, null, null, stream);
verify(mockManager, times(1)).getTransformerByMimeType(any(String.class));
// TODO - assert lookup by mime type
// TODO failure case
}
use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection in project ddf by codice.
the class CswEndpointTest method testPostGetRecordsResults.
@Test
public void testPostGetRecordsResults() throws CswException, UnsupportedQueryException, SourceUnavailableException, FederationException {
GetRecordsType grr = createDefaultPostRecordsRequest();
grr.setResultType(ResultType.RESULTS);
QueryType query = new QueryType();
List<QName> typeNames = new ArrayList<>();
typeNames.add(new QName(CswConstants.CSW_OUTPUT_SCHEMA, VALID_TYPE, VALID_PREFIX));
query.setTypeNames(typeNames);
QueryConstraintType constraint = new QueryConstraintType();
constraint.setCqlText(CQL_CONTEXTUAL_LIKE_QUERY);
query.setConstraint(constraint);
ElementSetNameType esnt = new ElementSetNameType();
esnt.setValue(ElementSetType.SUMMARY);
query.setElementSetName(esnt);
JAXBElement<QueryType> jaxbQuery = new JAXBElement<>(cswQnameOutPutSchema, QueryType.class, query);
grr.setAbstractQuery(jaxbQuery);
final String exampleSchema = CswConstants.CSW_OUTPUT_SCHEMA;
grr.setOutputSchema(exampleSchema);
final String exampleMime = "application/xml";
grr.setOutputFormat(exampleMime);
CswRecordCollection collection = csw.getRecords(grr);
assertThat(collection.getMimeType(), is(exampleMime));
assertThat(collection.getOutputSchema(), is(exampleSchema));
assertThat(collection.getSourceResponse(), notNullValue());
assertThat(collection.getResultType(), is(ResultType.RESULTS));
assertThat(collection.getElementSetType(), is(ElementSetType.SUMMARY));
}
use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection in project ddf by codice.
the class CswEndpoint method getRecordById.
@Override
@POST
@Consumes({ MediaType.TEXT_XML, MediaType.APPLICATION_XML })
@Produces({ MediaType.TEXT_XML, MediaType.APPLICATION_XML })
public CswRecordCollection getRecordById(GetRecordByIdType request, @HeaderParam(CswConstants.RANGE_HEADER) String rangeValue) throws CswException {
if (request == null) {
throw new CswException("GetRecordByIdRequest request is null");
}
String outputFormat = request.getOutputFormat();
String outputSchema = request.getOutputSchema();
validator.validateOutputFormat(outputFormat, mimeTypeTransformerManager);
validator.validateOutputSchema(outputSchema, schemaTransformerManager);
List<String> ids = request.getId();
if (!ids.isEmpty()) {
String id = ids.get(0);
// Check if the request wants to retrieve a product.
if (isProductRetrieval(ids, outputFormat, outputSchema)) {
LOGGER.debug("{} is attempting to retrieve product for: {}", request.getService(), id);
try {
return queryProductById(id, rangeValue);
} catch (UnsupportedQueryException e) {
throw new CswException(String.format(ERROR_ID_PRODUCT_RETRIEVAL, id), e);
}
}
LOGGER.debug("{} is attempting to retrieve records: {}", request.getService(), ids);
CswRecordCollection response = queryById(ids, outputSchema);
response.setOutputSchema(outputSchema);
if (request.isSetElementSetName() && request.getElementSetName().getValue() != null) {
response.setElementSetType(request.getElementSetName().getValue());
} else {
response.setElementSetType(ElementSetType.SUMMARY);
}
LOGGER.debug("{} successfully retrieved record(s): {}", request.getService(), request.getId());
return response;
} else {
throw new CswException("A GetRecordById Query must contain an ID.", CswConstants.MISSING_PARAMETER_VALUE, "id");
}
}
use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection in project ddf by codice.
the class CswEndpointTest method testPostRetrieveProductGetRecordByIdWithNoMimeType.
@Test
public void testPostRetrieveProductGetRecordByIdWithNoMimeType() throws IOException, ResourceNotFoundException, ResourceNotSupportedException, CswException {
final GetRecordByIdType getRecordByIdType = new GetRecordByIdType();
getRecordByIdType.setOutputFormat(MediaType.APPLICATION_OCTET_STREAM);
getRecordByIdType.setOutputSchema(OCTET_STREAM_OUTPUT_SCHEMA);
getRecordByIdType.setId(Collections.singletonList("123"));
setUpMocksForProductRetrieval(false);
CswRecordCollection cswRecordCollection = csw.getRecordById(getRecordByIdType, null);
assertThat(cswRecordCollection.getResource(), is(notNullValue()));
assertThat(cswRecordCollection.getResource().getMimeType().toString(), is(MediaType.APPLICATION_OCTET_STREAM));
}
Aggregations