Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(cleanup): Abstract away document type #389

Merged
merged 3 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading