-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge #2080 from branch 'rpb-141-suggest' of github.com:hbz/lobid-res…
…ources
- Loading branch information
Showing
3 changed files
with
241 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package tests; | ||
|
||
import static org.hamcrest.CoreMatchers.allOf; | ||
import static org.hamcrest.CoreMatchers.containsString; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.core.IsEqual.equalTo; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertTrue; | ||
import static play.test.Helpers.GET; | ||
import static play.test.Helpers.contentAsString; | ||
import static play.test.Helpers.fakeApplication; | ||
import static play.test.Helpers.fakeRequest; | ||
import static play.test.Helpers.route; | ||
import static play.test.Helpers.running; | ||
|
||
import org.junit.Test; | ||
|
||
import play.Application; | ||
import play.libs.Json; | ||
import play.mvc.Result; | ||
|
||
/** | ||
* Test suggestion responses (see {@link controllers.resources.Application}) | ||
*/ | ||
@SuppressWarnings("javadoc") | ||
public class SuggestionsTest extends LocalIndexSetup { | ||
|
||
@Test | ||
public void suggestionsWithoutCallback() { | ||
Application application = fakeApplication(); | ||
running(application, () -> { | ||
Result result = route(application, fakeRequest(GET, | ||
"/resources/search?q=*&filter=type:Book&format=json:title,contribution")); | ||
assertNotNull("We have a result", result); | ||
assertThat(result.contentType(), equalTo("application/json")); | ||
String content = contentAsString(result); | ||
assertNotNull("We can parse the result as JSON", Json.parse(content)); | ||
assertThat(content, | ||
allOf(// | ||
containsString("label"), // | ||
containsString("id"), // | ||
containsString("category"))); | ||
assertTrue("We used both given fields for any of the labels", | ||
Json.parse(content).findValues("label").stream() | ||
.anyMatch(label -> label.asText().contains(" | "))); | ||
}); | ||
|
||
} | ||
|
||
@Test | ||
public void suggestionsWithCallback() { | ||
Application application = fakeApplication(); | ||
running(application, () -> { | ||
Result result = route(application, fakeRequest(GET, | ||
"/resources/search?q=*&filter=type:Book&format=json:title&callback=test")); | ||
assertNotNull("We have a result", result); | ||
assertThat(result.contentType(), equalTo("application/javascript")); | ||
assertThat(contentAsString(result), | ||
allOf(containsString("test("), // callback | ||
containsString("label"), containsString("id"), | ||
containsString("category"))); | ||
}); | ||
} | ||
|
||
@Test | ||
public void suggestionsCorsHeader() { | ||
Application application = fakeApplication(); | ||
running(application, () -> { | ||
Result result = route(application, | ||
fakeRequest(GET, "/resources/search?q=*&format=json:title")); | ||
assertNotNull("We have a result", result); | ||
assertThat(result.header("Access-Control-Allow-Origin"), equalTo("*")); | ||
}); | ||
|
||
} | ||
|
||
@Test | ||
public void suggestionsTemplate() { | ||
Application application = fakeApplication(); | ||
running(application, () -> { | ||
String format = "json:title,ab_startDate+als_edition"; | ||
Result result = route(application, fakeRequest(GET, | ||
"/resources/search?q=*&filter=type:Book&format=" + format)); | ||
assertNotNull("We have a result", result); | ||
assertThat(result.contentType(), equalTo("application/json")); | ||
String content = contentAsString(result); | ||
assertNotNull("We can parse the result as JSON", Json.parse(content)); | ||
assertTrue( | ||
"We replaced the field names in the template with their values", | ||
Json.parse(content).findValues("label").stream() | ||
.anyMatch(label -> label.asText().contains("als "))); | ||
}); | ||
} | ||
|
||
@Test | ||
public void suggestionsTemplateMultiValues() { | ||
Application application = fakeApplication(); | ||
running(application, () -> { | ||
String format = "json:title,contribution,about_subject"; | ||
Result result = route(application, | ||
fakeRequest(GET, | ||
"/resources/search?q=Volksschulwesens&filter=type:Book&format=" | ||
+ format)); | ||
assertNotNull("We have a result", result); | ||
assertThat(result.contentType(), equalTo("application/json")); | ||
String content = contentAsString(result); | ||
assertNotNull("We can parse the result as JSON", Json.parse(content)); | ||
assertThat("Multi-values use consistent delimiter", content, allOf( | ||
containsString("Handwörterbuch des Volksschulwesens"), | ||
containsString("about Erziehung, Bildung, Unterricht; Volksschule"))); | ||
}); | ||
} | ||
|
||
@Test | ||
public void suggestionsArePrettyPrinted() { | ||
Application application = fakeApplication(); | ||
running(application, () -> { | ||
Result result = route(application, | ||
fakeRequest(GET, "/resources/search?q=*&format=json:suggest")); | ||
assertNotNull(result); | ||
assertThat(result.contentType(), equalTo("application/json")); | ||
assertThat(contentAsString(result), containsString("}, {\n")); | ||
}); | ||
} | ||
|
||
} |