@Test
  public void testExpandByValueSet() throws IOException {
    ValueSet toExpand = loadResourceFromClasspath(ValueSet.class, "/extensional-case-3-vs.xml");

    // @formatter:off
    Parameters respParam =
        ourClient
            .operation()
            .onType(ValueSet.class)
            .named("expand")
            .withParameter(Parameters.class, "valueSet", toExpand)
            .andParameter("filter", new StringType("11378"))
            .execute();
    ValueSet expanded = (ValueSet) respParam.getParameter().get(0).getResource();
    // @formatter:on

    String resp = myFhirCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(expanded);
    ourLog.info(resp);
    // @formatter:off
    assertThat(
        resp,
        stringContainsInOrder(
            "<code value=\"11378-7\"/>",
            "<display value=\"Systolic blood pressure at First encounter\"/>"));
    // @formatter:on

    assertThat(resp, not(containsString("<code value=\"8450-9\"/>")));
  }
  @Test
  public void testExpandByIdentifier() {
    // @formatter:off
    Parameters respParam =
        ourClient
            .operation()
            .onType(ValueSet.class)
            .named("expand")
            .withParameter(
                Parameters.class,
                "identifier",
                new UriType(
                    "http://www.healthintersections.com.au/fhir/ValueSet/extensional-case-2"))
            .andParameter("filter", new StringType("11378"))
            .execute();
    ValueSet expanded = (ValueSet) respParam.getParameter().get(0).getResource();
    // @formatter:on

    String resp = myFhirCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(expanded);
    ourLog.info(resp);
    // @formatter:off
    assertThat(
        resp,
        stringContainsInOrder(
            "<code value=\"11378-7\"/>",
            "<display value=\"Systolic blood pressure at First encounter\"/>"));
    // @formatter:on

    assertThat(resp, not(containsString("<code value=\"8450-9\"/>")));
  }
 private ValidationResult verifyCodeExternal(ValueSet vs, CodeableConcept cc, boolean tryCache)
     throws Exception {
   ValidationResult res = handleByCache(vs, cc, tryCache);
   if (res != null) return res;
   Parameters pin = new Parameters();
   pin.addParameter().setName("codeableConcept").setValue(cc);
   pin.addParameter().setName("valueSet").setResource(vs);
   res = serverValidateCode(pin, false);
   Map<String, ValidationResult> cache = validationCache.get(vs.getUrl());
   cache.put(cacheId(cc), res);
   return res;
 }
 private ValidationResult verifyCodeExternal(ValueSet vs, Coding coding, boolean tryCache)
     throws Exception {
   ValidationResult res = vs == null ? null : handleByCache(vs, coding, tryCache);
   if (res != null) return res;
   Parameters pin = new Parameters();
   pin.addParameter().setName("coding").setValue(coding);
   if (vs != null) pin.addParameter().setName("valueSet").setResource(vs);
   res = serverValidateCode(pin, vs == null);
   if (vs != null) {
     Map<String, ValidationResult> cache = validationCache.get(vs.getUrl());
     cache.put(cacheId(coding), res);
   }
   return res;
 }
 @SuppressWarnings("rawtypes")
 private String describeValidationParameters(Parameters pin) {
   CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder();
   for (ParametersParameterComponent p : pin.getParameter()) {
     if (p.hasValue() && p.getValue() instanceof PrimitiveType) {
       b.append(p.getName() + "=" + ((PrimitiveType) p.getValue()).asStringValue());
     } else if (p.hasValue() && p.getValue() instanceof Coding) {
       b.append("system=" + ((Coding) p.getValue()).getSystem());
       b.append("code=" + ((Coding) p.getValue()).getCode());
       b.append("display=" + ((Coding) p.getValue()).getDisplay());
     } else if (p.hasValue() && p.getValue() instanceof CodeableConcept) {
       if (((CodeableConcept) p.getValue()).hasCoding()) {
         Coding c = ((CodeableConcept) p.getValue()).getCodingFirstRep();
         b.append("system=" + c.getSystem());
         b.append("code=" + c.getCode());
         b.append("display=" + c.getDisplay());
       } else if (((CodeableConcept) p.getValue()).hasText()) {
         b.append("text=" + ((CodeableConcept) p.getValue()).getText());
       }
     } else if (p.hasResource() && (p.getResource() instanceof ValueSet)) {
       b.append("valueset=" + getVSSummary((ValueSet) p.getResource()));
     }
   }
   return b.toString();
 }
  @Test
  public void testValidateCodeOperationByCodeAndSystemType() {
    // @formatter:off
    Parameters respParam =
        ourClient
            .operation()
            .onType(ValueSet.class)
            .named("validate-code")
            .withParameter(Parameters.class, "code", new CodeType("8450-9"))
            .andParameter("system", new UriType("http://acme.org"))
            .execute();
    // @formatter:on

    String resp = myFhirCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(respParam);
    ourLog.info(resp);

    assertEquals(true, ((BooleanType) respParam.getParameter().get(0).getValue()).booleanValue());
  }
  private ValidationResult serverValidateCode(Parameters pin, boolean doCache) throws Exception {
    if (noTerminologyServer)
      return new ValidationResult(null, null, TerminologyServiceErrorClass.NOSERVICE);
    String cacheName = doCache ? generateCacheName(pin) : null;
    ValidationResult res = loadFromCache(cacheName);
    if (res != null) return res;
    log("Terminology Server: $validate-code " + describeValidationParameters(pin));
    for (ParametersParameterComponent pp : pin.getParameter())
      if (pp.getName().equals("profile"))
        throw new Error("Can only specify profile in the context");
    if (expProfile == null) throw new Exception("No ExpansionProfile provided");
    pin.addParameter().setName("profile").setResource(expProfile);

    Parameters pout = txServer.operateType(ValueSet.class, "validate-code", pin);
    boolean ok = false;
    String message = "No Message returned";
    String display = null;
    TerminologyServiceErrorClass err = TerminologyServiceErrorClass.UNKNOWN;
    for (ParametersParameterComponent p : pout.getParameter()) {
      if (p.getName().equals("result")) ok = ((BooleanType) p.getValue()).getValue().booleanValue();
      else if (p.getName().equals("message")) message = ((StringType) p.getValue()).getValue();
      else if (p.getName().equals("display")) display = ((StringType) p.getValue()).getValue();
      else if (p.getName().equals("cause")) {
        try {
          IssueType it = IssueType.fromCode(((StringType) p.getValue()).getValue());
          if (it == IssueType.UNKNOWN) err = TerminologyServiceErrorClass.UNKNOWN;
          else if (it == IssueType.NOTSUPPORTED)
            err = TerminologyServiceErrorClass.VALUESET_UNSUPPORTED;
        } catch (FHIRException e) {
        }
      }
    }
    if (!ok) res = new ValidationResult(IssueSeverity.ERROR, message, err);
    else if (display != null)
      res = new ValidationResult(new ConceptDefinitionComponent().setDisplay(display));
    else res = new ValidationResult(null);
    saveToCache(res, cacheName);
    return res;
  }
  @Test
  public void testExpandById() throws IOException {
    // @formatter:off
    Parameters respParam =
        ourClient
            .operation()
            .onInstance(myExtensionalVsId)
            .named("expand")
            .withNoParameters(Parameters.class)
            .execute();
    ValueSet expanded = (ValueSet) respParam.getParameter().get(0).getResource();
    // @formatter:on

    String resp = myFhirCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(expanded);
    ourLog.info(resp);
    // @formatter:off
    assertThat(
        resp,
        stringContainsInOrder(
            "<ValueSet xmlns=\"http://hl7.org/fhir\">",
            "<expansion>",
            "<contains>",
            "<system value=\"http://acme.org\"/>",
            "<code value=\"8450-9\"/>",
            "<display value=\"Systolic blood pressure--expiration\"/>",
            "</contains>",
            "<contains>",
            "<system value=\"http://acme.org\"/>",
            "<code value=\"11378-7\"/>",
            "<display value=\"Systolic blood pressure at First encounter\"/>",
            "</contains>",
            "</expansion>"));
    // @formatter:on

    /*
     * Filter with display name
     */

    // @formatter:off
    respParam =
        ourClient
            .operation()
            .onInstance(myExtensionalVsId)
            .named("expand")
            .withParameter(Parameters.class, "filter", new StringType("systolic"))
            .execute();
    expanded = (ValueSet) respParam.getParameter().get(0).getResource();
    // @formatter:on

    expanded = myValueSetDao.expand(myExtensionalVsId, ("systolic"));
    resp = myFhirCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(expanded);
    ourLog.info(resp);
    // @formatter:off
    assertThat(
        resp,
        stringContainsInOrder(
            "<code value=\"11378-7\"/>",
            "<display value=\"Systolic blood pressure at First encounter\"/>"));
    // @formatter:on

  }