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));
  }
示例#3
0
  public SendEvent(
      TransformerManager transformerManager, GetRecordsType request, QueryRequest query)
      throws CswException {

    URI deliveryMethodUrl;
    if (request.getResponseHandler() != null && !request.getResponseHandler().isEmpty()) {

      try {
        deliveryMethodUrl = new URI(request.getResponseHandler().get(0));
      } catch (URISyntaxException e) {
        throw new CswException("Invalid ResponseHandler URL", e);
      }
    } else {
      String msg = "Subscriptions require a ResponseHandler URL to be specified";
      LOGGER.error(msg);
      throw new CswException(msg);
    }
    this.transformerManager = transformerManager;
    this.query = query;
    this.callbackUrl = deliveryMethodUrl;
    this.request = request;
    this.outputSchema = request.getOutputSchema();
    this.mimeType = request.getOutputFormat();
    QueryType queryType = (QueryType) request.getAbstractQuery().getValue();
    this.elementName = queryType.getElementName();
    this.elementSetType =
        (queryType.getElementSetName() != null) ? queryType.getElementSetName().getValue() : null;
    this.resultType = request.getResultType() == null ? ResultType.HITS : request.getResultType();
    SecureCxfClientFactory<CswSubscribe> cxfClientFactory =
        new SecureCxfClientFactory<>(callbackUrl.toString(), CswSubscribe.class);
    webClient = cxfClientFactory.getWebClient();
  }
示例#4
0
  @Before
  public void setUp() throws Exception {
    System.setProperty("ddf.home", ".");
    callbackURI = new URL("https://localhost:12345/services/csw/subscription/event");
    ObjectFactory objectFactory = new ObjectFactory();
    request = new GetRecordsType();
    request.setOutputSchema(CswConstants.CSW_OUTPUT_SCHEMA);
    request.setResultType(ResultType.RESULTS);
    request.getResponseHandler().add(callbackURI.toString());
    queryType = new QueryType();
    elementSetNameType = new ElementSetNameType();
    elementSetNameType.setValue(ElementSetType.BRIEF);
    queryType.setElementSetName(elementSetNameType);
    request.setAbstractQuery(objectFactory.createAbstractQuery(queryType));
    transformerManager = mock(TransformerManager.class);
    transformer = mock(QueryResponseTransformer.class);
    binaryContent = mock(BinaryContent.class);
    when(transformerManager.getTransformerBySchema(
            Matchers.contains(CswConstants.CSW_OUTPUT_SCHEMA)))
        .thenReturn(transformer);
    when(transformer.transform(any(SourceResponse.class), anyMap())).thenReturn(binaryContent);
    when(binaryContent.getByteArray()).thenReturn("byte array with message contents".getBytes());
    query = mock(QueryRequest.class);

    metacard = mock(Metacard.class);
    webclient = mock(WebClient.class);
    mockCxfClientFactory = mock(SecureCxfClientFactory.class);
    response = mock(Response.class);
    subject = mock(Subject.class);

    mockSecurity = mock(Security.class);
    headers.put(Subject.class.toString(), Arrays.asList(new Subject[] {subject}));
    AccessPlugin accessPlugin = mock(AccessPlugin.class);
    accessPlugins.add(accessPlugin);
    when(mockCxfClientFactory.getWebClient()).thenReturn(webclient);
    //        when(webclient.head()).thenReturn(response);
    when(webclient.invoke(anyString(), any(QueryResponse.class))).thenReturn(response);
    when(response.getHeaders()).thenReturn(headers);
    when(accessPlugin.processPostQuery(any(QueryResponse.class)))
        .thenAnswer(
            new Answer<QueryResponse>() {
              @Override
              public QueryResponse answer(InvocationOnMock invocationOnMock) throws Throwable {
                return (QueryResponse) invocationOnMock.getArguments()[0];
              }
            });

    sendEvent = new SendEventExtension(transformerManager, request, query, mockCxfClientFactory);
    sendEvent.setSubject(subject);
  }
  @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));
  }
  private CswRecordCollection createCswRecordCollection(GetRecordsType request, int resultCount) {
    CswRecordCollection collection = new CswRecordCollection();

    int first = 1;
    int last = 2;
    if (request != null) {

      first = request.getStartPosition().intValue();
      int next = request.getMaxRecords().intValue() + first;
      last = next - 1;
      if (last >= resultCount) {
        last = resultCount;
        next = 0;
      }
    }
    int returned = last - first + 1;

    collection.setCswRecords(createMetacardList(first, last));
    collection.setNumberOfRecordsMatched(resultCount);
    collection.setNumberOfRecordsReturned(returned);
    //        collection.setRequest(request);
    return collection;
  }