Skip to content

Commit

Permalink
[NOID] Solve LoadHtml failing test due to string comparison (#3921) (#…
Browse files Browse the repository at this point in the history
…3926)

* [NOID] Solve LoadHtml failing test due to string comparison (#3921)

* [NOID] applied code formatting
  • Loading branch information
vga91 authored Mar 11, 2024
1 parent 5107101 commit d141160
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 41 deletions.
82 changes: 51 additions & 31 deletions full/src/test/java/apoc/load/LoadHtmlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.junit.After;
import org.junit.Assert;
Expand All @@ -49,20 +50,30 @@

public class LoadHtmlTest {

protected static final String RESULT_QUERY_METADATA = ("{attributes={charset=UTF-8}, tagName=meta}, "
+ "{attributes={name=ResourceLoaderDynamicStyles}, tagName=meta}, "
+ "{attributes={name=generator, content=MediaWiki 1.32.0-wmf.18}, tagName=meta}, "
+ "{attributes={name=referrer, content=origin}, tagName=meta}, "
+ "{attributes={name=referrer, content=origin-when-crossorigin}, tagName=meta}, "
+ "{attributes={name=referrer, content=origin-when-cross-origin}, tagName=meta}, "
+ "{attributes={property=og:image, content=https://upload.wikimedia.org/wikipedia/en/e/ea/Aap_Kaa_Hak_titles.jpg}, tagName=meta}");

protected static final String RESULT_QUERY_H2 =
("{text=Contents, tagName=h2}, " + "{text=Origins[edit], tagName=h2}, "
+ "{text=Content[edit], tagName=h2}, "
+ "{text=Legacy[edit], tagName=h2}, "
+ "{text=References[edit], tagName=h2}, "
+ "{text=Navigation menu, tagName=h2}");
protected static final List<Map<String, Object>> RESULT_QUERY_METADATA = asList(
map("tagName", "meta", "attributes", map("charset", "UTF-8")),
map("attributes", map("name", "ResourceLoaderDynamicStyles"), "tagName", "meta"),
map("attributes", map("name", "generator", "content", "MediaWiki 1.32.0-wmf.18"), "tagName", "meta"),
map("attributes", map("name", "referrer", "content", "origin"), "tagName", "meta"),
map("attributes", map("name", "referrer", "content", "origin-when-crossorigin"), "tagName", "meta"),
map("attributes", map("name", "referrer", "content", "origin-when-cross-origin"), "tagName", "meta"),
map(
"attributes",
map(
"property",
"og:image",
"content",
"https://upload.wikimedia.org/wikipedia/en/e/ea/Aap_Kaa_Hak_titles.jpg"),
"tagName",
"meta"));

protected static final List<Map<String, Object>> RESULT_QUERY_H2 = asList(
map("text", "Contents", "tagName", "h2"),
map("text", "Origins[edit]", "tagName", "h2"),
map("text", "Content[edit]", "tagName", "h2"),
map("text", "Legacy[edit]", "tagName", "h2"),
map("text", "References[edit]", "tagName", "h2"),
map("text", "Navigation menu", "tagName", "h2"));

private static final String INVALID_PATH = new File("src/test/resources/wikipedia1.html").getName();
private static final String VALID_PATH =
Expand Down Expand Up @@ -208,11 +219,7 @@ public void testQueryMetadata() {
map("url", new File("src/test/resources/wikipedia.html").toURI().toString(), "query", query),
result -> {
Map<String, Object> row = result.next();
assertEquals(
map("metadata", asList(RESULT_QUERY_METADATA))
.toString()
.trim(),
row.get("value").toString().trim());
assertEquals(map("metadata", RESULT_QUERY_METADATA), row.get("value"));
assertFalse(result.hasNext());
});
}
Expand Down Expand Up @@ -426,9 +433,7 @@ public void testQueryH2() {
map("url", new File("src/test/resources/wikipedia.html").toURI().toString(), "query", query),
result -> {
Map<String, Object> row = result.next();
assertEquals(
map("h2", asList(RESULT_QUERY_H2)).toString().trim(),
row.get("value").toString().trim());
assertEquals(map("h2", RESULT_QUERY_H2), row.get("value"));
assertFalse(result.hasNext());
});
}
Expand Down Expand Up @@ -472,8 +477,9 @@ public void testHref() {
public void testQueryWithFailsSilentlyWithList() {
Map<String, Object> query = map("a", "a", "invalid", "invalid", "h6", "h6");

String expectedH6 =
"[{attributes={id=correct}, text=test, tagName=h6}, {attributes={id=childIncorrect}, text=incorrecttest, tagName=h6}]";
List<Map<String, Object>> expectedH6 = asList(
map("attributes", map("id", "correct"), "text", "test", "tagName", "h6"),
map("attributes", map("id", "childIncorrect"), "text", "incorrecttest", "tagName", "h6"));

testResult(
db,
Expand All @@ -485,7 +491,7 @@ public void testQueryWithFailsSilentlyWithList() {
// number of <a> tags in html file minus the incorrect tag
assertEquals(107, ((List) value.get("a")).size());
assertEquals(Collections.emptyList(), value.get("invalid"));
assertEquals(expectedH6, value.get("h6").toString().trim());
assertEquals(expectedH6, value.get("h6"));
assertEquals(2, ((List) value.get(KEY_ERROR)).size());
assertFalse(result.hasNext());
});
Expand All @@ -495,8 +501,17 @@ public void testQueryWithFailsSilentlyWithList() {
public void testQueryWithFailsSilentlyWithListAndChildren() {
Map<String, Object> query = map("a", "a", "invalid", "invalid", "h6", "h6");

String expectedH6 =
"[{children=[], attributes={id=correct}, text=test, tagName=h6}, {children=[], attributes={id=childIncorrect}, text=incorrect, tagName=h6}]";
List<Map<String, Object>> expectedH6 = asList(
map("children", asList(), "attributes", map("id", "correct"), "text", "test", "tagName", "h6"),
map(
"children",
asList(),
"attributes",
map("id", "childIncorrect"),
"text",
"incorrect",
"tagName",
"h6"));

testResult(
db,
Expand All @@ -508,7 +523,7 @@ public void testQueryWithFailsSilentlyWithListAndChildren() {
// number of <a> tags in html file minus the incorrect tag
assertEquals(107, ((List) value.get("a")).size());
assertEquals(Collections.emptyList(), value.get("invalid"));
assertEquals(expectedH6, value.get("h6").toString().trim());
assertEquals(expectedH6, value.get("h6"));
assertEquals(3, ((List) value.get(KEY_ERROR)).size());
assertFalse(result.hasNext());
});
Expand Down Expand Up @@ -638,12 +653,17 @@ public static void skipIfBrowserNotPresentOrCompatible(Runnable runnable) {
try {
runnable.run();
} catch (RuntimeException e) {

// The test don't fail if the current chrome/firefox version is incompatible or if the browser is not
// installed
Stream<String> notPresentOrIncompatible = Stream.of(
"cannot find Chrome binary",
"Cannot find firefox binary",
"Expected browser binary location",
"browser start-up failure",
"This version of ChromeDriver only supports Chrome version");
final String msg = e.getMessage();
if (!msg.contains("cannot find Chrome binary")
&& !msg.contains("Cannot find firefox binary")
&& !msg.contains("This version of ChromeDriver only supports Chrome version")) {
if (notPresentOrIncompatible.noneMatch(msg::contains)) {
throw e;
}
}
Expand Down
13 changes: 3 additions & 10 deletions full/src/test/java/apoc/load/LoadHtmlTestParameterized.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import static apoc.util.MapUtil.map;
import static apoc.util.TestUtil.testResult;
import static com.google.common.collect.Lists.newArrayList;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyMap;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
Expand Down Expand Up @@ -99,12 +98,8 @@ public void testQueryAll() {
List<Map<String, Object>> metadata = (List<Map<String, Object>>) value.get("metadata");
List<Map<String, Object>> h2 = (List<Map<String, Object>>) value.get("h2");

assertEquals(
asList(RESULT_QUERY_METADATA).toString().trim(),
metadata.toString().trim());
assertEquals(
asList(RESULT_QUERY_H2).toString().trim(),
h2.toString().trim());
assertEquals(RESULT_QUERY_METADATA, metadata);
assertEquals(RESULT_QUERY_H2, h2);
});
});
}
Expand All @@ -131,9 +126,7 @@ public void testQueryH2WithConfig() {
config),
result -> {
Map<String, Object> row = result.next();
assertEquals(
map("h2", asList(RESULT_QUERY_H2)).toString().trim(),
row.get("value").toString().trim());
assertEquals(map("h2", RESULT_QUERY_H2), row.get("value"));
assertFalse(result.hasNext());
});
});
Expand Down

0 comments on commit d141160

Please sign in to comment.