private void getRecords( final int maxRecords, final int startPosition, final int totalResults, final int expectedNext, final int expectedReturn) throws JAXBException, UnsupportedEncodingException { XStream xstream = createXStream(CswConstants.GET_RECORDS_RESPONSE); GetRecordsType query = new GetRecordsType(); query.setMaxRecords(BigInteger.valueOf(maxRecords)); query.setStartPosition(BigInteger.valueOf(startPosition)); CswRecordCollection collection = createCswRecordCollection(query, totalResults); collection.setStartPosition(startPosition); String xml = xstream.toXML(collection); JAXBElement<GetRecordsResponseType> jaxb = (JAXBElement<GetRecordsResponseType>) getJaxBContext() .createUnmarshaller() .unmarshal(new ByteArrayInputStream(xml.getBytes("UTF-8"))); GetRecordsResponseType response = jaxb.getValue(); assertThat( response.getSearchResults().getNumberOfRecordsMatched().intValue(), equalTo(totalResults)); assertThat( response.getSearchResults().getNumberOfRecordsReturned().intValue(), equalTo(expectedReturn)); // assertThat(response.getSearchResults().getAbstractRecord().size(), // equalTo(expectedReturn)); assertThat(response.getSearchResults().getNextRecord().intValue(), equalTo(expectedNext)); }
@Ignore public void testMarshalRecordCollectionGetElements() throws UnsupportedEncodingException, JAXBException { final int totalResults = 5; XStream xstream = createXStream(CswConstants.GET_RECORDS_RESPONSE); GetRecordsType getRecords = new GetRecordsType(); QueryType query = new QueryType(); List<QName> elements = new LinkedList<QName>(); elements.add(CswRecordMetacardType.CSW_TITLE_QNAME); elements.add(CswRecordMetacardType.CSW_SOURCE_QNAME); query.setElementName(elements); ObjectFactory objectFactory = new ObjectFactory(); getRecords.setAbstractQuery(objectFactory.createAbstractQuery(query)); CswRecordCollection collection = createCswRecordCollection(getRecords, totalResults); collection.setElementName(elements); ArgumentCaptor<MarshallingContext> captor = ArgumentCaptor.forClass(MarshallingContext.class); String xml = xstream.toXML(collection); // Verify the context arguments were set correctly verify(mockProvider, times(totalResults)) .marshal(any(Object.class), any(HierarchicalStreamWriter.class), captor.capture()); MarshallingContext context = captor.getValue(); assertThat(context, not(nullValue())); assertThat( (String) context.get(CswConstants.OUTPUT_SCHEMA_PARAMETER), is(CswConstants.CSW_OUTPUT_SCHEMA)); assertThat((ElementSetType) context.get(CswConstants.ELEMENT_SET_TYPE), is(nullValue())); assertThat(context.get(CswConstants.ELEMENT_NAMES), is(notNullValue())); List<QName> qnames = (List<QName>) context.get(CswConstants.ELEMENT_NAMES); assertThat(qnames.contains(CswRecordMetacardType.CSW_TITLE_QNAME), is(true)); assertThat(qnames.contains(CswRecordMetacardType.CSW_SOURCE_QNAME), is(true)); JAXBElement<GetRecordsResponseType> jaxb = (JAXBElement<GetRecordsResponseType>) getJaxBContext() .createUnmarshaller() .unmarshal(new ByteArrayInputStream(xml.getBytes("UTF-8"))); GetRecordsResponseType response = jaxb.getValue(); // Assert the GetRecordsResponse elements and attributes assertThat(response, not(nullValue())); SearchResultsType resultsType = response.getSearchResults(); assertThat(resultsType, not(nullValue())); assertThat(resultsType.getElementSet(), is(nullValue())); assertThat(resultsType.getNumberOfRecordsMatched().intValue(), is(totalResults)); assertThat(resultsType.getNumberOfRecordsReturned().intValue(), is(totalResults)); assertThat(resultsType.getRecordSchema(), is(CswConstants.CSW_OUTPUT_SCHEMA)); }
@Ignore public void testMarshalRecordCollectionFullXml() throws UnsupportedEncodingException, JAXBException { final int totalResults = 5; TransformerManager mockMetacardManager = mock(TransformerManager.class); when(mockMetacardManager.getTransformerBySchema(anyString())) .thenReturn(new CswRecordConverter()); GetRecordsResponseConverter rrConverter = new GetRecordsResponseConverter(new CswTransformProvider(mockMetacardManager, null)); XStream xstream = new XStream(new StaxDriver(new NoNameCoder())); xstream.registerConverter(rrConverter); xstream.alias( CswConstants.CSW_NAMESPACE_PREFIX + CswConstants.NAMESPACE_DELIMITER + CswConstants.GET_RECORDS_RESPONSE, CswRecordCollection.class); GetRecordsType getRecords = new GetRecordsType(); QueryType query = new QueryType(); ElementSetNameType set = new ElementSetNameType(); set.setValue(ElementSetType.FULL); query.setElementSetName(set); ObjectFactory objectFactory = new ObjectFactory(); getRecords.setAbstractQuery(objectFactory.createAbstractQuery(query)); CswRecordCollection collection = createCswRecordCollection(getRecords, totalResults); collection.setElementSetType(ElementSetType.FULL); String xml = xstream.toXML(collection); JAXBElement<GetRecordsResponseType> jaxb = (JAXBElement<GetRecordsResponseType>) getJaxBContext() .createUnmarshaller() .unmarshal(new ByteArrayInputStream(xml.getBytes("UTF-8"))); GetRecordsResponseType response = jaxb.getValue(); // Assert the GetRecordsResponse elements and attributes assertThat(response, not(nullValue())); SearchResultsType resultsType = response.getSearchResults(); assertThat(resultsType, not(nullValue())); assertThat(resultsType.getElementSet(), is(ElementSetType.FULL)); assertThat(resultsType.getNumberOfRecordsMatched().intValue(), is(totalResults)); assertThat(resultsType.getNumberOfRecordsReturned().intValue(), is(totalResults)); assertThat(resultsType.getRecordSchema(), is(CswConstants.CSW_OUTPUT_SCHEMA)); }
@Ignore public void testMarshalRecordCollectionHits() throws UnsupportedEncodingException, JAXBException { final int totalResults = 5; XStream xstream = createXStream(CswConstants.GET_RECORDS_RESPONSE); GetRecordsType getRecords = new GetRecordsType(); QueryType query = new QueryType(); ElementSetNameType set = new ElementSetNameType(); set.setValue(ElementSetType.FULL); query.setElementSetName(set); ObjectFactory objectFactory = new ObjectFactory(); getRecords.setAbstractQuery(objectFactory.createAbstractQuery(query)); CswRecordCollection collection = createCswRecordCollection(getRecords, totalResults); collection.setElementSetType(ElementSetType.FULL); collection.setResultType(ResultType.HITS); String xml = xstream.toXML(collection); // Verify the context arguments were set correctly verify(mockProvider, never()) .marshal( any(Object.class), any(HierarchicalStreamWriter.class), any(MarshallingContext.class)); JAXBElement<GetRecordsResponseType> jaxb = (JAXBElement<GetRecordsResponseType>) getJaxBContext() .createUnmarshaller() .unmarshal(new ByteArrayInputStream(xml.getBytes("UTF-8"))); GetRecordsResponseType response = jaxb.getValue(); // Assert the GetRecordsResponse elements and attributes assertThat(response, not(nullValue())); SearchResultsType resultsType = response.getSearchResults(); assertThat(resultsType, not(nullValue())); assertThat(resultsType.getElementSet(), is(ElementSetType.FULL)); assertThat(resultsType.getNumberOfRecordsMatched().intValue(), is(totalResults)); assertThat(resultsType.getNumberOfRecordsReturned().intValue(), is(0)); assertThat(resultsType.getRecordSchema(), is(CswConstants.CSW_OUTPUT_SCHEMA)); }