@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));
  }
Exemplo n.º 2
0
  private Response sendEvent(String operation, Metacard... metacards) {
    try {
      List<Result> results =
          Arrays.asList(metacards).stream().map(ResultImpl::new).collect(Collectors.toList());

      SourceResponse queryResponse = new QueryResponseImpl(query, results, true, metacards.length);

      LOGGER.debug(
          "Attempting to transform an Event with mime-type: {} & outputSchema: {}",
          mimeType,
          outputSchema);

      QueryResponseTransformer transformer;
      Map<String, Serializable> arguments = new HashMap<>();
      if (StringUtils.isBlank(outputSchema)
          && StringUtils.isNotBlank(mimeType)
          && !XML_MIME_TYPES.contains(mimeType)) {
        transformer = transformerManager.getTransformerByMimeType(mimeType);
      } else {
        transformer = transformerManager.getTransformerBySchema(CswConstants.CSW_OUTPUT_SCHEMA);
        if (elementName != null) {
          arguments.put(CswConstants.ELEMENT_NAMES, elementName.toArray());
        }
        arguments.put(CswConstants.OUTPUT_SCHEMA_PARAMETER, outputSchema);
        arguments.put(CswConstants.ELEMENT_SET_TYPE, elementSetType);
        arguments.put(CswConstants.IS_BY_ID_QUERY, false);
        arguments.put(CswConstants.GET_RECORDS, request);
        arguments.put(CswConstants.RESULT_TYPE_PARAMETER, resultType);
        arguments.put(CswConstants.WRITE_NAMESPACES, false);
      }
      if (transformer == null) {
        throw new WebApplicationException(
            new CatalogTransformerException("Unable to locate Transformer."));
      }

      BinaryContent binaryContent = transformer.transform(queryResponse, arguments);

      if (binaryContent == null) {
        throw new WebApplicationException(
            new CatalogTransformerException("Transformer returned null."));
      }

      return webClient.invoke(operation, binaryContent.getByteArray());
    } catch (IOException | CatalogTransformerException | RuntimeException e) {
      LOGGER.error("Error sending event to {}.", callbackUrl, e);
    }
    return null;
  }
  /**
   * This test acutally runs the full thread of calling the GetRecordsResponseConverter then calls
   * the CswInputTransformer.
   */
  @Test
  public void testUnmarshalGetRecordsResponseFull() {

    XStream xstream = new XStream(new WstxDriver());
    xstream.setClassLoader(this.getClass().getClassLoader());

    CswTransformProvider provider = new CswTransformProvider(null, mockInputManager);

    when(mockInputManager.getTransformerBySchema(anyString())).thenReturn(new CswRecordConverter());

    xstream.registerConverter(new GetRecordsResponseConverter(provider));
    xstream.alias("GetRecordsResponse", CswRecordCollection.class);

    String xml =
        "<csw:GetRecordsResponse xmlns:csw=\"http://www.opengis.net/cat/csw\">\r\n"
            + "  <csw:SearchStatus status=\"subset\" timestamp=\"2013-05-01T02:13:36+0200\"/>\r\n"
            + "  <csw:SearchResults elementSet=\"full\" nextRecord=\"11\" numberOfRecordsMatched=\"479\" numberOfRecordsReturned=\"10\" recordSchema=\"csw:Record\">\r\n"
            + "    <csw:Record xmlns:csw=\"http://www.opengis.net/cat/csw\">\r\n"
            + "      <dc:identifier xmlns:dc=\"http://purl.org/dc/elements/1.1/\">{8C1F6297-EC96-4302-A01E-14988C9149FD}</dc:identifier>\r\n"
            + "      <dc:title xmlns:dc=\"http://purl.org/dc/elements/1.1/\">title 1</dc:title>\r\n"
            + "      <dct:modified xmlns:dct=\"http://purl.org/dc/terms/\">2008-12-15</dct:modified>\r\n"
            + "      <dc:subject xmlns:dc=\"http://purl.org/dc/elements/1.1/\">subject 1</dc:subject>\r\n"
            + "      <dc:subject xmlns:dc=\"http://purl.org/dc/elements/1.1/\">second subject</dc:subject>\r\n"
            + "      <dct:abstract xmlns:dct=\"http://purl.org/dc/terms/\">abstract 1</dct:abstract>\r\n"
            + "      <dc:rights xmlns:dc=\"http://purl.org/dc/elements/1.1/\">copyright 1</dc:rights>\r\n"
            + "      <dc:rights xmlns:dc=\"http://purl.org/dc/elements/1.1/\">copyright 2</dc:rights>\r\n"
            + "      <dc:language xmlns:dc=\"http://purl.org/dc/elements/1.1/\">english</dc:language>      \r\n"
            + "      <ows:BoundingBox xmlns:ows=\"http://www.opengis.net/ows\" crs=\"EPSG:RD_New (28992)\">\r\n"
            + "        <ows:LowerCorner>5.121 52.139</ows:LowerCorner>\r\n"
            + "        <ows:UpperCorner>4.468 52.517</ows:UpperCorner>\r\n"
            + "      </ows:BoundingBox>   \r\n"
            + "      <dc:type xmlns:dc=\"http://purl.org/dc/elements/1.1/\">dataset</dc:type>\r\n"
            + "      <dc:format xmlns:dc=\"http://purl.org/dc/elements/1.1/\">Shapefile</dc:format> \r\n"
            + "    </csw:Record>\r\n"
            + "    <csw:Record xmlns:csw=\"http://www.opengis.net/cat/csw\">\r\n"
            + "      <dc:identifier xmlns:dc=\"http://purl.org/dc/elements/1.1/\">{23362852-F370-4369-B0B2-BE74B2859614}</dc:identifier>\r\n"
            + "      <dc:title xmlns:dc=\"http://purl.org/dc/elements/1.1/\">mc2 title</dc:title>\r\n"
            + "      <dct:modified xmlns:dct=\"http://purl.org/dc/terms/\">2010-12-15</dct:modified>\r\n"
            + "      <dc:subject xmlns:dc=\"http://purl.org/dc/elements/1.1/\">first subject</dc:subject>\r\n"
            + "      <dc:subject xmlns:dc=\"http://purl.org/dc/elements/1.1/\">subject 2</dc:subject>\r\n"
            + "      <dct:abstract xmlns:dct=\"http://purl.org/dc/terms/\">mc2 abstract</dct:abstract>\r\n"
            + "      <dc:rights xmlns:dc=\"http://purl.org/dc/elements/1.1/\">first copyright</dc:rights>\r\n"
            + "      <dc:rights xmlns:dc=\"http://purl.org/dc/elements/1.1/\">second copyright</dc:rights>\r\n"
            + "      <dc:language xmlns:dc=\"http://purl.org/dc/elements/1.1/\">english</dc:language>\r\n"
            + "      <ows:BoundingBox xmlns:ows=\"http://www.opengis.net/ows\" crs=\"EPSG:RD_New (28992)\">\r\n"
            + "        <ows:LowerCorner>6.121 53.139</ows:LowerCorner>\r\n"
            + "        <ows:UpperCorner>5.468 53.517</ows:UpperCorner>\r\n"
            + "      </ows:BoundingBox>\r\n"
            + "      <dc:type xmlns:dc=\"http://purl.org/dc/elements/1.1/\">dataset 2</dc:type>\r\n"
            + "      <dc:format xmlns:dc=\"http://purl.org/dc/elements/1.1/\">Shapefile 2</dc:format>\r\n"
            + "    </csw:Record>\r\n"
            + "  </csw:SearchResults>\r\n"
            + "</csw:GetRecordsResponse>";
    InputStream inStream = IOUtils.toInputStream(xml);
    CswRecordCollection cswRecords = (CswRecordCollection) xstream.fromXML(inStream);
    IOUtils.closeQuietly(inStream);

    List<Metacard> metacards = cswRecords.getCswRecords();
    assertThat(metacards, not(nullValue()));
    assertThat(metacards.size(), equalTo(2));

    // verify first metacard's values
    Metacard mc = metacards.get(0);
    assertThat(mc, not(nullValue()));
    Map<String, Object> expectedValues = new HashMap<String, Object>();
    expectedValues.put(Metacard.ID, "{8C1F6297-EC96-4302-A01E-14988C9149FD}");
    expectedValues.put(
        CswRecordMetacardType.CSW_IDENTIFIER,
        new String[] {"{8C1F6297-EC96-4302-A01E-14988C9149FD}"});
    expectedValues.put(Metacard.TITLE, "title 1");
    expectedValues.put(CswRecordMetacardType.CSW_TITLE, new String[] {"title 1"});
    String expectedModifiedDateStr = "2008-12-15";
    DateTimeFormatter dateFormatter = ISODateTimeFormat.dateOptionalTimeParser();
    Date expectedModifiedDate = dateFormatter.parseDateTime(expectedModifiedDateStr).toDate();
    expectedValues.put(CswRecordMetacardType.CSW_MODIFIED, new String[] {expectedModifiedDateStr});
    expectedValues.put(Metacard.MODIFIED, expectedModifiedDate);
    expectedValues.put(
        CswRecordMetacardType.CSW_SUBJECT, new String[] {"subject 1", "second subject"});
    expectedValues.put(CswRecordMetacardType.CSW_ABSTRACT, new String[] {"abstract 1"});
    expectedValues.put(
        CswRecordMetacardType.CSW_RIGHTS, new String[] {"copyright 1", "copyright 2"});
    expectedValues.put(CswRecordMetacardType.CSW_LANGUAGE, new String[] {"english"});
    expectedValues.put(CswRecordMetacardType.CSW_TYPE, "dataset");
    expectedValues.put(CswRecordMetacardType.CSW_FORMAT, new String[] {"Shapefile"});
    expectedValues.put(
        Metacard.GEOGRAPHY,
        "POLYGON((52.139 5.121, 52.517 5.121, 52.517 4.468, 52.139 4.468, 52.139 5.121))");
    expectedValues.put(
        CswRecordMetacardType.OWS_BOUNDING_BOX,
        new String[] {
          "POLYGON((52.139 5.121, 52.517 5.121, 52.517 4.468, 52.139 4.468, 52.139 5.121))"
        });
    assertMetacard(mc, expectedValues);

    expectedValues.clear();

    // verify second metacard's values
    mc = metacards.get(1);
    assertThat(mc, not(nullValue()));
    expectedValues = new HashMap<String, Object>();
    expectedValues.put(Metacard.ID, "{23362852-F370-4369-B0B2-BE74B2859614}");
    expectedValues.put(
        CswRecordMetacardType.CSW_IDENTIFIER,
        new String[] {"{23362852-F370-4369-B0B2-BE74B2859614}"});
    expectedValues.put(Metacard.TITLE, "mc2 title");
    expectedValues.put(CswRecordMetacardType.CSW_TITLE, new String[] {"mc2 title"});
    expectedModifiedDateStr = "2010-12-15";
    dateFormatter = ISODateTimeFormat.dateOptionalTimeParser();
    expectedModifiedDate = dateFormatter.parseDateTime(expectedModifiedDateStr).toDate();
    expectedValues.put(CswRecordMetacardType.CSW_MODIFIED, new String[] {expectedModifiedDateStr});
    expectedValues.put(Metacard.MODIFIED, expectedModifiedDate);
    expectedValues.put(
        CswRecordMetacardType.CSW_SUBJECT, new String[] {"first subject", "subject 2"});
    expectedValues.put(CswRecordMetacardType.CSW_ABSTRACT, new String[] {"mc2 abstract"});
    expectedValues.put(
        CswRecordMetacardType.CSW_RIGHTS, new String[] {"first copyright", "second copyright"});
    expectedValues.put(CswRecordMetacardType.CSW_LANGUAGE, new String[] {"english"});
    expectedValues.put(CswRecordMetacardType.CSW_TYPE, "dataset 2");
    expectedValues.put(CswRecordMetacardType.CSW_FORMAT, new String[] {"Shapefile 2"});
    expectedValues.put(
        Metacard.GEOGRAPHY,
        "POLYGON((53.139 6.121, 53.517 6.121, 53.517 5.468, 53.139 5.468, 53.139 6.121))");
    expectedValues.put(
        CswRecordMetacardType.OWS_BOUNDING_BOX,
        new String[] {
          "POLYGON((53.139 6.121, 53.517 6.121, 53.517 5.468, 53.139 5.468, 53.139 6.121))"
        });
    assertMetacard(mc, expectedValues);

    expectedValues.clear();
  }