### Question:
RrTemplate implements RrTermMap { @Override public Optional<QuadPart> generateValue(Row input) { Matcher regexMatcher = pattern.matcher(template); StringBuffer resultString = new StringBuffer(); while (regexMatcher.find()) { String value = input.getRawValue(regexMatcher.group(1)); if (value == null) { return Optional.empty(); } else { if (termType == TermType.IRI) { try { value = URLEncoder.encode(value, "UTF-8").replace("+", "%20"); } catch (UnsupportedEncodingException e) { LOG.error("Java is being really stoopid and throws an unsupportedEncodingException for UTF-8."); throw new RuntimeException(e); } } regexMatcher.appendReplacement(resultString, value); } } if (StringUtils.isBlank(resultString.toString())) { return Optional.empty(); } regexMatcher.appendTail(resultString); switch (termType) { case IRI: return Optional.of(new RdfUri(resultString.toString())); case BlankNode: return Optional.of(new RdfBlankNode(resultString.toString())); case Literal: return Optional.of(new RdfValue(resultString.toString(), dataType)); default: throw new UnsupportedOperationException("Not all items in the Enumerable where handled"); } } RrTemplate(String template, TermType termType, String dataType); @Override Optional<QuadPart> generateValue(Row input); @Override String toString();  }### Answer:
@Test public void replacesTemplatesWithEmptyStringIfValueIsEmpty() throws Exception { TestRow row = new TestRow(ImmutableMap.of("foo", ""), ImmutableMap.of(), new ThrowingErrorHandler()); RrTemplate rrTemplate = new RrTemplate("start {foo} end", TermType.Literal, XSD_STRING); Optional<QuadPart> actual = rrTemplate.generateValue(row); assertThat(actual, is(Optional.of(new RdfValue("start end", "http: }
@Test public void returnsEmptyIfOneValueIsMissing() throws Exception { TestRow row = new TestRow(ImmutableMap.of("foo", ""), ImmutableMap.of(), new ThrowingErrorHandler()); RrTemplate rrTemplate = new RrTemplate("{foo} and {bar}", TermType.Literal, XSD_STRING); Optional<QuadPart> actual = rrTemplate.generateValue(row); assertThat(actual, is(Optional.empty())); }
@Test public void iriEncodesValueIfResultTypeIsIri() throws Exception { TestRow row = new TestRow( ImmutableMap.of("foo", "some s\"tring with wéird characters"), ImmutableMap.of(), new ThrowingErrorHandler() ); RrTemplate rrTemplate = new RrTemplate("http: Optional<QuadPart> actual = rrTemplate.generateValue(row); assertThat( actual, is(Optional.of(new RdfUri("http: ); }