/** Test successful search for a Platform in Turtle */
  @Test
  public void testSearchTurtle() throws Exception {
    final String[] searchTypes = {"http://purl.org/podd/ns/poddScience#Platform"}; // ,
    // OWL.THING.stringValue()
    // };
    final MediaType requestMediaType = MediaType.APPLICATION_RDF_TURTLE;

    final Model resultModel =
        this.internalTestSearchRdf("Scan", searchTypes, requestMediaType, null);

    Assert.assertEquals("Not the expected number of results", 5, resultModel.size());
    System.out.println(resultModel.toString());
    Assert.assertEquals(
        "Expected Platform CabScan not found",
        1,
        resultModel.filter(null, null, PODD.VF.createLiteral("CabScan")).size());
    Assert.assertEquals(
        "Expected Platform PlantScan not found",
        1,
        resultModel.filter(null, null, PODD.VF.createLiteral("PlantScan")).size());
  }
  /** Test successful search for a FOR Codes in RDF/XML */
  @Test
  public void testSearchRdfForWildtTypeAssertion() throws Exception {
    final String[] searchTypes = {"http://purl.org/podd/ns/poddScience#WildTypeAssertion"};
    final MediaType requestMediaType = MediaType.APPLICATION_RDF_XML;

    final Model resultModel = this.internalTestSearchRdf("", searchTypes, requestMediaType, null);

    // verify:
    Assert.assertEquals("Not the expected number of results", 4, resultModel.size());
    Assert.assertEquals(
        "Expected Assertion 'Yes' not found",
        1,
        resultModel.filter(null, null, PODD.VF.createLiteral("Yes")).size());
    Assert.assertEquals(
        "Expected Assertion 'Unknown' not found",
        1,
        resultModel.filter(null, null, PODD.VF.createLiteral("Unknown")).size());
    Assert.assertEquals(
        "Expected Assertion 'Not Applicable' not found",
        1,
        resultModel.filter(null, null, PODD.VF.createLiteral("Not Applicable")).size());
  }
  /** Test a search in RDF/XML, with no "searchTypes" specified. */
  @Test
  public void testSearchRdfWithoutSearchTypes() throws Exception {
    final String[] searchTypes = {}; // EVERYTHING in the search space is
    // compared
    final MediaType requestMediaType = MediaType.APPLICATION_RDF_XML;

    final Model resultModel = this.internalTestSearchRdf("e", searchTypes, requestMediaType, null);

    // verify:
    Assert.assertEquals("Not the expected number of results", 224, resultModel.size());

    Assert.assertEquals(
        "dcTerms not found",
        1,
        resultModel
            .filter(null, null, PODD.VF.createLiteral("The PODD Ontology for Dublin Core Terms"))
            .size());
    Assert.assertEquals(
        "The PODD Ontology for Dublin Core Terms not found",
        1,
        resultModel
            .filter(null, null, PODD.VF.createLiteral("The PODD Ontology for Dublin Core Terms"))
            .size());
  }
  /**
   * Test successful search for a Platform in RDF/XML including owl:Thing in the list to verify that
   * it doesn't expand the search space, as the search types must all match. Ie, it is not the case
   * that if "any" types match the search will succeed.
   */
  @Test
  public void testSearchRdfForPlatformsWithOWLThing() throws Exception {
    final String[] searchTypes = {
      "http://purl.org/podd/ns/poddScience#Platform", OWL.THING.stringValue()
    };
    final MediaType requestMediaType = MediaType.APPLICATION_RDF_XML;

    final Model resultModel = this.internalTestSearchRdf("me", searchTypes, requestMediaType, null);

    // verify:
    Assert.assertEquals("Not the expected number of results", 9, resultModel.size());
    Assert.assertEquals(
        "Expected Platform SPAD Meter not found",
        1,
        resultModel.filter(null, null, PODD.VF.createLiteral("SPAD Meter")).size());
    Assert.assertEquals(
        "Expected Platform Pyrometer not found",
        1,
        resultModel.filter(null, null, PODD.VF.createLiteral("Pyrometer")).size());
    Assert.assertEquals(
        "Expected Platform SC1 Porometer not found",
        1,
        resultModel.filter(null, null, PODD.VF.createLiteral("SC1 Porometer")).size());
  }
  /** Test successful search for PoddScience:Sex values in RDF/XML */
  @Test
  public void testSearchRdfForSex() throws Exception {
    // prepare: add an artifact
    final InferredOWLOntologyID testArtifact =
        this.loadTestArtifact(
            TestConstants.TEST_ARTIFACT_20130206, MediaType.APPLICATION_RDF_TURTLE);

    final String[] searchTypes = {"http://purl.org/podd/ns/poddScience#Sex"};
    final MediaType requestMediaType = MediaType.APPLICATION_RDF_XML;

    final Model resultModel =
        this.internalTestSearchRdf(
            "", searchTypes, requestMediaType, testArtifact.getOntologyIRI().toString());

    Assert.assertEquals("Not the expected number of results", 5, resultModel.size());
    Assert.assertEquals(
        "Value Hermaphrodite not found",
        1,
        resultModel.filter(null, null, PODD.VF.createLiteral("Hermaphrodite")).size());
    Assert.assertEquals(
        "Value Male not found",
        1,
        resultModel.filter(null, null, PODD.VF.createLiteral("Male")).size());
  }
  public VocabBuilder(String filename, RDFFormat format) throws IOException, RDFParseException {
    Path file = Paths.get(filename);
    if (!Files.exists(file)) throw new FileNotFoundException(filename);

    if (format == null) {
      format = Rio.getParserFormatForFileName(filename);
      log.trace("detected input format from filename {}: {}", filename, format);
    }

    try (final InputStream inputStream = Files.newInputStream(file)) {
      log.trace("Loading input file");
      model = Rio.parse(inputStream, "", format);
    }

    // import
    Set<Resource> owlOntologies = model.filter(null, RDF.TYPE, OWL.ONTOLOGY).subjects();
    if (!owlOntologies.isEmpty()) {
      setPrefix(owlOntologies.iterator().next().stringValue());
    }
  }
  /** Test successful search for a custom Platform in RDF/XML */
  @Test
  public void testSearchRdfForCustomPlatforms() throws Exception {
    // prepare:
    final InferredOWLOntologyID testArtifact =
        this.loadTestArtifact(
            TestConstants.TEST_ARTIFACT_20130206, MediaType.APPLICATION_RDF_TURTLE);

    final String[] searchTypes = {"http://purl.org/podd/ns/poddScience#Platform"};
    final MediaType requestMediaType = MediaType.APPLICATION_RDF_XML;

    final Model resultModel =
        this.internalTestSearchRdf(
            "lat", searchTypes, requestMediaType, testArtifact.getOntologyIRI().toString());

    // verify:
    Assert.assertEquals("Not the expected number of results", 1, resultModel.size());
    Assert.assertEquals(
        "Expected Platform 1 not found",
        1,
        resultModel.filter(null, null, PODD.VF.createLiteral("Platform 1")).size());
  }
  /** Test successful search for a PODD Project in RDF/XML */
  @Test
  public void testSearchRdfForProjects() throws Exception {
    // prepare:
    final InferredOWLOntologyID testArtifact =
        this.loadTestArtifact(
            TestConstants.TEST_ARTIFACT_20130206, MediaType.APPLICATION_RDF_TURTLE);

    final String[] searchTypes = {"http://purl.org/podd/ns/poddScience#Project"};
    final MediaType requestMediaType = MediaType.APPLICATION_RDF_XML;

    final Model resultModel =
        this.internalTestSearchRdf(
            "Cot", searchTypes, requestMediaType, testArtifact.getOntologyIRI().toString());

    // verify:
    Assert.assertEquals("Not the expected number of results", 1, resultModel.size());
    Assert.assertTrue(
        "Expected Project not found",
        resultModel
            .filter(null, RDFS.LABEL, null)
            .objectString()
            .contains("Cotton Leaf Morphology"));
  }
  @Test
  public void testPostRdfBasic() throws Exception {
    // prepare: add an artifact
    final InferredOWLOntologyID testArtifact =
        this.loadTestArtifact(
            TestConstants.TEST_ARTIFACT_20130206, MediaType.APPLICATION_RDF_TURTLE);

    final ClientResource searchClientResource =
        new ClientResource(this.getUrl(PoddWebConstants.PATH_SEARCH));
    try {
      searchClientResource.addQueryParameter(
          PoddWebConstants.KEY_ARTIFACT_IDENTIFIER, testArtifact.getOntologyIRI().toString());

      // prepare: the test input
      final String[] objectUris = {
        "http://purl.org/podd/basic-1-20130206/object:2966",
        "http://purl.org/podd/basic-2-20130206/artifact:1#Demo-Genotype",
        "http://purl.org/podd/basic-2-20130206/artifact:1#SqueekeeMaterial",
        "http://purl.org/podd/ns/poddScience#WildType_NotApplicable",
        "http://purl.org/podd/ns/poddPlant#DeltaTporometer-63",
        "http://purl.org/podd/ns/poddBase#DisplayType_LongText"
      };

      final String[] expectedLabels = {
        "Project#2012-0006_ Cotton Leaf Morphology",
        "Demo genotype",
        "Squeekee material",
        "Not Applicable",
        "Delta-T porometer",
        null
      };

      final Model testModel = new LinkedHashModel();
      for (final String s : objectUris) {
        testModel.add(PODD.VF.createURI(s), RDFS.LABEL, PODD.VF.createLiteral("?blank"));
      }

      final RDFFormat inputFormat = RDFFormat.RDFXML;
      final MediaType inputMediaType = MediaType.valueOf(inputFormat.getDefaultMIMEType());

      // build input representation
      final ByteArrayOutputStream output = new ByteArrayOutputStream(8096);
      Rio.write(testModel, output, inputFormat);
      final Representation input = new StringRepresentation(output.toString(), inputMediaType);

      // invoke service
      final Representation results =
          this.doTestAuthenticatedRequest(
              searchClientResource,
              Method.POST,
              input,
              inputMediaType,
              Status.SUCCESS_OK,
              AbstractResourceImplTest.WITH_ADMIN);

      // verify: response
      final Model resultModel = this.assertRdf(results, RDFFormat.RDFXML, 5);

      // verify: each URI has the expected label
      for (int i = 0; i < objectUris.length; i++) {
        final String objectString =
            resultModel.filter(PODD.VF.createURI(objectUris[i]), RDFS.LABEL, null).objectString();
        Assert.assertEquals("Not the expected label", expectedLabels[i], objectString);
      }
    } finally {
      this.releaseClient(searchClientResource);
    }
  }
  public void generate(String className, PrintWriter out)
      throws IOException, GraphUtilException, GenerationException {
    // be sure to have at least the uri constants generated
    if (stringGeneration == null && uriGeneration == null) {
      uriGeneration = GenerationSetting.createDefault(caseFormat, "", "");
      // throw new GenerationException("no generation settings present, please set explicitly");
    }
    log.trace("classname: {}", className);
    if (StringUtils.isBlank(name)) {
      name = className;
    }
    if (StringUtils.isBlank(prefix)) {
      throw new GenerationException("could not detect prefix, please set explicitly");
    } else {
      log.debug("prefix: {}", prefix);
    }

    Pattern pattern = Pattern.compile(Pattern.quote(getPrefix()) + "(.+)");
    ConcurrentMap<String, URI> splitUris = new ConcurrentHashMap<>();
    for (Resource nextSubject : model.subjects()) {
      if (nextSubject instanceof URI) {
        Matcher matcher = pattern.matcher(nextSubject.stringValue());
        if (matcher.find()) {
          String k = matcher.group(1);
          URI putIfAbsent = splitUris.putIfAbsent(k, (URI) nextSubject);
          if (putIfAbsent != null) {
            log.warn(
                "Conflicting keys found: uri={} key={} existing={}",
                nextSubject.stringValue(),
                k,
                putIfAbsent);
          }
        }
      }
    }

    // print

    // package is optional
    if (StringUtils.isNotBlank(packageName)) {
      out.printf("package %s;%n%n", getPackageName());
    }
    // imports
    out.println("import org.openrdf.model.URI;");
    out.println("import org.openrdf.model.ValueFactory;");
    out.println("import org.openrdf.model.impl.ValueFactoryImpl;");
    out.println();

    final URI pfx = new URIImpl(prefix);
    Literal oTitle =
        getFirstExistingObjectLiteral(model, pfx, getPreferredLanguage(), LABEL_PROPERTIES);
    Literal oDescr =
        getFirstExistingObjectLiteral(model, pfx, getPreferredLanguage(), COMMENT_PROPERTIES);
    Set<Value> oSeeAlso = model.filter(pfx, RDFS.SEEALSO, null).objects();

    // class JavaDoc
    out.println("/**");
    if (oTitle != null) {
      out.printf(
          " * %s.%n",
          WordUtils.wrap(oTitle.getLabel().replaceAll("\\s+", " "), 70, "\n * ", false));
      out.println(" * <p>");
    }
    if (oDescr != null) {
      out.printf(
          " * %s.%n",
          WordUtils.wrap(oDescr.getLabel().replaceAll("\\s+", " "), 70, "\n * ", false));
      out.println(" * <p>");
    }
    out.printf(" * Namespace %s.%n", name);
    out.printf(" * Prefix: {@code <%s>}%n", prefix);
    if (!oSeeAlso.isEmpty()) {
      out.println(" *");
      for (Value s : oSeeAlso) {
        if (s instanceof URI) {
          out.printf(" * @see <a href=\"%s\">%s</a>%n", s.stringValue(), s.stringValue());
        }
      }
    }
    out.println(" */");
    // class Definition
    out.printf("public class %s {%n", className);
    out.println();

    // constants
    out.printf(getIndent(1) + "/** {@code %s} **/%n", prefix);
    out.printf(getIndent(1) + "public static final String NAMESPACE = \"%s\";%n", prefix);
    out.println();
    out.printf(getIndent(1) + "/** {@code %s} **/%n", name.toLowerCase());
    out.printf(getIndent(1) + "public static final String PREFIX = \"%s\";%n", name.toLowerCase());
    out.println();

    List<String> keys = new ArrayList<>();
    keys.addAll(splitUris.keySet());
    Collections.sort(keys, String.CASE_INSENSITIVE_ORDER);
    // do the string constant generation (if set)
    if (stringGeneration != null) {
      for (String key : keys) {
        final Literal comment =
            getFirstExistingObjectLiteral(
                model, splitUris.get(key), getPreferredLanguage(), COMMENT_PROPERTIES);
        final Literal label =
            getFirstExistingObjectLiteral(
                model, splitUris.get(key), getPreferredLanguage(), LABEL_PROPERTIES);

        out.println(getIndent(1) + "/**");
        if (label != null) {
          out.printf(getIndent(1) + " * %s%n", label.getLabel());
          out.println(getIndent(1) + " * <p>");
        }
        out.printf(getIndent(1) + " * {@code %s}.%n", splitUris.get(key).stringValue());
        if (comment != null) {
          out.println(getIndent(1) + " * <p>");
          out.printf(
              getIndent(1) + " * %s%n",
              WordUtils.wrap(
                  comment.getLabel().replaceAll("\\s+", " "),
                  70,
                  "\n" + getIndent(1) + " * ",
                  false));
        }
        out.println(getIndent(1) + " *");
        out.printf(getIndent(1) + " * @see <a href=\"%s\">%s</a>%n", splitUris.get(key), key);
        out.println(getIndent(1) + " */");

        final String nextKey =
            cleanKey(
                // NOTE: CONSTANT PREFIX and constant SUFFIX are NOT part of caseFormatting
                String.format(
                    "%s%s%s",
                    StringUtils.defaultString(stringGeneration.getConstantPrefix()),
                    doCaseFormatting(key, stringGeneration.getCaseFormat()),
                    StringUtils.defaultString(stringGeneration.getConstantSuffix())));
        checkField(className, nextKey);
        out.printf(
            getIndent(1) + "public static final String %s = %s.NAMESPACE + \"%s\";%n",
            nextKey,
            className,
            key);
        out.println();
      }
    }
    // do the entire uri constant generation
    if (uriGeneration != null) {
      for (String key : keys) {
        Literal comment =
            getFirstExistingObjectLiteral(
                model, splitUris.get(key), getPreferredLanguage(), COMMENT_PROPERTIES);
        Literal label =
            getFirstExistingObjectLiteral(
                model, splitUris.get(key), getPreferredLanguage(), LABEL_PROPERTIES);

        out.println(getIndent(1) + "/**");
        if (label != null) {
          out.printf(getIndent(1) + " * %s%n", label.getLabel());
          out.println(getIndent(1) + " * <p>");
        }
        out.printf(getIndent(1) + " * {@code %s}.%n", splitUris.get(key).stringValue());
        if (comment != null) {
          out.println(getIndent(1) + " * <p>");
          out.printf(
              getIndent(1) + " * %s%n",
              WordUtils.wrap(
                  comment.getLabel().replaceAll("\\s+", " "),
                  70,
                  "\n" + getIndent(1) + " * ",
                  false));
        }
        out.println(getIndent(1) + " *");
        out.printf(getIndent(1) + " * @see <a href=\"%s\">%s</a>%n", splitUris.get(key), key);
        out.println(getIndent(1) + " */");
        final String nextKey =
            cleanKey(
                // NOTE: CONSTANT PREFIX and constant SUFFIX are NOT part of caseFormatting
                String.format(
                    "%s%s%s",
                    StringUtils.defaultString(uriGeneration.getConstantPrefix()),
                    doCaseFormatting(key, uriGeneration.getCaseFormat()),
                    StringUtils.defaultString(uriGeneration.getConstantSuffix())));

        // String nextKey = cleanKey(doCaseFormatting(key, uriGeneration.getCaseFormat()));
        checkField(className, nextKey);
        out.printf(getIndent(1) + "public static final URI %s;%n", nextKey);
        out.println();
      }
      // static init
      out.println(getIndent(1) + "static {");
      out.printf(getIndent(2) + "ValueFactory factory = ValueFactoryImpl.getInstance();%n");
      out.println();
      for (String key : keys) {
        final String nextKey =
            cleanKey(
                // NOTE: CONSTANT PREFIX and constant SUFFIX are NOT part of caseFormatting
                String.format(
                    "%s%s%s",
                    StringUtils.defaultString(uriGeneration.getConstantPrefix()),
                    doCaseFormatting(key, uriGeneration.getCaseFormat()),
                    StringUtils.defaultString(uriGeneration.getConstantSuffix())));
        out.printf(
            getIndent(2) + "%s = factory.createURI(%s.NAMESPACE, \"%s\");%n",
            nextKey,
            className,
            key);
      }
      out.println(getIndent(1) + "}");
      out.println();
    }

    // private contructor to avoid instances
    out.printf(getIndent(1) + "private %s() {%n", className);
    out.println(getIndent(2) + "//static access only");
    out.println(getIndent(1) + "}");
    out.println();

    // class end
    out.println("}");
    out.flush();
  }