Commit e74d5784 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

method for updating plugin data created

parent a0e2cece
......@@ -11,8 +11,7 @@ import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import lcsb.mapviewer.api.BaseController;
import lcsb.mapviewer.api.ObjectNotFoundException;
import lcsb.mapviewer.api.*;
import lcsb.mapviewer.model.user.User;
import lcsb.mapviewer.services.interfaces.IUserService;
......@@ -46,6 +45,16 @@ public class PluginController extends BaseController {
return pluginRest.createPlugin(hash, name, version, url, isPublic, isDefault);
}
@PreAuthorize("hasAuthority('IS_ADMIN')")
@PatchMapping(value = "/{hash}")
public Map<String, Object> updatePlugin(
@PathVariable(value = "hash") String hash,
@RequestBody String body) throws QueryException {
Map<String, Object> node = parseBody(body);
Map<String, Object> data = getData(node, "plugin");
return pluginRest.updatePlugin(hash, data);
}
@GetMapping(value = "/")
public List<Map<String, Object>> getPlugins(
@RequestParam(value = "onlyPublic", defaultValue = "false") String onlyPublic) {
......
......@@ -2,12 +2,13 @@ package lcsb.mapviewer.api.plugins;
import java.util.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import lcsb.mapviewer.api.BaseRestImpl;
import lcsb.mapviewer.api.ObjectNotFoundException;
import lcsb.mapviewer.api.*;
import lcsb.mapviewer.model.plugin.Plugin;
import lcsb.mapviewer.model.plugin.PluginDataEntry;
import lcsb.mapviewer.model.user.User;
......@@ -18,6 +19,8 @@ import lcsb.mapviewer.persist.dao.plugin.PluginDataEntryDao;
@Service
public class PluginRestImpl extends BaseRestImpl {
Logger logger = LogManager.getLogger();
private PluginDao pluginDao;
private PluginDataEntryDao pluginDataEntryDao;
......@@ -143,4 +146,62 @@ public class PluginRestImpl extends BaseRestImpl {
}
return getPlugin(hash);
}
public Map<String, Object> updatePlugin(String hash, Map<String, Object> data) throws QueryException {
Plugin plugin = pluginDao.getByHash(hash);
if (plugin == null) {
throw new ObjectNotFoundException("Plugin doesn't exist");
}
if (data == null) {
throw new QueryException("plugin data is not defined");
}
Set<String> fields = data.keySet();
for (String fieldName : fields) {
Object value = data.get(fieldName);
String stringValue = null;
Integer intValue = null;
Boolean boolValue = null;
if (value instanceof String) {
stringValue = (String) value;
}
if (value instanceof Integer) {
intValue = (Integer) value;
}
if (value instanceof Boolean) {
boolValue = (Boolean) value;
}
if (fieldName.equalsIgnoreCase("isPublic")) {
plugin.setPublic(boolValue);
} else if (fieldName.equalsIgnoreCase("isDefault")) {
plugin.setDefault(boolValue);
} else if (fieldName.equalsIgnoreCase("name")) {
plugin.setName(stringValue);
} else if (fieldName.equalsIgnoreCase("version")) {
plugin.setVersion(stringValue);
} else if (fieldName.equalsIgnoreCase("hash")) {
if (!stringValue.equals(hash)) {
throw new QueryException("plugin hash cannot be changed");
}
} else if (fieldName.equalsIgnoreCase("id")) {
if (intValue != 0 && intValue != plugin.getId()) {
throw new QueryException("plugin id cannot be changed");
}
} else if (fieldName.equalsIgnoreCase("urls")) {
if (value instanceof List) {
if (((List) value).size() > 0) {
plugin.getUrls().clear();
for (Object string : (List) value) {
plugin.getUrls().add(string.toString());
}
}
}
} else {
throw new QueryException("Unknown field: " + fieldName);
}
}
pluginDao.update(plugin);
return getPlugin(hash);
}
}
......@@ -9,7 +9,6 @@ import lcsb.mapviewer.api.convert.AllConvertTests;
import lcsb.mapviewer.api.files.AllFileTests;
import lcsb.mapviewer.api.genomics.AllGenomicsTests;
import lcsb.mapviewer.api.mesh.AllMeshTests;
import lcsb.mapviewer.api.plugins.AllPluginsTests;
import lcsb.mapviewer.api.projects.AllProjectTests;
import lcsb.mapviewer.api.users.AllUserTests;
......@@ -19,7 +18,6 @@ import lcsb.mapviewer.api.users.AllUserTests;
AllFileTests.class,
AllGenomicsTests.class,
AllMeshTests.class,
AllPluginsTests.class,
AllProjectTests.class,
AllUserTests.class,
})
......
package lcsb.mapviewer.api.plugins;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({ PluginRestImplTest.class })
public class AllPluginsTests {
}
package lcsb.mapviewer.api.plugins;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import lcsb.mapviewer.api.RestTestFunctions;
public class PluginRestImplTest extends RestTestFunctions {
@Autowired
PluginRestImpl pluginRestImpl;
@Test
public void testCreatePlugin() throws Exception {
pluginRestImpl.createPlugin("x", "x", "x", "x", true);
}
@Test
public void testRemovePlugin() throws Exception {
pluginRestImpl.createPlugin("x", "x", "x", "x", true);
pluginRestImpl.removePlugin("x");
}
}
package lcsb.mapviewer.web;
import static org.junit.Assert.assertEquals;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.Arrays;
import java.util.Map;
import org.apache.commons.collections4.map.HashedMap;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
......@@ -23,6 +24,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.transaction.annotation.Transactional;
import com.google.gson.Gson;
import com.google.gson.JsonParser;
import lcsb.mapviewer.model.plugin.Plugin;
......@@ -176,4 +178,34 @@ public class PluginControllerIntegrationTest extends ControllerIntegrationTest {
return plugin;
}
@Test
public void updatePluginWithPrivileges() throws Exception {
MockHttpSession session = createSession(TEST_USER_LOGIN, TEST_USER_PASSWORD);
userService.grantUserPrivilege(user, PrivilegeType.IS_ADMIN);
Plugin plugin = createPlugin();
Plugin updatedData = new Plugin();
updatedData.setHash(plugin.getHash());
updatedData.setName("new name");
updatedData.setVersion("v0.0.13");
updatedData.setDefault(true);
Map<String, Object> bodyMap = new HashedMap<>();
bodyMap.put("plugin", updatedData);
String body = new Gson().toJson(bodyMap);
RequestBuilder request = patch("/plugins/" + plugin.getHash())
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.content(body)
.session(session);
mockMvc.perform(request)
.andExpect(status().is2xxSuccessful());
assertEquals(updatedData.getName(), plugin.getName());
assertEquals(updatedData.getVersion(), plugin.getVersion());
assertEquals(updatedData.isDefault(), plugin.isDefault());
}
}
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