Skip to content

Commit

Permalink
chore(cleanup): Abstract away document type (#389)
Browse files Browse the repository at this point in the history
* Abstract away document type

* Update src/main/java/io/cryostat/core/templates/RemoteTemplateService.java

Co-authored-by: Andrew Azores <me@andrewazor.es>
Signed-off-by: Joshua Matsuoka <Josh.matsuoka@gmail.com>

* Fix Typo

---------

Signed-off-by: Joshua Matsuoka <Josh.matsuoka@gmail.com>
Co-authored-by: Andrew Azores <me@andrewazor.es>
  • Loading branch information
Josh-Matsuoka and andrewazores authored May 21, 2024
1 parent 13bb612 commit e4c733a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import io.cryostat.core.net.JFRConnection;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Parser;
import org.jsoup.select.Elements;
Expand All @@ -55,7 +54,7 @@ protected TemplateType providedTemplateType() {
}

@Override
public Optional<Document> getXml(String templateName, TemplateType type)
public Optional<String> getXml(String templateName, TemplateType type)
throws FlightRecorderException {
if (!providedTemplateType().equals(type)) {
return Optional.empty();
Expand All @@ -68,7 +67,8 @@ public Optional<Document> getXml(String templateName, TemplateType type)
Elements els = doc.getElementsByTag("configuration");
if (els.isEmpty()) {
throw new MalformedXMLException(
"Document did not contain \"configuration\" element");
"Document did not contain \"configuration\""
+ " element");
}
if (els.size() > 1) {
throw new MalformedXMLException(
Expand All @@ -83,6 +83,7 @@ public Optional<Document> getXml(String templateName, TemplateType type)
}
return configuration.attr("label").equals(templateName);
})
.map(doc -> doc.toString())
.findFirst();
} catch (org.openjdk.jmc.flightrecorder.configuration.FlightRecorderException
| IOException
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/io/cryostat/core/templates/TemplateService.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,15 @@

import io.cryostat.core.FlightRecorderException;

import org.jsoup.nodes.Document;

public interface TemplateService {

List<Template> getTemplates() throws FlightRecorderException;

default Optional<Document> getXml(Template template) throws FlightRecorderException {
default Optional<String> getXml(Template template) throws FlightRecorderException {
return getXml(template.getName(), template.getType());
}

Optional<Document> getXml(String templateName, TemplateType type)
throws FlightRecorderException;
Optional<String> getXml(String templateName, TemplateType type) throws FlightRecorderException;

default Optional<IConstrainedMap<EventOptionID>> getEvents(Template template)
throws FlightRecorderException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.parser.Parser;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -100,9 +99,10 @@ void getEventsShouldReturnEmptyForUnknownType() throws Exception {
void getXmlShouldReturnModelFromRemote() throws Exception {
Mockito.when(conn.getService()).thenReturn(svc);
Mockito.when(svc.getServerTemplates()).thenReturn(Collections.singletonList(xmlText));
Optional<Document> doc = templateSvc.getXml("Profiling", TemplateType.TARGET);
Optional<String> doc = templateSvc.getXml("Profiling", TemplateType.TARGET);
Assertions.assertTrue(doc.isPresent());
Assertions.assertTrue(doc.get().hasSameValue(Jsoup.parse(xmlText, "", Parser.xmlParser())));
Assertions.assertTrue(
doc.get().equals(Jsoup.parse(xmlText, "", Parser.xmlParser()).outerHtml()));
}

@Test
Expand Down

0 comments on commit e4c733a

Please sign in to comment.