Commit 30522f1d authored by Piotr Gawron's avatar Piotr Gawron
Browse files

documentation for plugin api calls

parent f10b492e
= Rest API Documentation - Plugins
Piotr Gawron
v{project-version} {build-time}
:toc: left
:sectnums:
== List registered plugins on the server
=== CURL sample
include::{snippets}/plugin/list_plugins/curl-request.adoc[]
=== Response Fields
include::{snippets}/plugin/list_plugins/response-fields.adoc[]
=== Sample Response
include::{snippets}/plugin/list_plugins/response-body.adoc[]
== Register new plugin on the server
=== CURL sample
include::{snippets}/plugin/create_plugin/curl-request.adoc[]
=== Request Parameters
include::{snippets}/plugin/create_plugin/request-parameters.adoc[]
=== Response Fields
include::{snippets}/plugin/create_plugin/response-fields.adoc[]
=== Sample Response
include::{snippets}/plugin/create_plugin/response-body.adoc[]
== Access information about plugin
=== CURL sample
include::{snippets}/plugin/get_plugin/curl-request.adoc[]
=== Path Parameters
include::{snippets}/plugin/get_plugin/path-parameters.adoc[]
=== Response Fields
include::{snippets}/plugin/get_plugin/response-fields.adoc[]
=== Sample Response
include::{snippets}/plugin/get_plugin/response-body.adoc[]
== Set global plugin parameter
=== CURL sample
include::{snippets}/plugin/set_plugin_data/curl-request.adoc[]
=== Path Parameters
include::{snippets}/plugin/set_plugin_data/path-parameters.adoc[]
=== Request Parameters
include::{snippets}/plugin/set_plugin_data/request-parameters.adoc[]
=== Response Fields
include::{snippets}/plugin/set_plugin_data/response-fields.adoc[]
=== Sample Response
include::{snippets}/plugin/set_plugin_data/response-body.adoc[]
== Get global plugin parameter
=== Path Parameters
include::{snippets}/plugin/get_plugin_data/path-parameters.adoc[]
=== Response Fields
include::{snippets}/plugin/get_plugin_data/response-fields.adoc[]
=== CURL sample
include::{snippets}/plugin/get_plugin_data/curl-request.adoc[]
=== Sample Response
include::{snippets}/plugin/get_plugin_data/response-body.adoc[]
== Set user plugin parameter
=== CURL sample
include::{snippets}/plugin/set_user_plugin_data/curl-request.adoc[]
=== Path Parameters
include::{snippets}/plugin/set_user_plugin_data/path-parameters.adoc[]
=== Request Parameters
include::{snippets}/plugin/set_user_plugin_data/request-parameters.adoc[]
=== Response Fields
include::{snippets}/plugin/set_user_plugin_data/response-fields.adoc[]
=== Sample Response
include::{snippets}/plugin/set_user_plugin_data/response-body.adoc[]
== Get user plugin parameter
=== Path Parameters
include::{snippets}/plugin/get_user_plugin_data/path-parameters.adoc[]
=== Response Fields
include::{snippets}/plugin/get_user_plugin_data/response-fields.adoc[]
=== CURL sample
include::{snippets}/plugin/get_user_plugin_data/curl-request.adoc[]
=== Sample Response
include::{snippets}/plugin/get_user_plugin_data/response-body.adoc[]
...@@ -2,11 +2,14 @@ package lcsb.mapviewer.web; ...@@ -2,11 +2,14 @@ package lcsb.mapviewer.web;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.*;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;
import static org.springframework.restdocs.request.RequestDocumentation.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.Arrays; import java.util.*;
import java.util.Map;
import org.apache.commons.collections4.map.HashedMap; import org.apache.commons.collections4.map.HashedMap;
import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.entity.UrlEncodedFormEntity;
...@@ -20,6 +23,8 @@ import org.junit.runner.RunWith; ...@@ -20,6 +23,8 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpSession; import org.springframework.mock.web.MockHttpSession;
import org.springframework.restdocs.payload.FieldDescriptor;
import org.springframework.restdocs.request.PathParametersSnippet;
import org.springframework.test.annotation.Rollback; import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.RequestBuilder; import org.springframework.test.web.servlet.RequestBuilder;
...@@ -59,6 +64,20 @@ public class PluginControllerIntegrationTest extends ControllerIntegrationTest { ...@@ -59,6 +64,20 @@ public class PluginControllerIntegrationTest extends ControllerIntegrationTest {
user = createUser(TEST_USER_LOGIN, TEST_USER_PASSWORD); user = createUser(TEST_USER_LOGIN, TEST_USER_PASSWORD);
} }
@Test
public void listPlugins() throws Exception {
createPlugin();
RequestBuilder request = get("/plugins/");
mockMvc.perform(request)
.andDo(document("plugin/list_plugins",
responseFields(fieldWithPath("[]")
.description("list of plugins")
.type("String")).andWithPrefix("[].", pluginResponseFields())))
.andExpect(status().is2xxSuccessful());
}
@Test @Test
public void createPublicPluginWithoutPrivileges() throws Exception { public void createPublicPluginWithoutPrivileges() throws Exception {
MockHttpSession session = createSession(TEST_USER_LOGIN, TEST_USER_PASSWORD); MockHttpSession session = createSession(TEST_USER_LOGIN, TEST_USER_PASSWORD);
...@@ -81,8 +100,7 @@ public class PluginControllerIntegrationTest extends ControllerIntegrationTest { ...@@ -81,8 +100,7 @@ public class PluginControllerIntegrationTest extends ControllerIntegrationTest {
@Test @Test
public void createPublicPluginWithPrivileges() throws Exception { public void createPublicPluginWithPrivileges() throws Exception {
MockHttpSession session = createSession(TEST_USER_LOGIN, TEST_USER_PASSWORD); MockHttpSession session = createSession(BUILT_IN_TEST_ADMIN_LOGIN, BUILT_IN_TEST_ADMIN_PASSWORD);
userService.grantUserPrivilege(user, PrivilegeType.IS_ADMIN);
String body = EntityUtils.toString(new UrlEncodedFormEntity(Arrays.asList( String body = EntityUtils.toString(new UrlEncodedFormEntity(Arrays.asList(
new BasicNameValuePair("hash", "x"), new BasicNameValuePair("hash", "x"),
...@@ -97,10 +115,67 @@ public class PluginControllerIntegrationTest extends ControllerIntegrationTest { ...@@ -97,10 +115,67 @@ public class PluginControllerIntegrationTest extends ControllerIntegrationTest {
.session(session); .session(session);
mockMvc.perform(request) mockMvc.perform(request)
.andDo(document("plugin/create_plugin",
requestParameters(
parameterWithName("hash")
.description("md5 sum of javascript file"),
parameterWithName("name")
.description("name"),
parameterWithName("version")
.description("version"),
parameterWithName("isDefault")
.optional()
.description("should be opened automatically whem map is browsed"),
parameterWithName("isPublic")
.description("should the plugin be visible by all users"),
parameterWithName("url")
.description("url to the javascript file with plugin source code")),
responseFields(pluginResponseFields())))
.andExpect(status().is2xxSuccessful()); .andExpect(status().is2xxSuccessful());
assertEquals(1, pluginDao.getAll().size()); assertEquals(1, pluginDao.getAll().size());
} }
@Test
public void getPluginInfo() throws Exception {
Plugin plugin = createPlugin();
RequestBuilder request = get("/plugins/{hash}", plugin.getHash());
mockMvc.perform(request)
.andDo(document("plugin/get_plugin",
pluginPathParameters(),
responseFields(pluginResponseFields())))
.andExpect(status().is2xxSuccessful());
}
private PathParametersSnippet pluginPathParameters() {
return pathParameters(
parameterWithName("hash")
.description("plugin md5 checksum"));
}
private List<FieldDescriptor> pluginResponseFields() {
return Arrays.asList(fieldWithPath("hash")
.description("md5 checksum of the source file")
.type("String"),
fieldWithPath("isDefault")
.description("should be opened automatically whem map is browsed")
.type("Boolean"),
fieldWithPath("isPublic")
.description("should be visible on plugin list to all users")
.type("Boolean"),
fieldWithPath("name")
.description("name")
.type("String"),
fieldWithPath("urls")
.description("list of urls where source can be found")
.type("String"),
fieldWithPath("version")
.description("plugin version")
.type("String"));
}
@Test @Test
public void createPublicPluginWithInvalidUrl() throws Exception { public void createPublicPluginWithInvalidUrl() throws Exception {
MockHttpSession session = createSession(TEST_USER_LOGIN, TEST_USER_PASSWORD); MockHttpSession session = createSession(TEST_USER_LOGIN, TEST_USER_PASSWORD);
...@@ -167,20 +242,68 @@ public class PluginControllerIntegrationTest extends ControllerIntegrationTest { ...@@ -167,20 +242,68 @@ public class PluginControllerIntegrationTest extends ControllerIntegrationTest {
String body = "value=xxx"; String body = "value=xxx";
Plugin plugin = createPlugin(); Plugin plugin = createPlugin();
RequestBuilder request = post("/plugins/" + plugin.getHash() + "/data/global/key/") RequestBuilder request = post("/plugins/{hash}/data/global/{key}/", plugin.getHash(), "my_key")
.contentType(MediaType.APPLICATION_FORM_URLENCODED) .contentType(MediaType.APPLICATION_FORM_URLENCODED)
.content(body); .content(body);
mockMvc.perform(request) mockMvc.perform(request)
.andDo(document("plugin/set_plugin_data",
pluginPathParameters().and(parameterWithName("key").description("id of the entry")),
requestParameters(parameterWithName("value").description("value of the entry")),
responseFields(fieldWithPath("key").description("id"),
fieldWithPath("value").description("value of the entry"))))
.andExpect(status().is2xxSuccessful());
request = get("/plugins/{hash}/data/global/{key}/", plugin.getHash(), "my_key");
String response = mockMvc.perform(request)
.andDo(document("plugin/get_plugin_data",
pluginPathParameters().and(parameterWithName("key").description("id of the entry")),
responseFields(fieldWithPath("key").description("id"),
fieldWithPath("value").description("value of the entry"))))
.andExpect(status().is2xxSuccessful()) .andExpect(status().is2xxSuccessful())
.andReturn().getResponse().getContentAsString(); .andReturn().getResponse().getContentAsString();
MockHttpSession session = createSession(TEST_USER_LOGIN, TEST_USER_PASSWORD); String result = new JsonParser()
request = get("/plugins/" + plugin.getHash() + "/data/global/key/") .parse(response)
.getAsJsonObject()
.get("value")
.getAsString();
assertEquals("xxx", result);
}
@Test
public void setAndGetUserPluginData() throws Exception {
MockHttpSession session = createSession(BUILT_IN_TEST_ADMIN_LOGIN, BUILT_IN_TEST_ADMIN_LOGIN);
String body = "value=xxx";
Plugin plugin = createPlugin();
RequestBuilder request = post("/plugins/{hash}/data/users/{key}/", plugin.getHash(), "my_key")
.contentType(MediaType.APPLICATION_FORM_URLENCODED) .contentType(MediaType.APPLICATION_FORM_URLENCODED)
.session(session)
.content(body);
mockMvc.perform(request)
.andDo(document("plugin/set_user_plugin_data",
pluginPathParameters().and(parameterWithName("key").description("id of the entry")),
requestParameters(parameterWithName("value").description("value of the entry")),
responseFields(fieldWithPath("key").description("id"),
fieldWithPath("value").description("value of the entry"),
fieldWithPath("user").description("owner of the entry")
)))
.andExpect(status().is2xxSuccessful());
request = get("/plugins/{hash}/data/users/{key}/", plugin.getHash(), "my_key")
.session(session); .session(session);
String response = mockMvc.perform(request) String response = mockMvc.perform(request)
.andDo(document("plugin/get_user_plugin_data",
pluginPathParameters().and(parameterWithName("key").description("id of the entry")),
responseFields(fieldWithPath("key").description("id"),
fieldWithPath("value").description("value of the entry"),
fieldWithPath("user").description("owner of the entry"))))
.andExpect(status().is2xxSuccessful()) .andExpect(status().is2xxSuccessful())
.andReturn().getResponse().getContentAsString(); .andReturn().getResponse().getContentAsString();
...@@ -207,9 +330,9 @@ public class PluginControllerIntegrationTest extends ControllerIntegrationTest { ...@@ -207,9 +330,9 @@ public class PluginControllerIntegrationTest extends ControllerIntegrationTest {
mockMvc.perform(request) mockMvc.perform(request)
.andExpect(status().is2xxSuccessful()); .andExpect(status().is2xxSuccessful());
assertNull(pluginDataEntryDao.getByKey(plugin, entry.getKey(), null)); assertNull(pluginDataEntryDao.getByKey(plugin, entry.getKey(), null));
} }
@Test @Test
...@@ -244,9 +367,10 @@ public class PluginControllerIntegrationTest extends ControllerIntegrationTest { ...@@ -244,9 +367,10 @@ public class PluginControllerIntegrationTest extends ControllerIntegrationTest {
private Plugin createPlugin() { private Plugin createPlugin() {
Plugin plugin = new Plugin(); Plugin plugin = new Plugin();
plugin.setHash("XYZ"); plugin.setHash("520e2c4c687047d78a7fd7355b9e215d");
plugin.setName("a"); plugin.setName("starter-kit");
plugin.setVersion("0.0.1"); plugin.setVersion("0.0.1");
plugin.addUrl("https://minerva-dev.lcsb.uni.lu/plugins/starter-kit/plugin.js");
pluginDao.add(plugin); pluginDao.add(plugin);
return plugin; return plugin;
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment