/**
   * Test method for {@link
   * RDFJSONUtility.kmr.scam.rest.util.RDFJSON#rdfJsonToGraph(java.lang.String)}.
   *
   * @throws IOException
   * @throws RDFParseException
   * @throws RDFHandlerException
   */
  @Test
  public void testRdfJsonToGraph5() throws IOException, RDFParseException, RDFHandlerException {
    this.testInputFile = "example5.json";

    final Model rdfJsonToGraph =
        Rio.parse(this.getClass().getResourceAsStream(this.testInputFile), "", RDFFormat.RDFJSON);

    // RDFJSONUtility.rdfJsonToHandler(new
    // InputStreamReader(this.getClass().getResourceAsStream(this.testInputFile),
    // StandardCharsets.UTF_8), new StatementCollector(rdfJsonToGraph),
    // ValueFactoryImpl.getInstance());

    Assert.assertEquals(1, rdfJsonToGraph.size());
  }
  /** 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 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 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 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 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 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());
  }
  /**
   * 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());
  }
  @Override
  public void endRDF() throws RDFHandlerException {
    final SesameRDFParser serialiser = new SesameRDFParser();
    try {
      Object output = JsonLdProcessor.fromRDF(model, serialiser);

      final JSONLDMode mode = getWriterConfig().get(JSONLDSettings.JSONLD_MODE);

      final JsonLdOptions opts = new JsonLdOptions();
      // opts.addBlankNodeIDs =
      // getWriterConfig().get(BasicParserSettings.PRESERVE_BNODE_IDS);
      opts.setUseRdfType(getWriterConfig().get(JSONLDSettings.USE_RDF_TYPE));
      opts.setUseNativeTypes(getWriterConfig().get(JSONLDSettings.USE_NATIVE_TYPES));
      // opts.optimize = getWriterConfig().get(JSONLDSettings.OPTIMIZE);

      if (mode == JSONLDMode.EXPAND) {
        output = JsonLdProcessor.expand(output, opts);
      }
      // TODO: Implement inframe in JSONLDSettings
      final Object inframe = null;
      if (mode == JSONLDMode.FLATTEN) {
        output = JsonLdProcessor.frame(output, inframe, opts);
      }
      if (mode == JSONLDMode.COMPACT) {
        final Map<String, Object> ctx = new LinkedHashMap<String, Object>();
        addPrefixes(ctx, model.getNamespaces());
        final Map<String, Object> localCtx = new HashMap<String, Object>();
        localCtx.put("@context", ctx);

        output = JsonLdProcessor.compact(output, localCtx, opts);
      }
      if (getWriterConfig().get(BasicWriterSettings.PRETTY_PRINT)) {
        JSONUtils.writePrettyPrint(writer, output);
      } else {
        JSONUtils.write(writer, output);
      }

    } catch (final JsonLdError e) {
      throw new RDFHandlerException("Could not render JSONLD", e);
    } catch (final JsonGenerationException e) {
      throw new RDFHandlerException("Could not render JSONLD", e);
    } catch (final JsonMappingException e) {
      throw new RDFHandlerException("Could not render JSONLD", e);
    } catch (final IOException e) {
      throw new RDFHandlerException("Could not render JSONLD", e);
    }
  }
  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
  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);
    }
  }
 @Override
 public void startRDF() throws RDFHandlerException {
   statementCollector.clear();
   model.clear();
 }
 @Override
 public void handleNamespace(String prefix, String uri) throws RDFHandlerException {
   model.setNamespace(prefix, uri);
 }
  public HashMap<String, Properties> generateResourceBundle(String baseName)
      throws GenerationException {
    // be sure to have at least the default URI constant settings
    if (uriGeneration == null) {
      uriGeneration = GenerationSetting.createDefault(caseFormat, "", "");
    }
    Pattern pattern = Pattern.compile(Pattern.quote(getPrefix()) + "(.+)");
    HashMap<String, URI> splitUris = new HashMap<>();
    for (Resource nextSubject : model.subjects()) {
      if (nextSubject instanceof URI) {
        Matcher matcher = pattern.matcher(nextSubject.stringValue());
        if (matcher.find()) {
          String k = matcher.group(1);
          splitUris.put(k, (URI) nextSubject);
        }
      }
    }

    List<String> keys = new ArrayList<>();
    keys.addAll(splitUris.keySet());
    Collections.sort(keys, String.CASE_INSENSITIVE_ORDER);

    HashMap<String, Properties> bundles = new HashMap<>();
    // Default we have for sure
    bundles.put(baseName, new Properties());
    for (String key : keys) {
      final URI resource = splitUris.get(key);
      //
      String nextKey = cleanKey(doCaseFormatting(key, uriGeneration.getCaseFormat()));

      for (URI p : LABEL_PROPERTIES) {
        for (Value v : GraphUtil.getObjects(model, resource, p)) {
          if (v instanceof Literal) {
            final Literal lit = (Literal) v;
            final String lang = lit.getLanguage();
            final Properties bundle;
            if (lang == null) {
              bundle = bundles.get(baseName);
            } else if (bundles.containsKey(baseName + "_" + lang)) {
              bundle = bundles.get(baseName + "_" + lang);
            } else {
              bundle = new Properties();
              bundles.put(baseName + "_" + lang, bundle);
            }

            if (!bundle.containsKey(nextKey + ".label")) {
              bundle.put(nextKey + ".label", lit.getLabel().replaceAll("\\s+", " "));
            }
          }
        }
      }

      for (URI p : COMMENT_PROPERTIES) {
        for (Value v : GraphUtil.getObjects(model, resource, p)) {
          if (v instanceof Literal) {
            final Literal lit = (Literal) v;
            final String lang = lit.getLanguage();
            final Properties bundle;
            if (lang == null) {
              bundle = bundles.get(baseName);
            } else if (bundles.containsKey(baseName + "_" + lang)) {
              bundle = bundles.get(baseName + "_" + lang);
            } else {
              bundle = new Properties();
              bundles.put(baseName + "_" + lang, bundle);
            }

            if (!bundle.containsKey(nextKey + ".comment")) {
              bundle.put(nextKey + ".comment", lit.getLabel().replaceAll("\\s+", " "));
            }
          }
        }
      }
    }

    if (getPreferredLanguage() != null) {
      log.debug("completing default Bundle with preferred language {}", getPreferredLanguage());
      final Properties defaultBundle = bundles.get(baseName);
      final Properties prefBundle = bundles.get(baseName + "_" + getPreferredLanguage());
      if (prefBundle != null) {
        for (Entry<Object, Object> key : prefBundle.entrySet()) {
          String nextKey = (String) key.getKey();
          if (!defaultBundle.containsKey(nextKey)) {
            log.trace("copying {} from {} to default Bundle", nextKey, getPreferredLanguage());
            defaultBundle.setProperty(nextKey, (String) key.getValue());
          }
        }
      } else {
        log.warn("No Bundle data found for preferred language {}", getPreferredLanguage());
      }
    }
    return bundles;
  }
  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();
  }
  /**
   * Test method for {@link
   * RDFJSONUtility.kmr.scam.rest.util.RDFJSON#graphToRdfJsonPreordered(java.util.Set,
   * java.io.Writer)} .
   *
   * @throws JSONException
   */
  @Test
  public void testModelToRdfJsonPreorderedSetOfStatementWriter() throws Exception {
    // final Set<Statement> testStatements = new TreeSet<Statement>(new StatementComparator());
    final Model testStatements = new TreeModel();
    final ValueFactoryImpl vf = ValueFactoryImpl.getInstance();

    final BNode testBNode1 = vf.createBNode();

    final BNode testBNode2 = vf.createBNode();

    final URI testURI1 = vf.createURI("http://example.org/test/rdf/json/1");

    final URI testURI2 = vf.createURI("http://my.test.org/rdf/type/2");

    final URI testURI3 = vf.createURI("http://example.org/test/rdf/json/3");

    final URI testURI4 = vf.createURI("http://example.org/test/rdf/json/4");

    final URI testURI5 = vf.createURI("http://my.test.org/rdf/type/5");

    final Statement testStatement1 = vf.createStatement(testURI1, testURI2, testURI3);
    testStatements.add(testStatement1);
    final Statement testStatement2 = vf.createStatement(testURI1, testURI2, testBNode1);
    testStatements.add(testStatement2);
    final Statement testStatement3 = vf.createStatement(testURI1, testURI2, testBNode2);
    testStatements.add(testStatement3);

    final Statement testStatement4 = vf.createStatement(testURI4, testURI2, testURI3);
    testStatements.add(testStatement4);
    final Statement testStatement5 = vf.createStatement(testURI4, testURI2, testBNode2);
    testStatements.add(testStatement5);
    final Statement testStatement6 = vf.createStatement(testURI4, testURI2, testBNode1);
    testStatements.add(testStatement6);

    final Statement testStatement7 = vf.createStatement(testBNode1, testURI5, testBNode2);
    testStatements.add(testStatement7);
    final Statement testStatement8 = vf.createStatement(testBNode1, testURI5, testURI1);
    testStatements.add(testStatement8);
    final Statement testStatement9 = vf.createStatement(testBNode1, testURI5, testURI4);
    testStatements.add(testStatement9);

    // RDFJSONUnitTest.log.info("testStatements=" + testStatements);

    Assert.assertEquals(9, testStatements.size());

    // Verify that the statements are in an acceptable order (testStatement5 and testStatement6
    // can be legitimately swapped)

    final Iterator<Statement> testStatementIterator = testStatements.iterator();

    Assert.assertTrue(testStatementIterator.hasNext());

    // testStatement7 should always be first by virtue of the fact that it has two blank nodes
    // and no other statements have two blank nodes
    Assert.assertEquals(testStatement7, testStatementIterator.next());
    Assert.assertTrue(testStatementIterator.hasNext());

    // Then testStatement8
    Assert.assertEquals(testStatement8, testStatementIterator.next());
    Assert.assertTrue(testStatementIterator.hasNext());

    // Then testStatement9
    Assert.assertEquals(testStatement9, testStatementIterator.next());
    Assert.assertTrue(testStatementIterator.hasNext());

    Rio.write(testStatements, this.testWriter, RDFFormat.RDFJSON);
    // RDFJSONUtility.modelToRdfJson(testStatements, this.testWriter, this.testWriterConfig);

    this.testOutput = this.testWriter.toString();

    Assert.assertTrue(this.testOutput.length() > 0);

    Assert.assertTrue(this.testOutput.startsWith("{"));

    Assert.assertTrue(this.testOutput.endsWith("}"));

    // RDFJSONUnitTest.log.info("testOutput=" + this.testOutput);

    final int firstBlankNode = this.testOutput.indexOf("\"_:");

    // Test that a bnode exists after the opening brace
    Assert.assertTrue(firstBlankNode > 0);

    // The first value after the first blank node should be a blank node identifier
    final int firstValue = this.testOutput.indexOf("\"value\" : \"_:", firstBlankNode);

    Assert.assertTrue("A suitable blank node value was not found", firstValue > 0);

    // This should be guaranteed by the indexOf contract, but doing a quick check anyway
    Assert.assertTrue(firstValue > firstBlankNode);

    // Do a quick check to see if the testOutput is valid JSON

    // FIXME: TODO: Test using Jackson
    // Assert.fail("TODO: Implement me using Jackson");
    // final JSONObject testJSONObject = new JSONObject(this.testOutput);

    // Assert.assertNotNull(testJSONObject);
    // Assert.assertTrue(testJSONObject.length() > 0);
    // Assert.assertTrue(testJSONObject.names().length() > 0);
    // Assert.assertTrue(testJSONObject.keys().hasNext());
  }
Esempio n. 17
0
 @BeforeMethod
 public void setUp() {
   model.clear();
 }