-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Produces expTemplate as a service and on the command line (closes #127)
Change-Id: I361b13ccdeb4e655474f8c4309b0a9772e31f417
- Loading branch information
Showing
4 changed files
with
163 additions
and
3 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
src/main/java/de/ids_mannheim/korap/plkexport/ExpTempl.java
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,56 @@ | ||
package de.ids_mannheim.korap.plkexport; | ||
|
||
import org.tinylog.Logger; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ArrayNode; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
|
||
|
||
public class ExpTempl { | ||
|
||
/* | ||
* Returns export template as JSON | ||
*/ | ||
public static String getExportTempl(String scheme, String host, String port){ | ||
|
||
String json = ""; | ||
try { | ||
|
||
ObjectMapper mapper = new ObjectMapper(); | ||
ObjectNode templ = mapper.createObjectNode(); | ||
|
||
templ.put("name", "Export"); | ||
templ.put("desc", "Exports Kalamar results"); | ||
|
||
ObjectNode embed = mapper.createObjectNode(); | ||
embed.put("panel", "result"); | ||
embed.put("title", "exports KWICs and snippets"); | ||
embed.put("icon", "\uf019"); | ||
|
||
ArrayNode classes = mapper.createArrayNode(); | ||
classes.add("button-icon"); | ||
classes.add("plugin"); | ||
embed.set("classes", classes); | ||
|
||
ObjectNode onClick = mapper.createObjectNode(); | ||
onClick.put("action", "addWidget"); | ||
onClick.put("template", scheme + "://" + host + ":" + port +"/export"); | ||
|
||
ArrayNode perm = mapper.createArrayNode(); | ||
perm.add("forms"); | ||
perm.add("scripts"); | ||
perm.add("downloads"); | ||
|
||
onClick.set("permissions", perm); | ||
embed.set("onClick", onClick); | ||
templ.set("embed", embed); | ||
|
||
json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(templ); | ||
|
||
} catch (Exception ex) { | ||
Logger.error(ex); | ||
return null; | ||
} | ||
return json; | ||
} | ||
} |
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
57 changes: 57 additions & 0 deletions
57
src/test/java/de/ids_mannheim/korap/plkexport/ExpTemlTest.java
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,57 @@ | ||
package de.ids_mannheim.korap.plkexport; | ||
|
||
import org.junit.Test; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
import jakarta.ws.rs.core.Response; | ||
import jakarta.ws.rs.core.Response.Status; | ||
import org.glassfish.jersey.test.JerseyTest; | ||
import org.glassfish.jersey.server.ResourceConfig; | ||
import java.util.Properties; | ||
import jakarta.ws.rs.core.Application; | ||
|
||
/** | ||
* @author Helge | ||
* | ||
* Tests if export template is correctly displayed | ||
*/ | ||
public class ExpTemlTest extends JerseyTest { | ||
|
||
@Override | ||
protected Application configure () { | ||
return new ResourceConfig(Service.class); | ||
} | ||
|
||
/* | ||
* Tests if export template is produced from service /export/template | ||
*/ | ||
@Test | ||
public void testTemplService(){ | ||
Response response = target("/export/template").request() | ||
.get(); | ||
assertEquals("Http Response should be 200:", | ||
Status.OK.getStatusCode(), response.getStatus()); | ||
String json = response.readEntity(String.class); | ||
assertTrue(json.contains("\"name\" : \"Export\"")); | ||
assertTrue(json.contains(" \"classes\" : [ \"button-icon\", \"plugin\" ]")); | ||
Properties properties = ExWSConf.properties(null); | ||
String templurl= properties.getProperty("server.scheme") + "://" + properties.getProperty("server.host") | ||
+ ":" + properties.getProperty("server.port") + "/export"; | ||
assertTrue(json.contains(templurl)); | ||
} | ||
|
||
/* | ||
* Tests if exportTemplate is returned correctly | ||
*/ | ||
@Test | ||
public void testGetTempl(){ | ||
String scheme = "httpx"; | ||
String host = "xlocalhost"; | ||
String port = "1234"; | ||
String json = ExpTempl.getExportTempl("httpx", "xlocalhost", "1234"); | ||
assertTrue(json.contains("\"desc\" : \"Exports Kalamar results\"")); | ||
assertTrue(json.contains( "\"title\" : \"exports KWICs and snippets\"")); | ||
String templ = "\"template\" : \""+ scheme +"://"+host + ":" + port + "/export\""; | ||
assertTrue(json.contains(templ)); | ||
} | ||
} |