/**
   * give a expected xml string and make sure it can be shortly deserialized
   *
   * @throws Exception
   */
  @Test
  public void shouldPersonShortDeserialization() throws Exception {
    // prepare the necessary data

    /*
     * Because "XXXShortConverter.unmarshal(HierarchicalStreamReader, UnmarshallingContext)" has operations accessing data in database,
     * We also need to use the "PersonShortSerializationTest.xml" here
     */
    initializeInMemoryDatabase();
    executeDataSet("org/openmrs/module/xstream/include/PersonShortSerializationTest.xml");
    authenticate();

    StringBuilder xmlBuilder = new StringBuilder();
    xmlBuilder.append(
        "<personAddress id=\"1\" uuid=\"3350d0b5-821c-4e5e-ad1d-a9bce331e118\" voided=\"false\">\n");
    xmlBuilder.append("  <creator id=\"2\" uuid=\"6adb7c42-cfd2-4301-b53b-ff17c5654ff7\"/>\n");
    xmlBuilder.append(
        "  <dateCreated class=\"sql-timestamp\" id=\"3\">2005-09-22 00:00:00 CST</dateCreated>\n");
    xmlBuilder.append("  <personAddressId>2</personAddressId>\n");
    xmlBuilder.append("  <person id=\"4\" uuid=\"da7f524f-27ce-4bb2-86d6-6d1d05312bd5\"/>\n");
    xmlBuilder.append("  <preferred>false</preferred>\n");
    xmlBuilder.append("  <address1>1050 Wishard Blvd.</address1>\n");
    xmlBuilder.append("  <address2>RG5</address2>\n");
    xmlBuilder.append("  <cityVillage>Indianapolis</cityVillage>\n");
    xmlBuilder.append("  <stateProvince>IN</stateProvince>\n");
    xmlBuilder.append("  <country>USA</country>\n");
    xmlBuilder.append("  <postalCode>46202</postalCode>\n");
    xmlBuilder.append("</personAddress>\n");

    PersonAddress pa =
        Context.getSerializationService()
            .deserialize(xmlBuilder.toString(), PersonAddress.class, XStreamShortSerializer.class);
    assertEquals("da7f524f-27ce-4bb2-86d6-6d1d05312bd5", pa.getPerson().getUuid());
  }
  private void verifyMetadataPackagesConfigured() throws Exception {
    MetadataPackagesConfig config;
    {
      InputStream inputStream =
          activator.getClass().getClassLoader().getResourceAsStream(MetadataUtil.PACKAGES_FILENAME);
      String xml = IOUtils.toString(inputStream);
      config =
          Context.getSerializationService()
              .getDefaultSerializer()
              .deserialize(xml, MetadataPackagesConfig.class);
    }

    MetadataSharingService metadataSharingService =
        Context.getService(MetadataSharingService.class);

    // To catch the (common) case where someone gets the groupUuid wrong, we look for any installed
    // packages that
    // we are not expecting

    List<String> groupUuids = new ArrayList<String>();

    for (MetadataPackageConfig metadataPackage : config.getPackages()) {
      groupUuids.add(metadataPackage.getGroupUuid());
    }

    for (ImportedPackage importedPackage : metadataSharingService.getAllImportedPackages()) {
      if (!groupUuids.contains(importedPackage.getGroupUuid())) {
        fail(
            "Found a package with an unexpected groupUuid. Name: "
                + importedPackage.getName()
                + " , groupUuid: "
                + importedPackage.getGroupUuid());
      }
    }

    for (MetadataPackageConfig metadataPackage : config.getPackages()) {
      ImportedPackage installedPackage =
          metadataSharingService.getImportedPackageByGroup(metadataPackage.getGroupUuid());
      Integer actualVersion = installedPackage == null ? null : installedPackage.getVersion();
      assertEquals(
          "Failed to install "
              + metadataPackage.getFilenameBase()
              + ". Expected version: "
              + metadataPackage.getVersion()
              + " Actual version: "
              + actualVersion,
          metadataPackage.getVersion(),
          actualVersion);
    }

    // this doesn't strictly belong here, but we include it as an extra sanity check on the MDS
    // module
    for (Concept concept : conceptService.getAllConcepts()) {
      ValidateUtil.validate(concept);
    }
  }
  /**
   * generate the relative objects and make sure the short serialization can work
   *
   * @throws Exception
   */
  @Test
  @SkipBaseSetup
  public void shouldPersonShortSerialization() throws Exception {
    // prepare the necessary data
    initializeInMemoryDatabase();
    executeDataSet("org/openmrs/module/xstream/include/PersonShortSerializationTest.xml");
    authenticate();

    PersonAddress pa =
        Context.getPersonService().getPersonAddressByUuid("3350d0b5-821c-4e5e-ad1d-a9bce331e118");
    String xmlOutput =
        Context.getSerializationService().serialize(pa, XStreamShortSerializer.class);
    // should only serialize "uuid"
    XMLAssert.assertXpathEvaluatesTo(
        "da7f524f-27ce-4bb2-86d6-6d1d05312bd5", "/personAddress/person/@uuid", xmlOutput);
    // with short serialization, the "person" element shouldn't contain any child element in the
    // serialized xml
    XMLAssert.assertXpathNotExists("/personAddress/person/*", xmlOutput);
  }
 /**
  * Private method for retrieving the Serializer that should be used for the passed
  * SerializedObject, defaulting to the default system serializer if none is explicitly set on the
  * object
  */
 private OpenmrsSerializer getSerializer(SerializedObject o) {
   if (o != null && o.getSerializationClass() != null) {
     return Context.getSerializationService().getSerializer(o.getSerializationClass());
   }
   return Context.getSerializationService().getDefaultSerializer();
 }